#!/bin/sh
#
# Testing RNAalifold (MFE and general features)

echo 1..17 # 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
}


# simple version test
testname="correct version number"
if [ "x$(RNAalifold --version)" != "xRNAalifold $CURRENT_VERSION" ] ; then failed "$testname"; else passed "$testname"; fi

# Test dangle models (d0, d2)
for dangles in 0 2
do
  testname="MFE prediction (RNAalifold -d${dangles})"
  RNAalifold -q --noPS -d ${dangles} ${DATADIR}/rfam_seed_many_short.stk > rnaalifold.out
  diff=$(${DIFF} -ru ${RNAALIFOLD_RESULTSDIR}/rfam_seed_many_short.d${dangles}.mfe.gold rnaalifold.out)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi
done

# Test --noLP
testname="MFE prediction (RNAalifold --noLP)"
RNAalifold -q --noPS --noLP ${DATADIR}/rfam_seed_selected.stk > rnaalifold.out
diff=$(${DIFF} -ru ${RNAALIFOLD_RESULTSDIR}/rfam_seed_selected.noLP.mfe.gold rnaalifold.out)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test temperatures (25degC, 40degC)
for T in 25 40
do
  testname="MFE prediction (RNAalifold -T ${T})"
  RNAalifold -q --noPS -T ${T} ${DATADIR}/rfam_seed_selected.stk > rnaalifold.out
  diff=$(${DIFF} -ru ${RNAALIFOLD_RESULTSDIR}/rfam_seed_selected.T${T}.mfe.gold rnaalifold.out)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi
done

# Test maximum base pair span
testname="MFE prediction (RNAalifold --maxBPspan=30)"
RNAalifold -q --noPS --maxBPspan=30 -p0 ${DATADIR}/rfam_seed_selected.stk > rnaalifold.out
diff=$(${DIFF} -ru -I frequency ${RNAALIFOLD_RESULTSDIR}/rfam_seed_selected.span30.gold rnaalifold.out)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test automatic ID generation
testname="MFE prediction and automatic alignment ID feature"
RNAalifold -q --noPS --auto-id --id-prefix="blabla" --id-start=40 ${DATADIR}/rfam_seed_selected.stk > rnaalifold.out
diff=$(${DIFF} -ru ${RNAALIFOLD_RESULTSDIR}/rfam_seed_selected.id.mfe.gold rnaalifold.out)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test parameter file loading
for par in dna_mathews1999.par dna_mathews2004.par rna_andronescu2007.par rna_turner1999.par
do
  testname="MFE prediction (-P ${par})"
  RNAalifold -q --noPS -P ${MISC_DIR}/${par} ${DATADIR}/rfam_seed_selected.stk > rnaalifold.out
  diff=$(${DIFF} -ru ${RNAALIFOLD_RESULTSDIR}/rfam_seed_selected.${par}.mfe.gold rnaalifold.out)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi
done

# Test ribosum scoring
testname="MFE prediction with ribosum scoring (RNAalifold -r)"
RNAalifold -q -r --noPS ${DATADIR}/rfam_seed_selected.stk > rnaalifold.out
diff=$(${DIFF} -ru ${RNAALIFOLD_RESULTSDIR}/rfam_seed_selected.ribosum.mfe.gold rnaalifold.out)
if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi

# Test different input formats (clustal, fasta, maf, stockholm)
for file in alignment_clustal.aln alignment_fasta.fa alignment_maf.maf alignment_stockholm.stk
do
  testname="MFE prediction with different file formats (${file})"
  RNAalifold -q --noPS -r ${DATADIR}/${file} > rnaalifold.out
  diff=$(${DIFF} -ru ${RNAALIFOLD_RESULTSDIR}/${file}.mfe.gold rnaalifold.out)
  if [ "x${diff}" != "x" ] ; then failed "$testname"; echo -e "$diff"; else passed "$testname"; fi
done

# clean up
rm rnaalifold.out

exit ${RETURN}
