Data Analysis and Visualization for High-Resolution Hippocampal Perfusion

public public 1yr ago 0 bookmarks

Hippocampal perfusion

This repository contains the data analysis code and (visualization) notebooks related to the preprint:

"Novel insights into hippocampal perfusion using high-resolution, multi-modal 7T MRI"

Code Snippets

 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
run:
    import numpy as np
    import nibabel as nib

    LR = nib.load(input[0])
    LR_labels = LR.get_fdata()
    LR_relabeled = np.zeros(LR_labels.shape)

    relabeling = [[1, 1],[2, 2],[20, 3],
                  [21, 4],[22, 5],[23, 6]]

    for labels in relabeling:
        LR_relabeled[LR_labels==labels[0]] = labels[1]

    img = nib.Nifti1Image(LR_relabeled.astype(np.int8), header=LR.header, affine=LR.affine)
    nib.save(img, output[0])
37
38
39
shell:
    'reg_aladin -flo {input.flo} -ref {input.ref} -res {output.warped} -aff {output.xfm_ras} -rigOnly -nac && '
    'c3d_affine_tool {output.xfm_ras} -oitk {output.xfm_itk}'
53
54
55
shell:
    'ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS={threads} '
    'antsApplyTransforms -d 3 --interpolation NearestNeighbor -i {input.nii} -o {output} -r {input.ref} -t {input.xfm}'     
66
67
shell:
    "c3d {input} -split -foreach -smooth 0.3mm -endfor -merge -o {output}"
 9
10
shell:
    "fslmaths {input} -bin {output}"
29
30
shell:
    "{params.merge_cmd}"    
40
41
42
43
shell:
    "wb_command -metric-reduce {input} MEAN {output.avg} -only-numeric &&"
    "wb_command -metric-reduce {input} STDEV {output.std} -only-numeric &&"
    "wb_command -metric-reduce {input} COV {output.cov} -only-numeric "   
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
run:
    import numpy as np
    import nibabel as nib
    from scipy.stats import variation 

    metric = nib.load(input[0])
    data = np.zeros((len(metric.darrays[0].data),len(input)))

    gii_merged = nib.gifti.GiftiImage()

    for i,in_metric in enumerate(input):
        data[:,i] = nib.load(in_metric).darrays[0].data
        gii_merged.add_gifti_data_array(
            nib.gifti.GiftiDataArray(data=data[:,i].astype(np.float32)
            )
        )       
    nib.save(gii_merged, output.merged)
    shell("wb_command -set-structure {output.merged} CORTEX_RIGHT")

    metric.darrays[0].data = np.nanmean(data,1)
    nib.save(metric, output.avg)

    metric.darrays[0].data = np.nanstd(data,1)
    nib.save(metric, output.std)

    metric.darrays[0].data = np.nanstd(data,1)/np.nanmean(data,1)
    nib.save(metric, output.cov)
103
104
shell:
    "wb_command -metric-gradient {input.midthickness} {input.metric} {output} -presmooth {params.sigma} -average-normals"
SnakeMake From line 103 of rules/common.smk
7
shell: "mris_convert {input} {output}"
15
shell: "mri_convert {input} {output}"
23
shell: 'mri_info {input} --tkr2scanner > {output}'
33
shell: 'wb_command -surface-apply-affine {input.surf} {input.tkr2scanner} {output}'
46
shell: 'wb_command -surface-resample {input.surf} {input.current_sphere} {input.new_sphere} {params.method} {output}'
56
shell: 'wb_command -surface-cortex-layer {input.white} {input.pial} 0.5 {output}'
66
67
shell:
    "wb_command -surface-generate-inflated {input} {output.inflated} {output.very_inflated} -iterations-scale 0.75"
82
83
84
85
shell: 
    """
    wb_command -label-resample {input.label} {input.current_sphere} {input.new_sphere} {params.method} {output} -area-surfs {input.current_surf} {input.new_surf}
    """
 98
 99
100
shell:
    'wb_command -label-to-volume-mapping {input.label} {input.surf} {input.vol_ref} {output}'
    ' -ribbon-constrained {input.white_surf} {input.pial_surf} -greedy'
7
8
shell:
    "wb_command -surface-vertex-areas {input} {output}"    
15
16
shell:
    "wb_command -surface-vertex-areas {input} {output}"    
25
26
27
28
shell:    
    """
    wb_command -metric-math x/y {output} -var x {input.native} -var y {input.unfolded}
    """
