Debian Codegraph @

metastudentPkg/lib/groupB/run/knn_weighted

raw
1 #!/usr/bin/perl
2 use strict;
3 use Carp;
4 use Cwd;
5 use Getopt::Std;
6 use File::Basename;
7 use IPC::Open3;
8 use File::chdir;
9
10 # ensure working directory is right
11 print "-> working directory: ", cwd(), "\n";
12
13 # kNN parameters
14 my $closure = "data/transitiveClosure2014.txt";
15 my $goterms = "data/goterm2014.txt";
16 my $distribution = "data/weighted.dist";
17
18
19 # 5 = global unweighted, 6 = global log, 7 = global log log
20 my $scorecolumn = 6;
21
22
23 # ------------------------------------------------
24
25 sub usage() {
26 print "Usage: -d <blast database> -i <fasta file with targets> -o <output_folder>\n\n";
27 exit(-1);
28 }
29
30 # checks the FASTA target file if we have to perform on-the-fly evaluation and for the number of targets
31 sub prepare_evaluation {
32
33 open(TARGETS, "<$_[0]");
34
35 my $num_targets = 0;
36 my $do_evaluation = 0;
37
38 # read the entire target FASTA file
39 while(my $line=<TARGETS>)
40 {
41 # check for FASTA header lines
42 if(index($line, ">") == 0)
43 {
44 # check first FASTA header if we have to activate evaluation
45 if($num_targets == 0)
46 {
47 # check if first FASTA header contains a comma ->
48 # for safety, we also check if the header matches a GO-term (just to be sure...)
49 if((index($line, ",") >=0) or (index($line, "GO:") >= 0))
50 {
51 $do_evaluation = 1;
52 }
53 }
54
55 $num_targets++;
56 }
57 }
58
59 close(TARGETS);
60
61 return ($do_evaluation, $num_targets);
62
63 }
64
65 # ------------------------------------------------
66
67 # get command line arguments
68 my $opt_string = 't:x:k:e:m:i:d:o:hj:l:';
69 my %opt = ();
70 getopts( "$opt_string", \%opt );
71
72 if(not defined $opt{d} or not defined $opt{i} or not defined $opt{o}) { usage(); }
73 if(not defined $opt{m}) { die "no prediction mode specified!\n";}
74
75 my $knn = "";
76
77 my $k = $opt{k};
78 my $e = $opt{e};
79 my $t = $opt{t}; # h Val
80 my $x = $opt{x}; # number iterations
81 my $jarPath = $opt{l};
82
83
84 my $mode = $opt{m};
85 my $blastdb = $opt{d};
86 my $targets = $opt{i};
87 my $output_folder = $opt{o};
88
89 # specifying -j <blastresults> allows to skip recalculation of blast results
90 my $jumpstart = "";
91 if(defined $opt{j})
92 { $jumpstart = $opt{j};}
93
94 # check which prediction mode we are using and parametrize according to results obtained from optimization
95 if($mode eq "besthit") {
96 print "-> mode: best Blast hit\n";
97 $knn = "-k 1 -nofilter";
98 $scorecolumn = 5;
99 $distribution = "data/besthit.dist";
100 } elsif ($mode eq "unweighted_knn") {
101 print "-> mode: unweighted kNN\n";
102 $scorecolumn = 5;
103 $knn = "-k 10";
104 $distribution = "data/unweighted.dist";
105 } elsif ($mode eq "weighted_knn") {
106 print "-> mode: weighted kNN\n";
107 $scorecolumn = 6;
108 $knn = "-e 1E-02";
109 $distribution = "data/weighted.dist";
110 } else {
111 die "no valid prediction mode specified!\n"
112 }
113
114 print "-> working directory: \n";
115 print " database: ", $blastdb, "\n";
116 print " targets: ", $targets, "\n";
117 print " output folder: ", $output_folder, "\n";
118
119 if((not -e $blastdb) or (not -e $targets) or (not -e $output_folder))
120 {
121 print STDERR "\nERROR: blast database/target file/output folder does not exist\n\n";
122 exit;
123 }
124
125 # also obtain a proper name for the output file
126 my $outfile_name = basename($targets);
127 my $outfile_path = $output_folder . ( substr($output_folder, length($output_folder)-1, 1) eq "/" ? "" : "/") . $outfile_name . "." . $mode;
128
129 # run BLAST or jumpstart
130 if($jumpstart eq "")
131 {
132 print "-> running BLAST...\n";
133 my @cmd = qq|blastpgp -i $targets -d $blastdb -e $e -h $t -j $x > $outfile_path.blastresults|;
134 print `@cmd`; if($?){ confess("@cmd failed: ".($?>>8)); }
135 # convert to tabular input format
136 #`ruby ../ruby/blast_file_parser_concatfiles.rb $outfile_path.blastresults > $outfile_path.tabularresults`;
137 {
138 local $CWD;
139 $CWD[-1] = 'java';
140 my @cmd = qq|java BlastFileParser $jumpstart $outfile_path.tabularresults|;
141 `@cmd`; if($?){ confess("@cmd failed: ".($?>>8)); }
142 }
143 }
144 else
145 {
146 print "-> jumpstarting from BLAST results!\n";
147 #`ruby ../ruby/blast_file_parser_concatfiles.rb $jumpstart > $outfile_path.tabularresults`;
148 {
149 local $CWD;
150 $CWD[-1] = 'java';
151 #print "@CWD\n";
152 print "java -cp $jarPath/blastfilep.jar BlastFileParser $jumpstart $outfile_path.tabularresults\n";
153 my @cmd = qq|java -cp $jarPath/blastfilep.jar BlastFileParser BlastFileParser $jumpstart $outfile_path.tabularresults|;
154 `@cmd`; if($?){ confess("@cmd failed: ".($?>>8)); }
155 }
156 }
157
158 #die "stopped";
159
160 # run PREDICTION
161 print "-> predicting GO-Terms...\n";
162
163 my $cc_skipped = 0;
164 print "$CWD\n";
165 my $cmdline = "cd ..; java -cp $jarPath/pp2.jar pp2.prediction.knn.KNNPredictor -i $outfile_path.tabularresults -o $outfile_path.raw -k $k -d $distribution -c $closure -t $goterms";
166 my $pid = open3(0, \*OUT, 0, $cmdline) or die("Could not fork: $!");
167
168 while(my $line = <OUT>) {
169 print $line;
170 if(index($line, "no DAG nodes for") >= 0)
171 { $cc_skipped++; }
172 }
173
174 close(OUT);
175
176 waitpid $pid, 0;
177 my $exitcode = $? >> 8;
178 $exitcode == 0 or die "KNNPredictor crashed. Exit code was $exitcode";
179
180
181 #my $predictor_out = `cd ..; java -jar pp2.jar pp2.prediction.knn.KNNPredictor -i $outfile_path.tabularresults -o $outfile_path.raw $knn -d $distribution -c $closure -t $goterms`;
182
183 # count how often targets were skipped due to CC filtering...
184
185
186 #while ($predictor_out =~ /no DAG nodes for/g)
187 #{
188 # $cc_skipped++;
189 #}
190
191 #print $predictor_out;
192 print " skipped $cc_skipped targets because they contained only cellular component annotations.\n";
193
194
195 #EVALUATE
196 my $precision = -1;
197 my $recall = -1;
198
199 (my $do_evaluation, my $number_targets) = &prepare_evaluation($targets);
200
201 $do_evaluation=0;
202
203 if($do_evaluation)
204 {
205 print "-> performing on-the-fly evaluation...\n";
206
207 # check if we want honest evaluation
208 my $honest = "";
209 if(defined $opt{h})
210 {
211 $number_targets -= $cc_skipped;
212 $honest = "-c $number_targets";
213 print " 'honest' evaluation: targets without prediction -> precision and recall set to 0\n";
214 }
215
216 # run evaluation
217 my @cmd = qq(cd ../perl; cat $outfile_path.raw.leaves | perl evaluate.pl -g ../data/goterm2014.txt -t ../data/goterm2term.txt -i -n 1 -p x $honest);
218 my $eval_out = `@cmd`; if($?){ confess("@cmd failed: ".($?>>8)); }
219
220 my @lines = split(/\n/, $eval_out);
221 shift @lines;
222
223 # offset between column (leaves output format) and line index (evaluation output format) is -2
224 my @scores = split(/\t/, $lines[$scorecolumn - 2]);
225 $precision = $scores[1];
226 $recall = $scores[2];
227
228 print "\n precision: $precision, recall: $recall\n";
229
230 }
231
232 print "-> converting to CAFA output...\n";
233 print "$CWD\n";
234 my @cmd = qq|cd ..; java -cp $jarPath/pp2.jar pp2.datahandler.GenerateCAFAOutput -m 1 -i $outfile_path.raw.leaves -o $outfile_path.predicted_leaves -c $scorecolumn -pr $precision -rc $recall|;
235
236 print `@cmd`; if($?){ confess("@cmd failed: ".($?>>8)); }
237 #print `java -classpath '.' pp2.datahandler.GenerateCAFAOutput -m 1 -i $outfile_path.raw.leaves -o $outfile_path.predicted_allnodes -c $scorecolumn -pr $precision -rc $recall -gc $closure -gn $goterms`;
238
239 print "-> output written to $outfile_path.predicted_leaves and\n $outfile_path.predicted_allnodes\n";
240