Skip to content

Commit 9c04d18

Browse files
authored
Merge pull request #3 from arnaudbore/add_bet_option
[ENH] Add bet + scilus 2.1.0 + invalid trk options
2 parents 9554d79 + 7fce37a commit 9c04d18

File tree

3 files changed

+133
-66
lines changed

3 files changed

+133
-66
lines changed

USAGE

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ register_flow pipeline
22
=====================
33

44
Register t1, metrics and tractograms to template space.
5+
If a metric name includes *mask*, it will be registered with the nearest-neighbour option.
56
We suggest to use https://github.com/scilus/combine_flows to create input.
67

78
USAGE
@@ -25,16 +26,15 @@ DESCRIPTION
2526
It has to be named template_t1.nii.gz
2627

2728
OPTIONAL ARGUMENTS (current value)
29+
--run_bet Run bet(antsBrainExtraction) if your t1s are not brain extracted ($run_bet).
2830
--linear_registration If set, it will use linear registration instead of non linear registration ($linear_registration).
2931

3032
--quick_registration If set, it will use antsRegistrationSyNQuick.sh instead of antsRegistrationSyN.sh for registration ($quick_registration).
3133

32-
--output_dir Specify a new output directory to avoid overwriting results (when dealing with multiple input labels) ($output_dir).
33-
34-
--processes_register Number of processes for registration task ($processes_register).
35-
36-
--processes The number of parallel processes to launch ($cpu_count).
37-
Only affects the local scheduler.
34+
TRK OPTIONS (current value)
35+
--trk_keep_invalid If set, it will keep invalid streamlines after registration ($trk_keep_invalid).
36+
--trk_cut_invalid If set, it will cut invalid points and may create sub-streamlines after registration ($trk_cut_invalid).
37+
--trk_remove_invalid If set, it will remove invalid streamlines after registration ($trk_remove_invalid).
3838

3939
AVAILABLE PROFILES (using -profile option (e.g. -profile fully_reproducible))
4040

main.nf

Lines changed: 119 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
#!/usr/bin/env nextflow
22

3+
params.input = false
4+
params.template = false
5+
36
if(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 ""
2080
log.info "Run Registration to template space"
21-
log.info "============================="
81+
log.info "=================================="
2282
log.info ""
2383

24-
log.info ""
2584
log.info "Start time: $workflow.start"
2685
log.info ""
2786

@@ -34,49 +93,28 @@ log.info "$workflow.repository - $workflow.revision [$workflow.commitId]"
3493
log.info ""
3594

3695
log.info "[Inputs]"
37-
log.info "Root: $params.input"
96+
log.info "Input folder: $params.input"
3897
log.info "Template: $params.template"
3998
log.info "Output directory: $params.output_dir"
4099
log.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+
42113
log.info "Number of processes per tasks"
43114
log.info "============================="
44115
log.info "Template registration: $params.processes_register"
45116
log.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-
80118
in_t1.combine(template_for_registration).set{anats_for_registration}
81119

82120
process 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

117155
Channel.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 $option ${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 $option ${tractogram} ${template} ${transfo} ${tractogram.getSimpleName()}_to_template.trk --inverse --inverse --in_deformation ${inverse_warp}
219278
"""
220279
}

nextflow.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ params {
1515
template=false
1616
output_dir="./results_registration/"
1717

18+
// **Template T1 path** //
19+
run_bet=false
20+
template_t1_for_bet="/human-data/mni_152_sym_09c/t1"
21+
22+
trk_keep_invalid = false
23+
trk_cut_invalid = false
24+
trk_remove_invalid = false
25+
1826
params.outdir_t1=[path: {"./$params.output_dir/$sid/"}]
1927
params.registration_metrics=[path: {"./$params.output_dir/$sid/Metrics_into_template_space/"}]
2028
params.registration_trks=[path: {"./$params.output_dir/$sid/Trks_into_template_space/"}]

0 commit comments

Comments
 (0)