Beispiel #1
0
def create_resting(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                   vol_to_remove, TR, epi_resolution, highpass, lowpass,
                   echo_space, pe_dir, standard_brain,
                   standard_brain_resampled, standard_brain_mask,
                   standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='resting_postscrub')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution'][
        'crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {
        'epi_scrubbed_interp':
        'preprocessing/preprocessed/{subject}/scrubbed_interpolated/rest2anat_denoised_scrubbed_intep.nii.gz',
        'anat_head':
        'preprocessing/preprocessed/{subject}/structural/T1.nii.gz',
        'anat_brain':
        'preprocessing/preprocessed/{subject}/structural/brain.nii.gz',
        'brain_mask':
        'preprocessing/preprocessed/{subject}/structural/T1_brain_mask.nii.gz',
        'ants_affine':
        'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform0GenericAffine.mat',
        'ants_warp':
        'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform1Warp.nii.gz'
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject = subject

    # node to remove first volumes
    #    remove_vol = Node(util.Function(input_names=['in_file', 't_min'],
    #                                    output_names=["out_file"],
    #                                    function=strip_rois_func),
    #                      name='remove_vol')
    #    remove_vol.inputs.t_min = vol_to_remove
    #    # workflow for motion correction
    #    moco = create_moco_pipeline()
    #
    #    # workflow for fieldmap correction and coregistration
    #    topup_coreg = create_topup_coreg_pipeline()
    #    topup_coreg.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    #    topup_coreg.inputs.inputnode.fs_subject_id = subject
    #    topup_coreg.inputs.inputnode.echo_space = echo_space
    #    topup_coreg.inputs.inputnode.pe_dir = pe_dir
    #
    #    # workflow for applying transformations to timeseries
    #    transform_ts = create_transform_pipeline()
    #    transform_ts.inputs.inputnode.resolution = epi_resolution
    #
    #
    #    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    # https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    # workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain
    ants_registration.inputs.inputnode.tr_sec = TR

    # FL added fullspectrum
    # workflow to transform fullspectrum timeseries to MNI
    #ants_registration_full = create_ants_registration_pipeline('ants_registration_full')
    #ants_registration_full.inputs.inputnode.ref = standard_brain
    #ants_registration_full.inputs.inputnode.tr_sec = TR

    # workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    # visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain

    # sink to store files
    sink = Node(nio.DataSink(
        parameterization=False,
        base_directory=out_dir,
        substitutions=[('rest_denoised_bandpassed_norm_trans.nii.gz',
                        'rest_scrubbed_int_mni_unsmoothed.nii.gz'),
                       ('rest_denoised_bandpassed_norm_trans_smooth.nii.gz',
                        'rest_scrubbed_int_mni_smoothed.nii.gz'),
                       ('rest_denoised_bandpassed_norm.nii.gz',
                        'rest_denoised_scrubbed_int_bp.nii.gz')]),
                name='sink')

    # connections
    func_preproc.connect([
        # remove the first volumes
        #        (selectfiles, remove_vol, [('func', 'in_file')]),
        #
        #        # align volumes and motion correction
        #        (remove_vol, moco, [('out_file', 'inputnode.epi')]),
        #
        #        # prepare field map
        #        (selectfiles, topup_coreg, [('ap', 'inputnode.ap'),
        #                                   ('pa', 'inputnode.pa'),
        #                                   ('anat_head', 'inputnode.anat_head'),
        #                                   ('anat_brain', 'inputnode.anat_brain')
        #                                   ]),
        #        (moco, topup_coreg, [('outputnode.epi_mean', 'inputnode.epi_mean')]),
        #
        #        # transform timeseries
        #        (remove_vol, transform_ts, [('out_file', 'inputnode.orig_ts')]),
        #        (selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
        #        (selectfiles, transform_ts, [('brain_mask', 'inputnode.brain_mask')]),
        #        (moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
        #        (topup_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]),
        #
        #        # correct slicetiming
        #        # FIXME slice timing?
        #        # (transform_ts, slicetiming, [('outputnode.trans_ts_masked', 'inputnode.ts')]),
        #        # (slicetiming, denoise, [('outputnode.ts_slicetcorrected', 'inputnode.epi_coreg')]),
        #        (transform_ts, denoise, [('outputnode.trans_ts_masked', 'inputnode.epi_coreg')]),

        # denoise data
        (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
                                ('anat_brain', 'inputnode.anat_brain'),
                                ('epi_scrubbed_interp',
                                 'inputnode.epi_denoised')]),
        (denoise, ants_registration, [('outputnode.normalized_file',
                                       'inputnode.denoised_ts')]),

        # registration to MNI space
        (selectfiles, ants_registration, [('ants_affine',
                                           'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')
                                          ]),

        # FL added fullspectrum
        #(selectfiles, ants_registration_full, [('epi_scrubbed_interp', 'inputnode.denoised_ts')]),
        #(selectfiles, ants_registration_full, [('ants_affine', 'inputnode.ants_affine')]),
        #(selectfiles, ants_registration_full, [('ants_warp', 'inputnode.ants_warp')]),
        (ants_registration, smoothing, [('outputnode.ants_reg_ts',
                                         'inputnode.ts_transformed')]),
        (smoothing, visualize, [('outputnode.ts_smoothed',
                                 'inputnode.ts_transformed')]),

        ##all the output
        #        (moco, sink, [  # ('outputnode.epi_moco', 'realign.@realigned_ts'),
        #                        ('outputnode.par_moco', 'realign.@par'),
        #                        ('outputnode.rms_moco', 'realign.@rms'),
        #                        ('outputnode.mat_moco', 'realign.MAT.@mat'),
        #                        ('outputnode.epi_mean', 'realign.@mean'),
        #                        ('outputnode.rotplot', 'realign.plots.@rotplot'),
        #                        ('outputnode.transplot', 'realign.plots.@transplot'),
        #                        ('outputnode.dispplots', 'realign.plots.@dispplots'),
        #                        ('outputnode.tsnr_file', 'realign.@tsnr')]),
        #        (topup_coreg, sink, [('outputnode.fmap', 'coregister.transforms2anat.@fmap'),
        #                            # ('outputnode.unwarpfield_epi2fmap', 'coregister.@unwarpfield_epi2fmap'),
        #                            ('outputnode.unwarped_mean_epi2fmap', 'coregister.@unwarped_mean_epi2fmap'),
        #                            ('outputnode.epi2fmap', 'coregister.@epi2fmap'),
        #                            # ('outputnode.shiftmap', 'coregister.@shiftmap'),
        #                            ('outputnode.fmap_fullwarp', 'coregister.transforms2anat.@fmap_fullwarp'),
        #                            ('outputnode.epi2anat', 'coregister.@epi2anat'),
        #                            ('outputnode.epi2anat_mat', 'coregister.transforms2anat.@epi2anat_mat'),
        #                            ('outputnode.epi2anat_dat', 'coregister.transforms2anat.@epi2anat_dat'),
        #                            ('outputnode.epi2anat_mincost', 'coregister.@epi2anat_mincost')
        #                            ]),
        #
        #        (transform_ts, sink, [('outputnode.trans_ts_masked', 'coregister.@full_transform_ts'),
        #                              ('outputnode.trans_ts_mean', 'coregister.@full_transform_mean'),
        #                              ('outputnode.resamp_brain', 'coregister.@resamp_brain')]),
        (
            denoise,
            sink,
            [
                ('outputnode.normalized_file', 'denoise.@normalized'),
                # FL added fullspectrum
            ]),
        (ants_registration, sink, [('outputnode.ants_reg_ts',
                                    'ants.@antsnormalized')]),
        #(ants_registration_full, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized_fullspectrum')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    func_preproc.write_graph(dotfilename='func_preproc.dot',
                             graph2use='colored',
                             format='pdf',
                             simple_form=True)
    func_preproc.run()
