#!/bin/bash

echo "Converting preprocessed timestat files to LaTeX files"
echo "with PS-Tricks macros using gnuplot ..."

# root dir
rootdir=/home/goetz/multal/alcompar

# data dir
datadir=$rootdir/alignments

# data file: file containing the input data
datafile=timestats.gnuplot

# labels dir
labelsdir=$rootdir/BAliBASE

# variables pointing to the script for gnuplot
global=$rootdir/gnuplots/scripts/timestats/glo

# dir containing the output files
outdir=$rootdir/gnuplots

# a filename for a temporary working file
tmpfile=/tmp/plttempfile.tmp

# function doing the real work:
# first parameter: dir name
# second parameter: output file name
# third parameter: dir containing no info about msa (=0) or containing (=M)
# optional 4th parameter: set range

run_gnuplot() {
    cd $labelsdir/$1
    echo "set output \"$outdir/$2.eps\"" > $tmpfile
    if [ "$4" != "" ]; then
	echo "set yrange [0:$4]" >> $tmpfile
    fi
    if [ "$3" == "M" ]; then
	echo "plot '$datadir/clustalx/$1/$datafile' t \"CLUSTAL-W\", '$datadir/TCOFFEE/$1/$datafile' t \"T-Coffee\", '$datadir/dialign2/$1/$datafile' t \"DIALIGN2\",'$datadir/hmmer/$1/$datafile' t \"HMMER\",'$datadir/msa/$1/$datafile' t \"MSA\" w points pt 9 ps 2" >> $tmpfile
    else
       	echo "plot '$datadir/clustalx/$1/$datafile' t \"CLUSTAL-W\", '$datadir/TCOFFEE/$1/$datafile' t \"T-Coffee\", '$datadir/dialign2/$1/$datafile' t \"DIALIGN2\",'$datadir/hmmer/$1/$datafile' t \"HMMER\"" >> $tmpfile
    fi
    cat $tmpfile | gnuplot $global -
    rm $tmpfile
}

# run conversion for each dir:
run_gnuplot ref1/test1 ts11 M 1
run_gnuplot ref1/test2 ts12 M 5
run_gnuplot ref1/test3 ts13 M 20
run_gnuplot ref2/test ts2 M 40
run_gnuplot ref3/test ts3 0
run_gnuplot ref4/test ts4 M
run_gnuplot ref5/test ts5 M 15
run_gnuplot ref6/test ts6 0 15000 
run_gnuplot ref6/test_1a ts61a 0
run_gnuplot ref6/test_1b ts61b 0
run_gnuplot ref6/test_2a ts62a M 1000
run_gnuplot ref6/test_2b ts62b 0 400
run_gnuplot ref6/test_2c ts62c 0 300
run_gnuplot ref6/test_3 ts63 0 300
run_gnuplot ref6/test_4 ts64 0 1000
run_gnuplot ref7/test ts7 0
run_gnuplot ref8/test ts8 0

echo "Done."
