-
Notifications
You must be signed in to change notification settings - Fork 2
/
exportTiffStack.py
169 lines (116 loc) · 4.19 KB
/
exportTiffStack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# exportTiffStack.py | Gives two examples of further operations:
# - one set of raw TIFF stacks is normalised using the same factors as in
# the NIfTI-1 file normalisation
# - one set of NIfTI-1 files is exported to TIFF stacks
# Please see the user manual for details.
#
# Copyright (C) 2015, Dimitri Perrin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from time import localtime, strftime
from subprocess import call
import ast
import glob
import ast
import sys
if len(sys.argv)!=2:
print "\nUsage: "+sys.argv[0]+" <parameter_file>"
quit()
# Reading the parameters
parameter_file = open(sys.argv[1],'r')
parameters = []
for line in parameter_file:
if line[0] == "#":
continue
parameters.append(line.rstrip())
parameter_file.close()
# Processing the parameters
raw_dir = parameters[0]
nii_dir = parameters[1]
out_dir = parameters[2]
brains_norm = parameters[3].split(",")
temp = parameters[4].split(",")
median = dict()
for i in range(0,len(brains_norm)):
median[brains_norm[i]] = ast.literal_eval(temp[i])
brains_conv = parameters[5].split(",")
nb_slices = ast.literal_eval(parameters[6])
log = parameters[7]
# Checking the parameters
print "The method will: "
print " - read raw files from "+raw_dir
print " - write normalised files to "+out_dir
print "\nBrains to be normalise: "
print brains_norm
print "\nMedian intensity: "
print median
print "\nBrains to be export: "
print brains_conv
print " "
while 1:
feedback = raw_input("Is this correct? (yes/no)\t").rstrip()
if feedback == "yes":
print "Program starting...\n"
break
if feedback == "no":
print "Please edit the parameter file."
quit()
# Running the analysis step
log_file = open(log,'w')
####################################
####################################
## ##
## Normalising the raw (DV) TIFF ##
## stacks for the signal channel ##
## ##
####################################
####################################
# calculating the highest value
highestMedian = 0
for el in median:
val = median[el]
if val > highestMedian:
highestMedian = val
for j in range(0,len(brains_norm)):
normalisationFactor = highestMedian/median[brains_norm[j]]
output_message = strftime("%H:%M:%S", localtime())+": Exporting results for brain "+brains_norm[j]
print output_message
log_file.write(output_message+"\n")
name_stem = raw_dir+"*"+brains_norm[j]+"_geneExp_DV"
dir_name = glob.glob(name_stem)[0]+"/"
nb_images = len(glob.glob(dir_name+"*.tif"))
for i in range(0,nb_images):
img = dir_name+"*Z%04d*.ome.tif" % (i)
cmd = "convert "+img+" -evaluate multiply "+str(normalisationFactor)+" "+out_dir+brains_norm[j]+"_geneExp_%04d.tif" % (i)
call([cmd],shell=True)
##################################
##################################
## ##
## Exporting files from ##
## from NIfTI-1 to TIFF stack ##
## ##
##################################
##################################
for b in brains_conv:
output_message = strftime("%H:%M:%S", localtime())+": Exporting from NIfTI-1 to TIFF for brain "+b
print output_message
log_file.write(output_message+"\n")
for sl in range(0,nb_slices):
b_out = b+"_%03d.tif" % (sl)
cmd = "c3d "+nii_dir+b+".nii.gz -slice z "+str(sl)+" -o "+out_dir+b_out
call([cmd],shell=True)
output_message = strftime("%H:%M:%S", localtime())+": Done."
print output_message
log_file.write(output_message+"\n")
log_file.close()