Skip to content

Commit

Permalink
Merge pull request #26 from juhuntenburg/lsd_lemon_v2.0
Browse files Browse the repository at this point in the history
v2.0 of the lsd_lemon preprocessing pipelines
  • Loading branch information
juhuntenburg committed May 2, 2017
2 parents 9a8c0a1 + 7b6f0a3 commit 9904065
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/lsd_lemon/lemon_resting.py
Expand Up @@ -33,7 +33,7 @@ def create_lemon_resting(subject, working_dir, data_dir, freesurfer_dir, out_dir
'fmap_mag' : 'nifti/lemon_resting/fmap_mag.nii.gz',
'anat_head' : 'preprocessed/anat/T1.nii.gz',
'anat_brain' : 'preprocessed/anat/T1_brain.nii.gz',
'brain_mask' : 'preprocessed/anat/T1_brain_mask.nii.gz'
'func_mask' : 'preprocessed/anat/func_mask.nii.gz'
}
selectfiles = Node(nio.SelectFiles(templates,
base_directory=data_dir),
Expand Down Expand Up @@ -101,7 +101,7 @@ def create_lemon_resting(subject, working_dir, data_dir, freesurfer_dir, out_dir
(selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
(moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
(fmap_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]),
(selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
(selectfiles, denoise, [('func_mask', 'inputnode.brain_mask'),
('anat_brain', 'inputnode.anat_brain')]),
(moco, denoise, [('outputnode.par_moco', 'inputnode.moco_par')]),
(fmap_coreg, denoise, [('outputnode.epi2anat_dat', 'inputnode.epi2anat_dat'),
Expand Down
4 changes: 2 additions & 2 deletions src/lsd_lemon/lsd_resting.py
Expand Up @@ -63,7 +63,7 @@ def fmap_info(scan_id):
'fmap_mag' : 'nifti/lsd_resting/{fmap_id}_mag.nii.gz',
'anat_head' : 'preprocessed/anat/T1.nii.gz',
'anat_brain' : 'preprocessed/anat/T1_brain.nii.gz',
'brain_mask' : 'preprocessed/anat/T1_brain_mask.nii.gz',
'func_mask' : 'preprocessed/anat/func_mask.nii.gz',
}
selectfiles = Node(nio.SelectFiles(templates,
base_directory=data_dir),
Expand Down Expand Up @@ -138,7 +138,7 @@ def fmap_info(scan_id):
(selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
(moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
(fmap_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]),
(selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
(selectfiles, denoise, [('func_mask', 'inputnode.brain_mask'),
('anat_brain', 'inputnode.anat_brain')]),
(fmap_coreg, denoise, [('outputnode.epi2anat_dat', 'inputnode.epi2anat_dat'),
('outputnode.unwarped_mean_epi2fmap', 'inputnode.unwarped_mean')]),
Expand Down
106 changes: 106 additions & 0 deletions src/lsd_lemon/mask_2mni.py
@@ -0,0 +1,106 @@
from nipype.pipeline.engine import Node, Workflow
import nipype.interfaces.utility as util
import nipype.interfaces.ants as ants
import nipype.interfaces.fsl as fsl
import nipype.interfaces.io as nio
import sys
import pandas as pd


'''
Project tsnr / brainmask from
individual structural to MNI152 2mm space
'''

#subject_list= sys.argv[1]

#with open(subject_list, 'r') as f:
# subjects = [line.strip() for line in f]

subjects = list(pd.read_csv('/home/raid3/huntenburg/workspace/lsd_data_paper/lsd_preproc.csv', dtype='str')['ID'])
subjects.sort()
#subjects = ['03820']


# local base and output directory
afs_dir = '/afs/cbs.mpg.de/projects/mar004_lsd-lemon-preproc/probands/'
base_dir = '/nobackup/ilz2/julia_2mni/working_dir_masks/'
ilz_dir = '/nobackup/ilz2/fix_mni/'
out_dir = '/nobackup/ilz2/julia_2mni/masks'

template ='/usr/share/fsl/data/standard/MNI152_T1_2mm_brain.nii.gz'

# workflow
mni = Workflow(name='mni')
mni.base_dir = base_dir
mni.config['execution']['crashdump_dir'] = mni.base_dir + "/crash_files"

# infosource to iterate over subjects
subject_infosource=Node(util.IdentityInterface(fields=['subject_id']),
name='subject_infosource')
subject_infosource.iterables=('subject_id', subjects)

# select files
templates_1={'mask': '{subject_id}/preprocessed/anat/T1_brain_mask.nii.gz',
#'affine': '{subject_id}/preprocessed/anat/transforms2mni/transform0GenericAffine.mat',
#'warp': '{subject_id}/preprocessed/anat/transforms2mni/transform1Warp.nii.gz',
}
selectfiles_1 = Node(nio.SelectFiles(templates_1,
base_directory=afs_dir),
name="selectfiles_1")

mni.connect([(subject_infosource, selectfiles_1, [('subject_id', 'subject_id')])])

# select files
templates_2={'affine': '{subject_id}/preprocessed/anat/transforms2mni/transform0GenericAffine.mat',
'warp': '{subject_id}/preprocessed/anat/transforms2mni/transform1Warp.nii.gz',
}
selectfiles_2 = Node(nio.SelectFiles(templates_2,
base_directory=ilz_dir),
name="selectfiles_2")

mni.connect([(subject_infosource, selectfiles_2, [('subject_id', 'subject_id')])])


# make filelist
translist = Node(util.Merge(2),
name='translist')
mni.connect([(selectfiles_2, translist, [('affine', 'in2'),
('warp', 'in1')])])



def make_name(sub):
return '%s_brainmask_mni.nii.gz' %(sub)

makename = Node(util.Function(input_names=['sub'],
output_names='fname',
function=make_name),
name='makename')

mni.connect([(subject_infosource, makename, [('subject_id', 'sub')])])

# apply all transforms
applytransform = Node(ants.ApplyTransforms(input_image_type = 3,
#output_image='rest_preprocessed2mni.nii.gz',
interpolation = 'NearestNeighbor',
invert_transform_flags=[False, False]),
name='applytransform')

applytransform.inputs.reference_image=template
mni.connect([(selectfiles_1, applytransform, [('mask', 'input_image')]),
(translist, applytransform, [('out', 'transforms')]),
(makename, applytransform, [('fname', 'output_image')])
])


# sink
sink = Node(nio.DataSink(base_directory=out_dir,
parameterization=False),
name='sink')

mni.connect([#(subject_infosource, sink, [(('subject_id', makebase, out_dir), 'base_directory')]),
(applytransform, sink, [('output_image', '@tsnr2mni')])
])

mni.run(plugin='MultiProc', plugin_args={'n_procs' : 10})
2 changes: 1 addition & 1 deletion src/lsd_lemon/run_structural.py
Expand Up @@ -25,7 +25,7 @@
working_dir = '/scr/ilz2/LEMON_LSD/working_dir_struct/' +subject+'/'
data_dir = '/scr/ilz2/LEMON_LSD/'+subject+'/'
out_dir = '/scr/ilz2/LEMON_LSD/'+subject+'/'
freesurfer_dir = '/scr/ilz2/LEMON_LSD/freesurfer/'
freesurfer_dir = '/scr/ilz2/LEMON_LSD/freesurfer/'
standard_brain = '/usr/share/fsl/5.0/data/standard/MNI152_T1_1mm_brain.nii.gz'

create_structural(subject=subject, working_dir=working_dir, data_dir=data_dir,
Expand Down
53 changes: 36 additions & 17 deletions src/lsd_lemon/struct_preproc/mgzconvert.py
Expand Up @@ -3,6 +3,7 @@
import nipype.interfaces.io as nio
import nipype.interfaces.freesurfer as fs
import nipype.interfaces.fsl as fsl
import os


'''
Expand All @@ -16,14 +17,14 @@ def create_mgzconvert_pipeline(name='mgzconvert'):

#inputnode
inputnode=Node(util.IdentityInterface(fields=['fs_subjects_dir',
'fs_subject_id'
'fs_subject_id',
]),
name='inputnode')

#outputnode
outputnode=Node(util.IdentityInterface(fields=['anat_head',
'anat_brain',
'brain_mask',
'func_mask',
'wmseg',
'wmedge']),
name='outputnode')
Expand All @@ -39,27 +40,44 @@ def create_mgzconvert_pipeline(name='mgzconvert'):
out_file='T1.nii.gz'),
name='head_convert')

# create brainmask from aparc+aseg with single dilation

# convert Freesurfer brain.finalsurf file to nifti
# grab finalsurf file
def grab_brain(fs_subjects_dir, fs_subject_id):
import os
brainfile = os.path.join(fs_subjects_dir, fs_subject_id,
'mri', 'brain.finalsurfs.mgz')
return os.path.abspath(brainfile)

brain_grab=Node(util.Function(input_names=['fs_subjects_dir',
'fs_subject_id'],
output_names=['brain_file'],
function=grab_brain),
name='brain_grab')

brain_convert=Node(fs.MRIConvert(out_type='niigz',
out_file='T1_brain.nii.gz'),
name='brain_convert')

# create brainmask from aparc+aseg with single dilation for functional data
# DIFFERENT APPROACHES TO MASK THE FUNCTIONAL AND STRUCTURAL DATA
# ARE USED FOR HISTORIC REASONS
def get_aparc_aseg(files):
for name in files:
if 'aparc+aseg' in name:
return name

brainmask = Node(fs.Binarize(min=0.5,
funcmask = Node(fs.Binarize(min=0.5,
dilate=1,
out_type='nii.gz'),
name='brainmask')
name='funcmask')


# fill holes in mask, smooth, rebinarize
fillholes = Node(fsl.maths.MathsCommand(args='-fillh -s 3 -thr 0.1 -bin',
out_file='T1_brain_mask.nii.gz'),
out_file='func_mask.nii.gz'),
name='fillholes')


# mask T1 with the mask
brain = Node(fsl.ApplyMask(out_file='T1_brain.nii.gz'),
name='brain')


# cortical and cerebellar white matter volumes to construct wm edge
# [lh cerebral wm, lh cerebellar wm, rh cerebral wm, rh cerebellar wm, brain stem]
Expand All @@ -76,17 +94,18 @@ def get_aparc_aseg(files):
# connections
mgzconvert.connect([(inputnode, fs_import, [('fs_subjects_dir','subjects_dir'),
('fs_subject_id', 'subject_id')]),
(fs_import, brainmask, [(('aparc_aseg', get_aparc_aseg), 'in_file')]),
(fs_import, head_convert, [('T1', 'in_file')]),
(inputnode, brain_grab, [('fs_subjects_dir', 'fs_subjects_dir'),
('fs_subject_id', 'fs_subject_id')]),
(brain_grab, brain_convert, [('brain_file', 'in_file')]),
(fs_import, wmseg, [(('aparc_aseg', get_aparc_aseg), 'in_file')]),
(brainmask, fillholes, [('binary_file', 'in_file')]),
(fillholes, brain, [('out_file', 'mask_file')]),
(head_convert, brain, [('out_file', 'in_file')]),
(fs_import, funcmask, [(('aparc_aseg', get_aparc_aseg), 'in_file')]),
(funcmask, fillholes, [('binary_file', 'in_file')]),
(wmseg, edge, [('binary_file', 'in_file'),
('binary_file', 'mask_file')]),
(head_convert, outputnode, [('out_file', 'anat_head')]),
(fillholes, outputnode, [('out_file', 'brain_mask')]),
(brain, outputnode, [('out_file', 'anat_brain')]),
(fillholes, outputnode, [('out_file', 'func_mask')]),
(brain_convert, outputnode, [('out_file', 'anat_brain')]),
(wmseg, outputnode, [('binary_file', 'wmseg')]),
(edge, outputnode, [('out_file', 'wmedge')])
])
Expand Down
2 changes: 1 addition & 1 deletion src/lsd_lemon/structural.py
Expand Up @@ -67,7 +67,7 @@ def create_structural(subject, working_dir, data_dir, freesurfer_dir, out_dir,
# ]),
(mgzconvert, sink, [('outputnode.anat_head', 'preprocessed.anat.@head'),
('outputnode.anat_brain', 'preprocessed.anat.@brain'),
('outputnode.brain_mask', 'preprocessed.anat.@brain_mask'),
('outputnode.func_mask', 'preprocessed.anat.@func_mask'),
('outputnode.wmedge', 'preprocessed.anat.@wmedge'),
#('outputnode.wmseg', 'preprocessed.mp2rage.brain_extraction.@wmseg')
]),
Expand Down
2 changes: 1 addition & 1 deletion src/lsd_lemon/structural_cbstools.py
Expand Up @@ -69,7 +69,7 @@ def create_structural(subject, working_dir, data_dir, freesurfer_dir, out_dir,
# ]),
(mgzconvert, sink, [('outputnode.anat_head', 'preprocessed.anat.@head'),
('outputnode.anat_brain', 'preprocessed.anat.@brain'),
('outputnode.brain_mask', 'preprocessed.anat.@brain_mask'),
('outputnode.func_mask', 'preprocessed.anat.@func_mask'),
('outputnode.wmedge', 'preprocessed.anat.@wmedge'),
#('outputnode.wmseg', 'preprocessed.mp2rage.brain_extraction.@wmseg')
]),
Expand Down

0 comments on commit 9904065

Please sign in to comment.