#!/bin/bash

isoEMDir=/galaxy/galaxy/tools-dependencies/bin/isoem2/
toolpath=${isoEMDir}/bin
isoDEPath=${isoEMDir}/bin
supportCalcPath=${isoEMDir}/src/calc
fpkmGeneCommand=""
fpkmIsoformCommand=""
tpmGeneCommand=""  
tpmIsoformCommand=""

numberOfBootstrapIterationsPerSample=199

arg=($*)
i=0
while [ $i -lt $# ]
do
        a=${arg[i]}
	if [ "$a" == "-c1" ]; then
		fpkmGeneCommand="$fpkmGeneCommand -c1"
		fpkmIsoformCommand="$fpkmIsoformCommand -c1"
		tpmGeneCommand="$tpmGeneCommand -c1"
		tpmIsoformCommand="$tpmIsoformCommand -c1"
		
		((i++))
		 a=${arg[i]}
		rep=1
		while [[  `expr index "$a" "/"` -ne 0  && $i -lt $# ]]
		do
			condition1File=$a
			${toolpath}/convertToisoDEFormatWithIsoforms $condition1File c1_rep${rep}

			fpkmGeneCommand="$fpkmGeneCommand c1_rep${rep}_fpkm_G"
			fpkmIsoformCommand="$fpkmIsoformCommand c1_rep${rep}_fpkm_I"
			tpmGeneCommand="$tpmGeneCommand c1_rep${rep}_tpm_G"
			tpmIsoformCommand="$tpmIsoformCommand c1_rep${rep}_tpm_I"
			((rep++))

			((i++))
			 a=${arg[i]}
		done
        elif [ "$a" == "-c2" ]; then 
		fpkmGeneCommand="$fpkmGeneCommand -c2"
		fpkmIsoformCommand="$fpkmIsoformCommand -c2"
		tpmGeneCommand="$tpmGeneCommand -c2"
		tpmIsoformCommand="$tpmIsoformCommand -c2"

		((i++))
		 a=${arg[i]}
		rep=1
		while [[  `expr index "$a" "/"` -ne 0 &&  $i -lt $# ]]
                do
			condition1File=$a
			#echo $condition1File
			${toolpath}/convertToisoDEFormatWithIsoforms $condition1File c2_rep${rep}

			fpkmGeneCommand="$fpkmGeneCommand c2_rep${rep}_fpkm_G"
			fpkmIsoformCommand="$fpkmIsoformCommand c2_rep${rep}_fpkm_I"
			tpmGeneCommand="$tpmGeneCommand c2_rep${rep}_tpm_G"
			tpmIsoformCommand="$tpmIsoformCommand c2_rep${rep}_tpm_I"
			((rep++))

                     ((i++))
                     a=${arg[i]}
                done
	elif [ "$a" == "-pval" ]; then 
		((i++))
		pval=${arg[i]}
		((i++))


	elif [ "$a" == "-geneFPKMout" ]; then 
		((i++))
		geneFPKMout_file=${arg[i]}
		((i++))


	elif [ "$a" == "-geneTPMout" ]; then 
		((i++))
		geneTPMout_file=${arg[i]}
		((i++))

	elif [ "$a" == "-isoFPKMout" ]; then 
		((i++))
		isoFPKMout_file=${arg[i]}
		((i++))

	elif [ "$a" == "-isoTPMout" ]; then 
		((i++))
		isoTPMout_file=${arg[i]}
		((i++))
	else

		((i++))
	fi

done
((rep--))
bootstrap=$(($rep*$numberOfBootstrapIterationsPerSample))
echo boostrap iterations $bootstrap
#support=`java -cp ${supportCalcPath} support 200 200 $pval`
#support=`java -cp ${supportCalcPath} support $bootstrap $bootstrap $pval`
#Calculator assumes IsoDE does all pairs when calculation the number of ratios. It multiplies the number of bootstrap samples per condition
# changed the second parameter to make the number of ratios equal to the number of bootstrap samples (match, not all pairs)
echo calculate support based on p-value and number or replicates
support=`java -cp ${supportCalcPath} support $bootstrap 1 $pval`
fpkmGeneCommand="$fpkmGeneCommand -b $support"
fpkmIsoformCommand="$fpkmIsoformCommand -b $support"
tpmGeneCommand="$tpmGeneCommand -b $support"
tpmIsoformCommand="$tpmIsoformCommand -b $support"

fpkmGeneCommand="$fpkmGeneCommand -dfc 2"
fpkmIsoformCommand="$fpkmIsoformCommand -dfc 2"
tpmGeneCommand="$tpmGeneCommand -dfc 2"
tpmIsoformCommand="$tpmIsoformCommand -dfc 2"
 

#if [ "${out_prefix}" == "$(basename ${out_prefix} )" ]; then
#	pth=`pwd`
#	out_prefix=${pth}/${out_prefix}
#fi
 

echo GENE FPKM
echo moving start
date

mkdir fpkm_G
cd fpkm_G
mv ../c*_fpkm_G .


echo isoDE start
date


${isoDEPath}/isodecalls $fpkmGeneCommand -out "output.txt"
#awk '{for (f=1; f<=NF; f++) {if (f == NF)  printf "%s",$f; else printf "%s\t", $f }; print ""}' Bootstrap_Merge1_DIR/output.txt | sed 1,1d > ${geneFPKMout_file}

echo awk command
date


awk '{if (NR == 1) {print "Gene ID\tConfident log2 FC\t Single run log2 FC\t c1 average FPKM\tc2 average FPKM";} else {if ($6 == 0 && $7 == 0) {two="0";} else {two=$2;} print $1 "\t" two "\t"  $5 "\t" $6 "\t" $7}}' Bootstrap_Merge1_DIR/output.txt > ${geneFPKMout_file}

cd ..


echo ISOFORM FPKM

echo moving start
date
 
mkdir fpkm_I
cd fpkm_I
mv ../c*_fpkm_I .


echo isoDE start
date

${isoDEPath}/isodecalls $fpkmIsoformCommand -out "output.txt"

echo awk command
date
pwd
awk '{if (NR == 1) {print "Isoform ID\tConfident log2 FC\t Single run log2 FC\t c1 average FPKM\tc2 average FPKM";} else {if ($6 == 0 && $7 == 0) {two="0";} else {two=$2;} print $1 "\t" two "\t"  $5 "\t" $6 "\t" $7}}' Bootstrap_Merge1_DIR/output.txt  > ${isoFPKMout_file}
cd ..


echo ISOFORM TPM
echo moving start
date

mkdir tpm_G
cd tpm_G
mv ../c*_tpm_G .

echo isoDE start
date

${isoDEPath}/isodecalls $tpmGeneCommand -out "output.txt"

echo awk command
date
pwd
awk '{if (NR == 1) {print "Gene ID\tConfident log2 FC\t Single run log2 FC\t c1 average TPM\tc2 average TPM";} else {if ($6 == 0 && $7 == 0) {two="0";} else {two=$2;} print $1 "\t" two "\t"  $5 "\t" $6 "\t" $7}}'  Bootstrap_Merge1_DIR/output.txt  > ${geneTPMout_file}
cd ..


echo ISOFORM TPM
echo moving start
date

mkdir tpm_I
cd tpm_I
mv ../c*_tpm_I .

echo isoDE start
date

${isoDEPath}/isodecalls $tpmIsoformCommand -out "output.txt"

echo awk command
date

awk '{if (NR == 1) {print "Isoform ID\tConfident log2 FC\t Single run log2 FC\t c1 average TPM\tc2 average TPM";} else {if ($6 == 0 && $7 == 0) {two="0";} else {two=$2;} print $1 "\t" two "\t"  $5 "\t" $6 "\t" $7}}'  Bootstrap_Merge1_DIR/output.txt  > ${isoTPMout_file}

cd ..

 
echo final cleanup
date
rm -fr fpkm_G fpkm_I tpm_G cd tpm_I
echo done
date


