-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.nf
2257 lines (1759 loc) · 73.7 KB
/
main.nf
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env nextflow
params.input = false
params.help = false
params.debug = true
if(params.help) {
usage = file("$baseDir/USAGE")
cpu_count = Runtime.runtime.availableProcessors()
bindings = ["rois_folder":"$params.rois_folder",
"filtering_lists_folder": "$params.filtering_lists_folder",
"run_bet":"$params.run_bet",
"orig":"$params.orig",
"extended":"$params.extended",
"keep_intermediate_steps":"$params.keep_intermediate_steps",
"quick_registration": "$params.quick_registration",
"cpu_count":"$cpu_count",
"processes_bet_register_t1":"$params.processes_bet_register_t1",
"processes_apply_registration":"$params.processes_apply_registration"]
engine = new groovy.text.SimpleTemplateEngine()
template = engine.createTemplate(usage.text).make(bindings)
print template.toString()
return
}
log.info "Extractor_flow pipeline"
log.info "==================="
log.info "Start time: $workflow.start"
log.info ""
workflow.onComplete {
log.info "Pipeline completed at: $workflow.complete"
log.info "Execution status: ${ workflow.success ? 'OK' : 'failed' }"
log.info "Execution duration: $workflow.duration"
}
if (!params.keep_intermediate_steps) {
log.info "Warning: You won't be able to resume your processing if you don't use the option --keep_intermediate_steps"
log.info ""
}
if (params.input){
log.info "Input: $params.input"
root = file(params.input)
in_tractogram = Channel
.fromFilePairs("$root/**/*.trk",
size:1,
maxDepth:1,
flat: true) {it.parent.name}
in_tractogram.into{check_trks;
in_tractogram_for_unplausible;
in_tractogram_for_transformation;
in_tractogram_for_mix}
Channel
.fromPath("$root/**/*_t1.nii.gz",
maxDepth:1)
.map{[it.parent.name, it]}
.into{t1s_for_register;
t1s_for_register_back;
t1s_for_copy_to_orig;
check_t1s;
t1s_empty}
}
else {
error "Error ~ Please use --input for the input data."
}
check_trks.count().into{check_subjects_number; number_subj_for_null_check}
check_t1s.count().into{number_t1s_for_compare; number_t1s_check_with_orig}
number_subj_for_null_check
.subscribe{a -> if (a == 0)
error "Error ~ No subjects found. Please check the naming convention, your --input path."}
check_subjects_number
.concat(number_t1s_for_compare)
.toList()
.subscribe{a, b -> if (a != b && b > 0)
error "Error ~ Some subjects have a T1w and others don't.\n" +
"Please be sure to have the same acquisitions for all subjects."}
if (params.orig){
number_t1s_check_with_orig
.subscribe{a -> if (a == 0)
error "Error ~ You cannot use --orig without having any T1w in the orig space."}
}
sides = params.sides?.tokenize(',')
Channel.from(sides).into{sides_ipsi;
sides_split_CC_BG;
sides_split_BG_Thal;
sides_split_BG_Put;
sides_split_BG_Caud;
side_corticopontineF;
side_corticopontinePOT;
side_cst}
/* BEGINNING TRANSFO */
process Register_T1 {
publishDir = params.final_output_mni_space
cpus params.processes_bet_register_t1
input:
set sid, file(t1) from t1s_for_register
output:
set sid, "${sid}__output0GenericAffine.mat", "${sid}__output1InverseWarp.nii.gz", "${sid}__output1Warp.nii.gz" into transformation_for_trk
file "${sid}__t1_${params.template_space}.nii.gz"
file "${sid}__t1_bet_mask.nii.gz" optional true
file "${sid}__t1_bet.nii.gz" optional true
script:
if (params.run_bet){
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=$task.cpus
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
export ANTS_RANDOM_SEED=1234
antsBrainExtraction.sh -d 3 -a $t1 -e $params.template_t1/t1_template.nii.gz\
-o bet/ -m $params.template_t1/t1_brain_probability_map.nii.gz -u 0
scil_image_math.py convert bet/BrainExtractionMask.nii.gz ${sid}__t1_bet_mask.nii.gz --data_type uint8
scil_image_math.py multiplication $t1 ${sid}__t1_bet_mask.nii.gz ${sid}__t1_bet.nii.gz
${params.registration_script} -d 3 -m ${sid}__t1_bet.nii.gz -f ${params.rois_folder}${params.atlas.template} -n ${task.cpus} -o "${sid}__output" -t s
mv ${sid}__outputWarped.nii.gz ${sid}__t1_${params.template_space}.nii.gz
"""
}
else{
"""
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=$task.cpus
export OMP_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
export ANTS_RANDOM_SEED=1234
${params.registration_script} -d 3 -m ${t1} -f ${params.rois_folder}${params.atlas.template} -n ${task.cpus} -o "${sid}__output" -t s
mv ${sid}__outputWarped.nii.gz ${sid}__t1_${params.template_space}.nii.gz
"""
}
}
transformation_for_trk.into{transformation_for_trk_registration;
transformation_for_join_with_t1}
if (params.orig) {
t1s_for_register_back
.cross(transformation_for_join_with_t1)
.map { [ it[0][0], it[0][1], it[1][1], it[1][2], it[1][3]] }
.into{transformation_and_t1_for_transformation_to_orig;
transformation_and_t1_for_transformation_to_orig_bundles}
}
transformation_for_trk_registration
.cross(in_tractogram_for_transformation)
.map { [ it[0][0], it[0][1], it[0][2], it[0][3], it[1][1] ] }
.set{trk_and_template_for_transformation_to_template}
process Transform_TRK {
publishDir = params.final_output_mni_space
cpus 1
input:
set sid, file(transfo), file(inv_deformation), file(deformation), file(trk) from trk_and_template_for_transformation_to_template
output:
set sid, "${trk.getSimpleName()}_${params.template_space}.trk" into transformed_for_remove_out_not_JHU, transformed_for_unplausible
script:
"""
scil_apply_transform_to_tractogram.py $trk ${params.rois_folder}${params.atlas.template} ${transfo} ${trk.getSimpleName()}_${params.template_space}.trk --remove_invalid --inverse --in_deformation ${inv_deformation}
"""
}
/* END TRANSFO */
for_remove_invalid_streamlines = Channel.empty()
if (t1s_empty.count().get()==0){
for_remove_invalid_streamlines = for_remove_invalid_streamlines.mix(in_tractogram_for_mix)
in_tractogram_for_unplausible.into{trk_for_extract_first_unplausible; trk_for_extract_unplausible}
}
else{
transformed_for_unplausible.into{trk_for_extract_first_unplausible; trk_for_extract_unplausible}
}
process Remove_invalid_streamlines {
cpus 1
input:
set sid, file(tractogram) from for_remove_invalid_streamlines
output:
set sid, "${sid}__rm_invalid_streamlines.trk" into rm_invalid_for_remove_out_not_JHU
script:
"""
scil_remove_invalid_streamlines.py ${tractogram} ${sid}__rm_invalid_streamlines.trk --cut_invalid --remove_single_point -f
"""
}
rm_invalid_for_remove_out_not_JHU.mix(transformed_for_remove_out_not_JHU).set{for_major_filtering}
process Major_filtering {
cpus 1
input:
set sid, file(tractogram) from for_major_filtering
output:
set sid, "${sid}/${tractogram.getSimpleName()}_filtered_filtered.trk" into wb_for_rm_end_in_cc_dwm
script:
"""
scil_filter_tractogram_anatomically.py ${tractogram} \
${params.rois_folder}${params.atlas.JHU_8} \
${sid} \
--minL ${params.min_streaminline_lenght} \
--maxL ${params.max_streaminline_lenght} \
-a ${params.loop_angle_threshold} \
--csf_bin ${params.rois_folder}${params.atlas.shell_limits} \
-f
"""
}
process Remove_ee_CC_DWM {
cpus 1
input:
set sid, file(wb_min20_noloop) from wb_for_rm_end_in_cc_dwm
output:
set sid, "${sid}__wb_clean01.trk" into wb_for_extract_end_in_cerebellum, wb_for_extract_first_unplausible
set sid, "${sid}__wb_no_In_CC_DWM.trk" optional true
file "${sid}__wb_clean01.txt" optional true
file "${sid}__wb_no_In_CC_DWM.txt" optional true
script:
"""
scil_filter_tractogram.py ${wb_min20_noloop} ${sid}__wb_clean01.trk \
--drawn_roi ${params.rois_folder}${params.atlas.cc} either_end exclude \
--drawn_roi ${params.rois_folder}${params.atlas.dwm} either_end exclude \
-f --display_count > ${sid}__wb_clean01.txt
if ($params.keep_intermediate_steps)
then
scil_streamlines_math.py difference ${wb_min20_noloop} ${sid}__wb_clean01.trk ${sid}__wb_no_In_CC_DWM.trk -f
scil_count_streamlines.py ${sid}__wb_no_In_CC_DWM.trk > ${sid}__wb_no_In_CC_DWM.txt
fi
"""
}
trk_for_extract_first_unplausible.join(wb_for_extract_first_unplausible).set{unplausible_streamlines}
process Extract_first_unplausible{
cpus 1
input:
set sid, file(tractogram1), file(tractogram2) from unplausible_streamlines
output:
set sid, "${sid}__unplausible_streamlines.trk" into unplausible_for_fornix
script:
"""
scil_streamlines_math.py difference ${tractogram1} \
${tractogram2} \
${sid}__unplausible_streamlines.trk;
"""
}
process Extract_fornix{
cpus 1
input:
set sid, file(tractogram) from unplausible_for_fornix
output:
set sid, "${sid}__fornix_f.trk" into fornix_for_trk_plausible, fornix_for_rename
file "${sid}__fornix_f.txt"
script:
filtering_list=params.filtering_lists_folder+"fornix_filtering_list.txt"
out_extension="fornix_f"
remaining_extension="unplausible_wo_fornix"
basename="${sid}"
keep="$params.keep_intermediate_steps"
extract_masks=""
template "filter_with_list.sh"
}
process Extract_ee_cerebellum {
cpus 1
input:
set sid, file(tractogram) from wb_for_extract_end_in_cerebellum
output:
set sid, "${sid}__wb_clean01_nocereb.trk" into wb_for_extract_end_in_brainstem
set sid, "${sid}__all_cerebellum.trk" into ee_cerebellum_for_extract_plausible
file "${sid}__all_cerebellum.txt" optional true
file "${sid}__wb_clean01_nocereb.txt" optional true
script:
atlas=params.rois_folder+params.atlas.cerebellum
mode=params.mode.either_end
criteria=params.criteria.exclude
out_extension='wb_clean01_nocereb'
remaining_extension='all_cerebellum'
basename="${sid}"
keep=true
template "filter_with_atlas.sh"
}
process Extract_plausible_cerebellum {
cpus 1
input:
set sid, file(tractogram) from ee_cerebellum_for_extract_plausible
output:
set sid, "${sid}__all_cerebellum_plausibles.trk" into cerebellum_for_trk_plausible, cerebellum_for_rename
file "${sid}__all_in_cerebellum_nocx_nocerebwm.trk"
file "${sid}__all_in_cerebellum_in_Medulla.trk"
file "${sid}__all_in_cerebellum_in_Pons.trk"
file "${sid}__all_in_cerebellum_in_Midbrain.trk"
file "${sid}__all_in_cerebellum_in_redN_and_Thal.trk"
script:
"""
scil_filter_tractogram.py ${tractogram} ${sid}__all_in_cerebellum_nocx_nocerebwm.trk --filtering_list /filtering_lists/list_cerebellum/cerebellum_nocx_in_cereb_filtering_list.txt -f
scil_filter_tractogram.py ${tractogram} ${sid}__all_in_cerebellum_in_Medulla.trk --filtering_list /filtering_lists/list_cerebellum/cerebellum_in_medulla_filtering_list.txt -f
scil_filter_tractogram.py ${tractogram} ${sid}__all_in_cerebellum_in_Pons.trk --filtering_list /filtering_lists/list_cerebellum/cerebellum_in_pons_filtering_list.txt -f
scil_filter_tractogram.py ${tractogram} ${sid}__all_in_cerebellum_in_Midbrain.trk --filtering_list /filtering_lists/list_cerebellum/cerebellum_in_midbrain_filtering_list.txt -f
scil_filter_tractogram.py ${tractogram} ${sid}__all_in_cerebellum_in_redN_and_Thal.trk --filtering_list /filtering_lists/list_cerebellum/cerebellum_in_rednucleus_and_thalamus_filtering_list.txt -f
scil_streamlines_math.py concatenate ${sid}__all_in_*.trk ${sid}__all_cerebellum_plausibles.trk -f
if ${params.keep_intermediate_steps}
then
scil_streamlines_math.py difference ${sid}__all_cerebellum.trk ${sid}__all_cerebellum_plausibles.trk ${sid}__all_cerebellum_unplausibles.trk -f
fi
"""
}
/*
END Cerebellum
*/
process Extract_ee_brainstem {
cpus 1
input:
set sid, file(tractogram) from wb_for_extract_end_in_brainstem
output:
set sid, "${sid}__wb_clean02.trk" into wb_for_split_end_in_CGMSWI
set sid, "${sid}__all_brainstem.trk" into all_brainstem_for_extract_plausible
file "${sid}__wb_clean02.txt" optional true
file "${sid}__all_brainstem.txt" optional true
script:
atlas=params.rois_folder+params.atlas.brainstem
mode=params.mode.either_end
criteria=params.criteria.exclude
out_extension='wb_clean02'
remaining_extension='all_brainstem'
basename="${sid}"
keep=true
template "filter_with_atlas.sh"
}
/*
Brainstem
*/
process Extract_plausible_brainstem {
cpus 1
input:
set sid, file(tractogram) from all_brainstem_for_extract_plausible
output:
set sid, "${sid}__all_brainstem_plausibles.trk" into brainstem_for_trk_plausible, brainstem_for_rename
file "${sid}__all_brainstem_unplausibles.trk" optional true
file "${sid}__be_midbrain.trk"
file "${sid}__be_medulla.trk"
file "${sid}__be_pons.trk"
file "${sid}__ee_thalamus.trk"
file "${sid}__ee_red_nucleus.trk"
set sid, "${sid}__ee_fronto_pontine.trk" into brainstem_corticopontine_frontal_for_rename
set sid, "${sid}__ee_parietotemporooccipital_pontine.trk" into brainstem_ee_corticopontine_parietotemporooccipital_for_rename
set sid, "${sid}__ee_pyramidal.trk" into brainstem_pyramidal_for_rename
file "${sid}__ee_cortico_tectal.trk"
script:
"""
# Extract be midbrain
scil_filter_tractogram.py ${sid}__all_brainstem.trk ${sid}__be_midbrain.trk --filtering_list /filtering_lists/list_brainstem/brainstem_be_midbrain_filtering_list.txt -f
# Extract be medulla
scil_filter_tractogram.py ${sid}__all_brainstem.trk ${sid}__be_medulla.trk --filtering_list /filtering_lists/list_brainstem/brainstem_be_medulla_filtering_list.txt -f
# Extract be pons
scil_filter_tractogram.py ${sid}__all_brainstem.trk ${sid}__be_pons.trk --filtering_list /filtering_lists/list_brainstem/brainstem_be_pons_filtering_list.txt -f
# Extract ee thalamus
scil_filter_tractogram.py ${sid}__all_brainstem.trk ${sid}__ee_thalamus.trk --filtering_list /filtering_lists/list_brainstem/brainstem_ee_thalamus_filtering_list.txt -f
# Extract ee red_nucleus
scil_filter_tractogram.py ${sid}__all_brainstem.trk ${sid}__ee_red_nucleus.trk --filtering_list /filtering_lists/list_brainstem/brainstem_ee_red_nucleus_filtering_list.txt -f
# Prepartion for fronto-pontine, parietotemporooccipito-pontine, pyramidal, cortico-tectal
scil_filter_tractogram.py ${sid}__all_brainstem.trk ${sid}__ee_tmp_01.trk --filtering_list /filtering_lists/list_brainstem/brainstem_ee_tmp_01_filtering_list.txt -f
scil_filter_tractogram.py ${sid}__all_brainstem.trk ${sid}__ee_tmp_02.trk --filtering_list /filtering_lists/list_brainstem/brainstem_ee_tmp_02_filtering_list.txt -f
scil_streamlines_math.py concatenate ${sid}__ee_tmp_01.trk ${sid}__ee_tmp_02.trk ${sid}__ee_tmp_03.trk -f
# Extract ee Fronto-pontine
scil_filter_tractogram.py ${sid}__ee_tmp_03.trk ${sid}__ee_fronto_pontine.trk --filtering_list /filtering_lists/list_brainstem/brainstem_ee_F_pontine_filtering_list.txt -f
# Extract ee ParietoTemporooccipital pontine
scil_filter_tractogram.py ${sid}__ee_tmp_03.trk ${sid}__ee_parietotemporooccipital_pontine.trk --filtering_list /filtering_lists/list_brainstem/brainstem_ee_PTO_pontine_filtering_list.txt -f
# Extract ee Pyramidal
scil_filter_tractogram.py ${sid}__ee_tmp_03.trk ${sid}__ee_pyramidal.trk --filtering_list /filtering_lists/list_brainstem/brainstem_ee_pyramidal_filtering_list.txt -f
# Extract ee Tectal
scil_filter_tractogram.py ${sid}__ee_tmp_03.trk ${sid}__ee_cortico_tectal.trk --filtering_list /filtering_lists/list_brainstem/brainstem_ee_cortico_tectal_filtering_list.txt -f
scil_filter_streamlines_by_length.py ${sid}__ee_cortico_tectal.trk ${sid}__ee_cortico_tectal.trk --maxL 100 -f
rm -f ${sid}__*tmp_*.trk
scil_streamlines_math.py concatenate ${sid}__be_*.trk ${sid}__ee_*.trk ${sid}__all_brainstem_plausibles.trk -f
if ${params.keep_intermediate_steps}
then
scil_streamlines_math.py difference ${sid}__all_brainstem.trk ${sid}__all_brainstem_plausibles.trk ${sid}__all_brainstem_unplausibles.trk -f
fi
"""
}
/*
Brain - Either end in CGM SWM
*/
process Remove_out_of_CGM_DWM {
cpus 1
input:
set sid, file(tractogram) from wb_for_split_end_in_CGMSWI
output:
set sid, "${sid}__wb_either_CGM_SWM.trk" into wb_for_extract_all_commissural
set sid, "${sid}__no_CGM_SWM.trk" optional true
file "${sid}__wb_either_CGM_SWM.txt" optional true
file "${sid}__no_CGM_SWM.txt" optional true
script:
atlas=params.rois_folder+params.atlas.CGM_SWM
mode=params.mode.either_end
criteria=params.criteria.include
out_extension='wb_either_CGM_SWM'
remaining_extension='no_CGM_SWM'
basename="${sid}"
keep="$params.keep_intermediate_steps"
template "filter_with_atlas.sh"
}
process Extract_all_commissural {
cpus 1
input:
set sid, file(tractogram) from wb_for_extract_all_commissural
output:
set sid, "${sid}__tmp_CC.trk" into cc_for_extract_CC_Cx, cc_for_extract_AC_Cx, cc_for_extract_CC_BG
set sid, "${sid}__wb_either_CGM_SWM_noCC.trk" into no_cc_for_split_asso_BG
file "${sid}__wb_either_CGM_SWM_noCC.txt" optional true
file "${sid}__tmp_CC.txt" optional true
script:
atlas=params.rois_folder+params.atlas.midline
mode=params.mode.any
criteria=params.criteria.exclude
out_extension="wb_either_CGM_SWM_noCC"
remaining_extension='tmp_CC'
basename="${sid}"
keep=true
template "filter_with_atlas.sh"
}
process Extract_plausible_CC_Cx {
cpus 1
input:
set sid, file(tractogram) from cc_for_extract_CC_Cx
output:
set sid, "${sid}__in_CC_Cx.trk" into cc_for_merge_plausible_01
file "mask_atlas_roi_*.nii.gz" optional true
script:
filtering_list=params.filtering_lists_folder+"CC_Cx_filtering_list.txt"
out_extension="in_CC_Cx"
remaining_extension="garbage"
basename="${sid}"
keep="$params.keep_intermediate_steps"
extract_masks=""
template "filter_with_list.sh"
}
process Extract_plausible_AC_Cx {
cpus 1
input:
set sid, file(tractogram) from cc_for_extract_AC_Cx
output:
set sid, "${sid}__in_AC_Cx.trk" into accx_for_trk_plausible, accx_for_rename
script:
filtering_list=params.filtering_lists_folder+"AC_Cx_filtering_list.txt"
out_extension="in_AC_Cx"
remaining_extension="garbage"
basename="${sid}"
keep="$params.keep_intermediate_steps"
extract_masks=""
template "filter_with_list.sh"
}
process Extract_plausible_CC_BG {
cpus 1
input:
set sid, file(tractogram) from cc_for_extract_CC_BG
output:
set sid, "${sid}__in_CC_BG_f.trk" into ccbg_for_trk_plausible
script:
filtering_list=params.filtering_lists_folder+"CC_BG_filtering_list.txt"
out_extension="in_CC_BG_f"
remaining_extension="garbage"
basename="${sid}"
keep="$params.keep_intermediate_steps"
extract_masks=""
template "filter_with_list.sh"
}
/*
Split not CC in asso BG and not BG
*/
process Split_no_CC_Asso_and_BG {
cpus 1
input:
set sid, file(tractogram) from no_cc_for_split_asso_BG
output:
set sid, "${sid}__all_subcortical_from_CGM_SWM_noCC_f.trk" into asso_BG_for_split_Thal, asso_BG_for_split_Put, asso_BG_for_split_Caud
file "${sid}__all_subcortical_from_CGM_SWM_noCC_f.txt" optional true
set sid, "${sid}__asso_noBG.trk" into asso_noBG_for_split_hemi
file "${sid}__asso_noBG.txt" optional true
script:
atlas=params.rois_folder+params.atlas.allsubcortical
mode=params.mode.either_end
criteria=params.criteria.include
out_extension="all_subcortical_from_CGM_SWM_noCC_f"
remaining_extension='asso_noBG'
basename="${sid}"
keep=true
template "filter_with_atlas.sh"
}
bg_list=params.bg_lists?.tokenize(',')
Channel.from(bg_list).into{bg_thal_list;
bg_put_list}
/*
BG THAL
*/
process Split_BG_Thal {
cpus 1
input:
set sid, file(tractogram) from asso_BG_for_split_Thal
each list from bg_thal_list
each side from sides_split_BG_Thal
output:
set sid, "${sid}__BG_ipsi_Thal_${list}_${side}.trk" into BG_ipsi_Thal_for_merge
set sid, val(side), "${sid}__BG_ipsi_Thal_${list}_${side}.trk" into BG_ipsi_Thal_split_for_merge
set sid, val(side), val(list), "${sid}__BG_ipsi_Thal_${list}_${side}.trk" into BG_ipsi_Thal_for_filter_CuGWM, BG_ipsi_Thal_for_filter_LGWM
script:
filtering_list=params.filtering_lists_folder+"BG_ipsi_Thal_${list}_${side}_f.txt"
out_extension="BG_ipsi_Thal_${list}_${side}"
remaining_extension="garbage_BG_ipsi_Thal_${list}_${side}"
basename="${sid}"
keep="$params.keep_intermediate_steps"
extract_masks=""
template "filter_with_list.sh"
}
BG_ipsi_Thal_split_for_merge.groupTuple(by:[0,1]).set{BG_ipsi_Thal_for_rename}
BG_ipsi_Thal_for_filter_CuGWM.filter{it[2]=='CuGWM'}.set{CuGWM_for_combine}
BG_ipsi_Thal_for_filter_LGWM.filter{it[2]=='LGWM'}.set{LGWM_for_combine}
CuGWM_for_combine.concat(LGWM_for_combine).groupTuple(by:[0,1]).set{optic_radiation_for_rename}
BG_ipsi_Thal_for_merge.groupTuple().map{it}.set{BG_ipsi_Thal_list_for_merge}
process Merge_BG_Thal{
cpus 1
input:
set sid, file(tractogram) from BG_ipsi_Thal_list_for_merge
output:
set sid, "${sid}__BG_ipsi_Thal_all.trk" into BG_ipsi_Thal_for_trk_plausible
script:
"""
scil_streamlines_math.py concatenate ${tractogram} ${sid}__BG_ipsi_Thal_all.trk -f
"""
}
/*
BG PUT
*/
process Split_BG_Put {
cpus 1
input:
set sid, file(tractogram) from asso_BG_for_split_Put
each list from bg_put_list
each side from sides_split_BG_Put
output:
set sid, "${sid}__BG_ipsi_Put_${list}_${side}.trk" into BG_ipsi_Put_for_merge
set sid, val(side), "${sid}__BG_ipsi_Put_${list}_${side}.trk" into BG_ipsi_Put_for_rename
script:
filtering_list=params.filtering_lists_folder+"BG_ipsi_Put_${list}_${side}_f.txt"
out_extension="BG_ipsi_Put_${list}_${side}"
remaining_extension="garbage_BG_ipsi_Put_${list}_${side}"
basename="${sid}"
keep="true"
extract_masks=""
template "filter_with_list.sh"
}
BG_ipsi_Put_for_merge.groupTuple().map{it}.set{BG_ipsi_Put_list_for_merge}
process Merge_BG_Put{
cpus 1
input:
set sid, file(tractogram) from BG_ipsi_Put_list_for_merge
output:
set sid, "${sid}__BG_ipsi_Put_all.trk" into BG_ipsi_Put_for_trk_plausible
script:
"""
scil_streamlines_math.py concatenate ${tractogram} ${sid}__BG_ipsi_Put_all.trk -f
"""
}
/*
BG CAUD
*/
bg_caud_list=params.bg_caud_lists?.tokenize(',')
process Split_BG_Caud {
cpus 1
input:
set sid, file(tractogram) from asso_BG_for_split_Caud
each list from bg_caud_list
each side from sides_split_BG_Caud
output:
set sid, "${sid}__BG_ipsi_Caud_${list}_${side}.trk" into BG_ipsi_Caud_for_merge
set sid, val(side), "${sid}__BG_ipsi_Caud_${list}_${side}.trk" into BG_ipsi_Caud_for_rename
script:
filtering_list=params.filtering_lists_folder+"BG_ipsi_Caud_${list}_${side}_f.txt"
out_extension="BG_ipsi_Caud_${list}_${side}"
remaining_extension="garbage_BG_ipsi_Caud_${list}_${side}"
basename="${sid}"
keep="true"
extract_masks=""
template "filter_with_list.sh"
}
BG_ipsi_Caud_for_merge.groupTuple().map{it}.set{BG_ipsi_Caud_list_for_merge}
process Merge_BG_Caud{
cpus 1
input:
set sid, file(tractogram) from BG_ipsi_Caud_list_for_merge
output:
set sid, "${sid}__BG_ipsi_Caud_all.trk" into BG_ipsi_Caud_for_trk_plausible
script:
"""
scil_streamlines_math.py concatenate ${tractogram} ${sid}__BG_ipsi_Caud_all.trk -f
"""
}
process Split_asso_in_hemi {
cpus 1
input:
set sid, file(tractogram) from asso_noBG_for_split_hemi
each side from sides
output:
set sid, val(side), "${sid}__asso_${side}.trk" into asso_for_extract_u_shape
set sid, "${sid}__asso_${side}_lost.trk" optional true
file "${sid}__asso_${side}.txt" optional true
file "${sid}__asso_${side}_lost.txt" optional true
script:
if (side=='L')
atlas=params.rois_folder+params.atlas.all+"_R.nii.gz"
else
atlas=params.rois_folder+params.atlas.all+"_L.nii.gz"
mode=params.mode.any
criteria=params.criteria.exclude
out_extension="asso_${side}"
remaining_extension="asso_${side}_lost"
basename="${sid}"
keep=true
template "filter_with_atlas.sh"
}
/*
Extracting U-shaped and streamlines restricted to Cortical GM and removing them from asso
*/
process Split_ushape_CGM_asso {
cpus 1
input:
set sid, val(side), file(tractogram) from asso_for_extract_u_shape
output:
set sid, val(side), "${sid}__asso_only_in_CGM_${side}.trk" into assoCGM
set sid, val(side), "${sid}__asso_Ushape_${side}.trk" into assoUShape
set sid, "${sid}__asso_Ushape_${side}_u.trk" into asso_u_shape_for_trk_plausible
set sid, val(side), "${sid}__asso_Ushape_${side}_u.trk" into asso_u_shape_for_rename
set sid, val(side), "${sid}__asso_f_${side}.trk" into asso_for_remove_long_range
file "${sid}__asso_only_in_CGM_${side}.txt" optional true
file "${sid}__asso_Ushape_${side}.txt" optional true
file "${sid}__asso_f_${side}.txt" optional true
script:
"""
scil_filter_tractogram.py ${tractogram} ${sid}__tmp1_${side}.trk \
--drawn_roi ${params.rois_folder}${params.atlas.CGM}_${side}.nii.gz ${params.mode.both_ends} include -f
scil_streamlines_math.py difference ${tractogram} ${sid}__tmp1_${side}.trk \
${sid}__asso_SWM_${side}.trk -f
scil_filter_tractogram.py ${sid}__tmp1_${side}.trk ${sid}__asso_only_in_CGM_${side}.trk \
--drawn_roi ${params.rois_folder}${params.atlas.SWM}_${side}.nii.gz ${params.mode.any} exclude
scil_streamlines_math.py difference ${sid}__tmp1_${side}.trk ${sid}__asso_only_in_CGM_${side}.trk \
${sid}__tmp2_${side}.trk -f
scil_filter_tractogram.py ${sid}__tmp2_${side}.trk ${sid}__asso_Ushape_${side}.trk \
--drawn_roi ${params.rois_folder}${params.atlas.DWM}_${side}.nii.gz ${params.mode.any} exclude
scil_extract_ushape.py ${sid}__asso_Ushape_${side}.trk --minU 0.5 --maxU 1 ${sid}__asso_Ushape_${side}_u.trk -f
scil_streamlines_math.py difference ${sid}__tmp2_${side}.trk ${sid}__asso_Ushape_${side}.trk \
${sid}__asso_DWM_${side}.trk -f
scil_streamlines_math.py concatenate ${sid}__asso_DWM_${side}.trk ${sid}__asso_SWM_${side}.trk ${sid}__asso_f_${side}.trk -f
if ${params.keep_intermediate_steps}
then
scil_count_streamlines.py ${sid}__asso_only_in_CGM_${side}.trk > ${sid}__asso_only_in_CGM_${side}.txt
scil_count_streamlines.py ${sid}__asso_Ushape_${side}.trk > ${sid}__asso_Ushape_${side}.txt
scil_count_streamlines.py ${sid}__asso_f_${side}.trk > ${sid}__asso_f_${side}.txt
fi
"""
}
/*
Extracting unplausible long-range association streamlines passing through subcortical structures (Cd, Put, GP, Thal, Amyg)
*/
process Remove_Unplausible_Long_Range_Asso {
cpus 1
input:
set sid, val(side), file(tractogram) from asso_for_remove_long_range
output:
set sid, val(side), "${sid}__asso_all_intra_inter_${side}.trk" into asso_all_intra_inter
set sid, "${sid}__asso_lost2_${side}.trk" optional true
file "${sid}__asso_all_intra_inter_${side}.txt" optional true
file "${sid}__asso_lost2_${side}.txt" optional true
script:
atlas=params.rois_folder+params.atlas.allsubcortical
mode="any"
criteria="exclude"
out_extension="asso_all_intra_inter_${side}"
remaining_extension="asso_lost2_${side}"
basename="${sid}"
keep="$params.keep_intermediate_steps"
template "filter_with_atlas.sh"
}
/*
inCCBG.groupTuple().map{it.flatten().toList()}.set{inCCBG_List}
assoUShape.groupTuple().map{it.flatten().toList()}.set{assoUShape_list}
*/
asso_all_intra_inter.into{asso_all_intra_inter_for_ventral_f_o_f_p_filtering;
asso_all_intra_inter_for_ventral_f_t_filtering;
asso_all_intra_inter_for_dorsal_f_o_f_t_filtering;
asso_all_intra_inter_for_dorsal_f_p_filtering;
asso_all_intra_inter_for_p_o_filtering;
asso_all_intra_inter_for_p_t_filtering;
asso_all_intra_inter_for_o_t_filtering;
asso_all_intra_inter_for_ins_filtering;
asso_all_intra_inter_for_cing_filtering;
asso_all_intra_inter_for_be_frontal_filtering;
asso_all_intra_inter_for_ee_frontal_filtering;
asso_all_intra_inter_for_be_occipital_filtering;
asso_all_intra_inter_for_ee_occipital_filtering;
asso_all_intra_inter_for_be_parietal_filtering;
asso_all_intra_inter_for_ee_parietal_filtering;
asso_all_intra_inter_for_be_temporal_filtering;
asso_all_intra_inter_for_ee_temporal_filtering
asso_all_intra_inter_plausible}
asso_all_intra_inter_plausible.groupTuple().map{it.flatten().toList()}.set{asso_all_intra_inter_list}
cc_for_merge_plausible_01.into{ccCleanedPlausible; CC_for_homotopic}
assoCGM.groupTuple().map{it.flatten().toList()}.set{assoCGM_list}
/*
CC Homotopic
*/
cc_homotopic_pairs=params.cc_homotopic_pairs?.tokenize(',')
process CC_Homotopic {
cpus 1
input:
set sid, file(tractogram) from CC_for_homotopic
each pair from cc_homotopic_pairs
output:
set sid, "${sid}__cc_homotopic_${pair}.trk" into CC_Homotopic_for_merge
set sid, val(pair), "${sid}__cc_homotopic_${pair}.trk" into CC_Homotopic_for_filter_AGWM, CC_Homotopic_for_filter_CingGWM, CC_Homotopic_for_filter_CuGWM, CC_Homotopic_for_filter_FuGWM, CC_Homotopic_for_filter_Hippo, CC_Homotopic_for_filter_IFGWM, CC_Homotopic_for_filter_Ins, CC_Homotopic_for_filter_IOGWM, CC_Homotopic_for_filter_ITGWM, CC_Homotopic_for_filter_LFOGWM, CC_Homotopic_for_filter_LGWM, CC_Homotopic_for_filter_MFGWM, CC_Homotopic_for_filter_MFOGWM, CC_Homotopic_for_filter_MOGWM, CC_Homotopic_for_filter_MTGWM, CC_Homotopic_for_filter_PHG, CC_Homotopic_for_filter_PoCGWM, CC_Homotopic_for_filter_PrCGWM, CC_Homotopic_for_filter_PrCuGWM, CC_Homotopic_for_filter_RGGWM, CC_Homotopic_for_filter_SFGWM, CC_Homotopic_for_filter_SMGWM, CC_Homotopic_for_filter_SOGWM, CC_Homotopic_for_filter_SPGWM, CC_Homotopic_for_filter_STGWM, CC_Homotopic_for_filter_T_pole_gwm
script:
filtering_list=params.filtering_lists_folder+"CC_homo_${pair}_filtering_list_f.txt"
out_extension="cc_homotopic_${pair}"
remaining_extension="garbage_${pair}"
basename="${sid}"
keep="$params.keep_intermediate_steps"
extract_masks=""
template "filter_with_list.sh"
}
/*
Filter + Concat frontal
*/
CC_Homotopic_for_filter_IFGWM.filter{it[1]=='IFGWM'}.set{CC_IFGWM_for_combine_frontal}
CC_Homotopic_for_filter_SFGWM.filter{it[1]=='SFGWM'}.set{CC_SFGWM_for_combine_frontal}
CC_Homotopic_for_filter_MFGWM.filter{it[1]=='MFGWM'}.set{CC_MFGWM_for_combine_frontal}
CC_Homotopic_for_filter_MFOGWM.filter{it[1]=='MFOGWM'}.set{CC_MFOGWM_for_combine_frontal}
CC_Homotopic_for_filter_LFOGWM.filter{it[1]=='LFOGWM'}.set{CC_LFOGWM_for_combine_frontal}
CC_Homotopic_for_filter_PrCGWM.filter{it[1]=='PrCGWM'}.set{CC_PrCGWM_for_combine_frontal}
CC_Homotopic_for_filter_RGGWM.filter{it[1]=='RGGWM'}.set{CC_RGGWM_for_combine_frontal}
CC_IFGWM_for_combine_frontal.concat(CC_SFGWM_for_combine_frontal).concat(CC_MFGWM_for_combine_frontal).concat(CC_MFOGWM_for_combine_frontal).concat(CC_LFOGWM_for_combine_frontal).concat(CC_PrCGWM_for_combine_frontal).concat(CC_RGGWM_for_combine_frontal).groupTuple(by:0).set{CC_Homotopic_frontal_for_rename}
/*
Filter + Concat occipital
*/
CC_Homotopic_for_filter_SOGWM.filter{it[1]=='SOGWM'}.set{CC_SOGWM_for_combine_occipital}
CC_Homotopic_for_filter_MOGWM.filter{it[1]=='MOGWM'}.set{CC_MOGWM_for_combine_occipital}
CC_Homotopic_for_filter_IOGWM.filter{it[1]=='IOGWM'}.set{CC_IOGWM_for_combine_occipital}
CC_Homotopic_for_filter_CuGWM.filter{it[1]=='CuGWM'}.set{CC_CuGWM_for_combine_occipital}
CC_Homotopic_for_filter_LGWM.filter{it[1]=='LGWM'}.set{CC_LGWM_for_combine_occipital}
CC_SOGWM_for_combine_occipital.concat(CC_MOGWM_for_combine_occipital).concat(CC_IOGWM_for_combine_occipital).concat(CC_CuGWM_for_combine_occipital).concat(CC_LGWM_for_combine_occipital).groupTuple(by:0).set{CC_Homotopic_occipital_for_rename}
/*
Filter + Concat temporal
*/
CC_Homotopic_for_filter_STGWM.filter{it[1]=='STGWM'}.set{CC_STGWM_for_combine_temporal}
CC_Homotopic_for_filter_T_pole_gwm.filter{it[1]=='T_pole_gwm'}.set{CC_T_pole_gwm_for_combine_temporal}
CC_Homotopic_for_filter_MTGWM.filter{it[1]=='MTGWM'}.set{CC_MTGWM_for_combine_temporal}
CC_Homotopic_for_filter_ITGWM.filter{it[1]=='ITGWM'}.set{CC_ITGWM_for_combine_temporal}
CC_Homotopic_for_filter_PHG.filter{it[1]=='PHG'}.set{CC_PHG_for_combine_temporal}
CC_Homotopic_for_filter_Hippo.filter{it[1]=='Hippo'}.set{CC_Hippo_for_combine_temporal}
CC_Homotopic_for_filter_FuGWM.filter{it[1]=='FuGWM'}.set{CC_FuGWM_for_combine_temporal}
CC_STGWM_for_combine_temporal.concat(CC_T_pole_gwm_for_combine_temporal).concat(CC_MTGWM_for_combine_temporal).concat(CC_ITGWM_for_combine_temporal).concat(CC_PHG_for_combine_temporal).concat(CC_Hippo_for_combine_temporal).concat(CC_FuGWM_for_combine_temporal).groupTuple(by:0).set{CC_Homotopic_temporal_for_rename}
/*
Filter + Concat parietal
*/
CC_Homotopic_for_filter_SPGWM.filter{it[1]=='SPGWM'}.set{CC_SPGWM_for_combine_parietal}
CC_Homotopic_for_filter_SMGWM.filter{it[1]=='SMGWM'}.set{CC_SMGWM_for_combine_parietal}
CC_Homotopic_for_filter_PrCuGWM.filter{it[1]=='PrCuGWM'}.set{CC_PrCuGWM_for_combine_parietal}
CC_Homotopic_for_filter_PoCGWM.filter{it[1]=='PoCGWM'}.set{CC_PoCGWM_for_combine_parietal}
CC_Homotopic_for_filter_AGWM.filter{it[1]=='AGWM'}.set{CC_AGWM_for_combine_parietal}
CC_SPGWM_for_combine_parietal.concat(CC_SMGWM_for_combine_parietal).concat(CC_PrCuGWM_for_combine_parietal).concat(CC_PoCGWM_for_combine_parietal).concat(CC_AGWM_for_combine_parietal).groupTuple(by:0).set{CC_Homotopic_parietal_for_rename}
/*
Filter CC Cingulum
*/
CC_Homotopic_for_filter_CingGWM.filter{it[1]=='CingGWM'}.set{CC_Homotopic_cingulum_for_rename}
/*
Filter CC Ins
*/
CC_Homotopic_for_filter_Ins.filter{it[1]=='Ins'}.set{CC_Homotopic_insular_for_rename}
/*
MERGE CC_Homotopic
*/
CC_Homotopic_for_merge.groupTuple().map{it}.set{CC_Homotopic_list_for_merge}
process CC_Homotopic_merge {
cpus 1
input:
set sid, file(tractogram) from CC_Homotopic_list_for_merge
output:
set sid, "${sid}__CC_homo.trk" into CC_homo_for_trk_plausible, CC_homo_for_renaming
script:
"""
scil_streamlines_math.py concatenate ${tractogram} ${sid}__CC_homo.trk
"""
}
/*
ASSO VENTRAL
*/
asso_ventral_f_t_list=params.asso_ventral_f_t_lists?.tokenize(',')
process Asso_ventral_f_t {
cpus 1
input:
set sid, val(side), file(tractogram) from asso_all_intra_inter_for_ventral_f_t_filtering
each asso_list from asso_ventral_f_t_list
output:
set sid, val(side), "${sid}__asso_F_${asso_list}_${side}.trk" into asso_all_intra_inter_ventral_f_t_for_merge
set sid, "${sid}__asso_lost_${asso_list}_${side}.trk"
file "${sid}__asso_F_${asso_list}_${side}.txt" optional true
file "${sid}__asso_lost_${asso_list}_${side}.txt" optional true
script:
filtering_list=params.filtering_lists_folder+"ASSO_F_${asso_list}_${side}_filtering_list.txt"
out_extension="asso_F_${asso_list}_${side}"
remaining_extension="asso_lost_${asso_list}_${side}"
basename="${sid}"
keep="$params.keep_intermediate_steps"
extract_masks=""