Beispiel #2
0
def create_hc_connec(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                     epi_resolution, standard_brain, standard_brain_resampled,
                     standard_brain_mask, standard_brain_mask_resampled,
                     fwhm_smoothing, side, TR, highpass, lowpass):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    hc_connec = Workflow(name='hc_connec_thr099_scrubbed')
    hc_connec.base_dir = working_dir
    hc_connec.config['execution'][
        'crashdump_dir'] = hc_connec.base_dir + "/crash_files"

    # select files
    templates = {
        #'rest_head': 'resting_state/denoise/rest_preprocessed_nativespace.nii.gz', #denoised and bandpass-filtered native space (2x2x2mm) image
        'rest2anat_scrubbed':
        'preprocessing/preprocessed/{subject}/scrubbed_interpolated/rest2anat_denoised_scrubbed_intep.nii.gz',  #denoised, scrubbed, interp, bp-filtered native space
        'ants_affine':
        'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform0GenericAffine.mat',
        'ants_warp':
        'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform1Warp.nii.gz',
        'scrubvols': 'quality_reports/poldrack_reports/{subject}/scrubvols.txt'
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject = subject

    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    # https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    #drop scrubbed volumes
    scrub_volumes = Node(util.Function(
        input_names=['scrubvols', 'in_file', 'working_dir'],
        output_names=['filename_scrubbed_img'],
        function=scrub_timepoints),
                         name='scrub_volumes')
    scrub_volumes.inputs.working_dir = working_dir

    #get T1 brainmask
    get_T1_brainmask = create_get_T1_brainmask()
    get_T1_brainmask.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    get_T1_brainmask.inputs.inputnode.fs_subject_id = subject

    #workflow to extract HC and transform into individual space
    transform_hc = create_transform_hc()
    transform_hc.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    transform_hc.inputs.inputnode.fs_subject_id = subject
    transform_hc.inputs.inputnode.resolution = 2
    transform_hc.inputs.inputnode.working_dir = working_dir

    #workflow to extract timeseries and correlate
    corr_ts = create_corr_ts()

    #workflow to tranform correlations to MNI space
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain  #_resampled: 2x2x2mm brain for RSV

    #
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing
    #sink to store files
    sink = Node(
        nio.DataSink(parameterization=True, base_directory=out_dir),
        #   substitutions=[('_binarize', 'binarize'), -> don't really seem to work and I don't know why.
        #                   #('_binarize', 'anterior_hc'),
        #                   ('_ants_reg1', 'posterior_hc'),
        #                   #('_ants_reg', 'anterior_hc'),
        #                   ('_smooth1', 'posterior_hc'),
        #                   ('_smooth0', 'anterior_hc'),
        #                   ('corr_Z_trans', 'corr_Z_MNI')],
        name='sink')

    sink.inputs.substitutions = [('_binarize0', 'posterior_hc'),
                                 ('_binarize1', 'anterior_hc'),
                                 ('_ants_reg0', 'posterior_hc'),
                                 ('_ants_reg1', 'anterior_hc'),
                                 ('_smooth0', 'posterior_hc'),
                                 ('_smooth1', 'anterior_hc'),
                                 ('_apply_FisherZ0', 'posterior_hc'),
                                 ('_apply_FisherZ1', 'anterior_hc')]

    # connections
    hc_connec.connect([
        #bandpass-filtering implemented after scrubbing and replacement!
        (selectfiles, scrub_volumes, [('scrubvols', 'scrubvols')]),
        (get_T1_brainmask, transform_hc, [('outputnode.T1',
                                           'inputnode.anat_head')]),
        (transform_hc, corr_ts, [('outputnode.hc_transformed_bin',
                                  'inputnode.hc_mask')]),
        (selectfiles, denoise, [('rest2anat_scrubbed',
                                 'inputnode.epi_denoised')]),
        (denoise, scrub_volumes, [('outputnode.normalized_file', 'in_file')]),
        (scrub_volumes, corr_ts, [('filename_scrubbed_img', 'inputnode.ts')]),
        (corr_ts, sink,
         [('outputnode.corrmap_z', 'hc_connectivity_thr099.scrubbed.' + side +
           '.corr.nativespace.@transformed')]),
        (corr_ts, ants_registration, [('outputnode.corrmap_z',
                                       'inputnode.corr_Z')]),
        (selectfiles, ants_registration, [('ants_affine',
                                           'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')
                                          ]),
        (ants_registration, sink,
         [('outputnode.ants_reg_corr_Z',
           'hc_connectivity_thr099.scrubbed.' + side + '.corr.ants')]),
        (ants_registration, smoothing, [('outputnode.ants_reg_corr_Z',
                                         'inputnode.ts_transformed')]),
        (smoothing, sink,
         [('outputnode.ts_smoothed',
           'hc_connectivity_thr099.scrubbed.' + side + '.corr.smoothed')]),
    ])

    hc_connec.run(
    )  #it can't run in multiproc as in one moment one file is hardcoded and saved to the disk which is
def create_rsfMRI_preproc_pipeline(working_dir, freesurfer_dir, ds_dir, use_fs_brainmask, name='rsfMRI_preprocessing'):
    # initiate workflow
    rsfMRI_preproc_wf = Workflow(name=name)
    rsfMRI_preproc_wf.base_dir = os.path.join(working_dir, 'LeiCA_resting')
    ds_dir = os.path.join(ds_dir, name)

    # set fsl output
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')

    # inputnode
    inputnode = Node(util.IdentityInterface(fields=['epi',
                                                    't1w',
                                                    'subject_id',
                                                    'TR_ms',
                                                    'vols_to_drop',
                                                    'lat_ventricle_mask_MNI',
                                                    'lp_cutoff_freq',
                                                    'hp_cutoff_freq']),
                     name='inputnode')

    # outputnode
    outputnode = Node(util.IdentityInterface(fields=['epi_moco',
                                                     'rs_preprocessed',
                                                     'epi_2_MNI_warp']),
                      name='outputnode')


    # MOCO
    moco = create_moco_pipeline(working_dir, ds_dir, 'motion_correction')
    rsfMRI_preproc_wf.connect(inputnode, 'epi', moco, 'inputnode.epi')
    rsfMRI_preproc_wf.connect(inputnode, 'vols_to_drop', moco, 'inputnode.vols_to_drop')



    # STRUCT PREPROCESSING
    struct_preproc = create_struct_preproc_pipeline(working_dir, freesurfer_dir, ds_dir, use_fs_brainmask, 'struct_preproc')
    rsfMRI_preproc_wf.connect(inputnode, 't1w', struct_preproc, 'inputnode.t1w')
    rsfMRI_preproc_wf.connect(inputnode, 'subject_id', struct_preproc, 'inputnode.subject_id')



    # REGISTRATIONS
    reg = create_registration_pipeline(working_dir, freesurfer_dir, ds_dir, 'registration')
    rsfMRI_preproc_wf.connect(moco, 'outputnode.initial_mean_epi_moco', reg, 'inputnode.initial_mean_epi_moco')
    rsfMRI_preproc_wf.connect(inputnode, 't1w', reg, 'inputnode.t1w')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.t1w_brain', reg, 'inputnode.t1w_brain')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.wm_mask_4_bbr', reg, 'inputnode.wm_mask_4_bbr')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.struct_brain_mask', reg, 'inputnode.struct_brain_mask')
    rsfMRI_preproc_wf.connect(inputnode, 'subject_id', reg, 'inputnode.subject_id')
    rsfMRI_preproc_wf.connect(reg, 'outputnode.epi_2_MNI_warp', outputnode, 'epi_2_MNI_warp')



    # DESKULL EPI
    deskull = create_deskull_pipeline(working_dir, ds_dir, 'deskull')
    rsfMRI_preproc_wf.connect(moco, 'outputnode.epi_moco', deskull, 'inputnode.epi_moco')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.struct_brain_mask', deskull, 'inputnode.struct_brain_mask')
    rsfMRI_preproc_wf.connect(reg, 'outputnode.struct_2_epi_mat', deskull, 'inputnode.struct_2_epi_mat')




    # DENOISE
    denoise = create_denoise_pipeline(working_dir, ds_dir, 'denoise')
    rsfMRI_preproc_wf.connect(inputnode, 'TR_ms', denoise, 'inputnode.TR_ms')
    rsfMRI_preproc_wf.connect(inputnode, 'subject_id', denoise, 'inputnode.subject_id')
    rsfMRI_preproc_wf.connect(inputnode, 'lat_ventricle_mask_MNI', denoise, 'inputnode.lat_ventricle_mask_MNI')
    rsfMRI_preproc_wf.connect(moco, 'outputnode.par_moco', denoise, 'inputnode.par_moco')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.epi_deskulled', denoise, 'inputnode.epi')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.mean_epi', denoise, 'inputnode.mean_epi')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.brain_mask_epiSpace', denoise, 'inputnode.brain_mask_epiSpace')
    rsfMRI_preproc_wf.connect(reg, 'outputnode.struct_2_epi_mat', denoise, 'inputnode.struct_2_epi_mat')
    rsfMRI_preproc_wf.connect(reg, 'outputnode.MNI_2_epi_warp', denoise, 'inputnode.MNI_2_epi_warp')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.wm_mask', denoise, 'inputnode.wm_mask')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.csf_mask', denoise, 'inputnode.csf_mask')
    rsfMRI_preproc_wf.connect(inputnode, 'lp_cutoff_freq', denoise, 'inputnode.lp_cutoff_freq')
    rsfMRI_preproc_wf.connect(inputnode, 'hp_cutoff_freq', denoise, 'inputnode.hp_cutoff_freq')

    rsfMRI_preproc_wf.connect(denoise, 'outputnode.rs_preprocessed', outputnode, 'rs_preprocessed')



    # QC
    qc = create_qc_pipeline(working_dir, ds_dir, 'qc')
    rsfMRI_preproc_wf.connect(inputnode, 'subject_id', qc, 'inputnode.subject_id')
    rsfMRI_preproc_wf.connect(moco, 'outputnode.par_moco', qc, 'inputnode.par_moco')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.epi_deskulled', qc, 'inputnode.epi_deskulled')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.brain_mask_epiSpace', qc, 'inputnode.brain_mask_epiSpace')
    rsfMRI_preproc_wf.connect([(struct_preproc, qc, [('outputnode.t1w_brain', 'inputnode.t1w_brain'),
                                                     ('outputnode.struct_brain_mask', 'inputnode.struct_brain_mask')])])
    rsfMRI_preproc_wf.connect([(reg, qc, [('outputnode.mean_epi_structSpace', 'inputnode.mean_epi_structSpace'),
                                          ('outputnode.mean_epi_MNIspace', 'inputnode.mean_epi_MNIspace'),
                                          ('outputnode.struct_MNIspace', 'inputnode.struct_MNIspace'),
                                          ('outputnode.struct_2_MNI_warp', 'inputnode.struct_2_MNI_warp')])])
    rsfMRI_preproc_wf.connect(denoise, 'outputnode.outlier_files', qc, 'inputnode.outlier_files')
    rsfMRI_preproc_wf.connect(denoise, 'outputnode.rs_preprocessed', qc, 'inputnode.rs_preprocessed')

    rsfMRI_preproc_wf.write_graph(dotfilename=rsfMRI_preproc_wf.name, graph2use='orig', format='pdf')
    rsfMRI_preproc_wf.write_graph(dotfilename=rsfMRI_preproc_wf.name, graph2use='colored', format='pdf')

    return rsfMRI_preproc_wf
