示例#1
0
def baw_FixBrainMask(brainmask, subjects_dir, FREESURFER_HOME, FS_SCRIPT, subj_session_id):
    base_subj_dir = os.path.join(subjects_dir, subj_session_id, 'mri')
    mkdir_p(base_subj_dir)
    output_brainmask_fn_mgz = os.path.join(base_subj_dir, 'brainmask.mgz')
    output_custom_brainmask_fn = os.path.join(base_subj_dir, 'custom_brain_mask.nii.gz')
    output_custom_brainmask_fn_mgz = os.path.join(base_subj_dir, 'custom_brain_mask.mgz')
    output_nu_fn_mgz = os.path.join(base_subj_dir, 'nu.mgz')
    if IsFirstNewerThanSecond(brainmask, output_brainmask_fn_mgz) \
        or IsFirstNewerThanSecond(brainmask, output_nu_fn_mgz) \
            or IsFirstNewerThanSecond(output_nu_fn_mgz, output_custom_brainmask_fn_mgz):
        print "Fixing BrainMask recon-auto1 stage"
        brain = sitk.ReadImage(brainmask)
        blood = sitk.BinaryThreshold(brain, 5, 5)
        not_blood = 1 - blood
        clipping = sitk.BinaryThreshold(brain, 1, 1000000) - blood
        fill_size = 2
        ## HACK: Unfortunately we need to hole fill because of a WM bug in BABC where
        ## some white matter is being classified as background when it it being avoid due
        ## to too strict of multi-modal thresholding.
        hole_filled = sitk.ErodeObjectMorphology(sitk.DilateObjectMorphology(clipping, fill_size), fill_size)
        final_mask = sitk.Cast(hole_filled * not_blood, sitk.sitkUInt8)
        ## Now make an mgz version of the binary custom brain mask
        sitk.WriteImage(final_mask, output_custom_brainmask_fn)  # brain_matter mask with blood zero'ed out, and no "NOT" regions.
        run_mri_convert_script(output_custom_brainmask_fn, output_custom_brainmask_fn_mgz, subjects_dir, subj_session_id, FREESURFER_HOME, FS_SCRIPT)
        os.rename(output_brainmask_fn_mgz, output_brainmask_fn_mgz.replace('.mgz', '_orig_backup.mgz'))
        ## Multipy output_brainmask_fn_mgz =  output_custom_brainmask_fn_mgz * output_nu_fn_mgz
        run_mri_mask_script(output_brainmask_fn_mgz, output_custom_brainmask_fn_mgz, output_nu_fn_mgz, subjects_dir, subj_session_id, FREESURFER_HOME, FS_SCRIPT)
    else:
        print "NOTHING TO BE DONE, SO SKIPPING."
        return  # Nothing to be done, files are already up-to-date.
