-
Notifications
You must be signed in to change notification settings - Fork 2
/
convertTiffFiles.py
177 lines (126 loc) · 5.24 KB
/
convertTiffFiles.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
170
171
172
173
174
175
176
# convertTiffFiles.py | Converts stacks of TIFF images to NIfTI-1 3D images.
# 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/>.
import os
import glob
from time import localtime, strftime
from subprocess import call
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
initial_dir = parameters[0]
nii_dir = parameters[1]
temp_dir = parameters[2]
XY_resolution = ast.literal_eval(parameters[3])
Z_step = ast.literal_eval(parameters[4])
factor = ast.literal_eval(parameters[5])
step = ast.literal_eval(parameters[6])
scaling=str(factor*100)+"%"
direction_start = ast.literal_eval(parameters[7].split(",")[0])
direction_end = ast.literal_eval(parameters[7].split(",")[1])
brainID_start = ast.literal_eval(parameters[8].split(",")[0])
brainID_end = ast.literal_eval(parameters[8].split(",")[1])
signal_start = ast.literal_eval(parameters[9].split(",")[0])
signal_end = ast.literal_eval(parameters[9].split(",")[1])
log = parameters[10]
# Checking the parameters
print "The method will: "
print " - read data from "+initial_dir
print " - save results to "+nii_dir
print " - use "+temp_dir+" for temporary files"
print " - save the log data to "+log
print "\nThe raw image resolution is "+str(XY_resolution)+"mm in the XY direction, and "+str(Z_step)+"mm in the Z direction."
print "The NIfTI files will be downscaled to "+scaling+" in the XY direction, and by a factor of "+str(step)+" in the Z direction."
list_samples = glob.glob(initial_dir+"*")
example = list_samples[0][len(initial_dir):]
print "\nA typical file format is: "+example
print "In that file: "
print "- the acquisition direction is "+example[direction_start:direction_end]
print "- the brain ID is "+example[brainID_start:brainID_end]
print "- the signal is "+example[signal_start:signal_end]
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')
for s in list_samples:
sample_dir = s[len(initial_dir):]
direction = sample_dir[direction_start:direction_end]
brainID = sample_dir[brainID_start:brainID_end]
nbSlices = len(glob.glob(initial_dir+sample_dir+"/*"))
signal = sample_dir[signal_start:signal_end]
name = direction+"_"+brainID+"_"+signal
output_message = strftime("%H:%M:%S", localtime())+": Starting to process "+name
print output_message
log_file.write(output_message+"\n")
if len(glob.glob(nii_dir+name+".nii.gz"))>0:
output_message = strftime("%H:%M:%S", localtime())+": \tNIfTI-1 file already available. Moving on to next file."
print output_message
log_file.write(output_message+"\n")
continue
output_message = strftime("%H:%M:%S", localtime())+": \tStarting to convert Tiff files."
print output_message
log_file.write(output_message+"\n")
# Convert (and rescale) original Tiff files
for i in range(0,nbSlices,step):
slice = "%04d" % (i)
outfile = "%03d" % (i/step)
file = initial_dir+sample_dir+"/*Z"+slice+"*.ome.tif"
call(["convert",file,"-resize",scaling,"-depth","16",temp_dir+outfile+".tif"])
output_message = strftime("%H:%M:%S", localtime())+": \tDone."
print output_message
log_file.write(output_message+"\n")
# Create 3D volume as NII file
orient = " -orient RAI "
if direction == "DV":
orient = " -orient RAS "
elif direction != "VD":
output_message = "Error in direction detection?"
print output_message
log_file.write(output_message+"\n")
quit()
scale = " -spacing "+str(XY_resolution/factor)+"x"+str(XY_resolution/factor)+"x"+str(Z_step*step)+"mm "
cmd = "c3d "+temp_dir+"*.tif -tile z "+orient+scale+" -o "+nii_dir+name+".nii.gz"
output_message = strftime("%H:%M:%S", localtime())+": \tStarting construct the 3D volume."
print output_message
log_file.write(output_message+"\n")
call([cmd],shell=True)
output_message = strftime("%H:%M:%S", localtime())+": \tDone."
print output_message
log_file.write(output_message+"\n")
# Remove the temporary files
for tempFile in glob.glob(temp_dir+"*.tif"):
os.remove(tempFile)
log_file.close()
quit()