39
40
shell:
    "wb_command -surface-smoothing {input} {params.strength} {params.iterations} {output}"
47
48
shell:
    "wb_command -surface-curvature {input} -mean {output}"     
58
59
shell:
    "wb_command -surface-to-surface-3d-distance {input.outer} {input.inner} {output}"  
72
73
74
75
shell:    
    "wb_command -surface-to-surface-3d-distance {input.avg} {input.subject} {output.distance} -vectors {output.displacement} && "
    "wb_command -metric-math 'metric' {output.distance} -fixnan 0 -var metric {output.distance} && "
    "wb_command -metric-math 'metric' {output.displacement} -fixnan 0 -var metric {output.displacement}"
 95
 96
 97
 98
 99
100
101
shell:
    """
    jupyter nbconvert \
        --TagRemovePreprocessor.enabled=True \
        --TagRemovePreprocessor.remove_cell_tags snakemake-job-properties \
        --to html {input}
    """
14
15
shell:
    "bash ./{params.script} {wildcards.subject} {input.gradcorrect} {params.mp2rage_correction}"     
29
30
shell:
    "bash ./{params.script} -i {input.inv2} -u {input.t1w} -o `dirname {output}`" 
44
45
shell:
    "bash {params.script} -i {input}"
56
57
shell:
    "cp {input.nii} {output.nii} && cp {input.json} {output.json}"
70
71
72
73
74
75
76
77
78
79
80
81
82
    shell:
        "bash scripts/skullstripping/skullstrip.sh {params.script} {input} `realpath {output}` " #&> {log} {params.out_dir}  

# Apply the brain mask
rule apply_brain_mask:
    input: 
        t1w = rules.mprageise.output,
        brain_mask = rules.skull_stripping.output
    output: 'results/freesurfer/sub-{subject}/mri/orig/001.mgz'
    group: 'preprocessing'
    singularity: config['singularity_freesurfer']
    shell:
        "mri_mask {input.t1w} {input.brain_mask} {output}"    
101
102
103
shell:
    "export SUBJECTS_DIR={params.sd} && "
    "recon-all -all -s sub-{wildcards.subject} -no-wsgcaatlas -notal-check -threads 8" 
116
117
shell:
    "c3d_affine_tool -ref {input.ref} -src {input.src} {input.xfm} -fsl2ras -oitk {output}"
134
135
136
shell:
    "ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS={threads} "
    "antsApplyTransforms -d 3 --interpolation Linear -i {input.nii} -o {output} -r {input.ref} -t {input.xfm2crop} -t {input.xfm2ref} -t {input.xfm2tse}" 
144
145
shell:
    "c3d {input} -flip x -o  {output}"
161
162
shell:
    "wb_command -volume-to-surface-mapping {input.nii} {input.midthickness} {output} -ribbon-constrained {input.outer} {input.inner} -volume-roi {input.ribbon}"
174
175
shell:
    "wb_command -metric-math '(t1w/t2w)' {output} -var t1w {input.t1w} -var t2w {input.t2w} -fixnan 0"
13
14
shell:
    "c3d_affine_tool -ref {input.ref} -src {input.src} {input.xfm} -fsl2ras -oitk {output}"
27
28
29
30
31
32
shell:
    """
    ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS={threads}
    antsApplyTransforms -d 3 --interpolation Linear -i {input.nii} -o {output} -r {input.ref} \
        -t {input.xfm2crop} -t {input.xfm2ref} -t {input.xfm2tse}
    """
39
40
shell:
    "c3d {input} -flip x -o {output}"
52
53
54
55
56
57
58
59
60
61
shell:
    """
    if [ {wildcards.tpl_res} = 'hires' ] ; then 
        wb_command -volume-to-surface-mapping {input.nii} {input.midthickness} {output} \
            -ribbon-constrained {input.outer} {input.inner} -volume-roi {input.ribbon}
    else
        wb_command -volume-to-surface-mapping {input.nii} {input.midthickness} {output} \
            -trilinear
    fi
    """
68
69
shell:
    "AverageImages 3 {output} 0 {input}"  
82
83
84
85
86
87
shell:
    """
    ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS={threads}
    antsApplyTransforms -d 3 --interpolation Linear -i {input.nii} -o {output} -r {input.ref} \
        -t {input.xfm2crop} -t {input.xfm2ref} -t {input.xfm2tse}
    """
94
95
shell:
    "c3d {input} -flip x -o {output}"
