######################################################################################
# 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 (FeatureCount 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 featureCounts
fi

rm -rf Pearson_FPKM_Filtered 
rm -rf Pearson_All
mkdir  Pearson_FPKM_Filtered 
mkdir Pearson_All

echo "Dataset_DIR:"
echo ${Dataset_DIR}
echo "Dataset_Label:"
echo ${Dataset_Label}
echo "SCRIPT_DIR:"
echo ${SCRIPT_DIR}
echo "Scripts for DiffEXP:"
#echo ${Setup_Pipeline_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

dd=$(find ../ -name "*b_DiffExp_*_featureCounts" | wc -l);
echo $dd
start_dd=1
for((j=$start_dd; j <=$dd; j++))
 do
        cp -rf ${Level_UP}/*b_DiffExp_${j}_featureCounts/Output_DiffExp_${j}a_featureCounts_GeneBody/DiffExp_v2_GeneBody* .
 done
        Rscript pearson_script.R

        mv  Pearson_Filtered_* Pearson_FPKM_Filtered/
        mv  Pearson_All_* Pearson_All/

