######################################################################################
# Kritika Karri, 08.04.2017
# Way to run script:
#Usage: ./Run_Jobs.sh
#Example:
#./Run_Jobs.sh
# The result fo this script copies all the Diffexp_v2_genebody (.txt) files from DE analysis (HTSeq Method) and copies in the current directory. These files are then used as input bu the Pearson_Script.R to generate pearson correlation plots and matrices. 
# The output of this script is two folders: 1) Pearson_All (All genes were involved to calculate the pearson) 2) Pearson_Filtered (Genes filtered by rpkm  > 1 are used for calculating the pearson values)
module load R/3.2.3

source ../00_Setup_Pipeline/01_Pipeline_Setup.sh

count=`ls -1 *.txt* 2>/dev/null  | wc -l`
if [ $count != 0 ]
then
rm -r *.txt
rm -r *.pdf
rm -r *.csv
rm -r DiffExp_*
rm -rf Pearson_All
rm -rf Pearson_Filtered
fi

rm -rf  Pearson_All
rm -rf Pearson_Filtered
mkdir Pearson_All 
mkdir Pearson_Filtered


echo "Dataset_DIR:"
echo ${Dataset_DIR}
echo "Dataset_Label:"
echo ${Dataset_Label}
echo "SCRIPT_DIR:"
echo ${SCRIPT_DIR}
cd ..

Level_UP=$(pwd)

cd ${SCRIPT_DIR}
echo "Script_Directory"
echo ${SCRIPT_DIR}
echo "Level_UP:"
echo ${Level_UP}

#echo 'Setup_Pipeline_DIR :' $Setup_Pipeline_DIR
cc=$(find ../ -name "*a_DiffExp_*_HTSeq" | wc -l);
echo $cc
start=1
for((i=$start; i <=$cc; i++))
 do 
	cp -rf ${Level_UP}/*a_DiffExp_${i}_HTSeq/Output_DiffExp_${i}a_HTSeq_GeneBody/DiffExp_v2_GeneBody* .
 done
	Rscript pearson_script.R

	mv  Pearson_Filtered_* Pearson_Filtered/
	mv  Pearson_All_* Pearson_All/ 
	
echo "HTSeq Pearson Analysis Done "

echo "#######################################################"
