#!/bin/bash

# Example multi-job script designed to submit a single job script to 
# the scheduler multiple times, each time using the ID of a different
# subject.
#
# This illustrates how the cluster can be used to run repetitive jobs
# (such as running the same analysis on different designs) in parallel
# in parallel. 
#
# In this example, the script that runs the analysis for each subject 
# uses 3 variables:
# 	SUBID = the subject id number
#	ANADIR = the main analysis directory
#	OW = overwrite flag: 1 = overwrite any existing directories with
#		the same name. 0 = Don't overwrite (script exits)


# Define main analysis directory
ANADIR=/imaging/ic01/projects/cbu/iRSA_fMRI/subjects2014

# Set overwrite flag
OW=1

# Define list of subjects:
ALLSUB="CBU101325  CBU101358  CBU101370  CBU101513  CBU101559  CBU101585  CBU101605  CBU110221  CBU110294  CBU111010  CBU111027  CBU111066
# CBU101312  CBU101331  CBU101363  CBU101450  CBU101542  CBU101576  CBU101592  CBU101606  CBU110242  CBU110362  CBU111013  CBU111033  CBU111111
# CBU101316  CBU101355  CBU101364  CBU101460  CBU101544  CBU101577  CBU101596  CBU101607  CBU110250  CBU110423  CBU111017  CBU111047
# CBU101322  CBU101356  CBU101365  CBU101461  CBU101555  CBU101578  CBU101597  CBU101616  CBU110280  CBU110428  CBU111026  CBU111057"


# Loop through subjects and submit each one to the scheduler:
for S in ${ALLSUB};do


	# use qsub to submit the single job script to the scheduler. 
	# The name of the current design is passed to the single job script
	# in a variable called SUBID using qsub's -v argument.
	#
	# The format for -v is:
	#
	# qsub -v <variable name>="<variable value>"
	# 
	# This makes a variable called <variable name> availalbe within the single jobs script.
	# In this example, the job script will have access to variables called
	# SUBID, ANADIR, and OW

	qsub -v SUBID=${S},ANADIR=${ANADIR},OW=${OW} 02_2_Freesurfer_prepro_single_sub.pbs
done

