#!/usr/local/apps/perl5.004/bin/perl
##!D:\Perl5\bin\perl.exe # for use on PC systems

##################################################################
#
#  ROC.pl - a script that uses a genetic sequence database file
#       containing a list of positives and negatives and uses
#	the results of the search algorithm on a query to
#	determine percentages of True Positives and True
#	Negatives.
#	
#
#  Author:  David Martinez
#       San Diego Supercomputer Center
#            (damartin@sdsc.edu)        August 1997
#
# infile1 contains:
# original database file name
# Number of true positives
# Number of true negatives
#
# infile2 contains:
# search query results
#
##################################################################

########################## GLOBALS ###############################

# name of the output file
## $outfilename = "query.out";

# name of the input files
#--------------------------------------------------------
# Database Match files
#--------------------------------------------------------
# $infile1 = "match/gbrna_demo.mat";
 $infile1 = "/misc/www/users/damartin/PWT/match/gbrna_Xlaevis5srna.mat";
# $infile1 = "match/gbrna_RNaseP.mat";
print "Using $infile1 matchfile\n";

#--------------------------------------------------------
# Search results output files
#--------------------------------------------------------

## $infile2 = "output/trna.out";
## $infile2 = "output/trna2.out";
## $infile2 = "output/trna3.out";
## $infile2 = "output/trna-best.out";

# $infile2 = "output/demo.out";
# $infile2 = "output/Xlaevis5S10.out";
# $infile2 = "output/Xlaevis5S11.out";
# $infile2 = "output/Xlaevis5S12.out";
# $infile2 = "/misc/www/users/damartin/cgi-bin/temp/Xlaevis5S13.out";
# $infile2 = "output/Xlaevis5S14.out";
# $infile2 = "output/RNaseP1.out";

## $infile2 = "/misc/www/users/damartin/cgi-bin/temp/temp.out";

 $infile2 = $ARGV[0];
print "Using $infile2 search output file\n";

@temp1= split('\/',$infile2);
@temp1= split('\.',$temp1[1]);
# name of the output file
$outfilename = "/misc/www/users/damartin/cgi-bin/temp/temp.sta";
# print "Creating $outfilename output file\n"; 

# for error checking
if ( !(-e $infile1) ) {
   print "sequence database file $infile1 doesn't exist!\n";
   print "Bailing out..\n";
   exit(1);
}
print "using sequence database file: $infile1\n";
open(INFILE1, "<$infile1") || die "Unable to open filename \"$!\".\n";

# for error checking
if ( !(-e $infile2) ) {
   print "sequence database file $infile2 doesn't exist!\n";
   print "Bailing out..\n";
   exit(1);
}
print "using sequence database file: $infile2\n";
open(INFILE2, "<$infile2") || die "Unable to open filename \"$!\".\n";


$Tp=0;
$Tf=0;
$Fp=0;
$pos="1";
$posval="1";
$neg="0";
$negval=0;

$numlocus=0;
$max=10;
$n=0;
$m=0;
$j=0;

########################### MAIN #################################

#-------------------------------------------------------------
# read input file
#-------------------------------------------------------------

while ($line = <INFILE1>) {
 @fields = split(' ',$line);
   $key = $fields[1];
   $result = $fields[3];
   $correctval{$key}=$result;

   if ($fields[0] eq "LOCUS") { $numlocus= $fields[1]; }
   if ($fields[1] eq "POSITIVES") { $n = $fields[2]; print "n=$n\n"; }
   if ($fields[1] eq "NEGATIVES") { $m = $fields[2]; print "m=$m\n"; }

}

#-------------------------------------------------------------
# start creating output file
#-------------------------------------------------------------
## if ( !open(,"> $outfilename") ) {
##   print "Error opening an output file $outfilename: $!\n";
##   print "Bailing out..\n";
##   exit(1);
## }

print  "There are $numlocus LOCUS values within $infile1<BR>\n";
print  "There are $n sequences with $tar_str0, $tar_str1<BR>\n";
print  "There are $n True positives, $m True negatives in $infile1<BR>\n";


## Parse Results Output file
##

$j=0;
$numlocus=0;


# print "\n\nBeginning of results output:\n";
# print "Printing out results of False pos/negs\n\n";
## check results file against database
while ($line = <INFILE2>) {
    $line =~ m/^\s*(\S+)/s;
    if ($1 eq "LOCUS") { # begin if ($1 eq "LOCUS")
    $numlocus++;
        ##  We're at the beginning of a sequence
        ##  Parse sequence name
	@fields = split(' ',$line);
	$keyR = $fields[1];
    } # end if ($1 eq "LOCUS")

    elsif ($1 eq "Seqence:") { # Note mispelling
        ##  Parse sequence keywords
	@fields = split(' ',$line);

	if ($fields[4] eq $pos) { 
	   $result=$posval; # a positive result
	   $posR++;
	} 

	elsif ($fields[4] eq $neg) { 
	   $result=$negval; # a negative result
	   $negR++;
	} 

	#  compare $key values
	if ($correctval{$keyR}==$result) {
	    if ($result==$posval) { $Tp++; }
	    else { $Tn++; }
	} # end if

	# results do not match, false pos/neg 
	elsif ($correctval{$keyR}!=$result) { 

##	    if ($result==$posval) 
##		 { print "pos key is $keyR, result = $result\n"; $Fp++; }
##	    else { print "neg key is $keyR, result = $result\n"; $Fn++; }

	    if ($result==$posval) { $Fp++; }
	    else { $Fn++; }
	}
    } # end elsif
  } # end while

print  "\nResults of Query for file: $infile2<BR>\n";
print  "There are $numlocus LOCUS values\n";
print  "Number of positives found in $infile2 is $posR\n";
print  "Number of negatives found in $infile2 is $negR\n\n";

print  "number of True Positives: $Tp\n";
print  "number of True Negative: $Tn\n";
print  "number of False Positives: $Fp\n";
print  "number of False Negatives: $Fn\n";

## $n is the number of true positives found in the
## sequence database.

$temp = 100*$Tp/$n;
print  "Percentage of True Positives: $temp\n";
$temp = 100*$Fp/$n;
print  "Percentage of False Positives: $temp\n";

## $m is the number of true negatives found in the
## sequence database.

$temp = 100*$Tn/$m;
print  "Percentage of True Negatives: $temp\n";
$temp = 100*$Fn/$m;
print  "Percentage of False Negatives: $temp\n";

close ;

## print "DONE\n";

