Pipeline for processing functional data of the 7T MELAS study

public public 1yr ago Version: 2 0 bookmarks

Snakemake workflow for processing functional data from the 7T MELAS study

Inputs:

  • participants.tsv with target subject IDs

  • For each target subject:

    • Freesurfer processed data

    • Functional data

Singularity containers required:

  • fMRIprep (for FreeSurfer, ADNI and FSL tools)

  • Connectome workbench

Authors

  • Roy AM Haast @royhaast

Usage

If you use this workflow in a paper, don't forget to give credits to the authors by citing the URL of this (original) repository and, if available, its DOI (see above).

Step 1: Obtain a copy of this workflow

  1. Create a new github repository using this workflow as a template .

  2. Clone the newly created repository to your local system, into the place where you want to perform the data analysis.

Step 2: Configure workflow

Configure the workflow according to your needs via editing the files in the config/ folder. Adjust config.yml to configure the workflow execution, and participants.tsv to specify your subjects.

Step 3: Install Snakemake

Install Snakemake using conda :

conda create -c bioconda -c conda-forge -n snakemake snakemake

For installation details, see the instructions in the Snakemake documentation .

Step 4: Execute workflow

Activate the conda environment:

conda activate snakemake

Test your configuration by performing a dry-run via

snakemake --use-singularity -n

Execute the workflow locally via

snakemake --use-singularity --cores $N

using $N cores or run it in a cluster environment via

snakemake --use-singularity --cluster qsub --jobs 100

or

snakemake --use-singularity --drmaa --jobs 100

If you are using Compute Canada, you can use the cc-slurm profile, which submits jobs and takes care of requesting the correct resources per job (including GPUs). Once it is set-up with cookiecutter, run:

snakemake --profile cc-slurm

Or, with neuroglia-helpers can get a 8-core, 32gb node and run locally there. First, get a node (default 8-core, 32gb, 3 hour limit):

regularInteractive 

Then, run:

snakemake --use-singularity --cores 8 --resources mem=32000

See the Snakemake documentation for further details.

Step 5: Investigate results

After successful execution, you can create a self-contained interactive HTML report with all results via:

snakemake --report report.html

This report can, e.g., be forwarded to your collaborators. An example (using some trivial test data) can be seen here .

Step 6: Commit changes

Whenever you change something, don't forget to commit the changes back to your github copy of the repository:

git commit -a
git push

Step 7: Obtain updates from upstream

Whenever you want to synchronize your workflow copy with new developments from upstream, do the following.

  1. Once, register the upstream repository in your local copy: git remote add -f upstream git@github.com:snakemake-workflows/{{cookiecutter.repo_name}}.git or git remote add -f upstream https://github.com/snakemake-workflows/{{cookiecutter.repo_name}}.git if you do not have setup ssh keys.

  2. Update the upstream version: git fetch upstream .

  3. Create a diff with the current version: git diff HEAD upstream/master workflow > upstream-changes.diff .

  4. Investigate the changes: vim upstream-changes.diff .

  5. Apply the modified diff via: git apply upstream-changes.diff .

  6. Carefully check whether you need to update the config files: git diff HEAD upstream/master config . If so, do it manually, and only where necessary, since you would otherwise likely overwrite your settings and samples.

Step 8: Contribute back

In case you have also changed or added steps, please consider contributing them back to the original repository:

  1. Fork the original repo to a personal or lab account.

  2. Clone the fork to your local system, to a different place than where you ran your analysis.

  3. Copy the modified files from your analysis to the clone of your fork, e.g., cp -r workflow path/to/fork . Make sure to not accidentally copy config file contents or sample sheets. Instead, manually update the example config files if necessary.

  4. Commit and push your changes to your fork.

  5. Create a pull request against the original repository.

Testing

TODO: create some test datasets

Code Snippets

 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
import numpy as np
import pandas as pd
import nibabel as nib
from nibabel.nifti1 import Nifti1Image
from nilearn.input_data import NiftiLabelsMasker

epi = nib.load(snakemake.input.vol)
csf = nib.load(snakemake.input.csf)
wm = nib.load(snakemake.input.wm)

img = Nifti1Image(csf.get_fdata(),epi.affine,csf.header)
masker = NiftiLabelsMasker(labels_img=img, standardize=False)
time_series1 = masker.fit_transform(epi)

