#!/bin/bash

echo "Converting preprocessed score files to .eps files"

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

# data dir
datadir=$rootdir/alignments

# labels dir
labelsdir=$rootdir/BAliBASE

# variables pointing to the script for gnuplot
global=$rootdir/gnuplots/scripts/scores/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: input file part
# third parameter: output file name
# 4th parameter: dir containing info about MSA (="M") 
# 5th parameter: dir containing info about BAliBASE ref ali (="B")
#                (ie. non-difference scores)
# optional 6th parameter: set range

run_gnuplot() {
    cd $labelsdir/$1
    echo "set output \"$outdir/$3.eps\"" > $tmpfile
    echo "set title \"$2\"" >> $tmpfile
    if [ "$6" != "" ]; then
	echo "set yrange [0:$6]" >> $tmpfile
    fi
    datafile=$2.gnuplot
    if [ "$4" == "M" ]; then
	if [ "$5" == "B" ]; 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, '$labelsdir/$1/$datafile' t \"BAliBASE\" w points pt 13 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\",'$datadir/msa/$1/$datafile' t \"MSA\" w points pt 9 ps 2" >> $tmpfile
	fi
    else
	if [ "$5" == "B" ]; 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\", '$labelsdir/$1/$datafile' t \"BAliBASE\" w points pt 13 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
    fi
    cat $tmpfile | gnuplot $global -
    rm $tmpfile
}

#process one directory
# first parameter: dir name
# second parameter: output file name
# third parameter: score list length l=long s=short (for ref6 ...)
# 4th parameter: MSA info available
# optional 5th parameter: set range
score_dir() {
    run_gnuplot $1 dis $2dis $4 0 $5
    run_gnuplot $1 pam $2pam $4 B $5
    run_gnuplot $1 blosum $2blosum $4 B $5

    if [ "$3" == "l" ]; then
	run_gnuplot $1 sdis $2sdis $4 0 $5
	run_gnuplot $1 spam $2spam $4 B $5
	run_gnuplot $1 sblosum $2sblosum $4 B $5

	run_gnuplot $1 csdis $2csdis $4 0 $5
	run_gnuplot $1 cspam $2cspam $4 B $5
	run_gnuplot $1 csblosum $2csblosum $4 B $5
    fi
}

# run conversion for each dir:
score_dir ref1/test1 sc11 l M
score_dir ref1/test2 sc12 l M
score_dir ref1/test3 sc13 l M
score_dir ref2/test sc2 l M
score_dir ref3/test sc3 l 0
score_dir ref4/test sc4 l M
score_dir ref5/test sc5 l M
score_dir ref6/test sc6 s 0
score_dir ref6/test_1a sc61a s 0 
score_dir ref6/test_1b sc61b s 0
score_dir ref6/test_2a sc62a s M
score_dir ref6/test_2b sc62b s 0
score_dir ref6/test_2c sc62c s 0
score_dir ref6/test_3 sc63 s 0
score_dir ref6/test_4 sc64 s 0
score_dir ref7/test sc7 s 0
score_dir ref8/test sc8 s 0

# a scaled version for some dirs

score_dir ref2/test scSC2 l M 5000
score_dir ref2/test scSCC2 l M 40000
score_dir ref3/test scSC3 l 0 5000 
score_dir ref3/test scSCC3 l 0 40000
score_dir ref4/test scSC4 l M 5000
score_dir ref4/test scSCC4 l M 40000
score_dir ref5/test scSC5 l M 5000
score_dir ref5/test scSCC5 l M 40000
score_dir ref6/test scSC6 s 0 1000000
score_dir ref6/test_2a scSC62a s M 100000
score_dir ref6/test_2b scSC62b s 0 50000
score_dir ref6/test_2c scSC62c s 0 50000
score_dir ref6/test_3 scSC63 s 0 50000

echo "Done."