107
108
109
110
111
112
113
114
115
116
shell:
    """
    if [ {wildcards.tpl_res} = 'hires' ] ; then 
        wb_command -volume-to-surface-mapping {input.nii} {input.midthickness} {output} \
            -ribbon-constrained {input.outer} {input.inner} -volume-roi {input.ribbon}
    else
        wb_command -volume-to-surface-mapping {input.nii} {input.midthickness} {output} \
            -trilinear
    fi
    """
126
127
shell:
    "fslsplit {input} {params.prefix} -t"
139
140
141
142
143
144
145
146
147
148
shell:
    """
    hemi={wildcards.hemi}
    in_dir=`dirname {input.nii}`
    out_dir=`dirname {output}`
    for file in $in_dir/*.nii.gz ; do
        out_file=`basename $file`
        antsApplyTransforms -d 3 --interpolation Linear -i $file -o $out_dir/$out_file -r {input.ref}  -t {input.xfm2crop} -t {input.xfm2ref} -t {input.xfm2tse}
    done
    """
155
156
157
158
159
160
161
162
163
shell:
    """
    in_dir=`dirname {input}`
    out_dir=`dirname {output}`
    for file in $in_dir/*.nii.gz ; do
        out_file=`basename $file`
        c4d $file -flip x -o $out_dir/$out_file
    done
    """
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
shell:
    """
    in_dir=`dirname {input.nii}`
    out_dir=`dirname {output}`
    exclude="0098 0197 0296 0395 0494 0593 0692 0791"
    for file in $in_dir/*.nii.gz ; do
        fname=`basename $file .nii.gz`
        vol=${{fname:10:4}}   

        if [[ $exclude != *$vol* ]] ; then
            out_file=${{fname}}.shape.gii
            wb_command -volume-to-surface-mapping $file {input.midthickness} $out_dir/$out_file -trilinear
        fi
    done
    """    
200
201
shell:
    "{params.merge_cmd}"    
212
213
214
215
216
shell:
    """
    ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS={threads}
    antsApplyTransforms -d 3 --interpolation Linear -i {input.nii} -o {output} -r {input.nii} -t {input.xfm}
    """
227
228
229
230
shell:
    """           
    wb_command -volume-to-surface-mapping {input.nii} {input.midthickness} {output} -trilinear        
    """
 9
10
shell:
    "mri_convert {input.t1w} {output} -nc -rl {input.fs}" 
23
24
shell:   
    "reg_aladin -flo {input.src} -ref {input.ref} -aff {output.xfm} -rigOnly -nac"
36
37
shell:
    "c3d_affine_tool {input.xfm} -oitk {output}"
55
56
57
shell:
    "ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS={threads} "
    "antsApplyTransforms -d 3 --interpolation Linear -i {input.nii} -o {output} -r {input.ref} -t {input.xfm2crop} -t {input.xfm2ref} -t {input.xfm2tse} -t {input.xfm2hires}" 
65
66
shell:
    "c3d {input} -flip x -o  {output}"
82
83
shell:
    "wb_command -volume-to-surface-mapping {input.nii} {input.midthickness} {output} -ribbon-constrained {input.outer} {input.inner} -volume-roi {input.ribbon}"
14
15
16
shell:
    "ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS={threads} "
    "antsApplyTransforms -d 3 --interpolation Linear -i {input.nii} -o {output} -r {input.ref}  -t {input.xfm} -t {input.init}"
SnakeMake From line 14 of rules/t2w.smk
24
25
shell:
    "c3d {input} -flip x -o  {output}"
41
42
shell:
    "wb_command -volume-to-surface-mapping {input.nii} {input.midthickness} {output} -ribbon-constrained {input.outer} {input.inner} -volume-roi {input.ribbon}"
SnakeMake From line 41 of rules/t2w.smk
ShowHide 57 more snippets with no or duplicated tags.

Login to post a comment if you would like to share your experience with this workflow.

Do you know this workflow well? If so, you can request seller status , and start supporting this workflow.

Free

Created: 1yr ago
Updated: 1yr ago
Maitainers: public
URL: https://github.com/royhaast/hippocampal_perfusion
Name: hippocampal_perfusion
Version: 1
Badge:
workflow icon

Insert copied code into your website to add a link to this workflow.

Downloaded: 0
Copyright: Public Domain
License: None
  • Future updates

Related Workflows

cellranger-snakemake-gke
snakemake workflow to run cellranger on a given bucket using gke.
A Snakemake workflow for running cellranger on a given bucket using Google Kubernetes Engine. The usage of this workflow ...