Debian Codegraph @

metastudentPkg/lib/groupB/perl/cafa_format_checker_2.pl

raw
1
2 use strict;
3
4 #check arguments, if none, print error
5 ##################################
6 ##################################
7
8 if ($#ARGV ==-1){
9 print "\nERROR\n\n";
10 print "Please provide the name of a file to check\n";
11 print "\nERROR\n\n";
12 exit;
13 }
14
15
16 #if more than one argument print error
17 ##################################
18 ##################################
19
20 if ($#ARGV != 0){
21 print "\nERROR\n\n";
22 print "Please provide only one argument\n";
23 print "\nERROR\n\n";
24 exit;
25 }
26
27
28 #read file
29 ##################################
30 ##################################
31
32 my $file = shift;
33
34 open(FILE, "$file") or die "\nERROR\n\ncouldn't open file $file\n\nERROR\n\n";
35 my @data = <FILE>;
36 close FILE;
37
38 chomp(@data);
39
40
41 #make hash of keywords
42 ##################################
43 ##################################
44
45
46 my @keytemp = ("sequence alignments", "profile-profile alignments", "sequence-profile alignments", "phylogeny", "phylogenomics", "derived/predicted", "sequence properties", "protein interactions", "gene expression", "mass spectrometry", "genetic interactions", "protein structure", "literature", "genomic context", "structure alignment", "comparative model", "predicted protein structure", "de novo prediction", "machine learning based method", "genome environment", "operon", "ortholog", "paralog", "protein interaction", "other functional information");
47 my %valid_keywords;
48 map $valid_keywords{$_} = 1, @keytemp;
49
50 #print map "$_\n", keys(%keywords);
51
52 my $ERROR_NUMBER = 1;
53
54 #first check first line
55 ##################################
56 ##################################
57 if ($data[0] !~ m/^AUTHOR\s{1}/){
58 print "\nERROR $ERROR_NUMBER\n\n";
59 $ERROR_NUMBER++;
60 print "First line should contain AUTHOR field\n\n";
61
62 print "this is what should be your AUTHOR line\n$data[0]\n\n";
63 print "\nEND ERROR\n\n";
64 exit;
65 }
66
67
68 #check to make sure AUTHOR is specified only once
69 ##################################
70 ##################################
71 my @author_lines = grep $data[$_] =~ /^AUTHOR\s{1}/, 0..$#data;
72
73 if ($#author_lines > 0){
74 print "\nERROR $ERROR_NUMBER\n\n";
75 $ERROR_NUMBER++;
76
77 print "It seems more than one AUTHOR was specified.\n";
78
79 print "Only one AUTHOR line per file is permitted.\n\n";
80
81 print "\nEND ERROR\n\n";
82 exit;
83
84
85 }
86
87
88
89 #next check last line
90 ##################################
91 ##################################
92 if ($data[$#data] !~ m/^END$/){
93
94 print "\nERROR $ERROR_NUMBER\n\n";
95 $ERROR_NUMBER++;
96
97 print "File should end with END\n\n";
98
99 print "this is your last line:\n$data[$#data]\n\n";
100 print "\nEND ERROR\n\n";
101 exit;
102 }
103
104
105
106 #check to make sure END is specified only once
107 ##################################
108 ##################################
109 my @end_lines = grep $data[$_] =~ /^END$/, 0..$#data;
110
111 if ($#end_lines > 0){
112 print "\nERROR $ERROR_NUMBER\n\n";
113 $ERROR_NUMBER++;
114
115 print "It seems more than one END was specified.\n";
116
117 print "Only one END line per file is permitted.\n\n";
118
119 print "\nEND ERROR\n\n";
120 exit;
121
122
123 }
124
125
126
127
128 #lets determine the author name
129 ##################################
130 ##################################
131
132 my $author_line = $data[0];
133
134 my @author_fields = split /[\s|\t]/, $author_line;
135
136 #print map "Hello $_\n", @author_fields;
137 shift(@author_fields);
138
139
140 my $author_name = join ' ', @author_fields;
141 chomp($author_name);
142
143 if (length($author_name) == 0){
144 print "\nERROR $ERROR_NUMBER\n\n";
145 $ERROR_NUMBER++;
146
147 print "It seems no Team Name was provided.\n";
148
149 print "First line should contain AUTHOR field.\n\n";
150
151 print "this is what should be your AUTHOR line.\n$data[0]\n\n";
152 print "\nEND ERROR\n\n";
153 # exit
154 }
155
156
157 #print "Author Name is $author_name\n";
158
159
160
161
162 #lets see how many models there are
163 ##################################
164 ##################################
165
166
167 my @model_lines = grep $data[$_] =~ /^MODEL/, 0..$#data;
168
169
170 #print map "$_\n", @model_lines;
171
172 if ($#model_lines == -1){
173 print "\nERROR $ERROR_NUMBER\n\n";
174 $ERROR_NUMBER++;
175
176 print "It seems no MODEL line was specified.\n";
177
178 print "Please specify a MODEL.\n\n";
179
180 print "\nEND ERROR\n\n";
181 exit;
182
183
184 }
185
186
187 if ($model_lines[0] != 1){
188 print "\nERROR $ERROR_NUMBER\n\n";
189 $ERROR_NUMBER++;
190
191 print "Invalid File Format.\n";
192
193 print "Please ensure the MODEL line is always the second line in the file.\n\n";
194
195 print "\nEND ERROR\n\n";
196 exit;
197
198
199 }
200
201
202
203 if ($#model_lines > 0){
204 print "\nERROR $ERROR_NUMBER\n\n";
205 $ERROR_NUMBER++;
206
207 print "It seems more than 1 MODEL line was specified.\n";
208
209 print "Please ensure that only one model is represented in each file.\n\n";
210
211 print "\nEND ERROR\n\n";
212 exit;
213
214
215 }
216
217
218 #make sure MODEL lines are of proper format
219 ##################################
220 ##################################
221
222
223
224 my $mln = $model_lines[0];
225 my @model_fields = split /[\s|\t]/, $data[$mln];
226 chomp(@model_fields);
227 my $line_number = $mln+1;
228
229 #check to make sure there is only one number in the model line
230 if ($#model_fields != 1){
231
232 print "\nERROR $ERROR_NUMBER\n\n";
233 $ERROR_NUMBER++;
234
235 print "It seems you've specified a model of illegal format.\n";
236
237 print "Models should be labeled 1 through 3.\n\n";
238
239 print "this is your invalid MODEL line, from line number $line_number.\n$data[$mln]\n\n";
240 print "\nEND ERROR\n\n";
241 # exit
242
243 }
244
245
246 my $MODEL_NUMBER = $model_fields[1];
247
248 #make sure model numbers are valid
249 if ($model_fields[1] > 3 || $model_fields[1] < 1){
250 print "\nERROR $ERROR_NUMBER\n\n";
251 $ERROR_NUMBER++;
252
253 print "Unexpected MODEL number. Model numbers should be 1 through 3.\n";
254
255 print "this is your invalid MODEL line, from line number $line_number.\n$data[$mln]\n\n";
256 print "\nEND ERROR\n\n";
257 #exit
258 }
259
260
261
262
263
264
265
266
267
268 #now lets get into our models
269 ##################################
270 ##################################
271
272 #pop off end line
273 pop(@data);
274 #shift off top two lines
275 shift(@data);
276 shift(@data);
277
278
279 #next line should be the keyword line
280 my @keyword_lines = grep $data[$_] =~ /^KEYWORDS\s{1}/, 0..$#data;
281
282 if ($#keyword_lines > 0){
283 print "\nERROR $ERROR_NUMBER\n\n";
284 $ERROR_NUMBER++;
285
286 print "It your file has more than one KEYWORDS line.\n";
287 print "Please ensure that each model has exactly 1 KEYWORDS line.\n";
288 print "\nEND ERROR\n\n";
289 exit;
290 }
291
292
293 if ($#keyword_lines == -1){
294 print "\nERROR $ERROR_NUMBER\n\n";
295 $ERROR_NUMBER++;
296
297 print "It seems your file has no KEYWORDS line.\n";
298 print "Please ensure that each model has a KEYWORDS line.\n";
299 print "\nEND ERROR\n\n";
300 exit;
301 }
302
303
304 if ($keyword_lines[0] != 0){
305 print "\nERROR $ERROR_NUMBER\n\n";
306 $ERROR_NUMBER++;
307
308 print "It seems your file is of invalid format.\n";
309 print "Please ensure that the third line is the KEYWORDS line.\n";
310 print "\nEND ERROR\n\n";
311 exit;
312 }
313
314
315 my $kwl = shift(@data);
316 $kwl =~ s/KEYWORDS\s{1}//;
317
318 my @keywords = split ',', $kwl;
319 chomp(@keywords);
320
321 #
322
323
324 #why perl doesn't have a function to remove leading white space, like chomp
325 #I'll never know
326 foreach my $j (0..$#keywords){
327 $keywords[$j] =~ s/^\s+//;
328 }
329
330 #my $temp = $keywords[$#keywords];
331 $keywords[$#keywords] =~ s/\.//;
332
333 #print map"keyword START$_ STOP\n", @keywords;
334 #print "\n\n";
335
336 my @nanners = grep !exists($valid_keywords{$_}), @keywords;
337
338
339 if ($#nanners != -1){
340 print "\nERROR $ERROR_NUMBER\n\n";
341 $ERROR_NUMBER++;
342
343 print "It seems you have one or more invalid keywords.\n";
344 print "Invalid Keywords: ";
345 print map "$_\t", @nanners;
346 print "\n";
347 print "\nEND ERROR\n\n";
348 # exit
349
350 }
351
352 ################################
353 #lets check for ACCURACY line #
354 ################################
355
356
357
358 if ($data[0] =~ /^ACCURACY\s{1}/){
359
360 my $acl = shift(@data);
361 my $raw_accuracy_line = $acl;
362
363 $acl =~ s/^ACCURACY\s{1}//;
364 my @accuracy_fields = split ';', $acl;
365
366 chomp(@accuracy_fields);
367 foreach my $j (0..$#accuracy_fields){
368 $accuracy_fields[$j] =~ s/^\s+//;
369 }
370
371
372 if ($#accuracy_fields != 1){
373 print "\nERROR $ERROR_NUMBER\n\n";
374 $ERROR_NUMBER++;
375
376 print "Unexpected ACCURACY line format.\n";
377 print "this is your invalid ACCURACY line.\n$raw_accuracy_line\n\n";
378 print "\nERROR\n\n";
379 # exit
380 }
381
382 if ($accuracy_fields[0] !~ /^PR=\d{1}\.{1}\d{2}$/){
383 print "\nERROR $ERROR_NUMBER\n\n";
384 $ERROR_NUMBER++;
385
386 print "Unexpected ACCURACY line format.\n";
387 print "this is your invalid ACCURACY line.\n$raw_accuracy_line\n\n";
388 print "\nEND ERROR\n\n";
389 # exit
390 }
391
392 if ($accuracy_fields[1] !~ /^RC=\d{1}\.{1}\d{2}$/){
393 print "\nERROR $ERROR_NUMBER\n\n";
394 $ERROR_NUMBER++;
395
396 print "Unexpected ACCURACY line format.\n";
397 print "this is your invalid ACCURACY line.\n$raw_accuracy_line\n\n";
398 print "\nEND ERROR\n\n";
399 # exit
400 }
401
402
403
404
405 }
406
407
408
409 my %target_counts;
410 my %term_counts;
411 my @scores;
412 my %term_target;
413 my @excluded;
414
415 ################################
416 # now, all thats left are target, go id, score lines
417 ################################
418
419
420 #space is assumed to be only valid delimiter, was told this was to be changed
421 foreach my $line (@data){
422
423 my @fields = split /\s{1}/, $line;
424
425 if ($#fields != 2){
426 print "\nERROR $ERROR_NUMBER\n\n";
427 $ERROR_NUMBER++;
428
429 print "It seems you have an invalid prediction line.\n";
430
431 print "this is your invalid prediction line.\n$line\n\n";
432
433 print "\nEND ERROR\n\n";
434 # exit
435
436 }
437
438 # if ($fields[0] !~ /^T\d{5}$/){
439 # print "\nERROR $ERROR_NUMBER\n\n";
440 # $ERROR_NUMBER++;
441 #
442 # print "It seems you have an invalid prediction line, invalid target id.\n";
443 #
444 # print "this is your invalid prediction line.\n$line\n\n";
445 #
446 # print "\nEND ERROR\n\n";
447 ## exit
448 #
449 #
450 # }
451 else{
452 if ($fields[2] ne "0"){
453 $target_counts{$fields[0]}++;
454 }
455 }
456
457
458 if ($fields[1] !~ /^GO\:\d{7}$/){
459 print "\nERROR $ERROR_NUMBER\n\n";
460 $ERROR_NUMBER++;
461
462 print "It seems you have an invalid prediction line, invalid GO id.\n";
463
464 print "this is your invalid prediction line.\n$line\n\n";
465
466 print "\nEND ERROR\n\n";
467 # exit
468
469
470 }
471 else{
472 if ($fields[2] ne "0"){
473 $term_counts{$fields[1]}++;
474 }
475
476
477 }
478
479 #this part changed to allow 0's
480 if ($fields[2] > 1 || $fields[2] < 0 ){
481 print "\nERROR $ERROR_NUMBER\n\n";
482 $ERROR_NUMBER++;
483
484 print "It seems you have an invalid prediction line, invalid score.\n";
485
486 print "this is your invalid prediction line.\n$line\n\n";
487
488 print "\nEND ERROR\n\n";
489 # exit
490
491
492 }elsif ($fields[2] !~ /^\d?\.{1}\d{2}$/){
493
494 #allow the new zero score
495 if ($fields[2] ne "0"){
496 print "Entry: ".$fields[2]."\n";
497 print "\nERROR $ERROR_NUMBER\n\n";
498 $ERROR_NUMBER++;
499
500 print "It seems you have an invalid prediction line, invalid score.\n";
501
502 print "this is your invalid prediction line.\n$line\n\n";
503
504 print "\nEND ERROR\n\n";
505 }else{
506
507 push @excluded, $fields[0];
508 }
509
510
511 }else{
512
513
514 push @scores, $fields[2];
515
516 }
517
518 if ( !exists( $term_target{$fields[0]}{$fields[1]} ) ){
519 $term_target{$fields[0]}{$fields[1]} = 1;
520 }
521 else{
522 print "\nERROR $ERROR_NUMBER\n\n";
523 $ERROR_NUMBER++;
524
525 print "It seems you have an invalid prediction line: Duplicate Predictions.\n";
526 print "Please only submit at most one score for one term/target pair\n";
527 print "this is your invalid prediction line.\n$line\n\n";
528
529 print "\nEND ERROR\n\n";
530
531
532 }
533
534
535
536 }
537
538
539
540 my @too_many = grep {$target_counts{$_} > 1000} keys %target_counts;
541
542
543 if ($#too_many > -1){
544 print "\nERROR $ERROR_NUMBER\n\n";
545 $ERROR_NUMBER++;
546
547 print "It seems some targets have too many predictions.\n";
548
549 print "these are the targets:\n";
550 print map "\t$_\n", @too_many;
551 print "\n\n";
552 print "\nEND ERROR\n\n";
553 # exit
554
555
556 }
557
558
559 if ($ERROR_NUMBER == 1){
560
561 my @targets = keys(%target_counts);
562 my $num_targets = $#targets + 1;
563 my @terms = keys(%term_counts);
564 my $num_terms = $#terms + 1;
565 my $num_excluded = $#excluded + 1;
566 my @counts = values(%target_counts);
567 @counts = sort @counts;
568
569 my $sum;
570
571 map{$sum += $_} @counts;
572 my $avg = $sum / ($#counts +1);
573 $avg = sprintf("%.2f", $avg);
574 print "\n\n\t\tCONGRATULATIONS! No Errors Found\n\n";
575 print"\t\tTeam Name: $author_name,\tModel Number: $MODEL_NUMBER\n";
576 print "\t\tNumber of Targets with Predictions: $num_targets\n";
577 print "\t\tNumber of GO Terms utilized: $num_terms\n";
578 print "\t\tMaximum Number of Terms per Target: $counts[-1]\n";
579 print "\t\tMinimum Number of Terms per Target: $counts[0]\n";
580 print "\t\tAverage Number of Terms per Target: $avg\n";
581 print "\t\tTotal Number of Excluded Annotations: $num_excluded\n";
582 print "\n\n\t\tGood luck! The AFP team.\n\n";
583 }
584 else{
585 print "\nFor the rules regarding valid file formats please refer to:\n\n";
586
587 print "http://biofunctionprediction.org/node/262\n\n";
588
589
590 }
591
592