-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakefile
More file actions
1390 lines (1288 loc) · 45 KB
/
Copy pathSnakefile
File metadata and controls
1390 lines (1288 loc) · 45 KB
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
"""
This is a minimal reimplementation of the recon-all script for educational purposes.
It only supports the processing of a single T1 MP-RAGE, so it skips steps like motion-correction
and averaging of multiple input volumes. I mimicks the following invocation:
recon-all -i datasets/T1w.nii.gz -hires -subjid subject-minimal -all
This implementation allows one to see how the FS pipeline is composed (as of FS 7.1.1), and where changes
should be made to solve ones specific problems.
It should not be used in production.
Daniel Gomez
2021.04.22
"""
import os
import glob
configfile: "recon-config.yaml"
# For the entire pipeline, consider that the brain only has 2 hemispheres.
wildcard_constraints:
hemi="(l|r)h"
# While we don't solve FS's referential transparency problem, we set these guys.
workdir: os.path.join(config["SUBJECTS_DIR"], config["SUBJECT"])
os.environ["SUBJECTS_DIR"] = config["SUBJECTS_DIR"]
# The FreeSurfer root folder.
FREESURFER_HOME=config["FREESURFER_HOME"]
# Create a symlink to fsaverage in the SUBJECTS_DIR, if it doesn't exist yet:
if not os.path.exists(os.path.join(config["SUBJECTS_DIR"], "fsaverage")):
os.symlink(os.path.join(config["FREESURFER_HOME"], "subjects/fsaverage"),
os.path.join(config["SUBJECTS_DIR"], "fsaverage"))
# BA Labels (for autorecon 3)
BA_LABELS = list(
glob.glob(
os.path.join(f"{config['FREESURFER_HOME']}",
"subjects/fsaverage/label/*.label")
)
)
# only the filename, not the path. And remove cortex, because ambiguous otherwise.
BA_LABELS = [x.split('/')[-1] for x in BA_LABELS if 'cortex' not in x]
##################
# Initialization #
##################
# This step will convert the input from NIfTI into MGH's mgz format. The mgz
# format is an .mdh file that has been compressed, thus mgh.gz. Info here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat
rule convert_input_to_mgz:
input:
config["INPUT_T1"]
output:
"mri/orig/001.mgz"
shell:
"""
mri_convert {input} {output}
"""
###############
# Autorecon 1 #
###############
# In the actual recon-all script, this step is more elaborate and takes care of
# motion correction and averaging to generate a single anatomical imaging for
# further processing.
rule copy_input_to_rawavg:
input:
"mri/orig/001.mgz"
output:
"mri/rawavg.mgz"
shell:
"""
cp {input} {output}
"""
# After generating an average T1 (or simply converting the single input T1), the
# dataset is "conformed". In FS lingo, this means that the files are padded (or
# interpolated) to isotropic dimensions.
# The talairach.xfm does not exist yet, but it is added to orig to keep orig's
# timestamp correct. It may well be that for the purposes of this minimal
# implementation, such details are unnecessary and addition could be done at a
# later time, when talairach is created.
rule conform_rawavg_to_orig_and_add_talairach_xfm:
params:
future_talairach="mri/transforms/talairach.xfm"
input:
"mri/rawavg.mgz"
output:
"mri/orig.mgz"
shell:
"""
mri_convert {input} {output} --conform_min
mri_add_xform_to_header -c {params.future_talairach} {output} {output}
"""
#########################
# Bias Field Correction #
#########################
# There are two calls to mri_nu_correct.
# Apparently one is used to compute the talairach registration (the orig_nu),
# and the other (nu) is used further down the stream.
rule bias_field_correct_and_add_talairach_xfm:
params:
nuparams="--ants-n4 --n 1 --proto-iters 1000 --distance 50",
input:
"mri/orig.mgz"
output:
orig_nu="mri/orig_nu.mgz",
shell:
"""
mri_nu_correct.mni --no-rescale --i {input} --o {output.orig_nu} {params.nuparams}
"""
# This computes the affine transform from the orig volume to the MNI305
# atlas using Avi Snyders 4dfp suite of image registration tools,
# through a FreeSurfer script called talairach_avi.
# Because talairach_avi fails a lot, here we use talairach directly.
# The reason there are two outputs here is because FS apparently keeps
# track of timestamps in order to know whether files were deleted or modified in
# order to correctly allow FS to re-run certain parts of the stream after manual edits.
# As far as I understand.
rule talairach_registration:
input:
"mri/orig_nu.mgz"
output:
autoxfm="mri/transforms/talairach.auto.xfm",
xfm="mri/transforms/talairach.xfm"
shell:
"""
talairach --i {input} --xfm {output.autoxfm}
cp {output.autoxfm} {output.xfm}
"""
# TODO See line 1634 in recon-all for symlinking talairach to talairach.lta
rule convert_talairach_to_lta:
params:
target_auto=f"{FREESURFER_HOME}/mni/share/mni_autoreg/average_305.mnc",
target=f"{FREESURFER_HOME}/average/mni305.cor.mgz",
input:
orig="mri/orig.mgz",
autoxfm="mri/transforms/talairach.auto.xfm",
xfm="mri/transforms/talairach.auto.xfm"
output:
autolta="mri/transforms/talairach.auto.xfm.lta",
lta="mri/transforms/talairach.auto.xfm"
shell:
"""
lta_convert --src {input.orig} --trg {params.target_auto}\
--inxfm {input.autoxfm} --outlta {output.autolta} --subject fsverage --ltavox2vox
lta_convert --src {input.orig} --trg {params.target}\
--inxfm {input.xfm} --outlta {output.lta} --subject fsverage --ltavox2vox
"""
############################
# Intensity Normalization. #
############################
# This is done so that white matter has a value of approx. 110, which is an FS
# magic number.
rule intensity_normalize:
params:
norm_max_grad = 1,
future_talairach="mri/transforms/talairach.xfm"
input:
orig="mri/orig.mgz",
talairach_xfm="mri/transforms/talairach.xfm"
output:
nu="mri/nu.mgz",
T1="mri/T1.mgz"
threads:
16
shell:
"""
mri_nu_correct.mni --i {input.orig} --o {output.nu} --uchar {input.talairach_xfm} --cm --n 2 --ants-n4
mri_normalize -g {params.norm_max_grad} -seed 1234 -mprage -noconform {output.nu} {output.T1}
mri_add_xform_to_header -c {params.future_talairach} {output.nu} {output.nu}
"""
#####################
# Register to Atlas #
#####################
# GCA linear regisration - Initial regisration to template.
rule mri_em_register_skull:
params:
atlas_gca=f"{config['FREESURFER_HOME']}/average/RB_all_withskull_2020_01_02.gca",
input:
"mri/nu.mgz"
output:
"mri/transforms/talairach_with_skull.lta"
threads:
8
shell:
"""
mri_em_register -skull {input} {params.atlas_gca} {output}
"""
###################
# Skull stripping #
###################
# Again, I still haven't figured this out, but it seems brainmask.auto exists for bookkeeping.
rule skull_strip:
params:
atlas_gca=f"{config['FREESURFER_HOME']}/average/RB_all_withskull_2020_01_02.gca",
input:
T1="mri/T1.mgz",
talairach_lta="mri/transforms/talairach_with_skull.lta"
output:
mask="mri/brainmask.nofix.mgz",
automask="mri/brainmask.auto.mgz"
threads:
12
shell:
"""
mri_watershed -T1 -brain_atlas {params.atlas_gca} {input.talairach_lta} -h 3 {input.T1} {output.automask}
cp {output.automask} {output.mask}
"""
# NON-RECON-ALL
# Sometimes the brain mask includes the pial surface or other stuff we don't
# want. Here I use the conservative approach of removing bone and other tissue
# if it is present in the brainmask, otherwise don't touch. This assumes that
# the SPM segmentation is correct, of course. So it may introduce a bias.
rule fix_brainmask_with_seg_from_spm:
input:
spm_nonbrain=config["SPM_NONBRAIN"],
brainmask="mri/brainmask.nofix.mgz"
output:
brainmask_fixed="mri/brainmask.mgz"
run:
import nibabel as nib
import numpy as np
nonbrain = nib.load(input["spm_nonbrain"])
brainmask = nib.load(input["brainmask"])
nonbrain_dat, brainmask_dat = nonbrain.get_fdata(), brainmask.get_fdata()
bmask_fixed = np.where(nonbrain_dat == 255, 0, brainmask_dat)
out = nib.MGHImage(bmask_fixed, header=brainmask.header, affine=brainmask.affine)
out.to_filename(output["brainmask_fixed"])
###############
# Autorecon 2 #
###############
############################
# Create normalized volume #
############################
# EM Registration computes transform to align volume to default GCA atlas.
# Generate Talairach LTA from brainmask and nu.mgz. Same as before but now without skull.
rule talairach_from_brainmask_and_nu:
params:
atlas_gca=f"{config['FREESURFER_HOME']}/average/RB_all_2020-01-02.gca",
input:
brainmask="mri/brainmask.mgz",
nu="mri/nu.mgz"
output:
talairach_lta="mri/transforms/talairach.lta"
threads:
12
shell:
"""
mri_em_register -uns 3 -mask {input.brainmask} {input.nu} {params.atlas_gca} {output.talairach_lta}
"""
# Create a normalized volume using the brain volume and an input gca file.
ruleorder: create_normalized_volume_within_brain_mask_with_ctrl_points > create_normalized_volume_within_brain_mask
# CA: Canonical -- mri_ca_normalize "Canonical Normalization"
rule create_normalized_volume_within_brain_mask_with_ctrl_points:
params:
atlas_gca=f"{config['FREESURFER_HOME']}/average/RB_all_2020-01-02.gca",
input:
ctrl_points="mri/ctrl_pts.mgz",
brainmask="mri/brainmask.mgz",
nu="mri/nu.mgz",
talairach_lta="mri/transforms/talairach.lta"
output:
norm="mri/norm.mgz"
threads:
12
shell:
"""
mri_ca_normalize -f {input.ctrl_points} -mask {input.brainmask} {input.nu} {params.atlas_gca} {input.talairach_lta} {output.norm}
"""
rule create_normalized_volume_within_brain_mask:
params:
atlas_gca=f"{config['FREESURFER_HOME']}/average/RB_all_2020-01-02.gca",
input:
brainmask="mri/brainmask.mgz",
nu="mri/nu.mgz",
talairach_lta="mri/transforms/talairach.lta"
output:
ctrl_points="mri/ctrl_pts.mgz",
norm="mri/norm.mgz"
threads:
16
shell:
"""
mri_ca_normalize -c {output.ctrl_points} -mask {input.brainmask} {input.nu} {params.atlas_gca} {input.talairach_lta} {output.norm}
"""
# This generates a *nonlinear transform* between norm and the atlas. This will be
# subsequently used by mri_ca_label to generate labels for cortical regions
# based on the atlas.
rule generate_multidimensional_talairach_transform:
params:
atlas_gca=f"{config['FREESURFER_HOME']}/average/RB_all_2020-01-02.gca"
input:
talairach_lta="mri/transforms/talairach.lta",
brainmask="mri/brainmask.mgz",
norm="mri/norm.mgz"
output:
talairach_m3z="mri/transforms/talairach.m3z"
threads:
32
shell:
"""
mri_ca_register -nobigventricles -T {input.talairach_lta} \
-align-after -mask {input.brainmask} \
{input.norm} {params.atlas_gca} {output.talairach_m3z}
"""
############################
# Subcortical Segmentation #
############################
# Label subcortical structures.
rule subcortical_segmentation:
params:
atlas_gca=f"{config['FREESURFER_HOME']}/average/RB_all_2020-01-02.gca"
input:
norm="mri/norm.mgz",
talairach_m3z="mri/transforms/talairach.m3z"
output:
aseg_noCC="mri/aseg.auto_noCCseg.mgz",
aseg_intensities="mri/aseg.auto_noCCseg.label_intensities.txt" # implicit output.
threads:
32
shell:
"""
mri_ca_label -relabel_unlikely 9 .3 -prior 0.5 -align {input.norm} {input.talairach_m3z} {params.atlas_gca} {output.aseg_noCC}
"""
################################
# Corpus Callosum Segmentation #
################################
# Note this is the first time that the subject name is explicitly mentioned in the pipeline,
# and the point where recon-all breaks referential transparency.
# From here onwards we cannot know only by reading the shell command what inputs
# a command may actually require.
rule cc_segmentation:
params:
subject=config["SUBJECT"]
input:
aseg_noCC="mri/aseg.auto_noCCseg.mgz"
output:
aseg_auto="mri/aseg.auto.mgz",
aseg_cc_lta="mri/transforms/cc_up.lta",
aseg_presurf="mri/aseg.presurf.nofix.mgz",
threads:
2
shell:
"""
mri_cc -aseg aseg.auto_noCCseg.mgz -o aseg.auto.mgz -lta mri/transforms/cc_up.lta {params.subject}
cp {output.aseg_auto} {output.aseg_presurf}
"""
# NON-RECON-ALL
# Sometimes aseg presurf gives too much to WM. Here we constrain it using SPM's segmentation.
rule fix_aseg_presurf_with_wm_seg_from_spm:
params:
threshold=config["SEGMENTATION"]["gray_low"] * 110 / 100 # 110 magic WM number.
input:
wm_seg=config["SPM_WM"],
aseg_presurf="mri/aseg.presurf.nofix.mgz"
output:
aseg_presurf_fixed="mri/aseg.presurf.mgz"
run:
import nibabel as nib
import numpy as np
wm = nib.load(input["wm_seg"])
aseg = nib.load(input["aseg_presurf"])
wm_dat, aseg_dat = wm.get_fdata(), aseg.get_fdata()
assert wm_dat.shape == aseg_dat.shape
WM_RH, GM_RH = 41, 42
WM_LH, GM_LH = 2, 3
aseg_fixed = np.where((aseg_dat == WM_RH) & (wm_dat < params["threshold"]), GM_RH, aseg_dat)
aseg_fixed = np.where( (aseg_dat == WM_LH) & (wm_dat < params["threshold"]), GM_LH, aseg_fixed)
out = nib.MGHImage(aseg_fixed, header=aseg.header, affine=aseg.affine)
out.to_filename(output["aseg_presurf_fixed"])
################################################################
# Again, intensity normalization, but taking ASEG into account #
################################################################
# Use the norm volume, and create a brain volume, making use of the aseg and
# masking with brainmask.
rule intensity_normalize_with_aseg:
input:
aseg_presurf="mri/aseg.presurf.mgz",
brainmask="mri/brainmask.mgz",
norm="mri/norm.mgz"
output:
brain="mri/brain.mgz"
threads:
8
shell:
"""
mri_normalize -seed 1234 -mprage -noconform -aseg {input.aseg_presurf} -mask {input.brainmask} {input.norm} {output.brain}
"""
###########################
# Create brain finalsurfs #
###########################
rule create_brainfinalsurfs:
input:
brain="mri/brain.mgz",
brainmask="mri/brainmask.mgz"
output:
brainfinalsurfs="mri/brain.finalsurfs.mgz"
shell:
"""
mri_mask -T 5 {input.brain} {input.brainmask} {output.brainfinalsurfs}
"""
#############################
# WHITE MATTER SEGMENTATION #
#############################
# Step 1. Denoise the image
rule denoise_brain:
input:
"mri/brain.mgz"
output:
"mri/antsdn.brain.mgz"
threads:
16
shell:
"""
AntsDenoiseImageFs -i {input} -o {output}
"""
# Step 2. Segment the denoised image
# ruleorder: segment_wm_with_spm > segment_wm
rule segment_wm:
params:
wsizemm=config["SEGMENTATION"]["wsizemm"],
wm_low=config["SEGMENTATION"]["wm_low"],
gray_hi=config["SEGMENTATION"]["gray_hi"],
gray_low=config["SEGMENTATION"]["gray_low"],
input:
"mri/antsdn.brain.mgz"
output:
wm_seg="mri/wm.seg.mgz",
segment_dat="mri/segment.dat"
threads:
12
shell:
"""
mri_segment -wsizemm {params.wsizemm} \
-dat {output.segment_dat} -gray_low {params.gray_low} \
-wm_low {params.wm_low} -gray_hi {params.gray_hi} \
-mprage {input} {output.wm_seg}
"""
# Step 3. Edit wm segmentation with aseg. Essentially add subcortical regions to wm.seg
rule add_subcortical_regions_to_wm_seg:
input:
wm_seg="mri/wm.seg.mgz",
brain="mri/brain.mgz",
aseg_presurf="mri/aseg.presurf.mgz"
output:
"mri/wm.asegedit.mgz"
threads:
4
shell:
"""
mri_edit_wm_with_aseg -keep-in {input.wm_seg} {input.brain} {input.aseg_presurf} {output}
"""
# Step 4. Fix WM segmentation so that neighbors of all voxels have a face in common:
# no edge or corner labels. This is an input for the potential WM surfaces.
rule wm_pre_tesselation:
input:
wm_aseg_edit="mri/wm.asegedit.mgz",
norm="mri/norm.mgz"
output:
"mri/wm.mgz"
threads:
4
shell:
"""
mri_pretess {input.wm_aseg_edit} wm {input.norm} {output}
"""
# Step 5. Create hemispheric cutting planes and fill white matter with specific
# values for surface tesselation.
# Filled devides the brain into left and right hemispheres, so we can operate on
# each of them individually.
rule fill_white_matter_with_values_for_tesselation:
input:
talairach_lta="mri/transforms/talairach.lta",
aseg_presurf="mri/aseg.presurf.mgz",
wm="mri/wm.mgz"
output:
"mri/filled.mgz"
threads:
8
shell:
"""
mri_fill -xform {input.talairach_lta} -segmentation {input.aseg_presurf} {input.wm} {output}
"""
######################
# Generate surfaces! #
######################
rule generate_initial_surfaces:
params:
fillval = lambda wildcards: 255 if wildcards['hemi'] == "lh" else 127
input:
filled="mri/filled.mgz",
norm="mri/norm.mgz"
output:
# This temp file has a slight different name from what recon-all
# generates just for our convenience and to help intuition.
filled_pretess=temp("mri/filled-pretess-{hemi}.mgz"),
surf_orig_nofix_predec="surf/{hemi}.orig.nofix.predec"
threads:
16
shell:
"""
mri_pretess {input.filled} {params.fillval} {input.norm} {output.filled_pretess}
mri_tessellate {output.filled_pretess} {params.fillval} {output.surf_orig_nofix_predec}
mris_extract_main_component {output.surf_orig_nofix_predec} {output.surf_orig_nofix_predec}
"""
# rule generate_initial_spm_surfaces:
# params:
# fillval = lambda wildcards: 255 if wildcards['hemi'] == "lh" else 127
# input:
# filled="mri/filled_spm.mgz",
# norm="mri/norm.mgz"
# output:
# # This temp file has a slight different name from what recon-all
# # generates just for our convenience and to help intuition.
# filled_pretess=temp("mri/filled-spm-pretess-{hemi}.mgz"),
# surf_orig_nofix_predec="surf/{hemi}.origspm.nofix.predec"
# threads:
# 16
# shell:
# """
# mri_pretess {input.filled} {params.fillval} {input.norm} {output.filled_pretess}
# mri_tessellate {output.filled_pretess} {params.fillval} {output.surf_orig_nofix_predec}
# mris_extract_main_component {output.surf_orig_nofix_predec} {output.surf_orig_nofix_predec}
# """
# The original surfaces are quite coarse, since they essentially only follow the initial
# white matter volumetric segmentation. This makes then more refined.
rule remesh_nofix_to_smaller_face_area:
params:
face_area = config["SURFACES"]["face_area"]
input:
"surf/{hemi}.orig.nofix.predec"
output:
"surf/{hemi}.orig.nofix"
shell:
"""
mris_remesh --desired-face-area {params.face_area} --input {input} --output {output}
"""
rule smooth_nofix:
input:
"surf/{hemi}.orig.nofix"
output:
"surf/{hemi}.smoothwm.nofix"
shell:
"""
mris_smooth -nw -seed 1234 {input} {output}
"""
rule inflate_nofix:
input:
"surf/{hemi}.smoothwm.nofix"
output:
"surf/{hemi}.inflated.nofix"
threads:
4
shell:
"""
mris_inflate -no-save-sulc -n 100 {input} {output}
"""
rule make_sphere_from_inflated_surf:
input:
"surf/{hemi}.inflated.nofix"
output:
"surf/{hemi}.qsphere.nofix"
threads:
4
shell:
"""
mris_sphere -q -p 6 -a 128 -seed 1234 {input} {output}
"""
# Again, broken referential transparency. Inputs are not really passed to program,
# but rather read from the folder.
rule fix_topology:
params:
subject=config["SUBJECT"],
hemi= lambda wildcards: wildcards["hemi"]
input:
sphere="surf/{hemi}.qsphere.nofix",
inflated="surf/{hemi}.qsphere.nofix",
nofix="surf/{hemi}.qsphere.nofix"
output:
expand("surf/{{hemi}}.defect_{defectfile}", defectfile=["borders", "chull", "labels"]),
premesh="surf/{hemi}.orig.premesh"
threads:
12
shell:
"""
mris_fix_topology -mgz -sphere qsphere.nofix -inflated inflated.nofix -orig orig.nofix -out orig.premesh -ga -seed 1234 {params.subject} {params.hemi}
"""
# The wrapper command defect2seg is a wrapper around mri_label2vol that generates a volume
# segmentation file showing the defects of the nofix initial surfaces.
# Here we don't call the wrapper but its commands directly:
rule defects_to_volume:
input:
orig="mri/orig.mgz",
# also called "defects" in the defect2seg wrapper
orig_nofix=expand("surf/{hemi}.orig.nofix", hemi=["lh", "rh"]),
defect_labels=expand("surf/{hemi}.defect_labels", hemi=["lh", "rh"])
output:
surface_defects="mri/surface.defects.mgz"
threads:
8
run:
for nofix, defect_labels in zip(input.orig_nofix, input.defect_labels):
shell("""
mri_label2vol --defects {nofix} {defect_labels} {input.orig} 1000 0 {output.surface_defects}
""")
# shell:
# """
# mri_label2vol --defects surf/lh.orig.nofix surf/lh.defect_labels {input.orig} 1000 0 {output.surface_defects}
# mri_label2vol --defects surf/rh.orig.nofix surf/rh.defect_labels {input.orig} 1000 0 {output.surface_defects}
# """
rule defects_to_pointsets:
input:
orig_nofix="surf/{hemi}.orig.nofix",
defect_labels="surf/{hemi}.defect_labels"
output:
pointset="surf/{hemi}.defects.pointset"
threads:
2
shell:
"""
mris_defects_pointset -s {input.orig_nofix} -d {input.defect_labels} -o {output.pointset}
"""
# There is visually very little difference between the premesh and orig.
# It looks almost as if the orig is more "continuous" or smooth, but their
# positioning should technically be the same.
rule remesh_fixed_surfaces_and_remove_interesections:
input:
"surf/{hemi}.orig.premesh"
output:
"surf/{hemi}.orig"
shell:
"""
mris_remesh --remesh --iters 3 --input {input} --output {output}
mris_remove_intersection {output} {output}
"""
# This will compute the gray/white statistics used to place white and pial surfaces.
# No idea why this has been changed to run on the premesh and not on the orig in FS 7.1.1 recon-all.
# When I compare both I get exactly the same results, so here we'll use orig, as per default in the standalone stats command.
rule compute_gwstats:
params:
wm_low=config["SEGMENTATION"]["wm_low"],
input:
finalsurfs="mri/brain.finalsurfs.mgz",
wm="mri/wm.mgz",
premesh="surf/{hemi}.orig"
output:
"surf/autodet.gw.stats.{hemi}.dat"
shell:
"""
mris_autodet_gwstats --o {output} --i {input.finalsurfs} \
--wm {input.wm} --surf {input.premesh} --min_border_white {params.wm_low}
"""
rule generate_preparc_surface:
params:
smooth_iterations=config["SURFACES"]["preparc_smoothing"],
max_cbv_dist=config["SURFACES"]["max_cbv_dist"],
input:
wm="mri/wm.mgz",
aseg_presurf="mri/aseg.presurf.mgz",
finalsurfs="mri/brain.finalsurfs.mgz",
stats="surf/autodet.gw.stats.{hemi}.dat",
orig_surf="surf/{hemi}.orig"
output:
"surf/{hemi}.white.preparc"
threads:
12
shell:
"""
mris_place_surface --adgws-in {input.stats} \
--max-cbv-dist {params.max_cbv_dist} \
--wm {input.wm} --invol {input.finalsurfs} --{wildcards.hemi} \
--i {input.orig_surf} --o {output} --white --seg {input.aseg_presurf} \
--nsmooth {params.smooth_iterations} --threads {threads}
"""
rule smooth_preaparc_surface:
input:
"surf/{hemi}.white.preparc"
output:
"surf/{hemi}.smoothwm"
shell:
"""
mris_smooth -n 3 -nw -seed 1234 {input} {output}
"""
rule inflate_preaparc_surface:
input:
"surf/{hemi}.smoothwm"
output:
"surf/{hemi}.inflated"
threads:
4
shell:
"""
mris_inflate -n 100 {input} {output}
"""
########################
# Create cortex labels #
########################
# This is because we have a rule to generate labels from fsaverage labels
# further down the line.
# This rule order says that we create cortex label with this rule and not the other.
ruleorder: create_cortex_label > generate_ba_label
# Use the segmentation file and the white surface to create a cortex label.
# The --label-cortex is undocumented, but can be seen with mri_label2label --help
rule create_cortex_label:
params:
with_hypamyg=0
input:
white_preaparc="surf/{hemi}.white.preparc",
aseg_presurf="mri/aseg.presurf.mgz"
output:
"label/{hemi}.cortex.label"
shell:
"""
mri_label2label --label-cortex {input.white_preaparc} {input.aseg_presurf} {params.with_hypamyg} {output}
"""
rule create_cortex_label_with_hypamyg:
params:
with_hypamyg=1
input:
white_preaparc="surf/{hemi}.white.preparc",
aseg_presurf="mri/aseg.presurf.mgz"
output:
"label/{hemi}.cortex+hypamyg.label"
shell:
"""
mri_label2label --label-cortex {input.white_preaparc} {input.aseg_presurf} {params.with_hypamyg} {output}
"""
##########################
# Cortical Parcellation! #
##########################
# Remember that an annotation file is nothing but a collection of label files.
rule cortical_parcellation:
params:
subject=config["SUBJECT"],
reg_atlas_gcs= lambda wildcards: f"{config['FREESURFER_HOME']}/average/{wildcards.hemi}.DKaparc.atlas.acfb40.noaparc.i12.2016-08-02.gcs",
input:
cortex_label="label/{hemi}.cortex.label",
aseg_presurf="mri/aseg.presurf.mgz",
reg="surf/{hemi}.sphere.reg"
output:
"label/{hemi}.aparc.annot"
threads:
16
shell:
"""
mris_ca_label -l {input.cortex_label} -aseg {input.aseg_presurf}\
-seed 1234 {params.subject} {wildcards.hemi} {input.reg} {params.reg_atlas_gcs} {output}
"""
###############################
# ########################### #
# # Create white surface!!! # #
# ########################### #
###############################
rule create_white_surface:
input:
placement_stats="surf/autodet.gw.stats.{hemi}.dat",
aseg_presurf="mri/aseg.presurf.mgz",
wm="mri/wm.mgz",
finalsurfs="mri/brain.finalsurfs.mgz",
white_preaparc="surf/{hemi}.white.preparc",
cortex_label="label/{hemi}.cortex.label",
aparc_annot="label/{hemi}.aparc.annot"
output:
white_surface="surf/{hemi}.white"
threads:
12
shell:
"""
mris_place_surface --adgws-in {input.placement_stats}\
--seg {input.aseg_presurf} --threads {threads} --wm {input.wm}\
--invol {input.finalsurfs} --{wildcards.hemi} --i {input.white_preaparc}\
--o {output.white_surface} --white --nsmooth 0 --rip-label {input.cortex_label}\
--rip-bg --rip-surf {input.white_preaparc} --aparc {input.aparc_annot}
"""
###############
# AUTORECON 3 #
###############
rule spherisize_preaparc_surface:
input:
"surf/{hemi}.inflated"
output:
"surf/{hemi}.sphere"
threads:
16
shell:
"""
mris_sphere -seed 1234 {input} {output}
"""
rule register_sphere_to_atlas:
params:
reg_atlas= lambda wildcards: f"{config['FREESURFER_HOME']}/average/{wildcards.hemi}.folding.atlas.acfb40.noaparc.i12.2016-08-02.tif"
input:
sphere="surf/{hemi}.sphere",
smoothed="surf/{hemi}.smoothwm"
output:
sphere_reg="surf/{hemi}.sphere.reg",
threads:
8
shell:
"""
mris_register -curv {input.sphere} {params.reg_atlas} {output.sphere_reg}
"""
#####################################
# Measure the curvature of surfaces #
#####################################
# TODO See mris_curvature in recon-all. It symlinks the preaparc to the actual
# white curvature measurement.
rule measure_preaparc_surface_curvature:
input:
"surf/{hemi}.white.preparc"
output:
mean_curvature="surf/{hemi}.white.preparc.H",
gaussian_curvature="surf/{hemi}.white.preparc.K"
shell:
"""
mris_curvature -w -seed 1234 {input}
"""
# This calls mris_curvature with options hidden from the user.
rule measure_inflated_surface_curvature:
params:
threshold=0.999,
averages=5
input:
"surf/{hemi}.inflated"
output:
mean_curvature="surf/{hemi}.inflated.H",
gaussian_curvature="surf/{hemi}.inflated.K"
shell:
"""
mris_curvature -seed 1234\
-thresh {params.threshold} -n -a {params.averages}\
-w -distances 10 10 {input}
"""
####################################
# Compute the Jacobian of surfaces #
####################################
rule compute_surface_jacobians:
input:
surf_preaparc="surf/{hemi}.white.preparc",
reg="surf/{hemi}.sphere.reg"
output:
jacobian="surf/{hemi}.jacobian_white"
shell:
"""
mris_jacobian {input.surf_preaparc} {input.reg} {output.jacobian}
"""
# Sample the template registration atlas on the surface and save the results in curv file format.
rule sample_reg_atlas_on_surface:
params:
reg_atlas= lambda wildcards: f"{config['FREESURFER_HOME']}/average/{wildcards.hemi}.folding.atlas.acfb40.noaparc.i12.2016-08-02.tif",
frame_number=6 # Mimicks recon-all
input:
"surf/{hemi}.sphere.reg"
output:
"surf/{hemi}.avg_curv"
shell:
"""
mris_paint -a 5 {params.reg_atlas}#{params.frame_number} {input} {output}
"""
##############################
# ########################## #
# # Create pial surface!!! # #
# ########################## #
##############################
# Note that in the single-subject stream with T2, the pial surface is refined,
# which we don't do here, hence ?h.pial.T1 and ?h.pial are the same.
rule create_pial_surface:
input:
placement_stats="surf/autodet.gw.stats.{hemi}.dat",
aseg_presurf="mri/aseg.presurf.mgz",
wm="mri/wm.mgz",
finalsurfs="mri/brain.finalsurfs.mgz",
white_surface="surf/{hemi}.white",
cortex_label="label/{hemi}.cortex.label",
cortex_with_hypamyg_label="label/{hemi}.cortex+hypamyg.label",
aparc_annot="label/{hemi}.aparc.annot"
output:
pial_surface_T1="surf/{hemi}.pial.T1"
threads:
12
shell:
"""
mris_place_surface --adgws-in {input.placement_stats}\
--seg {input.aseg_presurf} --threads {threads} --wm {input.wm}\
--invol {input.finalsurfs} --{wildcards.hemi} --i {input.white_surface}\
--o {output.pial_surface_T1} --pial --nsmooth 0\
--rip-label {input.cortex_with_hypamyg_label}\
--pin-medial-wall {input.cortex_label}\
--aparc {input.aparc_annot}\
--repulse-surf {input.white_surface} --white-surf {input.white_surface}
"""
# Change this rule to allow for refinement with T2.
rule pial_from_pial_T1:
input:
"surf/{hemi}.pial.T1"
output:
"surf/{hemi}.pial"
shell:
"""
cp {input} {output}
"""
#####################################################
# Compute curvature and thickness of final surfaces #
#####################################################
rule wm_curvature:
input:
"surf/{hemi}.white"
output:
"surf/{hemi}.curv"
shell:
"""
mris_place_surface --curv-map {input} 2 10 {output}
"""
rule pial_curvature:
input:
"surf/{hemi}.pial"
output:
"surf/{hemi}.curv.pial"
shell:
"""
mris_place_surface --curv-map {input} 2 10 {output}
"""
rule compute_thickness_of_final_surfaces:
input:
white="surf/{hemi}.white",
pial="surf/{hemi}.pial"
output:
"surf/{hemi}.thickness"
shell:
"""
mris_place_surface --thickness {input.white} {input.pial} 20 5 {output}