#
# In this script some of the tools are overly verbose and print lots of useless information
# as they run. This can be quite annoying. In those cases we redirect and append the stdout and 
# stderr into the log.txt file  using:  1>>log.txt 2>>log.txt
# We keep the output in case we need to troubleshoot.
#

# Stop on errors.
set -uex

# UNIX trick: Adding the -x flag will also show the commands as these are executed.
# This can help you track down the errors as these happen.

# There will be many ancillary files.
# Better keep them out of sight.
mkdir -p db

#
# When run from command line use the second lines to set the parameters.
#
# AF086833  - accession number for the Mayinga Ebola strain from 1976
ACC=AF086833
#ACC=AF086833

# The SRR number for the sequencing data.
SRR=SRR1972739
#SRR=SRR1972739

# This will be the reference genome we align against.
REF=db/${ACC}.fa

# It is best to build all variables at the top ot the program.
# That way you see how these are constructed.
READ1=${SRR}_1.fastq
READ2=${SRR}_2.fastq

# AF086833  - Mayinga Ebola strain from 1976
efetch -db=nuccore -format=fasta -id=${ACC} > $REF

# Obtain the sequencing data from SRA.
fastq-dump -X 10000 --split-files $SRR 1>> log.txt 2>> log.txt

# Build a bowtie2 index.
# This only needs to be done once.
bowtie2-build $REF $REF 1>> log.txt 2>> log.txt

# Build a bwa index.
# Redirect both stdout and stderr to a file.
# This only needs to be done once.
bwa index $REF  1>> log.txt 2>> log.txt

# Run the bowtie2 aligner in single end mode.
bowtie2 -x $REF -U $READ1 > bowtie2.SE.sam 2>> log.txt

# Run the bwa aligner in single end mode.
bwa mem $REF $READ1 > bwa.SE.sam 2>> log.txt

# We need to run the flagstat ourselves.
echo "*** BOWTIE2 SE flagstat:"
samtools flagstat bowtie2.SE.sam

echo "*** BWA SE flagstat:"
samtools flagstat bwa.SE.sam

# Run the bowtie2 aligner in single end mode.
bowtie2 -x $REF -1 $READ1 -2 $READ2 > bowtie2.PE.sam 2>> log.txt

# Run the bowtie2 aligner in single end mode.
bwa mem $REF $READ1 $READ2 > bwa.PE.sam 2>> log.txt

echo "*** BOWTIE2 PE flagstat:"
samtools flagstat bowtie2.PE.sam

echo "*** BWA PE flagstat:"
samtools flagstat bwa.PE.sam