#!/bin/sh
#
# Testing RNAfold (special features)

echo 1..21 # Number of tests to be executed.

RETURN=0

failed () {
    RETURN=1
    if [ "x$1" != "x" ]
    then
        echo "not ok - $1"
    else
        echo "not ok"
    fi
}

passed () {
    if [ "x$1" != "x" ]
    then
        echo "ok - $1"
    else
        echo "ok"
    fi
}


# Test G-Quadruplex feature
testname="G-Quadruplex feature (MFE, centroid, MEA)"
RNAfold --noPS -p --MEA -g --auto-id --id-prefix="rnafold_gquad_test" < ${DATADIR}/rnafold.gquad.fa > rnafold_special.fold
diff=$(${DIFF} -ru -I frequency ${RNAFOLD_RESULTSDIR}/rnafold.gquad.gold rnafold_special.fold)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

testname="G-Quadruplex feature (probabilities)"
for file in rnafold_gquad_test_00*dp.ps
do
  diff=$(${DIFF} -ru -I CreationDate -I Creator ${RNAFOLD_RESULTSDIR}/${file} ${file})
  if [ "x${diff}" != "x" ] ; then break; fi
done
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test Ligand-motif feature
testname="Aptamer Motif - Theophylline (MFE, centroid, MEA)"
RNAfold --noPS -p --MEA --motif="GAUACCAG&CCCUUGGCAGC,(...((((&)...)))...),-9.22" --id-prefix="rnafold_theo_test" < ${DATADIR}/rnafold.theo.fa > rnafold_special.fold
diff=$(${DIFF} -ru -I frequency ${RNAFOLD_RESULTSDIR}/rnafold.theo.gold rnafold_special.fold)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

testname="Aptamer Motif - Theophylline (probabilities)"
diff=$(${DIFF} -ru -I CreationDate -I Creator ${RNAFOLD_RESULTSDIR}/rnafold_theo_test_0001_dp.ps rnafold_theo_test_0001_dp.ps)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test Constraints (Hard, Soft, Unstructured domains)
testname="Command file - Constraints and Ligand motifs (MFE, centroid, MEA)"
RNAfold --noPS --commands=${DATADIR}/rnafold.cmds -v -p --MEA --auto-id --id-prefix="rnafold_cmd_test" < ${DATADIR}/rnafold.small.seq > rnafold_special.fold
diff=$(${DIFF} -ru  -I frequency -I MEA ${RNAFOLD_RESULTSDIR}/rnafold.small.cmds.gold rnafold_special.fold)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

testname="Command file - Constraints and Ligand motifs (probabilities)"
for file in rnafold_cmd_test_00*dp.ps
do
  diff=$(${DIFF} -ru -I CreationDate -I Creator ${RNAFOLD_RESULTSDIR}/${file} ${file})
  if [ "x${diff}" != "x" ] ; then break; fi
done
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi


# Test SHAPE reactivity input (Deigan, Zarringhalam, and Washietl methods)
for f in TPP_riboswitch_E.coli Lysine_riboswitch_T._martima 5domain16S_rRNA_H.volcanii 5domain16S_rRNA_E.coli
do
  testname="SHAPE probing data ($f) Deigan et al. 2009 method"
  RNAfold --noPS --shape=${DATADIR}/${f}.shape_2rows --shapeMethod=D < ${DATADIR}/${f}.db > rnafold_special.fold
  diff=$(${DIFF} -ru ${RNAFOLD_RESULTSDIR}/rnafold.SHAPE.${f}.D.gold rnafold_special.fold)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

  testname="SHAPE probing data ($f) Zarringhalam et al. 2012 method"
  RNAfold --noPS --shape=${DATADIR}/${f}.shape_2rows --shapeMethod=Z < ${DATADIR}/${f}.db > rnafold_special.fold
  diff=$(${DIFF} -ru ${RNAFOLD_RESULTSDIR}/rnafold.SHAPE.${f}.Z.gold rnafold_special.fold)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

  testname="SHAPE probing data ($f) Washietl et al. 2012 method"
  RNAfold --noPS --shape=${DATADIR}/${f}.pvmin --shapeMethod=W < ${DATADIR}/${f}.db > rnafold_special.fold
  diff=$(${DIFF} -ru ${RNAFOLD_RESULTSDIR}/rnafold.SHAPE.${f}.W.gold rnafold_special.fold)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi
done

# Test circfold
testname="MFE/PF prediction (RNAfold -p0 --circ)"
RNAfold --noPS -c -p0 < ${DATADIR}/rnafold.small.seq > rnafold_special.fold
diff=$(${DIFF} -ru -I frequency ${RNAFOLD_RESULTSDIR}/rnafold.small.circ.gold rnafold_special.fold)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test circfold with hard constraints
testname="MFE/PF prediction (circular RNAs + Hard constraints)"
RNAfold --noPS --commands=${DATADIR}/rnafold_hc.cmds -c -p0 < ${DATADIR}/rnafold.small.seq > rnafold_special.fold
diff=$(${DIFF} -ru -I frequency ${RNAFOLD_RESULTSDIR}/rnafold.small.circ.hc.gold rnafold_special.fold)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test circfold with soft constraints
# Here, the idea is that shifting the free energy of each structure by -1kcal/mol per nucleotide (100nt)
# does not change equilibrium probabilities
testname="MFE/PF prediction (circular RNAs + Soft constraints)"
for s in `cat ${DATADIR}/rnafold.small.seq`
do
  # compute base pair probabilities without soft constraints
  echo $s | RNAfold --noPS -c -p --id-prefix="test_circ" > rnafold_special.fold
  # compute base pair probabilities with soft constraints (shifting ensemble by -1kcal/mol per nucleotide)
  echo $s | RNAfold --noPS -c -p --id-prefix="test_circ_sc" --commands=${DATADIR}/rnafold_sc_lift.cmds > rnafold_special.fold
  diff=$(${DIFF} -ru -I CreationDate -I test_circ test_circ_sc_0001_dp.ps test_circ_0001_dp.ps)
  rm test_circ_0001_dp.ps test_circ_sc_0001_dp.ps
  if [ "x${diff}" != "x" ]; then break; fi
done
if [ "x${diff}" != "x" ]; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# clean up
rm rnafold_special.fold
rm rnafold_gquad_test_00*dp.ps rnafold_cmd_test_00*dp.ps rnafold_theo_test_0001_dp.ps

exit ${RETURN}