示例#2
0
def runLongitudinal(args, FREESURFER_HOME, FS_SCRIPT):
    """ Create the longitudinal analysis """
    subj_session_id = args.subj_session_id
    subjects_dir = args.subjects_dir
    base_template_id = args.base_template_id
    assert isinstance(
        subj_session_id,
        str), "Must input a singel subj_session_id as string :{0}:".format(
            subj_session_id)
    StageToRun = "Longitudinal"
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    auto_recon_script = """#!/bin/bash
#$ -o {FSSUBJDIR}/{LONGSESSIONID}/scripts/long_{TEMPLATEID}_{LONGSESSIONID}_qsub.out
#$ -e {FSSUBJDIR}/{LONGSESSIONID}/scripts/long_{TEMPLATEID}_{LONGSESSIONID}_qsub.err
#$ -cwd
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
## Need to delete the "IsRunning" flags, if nipype pipeline is running this, then nipype prevents duplicates
rm -f {FSSUBJDIR}/{LONGSESSIONID}/scripts/Is*

if [ -f {FSSUBJDIR}/{LONGSESSIONID}.long.{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats ]; then
   echo "--- SKIPPING: {LONGSESSIONID}.long.{TEMPLATEID} file already exits: {FSSUBJDIR}/{LONGSESSIONID}.long.{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats"
   status=$?
else
   echo "--- RUNNING: {LONGSESSIONID}.long.{TEMPLATEID} file already exits: {FSSUBJDIR}/{LONGSESSIONID}.long.{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats"
   {FSHOME}/bin/recon-all -debug -long {LONGSESSIONID} {TEMPLATEID} -all
   status=$?
fi
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
           FSHOME=FREESURFER_HOME,
           FSSUBJDIR=subjects_dir,
           TEMPLATEID=base_template_id,
           LONGSESSIONID=subj_session_id)
    base_run_dir = os.path.join(subjects_dir, subj_session_id, 'scripts')
    mkdir_p(base_run_dir)
    script_name = os.path.join(
        base_run_dir, 'run_autorecon_stage_' + str(StageToRun) + '.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(
        StageToRun, base_template_id)
    scriptStatus = subprocess.check_call([script_name],
                                         stdout=script_name_stdout_fid,
                                         stderr=subprocess.STDOUT,
                                         shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(
        StageToRun, base_template_id)
    script_name_stdout_fid.close()
    return
示例#3
0
def runSubjectTemplate(args, FREESURFER_HOME, FS_SCRIPT):
    """ Create the within-subject template """
    base_template_id = args.base_template_id
    list_all_subj_session_ids = args.list_all_subj_session_ids
    subjects_dir = args.subjects_dir
    print "X" * 80
    print "base_template_id :{0}:".format(base_template_id)
    print "Input a list of list_all_subj_session_ids :{0}:".format(list_all_subj_session_ids)
    print "subjects_dir :{0}:".format(subjects_dir)
    print "X" * 80
    assert isinstance(list_all_subj_session_ids, list), "Must input a list of list_all_subj_session_ids :{0}:".format(list_all_subj_session_ids)
    StageToRun = "Within-SubjectTemplate"
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    allTimePointFlags = ""
    for subj_session_id in list_all_subj_session_ids:
        allTimePointFlags += " -tp {timepoint}".format(timepoint=subj_session_id)
    allTimePointFlags += " -all"
    auto_recon_script = """#!/bin/bash
#$ -o {FSSUBJDIR}/{TEMPLATEID}/scripts/base_{TEMPLATEID}_qsub.out
#$ -e {FSSUBJDIR}/{TEMPLATEID}/scripts/base_{TEMPLATEID}_qsub.err
#$ -cwd
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
## Need to delete the "IsRunning" flags, if nipype pipeline is running this, then nipype prevents duplicates
rm -f {FSSUBJDIR}/{TEMPLATEID}/scripts/Is*

