-
Notifications
You must be signed in to change notification settings - Fork 2
/
internalAlignment.py
158 lines (108 loc) · 4.25 KB
/
internalAlignment.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
# internalAlignment.py | Aligns individual brain samples to an internal
# reference brain chosen by the user.
# 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 glob
from time import localtime, strftime
from subprocess import call
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
nii_dir = parameters[0]
reg_dir = parameters[1]
brains = parameters[2].split(",")
internal_ref = parameters[3]
reg_method = parameters[4]
log = parameters[5]
# Checking the parameters
print "The method will: "
print " - read NIfTI-1 from "+nii_dir
print " - save registration data to "+reg_dir
print "\nBrains to be processed: "
print brains
print "\nInternal reference brain: "
print internal_ref
print "\nRegistration method: "
print reg_method
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
reg_param = ""
if reg_method == "affine":
reg_param = "-i 0"
elif reg_method == "SyN":
reg_param = "-i 10x10x10"
else:
print "Unrecognised registration method."
quit()
log_file = open(log,'w')
ref_stain = nii_dir+internal_ref+"_nuclear.nii.gz"
for i in range(0,len(brains)):
output_message = strftime("%H:%M:%S", localtime())+": Starting to process brain "+brains[i]
print output_message
log_file.write(output_message+"\n")
if internal_ref == brains[i]:
output_message = "\t\t(internal reference brain, skipped)"
print output_message
log_file.write(output_message+"\n")
continue
current_stain = nii_dir+brains[i]+"_nuclear.nii.gz"
current_fluo = nii_dir+brains[i]+"_geneExp.nii.gz"
aligned_stain = nii_dir+brains[i]+"_nuclear_aligned_"+internal_ref+".nii.gz"
aligned_fluo = nii_dir+brains[i]+"_geneExp_aligned_"+internal_ref+".nii.gz"
# Registration
output_message = strftime("%H:%M:%S", localtime())+": Registration"
print output_message
log_file.write(output_message+"\n")
reg = reg_dir+"merged_reg_"+brains[i]+"_to_"+internal_ref+"_.nii"
log = reg_dir+"log_"+brains[i]+"_to_"+internal_ref+"_.txt"
cmd = "ANTS 3 "+reg_param+" -o "+reg+" --MI-option 64x300000 -m CC["+ref_stain+","+current_stain+",1,5] >> "+log
call([cmd],shell=True)
# Alignement
output_message = strftime("%H:%M:%S", localtime())+": Alignment"
print output_message
log_file.write(output_message+"\n")
if reg_method == "affine":
cmd = "WarpImageMultiTransform 3 "+current_stain+" "+aligned_stain+" -R "+ref_stain+" "+reg[:-4]+"Affine.txt"
call([cmd],shell=True)
cmd = "WarpImageMultiTransform 3 "+current_fluo+" "+aligned_fluo+" -R "+ref_stain+" "+reg[:-4]+"Affine.txt"
call([cmd],shell=True)
if reg_method == "SyN":
cmd = "WarpImageMultiTransform 3 "+current_stain+" "+aligned_stain+" -R "+ref_stain+" "+reg[:-4]+"Warp.nii "+reg[:-4]+"Affine.txt"
call([cmd],shell=True)
cmd = "WarpImageMultiTransform 3 "+current_fluo+" "+aligned_fluo+" -R "+ref_stain+" "+reg[:-4]+"Warp.nii "+reg[:-4]+"Affine.txt"
call([cmd],shell=True)
output_message = strftime("%H:%M:%S", localtime())+": Done."
print output_message
log_file.write(output_message+"\n")