#!/bin/bash

FL=1000   # frequency limit

if [ $# != 8 ]; then
   echo " ==== ERROR ... you called this script inappropriately."
   echo ""
   echo "   usage:  $0 seqXName seqYName lenght similarity WL fixedL output.frags output.csv"
   echo ""
   exit -1
fi

#{


MYRAND=$((( RANDOM % 10000000) +1))
MGDIR=${PWD}/${MYRAND}
echo "MGDIR $MGDIR"
mkdir -p ${MGDIR}

genoXname=$(basename "$1")
genoYname=$(basename "$2")
genoXname="${genoXname%.*}"
genoYname="${genoYname%.*}"


cp $1 $MGDIR/${genoXname}.fasta
cp $2 $MGDIR/${genoYname}.fasta
mkdir -p ${MGDIR}/dictionaries
mkdir -p ${MGDIR}/fragments

genoXExt="fasta"
genoYExt="fasta"



BINDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

length=${3}
similarity=${4}
WL=${5} # wordSize
fixedL=${6}
output=${7}
csv=${8}

mkdir ${MGDIR}/intermediateFiles

mkdir ${MGDIR}/intermediateFiles/${genoXname}-${genoYname}
mkdir ${MGDIR}/results
mkdir ${MGDIR}/intermediateFiles/dictionaries
mkdir ${MGDIR}/intermediateFiles/hits

# Copiamos los fastas
ln -s ${MGDIR}/${genoXname}.${genoXname} ${MGDIR}/intermediateFiles/${genoXname}-${genoYname}
ln -s ${MGDIR}/${genoYname}.${genoYname} ${MGDIR}/intermediateFiles/${genoYname}-${genoXname}

cd ${MGDIR}/intermediateFiles/${genoXname}-${genoYname}

###############



echo "${BINDIR}/reverseComplement ${MGDIR}/${genoYname}.${genoXExt} ${genoYname}-revercomp.${genoYExt}"
${BINDIR}/reverseComplement ${MGDIR}/${genoYname}.${genoYExt} ${MGDIR}/${genoYname}-revercomp.${genoYExt}

if [[ ! -f ../dictionaries/${genoXname}.d2hP ]];	then
	echo "${BINDIR}/dictionary.sh ${MGDIR}/${genoXname}.${genoXExt} &"
	${BINDIR}/dictionary.sh ${MGDIR}/${genoXname}.${genoXExt} &		
fi

if [[ ! -f ../dictionaries/${seqYName}.d2hP ]];	then
	echo "${BINDIR}/dictionary.sh ${MGDIR}/${genoYname}.${genoYExt} &"
	${BINDIR}/dictionary.sh ${MGDIR}/${genoYname}.${genoYExt} &
fi
		
if [[ ! -f ../dictionaries/${genoYname}-revercomp.d2hP ]];	then
	echo "${BINDIR}/dictionary.sh ${MGDIR}/${genoYname}-revercomp.${genoYExt} &"
	${BINDIR}/dictionary.sh ${MGDIR}/${genoYname}-revercomp.${genoYExt} &
fi		

echo "Waiting for the calculation of the dictionaries"

for job in `jobs -p`
do
    #echo $job
    wait $job
done



mv ${genoXname}.d2hP ../dictionaries/
mv ${genoXname}.d2hW ../dictionaries/
mv ${genoYname}.d2hP ../dictionaries/
mv ${genoYname}.d2hW ../dictionaries/
mv ${genoYname}-revercomp.d2hP ../dictionaries/
mv ${genoYname}-revercomp.d2hW ../dictionaries/


		
# Hacemos enlace simbolico
ln -s ../dictionaries/${genoXname}.d2hP .
ln -s ../dictionaries/${genoXname}.d2hW .

ln -s ../dictionaries/${genoYname}.d2hP .
ln -s ../dictionaries/${genoYname}.d2hW .

ln -s ../dictionaries/${genoYname}-revercomp.d2hP .
ln -s ../dictionaries/${genoYname}-revercomp.d2hW .

echo "${BINDIR}/comparison.sh ${MGDIR}/${genoXname}.${genoXExt} ${MGDIR}/${genoYname}.${genoYExt} ${length} ${similarity} ${WL} ${fixedL} f &"
${BINDIR}/comparison.sh ${MGDIR}/${genoXname}.${genoXExt} ${MGDIR}/${genoYname}.${genoYExt} ${length} ${similarity} ${WL} ${fixedL} f &

echo "${BINDIR}/comparison.sh ${MGDIR}/${genoXname}.${genoXExt} ${MGDIR}/${genoYname}-revercomp.${genoYExt} ${length} ${similarity} ${WL} ${fixedL} r &"
${BINDIR}/comparison.sh ${MGDIR}/${genoXname}.${genoXExt} ${MGDIR}/${genoYname}-revercomp.${genoYExt} ${length} ${similarity} ${WL} ${fixedL} r &

echo "Waiting for the comparisons"

for job in `jobs -p`
do
    #echo $job
    wait $job
done


#echo "rm ${seqYName}-revercomp.${extensionY}"
#rm ${seqYName}-revercomp.${extensionY}


echo "${BINDIR}/combineFrags ${genoXname}-${genoYname}-sf.frags ${genoXname}-${genoYname}-revercomp-sr.frags ${genoXname}-${genoYname}.frags"
${BINDIR}/combineFrags ${genoXname}-${genoYname}-sf.frags ${genoXname}-${genoYname}-revercomp-sr.frags ${genoXname}-${genoYname}.frags

#Borramos todo menos los frags y los diccionarios

# Get Info from frags 
echo "${BINDIR}/getInfo ${genoXname}-${genoYname}.frags > ${genoXname}-${genoYname}.csv"
${BINDIR}/getInfo ${genoXname}-${genoYname}.frags > ${genoXname}-${genoYname}.csv.tmp
cat ${genoXname}-${genoYname}.frags.INF ${genoXname}-${genoYname}.csv.tmp > ${genoXname}-${genoYname}.csv
rm -rf ${genoXname}-${genoYname}.csv.tmp
	
if [[ -L "../../${genoXname}.fasta" ]]
then
	rm ../../${genoYname}.fasta
fi

if [[ -L "../../${genoXname}.fasta" ]]
then
	rm ../../${genoYname}.fasta
fi

#Movemos los frags y los info
mv ${genoXname}-${genoYname}.frags $output
mv ${genoXname}-${genoYname}.csv $csv



#echo "Borrando ${seqXName}-${seqYName}"
cd ..

#rm -rf ${seqXName}-${seqYName}

cd ..


rm -r ${MGDIR}

