| 1 |
|
| 2 |
|
| 3 |
# usage: ./perl GOeval.pl <GOterm_file> <GOterm2term_file> <inputFile> <outputFile> |
| 4 |
|
| 5 |
|
| 6 |
use strict; |
| 7 |
|
| 8 |
# load GOfunctions |
| 9 |
require GOfunctions; |
| 10 |
GOfunctions::import($ARGV[0],$ARGV[1]); |
| 11 |
|
| 12 |
|
| 13 |
open(INFILE, "< $ARGV[2]") or die "could not open input file!\n"; |
| 14 |
open(OUTFILE, "> $ARGV[3]") or die "could not open output file!\n"; |
| 15 |
|
| 16 |
# -- parameter -- |
| 17 |
my $GOseperator = ','; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
# -- comparer -- |
| 22 |
|
| 23 |
sub compare { |
| 24 |
my ($target_ref, $prediction_ref) = @_; |
| 25 |
my @GOtarget = @{$target_ref}; |
| 26 |
my @GOprediction = @{$prediction_ref}; |
| 27 |
|
| 28 |
@GOtarget = keys %{{ map { $_ => 1 } @GOtarget }}; # remove duplicates, just to be sure |
| 29 |
@GOprediction = keys %{{ map { $_ => 1 } @GOprediction }}; |
| 30 |
|
| 31 |
|
| 32 |
my $correct = 0; |
| 33 |
foreach my $target(@GOtarget) { |
| 34 |
|
| 35 |
foreach my $prediction(@GOprediction) { |
| 36 |
|
| 37 |
if($target eq $prediction) { |
| 38 |
$correct++; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
my $precision = 0.0; |
| 44 |
if(scalar(@GOprediction) != 0) { |
| 45 |
$precision = $correct/scalar(@GOprediction); |
| 46 |
} |
| 47 |
my $recall= 0.0; |
| 48 |
if(scalar(@GOtarget) != 0) { |
| 49 |
$recall = $correct/scalar(@GOtarget); |
| 50 |
} |
| 51 |
|
| 52 |
return($precision,$recall,$correct,scalar(@GOtarget),scalar(@GOprediction)); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
my $output = ""; |
| 57 |
my $avrpre = 0; |
| 58 |
my $avrrec = 0; |
| 59 |
my $size = 0; |
| 60 |
|
| 61 |
my $tmppre; my $tmprec; my $tmpcor; my $tmptar; my $tmppred; |
| 62 |
$output .= "#precision\trecall\tcorrect\ttargets\tpredictions\n"; # header vor output |
| 63 |
|
| 64 |
while(my $line=<INFILE>) { |
| 65 |
|
| 66 |
|
| 67 |
if(substr($line,0,1) eq '#') { |
| 68 |
#$output .= $line; |
| 69 |
} else { |
| 70 |
|
| 71 |
|
| 72 |
# file "parser" |
| 73 |
# format: GOtargets \t GOprediction \t evalue \t bitscore |
| 74 |
my @table = split(/\t/,$line,3); |
| 75 |
my @GOtarget = split($GOseperator,$table[0]); |
| 76 |
my @GOprediction = split($GOseperator,$table[1]); |
| 77 |
|
| 78 |
|
| 79 |
@GOtarget = GOfunctions->goAddPath(GOfunctions->goFilter(@GOtarget)); # remove cellular_component and add the path GOs |
| 80 |
@GOprediction = GOfunctions->goAddPath(GOfunctions->goFilter(@GOprediction)); |
| 81 |
|
| 82 |
|
| 83 |
# eval |
| 84 |
($tmppre, $tmprec, $tmpcor, $tmptar, $tmppred) = compare(\@GOtarget,\@GOprediction); |
| 85 |
|
| 86 |
if($tmptar > 0) { |
| 87 |
$output .= join("\t",$tmppre, $tmprec, $tmpcor, $tmptar, $tmppred)."\n"; |
| 88 |
|
| 89 |
$avrpre += $tmppre; |
| 90 |
$avrrec += $tmprec; |
| 91 |
$size++; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
$output = "#precision:\t".$avrpre/$size."\trecall:\t". $avrrec/$size."\tsize:\t".$size ."\n".$output; |
| 97 |
|
| 98 |
print OUTFILE $output; |
| 99 |
|
| 100 |
#print $output; |
| 101 |
|
| 102 |
|
| 103 |
close(INFILE); |
| 104 |
close(OUTFILE); |