#!/bin/sh

# runtests - created from runtests.txt by make
#
# $Id: runtests.txt 1823 2007-05-31 07:09:02Z tom $
#
# $Log$
# Revision 1.7  2006/03/08 20:50:11  nadya
# merge chamges from v3_5_2 branch
#
# Revision 1.6.4.4  2006/01/31 19:16:42  nadya
# rm obsolete $suffix references
# use MEME_SH variable for script calling
#
# Revision 1.6.4.3  2006/01/25 08:07:52  tbailey
# Remove blank lines from output files before diff-ing so that they
# don't mess up the automatic tests.
# Remove output file lines starting with "LAM " produced by some MPI implementations.
#
# Revision 1.6.4.2  2006/01/24 02:24:29  nadya
# EXT is obsoleted by using printf()
#
# Revision 1.6.4.1  2006/01/24 02:09:22  nadya
# use EXT variable for echo
# change "info" from an array to string to make it work on solaris
# change output to be table-like
#
# Revision 1.6  2005/10/05 06:18:35  nadya
# use full path for "rm". Asssume everybody has /bin/rm.
#
# Revision 1.5  2005/10/02 05:13:54  nadya
# set a variable for sourcing meme.sh
# add sourcing of meme_config to set additional environment
# calli binary executables by variables set in meme_config.
#
# Revision 1.4  2005/09/01 00:30:42  nadya
# extrapolate dataset name from one  embedded (with wrong path) in memefile
#
# Revision 1.3  2005/08/31 04:12:47  nadya
# fix sed regexpr
#
# Revision 1.2  2005/08/24 04:04:39  nadya
# add func_run_mast_adn() for a speciic test
#
# Revision 1.1  2005/08/18 01:40:18  nadya
# new runtests, initial revision
#
#

# set meme environment
top=`pwd`/..
MEME_DIRECTORY="$top"
export MEME_DIRECTORY
if [ ! $testdir ] ; then
  testdir=$top/tests
fi
meme=$top/src/meme.bin
mast=$top/src/mast.bin
make_logodds=$top/scripts/make_logodds

if [ ! -f $make_logodds ] ; then
    echo "$make_logodds does not exist"
    echo "Run 'make' before running 'make check'"
    exit 1
fi

if [ ! -x $meme ] ; then
    echo "$meme does not exist or is not executable"
    echo "Run 'make' before running 'make check'"
    exit 1
fi

if [ ! -x $mast ] ; then
    echo "$mast does not exist or is not executable"
    echo "Run 'make' before running 'make check'"
    exit 1
fi

####################################################################
#                      define functions 
####################################################################

# prints usage on stdout
func_usage () {
   echo "USAGE: $0 [OPTION]"
   echo
   echo "Known options:"
   echo "    -p NUM    run parallel version of MEME on NUM nodes"
   echo "    -s        save results of MEME and MAST"
   echo "    -nomeme   don't run MEME tests"
   echo "    -nomast   don't run MAST tests"
   echo "    -h        print this help"

}

# sets parameters for datasets
# takes a dataset name as an argument
func_set_params () {
    case "$1" in 
    crp0)
        params="-dna -revcomp" ;;
    INO_up800)
        params="-dna -revcomp -bfile $dir/common/yeast.nc.6.freq -x_branch" ;;
    farntrans5)
        params="-protein" ;;
    lipocalin)
        params="-protein" ;;
    adh)
        params="-protein" ;;
    esac
    params="$params -nostatus -nmotifs 2 -minw 8"
}

# runs make_logodds on input file and outputs
# alphabet and database used for corresponding meme run
# takes a meme filename as an argument
func_logodds () {
    logodds=$dirout/logodds.$$
    info=`perl $make_logodds $1 $logodds`
    alphabet=`echo $info | awk '{print $1}'`
    vardb=`echo $info | awk '{print $2}'`
    database=$dir/`basename $vardb`
}


# run mast tests on all datasets 
func_run_mast () {
    if [ $nomast ] ; then
        echo "Skipping mast test"
        return
    fi
    echo ""
    echo  "                   Mast test for "
    echo  "    ----------------------------------------------"
    echo  "      Dataset       Model     Sequence     result " 
    echo  "    ----------------------------------------------"
    for dset in $datasets
    do
        for mod in $models
        do
        seq=""
            memefile=meme/meme.$dset.$mod
            func_logodds $dir/$memefile
            params="-nostatus -logodds $logodds -alphabet $alphabet \
               -mf $dir/$memefile"
            mastfile=mast/mast.$dset.$mod
            params="$params -database $database -df $database"
            $mast "mast $memefile" $params > $dirout/$mastfile
             line=`echo $dset $mod | \
               awk '{printf("    %-16s%-8s%-14s  ", $1,$2, "")}'`
            func_diff $mastfile 
            func_save $dirout/$mastfile
            if [ "$dset" = "adh" ]; then
                func_run_mast_adn 
            fi

        done
    done
}

