-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess.nf
1334 lines (1202 loc) · 48.7 KB
/
preprocess.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
nextflow.enable.dsl=2
include {
merge_channels_non_blocking;
replace_dwi_file;
exclude_missing_datapoints;
fill_missing_datapoints;
filter_datapoints;
get_config_path;
collect_paths;
is_path_list
} from '../modules/functions.nf'
include {
extract_b0 as dwi_b0;
extract_b0 as extract_epi_corrected_b0;
extract_b0 as extract_b0_preprocessed;
extract_b0 as extract_b0_reference
} from '../modules/processes/preprocess.nf'
include {
scil_compute_dti_fa
} from '../modules/processes/measure.nf'
include {
ants_transform as ants_transform_base_t1;
ants_transform as ants_transform_base_dwi;
ants_transform as ants_transform_syn_t1;
ants_transform as ants_transform_syn_dwi;
ants_transform as ants_transform_base_wm;
ants_transform as ants_transform_base_gm;
ants_transform as ants_transform_base_csf;
ants_transform as ants_transform_syn_wm;
ants_transform as ants_transform_syn_gm;
ants_transform as ants_transform_syn_csf;
ants_transform as ants_transform_base_raw_t1;
ants_transform as ants_transform_syn_raw_t1;
ants_transform as ants_transform_wm_mask;
ants_transform as ants_transform_gm_mask;
ants_transform as ants_transform_csf_mask;
ants_transform as ants_transform_safe_wm_mask;
ants_transform as ants_transform_raw_t1_mask;
ants_transform as apply_transform_epi_dwi;
ants_transform as apply_transform_epi_rev;
ants_transform as apply_transform_epi_field
} from '../modules/processes/register.nf'
include {
convert_float_to_integer as convert_wm_segmentation;
convert_float_to_integer as convert_gm_segmentation;
convert_float_to_integer as convert_csf_segmentation;
convert_float_to_integer as dwi_mask_convert_datatype;
convert_float_to_integer as t1_mask_convert_datatype;
crop_image as crop_dwi;
crop_image as crop_t1;
crop_image as crop_wm;
crop_image as crop_gm;
crop_image as crop_csf;
crop_image as crop_wm_mask;
crop_image as crop_gm_mask;
crop_image as crop_csf_mask;
crop_image as crop_safe_wm_mask;
crop_image as crop_raw_dwi;
crop_image as crop_raw_t1;
crop_image as crop_d99_atlas;
crop_image as crop_sarm_atlas;
crop_image as crop_charm_atlas;
crop_image as crop_inia19_atlas;
bet_mask;
fit_bounding_box;
merge_masks;
check_odd_dimensions;
pvf_to_mask;
validate_gradients;
patch_in_mask;
apply_mask as mask_t1
} from '../modules/processes/utils.nf'
include {
gibbs_removal as dwi_gibbs_removal;
gibbs_removal as rev_gibbs_removal;
nlmeans_denoise;
ants_gaussian_denoise;
normalize_inter_b0
} from '../modules/processes/denoise.nf'
include {
scilpy_resample_to_reference as resample_wm;
scilpy_resample_to_reference as resample_gm;
scilpy_resample_to_reference as resample_csf;
scilpy_resample_to_reference as resample_t1;
scilpy_resample_to_reference as resample_dwi;
scilpy_resample_to_reference as resample_raw_dwi;
scilpy_resample_to_reference as resample_raw_t1;
resampling_reference
} from '../modules/processes/upsample.nf'
include {
registration_wkf as dwi_mask_registration_wkf;
dwi_denoise_wkf;
dwi_denoise_wkf as rev_denoise_wkf;
n4_denoise_wkf;
n4_denoise_wkf as n4_denoise_t1_to_b0_wkf;
squash_wkf;
squash_wkf as squash_raw_wkf;
squash_wkf as squash_for_epi_correction_wkf;
epi_correction_wkf;
apply_topup_wkf;
apply_topup_wkf as raw_apply_topup_wkf;
eddy_wkf;
apply_epi_field_wkf;
apply_epi_field_wkf as raw_apply_epi_field_wkf
} from "../modules/workflows/preprocess.nf"
include {
t1_mask_to_b0;
t12b0_registration as t1_registration_wkf
} from '../modules/workflows/t1_registration.nf'
include {
segment_nmt_wkf;
segment_wm_wkf
} from '../modules/workflows/segment.nf'
include {
change_name as rename_ec_input_dwi;
change_name as rename_ec_input_rev;
enforce_sid_convention as rename_ec_input_dwi_meta;
enforce_sid_convention as rename_ec_input_rev_meta;
change_name as rename_epi_corrected_dwi;
change_name as rename_epi_corrected_meta;
change_name as rename_transformed_raw_dwi;
change_name as rename_transformed_raw_rev;
change_name as rename_transformed_raw_metadata;
change_name as rename_transformed_raw_rev_metadata;
change_name as rename_processed_dwi;
change_name as rename_processed_dwi_metadata;
change_name as rename_processed_dwi_mask;
change_name as rename_dwi_mask;
change_name as rename_dwi_for_eddy;
change_name as rename_rev_for_eddy;
change_name as rename_dwi_metadata_for_eddy;
change_name as rename_rev_metadata_for_eddy
} from '../modules/processes/io.nf'
// Preprocess workflow parameters
params.gaussian_noise_correction = true
params.gibbs_ringing_correction = true
params.dwi_mask_from_t1_mask = true
params.epi_correction = true
params.epi_algorithm = "topup"
params.eddy_correction = true
params.dwi_intensity_normalization = true
params.resample_data = true
params.register_t1_to_dwi = true
params.generate_tissue_segmentation = false
params.generate_wm_segmentation = true
params.raw_to_processed_space = false
params.resampling_subdivision = 2
params.resampling_min_resolution = false
params.force_resampling_resolution = false
// T1 preprocess workflow parameters
params.denoise_t1 = true
params.nlmeans_t1 = true
params.t1_intensity_normalization = true
params.quick_t1_mask_registration = true
params.quick_denoised_t1_registration = false
params.t1_registration_in_subject_space = false
params.b02t1_mask_registration_config = file("${get_config_path()}/b02t1_mask_registration_config.py")
params.ants_transform_base_config = file("${get_config_path()}/ants_transform_base_config.py")
params.ants_transform_mask_config = file("${get_config_path()}/ants_transform_mask_config.py")
params.extract_mean_b0_base_config = file("${get_config_path()}/extract_mean_b0_base_config.py")
params.dwi_n4_normalization_config = file("${get_config_path()}/dwi_n4_normalization_config.py")
params.dwi_n4_normalization_quick_config = file("${get_config_path()}/dwi_n4_normalization_quick_config.py")
params.t1_n4_normalization_config = file("${get_config_path()}/t1_n4_normalization_config.py")
params.b0_to_b0_normalization_config = file("${get_config_path()}/b0_to_b0_normalization_config.py")
workflow preprocess_wkf {
take:
dwi_channel
rev_channel
t1_channel
pvf_channel
meta_channel
rev_meta_channel
dwi_mask_channel
t1_mask_channel
main:
// Keep sid references for channel management
ref_id_channel = dwi_channel.map{ [it[0]] }
absent_dwi_mask_id_channel = filter_datapoints(
dwi_mask_channel,
{ it[1] == "" }
).map{ [it[0]] }
present_rev_id_channel = filter_datapoints(
rev_channel,
{ it[1] != "" }
).map{ [it[0]] }
// T1 preprocessing
t1_preprocess_wkf(t1_channel, t1_mask_channel)
t1_channel = t1_preprocess_wkf.out.t1
t1_mask_channel = t1_preprocess_wkf.out.mask
// Fix odd number of slices in phase direction for EPI correction
check_odd_dimensions(
dwi_channel
.join(rev_channel)
.join(dwi_mask_channel)
.join(collect_paths(meta_channel.join(rev_meta_channel))),
"preprocess"
)
rev_bval_bvec_channel = fill_missing_datapoints(
check_odd_dimensions.out.rev_bval_bvec,
ref_id_channel,
1, ["", ""]
)
dwi_channel = check_odd_dimensions.out.dwi
rev_channel = fill_missing_datapoints(
check_odd_dimensions.out.rev,
ref_id_channel,
1, [""]
).join(rev_bval_bvec_channel)
dwi_mask_channel = fill_missing_datapoints(
check_odd_dimensions.out.mask,
ref_id_channel,
1, [""]
)
checked_meta_channel = exclude_missing_datapoints(
check_odd_dimensions.out.metadata
.map{ it.flatten() }
.map{ [it[0], it[1..-1]] }
.transpose(),
1, ""
).groupTuple()
meta_channel = checked_meta_channel
.map{ [it[0], it[1].findAll{ i -> !i.simpleName.contains("_rev") }].flatten() }
rev_meta_channel = fill_missing_datapoints(
checked_meta_channel
.map{ [it[0], it[1].findAll{ i -> i.simpleName.contains("_rev") }].flatten() },
ref_id_channel,
1, [""]
)
// Copy input channels for later
dwi_channel.tap{ raw_dwi_channel }
rev_channel.tap{ raw_rev_channel }
t1_channel.tap{ raw_t1_channel }
t1_mask_channel.tap{ raw_t1_mask_channel }
meta_channel.tap{ raw_meta_channel }
rev_meta_channel.tap{ raw_rev_meta_channel }
// Perform DWI and b0 denoising
if ( params.gaussian_noise_correction ) {
dwi_denoise_wkf(dwi_channel, dwi_mask_channel, meta_channel, "true")
dwi_channel = replace_dwi_file(dwi_channel, dwi_denoise_wkf.out.image)
meta_channel = dwi_denoise_wkf.out.metadata
rev_denoise_wkf(rev_channel, dwi_mask_channel, rev_meta_channel, "false")
rev_channel = replace_dwi_file(rev_channel, rev_denoise_wkf.out.image)
rev_meta_channel = fill_missing_datapoints(
rev_denoise_wkf.out.metadata,
ref_id_channel,
1, [""]
)
}
// Perform DWI and b0 gibbs correction
if ( params.gibbs_ringing_correction ) {
dwi_gibbs_removal(dwi_channel.map{ it[0..1] }.join(meta_channel), "preprocess", "true")
dwi_channel = replace_dwi_file(dwi_channel, dwi_gibbs_removal.out.image)
meta_channel = dwi_gibbs_removal.out.metadata
rev_gibbs_removal(
exclude_missing_datapoints(rev_channel.map{ it[0..1] }.join(rev_meta_channel), 1, ""),
"preprocess", "false"
)
rev_channel = replace_dwi_file(
rev_channel,
fill_missing_datapoints(rev_gibbs_removal.out.image, ref_id_channel, 1, [""])
)
rev_meta_channel = fill_missing_datapoints(rev_gibbs_removal.out.metadata, ref_id_channel, 1, [""])
}
// Perform DWI signal normalization between b0 volumes
if ( params.normalize_inter_b0 ) {
normalize_inter_b0(
dwi_channel
.map{ it[0..2] }
.join(rev_channel.map{ it[0..2] })
.join(meta_channel)
.join(rev_meta_channel),
"preprocess",
params.b0_to_b0_normalization_config
)
dwi_channel = replace_dwi_file(dwi_channel, normalize_inter_b0.out.dwi)
meta_channel = normalize_inter_b0.out.dwi_metadata
rev_channel = replace_dwi_file(
rev_channel,
fill_missing_datapoints(normalize_inter_b0.out.rev, ref_id_channel, 1, [""])
)
rev_meta_channel = fill_missing_datapoints(
normalize_inter_b0.out.rev_metadata,
ref_id_channel,
1, [""]
)
}
// Average consecutive b0 volumes just like for EPI correction
squash_wkf(
dwi_channel,
rev_channel,
meta_channel.join(rev_meta_channel),
""
)
dwi_channel = squash_wkf.out.dwi
rev_channel = squash_wkf.out.rev
squashed_meta_channel = exclude_missing_datapoints(
squash_wkf.out.metadata
.map{ it.flatten() }
.map{ [it[0], it[1..-1]] }
.transpose(),
1, ""
).groupTuple()
meta_channel = squashed_meta_channel
.map{ [it[0], it[1].findAll{ i -> !i.simpleName.contains("_rev") }].flatten() }
rev_meta_channel = fill_missing_datapoints(
squashed_meta_channel
.map{ [it[0], it[1].findAll{ i -> i.simpleName.contains("_rev") }].flatten() },
ref_id_channel,
1, [""]
)
// Extract mean b0
dwi_b0(
dwi_channel
.map{ it[0..2] }
.join(meta_channel.map{ [it[0], it[1..-1]] }),
"preprocess",
"false",
params.extract_mean_b0_base_config
)
b0_channel = dwi_b0.out.b0
b0_metadata_channel = dwi_b0.out.metadata
// EPI correction
ec2eddy_channel = ref_id_channel.map{ it + ["", "", []] }
epi_corrected_dwi_channel = dwi_channel
epi_corrected_meta_channel = meta_channel
if ( params.epi_correction ) {
ref_rev_id_channel = exclude_missing_datapoints(
raw_rev_channel, 1, ""
).map{ [it[0]] }
excluded_id_channel = filter_datapoints(
raw_rev_channel, { it[1] == "" }
).map{ [it[0]] }
// Average consecutive b0 volumes to speed up EPI correction
squash_for_epi_correction_wkf(
ref_rev_id_channel.join(raw_dwi_channel),
ref_rev_id_channel.join(raw_rev_channel),
ref_rev_id_channel.join(raw_meta_channel).join(raw_rev_meta_channel),
""
)
// Run EPI correction sub-workflow
epi_correction_wkf(
squash_for_epi_correction_wkf.out.dwi,
squash_for_epi_correction_wkf.out.rev,
squash_for_epi_correction_wkf.out.metadata.map{ it.flatten() }
)
ec_input_dwi_channel = rename_ec_input_dwi(
collect_paths(epi_correction_wkf.out.corrected_indexes.join(dwi_channel)),
"ec_input_dwi"
).map{ [it[0], it[1][2], it[1][0], it[1][1]] }
ec_input_rev_channel = rename_ec_input_rev(
collect_paths(epi_correction_wkf.out.corrected_indexes.join(rev_channel)),
"ec_input_rev"
).map{ it.flatten() }.map{ it.size() == 4 ? [it[0], it[3], it[1], it[2]] : it + ["", ""] }
ec_input_dwi_meta_channel = rename_ec_input_dwi_meta(
epi_correction_wkf.out.in_metadata_w_epi_correction
.map{ [it[0], it[1].find{m -> m.simpleName.contains("_dwi")}, "dwi__ec_input_dwi_metadata"] }
).map{ it.flatten() }
ec_input_rev_meta_channel = rename_ec_input_rev_meta(
epi_correction_wkf.out.in_metadata_w_epi_correction
.map{ [it[0], it[1].find{m -> m.simpleName.contains("_rev")}, "rev__ec_input_rev_metadata"] }
).map{ it.flatten() }
extract_b0_reference(
ec_input_dwi_channel.map{ it[0..2] + [""] },
"preprocess", "false", params.extract_mean_b0_base_config
)
b0_reference_for_registration = extract_b0_reference.out.b0
apply_transform_epi_rev(
ec_input_rev_channel.map{ it[0..1] }
.join(b0_reference_for_registration)
.join(epi_correction_wkf.out.forward_transform)
.join(epi_correction_wkf.out.reverse_transform)
.map{ it[0..-3] + [it[-2] + it[-1]] }
.map{ it + [["true", "false"], "", ""] },
"preprocess",
"",
"false",
"",
params.ants_transform_base_config
)
rev_channel = replace_dwi_file(rev_channel, apply_transform_epi_rev.out.image)
// Applied estimated susceptibility correction to DWI
ec2eddy_channel = Channel.empty()
epi_fieldmap_channel = Channel.empty()
epi_displacement_field_channel = Channel.empty()
if ( params.epi_algorithm == "topup" ) {
ec2eddy_channel = epi_correction_wkf.out.param
.join(epi_correction_wkf.out.prefix)
.join(epi_correction_wkf.out.topup.map{ [it[0], it[1..-1]] })
// Applied estimated susceptibility correction to DWI
apply_topup_wkf(
ec_input_dwi_channel,
apply_transform_epi_rev.out.image,
ec2eddy_channel,
ec_input_dwi_meta_channel
.join(ec_input_rev_meta_channel)
.map{ [it[0], it[1..-1]] },
""
)
epi_corrected_dwi_channel = rename_epi_corrected_dwi(
collect_paths(apply_topup_wkf.out.dwi),
"topup_corrected"
).map{ [it[0], it[1][2], it[1][0], it[1][1]] }
epi_corrected_meta_channel = rename_epi_corrected_meta(
collect_paths(apply_topup_wkf.out.metadata),
"topup_corrected_metadata"
).map{ it.flatten() }
}
else {
epi_displacement_field_channel = epi_correction_wkf.out.field
epi_fieldmap_channel = epi_correction_wkf.out.fieldmap
apply_transform_epi_field(
epi_displacement_field_channel
.join(b0_reference_for_registration)
.join(epi_correction_wkf.out.forward_transform)
.map{ it[0..-2] + [it[-1]] }
.map{ it + [["true"], "", ""] },
"preprocess",
"",
"false",
"",
params.ants_transform_base_config
)
apply_epi_field_wkf(
ec_input_dwi_channel.map{ it[0..1] },
apply_transform_epi_rev.out.image,
apply_transform_epi_field.out.image,
ec_input_dwi_meta_channel
.map{ [it[0], it[1]] },
""
)
epi_corrected_dwi_channel = rename_epi_corrected_dwi(
collect_paths(replace_dwi_file(ec_input_dwi_channel, apply_epi_field_wkf.out.dwi)),
"epi_corrected"
).map{ [it[0], it[1][2], it[1][0], it[1][1]] }
epi_corrected_meta_channel = rename_epi_corrected_meta(
collect_paths(ec_input_dwi_meta_channel),
"epi_corrected_metadata"
).map{ it.flatten() }
}
epi_displacement_field_channel = fill_missing_datapoints(
epi_displacement_field_channel,
ref_id_channel,
1, [""]
)
epi_fieldmap_channel = fill_missing_datapoints(
epi_fieldmap_channel,
ref_id_channel,
1, [""]
)
ec2eddy_channel = fill_missing_datapoints(
ec2eddy_channel,
ref_id_channel,
1, ["", "", []]
)
epi_corrected_dwi_channel = excluded_id_channel
.join(dwi_channel)
.mix(epi_corrected_dwi_channel)
epi_corrected_meta_channel = excluded_id_channel
.join(meta_channel)
.mix(epi_corrected_meta_channel)
// Get average susceptibility corrected b0
extract_epi_corrected_b0(
epi_corrected_dwi_channel
.map{ it[0..2] }
.join(collect_paths(epi_corrected_meta_channel)),
"preprocess",
"true",
params.extract_mean_b0_base_config
)
b0_channel = excluded_id_channel
.join(b0_channel)
.mix(extract_epi_corrected_b0.out.b0)
b0_metadata_channel = excluded_id_channel
.join(b0_metadata_channel)
.mix(extract_epi_corrected_b0.out.metadata)
if ( !params.eddy_correction ) {
dwi_channel = epi_corrected_dwi_channel
meta_channel = epi_corrected_meta_channel
}
// Apply susceptibility corrections to raw images (for comparison)
if ( params.raw_to_processed_space ) {
raw_dwi_channel = rename_transformed_raw_dwi(
collect_paths(raw_dwi_channel),
"raw"
).map{ [it[0], it[1][2], it[1][0], it[1][1]] }
raw_rev_channel = rename_transformed_raw_rev(
collect_paths(raw_rev_channel).filter{ it[1] },
"raw"
)
raw_rev_channel = raw_rev_channel
.map{ it.flatten() }
.map{ it.size() == 4 ? [it[0], it[3], it[1], it[2]] : it + ["", ""] }
raw_meta_channel = excluded_id_channel.join(raw_meta_channel)
.mix(
rename_transformed_raw_metadata(
collect_paths(ec_input_dwi_meta_channel).filter{ it[1] },
"raw_metadata"
).map{ it.flatten() }
)
raw_rev_meta_channel = excluded_id_channel.join(raw_meta_channel)
.mix(
rename_transformed_raw_rev_metadata(
collect_paths(ec_input_rev_meta_channel),
"raw_metadata"
).map{ it.flatten() }
)
if ( params.epi_algorithm == "topup" ) {
raw_apply_topup_wkf(
epi_correction_wkf.out.corrected_indexes.join(raw_dwi_channel),
epi_correction_wkf.out.corrected_indexes
.join(raw_rev_channel)
.map{ it[0..1] },
ec2eddy_channel,
collect_paths(raw_meta_channel.join(raw_rev_meta_channel)),
"raw"
)
raw_dwi_channel = excluded_id_channel
.join(raw_dwi_channel)
.mix(raw_apply_topup_wkf.out.dwi)
raw_meta_channel = excluded_id_channel
.join(raw_meta_channel)
.mix(raw_apply_topup_wkf.out.metadata)
}
else {
raw_apply_epi_field_wkf(
epi_correction_wkf.out.corrected_indexes.join(raw_dwi_channel),
epi_correction_wkf.out.corrected_indexes
.join(raw_rev_channel)
.map{ it[0..1] },
epi_displacement_field_channel,
collect_paths(raw_meta_channel.join(raw_rev_meta_channel)),
"raw"
)
raw_dwi_channel = excluded_id_channel
.join(raw_dwi_channel)
.mix(raw_apply_epi_field_wkf.out.dwi)
raw_meta_channel = excluded_id_channel
.join(raw_meta_channel)
.mix(raw_apply_epi_field_wkf.out.metadata)
}
}
}
empty_dwi_mask_id_channel = filter_datapoints(
dwi_mask_channel,
{ it[1] == "" }
).map{ [it[0]] }
dwi_mask_channel = exclude_missing_datapoints(dwi_mask_channel, 1, "")
// Compute brain mask for the DWI (when missing)
bet_mask(
empty_dwi_mask_id_channel.join(b0_channel),
"preprocess",
"${!params.dwi_mask_from_t1_mask}",
"dwi_mask"
)
dwi_mask_channel = dwi_mask_channel.mix(bet_mask.out.mask)
// Get better mask for the DWI from the T1 (when missing and if present)
if ( params.dwi_mask_from_t1_mask ) {
existing_t1_mask_id_channel = exclude_missing_datapoints(
t1_mask_channel,
1, ""
).map{ [it[0]] }
absent_t1_mask_id_channel = filter_datapoints(
t1_mask_channel,
{ it[1] == "" }
).map{ [it[0]] }
n4_denoise_t1_to_b0_wkf(
existing_t1_mask_id_channel.join(epi_corrected_dwi_channel.map{ it[0..1] }),
existing_t1_mask_id_channel.join(b0_channel),
existing_t1_mask_id_channel.join(dwi_mask_channel),
existing_t1_mask_id_channel.join(epi_corrected_meta_channel),
params.dwi_n4_normalization_quick_config,
false
)
t1_mask_to_b0(
replace_dwi_file(epi_corrected_dwi_channel, n4_denoise_t1_to_b0_wkf.out.image),
existing_t1_mask_id_channel.join(t1_channel),
existing_t1_mask_id_channel.join(t1_mask_channel),
"false"
)
t1_mask_convert_datatype(
t1_mask_to_b0.out.mask,
"uint8", "preprocess",
!params.register_t1_to_dwi,
"dwi_mask", ""
)
dwi_mask_channel = t1_mask_convert_datatype.out.image
.mix(absent_t1_mask_id_channel.join(dwi_mask_channel))
}
// Perform Eddy currents and motion correction on the DWI
if ( params.eddy_correction ) {
dwi_channel = rename_dwi_for_eddy(
collect_paths(dwi_channel),
"to_eddy"
).map{ [it[0], it[1][2], it[1][0], it[1][1]] }
rev_channel = rename_rev_for_eddy(
collect_paths(rev_channel).filter{ it[1] },
"to_eddy"
)
rev_channel = rev_channel
.map{ it.flatten() }
.map{ it.size() == 4 ? [it[0], it[3], it[1], it[2]] : it + ["", ""] }
rev_channel = fill_missing_datapoints(
rev_channel,
ref_id_channel,
1, ["", "", ""]
)
meta_channel = rename_dwi_metadata_for_eddy(
collect_paths(meta_channel).filter{ it[1] },
"to_eddy_metadata"
).map{ it.flatten() }
rev_meta_channel = rename_rev_metadata_for_eddy(
collect_paths(rev_meta_channel).filter{ it[1] },
"to_eddy_metadata"
).map{ it.flatten() }
rev_meta_channel = fill_missing_datapoints(
rev_meta_channel,
ref_id_channel,
1, [""]
)
// Run Eddy sub-workflow
eddy_wkf(
dwi_channel,
dwi_mask_channel,
ec2eddy_channel,
epi_fieldmap_channel,
epi_displacement_field_channel,
rev_channel,
meta_channel.join(rev_meta_channel)
)
dwi_channel = eddy_wkf.out.dwi
.join(eddy_wkf.out.bval)
.join(eddy_wkf.out.bvec)
meta_channel = eddy_wkf.out.metadata
}
// Perform intensity normalization on the DWI
if ( params.dwi_intensity_normalization ) {
// Run N4 sub-workflow
n4_denoise_wkf(
dwi_channel.map{ it[0..1] },
b0_channel,
dwi_mask_channel,
meta_channel,
params.dwi_n4_normalization_config,
true
)
dwi_channel = replace_dwi_file(dwi_channel, n4_denoise_wkf.out.image)
meta_channel = n4_denoise_wkf.out.metadata
}
absent_t1_mask_id_channel = filter_datapoints(
t1_mask_channel,
{ it[1] == "" }
).map{ [it[0]] }
existing_t1_mask_id_channel = exclude_missing_datapoints(
t1_mask_channel,
1, ""
).map{ [it[0]] }
// Get DWI mask in T1 space (for missing T1 masks)
dwi_mask_registration_wkf(
absent_t1_mask_id_channel.join(t1_channel).map{ [it[0], [it[1]]] },
absent_t1_mask_id_channel.join(b0_channel).map{ [it[0], [it[1]]] },
absent_t1_mask_id_channel.join(dwi_mask_channel),
null,
null,
absent_t1_mask_id_channel
.join(b0_metadata_channel)
.map{ it[0..1] + [""] },
"",
false, "", "",
params.b02t1_mask_registration_config,
null
)
dwi_mask_convert_datatype(
dwi_mask_registration_wkf.out.image,
"uint8", "preprocess",
true,
"t1_mask", ""
)
t1_mask_channel = existing_t1_mask_id_channel
.join(t1_mask_channel)
.mix(dwi_mask_convert_datatype.out.image)
raw_t1_mask_channel = t1_mask_channel
raw_dwi_mask_channel = dwi_mask_channel
// Compute best resampling reference
resampling_reference(
collect_paths(dwi_channel.map{ it[0..1] }.join(t1_channel)),
"preprocess",
params.resample_data ? params.resampling_subdivision : "1",
params.resample_data ? params.resampling_min_resolution : "",
params.resample_data ? params.force_resampling_resolution : ""
)
reference_channel = resampling_reference.out.reference
pvf_to_resample_channel = pvf_channel.filter{ !it[1].isEmpty() }
// Resample all volumes
resample_dwi(
dwi_channel
.map{ it[0..1] }
.join(reference_channel)
.join(dwi_mask_channel)
.join(meta_channel),
"preprocess", "lin",
true, true,
"dwi_mask", ""
)
resample_t1(
t1_channel
.join(reference_channel)
.join(t1_mask_channel)
.map{ it + [""] },
"preprocess", "lin",
true, true,
"t1_mask", ""
)
resample_wm(
pvf_to_resample_channel
.map{ [it[0], it[1][0]] }
.join(reference_channel)
.join(t1_mask_channel)
.map{ it + [""] },
"preprocess", "nn",
true, false,
"", "segmentation"
)
resample_gm(
pvf_to_resample_channel
.map{ [it[0], it[1][1]] }
.join(reference_channel)
.join(t1_mask_channel)
.map{ it + [""] },
"preprocess", "nn",
true, false,
"", "segmentation"
)
resample_csf(
pvf_to_resample_channel
.map{ [it[0], it[1][2]] }
.join(reference_channel)
.join(t1_mask_channel)
.map{ it + [""] },
"preprocess", "nn",
true, false,
"", "segmentation"
)
dwi_channel = replace_dwi_file(dwi_channel, resample_dwi.out.image)
dwi_mask_channel = resample_dwi.out.mask
meta_channel = resample_dwi.out.metadata
t1_channel = resample_t1.out.image
t1_mask_channel = resample_t1.out.mask
pvf_channel = resample_wm.out.image
.join(resample_gm.out.image)
.join(resample_csf.out.image)
.map{ [it[0], it[1..-1]] }
.mix(pvf_channel.filter{ it[1].isEmpty() })
if ( params.raw_to_processed_space ) {
resample_raw_dwi(
raw_dwi_channel
.map{ it[0..1] }
.join(reference_channel)
.join(raw_dwi_mask_channel)
.join(raw_meta_channel),
"preprocess", "lin",
true, true,
"dwi_mask", "raw"
)
resample_raw_t1(
raw_t1_channel
.join(reference_channel)
.join(raw_t1_mask_channel)
.map{ it + [""] },
"preprocess", "lin",
true, true,
"t1_mask", "raw"
)
raw_dwi_channel = replace_dwi_file(raw_dwi_channel, resample_raw_dwi.out.image)
raw_meta_channel = resample_raw_dwi.out.metadata
raw_t1_channel = resample_raw_t1.out.image
raw_t1_mask_channel = resample_raw_t1.out.mask
raw_dwi_mask_channel = resample_raw_dwi.out.mask
}
// Get tissue masks from PVF
pvf_to_mask(
pvf_channel
.filter{ !it[1].isEmpty() }
.join(dwi_mask_channel),
"preprocess",
"segmentation"
)
tissue_mask_channel = collect_paths(
pvf_to_mask.out.masks
.map{ [ it[0], it[1].sort{ a, b -> ["wm", "gm", "csf"].findIndexOf{ i -> a.simpleName.contains(i) } <=> ["wm", "gm", "csf"].findIndexOf{ i -> b.simpleName.contains(i) } }] }
).mix(pvf_channel.filter{ it[1].isEmpty() })
safe_wm_mask_channel = pvf_channel
.filter{ it[1].isEmpty() }
.map{ [it[0], ""] }
.mix(pvf_to_mask.out.safe_wm_mask)
// Register T1 to diffusion space (DWI) with masks and segmentations
template_resampling_reference = null
template_to_b0_transform = null
if ( params.register_t1_to_dwi ) {
t1_registration_wkf(
dwi_channel,
t1_channel,
t1_mask_channel,
dwi_mask_channel,
meta_channel,
true,
true,
params.quick_denoised_t1_registration,
params.t1_registration_in_subject_space
)
template_resampling_reference = t1_registration_wkf.out.resampling_reference
template_to_b0_transform = t1_registration_wkf.out.template_to_b0_transform
t1_channel = t1_registration_wkf.out.t1
t1_mask_channel = t1_registration_wkf.out.mask
dwi_mask_channel = rename_dwi_mask(
collect_paths(t1_registration_wkf.out.mask),
"dwi_mask"
).map{ it.flatten() }
pvf_to_register_channel = pvf_channel
.filter{ !it[1].isEmpty() }
tissue_mask_to_register_channel = tissue_mask_channel
.filter{ !it[1].isEmpty() }
ants_transform_base_wm(
pvf_to_register_channel.map{ [it[0], it[1][0]] }
.join(t1_channel)
.join(t1_registration_wkf.out.transform)
.map{ it + ["", ""] },
"preprocess", "segmentation", "true", "",
params.ants_transform_base_config
)
ants_transform_base_gm(
pvf_to_register_channel
.map{ [it[0], it[1][1]] }
.join(t1_channel)
.join(t1_registration_wkf.out.transform)
.map{ it + ["", ""] },
"preprocess", "segmentation", "true", "",
params.ants_transform_base_config
)
ants_transform_base_csf(
pvf_to_register_channel
.map{ [it[0], it[1][2]] }
.join(t1_channel)
.join(t1_registration_wkf.out.transform)
.map{ it + ["", ""] },
"preprocess", "segmentation", "true", "",
params.ants_transform_base_config
)
ants_transform_wm_mask(
tissue_mask_to_register_channel
.map{ [it[0], it[1][0]] }
.join(t1_channel)
.join(t1_registration_wkf.out.transform)
.map{ it + ["", ""] },
"preprocess", "segmentation", "true", "",
params.ants_transform_mask_config
)
ants_transform_gm_mask(
tissue_mask_to_register_channel
.map{ [it[0], it[1][1]] }
.join(t1_channel)
.join(t1_registration_wkf.out.transform)
.map{ it + ["", ""] },
"preprocess", "segmentation", "true", "",
params.ants_transform_mask_config
)
ants_transform_csf_mask(
tissue_mask_to_register_channel
.map{ [it[0], it[1][2]] }
.join(t1_channel)
.join(t1_registration_wkf.out.transform)
.map{ it + ["", ""] },
"preprocess", "segmentation", "true", "",
params.ants_transform_mask_config
)
ants_transform_safe_wm_mask(
safe_wm_mask_channel
.filter{ it[1] }
.join(t1_channel)
.join(t1_registration_wkf.out.transform)
.map{ it + ["", ""] },
"preprocess", "segmentation", "true", "",
params.ants_transform_mask_config
)
pvf_channel = collect_paths(
ants_transform_base_wm.out.image
.join(ants_transform_base_gm.out.image)
.join(ants_transform_base_csf.out.image)
).mix(pvf_channel.filter{ it[1].isEmpty() })
tissue_mask_channel = collect_paths(
ants_transform_wm_mask.out.image
.join(ants_transform_gm_mask.out.image)
.join(ants_transform_csf_mask.out.image)
).mix(pvf_channel.filter{ it[1].isEmpty() })
safe_wm_mask_channel = pvf_channel
.filter{ it[1].isEmpty() }
.map{ [it[0], ""] }
.mix(ants_transform_safe_wm_mask.out.image)
if ( params.raw_to_processed_space ) {