if [ -f {FSSUBJDIR}/{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats ]; then
   echo "--- SKIPPING: {TEMPLATEID} sentinal file already exits: {FSSUBJDIR}/{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats"
   status=$?
else
   {FSHOME}/bin/recon-all -debug -base {TEMPLATEID} {ALL_TIME_POINTS}
   status=$?
fi
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
               FSHOME=FREESURFER_HOME,
               FSSUBJDIR=subjects_dir,
               TEMPLATEID=base_template_id,
               ALL_TIME_POINTS=allTimePointFlags)
    base_run_dir = os.path.join(subjects_dir, base_template_id, 'scripts')
    mkdir_p(base_run_dir)
    script_name = os.path.join(base_run_dir, 'run_autorecon_stage_' + str(StageToRun) + '.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, base_template_id)
    scriptStatus = subprocess.check_call([script_name], stdout=script_name_stdout_fid, stderr=subprocess.STDOUT, shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, base_template_id)
    script_name_stdout_fid.close()
    return
示例#4
0
def runAutoReconStage(subj_session_id, StageToRun, t1_fn, subjects_dir,
                      FREESURFER_HOME, FS_SCRIPT):
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    base_subj_dir = os.path.join(subjects_dir, subj_session_id)
    orig_001_mgz_fn = os.path.join(base_subj_dir, 'mri', 'orig', '001.mgz')
    if IsFirstNewerThanSecond(t1_fn, orig_001_mgz_fn):
        if os.path.exists(base_subj_dir):
            removeDir(base_subj_dir)
        mkdir_p(os.path.dirname(orig_001_mgz_fn))
        run_mri_convert_script(t1_fn, orig_001_mgz_fn, subjects_dir,
                               subj_session_id, FREESURFER_HOME, FS_SCRIPT)
    auto_recon_script = """#!/bin/bash
#$ -o {FSSUBJDIR}/{SUBJ_SESSION_ID}/scripts/autorecon{AUTORECONSTAGE}_qsub.out
#$ -e {FSSUBJDIR}/{SUBJ_SESSION_ID}/scripts/autorecon{AUTORECONSTAGE}_qsub.err
#$ -cwd
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
## Need to delete the "IsRunning" flags, if nipype pipeline is running this, then nipype prevents duplicates
rm -f {FSSUBJDIR}/{SUBJ_SESSION_ID}/scripts/Is*

{FSHOME}/bin/recon-all -debug -subjid {SUBJ_SESSION_ID} -make autorecon{AUTORECONSTAGE}
status=$?
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
           FSHOME=FREESURFER_HOME,
           FSSUBJDIR=subjects_dir,
           AUTORECONSTAGE=StageToRun,
           SUBJ_SESSION_ID=subj_session_id)
    base_run_dir = os.path.join(subjects_dir, subj_session_id, 'scripts')
    mkdir_p(base_run_dir)
    script_name = os.path.join(base_run_dir,
                               'run_autorecon_stage' + str(StageToRun) + '.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(
        StageToRun, subj_session_id)
    scriptStatus = subprocess.check_call([script_name],
                                         stdout=script_name_stdout_fid,
                                         stderr=subprocess.STDOUT,
                                         shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(
        StageToRun, subj_session_id)
    script_name_stdout_fid.close()
    return
示例#5
0
def runLongitudinal(args, FREESURFER_HOME, FS_SCRIPT):
    """ Create the longitudinal analysis """
    subj_session_id = args.subj_session_id
    subjects_dir = args.subjects_dir
    base_template_id = args.base_template_id
    assert isinstance(subj_session_id, str), "Must input a singel subj_session_id as string :{0}:".format(subj_session_id)
    StageToRun = "Longitudinal"
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    auto_recon_script = """#!/bin/bash
#$ -o {FSSUBJDIR}/{LONGSESSIONID}/scripts/long_{TEMPLATEID}_{LONGSESSIONID}_qsub.out
#$ -e {FSSUBJDIR}/{LONGSESSIONID}/scripts/long_{TEMPLATEID}_{LONGSESSIONID}_qsub.err
#$ -cwd
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
## Need to delete the "IsRunning" flags, if nipype pipeline is running this, then nipype prevents duplicates
rm -f {FSSUBJDIR}/{LONGSESSIONID}/scripts/Is*

if [ -f {FSSUBJDIR}/{LONGSESSIONID}.long.{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats ]; then
   echo "--- SKIPPING: {LONGSESSIONID}.long.{TEMPLATEID} file already exits: {FSSUBJDIR}/{LONGSESSIONID}.long.{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats"
   status=$?
else
   echo "--- RUNNING: {LONGSESSIONID}.long.{TEMPLATEID} file already exits: {FSSUBJDIR}/{LONGSESSIONID}.long.{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats"
   {FSHOME}/bin/recon-all -debug -long {LONGSESSIONID} {TEMPLATEID} -all
   status=$?
fi
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
               FSHOME=FREESURFER_HOME,
               FSSUBJDIR=subjects_dir,
               TEMPLATEID=base_template_id,
               LONGSESSIONID=subj_session_id)
    base_run_dir = os.path.join(subjects_dir, subj_session_id, 'scripts')
    mkdir_p(base_run_dir)
    script_name = os.path.join(base_run_dir, 'run_autorecon_stage_' + str(StageToRun) + '.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, base_template_id)
    scriptStatus = subprocess.check_call([script_name], stdout=script_name_stdout_fid, stderr=subprocess.STDOUT, shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, base_template_id)
    script_name_stdout_fid.close()
    return
示例#6
0
def runSubjectTemplate(args, FREESURFER_HOME, FS_SCRIPT):
    """ Create the within-subject template """
    subjectTemplate_id = args.subjectTemplate_id
    session_ids = args.session_ids
    subjects_dir = args.subjects_dir
    print "X"*80
    print "subjectTemplate_id :{0}:".format(subjectTemplate_id)
    print "Input a list of session_ids :{0}:".format(session_ids)
    print "subjects_dir :{0}:".format(subjects_dir)
    print "X"*80
    assert isinstance(session_ids, list), "Must input a list of session_ids :{0}:".format(session_ids)
    StageToRun = "Within-SubjectTemplate"
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    allTimePointFlags = ""
    for session_id in session_ids:
        allTimePointFlags += " -tp {timepoint}".format(timepoint=session_id)
    allTimePointFlags += " -all"
    auto_recon_script="""#!/bin/bash
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
{FSHOME}/bin/recon-all -debug -base {TEMPLATEID} {ALL_TIME_POINTS}
status=$?
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
               FSHOME=FREESURFER_HOME,
               FSSUBJDIR=subjects_dir,
               TEMPLATEID=subjectTemplate_id,
               ALL_TIME_POINTS=allTimePointFlags)
    base_run_dir = os.path.join(subjects_dir,'run_scripts', subjectTemplate_id)
    mkdir_p(base_run_dir)
    script_name = os.path.join(base_run_dir,'run_autorecon_stage_'+str(StageToRun)+'.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, subjectTemplate_id)
    scriptStatus = subprocess.check_call([script_name], stdout=script_name_stdout_fid, stderr=subprocess.STDOUT, shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, subjectTemplate_id)
    script_name_stdout_fid.close()
    return
示例#7
0
def runAutoReconStage(subj_session_id, StageToRun, t1_fn, subjects_dir, FREESURFER_HOME, FS_SCRIPT):
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    base_subj_dir = os.path.join(subjects_dir, subj_session_id)
    orig_001_mgz_fn = os.path.join(base_subj_dir, 'mri', 'orig', '001.mgz')
    if IsFirstNewerThanSecond(t1_fn, orig_001_mgz_fn):
        if os.path.exists(base_subj_dir):
            removeDir(base_subj_dir)
        mkdir_p(os.path.dirname(orig_001_mgz_fn))
        run_mri_convert_script(t1_fn, orig_001_mgz_fn, subjects_dir, subj_session_id, FREESURFER_HOME, FS_SCRIPT)
    auto_recon_script = """#!/bin/bash
#$ -o {FSSUBJDIR}/{SUBJ_SESSION_ID}/scripts/autorecon{AUTORECONSTAGE}_qsub.out
#$ -e {FSSUBJDIR}/{SUBJ_SESSION_ID}/scripts/autorecon{AUTORECONSTAGE}_qsub.err
#$ -cwd
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
## Need to delete the "IsRunning" flags, if nipype pipeline is running this, then nipype prevents duplicates
rm -f {FSSUBJDIR}/{SUBJ_SESSION_ID}/scripts/Is*

{FSHOME}/bin/recon-all -debug -subjid {SUBJ_SESSION_ID} -make autorecon{AUTORECONSTAGE}
status=$?
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
               FSHOME=FREESURFER_HOME,
               FSSUBJDIR=subjects_dir,
               AUTORECONSTAGE=StageToRun,
               SUBJ_SESSION_ID=subj_session_id)
    base_run_dir = os.path.join(subjects_dir, subj_session_id, 'scripts')
    mkdir_p(base_run_dir)
    script_name = os.path.join(base_run_dir, 'run_autorecon_stage' + str(StageToRun) + '.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, subj_session_id)
    scriptStatus = subprocess.check_call([script_name], stdout=script_name_stdout_fid, stderr=subprocess.STDOUT, shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, subj_session_id)
    script_name_stdout_fid.close()
    return
示例#8
0
def baw_FixBrainMask(brainmask, subjects_dir, FREESURFER_HOME, FS_SCRIPT,
                     subj_session_id):
    base_subj_dir = os.path.join(subjects_dir, subj_session_id, 'mri')
    mkdir_p(base_subj_dir)
    output_brainmask_fn_mgz = os.path.join(base_subj_dir, 'brainmask.mgz')
    output_custom_brainmask_fn = os.path.join(base_subj_dir,
                                              'custom_brain_mask.nii.gz')
    output_custom_brainmask_fn_mgz = os.path.join(base_subj_dir,
                                                  'custom_brain_mask.mgz')
    output_nu_fn_mgz = os.path.join(base_subj_dir, 'nu.mgz')
    if IsFirstNewerThanSecond(brainmask, output_brainmask_fn_mgz) \
        or IsFirstNewerThanSecond(brainmask, output_nu_fn_mgz) \
            or IsFirstNewerThanSecond(output_nu_fn_mgz, output_custom_brainmask_fn_mgz):
        print "Fixing BrainMask recon-auto1 stage"
        brain = sitk.ReadImage(brainmask)
        blood = sitk.BinaryThreshold(brain, 5, 5)
        not_blood = 1 - blood
        clipping = sitk.BinaryThreshold(brain, 1, 1000000) - blood
        fill_size = 2
        ## HACK: Unfortunately we need to hole fill because of a WM bug in BABC where
        ## some white matter is being classified as background when it it being avoid due
        ## to too strict of multi-modal thresholding.
        hole_filled = sitk.ErodeObjectMorphology(
            sitk.DilateObjectMorphology(clipping, fill_size), fill_size)
        final_mask = sitk.Cast(hole_filled * not_blood, sitk.sitkUInt8)
        ## Now make an mgz version of the binary custom brain mask
        sitk.WriteImage(
            final_mask, output_custom_brainmask_fn
        )  # brain_matter mask with blood zero'ed out, and no "NOT" regions.
        run_mri_convert_script(output_custom_brainmask_fn,
                               output_custom_brainmask_fn_mgz, subjects_dir,
                               subj_session_id, FREESURFER_HOME, FS_SCRIPT)
        os.rename(output_brainmask_fn_mgz,
                  output_brainmask_fn_mgz.replace('.mgz', '_orig_backup.mgz'))
        ## Multipy output_brainmask_fn_mgz =  output_custom_brainmask_fn_mgz * output_nu_fn_mgz
        run_mri_mask_script(output_brainmask_fn_mgz,
                            output_custom_brainmask_fn_mgz, output_nu_fn_mgz,
                            subjects_dir, subj_session_id, FREESURFER_HOME,
                            FS_SCRIPT)
    else:
        print "NOTHING TO BE DONE, SO SKIPPING."
        return  # Nothing to be done, files are already up-to-date.
示例#9
0
def runLongitudinal(args, FREESURFER_HOME, FS_SCRIPT):
    """ Create the longitudinal analysis """
    session_id = args.session_id
    subjects_dir = args.subjects_dir
    template_id = args.template_id
    assert type(session_id, str), "Must input a list of session_ids"
    StageToRun = "Longitudinal"
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    auto_recon_script = """#!/bin/bash
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
{FSHOME}/bin/recon-all -debug -long {TIMEPOINT} {TEMPLATEID} -all
status=$?
mv -n {FSSUBJDIR}/{TIMEPOINT}.long.{TEMPLATEID} {FSSUBJDIR}/{TEMPLATEID}_{TIMEPOINT}.long
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
               FSHOME=FREESURFER_HOME,
               FSSUBJDIR=subjects_dir,
               TEMPLATEID=template_id,
               TIMEPOINT=session_id)
    base_run_dir = os.path.join(subjects_dir,'run_scripts', template_id)
    mkdir_p(base_run_dir)
    script_name = os.path.join(base_run_dir,'run_autorecon_stage_'+str(StageToRun)+'.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, template_id)
    scriptStatus = subprocess.check_call([script_name], stdout=script_name_stdout_fid, stderr=subprocess.STDOUT, shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, template_id)
    script_name_stdout_fid.close()
    return
示例#10
0
def runSubjectTemplate(args, FREESURFER_HOME, FS_SCRIPT):
    import sys
    """ Create the within-subject template """
    base_template_id = args.base_template_id
    list_all_subj_session_ids = args.list_all_subj_session_ids
    subjects_dir = args.subjects_dir
    print "X" * 80
    print "base_template_id :{0}:".format(base_template_id)
    print "Input a list of list_all_subj_session_ids :{0}:".format(
        list_all_subj_session_ids)
    print "subjects_dir :{0}:".format(subjects_dir)
    print "X" * 80
    assert isinstance(
        list_all_subj_session_ids,
        list), "Must input a list of list_all_subj_session_ids :{0}:".format(
            list_all_subj_session_ids)
    StageToRun = "Within-SubjectTemplate"
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    allTimePointFlags = ""

    ## Determine if previous run had same sessions as current run
    base_tempate_list = os.path.join(subjects_dir, base_template_id,
                                     'base-tps')
    #print "XXX Setting file to look for ", base_tempate_list
    prev_sessions = list()
    if os.path.exists(base_tempate_list):
        #print "XXX Opening ", base_tempate_list
        ff = open(base_tempate_list, 'r')
        #print "XXX Reading lines"
        prev_sessions = ff.readlines()
        #print prev_sessions
        prev_sessions = [x.rstrip() for x in prev_sessions]
        #print prev_sessions

    reset_template_directory = False

    for subj_session_id in list_all_subj_session_ids:
        allTimePointFlags += " -tp {timepoint}".format(
            timepoint=subj_session_id)
        if subj_session_id not in prev_sessions:
            reset_template_directory = True

    #print("XXXXXXXXXXX {0}  =? {1}".format(prev_sessions,list_all_subj_session_ids))
    #print("XXXX {0}".format(reset_template_directory))

    if reset_template_directory:
        recursive_dir_rm(os.path.join(subjects_dir, base_template_id))
        ## Now remove subsequent directories
        for subj_session_id in list_all_subj_session_ids:
            recursive_dir_rm(
                os.path.join(subjects_dir,
                             subj_session_id + '.long.' + base_template_id))
    allTimePointFlags += " -all"
    auto_recon_script = """#!/bin/bash
#$ -o {FSSUBJDIR}/{TEMPLATEID}/scripts/base_{TEMPLATEID}_qsub.out
#$ -e {FSSUBJDIR}/{TEMPLATEID}/scripts/base_{TEMPLATEID}_qsub.err
#$ -cwd
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
## Need to delete the "IsRunning" flags, if nipype pipeline is running this, then nipype prevents duplicates
rm -f {FSSUBJDIR}/{TEMPLATEID}/scripts/Is*

if [ -f {FSSUBJDIR}/{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats ]; then
   echo "--- SKIPPING: {TEMPLATEID} sentinal file already exits: {FSSUBJDIR}/{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats"
   status=$?
else
   {FSHOME}/bin/recon-all -debug -base {TEMPLATEID} {ALL_TIME_POINTS}
   status=$?
fi
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
           FSHOME=FREESURFER_HOME,
           FSSUBJDIR=subjects_dir,
           TEMPLATEID=base_template_id,
           ALL_TIME_POINTS=allTimePointFlags)
    base_run_dir = os.path.join(subjects_dir, base_template_id, 'scripts')
    mkdir_p(base_run_dir)
    script_name = os.path.join(
        base_run_dir, 'run_autorecon_stage_' + str(StageToRun) + '.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(
        StageToRun, base_template_id)
    scriptStatus = subprocess.check_call([script_name],
                                         stdout=script_name_stdout_fid,
                                         stderr=subprocess.STDOUT,
                                         shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(
        StageToRun, base_template_id)
    script_name_stdout_fid.close()
    return
示例#11
0
def runSubjectTemplate(args, FREESURFER_HOME, FS_SCRIPT):
    import sys
    """ Create the within-subject template """
    base_template_id = args.base_template_id
    list_all_subj_session_ids = args.list_all_subj_session_ids
    subjects_dir = args.subjects_dir
    print "X" * 80
    print "base_template_id :{0}:".format(base_template_id)
    print "Input a list of list_all_subj_session_ids :{0}:".format(list_all_subj_session_ids)
    print "subjects_dir :{0}:".format(subjects_dir)
    print "X" * 80
    assert isinstance(list_all_subj_session_ids, list), "Must input a list of list_all_subj_session_ids :{0}:".format(list_all_subj_session_ids)
    StageToRun = "Within-SubjectTemplate"
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    allTimePointFlags = ""

    ## Determine if previous run had same sessions as current run
    base_tempate_list = os.path.join(subjects_dir,base_template_id,'base-tps')
    #print "XXX Setting file to look for ", base_tempate_list
    prev_sessions = list()
    if os.path.exists(base_tempate_list):
        #print "XXX Opening ", base_tempate_list
        ff = open ( base_tempate_list, 'r' )
        #print "XXX Reading lines"
        prev_sessions = ff.readlines()
        #print prev_sessions
        prev_sessions = [ x.rstrip() for x in prev_sessions ]
        #print prev_sessions


    reset_template_directory = False


    for subj_session_id in list_all_subj_session_ids:
        allTimePointFlags += " -tp {timepoint}".format(timepoint=subj_session_id)
        if subj_session_id not in prev_sessions:
                reset_template_directory = True

    #print("XXXXXXXXXXX {0}  =? {1}".format(prev_sessions,list_all_subj_session_ids))
    #print("XXXX {0}".format(reset_template_directory))

    if reset_template_directory:
        recursive_dir_rm(os.path.join(subjects_dir,base_template_id))
        ## Now remove subsequent directories
        for subj_session_id in list_all_subj_session_ids:
           recursive_dir_rm(os.path.join(subjects_dir,subj_session_id+'.long.'+base_template_id))
    allTimePointFlags += " -all"
    auto_recon_script = """#!/bin/bash
#$ -o {FSSUBJDIR}/{TEMPLATEID}/scripts/base_{TEMPLATEID}_qsub.out
#$ -e {FSSUBJDIR}/{TEMPLATEID}/scripts/base_{TEMPLATEID}_qsub.err
#$ -cwd
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
## Need to delete the "IsRunning" flags, if nipype pipeline is running this, then nipype prevents duplicates
rm -f {FSSUBJDIR}/{TEMPLATEID}/scripts/Is*

if [ -f {FSSUBJDIR}/{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats ]; then
   echo "--- SKIPPING: {TEMPLATEID} sentinal file already exits: {FSSUBJDIR}/{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats"
   status=$?
else
   {FSHOME}/bin/recon-all -debug -base {TEMPLATEID} {ALL_TIME_POINTS}
   status=$?
fi
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
               FSHOME=FREESURFER_HOME,
               FSSUBJDIR=subjects_dir,
               TEMPLATEID=base_template_id,
               ALL_TIME_POINTS=allTimePointFlags)
    base_run_dir = os.path.join(subjects_dir, base_template_id, 'scripts')
    mkdir_p(base_run_dir)
    script_name = os.path.join(base_run_dir, 'run_autorecon_stage_' + str(StageToRun) + '.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, base_template_id)
    scriptStatus = subprocess.check_call([script_name], stdout=script_name_stdout_fid, stderr=subprocess.STDOUT, shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(StageToRun, base_template_id)
    script_name_stdout_fid.close()
    return
示例#12
0
def runSubjectTemplate(args, FREESURFER_HOME, FS_SCRIPT):
    """ Create the within-subject template """
    base_template_id = args.base_template_id
    list_all_subj_session_ids = args.list_all_subj_session_ids
    subjects_dir = args.subjects_dir
    print "X" * 80
    print "base_template_id :{0}:".format(base_template_id)
    print "Input a list of list_all_subj_session_ids :{0}:".format(
        list_all_subj_session_ids)
    print "subjects_dir :{0}:".format(subjects_dir)
    print "X" * 80
    assert isinstance(
        list_all_subj_session_ids,
        list), "Must input a list of list_all_subj_session_ids :{0}:".format(
            list_all_subj_session_ids)
    StageToRun = "Within-SubjectTemplate"
    FS_SCRIPT_FN = os.path.join(FREESURFER_HOME, FS_SCRIPT)
    allTimePointFlags = ""
    for subj_session_id in list_all_subj_session_ids:
        allTimePointFlags += " -tp {timepoint}".format(
            timepoint=subj_session_id)
    allTimePointFlags += " -all"
    auto_recon_script = """#!/bin/bash
#$ -o {FSSUBJDIR}/{TEMPLATEID}/scripts/base_{TEMPLATEID}_qsub.out
#$ -e {FSSUBJDIR}/{TEMPLATEID}/scripts/base_{TEMPLATEID}_qsub.err
#$ -cwd
export FREESURFER_HOME={FSHOME}
export SUBJECTS_DIR={FSSUBJDIR}
source {SOURCE_SCRIPT}
## Need to delete the "IsRunning" flags, if nipype pipeline is running this, then nipype prevents duplicates
rm -f {FSSUBJDIR}/{TEMPLATEID}/scripts/Is*

if [ -f {FSSUBJDIR}/{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats ]; then
   echo "--- SKIPPING: {TEMPLATEID} sentinal file already exits: {FSSUBJDIR}/{TEMPLATEID}/stats/rh.entorhinal_exvivo.stats"
   status=$?
else
   {FSHOME}/bin/recon-all -debug -base {TEMPLATEID} {ALL_TIME_POINTS}
   status=$?
fi
exit $status
""".format(SOURCE_SCRIPT=FS_SCRIPT_FN,
           FSHOME=FREESURFER_HOME,
           FSSUBJDIR=subjects_dir,
           TEMPLATEID=base_template_id,
           ALL_TIME_POINTS=allTimePointFlags)
    base_run_dir = os.path.join(subjects_dir, base_template_id, 'scripts')
    mkdir_p(base_run_dir)
    script_name = os.path.join(
        base_run_dir, 'run_autorecon_stage_' + str(StageToRun) + '.sh')
    script = open(script_name, 'w')
    script.write(auto_recon_script)
    script.close()
    os.chmod(script_name, 0777)
    script_name_stdout = script_name + '_out'
    script_name_stdout_fid = open(script_name_stdout, 'w')
    print "Starting auto_recon Stage: {0} for SubjectSession {1}".format(
        StageToRun, base_template_id)
    scriptStatus = subprocess.check_call([script_name],
                                         stdout=script_name_stdout_fid,
                                         stderr=subprocess.STDOUT,
                                         shell='/bin/bash')
    if scriptStatus != 0:
        sys.exit(scriptStatus)
    print "Ending auto_recon Stage: {0} for SubjectSession {1}".format(
        StageToRun, base_template_id)
    script_name_stdout_fid.close()
    return