forked from cmungall/obo-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobo-merge-equivalent-terms.pl
executable file
·92 lines (79 loc) · 1.86 KB
/
obo-merge-equivalent-terms.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
#!/usr/bin/perl -w
use strict;
my %tag_h=();
while ($ARGV[0] =~ /^\-/) {
my $opt = shift @ARGV;
if ($opt eq '-h' || $opt eq '--help') {
print usage();
exit 0;
}
if ($opt eq '-t' || $opt eq '--tag') {
$tag_h{shift @ARGV} = 1;
}
}
print STDERR "Tags: ", join(', ',keys %tag_h),"\n";
my %filtered_lines_by_id_h=();
my %filtered_lines_by_xp_h=();
while (@ARGV) {
my $f = pop @ARGV; # go through in REVERSE order
my $is_final = !@ARGV;
my $id;
my $in_header = 1;
open(F,$f) || die $f;
while(<F>) {
if (/^id:\s+(\S+)/) {
$id = $1;
}
elsif (!$is_final && /^(\S+):/) {
if (!$id) {
if (!$in_header) {
die "assertion error!";
}
$id = ''; # in header - call this ID ''
}
if ($tag_h{$1}) {
push(@{$filtered_lines_by_id_h{$id}},$_);
}
}
elsif ($is_final && /^\s*$/) {
# end of stanza, show any additional tags
showtags($id);
# later on we print the newline stanza separator...
}
else {
}
if ($is_final) {
print $_;
}
} # end of file
if ($is_final) {
# don't forget the last one
showtags($id);
}
close(F);
}
exit 0;
sub showtags {
my $id = shift || '';
my %done_h = (); # avoid dupes
if ($filtered_lines_by_id_h{$id}) {
foreach (@{$filtered_lines_by_id_h{$id} || []}) {
next if $done_h{$_};
print $_;
$done_h{$_} = 1;
}
delete $filtered_lines_by_id_h{$id};
}
return;
}
sub scriptname {
my @p = split(/\//,$0);
pop @p;
}
sub usage {
my $sn = scriptname();
<<EOM;
$sn [-t tag]* BASE-FILE FILE-TO-MERGE1 [FILE-TO-MERGE2...]
TODO
EOM
}