11#! /usr/bin/env nextflow
22
3+ params. input = false
4+ params. template = false
5+
36if (params. help) {
47 usage = file(" $baseDir /USAGE" )
58 cpu_count = Runtime . runtime. availableProcessors()
69
7- bindings = [" quick_registration" :" $params . quick_registration " ,
10+ bindings = [" input" :" $params . input " ,
11+ " template" :" $params . template " ,
12+ " run_bet" :" $params . run_bet " ,
13+ " quick_registration" :" $params . quick_registration " ,
814 " linear_registration" :" $params . linear_registration " ,
15+ " trk_keep_invalid" :" $params . trk_keep_invalid " ,
16+ " trk_cut_invalid" :" $params . trk_cut_invalid " ,
17+ " trk_remove_invalid" :" $params . trk_remove_invalid " ,
918 " output_dir" :" $params . output_dir " ,
1019 " processes_register" :" $params . processes_register " ,
1120 " cpu_count" :" $cpu_count " ]
@@ -17,11 +26,61 @@ if(params.help) {
1726 return
1827}
1928
29+ root = file(params. input)
30+
31+ if (! params. input){
32+ error " Error ~ Please set the input directory with --input." }
33+ else {
34+ Channel
35+ .fromPath(" $root /**/*t1.nii.gz" ,
36+ maxDepth :1 , checkIfExists : true )
37+ .map{[it. parent. name, it]}
38+ .into{in_t1; subjects_for_count}
39+
40+ in_metrics = Channel
41+ .fromFilePairs(" $root /**/metrics/*.nii.gz" ,
42+ size : -1 ,
43+ maxDepth :2 ) {it. parent. parent. name}
44+
45+ in_trks = Channel
46+ .fromFilePairs(" $root /**/tractograms/*.trk" ,
47+ size : -1 ,
48+ maxDepth :2 ) {it. parent. parent. name}
49+ }
50+
51+ if (! params. trk_cut_invalid && ! params. trk_keep_invalid && ! params. trk_remove_invalid){
52+ log. warn " No option is set to handle invalid streamlines after registration. Default is to remove them."
53+ trk_remove_invalid = true
54+ }
55+ else {
56+ trk_remove_invalid = false
57+ }
58+
59+ if ((params. trk_keep_invalid && params. trk_cut_invalid) ||
60+ (params. trk_keep_invalid && params. trk_remove_invalid) ||
61+ (params. trk_cut_invalid && params. trk_remove_invalid)){
62+ log. error " Only one option is allowed to handle invalid streamlines after registration."
63+ error " Please set only one of the following options: --trk_keep_invalid, --trk_cut_invalid, --trk_remove_invalid."
64+ }
65+
66+ subjects_for_count. count()
67+ .subscribe{a -> if (a == 0 )
68+ error " Error ~ No subjects found. Please check the naming convention, your --input path." }
69+
70+
71+ if (! params. template){
72+ error " Error ~ Please set the template with --template." }
73+ else
74+ {
75+ Channel . fromPath(file(params. template), checkIfExists : true )
76+ .into{template_for_registration;template_for_transformation_trks;template_for_transformation_metrics; template_check_name}
77+ }
78+
79+ log. info " "
2080log. info " Run Registration to template space"
21- log. info " ============================="
81+ log. info " ================================== "
2282log. info " "
2383
24- log. info " "
2584log. info " Start time: $workflow . start "
2685log. info " "
2786
@@ -34,49 +93,28 @@ log.info "$workflow.repository - $workflow.revision [$workflow.commitId]"
3493log. info " "
3594
3695log. info " [Inputs]"
37- log. info " Root : $params . input "
96+ log. info " Input folder : $params . input "
3897log. info " Template: $params . template "
3998log. info " Output directory: $params . output_dir "
4099log. info " "
41100
101+ log. info " [Parameters]"
102+ log. info " Run BET: $params . run_bet "
103+ log. info " Quick registration: $params . quick_registration "
104+ log. info " Linear registration: $params . linear_registration "
105+ log. info " "
106+
107+ log. info " [TRK Parameters]"
108+ log. info " Keep invalid: $params . trk_keep_invalid "
109+ log. info " Cut invalid: $params . trk_cut_invalid "
110+ log. info " Remove invalid: $trk_remove_invalid "
111+ log. info " "
112+
42113log. info " Number of processes per tasks"
43114log. info " ============================="
44115log. info " Template registration: $params . processes_register "
45116log. info " "
46117
47- root = file(params. input)
48- /* Watch out, files are ordered alphabetically in channel */
49- Channel
50- .fromPath(" $root /**/*t1.nii.gz" ,
51- maxDepth :1 )
52- .map{[it. parent. name, it]}
53- .into{in_t1; subjects_for_count}
54-
55- in_trks = Channel
56- .fromFilePairs(" $root /**/tractograms/*.trk" ,
57- size : -1 ,
58- maxDepth :2 ) {it. parent. parent. name}
59-
60- if (! params. template. contains(" template_t1.nii.gz" )) {
61- error " Error ~ Template filename should be named template_t1.nii.gz." }
62-
63- Channel . fromPath(file(params. template))
64- .into{template_for_registration;template_for_transformation_trks;template_for_transformation_metrics; template_check_name}
65-
66- in_metrics = Channel
67- .fromFilePairs(" $root /**/metrics/*.nii.gz" ,
68- size : -1 ,
69- maxDepth :2 ) {it. parent. parent. name}
70-
71- subjects_for_count. count()
72- .subscribe{a -> if (a == 0 )
73- error " Error ~ No subjects found. Please check the naming convention, your --input path." }
74-
75- if (! params. template){
76- error " Error ~ Please set the template to use."
77- }
78-
79-
80118in_t1. combine(template_for_registration). set{anats_for_registration}
81119
82120process Register_T1_to_template {
@@ -90,28 +128,28 @@ process Register_T1_to_template {
90128 output:
91129 set sid, " ${ sid} __output0GenericAffine.mat" , " ${ sid} __output1Warp.nii.gz" , " ${ sid} __output1InverseWarp.nii.gz" into nonlinear_transformation_for_trks, nonlinear_transformation_for_metrics optional true
92130 set sid, " ${ sid} __output0GenericAffine.mat" into linear_transformation_for_trks, linear_transformation_for_metrics optional true
93- set sid, " ${ sid} __outputWarped.nii.gz" into t1_to_template
131+ set sid, " ${ sid} __t1_template_space.nii.gz" into t1_to_template
132+ file " ${ sid} __t1_bet_mask.nii.gz" optional true
133+ file " ${ sid} __t1_bet.nii.gz" optional true
94134
95135 script:
136+ if (params. run_bet){
96137 """
97- ${ params.script_registration} -d 3 -m ${ anat} -f ${ template} -n ${ task.cpus} -o "${ sid} __output" -t ${ params.transfo}
138+ antsBrainExtraction.sh -d 3 -a ${ anat} -e $params . template_t1_for_bet /t1_template.nii.gz\
139+ -o bet/ -m $params . template_t1_for_bet /t1_brain_probability_map.nii.gz -u 0
140+ scil_volume_math.py convert bet/BrainExtractionMask.nii.gz ${ sid} __t1_bet_mask.nii.gz --data_type uint8
141+ scil_volume_math.py multiplication $t1 ${ sid} __t1_bet_mask.nii.gz ${ sid} __t1_bet.nii.gz
142+
143+ ${ params.script_registration} -d 3 -m ${ sid} __t1_bet.nii.gz -f ${ template} -n ${ task.cpus} -o "${ sid} __output" -t ${ params.transfo}
144+ mv ${ sid} __outputWarped.nii.gz ${ sid} __t1_template_space.nii.gz
98145 """
99- }
100-
101- process Get_T1_Template_Space{
102- cpus 1
103- publishDir = params. outdir_t1
104-
105- input:
106- set sid, file(t1_warped) from t1_to_template
107-
108- output:
109- file " ${ sid} __t1_to_template.nii.gz"
110-
111- script:
146+ }
147+ else {
112148 """
113- mv ${ t1_warped} ${ sid} __t1_to_template.nii.gz
149+ ${ params.script_registration} -d 3 -m ${ anat} -f ${ template} -n ${ task.cpus} -o "${ sid} __output" -t ${ params.transfo}
150+ mv ${ sid} __outputWarped.nii.gz ${ sid} __t1_template_space.nii.gz
114151 """
152+ }
115153}
116154
117155Channel . empty()
@@ -142,13 +180,15 @@ process Linear_Registration_Metrics_to_template {
142180 output:
143181 file " *_to_template.nii.gz"
144182
183+ when: params. linear_registration
184+
145185 script:
146- if (params. linear_registration)
147186 """
148- if [[ "$metric " == *"nufo"* ]]; then
149- antsApplyTransforms -d 3 -i $metric -r $template -t $transfo -o ${ metric.getSimpleName()} _to_template.nii.gz -n NearestNeighbor
187+ extract_dim=\$ (mrinfo $metric -ndim)
188+ if [[ "$metric " == *"nufo"* || "$metric " == *"mask"* ]]; then
189+ antsApplyTransforms -d \$ extract_dim -i $metric -r $template -t $transfo -o ${ metric.getSimpleName()} _to_template.nii.gz -n NearestNeighbor
150190 else
151- antsApplyTransforms -d 3 -i $metric -r $template -t $transfo -o ${ metric.getSimpleName()} _to_template.nii.gz
191+ antsApplyTransforms -d \$ extract_dim -i $metric -r $template -t $transfo -o ${ metric.getSimpleName()} _to_template.nii.gz
152192 fi
153193 """
154194}
@@ -165,7 +205,12 @@ process NonLinear_Registration_Metrics_to_template {
165205
166206 script:
167207 """
168- antsApplyTransforms -d 3 -i $metric -r $template -t $warp $transfo -o ${ metric.getSimpleName()} _to_template.nii.gz
208+ extract_dim=\$ (mrinfo $metric -ndim)
209+ if [[ "$metric " == *"nufo"* || "$metric " == *"mask"* ]]; then
210+ antsApplyTransforms -d \$ extract_dim -i $metric -r $template -t $transfo -o ${ metric.getSimpleName()} _to_template.nii.gz -n NearestNeighbor
211+ else
212+ antsApplyTransforms -d \$ extract_dim -i $metric -r $template -t $warp $transfo -o ${ metric.getSimpleName()} _to_template.nii.gz
213+ fi
169214 """
170215}
171216
@@ -198,8 +243,15 @@ process Linear_Registration_Tractograms_to_template {
198243 file " *_to_template.trk"
199244
200245 script:
246+ def option = trk_remove_invalid ? " --remove_invalid" : " "
247+ if (params. trk_cut_invalid) {
248+ option = " --cut_invalid"
249+ }
250+ else if (params. trk_keep_invalid) {
251+ option = " --keep_invalid"
252+ }
201253 """
202- scil_apply_transform_to_tractogram .py ${ tractogram} ${ template} ${ transfo} ${ tractogram.getSimpleName()} _to_template.trk --inverse
254+ scil_tractogram_apply_transform .py $o ption ${ tractogram} ${ template} ${ transfo} ${ tractogram.getSimpleName()} _to_template.trk --inverse
203255 """
204256}
205257
@@ -214,7 +266,14 @@ process NonLinear_Registration_Tractograms_to_template {
214266 file " *_to_template.trk"
215267
216268 script:
269+ def option = trk_remove_invalid ? " --remove_invalid" : " "
270+ if (params. trk_cut_invalid) {
271+ option = " --cut_invalid"
272+ }
273+ else if (params. trk_keep_invalid) {
274+ option = " --keep_invalid"
275+ }
217276 """
218- scil_apply_transform_to_tractogram .py ${ tractogram} ${ template} ${ transfo} ${ tractogram.getSimpleName()} _to_template.trk --inverse --inverse --in_deformation ${ inverse_warp}
277+ scil_tractogram_apply_transform .py $o ption ${ tractogram} ${ template} ${ transfo} ${ tractogram.getSimpleName()} _to_template.trk --inverse --inverse --in_deformation ${ inverse_warp}
219278 """
220279}
0 commit comments