#################################################
# metagenomic structural variation pipeline v1.0 - part II
#################################################

wd=/groups/umcg-lld/tmp04/umcg-dwang/SVs/lld # absolute path of work space
input_list=$wd/input/cohorts.tsv # IMPORTANT! A table contains the cohorts information and corresponding *.jsdel files which are generated by part I, please see 'cohorts.tsv' as a reference, the first column is cohort id, the second column is the path with wildcard of the corresponding cohort, the third column is the minimum number of samples in which a microbe exists with sufficient coverage to be considered in the analysis (suggest 10% of total sample size)
job_pre=s02.SVs_cohorts # a job prefix you prefer

# rm    -rf  $wd/$job_pre.sbatch

mkdir -p  $wd/$job_pre.sbatch
mkdir -p  $wd/$job_pre.res

cat $input_list | while read line
do
	read -r -a array <<< "$line"
	
	sample_id="${array[0]}"
	glob="${array[1]}"
	n="${array[2]}"
	oo="$wd/$job_pre.sbatch/$job_pre.$sample_id.sh"
	
	echo "#!/bin/bash" > $oo
	echo "#SBATCH --job-name=$job_pre.$sample_id" >> $oo
	echo "#SBATCH --error=$job_pre.$sample_id.err" >> $oo
	echo "#SBATCH --output=$job_pre.$sample_id.out" >> $oo
	echo "#SBATCH --mem=10gb" >> $oo
	echo "#SBATCH --time=10:00:00" >> $oo
	echo "#SBATCH --cpus-per-task=1" >> $oo
	echo "#SBATCH --constraint=\"tmp04\"" >> $oo
	echo "#SBATCH --export=NONE" >> $oo
	echo "#SBATCH --get-user-env=L" >> $oo

	echo "module load Cython/0.24.1-foss-2015b-Python-2.7.11" >> $oo

	echo "mkdir -p $wd/$job_pre.res/$sample_id" >> $oo
	echo "mkdir -p $wd/$job_pre.res/$sample_id/$sample_id.html" >> $oo
	
	echo "python /groups/umcg-lld/tmp04/umcg-dwang/software/SGVFinder/src/SGVF_cmd.py \"$glob\" $wd/$job_pre.res/$sample_id/$sample_id.dsgv.csv $wd/$job_pre.res/$sample_id/$sample_id.vsgv.csv --min_samp_cutoff $n --x_coverage 0.01 --rate_param 10 --browser_path $wd/$job_pre.res/$sample_id/$sample_id.html --csv_output" >> $oo # KEY command! you can change the parameters to make it suitable with your project, the value of '--x_coverage' should be same with you set in part I 
done

