#/data/zhang/wuj/tools/bowtie-0.12.3/bowtie -a --best /data/zhang/wuj/database/hg18/AS/TXdb.2/btw/TXdb -f -t s_1_sequence.txtparta >s_1.map &
#SrcFolder='/data/zhang/wuj/scripts/SpliceTrap.0.8'
InputFileName=$1
faorfq=$2
DatabasePrefix=$3
Outputfolder=$4
SrcFolder=$5
DatabaseFolder=$SrcFolder'/../db/'$DatabasePrefix'/TXdb.fasta'
TmpFolderName=`basename $1`

cd $Outputfolder;
#prepare the folder
if [ -d $TmpFolderName.result ];then
        echo "MAPPING: !!!Error, there is already a folder named "$TmpFolderName".result !"
        echo "MAPPING: !!!change the name of that folder first in case I erase them..."
        exit
fi
echo "MAPPING: Start to map $InputFileName....Creating cache folder $TmpFolderName.result"
mkdir  $TmpFolderName".result"
cd $TmpFolderName".result"
mkdir cache stat
cd cache
echo "MAPPING: Split file..."
split -l 1000000 $InputFileName
echo "MAPPING: generating shell scirpts...."
for name in x*
do

	echo "rmap -M 100 -m 2 -c $DatabaseFolder -o $name.rmap $name; perl $SrcFolder/rmap2eland.pl $name.rmap $name $name.eland;rm $name.rmap ;perl $SrcFolder/mark.mt.4eland.pl $name.eland >$name.nomt;rm $name.eland">>map.sh
	echo $name >>checklist	
done
tasknum=`wc -l map.sh |tr -d "\n"`
perl $SrcFolder/batchqsub.pl map.sh

echo "MAPPING: map $InputFileName to TXdb done...start to check.."
while [ 1 ]
do
        if [ -f mapcheck.sh ];then
                rm mapcheck.sh
        fi
        while read checklist
        do

                name=`echo $checklist |tr -d "\n"`
                echo "MAPPING: checking $name...."
                readnum=`wc -l $name | cut -f1 -d" "`
                if [ $faorfq == "fasta" ];then
                        readnum=`echo "$readnum/2"|bc`
                else
                        readnum=`echo "$readnum/4"|bc`
                fi
                if [ -f $name.nomt ];then
                        rmapnum=`wc -l $name.nomt | cut -f1 -d" "`
                else
                        rmapnum=0
                fi
                if [ $rmapnum != $readnum ];then
			echo "rmap -M 100 -m 2 -c $DatabaseFolder -o $name.rmap $name; perl $SrcFolder/rmap2eland.pl $name.rmap $name $name.eland;rm $name.rmap ;perl $SrcFolder/mark.mt.4eland.pl $name.eland >$name.nomt;rm $name.eland">>mapcheck.sh

                fi
        done <checklist
        if [ -f mapcheck.sh ];then
                checktasknum=`wc -l mapcheck.sh |tr -d "\n"`
                if [ $checktasknum == $tasknum ];then
                        echo "MAPPING: warning! none of the mapping tasks properly finished!"
                fi
                echo "MAPPING: resubmiting TASKS...."
                perl $SrcFolder/batchqsub.pl mapcheck.sh
        else
                break
        fi
done
echo "MAPPING: Mapping is really done....merging files..."


cat *.nomt >$Outputfolder/$TmpFolderName.nomt
cd ../../

rm $TmpFolderName.result -rf