img = Nifti1Image(wm.get_fdata(),epi.affine,wm.header)
masker = NiftiLabelsMasker(labels_img=img, standardize=False)
time_series2 = masker.fit_transform(epi)

# Concatenate timeseries
df1 = pd.DataFrame({'CSF': time_series1[:,0],'WhiteMatter': time_series2[:,0]})

# Load movement parameters (and their derivatives)
names = ['X','Y','Z','RotX','RotY','RotZ']
df2 = pd.read_csv(snakemake.input.movreg, names=names, header=None, delim_whitespace=True)

# Put everything together (excluding derivatives) and write to .tsv file for 'ciftify_clean_img' 
df_concat = pd.concat([df2, df1], axis=1)
df_concat.to_csv(snakemake.output[0],sep='\t')
 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
epi=$1
mni=$2

# Relevant transformations
gdc_warp=$3
mc_dir=$4
topup_dir=$5
bbr_mat=$6
fnirt_warp=$7

# Output paths
warped_mni=$8
out_dir=`dirname $warped_mni`

# Generate second warp: gradient distortion corrected to MNI
fslmaths $topup_dir/epi_fout.nii.gz -mul 0.0343097 $topup_dir/epi_fout_scaled.nii.gz
secondwarp=$out_dir/gdc_to_mni.nii.gz
convertwarp -s $topup_dir/epi_fout_scaled.nii.gz -d y- --premat=$bbr_mat --warp1=$fnirt_warp --ref=$mni --out=$secondwarp --relout --rel -v

# Generate seperate warp up to topup correction
intermediatewarp=$out_dir/gdc_to_topup.nii.gz
convertwarp -s $topup_dir/epi_fout_scaled.nii.gz -d y- --ref=${epi}_0000.nii.gz --out=$intermediatewarp --relout --rel -v

