#!/usr/bin/env sh

help="[Usage] ./predict_gtf.sh infa ingtf specise_name outdir"

if [[ $# -eq 0 ]];then
    echo ${help}
    exit 2
fi
#transcript sequence
myfa=$(realpath $1)
#gtf file
mygtf=$(realpath $2)
#species
# human or mouse
myspe=$3
outdir=$(realpath $4) || exit 1
wd=${outdir}/wd
faname=${myfa##*/}
gtfname=${mygtf##*/}
mkdir -p ${outdir}
mkdir ${wd}
cp ${myfa} ${wd}
cp ${mygtf} ${wd}
myfa=${wd}/${faname}
mygtf=${wd}/${gtfname}

# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=$(dirname "$SCRIPT")
CONFDIR="../../data/confs"

if [[ ${myspe} == "mouse" ]];then
    iSeeConf=${CONFDIR}/iSeeRNA-1.2.2/conf/mm10.conf
    lncRScanConf=${CONFDIR}/lncRScan-SVM_v1.0.1/conf/mm10.conf
    lncScore_x=${CONFDIR}/lncScore/dat/Mouse_Hexamer.tsv
    lncScore_t=${CONFDIR}/lncScore/dat/Mouse_training.dat
elif [[ ${myspe} == "human" ]];then
    iSeeConf=${CONFDIR}/iSeeRNA-1.2.2/conf/hg19.conf
    lncRScanConf=${CONFDIR}/lncRScan-SVM_v1.0.1/conf/hg19.conf
    lncScore_x=${CONFDIR}/lncScore/dat/Human_Hexamer.tsv
    lncScore_t=${CONFDIR}/lncScore/dat/Human_training.dat
else
    echo "species name(the third argument) should be human or mouse"
    exit 1
fi
#COME
come1=${outdir}/come1
come2=${outdir}/come2
time -p -o ${outdir}/come1.time COME $mygtf ${come1} /home/yduan/soft/bio/seq/RNA_seq/COME/bin/ ${myspe} ${myspe}.NoExpHis.model
time -p -o ${outdir}/come2.time COME $mygtf ${come2} /home/yduan/soft/bio/seq/RNA_seq/COME/bin/ ${myspe} ${myspe}.model

#lncScore
time -p -o ${outdir}/lncScore.time lncScore.py -f ${myfa} -g $mygtf -o ${outdir}/lncScore.out -x ${lncScore_x} -t ${lncScore_t}

#lncRScan_SVM
lncRScan=${outdir}/lncRScan
time -p -o ${outdir}/lncRScan.time lncRScan-SVM-predict.py -g $mygtf -c ${lncRScanConf} -o ${lncRScan}

#iSeeRNA
iSeeRNA=${outdir}/iSeeRNA
mkdir ${iSeeRNA}
cd ${iSeeRNA}
iSeeOut=$(realpath .)
time -p -o ${outdir}/iSeeRNA.time sh $SCRIPTPATH/run_iSeeRNA.sh $mygtf $iSeeOut $iSeeConf 
cd ..

# mv intermediate file

itmf=${outdir}/itmf
mkdir ${itmf}
# come
mv ${come1}/result.txt ${outdir}/come1.out
mv ${come2}/result.txt ${outdir}/come2.out
rm -d ${come1}
rm -d ${come2}
# lncScore
mv ${outdir}/lncScore.out.log ${itmf}
# lncRScan
mv ${lncRScan}/${faname%.*}.prediction.result ${outdir}/lncRScan.out
mv ${lncRScan} ${itmf}
# iSeeRNA
mv ${iSeeRNA}/${faname%.*}.result ${outdir}/iSeeRNA.out
mv ${iSeeRNA} ${itmf}

rm -r ${wd}