# run additional mast test for adh dataset
func_run_mast_adn () {
    seq=mini-drosoph.s
    params="-nostatus -logodds $logodds -alphabet $alphabet \
      -mf $dir/meme/$memefile"
    mastfile="mast/mast.$dset.$mod.$seq"
    args="$memefile -stdin -dna -seqp"
    params="$params -stdin -df -stdin -dna -seqp"
    cat $dir/common/$seq | $mast \
      "mast $args" $params > $dirout/$mastfile
    func_diff $mastfile 
    func_save $dirout/$mastfile
}

# run meme tests on all datasets
func_run_meme () {
    if [ $nomeme ] ; then
        echo "Skipping meme test"
        return
    fi
    echo ""
    echo  "                   Meme test for "
    echo  "    ----------------------------------------------"
    echo  "      Dataset       Model     Sequence     result " 
    echo  "    ----------------------------------------------"
    for dset in $datasets
    do
        func_set_params $dset 
        for mod in $models
        do
            memefile=meme/meme.$dset.$mod
            $meme $pargs $testdir/common/$dset.s -text -mod \
              $mod $params > $dirout/$memefile
            func_diff $memefile 
            func_save $dirout/$memefile
        done
    done
}

# remove lines specified by regexpr from the file
# takes a filename as an argument
func_sed () {
    e1="/Release date/d"
    e2="/http:/d"
    e3="/MOTIFS/d"
    e4="/Time/d"
    e5="/CPU:/d"
    e6="/DATAFILE=/d"
    e7="/DATABASE/d"
    e8="/command: /d"
    e9="/Background letter frequencies/d"
    e10="/Last updated/d"
    e11="/^LAM/d"
    e12="/^$/d"
    sed -e "$e1" -e "$e2" -e "$e3" -e "$e4" -e "$e5" \
        -e "$e6" -e "$e7" -e "$e8" -e "$e9" -e "$e10" \
        -e "$e11" -e "$e12" $1 > $1.sed
}

# checks two files for differences.  Arguments - a filename
# and a string containing dataset and model info to print.
func_diff () {
    f1=$dir/$1      # first file
    f2=$dirout/$1   # second file

    # string to print
    str=`echo $dset $mod $seq | awk '{printf("    %-16s%-8s%-14s  ", $1,$2,$3)}'`

    if [ ! -f  $f1 ]; then
        echo "File $f1 does not exist, can't compare output"
        echo  "$str" "SKIPPED"
        return 
    fi
    if [ ! -f  $f2 ]; then
        echo "File $f2 does not exist, can't compare output"
        echo  "$str" "SKIPPED"
        return 
    fi
    func_sed $f1 
    func_sed $f2 
    num=`diff $f1.sed $f2.sed | wc -l`
    status=$?
    if [ $status -eq 0 -a  $num -eq 0 ]; then
        echo  "$str" "OK"
    else
        echo  "$str" "FAIL"
        err=1
    fi
    return 
}

# initializes tests parameters
func_set () {
    datasets="crp0 INO_up800 farntrans5 lipocalin adh"
    models="oops zoops tcm"
    dir=$top/tests
    dirout=$top/tests/results
    if [ ! -d $dirout ]; then
        cmd=`mkdir $dirout`
        cmd=`mkdir $dirout/meme`
        cmd=`mkdir $dirout/mast`
        status=$?
        if [ $status -eq 1 ] ; then 
            exit 1
        fi
    fi
    err=0
}

# removes temp output files
func_clean () {
    /bin/rm -rf $dir/*.sed #$dirout/*.sed $dirout/logodds.*
    if [ ! $err ] ; then
        /bin/rm -rf $dirout
    fi
}

# saves file produced by meme or mast into tests/
# takes a file name as an argument
func_save () {
    if [ $save ]; then
        mv $1 $dir
    fi
}


####################################################################
#             check command line arguments and run tests
####################################################################
# check arguments
while test $# -gt 0; do
    case "$1" in
    -s)
        save=1 ;;
    -p)
        shift
        procs=$1
        pargs="-p $1"
        meme=$top/src/parallel/meme_p
        ;;
    -nomeme)
        nomeme=1 ;;
    -nomast)
        nomast=1 ;;
    -h)
        func_usage
        exit 1 ;;
    *)
        func_usage
        exit 1 ;;
    esac
    shift
done

# run tests
func_set 
func_run_meme
func_run_mast
func_clean

