#!/bin/bash

# read1 in fastq format
r1=$1

# read2 in fastq format
r2=$2

# memory for qsub job
mem=$3

# num CPUs to use
cpu=$4

# directory to write outfiles to 
out=$5

# sample name to use as prefix for outfiles
sample=$6

# reference sequence in fasta format, indexed for bwa 
# to index:
# bwa index ref.fa

ref=$7

NAME="map_mem.$sample"

# this generates a unique instance for the generated script and allows tracking of the steps in this script 
instance=$(date +%j-%M-%N)

echo "#!/bin/bash

## these options are for submitting this script to a SGE cluster. Modify to fit yours, or disregard if you want to run it locally

#$ -S /bin/bash                                                                                                                                                        
                                                                                                                                                                       
#$ -l  h_vmem="$mem"G,virtual_free="$mem"G                                                                                                                                     
                                                                                                                                                                       
#$ -pe smp $cpu                                                                        
      
#$ -o $out/$sample.map_mem.out
                                                                                                                                                                       
#$ -e $out/$sample.map_mem.err
                                                                                                                                                                       
#$ -m b

#$ -m e                                                                                                                                                              
                                                                                                                                                                       
#$ -M my_email@my_lab.org                                                                                                                                                
                                                                                                                                                                       
#$ -N map_mem.$sample                                                                                                                                                  
                                                                                                                                           
#$ -r n

## end SGE options

instance=$instance
script_name=mem_map
name=$NAME
out=$out 

sample=$sample.vs.hg19_pbneo

r1=$r1
r2=$r2
cpu=$cpu


# modify these to explicitely state the /path/to/tool if these are not already in your $PATH
samtools=samtools
bwa=bwa


ref=$ref


date
echo \"START!\"

date=\$(date)
echo \"START \$date $name \$instance \$script_name \$out/$sample.map_mem\" 

step_name=\"map\"
if \$bwa mem -t \$cpu \$ref \$r1 \$r2 | samtools view -Sb - > \$out/\$sample.mem.bam ; then
    date=\$(date)
    echo \"STEP \$date \$name \$instance \$step_name\" 
else
    date=\$(date)
    echo \"FAIL \$date \$name \$instance \$step_name\" 
 exit 1
fi


date=\$(date)
echo \"END \$date $name \$instance \$script_name \$out\" 




date
echo \"DONE!\" 
"


