-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobo-subtract.pl
executable file
·174 lines (151 loc) · 3.82 KB
/
obo-subtract.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
#!/usr/bin/perl -w
use strict;
my %tag_h=();
my $regexp = '';
my $noheader;
my $negate;
my $comment;
my $rmc = 0;
if (!@ARGV) {
print usage();
exit 1;
}
while ($ARGV[0] =~ /^\-.+/) {
my $opt = shift @ARGV;
if ($opt eq '-h' || $opt eq '--help') {
print usage();
exit 0;
}
if ($opt eq '--noheader') {
$noheader = 1;
}
if ($opt eq '--comment') {
$comment = 1;
}
if ($opt eq '--remove-comments') {
$rmc = 1;
}
}
my %th = ();
$/ = "\n\n";
my $n = 0;
my $firstf = shift @ARGV;
while (@ARGV) {
my $f = shift @ARGV;
if ($f eq '-') {
*F=*STDIN;
}
else {
open(F,$f) || die $f;
}
my $hdr = 0;
while(<F>) {
if ($rmc) {
s/^\!*//g;
s/\n\!*/\n/g;
}
# remove commented out info
#s/\!.*\n//g;
if (!$hdr && $_ !~ /^\[/) {
$hdr = 1;
}
else {
if (/id: (\S+)/) {
my $id = $1;
if ($th{$id}) {
if ($th{$id} eq $_) {
print STDERR "IDENTICAL: $id\n";
}
else {
print STDERR "========================================\nDITCHING:\n\n$_ <<< >>>>\n\nUSING:\n\n$th{$id}";
}
}
else {
$th{$id} = $_;
}
}
else {
# no ID - this is allowed; e.g. Annotation stanzas
$th{$_} = $_;
}
}
}
close(F);
}
open(F,$firstf) || die $firstf;
my $hdr = 0;
while (<F>) {
if (!$hdr && $_ !~ /^\[/) {
print unless $noheader;
$hdr = 1;
$noheader = 1; # show max 1 times
}
else {
if (/\nid: (\S+)/) {
my $id = $1;
if ($th{$id}) {
my $diff = diff($_,$th{$id});
print STDERR "Subtracting $id $diff\n";
if ($comment) {
my @lines = map {"!! $_"} split(/\n/,$_);
print join("\n",@lines),"\n\n";
}
}
else {
print STDERR "Keeping $id\n";
print $_;
}
}
else {
# no ID - this is allowed; e.g. Annotation stanzas
print $_;
}
}
}
close(F);
exit 0;
sub diff {
my $x = shift;
my $y = shift;
if ($x eq $y) {
return "[identical]";
}
my $xc = clean($x);
my $yc = clean($y);
if ($xc eq $yc) {
return "[equivalent]";
}
return "[DIFFERENT] <<$xc>> <<$yc>>\nDITCHING:\n$y\nUSING:\n$x\n";
}
sub clean {
my $x = shift;
$x =~ s/\!.*//g;
$x =~ s/\s//g;
return $x;
}
sub scriptname {
my @p = split(/\//,$0);
pop @p;
}
sub usage {
my $sn = scriptname();
<<EOM;
$sn [--noheader] [--comment] OBO-FILE1 OBO-FILE2 [OBO-FILE3...]
Subtracts one obo file from another. Each stanza is treated
as atomic, and identified by its ID. No attempt is made to merge tags
within a stanza.
If you pass in files F1, F2, F3, ... then the result will be F1, with
stanzas from F2 subtracted, then stanzas from F3 subtracted....
i.e. left-associative just like arithmentic subtraction
e.g. passing in 4 files will yield (((F1-F2)-F3)-F4)
If F2 is subtracted from F1, and F2 contains stanzas not mappable to
F1, then these will simply be ignored.
If F2 is subtracted from F1, any stanza in the intersection between F1
and F2 will not be present in the output file. Here stanzas are
compared purely with respect to their id. The report will tell whether
the intersecting stanzas are equivalent, or different.
If --comment is passed, then the duplicate stanzas are commented out rather than removed
A report is written on STDERR
See also: obo-merge-tags.pl
EOM
}