Skip to content

Scripts

Anthony Dick edited this page Jun 18, 2019 · 4 revisions

Python script to submit jobs for bidsify/mriqc

  • e.g., bidsify.py
import os.path as op
import shutil
import subprocess
from zipfile import ZipFile
from glob import glob

#this is a comment

workdir = '/home/data/dcnlab/AHEAD/original_scans/orig_new'
for z_file in sorted(glob(op.join(workdir, '*.zip'))):
    #i can put a comment here
    print(z_file)
    zid = op.basename(z_file)
    _, _, subject, _ = zid.split("-")
    newpath = op.join(workdir, subject)
    if not op.exists(newpath):
        os.makedirs(newpath)
        print(subject)
        shutil.copy(z_file, newpath)
        with ZipFile(op.join(newpath, zid), "r") as zip_ref:
            zip_ref.extractall(newpath)
        os.remove(op.join(newpath, zid))
    print("Start script!")

    template_job = "/scratch/adick/bids_jobs/sample_job.sub"
    subj_job = '/scratch/adick/bids_jobs/{0}_sample_job.sub'.format(subject)
    with open(template_job, 'r') as fo:
        text = fo.read()
    text = text.replace('subject', subject)
    with open(subj_job, 'w') as fo:
        fo.write(text)
    os.chmod(subj_job, 0o775)
    subprocess.call('sbatch {0}'.format(subj_job), shell=True)

e.g., sample_job.sub

#---Number of core
#SBATCH -c 1

#---Job's name in LSF system
#SBATCH -J subject

#---Error file
#SBATCH -e job_err

#---Output file
#SBATCH -o job_out

#---Queue name
#SBATCH -q normal

#---Partition
#SBATCH -p investor

##########################################################
export NPROCS=`echo $LSB_HOSTS | wc -w`
export OMP_NUM_THREADS=$NPROCS

. $MODULESHOME/../global/profile.modules
module load singularity
module load python/2.7.5

##########################################################
##########################################################
sub=subject

python /home/data/nbc/cis_dataqc/cis-processing/run.py -d /home/data/dcnlab/AHEAD/original_scans/orig_new/$sub/scans -w /scratch/adick/ -b /home/data/dcnlab/AHEAD/dset --config /home/data/nbc/cis_dataqc/cis-processing/config/Dick_AHEAD.json --project AHEAD --sub $sub --ses S1
  • e.g., bidsify_tar.py
import os.path as op
import shutil
import subprocess
from tarfile import TarFile
from glob import glob

#this is a comment

workdir = '/home/data/dcnlab/AHEAD/original_scans/ses2_food_scans'
for t_file in sorted(glob(op.join(workdir, '*.tar'))):
    #i can put a comment here
    print(t_file)
    tid = op.basename(t_file)
    _, _, subject, _ = tid.split("-")
    #newpath = op.join(workdir, subject)
    #if not op.exists(newpath):
    #    os.makedirs(newpath)
    print(subject)
    #    shutil.copy(t_file, newpath)
    #    with TarFile(op.join(newpath, tid), "r") as tf:
    #        tf.extractall(newpath)
    #    os.remove(op.join(newpath, tid))
    print("Start script!")

    template_job = "/scratch/adick/bids_jobs/sample_job_tar.sub"
    subj_job = '/scratch/adick/bids_jobs/{0}_sample_job_tar.sub'.format(subject)
    with open(template_job, 'r') as fo:
        text = fo.read()
    text = text.replace('subject', subject)
    with open(subj_job, 'w') as fo:
        fo.write(text)
    os.chmod(subj_job, 0o775)
    subprocess.call('sbatch {0}'.format(subj_job), shell=True)

e.g., sample_job_tar.sub

##---Number of core
##SBATCH -c 1
#
##---Job's name in slurm system
##SBATCH -J subject
#
##---Error file
##SBATCH -e /home/data/dcnlab/AHEAD/error/bids_err_subject
#
##---Output file
##SBATCH -o /home/data/dcnlab/AHEAD/out/bids_out_subject
#
##---Queue name
##SBATCH -q pq_dcn
#
##---Access
#
##SBATCH -A acc_dcn
#
##---Partition
##SBATCH -p investor
#
##########################################################
# Set up environmental variables.
##########################################################
##########################################################
export NPROCS=`echo $LSB_HOSTS | wc -w`
export OMP_NUM_THREADS=$NPROCS

. $MODULESHOME/../global/profile.modules
module load singularity
module load python/2.7.5
source activate /home/data/nbc/data-analysis/env3

##########################################################
##########################################################
sub=subject

python /home/data/cis/cis-processing/cis_proc.py -t /home/data/dcnlab/AHEAD/original_scans/ses2_food_scans/Dick_AHEAD-000-$sub-S2.tar -b /home/data/dcnlab/AHEAD/food/dset -w /scratch/adick/bids_jobs --config /home/data/cis/cis-processing/config/Dick_AHEAD.json --sub $sub --ses S2 --n_procs 1

Python script to submit jobs to run freesurfer

import os.path as op
import shutil
import subprocess
from glob import glob

workdir = '/scratch/adick/aheadfmriprep/dset/'
for z_file in sorted(glob(op.join(workdir, 'sub-*'))):
    print(z_file)
    zid = op.basename(z_file)
    _, subject, = zid.split("-")
    print(subject)
    print("Start script!")
    template_job = "/scratch/adick/fsjobs/freesurfer_template.sub"
    subj_job = '/scratch/adick/fsjobs/{0}_freesurfer_AHEAD.sub'.format(subject)
    with open(template_job, 'r') as fo:
        text = fo.read()
        text = text.replace('subject', subject)
    with open(subj_job, 'w') as fo:
        fo.write(text)
        os.chmod(subj_job, 0o775)
    subprocess.call('sbatch {0}'.format(subj_job), shell=True)

e.g., freesurfer_template.sub

##---Number of core
##SBATCH -c 1
#
##---Job's name in slurm system
##SBATCH -J subject
#
##---Error file
#SBATCH -e /home/data/dcnlab/AHEAD/error/freesurfer_err_subject
#
##---Output file
#SBATCH -o /home/data/dcnlab/AHEAD/out/freesurfer_out_subject
#
##---Queue name
##SBATCH -q pq_dcn
#
##---Access
#
##SBATCH -A acc_dcn
#
##---Partition
##SBATCH -p investor
#
##########################################################
# Set up environmental variables.
##########################################################
export NPROCS=`echo $LSB_HOSTS | wc -w`
export OMP_NUM_THREADS=$NPROCS

. $MODULESHOME/../global/profile.modules
module load perl
export FREESURFER_HOME=/home/applications/freesurfer/6.0/freesurfer
source $FREESURFER_HOME/FreeSurferEnv.sh
export SUBJECTS_DIR=/scratch/adick/aheadfmriprep/dset/
module load netcdf/4.3.2
##########################################################
##########################################################
sub=subject

/home/applications/freesurfer/6.0/freesurfer/bin/recon-all -all -i /scratch/adick/aheadfmriprep/dset/sub-$sub/ses-S1/anat/sub-$sub\_ses-S1_T1w.nii.gz -subjid $sub -hippocampal-subfields-T1