-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget-gafs.pl
executable file
·1385 lines (1201 loc) · 39.9 KB
/
get-gafs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
use Storable;
use DateTime::Format::Strptime;
## CONFIG
my $go_path = '';
my $base_path = '/Users/gwg/';
my $xrf_abbs = $go_path . 'go/doc/GO.xrf_abbs';
my $checks_script = $go_path . 'go/software/utilities/filter-gene-association.pl';
my $path = $go_path.'go/gene-associations/';
my $svn_repo = 'piwi/go/gene-associations/';
my $user = 'aji';
my $submissions;
if ($path =~ /submission/)
{ $submissions++;
}
##
my ($day, $month, $year) = (localtime)[3, 4, 5];
my $today = sprintf("%04d%02d%02d", $year + 1900, $month + 1, $day);
my $saved;
my $verbose = 1;
## get the names of the dbs in this dir
my $db_list = get_dbs_from_dir();
my $prep;
#get_date_rev_info_cvsweb( logfile => 'tair.log.txt', outfile => 'go/gaf-versions/tair/tair_metadata.txt', db => 'tair', save_path => 'go/gaf-versions/tair/' );
#my @todo = qw( PAMGO_Mgrisea PAMGO_Oomycetes PAMGO_Ddadantii PAMGO_Atumefaciens); #jcvi mgi pombase pseudocap reactome rgd sgd sgn tair rgd wb zfin );
my @todo = qw(zfin);
my $all_mdata_file = 'go/gaf-versions/all-metadata.txt';
my $metadata = parse_all_metadata(metadata_file => $all_mdata_file );
commit_monthly_release( metadata => $metadata );
if ($prep)
{
## prepping files
foreach my $db (@todo)
# foreach my $db (@$db_list)
{ ## make sure we have an appropriate directory to save the files
my $save_path = 'go/gaf-versions/' . $db . '/';
if (! -e $save_path)
{ mkdir($save_path);
}
## create a derived directory
my $derived = $save_path . 'derived/';
if (! -e $derived)
{ mkdir($derived);
}
# get_date_rev_info( db => $db, save_path => $save_path ); #, f_name => $f_name );
# get_files_from_cvs( db => $db, save_path => $save_path );
process_directory_files( db => $db, save_path => $save_path, metadata_file => $all_mdata_file );
print STDERR "Finished processing $db\n" if $verbose;
}
}
exit(0);
foreach my $y (2004..2011)
{ foreach my $m qw(01 02 03 04 05 06 07 08 09 10 11 12)
{ print STDERR "Looking at $y$m...\n";
commit_monthly_release( save_path => 'go/gaf-versions/', metadata => $metadata, date => $y.$m );
}
}
my $date_h;
foreach my $db (@$db_list)
{ my @dates = sort keys %{$metadata->{rev_date}{$db}{by_date}};
$metadata->{first}{ $dates[0] }{$db}++;
$metadata->{last}{ $dates[-1]}{$db}++;
foreach (@dates)
{ $date_h->{$_}++;
}
}
## processing existing files
foreach my $db (@$db_list)
{ ## get all file names
my $save_path = 'go/gaf-versions/' . $db . '/';
my $derived = $save_path . "derived/";
## metadata
# $metadata->{rev_date}{$db} = parse_metadata( db => $db, save_path => $save_path );
## get the list of quarterly files for this db
get_quarterly_files( metadata => $metadata, db => $db, derived => $derived );
}
## see which file sets start when.
foreach my $d (sort keys %{$metadata->{first}})
{ print STDERR "$d\n";
foreach (sort keys %{$metadata->{first}{$d}})
{ print STDERR "$_\t" . $metadata->{rev_date}{$_}{by_date}{$d} . "\n";
}
}
print STDERR "\n\n";
my $dbs;
my $stats;
## what's our start date for each project?
foreach my $d (sort keys %{$metadata->{by_date}})
{ if ($metadata->{first}{$d})
{ foreach (keys %{$metadata->{first}{$d}})
{ $dbs->{$_}++;
}
}
if ($metadata->{last}{$d})
{ foreach (keys %{$metadata->{last}{$d}})
{ delete $dbs->{$_};
}
}
my @files = sort values %{$metadata->{by_date}{$d}};
print STDERR "files for $d:\n" . join("\n", @files) . "\n"; #sort values %{$metadata->{by_date}{$d}}) . "\n";
foreach (sort keys %$dbs)
{ if (! $metadata->{by_date}{$d}{$_})
{ print STDERR "MISSING $_\n";
}
}
# print STDERR "\n\n";
$stats = get_stats(metadata => $metadata, date => $d, files => [ @files ], stats => $stats );
print STDERR "Finished processing files for $d\n";
}
## look at the
exit(0);
sub get_obo_files {
foreach my $y (2007..2011)
{ foreach my $m qw(01 02 03 04 05 06 07 08 09 10 11 12)
{ ## cmd
my $cmd = "cvs -q -d :ext:aji\@ext.geneontology.org:/share/go/cvs update -p -D$y$m"."01 go/ontology/gene_ontology_edit.obo > go/ontology-archive/go-$y$m.obo";
`$cmd`;
}
}
}
sub get_dbs_from_dir {
opendir(DIR, $path) or die "can't opendir $path: $!";
my $dbs;
while (defined(my $file = readdir(DIR)))
{ ## check the file name for the db #
if ($file =~ /gene_association\.(.+)\.gz/)
{ push @$dbs, $1;
}
}
closedir(DIR);
return $dbs;
}
sub get_date_rev_info {
my %args = (@_);
my $f_name = 'gene_association.' .$args{db}. '.gz';
## parse the revision/date list
## use cvs log command to find the file history
## log format:
## ^date: (\d{4})-(\d\d)-(\d\d) \d\d:\d\d:\d\d.*?author: .*?; state: Exp; lines: .*?;
## get the log message
my $cmd = "cvs -q -d :ext:aji\@ext.geneontology.org:/share/go/cvs log $path$f_name";
my $text = `$cmd`;
my @date_arr;
# print STDERR "text: $text\n" if $verbose;
my $temp;
while ($text =~ /----------------------------.*?revision (\d.\d+).*?date: (\d{4})-(\d\d)-(\d\d) \d\d:\d\d:\d\d /sg)
{ ## save the revision and date
$temp->{by_date}{ $2.$3 }{ $1 }++;
$temp->{by_rev}{ $1 }{ $2.$3 }++;
}
foreach my $d (keys %{$temp->{by_date}})
{ ## find the revision with the earliest date
## split up the revision into major and minor parts
my @sorted =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
# map { [$_, foo($_)] }
map {
my ($maj, $min) = split(/\./, $_, 2);
[ $_, $maj."\0".$min ];
} keys %{$temp->{by_date}{$d}};
# print STDERR "date: $d; sorted: " . join(", ", @sorted) . "\n\n";
## so for date $d, we want to get $sorted[0]
$saved->{$args{db}}{by_date}{$d} = $sorted[0];
$saved->{$args{db}}{by_rev}{ $sorted[0] }{$d}++;
}
# print STDERR "saved by date: " . join("\n", map { "$_: " . join(", ", @{$saved->{by_date}{$_}} ) } sort keys %{$saved->{by_date}}) . "\n\n";
## save this info so we don't have to retrieve it again
open(FH, "> $args{save_path}" . "metadata.txt") or die "Could not create file " . $args{save_path} . "metadata for writing: $!";
print FH "Saved: by date:\n" . join("\n", map { "$_: " . $saved->{$args{db}}{by_date}{$_} } sort keys %{$saved->{$args{db}}{by_date}}) . "\n\n";
print FH "\n\nAll revisions:\n";
foreach my $k (sort keys %{$saved->{$args{db}}{by_rev}})
{ print FH "$k:\n" . join(", ", sort keys %{$saved->{$args{db}}{by_rev}{$k}}) . "\n";
}
close(FH);
}
## work out the metadata from a log copied from a cvsweb page
## note that we put in a line so that the format is
## ----------------------------
## Revision 1.1239: download
## Wed Feb 8 03:16:40 2009 UTC (2 years, 11 months ago) by gocvs
## Branches: MAIN
sub get_date_rev_info_cvsweb {
my %args = (@_);
## parse the revision/date list
## use cvs log command to find the file history
my $temp;
my $parser = DateTime::Format::Strptime->new( pattern => '%b %d %H:%M:%S %Y' );
## log format:
## Revision 1.1239: download
## Wed Feb 8 03:16:40 2009 UTC (2 years, 11 months ago) by gocvs
## get the log message
my $logfile = $args{logfile};
## open it up, let the separator be the line of hyphens
open(LOG, "< $args{logfile}") or die "Could not open $args{logfile}: $!";
{ local $/ = "----------------------------";
while (<LOG>)
{ next unless /\w/;
if (/Revision (\d\.\d+): download\s+\w+ ((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d\d:\d\d:\d\d (19|20)\d\d UTC)/)
{ my $rev = $1;
my $date = $2;
## parse the date
my $dt = $parser->parse_datetime( $date );
## convert into YYYYMMDD
$date = $dt->strftime( "%Y%m" );
## save the revision and date
$temp->{by_date}{ $date }{ $rev }++;
$temp->{by_rev}{ $rev }{ $date }++;
}
elsif (/Revision (\d\.\d+)/)
{ warn "Could not parse revision/date: check $_";
}
}
}
foreach my $d (keys %{$temp->{by_date}})
{ ## find the revision with the earliest date
## split up the revision into major and minor parts
my @sorted =
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
# map { [$_, foo($_)] }
map {
my ($maj, $min) = split(/\./, $_, 2);
[ $_, $maj."\0".$min ];
} keys %{$temp->{by_date}{$d}};
# print STDERR "date: $d; sorted: " . join(", ", @sorted) . "\n\n";
## so for date $d, we want to get $sorted[0]
$saved->{$args{db}}{by_date}{$d} = $sorted[0];
$saved->{$args{db}}{by_rev}{ $sorted[0] }{$d}++;
}
## save this info so we don't have to retrieve it again
open(FH, "> " . $args{outfile}) or die "Could not create file " . $args{outfile} . " for writing: $!";
print FH "Saved: by date:\n" . join("\n", map { "$_: " . $saved->{$args{db}}{by_date}{$_} } sort keys %{$saved->{$args{db}}{by_date}}) . "\n\n";
print FH "\n\nAll revisions:\n";
foreach my $k (sort keys %{$saved->{$args{db}}{by_rev}})
{ print FH "$k:\n" . join(", ", sort keys %{$saved->{$args{db}}{by_rev}{$k}}) . "\n";
}
close(FH);
foreach my $d (keys %{$saved->{$args{db}}{by_date}})
{ ## location for saved file
## date: $d; revision: $saved->{$args{db}}{by_date}{$d}
my $save_file = $args{db}."-r-".$saved->{$args{db}}{by_date}{$d}."-d-".$d.".gaf.gz";
## let's check whether we have this file already!
if (! -e $args{save_path}.$save_file)
{
my $cmd = "cvs -q -d :ext:aji\@ext.geneontology.org:/share/go/cvs update -p -r " . $saved->{$args{db}}{by_date}{$d} . " go/gene-associations/gene_association.tair.Mar2004-Feb2009.gz > $args{save_path}$save_file";
print STDERR "Running $cmd\n" if $verbose;
my $status = `$cmd`;
## check the status?
if ($status =~ /is no longer in the repository/)
{ warn "ERROR: performing update to " . $saved->{$args{db}}{by_date}{$d} . "; error:\n$status";
}
}
}
}
sub get_files_from_cvs {
my %args = (@_);
## get the revisions we're looking for
my $status;
my $f_name = 'gene_association.' .$args{db}. '.gz';
if (! $saved || ! $saved->{$args{db}})
{ $saved->{$args{db}} = parse_metadata(%args);
}
foreach my $d (keys %{$saved->{$args{db}}{by_date}})
{ ## location for saved file
## date: $d; revision: $saved->{$args{db}}{by_date}{$d}
my $save_file = $args{db}."-r-".$saved->{$args{db}}{by_date}{$d}."-d-".$d.".gaf.gz";
## let's check whether we have this file already!
if (! -e $args{save_path}.$save_file)
{
my $cmd = "cvs -q -d :ext:aji\@ext.geneontology.org:/share/go/cvs update -p -r " . $saved->{$args{db}}{by_date}{$d} . " $path$f_name > $args{save_path}$save_file";
print STDERR "Running $cmd\n" if $verbose;
$status = `$cmd`;
## check the status?
if ($status =~ /is no longer in the repository/)
{ warn "ERROR: performing update to " . $saved->{$args{db}}{by_date}{$d} . " of $f_name; error:\n$status";
}
}
}
}
sub process_directory_files {
my %args = (@_);
## now let's go through the files and tidy things up
## get the contents of $args{save_path} and iterate through them
opendir(DIR, $args{save_path}) or die "can't opendir $args{save_path}: $!";
my @to_check;
while (defined(my $file = readdir(DIR)))
{ # do something with "$dirname/$file"
## check the file name for the revision #
if ($file =~ /(.*?)-r-(\d\.\d+)-d-(\d{6}).gaf(.gz)?/)
{ push @to_check, [ $file, $3 ];
# $rev = $1;
}
else
{ #print STDERR "No revision found for $file!\n";
next;
}
}
closedir(DIR);
## make sure that we have the metadata (if reqd)
## find the latest date for this file, remove annots after that date.
if (! $saved || ! $saved->{$args{db}})
{ $saved->{$args{db}} = parse_all_metadata( %args );
}
my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } @to_check;
my $status;
my @done;
foreach my $file (@sorted)
{ my $rev;
my $date;
my $year;
my $month;
my $date_8;
if ($file =~ /(.*?)-r-(\d\.\d+)-d-(\d{4})(\d{2})/)
{ $rev = $2;
$year = $3;
$month = $4;
}
$date = $year . $month;
my $previous_quarter;
if ($month eq '01')
{ $previous_quarter = $year;
$previous_quarter--;
$previous_quarter .= '1000';
}
elsif ($month eq '04')
{ $previous_quarter = $year . '0100';
}
elsif ($month eq '07')
{ $previous_quarter = $year . '0400';
}
elsif ($month eq '10')
{ $previous_quarter = $year . '0700';
}
## add two zeros to the date to convert it into the eight digit format
$date_8 = $date . "00";
## make sure that the file exists and has a non-zero size
if (! -e $args{save_path}.$file || -z $args{save_path}.$file)
{ print STDERR "$args{save_path}$file does not exist or has zero size!\n";
next;
}
## new files to create
my $sorted = $args{save_path}. "derived/" . $args{db} . "-r-". $rev . "-sorted.gaf";
my $filter = $args{save_path}. "derived/" . $args{db} . "-r-". $rev . "-filter.gaf";
my $recent = $args{save_path}. "derived/" . $args{db} . "-r-". $rev . "-recent.gaf";
next if (-e $sorted || -e $filter);
# print STDERR "Looking at $file; revision: $rev; date: $date\n" if $verbose;
my $n_lines;
if ($previous_quarter)
{ $n_lines = remove_dupes_and_filter_by_date( %args, date_8 => $date_8, file => $file, sorted => $sorted, recent => $recent, previous_quarter => $previous_quarter);
}
else
{ $n_lines = remove_dupes_and_filter_by_date( %args, date_8 => $date_8, file => $file, sorted => $sorted);
}
## $sorted should now contain all unique lines
if (! $n_lines || $n_lines == 0)
{ warn "No body lines found in $file!";
next;
}
if ($submissions)
{ ## if we have the appropriate OBO file, run the GAF filter script
if ($date > 200610)
{ my $error = run_checks_script( %args, date => $date, input => $sorted, output => $filter);
## if it ran OK, let's replace $sorted with $filter
if (! $error)
{ my $status = `mv $filter $sorted`;
if ($status)
{ warn "Error: mv $filter $sorted: $status";
}
}
}
}
}
=cut
## open the annotation file, remove the header lines
open(F, "gzip -dc $args{save_path}$file |") || die "cannot open $args{save_path}$file: $!";
open(HEAD, "> $f_head") or die "Could not open $f_head for writing: $!";
while (<F>)
{ next unless /\w/;
if (/^!/)
{ print HEAD $_;
}
else
{ ## see if we have an annotation line
if (/(.*?\t){10,}/)
{ last;
}
}
}
close(F);
close(HEAD);
## sort the file lines, removing dupes, decompressing if rqd
$status = `gzip -dc $args{save_path}$file | sort -u -o $sorted`;
if ($status)
{ warn "running sort -u; status: $status";
}
## open the sorted file, remove the header lines and extract the annotation data
open(F, "< $sorted") || die "cannot open $sorted: $!";
open(EDIT, "> $f_edit") or die "Could not open $f_edit for writing: $!";
my $count;
## now process this file.
while (<F>)
{ next unless /\w/;
next if /^!/;
## extract the stuff we want
chomp;
my @cols = split /\t/, $_;
my $n_cols = scalar @cols;
while ($n_cols < 17)
{ push @cols, "";
$n_cols++;
}
if ($date_8)
{ ## find the date, check if we're OK or not
next if ! $cols[13]; ## no date specified
next if $cols[13] > $date_8; ## later than our cutoff date
}
## body material!
print BODY $_ . "\n";
$count->{body}++;
print EDIT join("\t", map { "$_" || "" } ($cols[0], $cols[1], "", @cols[3..7],
("", "", "", ""),
@cols[12..$#cols]) ). "\n";
}
close EDIT;
close BODY;
if (! $count->{body} || $count->{body} < 0)
{ print STDERR "No lines found in body of $file!\n";
next;
}
## sort and remove dupes in EDIT
my $cmd = "sort -u -o $e_sort $f_edit";
$status = `$cmd`;
if ($status)
{ warn "running sort -u on edited file: $status";
}
open(FILE, "< $e_sort") or die "can't open $e_sort: $!";
$count->{edit} += tr/\n/\n/ while sysread(FILE, $_, 2 ** 16);
close FILE;
# push @done, $edited_sort;
if ($count->{body} != $count->{edit})
{ warn "$args{db} r $rev diffs: lost " . ($count->{body} - $count->{edit}) . " lines";
}
# gaf_stats($edited_sort);
## put the head and the body of the file back together, save with the new date
$cmd = `cat $f_head $f_body > $f_new`;
$status = `$cmd`;
if ($status)
{ warn "concatenating head and body files: $status";
}
## now run the checks script
## only need to do this on the submissions files
if ($error)
{ ## Don't commit!!
}
else
{ ## we're ready to commit.
# svn_commit_file( %args, date => $date_8, rev => $rev, file => $filter );
}
}
=cut
}
=cut
Remove duplicate lines, filter by date
input: file, f_head, sorted, f_body
output: n lines of body
=cut
sub remove_dupes_and_filter_by_date {
my %args = (@_);
## open the annotation file, remove the header lines
open(F, "gzip -dc $args{save_path}$args{file} |") || die "cannot open $args{save_path}$args{file}: $!";
open(OUT, "> $args{sorted}") or die "Could not open $args{sorted} for writing: $!";
while (<F>)
{ next unless /\w/;
if (/^!/)
{ print OUT $_;
}
else
{ ## see if we have an annotation line
if (/(.*?\t){10,}/)
{ last;
}
}
}
close F;
close OUT;
## sort the file lines, removing dupes, decompressing if rqd
## save the data in a temporary file
my $temp = $args{sorted} . ".temp";
my $status = `gzip -dc $args{save_path}$args{file} | sort -u -o $temp`;
if ($status)
{ warn "running sort -u; status: $status";
}
if ($args{previous_quarter})
{ open(Q, "> $args{recent}") or die "Could not open $args{recent} for writing: $!";
}
## open the sorted file, remove the header lines and extract the annotation data
open(S, "< $temp") || die "cannot open $temp: $!";
open(OUT, ">> $args{sorted}") or die "Could not open $args{sorted} for writing: $!";
my $count = 0;
my $dates;
## now process this file.
while (<S>)
{ next unless /\w/;
next if /^!/;
## extract the stuff we want
chomp;
my @cols = split /\t/, $_;
next if ! $cols[13]; ## no date specified
if ($cols[13] !~ /^\d{8}$/)
{ warn "Incorrect date format in $args{file} line\n$_";
next;
}
if ($args{date_8})
{ ## find the date, check if we're OK or not
$dates->{ $cols[13] }++;
next if $cols[13] > $args{date_8}; ## later than our cutoff date
}
## body material!
print OUT $_ . "\n";
$count++;
if ($args{previous_quarter} && $cols[13] > $args{previous_quarter})
{ print Q $_ . "\n";
}
}
close OUT;
close S;
if ($args{previous_quarter})
{ close Q;
}
print STDERR "file: $args{file}; date: $args{date_8}; previous quarter: ". ( $args{previous_quarter} || "N/A" ) . "\n" . join("\n", map { "$_\t" . $dates->{$_} } sort keys %$dates) . "\n\n";
## delete the temp file
unlink $temp;
return $count;
}
sub run_checks_script {
# return;
my %args = (@_);
# return if $args{date} < 200611;
## the obo file
my $cmd = "$checks_script -i $args{input} -p nocheck -o /Users/gwg/go/ontology-archive/go-".$args{date}.".obo -n ".$args{date}."00 -w 2>&1 1>$args{output}";
print STDERR "running $cmd\n" if $verbose;
my $status = `$cmd`;
if ($status =~ /TOTAL ERRORS or WARNINGS = (\d+)\s.*?TOTAL out of date IEAs removed = (\d+)\s.*?Total of (\d+) lines \(not including header\) written to STDOUT./s)
{ my $total = $3;
my $errs = $1 - $2;
return if $errs == 0;
if ($total == 0)
{ warn "No lines left after checking $args{input}!";
return 1;
}
## ignoring IEAs, how many dodgy lines did we get rid of?
my $ten_percent = $total / 10;
if ($errs > $ten_percent)
{ ## get percentage errors
my $p = sprintf("%.1f", $errs / $total * 100);
warn "$args{input} has error rate $p\%";
return 1;
}
}
elsif ($status =~ /Congratulations, there are no errors/)
{
}
else
{ warn "No report produced by $checks_script! status: $status";
}
}
## move files to cvs sub dirs, create GPX files
sub prep_monthly_release {
my %args = (@_);
## get all the files for that month
if (! $args{metadata})
{ $args{metadata} = parse_all_metadata(%args);
}
if ($args{date})
{ ## find that date in the metadata, work out the names of the files reqd
FILE_CHECK:
foreach my $db (keys %{$metadata->{rev_date}})
{ print STDERR "Looking at $db...\n";
if ($metadata->{rev_date}{$db}{by_date}{$args{date}})
{ ## work out the file name
my $errs;
my $rev = $metadata->{rev_date}{$db}{by_date}{$args{date}};
my $f = $args{save_path} . $db . "/derived/" . $db . "-r-". $rev . "-sorted.gaf";
if (! -e $f)
{ warn "Could not find release file $f!";
$errs++;
next FILE_CHECK;
}
## copy file to the release directory
## new file name
my $new_f_name = "gene_association." . $db . "-r-" . $rev . ".gaf";
my $new_f_dir = $svn_repo . 'gaf/';
my $status = `cp $f $new_f_dir$new_f_name`;
if ($status)
{ warn "Error copying $f: $status";
$errs++;
next FILE_CHECK;
}
if (! $errs)
{ ## check whether we have the files or not:
my $gpad_f = $svn_repo . 'gpad/' . $db . "-r-$rev.gpad";
my $gpi_f = $svn_repo . 'gpi/' . $db . "-r-$rev.gpi";
if (-e $gpad_f && -e $gpi_f)
{ next FILE_CHECK;
}
## create the GPAD/GPI files
my $cmd = 'perl /Users/gwg/obo-scripts/gaf2gpx.pl -i ' . $new_f_dir . $new_f_name
. ' --gpad ' . $svn_repo . 'gpad/' . $db . "-r-$rev.gpad"
. ' --gpi ' . $svn_repo . 'gpi/' . $db . "-r-$rev.gpi";
# . ' -v';
print STDERR "About to GPXify $new_f_name\n";
my $status = `$cmd`;
print STDERR $status . "\n";
}
}
else
{ #warn "No files found for date $args{date}";
}
}
## svn copy svn+ssh://ext.geneontology.org/share/go/svn/trunk/gene-associations svn+ssh://ext.geneontology.org/share/go/svn/releases/YYYY-MM-DD
}
}
sub commit_monthly_release {
my %args = (@_);
## get all the files for that month
if (! $args{metadata})
{ $args{metadata} = parse_all_metadata(%args);
}
my $m_data = $args{metadata};
chdir $svn_repo;
# $meta->{rev_date}{$cols[0]}{by_date}{$cols[1]} = $cols[2];
my $st = `pwd`;
print STDERR $st . "\n\n";
$st = `svn info`;
print STDERR $st . "\n\n";
## find that date in the metadata, work out the names of the files reqd
## index the files by date, not db
foreach my $db (keys %{$m_data->{rev_date}})
{ foreach my $date (keys %{$m_data->{rev_date}{$db}{by_date}})
{ $m_data->{by_date}{$date}{$db} = $m_data->{rev_date}{$db}{by_date}{$date};
}
}
my $path = '/Users/gwg/piwi/';
foreach my $date (sort keys %{$m_data->{by_date}})
{ my $err;
my $status;
foreach my $db (keys %{$m_data->{by_date}{$date}})
{ my $rev = $m_data->{by_date}{$date}{$db};
## copy file to the release directory
## check whether we have the files or not:
my $gaf_f = $path.'gaf/gene_association.' . $db . "-r-" . $rev . ".gaf";
my $gpad_f = $path.'gpad/' . $db . "-r-$rev.gpad";
my $gpi_f = $path.'gpi/' . $db . "-r-$rev.gpi";
my $new_gaf_f = "gaf/gene_association." . $db . ".gaf";
my $new_gpad_f = 'gpad/' . $db . ".gpad";
my $new_gpi_f = 'gpi/' . $db . ".gpi";
if (-e $gpad_f && -e $gpi_f && -e $gaf_f)
{ # OK, we're good to commit!
# copy these files to the 'master' version
my $cmd = "cp $gaf_f $new_gaf_f";
print STDERR "Running $cmd\n";
$status = `$cmd`;
if ($status)
{ warn $status;
$err++;
}
$cmd = "cp $gpi_f $new_gpi_f";
print STDERR "Running $cmd\n";
$status = `$cmd`;
if ($status)
{ warn $status;
$err++;
}
$cmd = "cp $gpad_f $new_gpad_f";
print STDERR "Running $cmd\n";
$status = `$cmd`;
if ($status)
{ warn $status;
$err++;
}
}
else
{ warn "Missing files for $date, $db, $rev:" .
( -e $gpad_f ? "" : " GPAD" )
. (-e $gpi_f ? "" : " GPI" )
. (-e $gaf_f ? "" : " GAF" )
. "\n";
}
}
if ($err)
{ warn "Aborting commit of files for $date";
next;
}
## otherwise, let's commit!
my $cmd = "svn commit --username bbop --password bbop -m 'GAF, GPI, GPAD files for date " . $date . "'";
print STDERR "About to run\n$cmd\n";
$status = `$cmd 2>&1`;
if ($status)
{ warn "svn commit: $status";
}
else
{ warn "svn commit seemed to work!";
}
# exit(0);
## Try making the monthly release
$cmd = "svn copy svn://piwi.lbl.gov/go/gene-associations svn://piwi.lbl.gov/go/releases/".$date."01";
# $status = `$cmd`;
# if ($status)
# { warn "svn copy failed! $status";
## svn copy svn+ssh://ext.geneontology.org/share/go/gene-associations svn+ssh://ext.geneontology.org/share/go/releases/YYYY-MM-DD
# }
}
}
sub svn_commit_file {
my %args = (@_);
## copy the file to the correct directory.
## compress the file??
## new file name is gene-association.DBNAME.gaf
my $f_name = "gene-association.".$args{db}.".gaf";
my $cmd = "cp $args{file} $svn_repo$f_name";
print STDERR "about to run $cmd\n";
my $status = `$cmd`;
if ($status)
{ warn "copy: $status";
}
$user = 'bbop';
## commit this file to svn?
chdir $svn_repo;
$cmd = "svn commit -m 'Gene association file for " . $args{db} . "; release date: " . $args{date} . ", CVS revision: " . $args{rev} . "' $f_name";
print STDERR "About to run\n$cmd\n";
$status = `$cmd`;
if ($status)
{ warn "svn commit: $status";
}
chdir $base_path;
# $cmd = "svn copy svn+ssh://$user\@ext.geneontology.org/share/go/svn/trunk/gene-associations/$args{f_name} svn+ssh://$user\@ext.geneontology.org/share/go/svn/releases/$args{date}";
# $status = `$cmd`;
# if ($status)
# { warn "svn copy to release directory: $status";
# }
}
## input: save_path => ..., db => ...
sub parse_metadata {
my %args = (@_);
my $meta;
## open the metadata file
# sftp://plutonium.lbl.gov//Users/gwg/go/gaf-versions/aspgd/metadata.txt
open( M, "< " . $args{save_path} . 'metadata.txt') or die "Could not open metadata file: " . $args{save_path} . "metadata.txt: $!";
my $on;
while (<M>)
{ next unless /\w/;
last if /All revisions:/;
if (/Saved: by date:/)
{ $on++;
}
elsif ($on)
{ if (/(\d{6}): (\d+\.\d+)/)
{ $meta->{by_date}{$1} = $2;
$meta->{by_rev}{$2} = $1;
}
}
}
close(M);
return $meta;
}
## input: save_path => ..., db => ...
sub parse_all_metadata {
my %args = (@_);
my $meta;
## open the metadata file
open( M, "< " . $args{metadata_file}) or die "Could not open metadata file " . $args{metadata_file} .": $!";
my $on;
while (<M>)
{ next unless /\w/;
chomp;
my @cols = split(/\t/, $_, 3);
$meta->{rev_date}{$cols[0]}{by_date}{$cols[1]} = $cols[2];
$meta->{rev_date}{$cols[0]}{by_rev}{$cols[2]} = $cols[1];
}
close(M);
return $meta;
}
sub get_quarterly_files {
my %args = (@_);
opendir(DIR, $args{derived}) or die "can't opendir $args{derived}: $!";
my $files;
while (defined(my $file = readdir(DIR)))
{ ## check the file name for the db #
if ($file =~ /-r-(\d\.\d+)-recent\.gaf$/)
{ ## find the date from the metadata
if ($metadata->{rev_date}{ $args{db} }{by_rev}{$1})
{ $metadata->{by_date}{ $metadata->{rev_date}{ $args{db} }{by_rev}{$1} }{ $args{db} } = $file;
}
else
{ warn "Could not find date for $file!";
}
$metadata->{by_rev}{$args{db}}{$1} = $file;
}
}
closedir(DIR);
return $files;
}
sub write_metadata {
my $m_file = 'go/gaf-versions/all-metadata.txt';
open(M, "> $m_file") or die "Could not open $m_file: $!";
foreach my $db (sort keys %{$metadata->{rev_date}})
{ foreach my $d (sort keys %{$metadata->{rev_date}{$db}{by_date}})
{ print M "$db\t$d\t" . $metadata->{rev_date}{$db}{by_date}{$d} . "\n";
}
print M "\n";
}
close M;
}
sub get_stats {
my %args = (@_);
## fname of the form
## aspgd/derived/aspgd-r-1.121-recent.gaf
my $quarter = {
'01' => '1',
'02' => '1',
'03' => '1',
'04' => '2',
'05' => '2',
'06' => '2',
'07' => '3',
'08' => '3',
'09' => '3',
'10' => '4',
'11' => '4',
'12' => '4',
};
my $all_stats;
my $q_stats;
my $database;
foreach my $file (@{$args{files}})
{ ## find db name
# print STDERR "looking at $file\n";
if ($file =~ /^(.*?)-r-\d\.\d+/)
{ $database = $1;
}
else
{ warn "Unknown database name: $file";
}
my $file_path = "go/gaf-versions/$database/derived/";
# print STDERR "Looking at $file_path$file for $database\n";
next unless -e $file_path && -e $file_path.$file;
# print STDERR "Found file, about to open it...\n";
if ($file =~ /\.gz$/)
{ open( FH, "gzip -dc $file_path$file|") or die "Could not open $file: $!";
}
else
{ open( FH, "< $file_path$file") or die "Could not open $file: $!";
}
if (! -e 'go/gaf-versions/stats')
{ mkdir 'go/gaf-versions/stats';
}
my $output = "go/gaf-versions/stats/$database-".$args{date}."-stats.txt";
# print STDERR "output going to $output\n";