#!/bin/perl


#########################################
# Author - Huzefa
# Idea   - Written to determine the best training model for a set of
#          prosat models. Goes over the various parameters. Can be time consuming
#
#  
# Parameters - Input file with profile information
#
############################################
#
#
#
#

$INPUTFILE=$ARGV[0];
$BINDIR = "/project/dminers40/huzefa/SERVER/buildModels/programs/rangwala/ProSAT-Advanced/builds/Linux-i686/";
$OPFILE  =  "tmp-log.dat";

$PROSAT_EVAL = "$BINDIR/Prosat_Eval1.0";

$PROSAT_LEARN = "$BINDIR/Prosat_Learn1.0";


my @kernel_pm = ("soe","rbf","linear");
my @kmer_pm   = ("5", "9", "13", "17");

#my $nfold=5;

$nfold=5;
#my @kernel_pm = ("soe");
#my @kmer_pm   = ("5");

$max_roc = -100.0;

$kernel_index =-1;
$kmer_index = -1;
$smer_index = -1;


for ($kernel=0;$kernel<=$#kernel_pm;$kernel++){
   
    for ($k=0;$k<=$#kmer_pm;$k++)
    {
      for($s=$kmer_pm[$k]-3;$s<=$kmer_pm[$k];$s+=3){
            $cmd = "$PROSAT_EVAL -kernel=$kernel_pm[$kernel] -smer=$s  -wmer=$kmer_pm[$k] -usecr -sample=5 $INPUTFILE  | tee $OPFILE";
            print $cmd;
            system($cmd);

            system ("grep ROC $OPFILE > ROC_store.dat");
            open(TMPFILE, "./ROC_store.dat");
            $sum_roc  = 0.0;
            while ($line = <TMPFILE>){
                chomp $line;
                my @rec = split /\s+/,$line;
                $sum_roc += $rec[4];
                print $line;
            }
            close (TMPFILE);
            if ($sum_roc > $max_roc){
                $max_roc = $sum_roc;
                $kernel_index = $kernel;
                $kmer_index   = $k;
                $smer_index   = $s;

            }
            }
   }

}

printf ("Best Model To be trained");
$cmd = "$PROSAT_LEARN -kernel=$kernel_pm[$kernel_index] -smer=$smer_index  -wmer=$kmer_pm[$kmer_index] -usecr  $INPUTFILE  best-model-ker=$kmer_pm[$kernel_index]-w=$kmer_pm[$kmer_index]-s=$smer_index.model | tee $OPFILE";

system("$cmd");