for file in $mc_dir/* ; do
    vol=`echo $file | rev | cut -d'_' -f1  | rev`
    epi_vol=${epi}_${vol}

    # Generate first warp: slicetiming corrected to gradient distortion corrected
    firstwarp=$out_dir/firstwarps/st_${vol}_to_gdc.nii.gz

    if [ ! -d $out_dir/firstwarps ] ; then mkdir -p $out_dir/firstwarps ; fi
    convertwarp --warp1=$gdc_warp --midmat=$file --ref=$epi_vol --out=$firstwarp --abs --relout -v

    # Combine with second warp: slicetiming corrected to MNI
    combinedwarp=$out_dir/combinedwarps/st_${vol}_to_MNI.nii.gz

    if [ ! -d $out_dir/combinedwarps ] ; then mkdir -p $out_dir/combinedwarps ; fi
    convertwarp --warp1=$firstwarp --warp2=$secondwarp --out=$combinedwarp --ref=$mni --rel --relout -v

    # Combine with intermediate warp: slicetiming corrected to topup correction
    topupwarp=$out_dir/topupwarps/st_${vol}_to_topup.nii.gz

    if [ ! -d $out_dir/topupwarps ] ; then mkdir -p $out_dir/topupwarps ; fi
    convertwarp --warp1=$firstwarp --warp2=$intermediatewarp --out=$topupwarp --ref=$epi_vol --rel --relout -v

    # Apply warps to MNI
    if [ ! -d $out_dir/warped_mni ] ; then mkdir -p $out_dir/warped_mni ; fi
    applywarp -i $epi_vol -r $mni -o $out_dir/warped_mni/epi_rest_${vol}_mni.nii.gz -w $combinedwarp --rel --interp=spline -v

    # Apply warps to topup correction
    if [ ! -d $out_dir/warped_topup ] ; then mkdir -p $out_dir/warped_topup ; fi    
    applywarp -i $epi_vol -r $epi_vol -o $out_dir/warped_topup/epi_rest_${vol}_topup.nii.gz -w $topupwarp --rel --interp=spline -v
done

# Merge warped output into 4D volume
fslmerge -tr $warped_mni `ls $out_dir/warped_mni/epi_rest_*_mni.nii.gz` 2.0
fslmerge -tr $out_dir/epi_rest_topup.nii.gz `ls $out_dir/warped_topup/epi_rest_*topup.nii.gz` 2.0
21
22
23
24
shell:
    "{params.fs_setup} mri_convert {input.parc} {output.parc_anat} && "
    "applywarp -i {output.parc_anat} -r {params.mni} -w {input.warp} -o {output.parc_mni} -d int --interp=nn  && "
    "flirt -in {output.parc_anat} -ref {input.epi} -applyxfm -init {input.inverse_mat} -datatype int -interp nearestneighbour -out {output.parc_epi}"
40
41
42
43
44
45
shell:
    "{params.fs_setup} mri_binarize --i {input} --match 2 41 --erode 2 --o {output.wm} && "
    "mri_binarize --i {input} --match 4 43 --erode 1 --o {output.csf} && "
    "mri_binarize --i {input} --match 8 10 11 12 13 16 17 18 26 28 47 49 50 51 52 53 54 58 60 --o {output.rois} && "
    "mri_binarize --i {input} --match 0 --inv --o {output.brain} && "
    "fslmaths {output.rois} -mul {input} {output.rois}"
54
55
shell:
    "wb_command -volume-label-import {input.rois} {input.labels} {output}"
65
66
shell:
    "wb_command -cifti-create-label {output} -volume {input.rois} {input.rois} -left-label {input.lh_mmp} -right-label {input.rh_mmp}"
80
81
shell:
    "wb_command -cifti-create-dense-timeseries {output} -volume {input.vol} {input.rois} -left-metric {input.lh} -right-metric {input.rh} -timestep 2.0"
95
96
shell:
    "wb_command -cifti-create-dense-timeseries {output} -volume {input.vol} {input.rois} -left-metric {input.lh} -right-metric {input.rh} -timestep 2.0"
110
111
script:
    "../../scripts/extract_confounds.py"
SnakeMake From line 110 of rules/cifti.smk
129
130
131
shell:
    "ciftify_clean_img --verbose --output-file={output} --confounds-tsv={input.confounds} "
    "--smooth-fwhm=3 --left-surface {input.lh_surf} --right-surface {input.rh_surf} {params} {input.dtseries} &> {log}"
SnakeMake From line 129 of rules/cifti.smk
145
146
shell:
    'ciftify_clean_img --verbose --output-file={output} --confounds-tsv={input.confounds} {params} {input.dtseries} &> {log}'
SnakeMake From line 145 of rules/cifti.smk
158
159
shell:
    "wb_command -cifti-parcellate {input.dtseries} {input.dlabels} COLUMN {output}"
SnakeMake From line 158 of rules/cifti.smk
169
170
shell:
    "wb_command -cifti-correlation {input} {output}"
SnakeMake From line 169 of rules/cifti.smk
21
22
23
shell:
    "{params.fs_setup} bbregister --s {wildcards.subject} --mov {input.epi} --reg {output.reg} --o {output.vol} {params.optional} && "
    "tkregister2 --noedit --reg {output.reg} --mov {input.epi} --targ {input.t1w} --fslregout {output.mat} &> {log}"      
38
39
40
shell:
    "convert_xfm -omat {output.inverse_mat} -inverse {input.mat} && "
    "flirt -interp spline -in {input.t1w} -ref {input.epi} -applyxfm -init {output.inverse_mat} -out {output.t1w_coreg}"
55
56
shell:
    "{params.fs_setup} mri_vol2vol --mov {input.epi} --targ {input.targ} --o {output} --reg {input.bbr} --no-resample"
74
75
shell:
    "flirt {params.optional} -in {input} -ref {params.MNI} -omat {output} -out {params.prefix} &> {log}"
 98
 99
100
shell:
    "fnirt --in={input.t1w} --ref={params.MNI} --aff={input.affine} --fout={output.fout} --jout={output.jout} "
    "--refout={output.refout} --iout={output.iout} --logout={output.logout} --intout={output.intout} --cout={output.cout} -v &> {log}"
113
114
shell:
    "invwarp -w {input.warp} -o {output} -r {input.t1w} --noconstraint"
136
137
138
shell:
    "bash scripts/onestepresampling.sh {params.epi} {params.mni} {input.gdc} {params.mat} "
    "{input.topup} {input.bbr} {input.fnirt} {output.warped_mni} &> {log}"
154
155
156
shell:
    "python /opt/ICA-AROMA/ICA_AROMA.py -o `dirname {output.den}` -i {input.vol} -mc {input.mc} "
    "-a {input.bbr} -w {input.fnirt} -m {input.mask} -tr 2 -den nonaggr -overwrite"
168
169
shell:
    "applywarp -i {input.vol} -r {input.mni} -o {output} -w {input.warp} --rel --interp=spline -v"
15
16
17
18
shell:
    "TR=`fslval {input[0]} pixdim4` && "
    "3dTshift -prefix {output.corrected} -tpattern @{params.slicetiming} -TR $TR -tzero 0.0 {input} && "
    "fslsplit {output.corrected} `echo {output.splitted} | rev | cut -d'_' -f2-  | rev`_ -t &> {log}"
35
36
37
shell:
    "fslroi {input} {output.firstvol} 0 1 && "
    "procGradCorrect -i {output.firstvol} -g {params.coeff} -w {output.warp} -s {params.scratch} &> {log}"
53
54
55
56
57
58
59
shell:
    "applywarp -i {input.epi} -o {output.unwarped} -r {input.epi} -w {input.warp} --abs --interp=spline && "
    "fslroi {output.unwarped} {output.jacobian} 0 1 && "
    "reg_jacobian -ref {output.jacobian} -def {input.warp} -jac {output.jacobian} && "
    "fslmaths {output.jacobian} -mul -1 -abs {output.jacobian} && "
    "fslmaths {output.unwarped} -mul {output.jacobian} {output.corrected} && "
    "fslcpgeom {input.epi} {output.corrected}"
72
73
74
shell:
    "fslroi {input[0]} {output.first_vols} 0 5 && "
    "fslmerge -tr {output.concat_out} {output.first_vols} {input[1]} 2"
89
90
91
92
shell:
    "mkdir -p {output} && "
    "topup --imain={input} --datain={params.acquisition} --config={params.topup} "
    "--out={output}/epi --fout={output}/epi_fout --iout={output}/epi_iout -v &> {log}"
108
109
shell:
    "mcflirt -in {input} -out {output.vol} {params.optional} &> {log}"
128
129
130
131
shell:
    "fslroi {input.epi} {output.firstvol} 0 1 && "
    "applytopup --imain={output.firstvol} --inindex=1 --datain={params.acquisition} --topup={input.topup}/epi --out={output.firstvol_gdc} --method=jac -v &> {log} && "
    "applytopup --imain={input.epi} --inindex=1 --datain={params.acquisition} --topup={input.topup}/epi --out={output.gdc} --method=jac -v &> {log}"
137
138
shell:
    "bet {input.mask} {output} -f 0.3 -n -m -R"
10
11
shell:
    "{params.fs_setup} mris_convert {input} {output}"
20
21
shell:
    "wb_command -surface-cortex-layer {input.white} {input.pial} 0.5 {output}"      
32
33
shell: 
    "{params.fs_setup} mri_info {input} --tkr2scanner > {output}"
44
45
shell:
    "wb_command -convert-affine -from-flirt {input.bbr} {input.t1w} {input.epi} -to-world {output}"
55
56
shell: 
    "wb_command -surface-apply-affine {input.surf} {input.tkr2scanner} {output}"    
66
67
shell:     
    "wb_command -surface-apply-affine {input.surf} {input.scanner2epi} {output}"
78
79
shell:
    "wb_command -surface-apply-warpfield {input.surf} {input.inverse_warp} {output} -fnirt {input.warp}"
91
92
shell:    
    "wb_command -surface-resample {input.surf} {input.sphere_old} {input.sphere_new} BARYCENTRIC {output}"
103
104
shell:     
    "wb_command -surface-generate-inflated {input} {output.inflated} {output.very_inflated} {params}"   
119
120
shell: 
    "wb_command -volume-to-surface-mapping {input.epi} {input.surf} {output} -trilinear"
133
134
shell: 
    "wb_command -volume-to-surface-mapping {input.epi} {input.surf} {output} -trilinear"
149
150
shell:     
    "wb_command -metric-resample {input.metric} {input.sphere_old} {input.sphere_new} ADAP_BARY_AREA {output} -area-surfs {input.area_old} {input.area_new}"
163
164
shell:     
    "wb_command -metric-resample {input.metric} {input.sphere_old} {input.sphere_new} ADAP_BARY_AREA {output} -area-surfs {input.area_old} {input.area_new}"
ShowHide 42 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/khanlab-snakemake/smk-melas
Name: smk-melas
Version: 2
Badge:
workflow icon

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

Other Versions:
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 ...