#!/bin/bash
##################################################################################
#Andy Rampersaud, 02.23.16
#This script would be used to summarize CollectRnaSeqMetrics statistics 
#Way to run script:
#Usage: 
#./CollectRnaSeqMetrics_Summary.sh
##################################################################################
#---------------------------------------------------------------------------------
#Source the setup file to initialize variables
source ../00_Setup_Pipeline/01_Pipeline_Setup.sh
#---------------------------------------------------------------------------------
#Check that each variable prints a value to the terminal:
echo "-----------------------"
echo "Start of variable list:"
echo "-----------------------"
echo "Dataset_DIR:"
echo ${Dataset_DIR}
echo "Sample_Labels_DIR:"
echo ${Sample_Labels_DIR}
echo "STRAND_SPECIFICITY:"
echo ${STRAND_SPECIFICITY}
echo "SCRIPT_DIR:"
echo ${SCRIPT_DIR}
echo "TIME_LIMIT:"
echo ${TIME_LIMIT}
echo "-----------------------"
echo "End of variable list"
echo "-----------------------"
#---------------------------------------------------------------------------------
##################################################################################
#---------------------------------------------------------------------------------
#Retrieve the job name for this step:
Current_DIR=$(pwd)
#Extract the folder name:
DIR_name=`basename ${Current_DIR}`
#---------------------------------------------------------------------------------
OUTPUT_DIR=${Dataset_DIR}/Scripts/${DIR_name}/Job_Summary
###############################
if [ ! -d ${OUTPUT_DIR} ]; then
mkdir -p ${OUTPUT_DIR}
else
#Remove dir:
rm -r ${OUTPUT_DIR}
#Make new dir:
mkdir -p ${OUTPUT_DIR}
fi
###############################
OUTPUT_FILE=$OUTPUT_DIR/CollectRnaSeqMetrics_Stats.txt
######################
if [ -f $OUTPUT_FILE ]
then 
rm $OUTPUT_FILE
else
touch $OUTPUT_FILE
fi
######################
cd ${Current_DIR}
################################################
#A text file (Sample_Labels.txt) is needed to run this script
SCRIPT_DIR=$(pwd)
cp $Sample_Labels_DIR/Sample_Labels.txt $SCRIPT_DIR
################################################
#The text file is formatted like the following:
#----------------------------------------------
#Sample_DIR	Sample_ID	Description
#Sample_Waxman-TP17	G83_M1	Male 8wk-pool 1
#Sample_Waxman-TP18	G83_M2	Male 8wk-pool 2
#Sample_Waxman-TP19	G83_M3	Female 8wk-pool 1
#Sample_Waxman-TP20	G83_M4	Female 8wk-pool 2	
#----------------------------------------------
#The 1st column: The Sample_DIR name
#The 2nd column: Waxman Lab Sample_ID 
#The 3rd column: Sample's description 
################################################
#Print header to output file:
echo SAMPLE_ID $'\t'DESCRIPTION $'\t'PCT_CODING_BASES $'\t'PCT_UTR_BASES $'\t'PCT_INTRONIC_BASES $'\t'PCT_INTERGENIC_BASES $'\t'PCT_MRNA_BASES $'\t'PCT_RIBOSOMAL_BASES >> $OUTPUT_FILE
################################################
#Text file has a header line to ignore:
tail -n +2 Sample_Labels.txt > Sample_Labels.temp
#Use a while loop to run jobs
while IFS=$'\t' read -r -a myArray
do
#---------------------------
##Check that text file is read in properly:
#echo 'Sample_DIR:'
Sample_DIR=${myArray[0]}
#echo 'Sample_ID:'
Sample_ID=${myArray[1]}
#echo $Sample_ID
#echo 'Description:'
Description=${myArray[2]}
#echo $Description
#---------------------------
echo
echo $Sample_ID
#Need to cd to sample specific CollectRnaSeqMetrics folder
cd ${Dataset_DIR}/$Sample_ID/fastq/tophat2/CollectRnaSeqMetrics
#Extract data from temp1.txt
#Extract lines 7 and 8 from file
sed -n 7,8p $Sample_ID'_metrics' > temp1.txt
PCT_CODING_BASES=$(awk '{print $12}' temp1.txt | sed -n 2p)
#echo $PCT_CODING_BASES
PCT_UTR_BASES=$(awk '{print $13}' temp1.txt | sed -n 2p)
#echo $PCT_UTR_BASES
PCT_INTRONIC_BASES=$(awk '{print $14}' temp1.txt | sed -n 2p)
#echo $PCT_INTRONIC_BASES
PCT_INTERGENIC_BASES=$(awk '{print $15}' temp1.txt | sed -n 2p)
#echo $PCT_INTERGENIC_BASES
PCT_MRNA_BASES=$(awk '{print $16}' temp1.txt | sed -n 2p)
#echo $PCT_MRNA_BASES
PCT_RIBOSOMAL_BASES=$(awk '{print $11}' temp1.txt | sed -n 2p)
#echo $PCT_RIBOSOMAL_BASES
#Remove temp1.txt
rm temp1.txt
#Print to output file
echo $Sample_ID $'\t'$Description $'\t'$PCT_CODING_BASES $'\t'$PCT_UTR_BASES $'\t'$PCT_INTRONIC_BASES $'\t'$PCT_INTERGENIC_BASES $'\t'$PCT_MRNA_BASES $'\t'$PCT_RIBOSOMAL_BASES >> $OUTPUT_FILE
done < Sample_Labels.temp
cd ${Current_DIR}
#Remove the temp file:
rm Sample_Labels.temp
echo '#-------------------------------------------------------------------------'
echo 'Check out '$OUTPUT_FILE
echo '#-------------------------------------------------------------------------'
##################################################################################