def create_lemon_resting(subject, working_dir, data_dir, data_dir_WDR, freesurfer_dir, out_dir,
    vol_to_remove, TR, epi_resolution, highpass, lowpass,
    echo_space, te_diff, pe_dir, standard_brain, standard_brain_resampled, standard_brain_mask, 
    standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution']['crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
   
   
       # select files
    templates={
    'anat_brain' : 'preprocessed/mod/anat/brain.nii.gz', 
    'brain_mask' : 'preprocessed/mod/anat/T1_brain_mask.nii.gz',
    'ants_affine': 'preprocessed/mod/anat/transforms2mni/transform0GenericAffine.mat',
    'ants_warp':   'preprocessed/mod/anat/transforms2mni/transform1Warp.nii.gz',

    }
         
    templates_WDR={
    'par_moco': 'lemon_resting/motion_correction/mcflirt/rest_realigned.nii.gz.par',
    'trans_ts': 'lemon_resting/transform_timeseries/merge/rest2anat.nii.gz',
    'epi2anat_dat': 'lemon_resting/fmap_coreg/bbregister/rest2anat.dat',
    'unwarped_mean_epi2fmap': 'lemon_resting/fmap_coreg/applywarp0/rest_mean2fmap_unwarped.nii.gz',
     
    }
    
    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),    name="selectfiles")
    selectfiles_WDR = Node(nio.SelectFiles(templates_WDR, base_directory=data_dir_WDR),    name="selectfiles_WDR")
   
      
       
          
    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma= 1./(2*TR*highpass)
    denoise.inputs.inputnode.lowpass_sigma= 1./(2*TR*lowpass)
    #https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR
    
    #workflow to transform timeseries to MNI
    ants_registration=create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref=standard_brain_resampled    
    
    #workflow to smooth
    smoothing = create_smoothing_pipeline() 
    smoothing.inputs.inputnode.fwhm=fwhm_smoothing
   
    #workflow to slice time in the end as a try
    slicetiming = create_slice_timing_pipeline() 
    
    #visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template=standard_brain_resampled 
    

    
    #sink to store files
    sink = Node(nio.DataSink(parameterization=False,
    base_directory=out_dir,
    substitutions=[('fmap_phase_fslprepared', 'fieldmap'),
    ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
    ('plot.rest_coregistered', 'outlier_plot'),
    ('filter_motion_comp_norm_compcor_art_dmotion', 'nuissance_matrix'),
    ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
    ('rest_realigned.nii.gz.par','rest_realigned.par'),
    ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
    ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
    ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
    ('art.rest_coregistered_outliers', 'outliers'),
    ('global_intensity.rest_coregistered', 'global_intensity'),
    ('norm.rest_coregistered', 'composite_norm'),
    ('stats.rest_coregistered', 'stats'),
    ('rest_denoised_bandpassed_norm.nii.gz', 'rest_preprocessed_nativespace.nii.gz'),
    ('rest_denoised_bandpassed_norm_trans.nii.gz', 'rest_mni_unsmoothed.nii.gz'),
    ('rest_denoised_bandpassed_norm_trans_smooth.nii', 'rest_mni_smoothed.nii')]),
    name='sink')
    
    
    # connections
    func_preproc.connect([
    
    #correct slicetiming
    (selectfiles_WDR, slicetiming, [('trans_ts', 'inputnode.ts')]),
    (slicetiming, denoise, [('outputnode.ts_slicetcorrected','inputnode.epi_coreg')]),
    
    #denoise data
    (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
    ('anat_brain', 'inputnode.anat_brain')]),
    (selectfiles_WDR, denoise, [('par_moco', 'inputnode.moco_par')]),
    (selectfiles_WDR, denoise, [('epi2anat_dat', 'inputnode.epi2anat_dat'),
    ('unwarped_mean_epi2fmap', 'inputnode.unwarped_mean')]),
    (denoise, ants_registration, [('outputnode.normalized_file', 'inputnode.denoised_ts')]),
        
   
    #registration to MNI space
    (selectfiles, ants_registration, [('ants_affine', 'inputnode.ants_affine')] ),
    (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')] ),

    (ants_registration, smoothing, [('outputnode.ants_reg_ts', 'inputnode.ts_transformed')]),

    (smoothing, visualize,  [('outputnode.ts_smoothed', 'inputnode.ts_transformed')]),


    ##all the output
    (denoise, sink, [
    ('outputnode.wmcsf_mask', 'denoise.mask.@wmcsf_masks'),
    ('outputnode.combined_motion','denoise.artefact.@combined_motion'),
    ('outputnode.outlier_files','denoise.artefact.@outlier'),
    ('outputnode.intensity_files','denoise.artefact.@intensity'),
    ('outputnode.outlier_stats','denoise.artefact.@outlierstats'),
    ('outputnode.outlier_plots','denoise.artefact.@outlierplots'),
    ('outputnode.mc_regressor', 'denoise.regress.@mc_regressor'),
    ('outputnode.comp_regressor', 'denoise.regress.@comp_regressor'),
    ('outputnode.mc_F', 'denoise.regress.@mc_F'),
    ('outputnode.mc_pF', 'denoise.regress.@mc_pF'),
    ('outputnode.comp_F', 'denoise.regress.@comp_F'),
    ('outputnode.comp_pF', 'denoise.regress.@comp_pF'),
    ('outputnode.brain_mask_resamp', 'denoise.mask.@brain_resamp'),
    ('outputnode.brain_mask2epi', 'denoise.mask.@brain_mask2epi'),
    ('outputnode.normalized_file', 'denoise.@normalized')
    ]),
    (ants_registration, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized')
    ]),
    (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    #func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run()
    # plugin='MultiProc'plugin='MultiProc'plugin='CondorDAGMan')
    #func_preproc.run()plugin='CondorDAGMan'plugin='CondorDAGMan'plugin='CondorDAGMan'plugin='CondorDAGMan'
#plugin='CondorDAGMan'
def create_lemon_resting(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                         vol_to_remove, TR, epi_resolution, highpass, lowpass,
                         echo_space, te_diff, pe_dir, standard_brain, standard_brain_resampled, standard_brain_mask,
                         standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution']['crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {'func': 'raw_data/{subject}/func/EPI_t2.nii',
                 'fmap_phase': 'raw_data/{subject}/unwarp/B0_ph.nii',
                 'fmap_mag': 'raw_data/{subject}/unwarp/B0_mag.nii',
                 'anat_head': 'preprocessed/{subject}/structural/T1.nii.gz',  # either with mod or without
                 'anat_brain': 'preprocessed/{subject}/structural/brain.nii.gz',
                 # new version with brain_extraction from freesurfer  #T1_brain_brain.nii.gz',
                 'brain_mask': 'preprocessed/{subject}/structural/T1_brain_mask.nii.gz',  # T1_brain_brain_mask.nii.gz',
                 'ants_affine': 'preprocessed/{subject}/structural/transforms2mni/transform0GenericAffine.mat',
                 'ants_warp': 'preprocessed/{subject}/structural/transforms2mni/transform1Warp.nii.gz'
                 }

    selectfiles = Node(nio.SelectFiles(templates,
                                       base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject = subject


    # node to remove first volumes
    remove_vol = Node(util.Function(input_names=['in_file', 't_min'],
                                    output_names=["out_file"],
                                    function=strip_rois_func),
                      name='remove_vol')
    remove_vol.inputs.t_min = vol_to_remove
    # workflow for motion correction
    moco = create_moco_pipeline()

    # workflow for fieldmap correction and coregistration
    fmap_coreg = create_fmap_coreg_pipeline()
    fmap_coreg.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    fmap_coreg.inputs.inputnode.fs_subject_id = subject
    fmap_coreg.inputs.inputnode.echo_space = echo_space
    fmap_coreg.inputs.inputnode.te_diff = te_diff
    fmap_coreg.inputs.inputnode.pe_dir = pe_dir

    # workflow for applying transformations to timeseries
    transform_ts = create_transform_pipeline()
    transform_ts.inputs.inputnode.resolution = epi_resolution


    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    # https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    # workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain_resampled
    ants_registration.inputs.inputnode.tr_sec = TR

    # FL added fullspectrum
    # workflow to transform fullspectrum timeseries to MNI
    ants_registration_full = create_ants_registration_pipeline('ants_registration_full')
    ants_registration_full.inputs.inputnode.ref = standard_brain_resampled
    ants_registration_full.inputs.inputnode.tr_sec = TR

    # workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    # visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain_resampled



    # sink to store files
    sink = Node(nio.DataSink(parameterization=False,
                             base_directory=out_dir,
                             substitutions=[('fmap_phase_fslprepared', 'fieldmap'),
                                            ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
                                            ('plot.rest_coregistered', 'outlier_plot'),
                                            ('filter_motion_comp_norm_compcor_art_dmotion', 'nuissance_matrix'),
                                            ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
                                            ('rest_realigned.nii.gz.par', 'rest_realigned.par'),
                                            ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
                                            ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
                                            ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
                                            ('art.rest_coregistered_outliers', 'outliers'),
                                            ('global_intensity.rest_coregistered', 'global_intensity'),
                                            ('norm.rest_coregistered', 'composite_norm'),
                                            ('stats.rest_coregistered', 'stats'),
                                            ('rest_denoised_bandpassed_norm.nii.gz',
                                             'rest_preprocessed_nativespace.nii.gz'),
                                            ('rest_denoised_bandpassed_norm_trans.nii.gz',
                                             'rest_mni_unsmoothed.nii.gz'),
                                            ('rest_denoised_bandpassed_norm_trans_smooth.nii',
                                             'rest_mni_smoothed.nii'),
                                            # FL added
                                            ('rest2anat_masked.nii.gz', 'rest_coregistered_nativespace.nii.gz'),
                                            ('rest2anat_denoised.nii.gz',
                                             'rest_preprocessed_nativespace_fullspectrum.nii.gz'),
                                            ('rest2anat_denoised_trans.nii.gz',
                                             'rest_mni_unsmoothed_fullspectrum.nii.gz')
                                            ]),
                name='sink')


    # connections
    func_preproc.connect([
        # remove the first volumes
        (selectfiles, remove_vol, [('func', 'in_file')]),

        # align volumes and motion correction
        (remove_vol, moco, [('out_file', 'inputnode.epi')]),

        # prepare field map
        (selectfiles, fmap_coreg, [('fmap_phase', 'inputnode.phase'),
                                   ('fmap_mag', 'inputnode.mag'),
                                   ('anat_head', 'inputnode.anat_head'),
                                   ('anat_brain', 'inputnode.anat_brain')
                                   ]),
        (moco, fmap_coreg, [('outputnode.epi_mean', 'inputnode.epi_mean')]),

        # transform timeseries
        (remove_vol, transform_ts, [('out_file', 'inputnode.orig_ts')]),
        (selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
        (selectfiles, transform_ts, [('brain_mask', 'inputnode.brain_mask')]),
        (moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
        (fmap_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]),

        # correct slicetiming
        # FIXME slice timing?
        # (transform_ts, slicetiming, [('outputnode.trans_ts_masked', 'inputnode.ts')]),
        # (slicetiming, denoise, [('outputnode.ts_slicetcorrected', 'inputnode.epi_coreg')]),
        (transform_ts, denoise, [('outputnode.trans_ts_masked', 'inputnode.epi_coreg')]),

        # denoise data
        (selectfiles, denoise, [('brain_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'),
                               ('outputnode.unwarped_mean_epi2fmap', 'inputnode.unwarped_mean')]),
        (denoise, ants_registration, [('outputnode.normalized_file', 'inputnode.denoised_ts')]),

        # registration to MNI space
        (selectfiles, ants_registration, [('ants_affine', 'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')]),

        # FL added fullspectrum
        (denoise, ants_registration_full, [('outputnode.ts_fullspectrum', 'inputnode.denoised_ts')]),
        (selectfiles, ants_registration_full, [('ants_affine', 'inputnode.ants_affine')]),
        (selectfiles, ants_registration_full, [('ants_warp', 'inputnode.ants_warp')]),

        (ants_registration, smoothing, [('outputnode.ants_reg_ts', 'inputnode.ts_transformed')]),

        (smoothing, visualize, [('outputnode.ts_smoothed', 'inputnode.ts_transformed')]),

        ##all the output
        (moco, sink, [  # ('outputnode.epi_moco', 'realign.@realigned_ts'),
                        ('outputnode.par_moco', 'realign.@par'),
                        ('outputnode.rms_moco', 'realign.@rms'),
                        ('outputnode.mat_moco', 'realign.MAT.@mat'),
                        ('outputnode.epi_mean', 'realign.@mean'),
                        ('outputnode.rotplot', 'realign.plots.@rotplot'),
                        ('outputnode.transplot', 'realign.plots.@transplot'),
                        ('outputnode.dispplots', 'realign.plots.@dispplots'),
                        ('outputnode.tsnr_file', 'realign.@tsnr')]),
        (fmap_coreg, sink, [('outputnode.fmap', 'coregister.transforms2anat.@fmap'),
                            # ('outputnode.unwarpfield_epi2fmap', 'coregister.@unwarpfield_epi2fmap'),
                            ('outputnode.unwarped_mean_epi2fmap', 'coregister.@unwarped_mean_epi2fmap'),
                            ('outputnode.epi2fmap', 'coregister.@epi2fmap'),
                            # ('outputnode.shiftmap', 'coregister.@shiftmap'),
                            ('outputnode.fmap_fullwarp', 'coregister.transforms2anat.@fmap_fullwarp'),
                            ('outputnode.epi2anat', 'coregister.@epi2anat'),
                            ('outputnode.epi2anat_mat', 'coregister.transforms2anat.@epi2anat_mat'),
                            ('outputnode.epi2anat_dat', 'coregister.transforms2anat.@epi2anat_dat'),
                            ('outputnode.epi2anat_mincost', 'coregister.@epi2anat_mincost')
                            ]),

        (transform_ts, sink, [('outputnode.trans_ts_masked', 'coregister.@full_transform_ts'),
                              ('outputnode.trans_ts_mean', 'coregister.@full_transform_mean'),
                              ('outputnode.resamp_brain', 'coregister.@resamp_brain')]),

        (denoise, sink, [
            ('outputnode.wmcsf_mask', 'denoise.mask.@wmcsf_masks'),
            ('outputnode.combined_motion', 'denoise.artefact.@combined_motion'),
            ('outputnode.outlier_files', 'denoise.artefact.@outlier'),
            ('outputnode.intensity_files', 'denoise.artefact.@intensity'),
            ('outputnode.outlier_stats', 'denoise.artefact.@outlierstats'),
            ('outputnode.outlier_plots', 'denoise.artefact.@outlierplots'),
            ('outputnode.mc_regressor', 'denoise.regress.@mc_regressor'),
            ('outputnode.comp_regressor', 'denoise.regress.@comp_regressor'),
            ('outputnode.mc_F', 'denoise.regress.@mc_F'),
            ('outputnode.mc_pF', 'denoise.regress.@mc_pF'),
            ('outputnode.comp_F', 'denoise.regress.@comp_F'),
            ('outputnode.comp_pF', 'denoise.regress.@comp_pF'),
            ('outputnode.brain_mask_resamp', 'denoise.mask.@brain_resamp'),
            ('outputnode.brain_mask2epi', 'denoise.mask.@brain_mask2epi'),
            ('outputnode.normalized_file', 'denoise.@normalized'),
            # FL added fullspectrum
            ('outputnode.ts_fullspectrum', 'denoise.@ts_fullspectrum')
        ]),
        (ants_registration, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized')]),
        (ants_registration_full, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized_fullspectrum')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run(plugin='CondorDAGMan', plugin_args={'initial_specs': 'request_memory = 1500'})
Beispiel #6
0
def create_rsfMRI_preproc_pipeline(working_dir,
                                   freesurfer_dir,
                                   ds_dir,
                                   use_fs_brainmask,
                                   name='rsfMRI_preprocessing'):
    # initiate workflow
    rsfMRI_preproc_wf = Workflow(name=name)
    rsfMRI_preproc_wf.base_dir = os.path.join(working_dir, 'LeiCA_resting')
    ds_dir = os.path.join(ds_dir, name)

    # set fsl output
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')

    # inputnode
    inputnode = Node(util.IdentityInterface(fields=[
        'epi', 't1w', 'subject_id', 'TR_ms', 'vols_to_drop',
        'lat_ventricle_mask_MNI', 'lp_cutoff_freq', 'hp_cutoff_freq'
    ]),
                     name='inputnode')

    # outputnode
    outputnode = Node(util.IdentityInterface(
        fields=['epi_moco', 'rs_preprocessed', 'epi_2_MNI_warp']),
                      name='outputnode')

    # MOCO
    moco = create_moco_pipeline(working_dir, ds_dir, 'motion_correction')
    rsfMRI_preproc_wf.connect(inputnode, 'epi', moco, 'inputnode.epi')
    rsfMRI_preproc_wf.connect(inputnode, 'vols_to_drop', moco,
                              'inputnode.vols_to_drop')

    # STRUCT PREPROCESSING
    struct_preproc = create_struct_preproc_pipeline(working_dir,
                                                    freesurfer_dir, ds_dir,
                                                    use_fs_brainmask,
                                                    'struct_preproc')
    rsfMRI_preproc_wf.connect(inputnode, 't1w', struct_preproc,
                              'inputnode.t1w')
    rsfMRI_preproc_wf.connect(inputnode, 'subject_id', struct_preproc,
                              'inputnode.subject_id')

    # REGISTRATIONS
    reg = create_registration_pipeline(working_dir, freesurfer_dir, ds_dir,
                                       'registration')
    rsfMRI_preproc_wf.connect(moco, 'outputnode.initial_mean_epi_moco', reg,
                              'inputnode.initial_mean_epi_moco')
    rsfMRI_preproc_wf.connect(inputnode, 't1w', reg, 'inputnode.t1w')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.t1w_brain', reg,
                              'inputnode.t1w_brain')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.wm_mask_4_bbr', reg,
                              'inputnode.wm_mask_4_bbr')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.struct_brain_mask',
                              reg, 'inputnode.struct_brain_mask')
    rsfMRI_preproc_wf.connect(inputnode, 'subject_id', reg,
                              'inputnode.subject_id')
    rsfMRI_preproc_wf.connect(reg, 'outputnode.epi_2_MNI_warp', outputnode,
                              'epi_2_MNI_warp')

    # DESKULL EPI
    deskull = create_deskull_pipeline(working_dir, ds_dir, 'deskull')
    rsfMRI_preproc_wf.connect(moco, 'outputnode.epi_moco', deskull,
                              'inputnode.epi_moco')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.struct_brain_mask',
                              deskull, 'inputnode.struct_brain_mask')
    rsfMRI_preproc_wf.connect(reg, 'outputnode.struct_2_epi_mat', deskull,
                              'inputnode.struct_2_epi_mat')

    # DENOISE
    denoise = create_denoise_pipeline(working_dir, ds_dir, 'denoise')
    rsfMRI_preproc_wf.connect(inputnode, 'TR_ms', denoise, 'inputnode.TR_ms')
    rsfMRI_preproc_wf.connect(inputnode, 'subject_id', denoise,
                              'inputnode.subject_id')
    rsfMRI_preproc_wf.connect(inputnode, 'lat_ventricle_mask_MNI', denoise,
                              'inputnode.lat_ventricle_mask_MNI')
    rsfMRI_preproc_wf.connect(moco, 'outputnode.par_moco', denoise,
                              'inputnode.par_moco')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.epi_deskulled', denoise,
                              'inputnode.epi')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.mean_epi', denoise,
                              'inputnode.mean_epi')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.brain_mask_epiSpace',
                              denoise, 'inputnode.brain_mask_epiSpace')
    rsfMRI_preproc_wf.connect(reg, 'outputnode.struct_2_epi_mat', denoise,
                              'inputnode.struct_2_epi_mat')
    rsfMRI_preproc_wf.connect(reg, 'outputnode.MNI_2_epi_warp', denoise,
                              'inputnode.MNI_2_epi_warp')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.wm_mask', denoise,
                              'inputnode.wm_mask')
    rsfMRI_preproc_wf.connect(struct_preproc, 'outputnode.csf_mask', denoise,
                              'inputnode.csf_mask')
    rsfMRI_preproc_wf.connect(inputnode, 'lp_cutoff_freq', denoise,
                              'inputnode.lp_cutoff_freq')
    rsfMRI_preproc_wf.connect(inputnode, 'hp_cutoff_freq', denoise,
                              'inputnode.hp_cutoff_freq')

    rsfMRI_preproc_wf.connect(denoise, 'outputnode.rs_preprocessed',
                              outputnode, 'rs_preprocessed')

    # QC
    qc = create_qc_pipeline(working_dir, ds_dir, 'qc')
    rsfMRI_preproc_wf.connect(inputnode, 'subject_id', qc,
                              'inputnode.subject_id')
    rsfMRI_preproc_wf.connect(moco, 'outputnode.par_moco', qc,
                              'inputnode.par_moco')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.epi_deskulled', qc,
                              'inputnode.epi_deskulled')
    rsfMRI_preproc_wf.connect(deskull, 'outputnode.brain_mask_epiSpace', qc,
                              'inputnode.brain_mask_epiSpace')
    rsfMRI_preproc_wf.connect([(struct_preproc, qc, [
        ('outputnode.t1w_brain', 'inputnode.t1w_brain'),
        ('outputnode.struct_brain_mask', 'inputnode.struct_brain_mask')
    ])])
    rsfMRI_preproc_wf.connect([(reg, qc, [
        ('outputnode.mean_epi_structSpace', 'inputnode.mean_epi_structSpace'),
        ('outputnode.mean_epi_MNIspace', 'inputnode.mean_epi_MNIspace'),
        ('outputnode.struct_MNIspace', 'inputnode.struct_MNIspace'),
        ('outputnode.struct_2_MNI_warp', 'inputnode.struct_2_MNI_warp')
    ])])
    rsfMRI_preproc_wf.connect(denoise, 'outputnode.outlier_files', qc,
                              'inputnode.outlier_files')
    rsfMRI_preproc_wf.connect(denoise, 'outputnode.rs_preprocessed', qc,
                              'inputnode.rs_preprocessed')

    rsfMRI_preproc_wf.write_graph(dotfilename=rsfMRI_preproc_wf.name,
                                  graph2use='orig',
                                  format='pdf')
    rsfMRI_preproc_wf.write_graph(dotfilename=rsfMRI_preproc_wf.name,
                                  graph2use='colored',
                                  format='pdf')

    return rsfMRI_preproc_wf
def create_lemon_resting(subject, working_dir, data_dir, data_dir_WDR,
                         freesurfer_dir, out_dir, vol_to_remove, TR,
                         epi_resolution, highpass, lowpass, echo_space,
                         te_diff, pe_dir, standard_brain,
                         standard_brain_resampled, standard_brain_mask,
                         standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution'][
        'crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files

    # select files
    templates = {
        'anat_brain': 'preprocessed/mod/anat/brain.nii.gz',
        'brain_mask': 'preprocessed/mod/anat/T1_brain_mask.nii.gz',
        'ants_affine':
        'preprocessed/mod/anat/transforms2mni/transform0GenericAffine.mat',
        'ants_warp':
        'preprocessed/mod/anat/transforms2mni/transform1Warp.nii.gz',
    }

    templates_WDR = {
        'par_moco':
        'lemon_resting/motion_correction/mcflirt/rest_realigned.nii.gz.par',
        'trans_ts':
        'lemon_resting/transform_timeseries/merge/rest2anat.nii.gz',
        'epi2anat_dat':
        'lemon_resting/fmap_coreg/bbregister/rest2anat.dat',
        'unwarped_mean_epi2fmap':
        'lemon_resting/fmap_coreg/applywarp0/rest_mean2fmap_unwarped.nii.gz',
    }

    selectfiles = Node(nio.SelectFiles(templates, base_directory=data_dir),
                       name="selectfiles")
    selectfiles_WDR = Node(nio.SelectFiles(templates_WDR,
                                           base_directory=data_dir_WDR),
                           name="selectfiles_WDR")

    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    #https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    #workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain_resampled

    #workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    #workflow to slice time in the end as a try
    slicetiming = create_slice_timing_pipeline()

    #visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain_resampled

    #sink to store files
    sink = Node(nio.DataSink(
        parameterization=False,
        base_directory=out_dir,
        substitutions=[
            ('fmap_phase_fslprepared', 'fieldmap'),
            ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
            ('plot.rest_coregistered', 'outlier_plot'),
            ('filter_motion_comp_norm_compcor_art_dmotion',
             'nuissance_matrix'),
            ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
            ('rest_realigned.nii.gz.par', 'rest_realigned.par'),
            ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
            ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
            ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
            ('art.rest_coregistered_outliers', 'outliers'),
            ('global_intensity.rest_coregistered', 'global_intensity'),
            ('norm.rest_coregistered', 'composite_norm'),
            ('stats.rest_coregistered', 'stats'),
            ('rest_denoised_bandpassed_norm.nii.gz',
             'rest_preprocessed_nativespace.nii.gz'),
            ('rest_denoised_bandpassed_norm_trans.nii.gz',
             'rest_mni_unsmoothed.nii.gz'),
            ('rest_denoised_bandpassed_norm_trans_smooth.nii',
             'rest_mni_smoothed.nii')
        ]),
                name='sink')

    # connections
    func_preproc.connect([

        #correct slicetiming
        (selectfiles_WDR, slicetiming, [('trans_ts', 'inputnode.ts')]),
        (slicetiming, denoise, [('outputnode.ts_slicetcorrected',
                                 'inputnode.epi_coreg')]),

        #denoise data
        (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
                                ('anat_brain', 'inputnode.anat_brain')]),
        (selectfiles_WDR, denoise, [('par_moco', 'inputnode.moco_par')]),
        (selectfiles_WDR, denoise, [('epi2anat_dat', 'inputnode.epi2anat_dat'),
                                    ('unwarped_mean_epi2fmap',
                                     'inputnode.unwarped_mean')]),
        (denoise, ants_registration, [('outputnode.normalized_file',
                                       'inputnode.denoised_ts')]),

        #registration to MNI space
        (selectfiles, ants_registration, [('ants_affine',
                                           'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')
                                          ]),
        (ants_registration, smoothing, [('outputnode.ants_reg_ts',
                                         'inputnode.ts_transformed')]),
        (smoothing, visualize, [('outputnode.ts_smoothed',
                                 'inputnode.ts_transformed')]),

        ##all the output
        (denoise, sink,
         [('outputnode.wmcsf_mask', 'denoise.mask.@wmcsf_masks'),
          ('outputnode.combined_motion', 'denoise.artefact.@combined_motion'),
          ('outputnode.outlier_files', 'denoise.artefact.@outlier'),
          ('outputnode.intensity_files', 'denoise.artefact.@intensity'),
          ('outputnode.outlier_stats', 'denoise.artefact.@outlierstats'),
          ('outputnode.outlier_plots', 'denoise.artefact.@outlierplots'),
          ('outputnode.mc_regressor', 'denoise.regress.@mc_regressor'),
          ('outputnode.comp_regressor', 'denoise.regress.@comp_regressor'),
          ('outputnode.mc_F', 'denoise.regress.@mc_F'),
          ('outputnode.mc_pF', 'denoise.regress.@mc_pF'),
          ('outputnode.comp_F', 'denoise.regress.@comp_F'),
          ('outputnode.comp_pF', 'denoise.regress.@comp_pF'),
          ('outputnode.brain_mask_resamp', 'denoise.mask.@brain_resamp'),
          ('outputnode.brain_mask2epi', 'denoise.mask.@brain_mask2epi'),
          ('outputnode.normalized_file', 'denoise.@normalized')]),
        (ants_registration, sink, [('outputnode.ants_reg_ts',
                                    'ants.@antsnormalized')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    #func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run()
    # plugin='MultiProc'plugin='MultiProc'plugin='CondorDAGMan')
    #func_preproc.run()plugin='CondorDAGMan'plugin='CondorDAGMan'plugin='CondorDAGMan'plugin='CondorDAGMan'


#plugin='CondorDAGMan'
Beispiel #8
0
def create_resting(subject, working_dir, data_dir, freesurfer_dir, out_dir,
                         vol_to_remove, TR, epi_resolution, highpass, lowpass,
                         echo_space, pe_dir, standard_brain, standard_brain_resampled, standard_brain_mask,
                         standard_brain_mask_resampled, fwhm_smoothing):
    # set fsl output type to nii.gz
    fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
    # main workflow
    func_preproc = Workflow(name='lemon_resting')
    func_preproc.base_dir = working_dir
    func_preproc.config['execution']['crashdump_dir'] = func_preproc.base_dir + "/crash_files"
    # select files
    templates = {'func': 'raw/{subject}/func/EPI_t2.nii',
                 'ap': 'raw/{subject}/topup/se.nii',
                 'pa': 'raw/{subject}/topup/seinv_ph.nii',
                 'anat_head': 'preprocessing/preprocessed/{subject}/structural/T1.nii.gz',  
                 'anat_brain': 'preprocessing/preprocessed/{subject}/structural/brain.nii.gz',
                 'brain_mask': 'preprocessing/preprocessed/{subject}/structural/T1_brain_mask.nii.gz',  
                 'ants_affine': 'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform0GenericAffine.mat',
                 'ants_warp': 'preprocessing/preprocessed/{subject}/structural/transforms2mni/transform1Warp.nii.gz'
                 }

    selectfiles = Node(nio.SelectFiles(templates,
                                       base_directory=data_dir),
                       name="selectfiles")
    selectfiles.inputs.subject = subject


    # node to remove first volumes
    remove_vol = Node(util.Function(input_names=['in_file', 't_min'],
                                    output_names=["out_file"],
                                    function=strip_rois_func),
                      name='remove_vol')
    remove_vol.inputs.t_min = vol_to_remove
    # workflow for motion correction
    moco = create_moco_pipeline()

    # workflow for fieldmap correction and coregistration
    topup_coreg = create_topup_coreg_pipeline()
    topup_coreg.inputs.inputnode.fs_subjects_dir = freesurfer_dir
    topup_coreg.inputs.inputnode.fs_subject_id = subject
    topup_coreg.inputs.inputnode.echo_space = echo_space
    topup_coreg.inputs.inputnode.pe_dir = pe_dir

    # workflow for applying transformations to timeseries
    transform_ts = create_transform_pipeline()
    transform_ts.inputs.inputnode.resolution = epi_resolution


    # workflow to denoise timeseries
    denoise = create_denoise_pipeline()
    denoise.inputs.inputnode.highpass_sigma = 1. / (2 * TR * highpass)
    denoise.inputs.inputnode.lowpass_sigma = 1. / (2 * TR * lowpass)
    # https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=ind1205&L=FSL&P=R57592&1=FSL&9=A&I=-3&J=on&d=No+Match%3BMatch%3BMatches&z=4
    denoise.inputs.inputnode.tr = TR

    # workflow to transform timeseries to MNI
    ants_registration = create_ants_registration_pipeline()
    ants_registration.inputs.inputnode.ref = standard_brain
    ants_registration.inputs.inputnode.tr_sec = TR

    # FL added fullspectrum
    # workflow to transform fullspectrum timeseries to MNI
    ants_registration_full = create_ants_registration_pipeline('ants_registration_full')
    ants_registration_full.inputs.inputnode.ref = standard_brain
    ants_registration_full.inputs.inputnode.tr_sec = TR

    # workflow to smooth
    smoothing = create_smoothing_pipeline()
    smoothing.inputs.inputnode.fwhm = fwhm_smoothing

    # visualize registration results
    visualize = create_visualize_pipeline()
    visualize.inputs.inputnode.mni_template = standard_brain



    # sink to store files
    sink = Node(nio.DataSink(parameterization=False,
                             base_directory=out_dir,
                             substitutions=[('fmap_phase_fslprepared', 'fieldmap'),
                                            ('fieldmap_fslprepared_fieldmap_unmasked_vsm', 'shiftmap'),
                                            ('plot.rest_coregistered', 'outlier_plot'),
                                            ('filter_motion_comp_norm_compcor_art_dmotion', 'nuissance_matrix'),
                                            ('rest_realigned.nii.gz_abs.rms', 'rest_realigned_abs.rms'),
                                            ('rest_realigned.nii.gz.par', 'rest_realigned.par'),
                                            ('rest_realigned.nii.gz_rel.rms', 'rest_realigned_rel.rms'),
                                            ('rest_realigned.nii.gz_abs_disp', 'abs_displacement_plot'),
                                            ('rest_realigned.nii.gz_rel_disp', 'rel_displacment_plot'),
                                            ('art.rest_coregistered_outliers', 'outliers'),
                                            ('global_intensity.rest_coregistered', 'global_intensity'),
                                            ('norm.rest_coregistered', 'composite_norm'),
                                            ('stats.rest_coregistered', 'stats'),
                                            ('rest_denoised_bandpassed_norm.nii.gz',
                                             'rest_preprocessed_nativespace.nii.gz'),
                                            ('rest_denoised_bandpassed_norm_trans.nii.gz',
                                             'rest_mni_unsmoothed.nii.gz'),
                                            ('rest_denoised_bandpassed_norm_trans_smooth.nii',
                                             'rest_mni_smoothed.nii'),
                                            # FL added
                                            ('rest2anat_masked.nii.gz', 'rest_coregistered_nativespace.nii.gz'),
                                            ('rest2anat_denoised.nii.gz',
                                             'rest_preprocessed_nativespace_fullspectrum.nii.gz'),
                                            ('rest2anat_denoised_trans.nii.gz',
                                             'rest_mni_unsmoothed_fullspectrum.nii.gz')
                                            ]),
                name='sink')


    # connections
    func_preproc.connect([
        # remove the first volumes
        (selectfiles, remove_vol, [('func', 'in_file')]),

        # align volumes and motion correction
        (remove_vol, moco, [('out_file', 'inputnode.epi')]),

        # prepare field map
        (selectfiles, topup_coreg, [('ap', 'inputnode.ap'),
                                   ('pa', 'inputnode.pa'),
                                   ('anat_head', 'inputnode.anat_head'),
                                   ('anat_brain', 'inputnode.anat_brain')
                                   ]),
        (moco, topup_coreg, [('outputnode.epi_mean', 'inputnode.epi_mean')]),

        # transform timeseries
        (remove_vol, transform_ts, [('out_file', 'inputnode.orig_ts')]),
        (selectfiles, transform_ts, [('anat_head', 'inputnode.anat_head')]),
        (selectfiles, transform_ts, [('brain_mask', 'inputnode.brain_mask')]),
        (moco, transform_ts, [('outputnode.mat_moco', 'inputnode.mat_moco')]),
        (topup_coreg, transform_ts, [('outputnode.fmap_fullwarp', 'inputnode.fullwarp')]),

        # correct slicetiming
        # FIXME slice timing?
        # (transform_ts, slicetiming, [('outputnode.trans_ts_masked', 'inputnode.ts')]),
        # (slicetiming, denoise, [('outputnode.ts_slicetcorrected', 'inputnode.epi_coreg')]),
        (transform_ts, denoise, [('outputnode.trans_ts_masked', 'inputnode.epi_coreg')]),

        # denoise data
        (selectfiles, denoise, [('brain_mask', 'inputnode.brain_mask'),
                                ('anat_brain', 'inputnode.anat_brain')]),
        (moco, denoise, [('outputnode.par_moco', 'inputnode.moco_par')]),
        (topup_coreg, denoise, [('outputnode.epi2anat_dat', 'inputnode.epi2anat_dat'),
                               ('outputnode.unwarped_mean_epi2fmap', 'inputnode.unwarped_mean')]),
        (denoise, ants_registration, [('outputnode.normalized_file', 'inputnode.denoised_ts')]),

        # registration to MNI space
        (selectfiles, ants_registration, [('ants_affine', 'inputnode.ants_affine')]),
        (selectfiles, ants_registration, [('ants_warp', 'inputnode.ants_warp')]),

        # FL added fullspectrum
        (denoise, ants_registration_full, [('outputnode.ts_fullspectrum', 'inputnode.denoised_ts')]),
        (selectfiles, ants_registration_full, [('ants_affine', 'inputnode.ants_affine')]),
        (selectfiles, ants_registration_full, [('ants_warp', 'inputnode.ants_warp')]),

        (ants_registration, smoothing, [('outputnode.ants_reg_ts', 'inputnode.ts_transformed')]),

        (smoothing, visualize, [('outputnode.ts_smoothed', 'inputnode.ts_transformed')]),

        ##all the output
        (moco, sink, [  # ('outputnode.epi_moco', 'realign.@realigned_ts'),
                        ('outputnode.par_moco', 'realign.@par'),
                        ('outputnode.rms_moco', 'realign.@rms'),
                        ('outputnode.mat_moco', 'realign.MAT.@mat'),
                        ('outputnode.epi_mean', 'realign.@mean'),
                        ('outputnode.rotplot', 'realign.plots.@rotplot'),
                        ('outputnode.transplot', 'realign.plots.@transplot'),
                        ('outputnode.dispplots', 'realign.plots.@dispplots'),
                        ('outputnode.tsnr_file', 'realign.@tsnr')]),
        (topup_coreg, sink, [('outputnode.fmap', 'coregister.transforms2anat.@fmap'),
                            # ('outputnode.unwarpfield_epi2fmap', 'coregister.@unwarpfield_epi2fmap'),
                            ('outputnode.unwarped_mean_epi2fmap', 'coregister.@unwarped_mean_epi2fmap'),
                            ('outputnode.epi2fmap', 'coregister.@epi2fmap'),
                            # ('outputnode.shiftmap', 'coregister.@shiftmap'),
                            ('outputnode.fmap_fullwarp', 'coregister.transforms2anat.@fmap_fullwarp'),
                            ('outputnode.epi2anat', 'coregister.@epi2anat'),
                            ('outputnode.epi2anat_mat', 'coregister.transforms2anat.@epi2anat_mat'),
                            ('outputnode.epi2anat_dat', 'coregister.transforms2anat.@epi2anat_dat'),
                            ('outputnode.epi2anat_mincost', 'coregister.@epi2anat_mincost')
                            ]),

        (transform_ts, sink, [('outputnode.trans_ts_masked', 'coregister.@full_transform_ts'),
                              ('outputnode.trans_ts_mean', 'coregister.@full_transform_mean'),
                              ('outputnode.resamp_brain', 'coregister.@resamp_brain')]),

        (denoise, sink, [
            ('outputnode.wmcsf_mask', 'denoise.mask.@wmcsf_masks'),
            ('outputnode.combined_motion', 'denoise.artefact.@combined_motion'),
            ('outputnode.outlier_files', 'denoise.artefact.@outlier'),
            ('outputnode.intensity_files', 'denoise.artefact.@intensity'),
            ('outputnode.outlier_stats', 'denoise.artefact.@outlierstats'),
            ('outputnode.outlier_plots', 'denoise.artefact.@outlierplots'),
            ('outputnode.mc_regressor', 'denoise.regress.@mc_regressor'),
            ('outputnode.comp_regressor', 'denoise.regress.@comp_regressor'),
            ('outputnode.mc_F', 'denoise.regress.@mc_F'),
            ('outputnode.mc_pF', 'denoise.regress.@mc_pF'),
            ('outputnode.comp_F', 'denoise.regress.@comp_F'),
            ('outputnode.comp_pF', 'denoise.regress.@comp_pF'),
            ('outputnode.brain_mask_resamp', 'denoise.mask.@brain_resamp'),
            ('outputnode.brain_mask2epi', 'denoise.mask.@brain_mask2epi'),
            ('outputnode.normalized_file', 'denoise.@normalized'),
            # FL added fullspectrum
            ('outputnode.ts_fullspectrum', 'denoise.@ts_fullspectrum')
        ]),
        (ants_registration, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized')]),
        (ants_registration_full, sink, [('outputnode.ants_reg_ts', 'ants.@antsnormalized_fullspectrum')]),
        (smoothing, sink, [('outputnode.ts_smoothed', '@smoothed.FWHM6')]),
    ])

    func_preproc.write_graph(dotfilename='func_preproc.dot', graph2use='colored', format='pdf', simple_form=True)
    func_preproc.run(plugin='MultiProc')