#  Wustl_RNAseq_Pipeline.sh
#  Processes RNAseq data from the raw fastq reads file through visualization of DGE using ggplot2 and GO/DAVID.
#  Supply as argument 1 a parameter file.
#  Example usage: user@domain:~$ Path/to/script/Wustl_RNAseq_Pipeline.sh /Path/to/ParameterFile.txt
#  Created by Paul Gontarz@BRC on 1/28/16.
#
paramfile=$1
pair_reads=FALSE
const_lane_size=FALSE

#Read through the paramater file

while read line;
do
{
    #Comment lines are denoted with '#'...Skip these.
    if [[ ${line:0:1} == "#" ]] || [[ ${#line} == 0 ]]
    then
    x=0
    else #Read in the lines with paramaters and their values and set them.
    {
        param="${line%%=*}"
        param_val="${line##*=}"

        if [[ $param == "WorkingDirectory" ]] ; then
            WD=$param_val
        elif [[ $param == "FastqDirectory" ]] ; then
            FD=$param_val
        elif [[ $param == "PairEndDirectory" ]] ; then
        {
            PD=$param_val
            if [[ ${#PD} != 0 ]] ; then
                pair_reads="TRUE"
            else
                pair_reads="FALSE"
            fi
        }
        elif [[ $param == "Lanes" ]] ; then
        {
            Lanes=$param_val
            if [[ ${#Lanes} == 1 ]] ; then
                const_lane_size="TRUE"
            fi
        }
        elif [[ $param == "Annotations" ]] ; then
            AF=$param_val
        elif [[ $param == "Aligner" ]] ; then
            aligner=$param_val
        elif [[ $param == "STARindex" ]] ; then
            Sindex=$param_val
        elif [[ $param == "STARThreads" ]] ; then
            threads=$param_val
        elif [[ $param == "ExtraSTARparams" ]] ; then
            ExParams=$param_val
        elif [[ $param == "fCountT" ]] ; then
            fCthreads=$param_val
        elif [[ $param == "fCountMQ" ]] ; then
            fCmapQ=$param_val
        elif [[ $param == "ExfCountParams" ]] ; then
            ExFparams=$param_val
        elif [[ $param == "DEGalgorithm" ]] ; then
            DEGalgorithm=$param_val
        elif [[ $param == "design" ]] ; then
            design=$param_val
        elif [[ $param == "Replicates" ]] ; then
            Replicates=$param_val
        elif [[ $param == "Conditions" ]] ; then
            Conditions=$param_val
        elif [[ $param == "ConName" ]] ; then
            ConName=$param_val
        elif [[ $param == "ChrSize" ]] ; then
            ChrSize=$param_val
        elif [[ $param == "RefGenName" ]] ; then
            RefGenName=$param_val
        elif [[ $param == "CPMcutoff" ]] ; then
            CPMcutoff=$param_val
        elif [[ $param == "pCutoff" ]] ; then
            pCutoff=$param_val
        elif [[ $param == "Log2FoldChange" ]] ; then
            Log2FoldChange=$param_val
        else echo "Unrecognized paramater" $param
        fi
    }
    fi
}
done <"$paramfile"
while read line; do
{
    if [[ $line == $RefGenName ]] ; then
    {
        read line
        Sindex=$line
        read line
        ChrSize=$line
        read line
        AF=$line
    }
    fi
}
done <"/home/pgontarz/RNASeqPipelineGenomeList.txt"
threads="6"
fCthreads="6"
fCmapQ="10"
DEGalgorithm="DESeq2"
if [[ $pair_reads == "TRUE" ]] ; then
    echo "Pair End Alignment Set"
else
    echo "Single End Alignment Set"
fi
#Begin analysis in the RNAseq pipeline.
#Create the working directory
if [[ -d "$WD" ]] ; then
x=0
else
mkdir "$WD" ; fi
#Get the list of fastq files
if [[ -f fastqs.txt ]] ; then
    rm fastqs.txt
fi
if [[ -f fastqs2.txt ]] ; then
    rm fastqs2.txt
fi
eval ls  $FD"*.fq" $FD"*.fastq" $FD"*.fq.gz" $FD"*.fastq.gz" 1> $WD"fastqs.txt" 2> $WD"warnings.txt"
if [[ $pair_reads == "TRUE" ]] ; then
    eval ls $PD"*.fq" $PD"*.fastq" $PD"*.fq.gz" $PD"*.fastq.gz" 1> $WD"fastqs2.txt" 2> $WD"warnings.txt"
fi

#Make a file for Alignment stats:
echo -e "File\tTotal_Reads\tUniquely_Mapped_Reads\tMulti_mapped_reads\tMulti_mapped_too_many_alignments" > $WD"AlnStatsTable.txt"

#Perform all alignments
FastqFiles=$WD"fastqs.txt"
if [[ $pair_reads == "TRUE" ]] ; then
    FastqFiles2=$WD"fastqs2.txt"
fi
sample=1
linesread=1
if [[ $const_lane_size == "TRUE" ]] ; then
{
    while read line; do
    {
        FullLane=$line
        for (( lanenum=2; lanenum <= Lanes; lanenum++ )) ; do
        {
            read line
            FullLane=$FullLane","$line
        }
        done
        if [[ $pair_reads == "TRUE" ]] ; then
        {
            line2=$(sed -n "${linesread}p" "$FastqFiles2")
            FullLane2=$line2
            ((linesread++))
            for (( lanenum=2; lanenum <= Lanes; lanenum++ )) ; do
            {
                line2=$(sed -n "${linesread}p" "$FastqFiles2")
                FullLane2=$FullLane2","$line2
                ((linesread++))
            }
            done
        }
        fi
echo "Processing sample "$sample": "$FullLane"."
        if [[ $pair_reads == "FALSE" ]] ; then
            STAR --genomeDir $Sindex --readFilesIn $FullLane --runThreadN $threads --outFileNamePrefix $WD$sample --outSAMtype BAM SortedByCoordinate --readFilesCommand zcat --limitBAMsortRAM 68719476736 $ExParams
        else
            STAR --genomeDir $Sindex --readFilesIn $FullLane $FullLane2 --runThreadN $threads --outFileNamePrefix $WD$sample --outSAMtype BAM SortedByCoordinate --readFilesCommand zcat --limitBAMsortRAM 68719476736 $ExParams
        fi
        mv $WD$sample"Aligned.sortedByCoord.out.bam" $WD$sample".bam"

        #Adds alignment results stats to text file "AlnStatsTable.txt"
        STARlog=$WD$sample"Log.final.out"
        echo -e $FullLane"\t\c" >> $WD"AlnStatsTable.txt"
        while read statline; do
        {
            for ((skip=0; skip<5; skip++)) ; do
                read statline ; done
            stat_val="${statline##*|}"
            echo -e $stat_val"\t\c" >> $WD"AlnStatsTable.txt"
            for ((skip=0; skip<3; skip++)) ; do
                read statline ; done
            stat_val="${statline##*|}"
            echo -e $stat_val"\t\c" >> $WD"AlnStatsTable.txt"
            for ((skip=0; skip<15; skip++)) ; do
                read statline ; done
            stat_val="${statline##*|}"
            echo -e $stat_val"\t\c" >> $WD"AlnStatsTable.txt"
            for ((skip=0; skip<2; skip++)) ; do
                read statline ; done
            stat_val="${statline##*|}"
            echo -e $stat_val >> $WD"AlnStatsTable.txt"
            for ((skip=0; skip<10; skip++)) ; do
               read statline ; done
        }
        done <"$STARlog"
        #Create the bigwig file for EpiGenomeBrowswer
        echo "Making wig and bigwig files"
        igvtools count -z 5 -w 25 $WD$sample".bam" $WD$sample".wig" $ChrSize 1>$WD"warnings.txt" 2>$WD"warnings.txt"
        wigToBigWig $WD$sample".wig" $ChrSize $WD$sample".bw"
        ((sample++))
    }
    done <"$FastqFiles"
}
else
{
x=0
}
fi
#Generate Counts for each Alignment file.
allbams=$WD"1.bam"
for (( x=2; x < sample; x++ )) ; do
{
    allbams=$allbams" "$WD$x".bam"
}
done
featureCounts -a $AF -o $WD"fCount.txt" -T $fCthreads -Q $fCmapQ -g gene_name $ExFparams $allbams
#Find DEG
echo "Entering The differential gene expression R script"
Rscript "/home/pgontarz/DEG_base.R" $WD"fCount.txt" $WD $sample $design $DEGalgorithm $Replicates $Conditions $ConName $CPMcutoff $pCutoff $RefGenName $Log2FoldChange
echo "Finished finding differentially expressed genes from R script. Cleaning up"
sample=1
while read fqline; do
fqline=${fqline##*/}
fqline=${fqline/%.fq.gz/}
mv $WD$sample".bam" $WD$fqline".bam"
mv $WD$sample".bw" $WD$fqline".bw"
((sample++))
done <"$FastqFiles"
rm $WD*.wig
rm -r $WD*_STAR*
rm -r $WD*out*



