-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobo-def-differ.pl
executable file
·267 lines (209 loc) · 5.96 KB
/
obo-def-differ.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
#!/usr/bin/perl -w
=head1 NAME
obo-def-differ.pl.pl - compare term defs between two OBO files
=head1 SYNOPSIS
obo-def-differ.pl.pl --file_1 old_gene_ontology.obo --file_2 gene_ontology.obo
-o results.txt
=head1 DESCRIPTION
Compares the defs in two OBO files and records the differences between them
=head2 Input parameters
=head3 Required
=over
=item -f1 || --file_1 /path/to/file_name
"old" ontology file
=item -f2 || --file_2 /path/to/file_2_name
"new" ontology file
=item -o || --output /path/to/file_name
output file for results
=back
=head3 Optional switches
=over
=item -v || --verbose
prints various messages
=back
=cut
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
run_script(\@ARGV);
exit(0);
sub run_script {
my $options = parse_options(@_);
# check verbosity
if (! defined $options->{verbose})
{ $options->{verbose} = $ENV{GO_VERBOSE} || 0;
}
## OK, looks like we're going to have to compare the files.
print STDERR "Parsed options. Now starting script...\n" if $options->{verbose};
my $data;
open(OUT, ">" . $options->{'output'}) or die("Could not create " . $options->{output} . ": $!");
open(FH, "<" . $options->{'f1'}) or die("Could not open " . $options->{'f1'} . "! $!");
my @arr;
# remove and parse the header
{ local $/ = "\n[";
@arr = split("\n", <FH> );
# $data->{$f}{header} = tag_val_arr_to_hash( \@arr );
# print STDERR "Parsed $f header; starting body\n" if $options->{verbose};
my @lines;
{ local $/ = "\n[";
while (<FH>)
{ if (/^(\S+)\]\s*.*?^id:\s*(\S+)/sm)
{ # extract the interesting data
if ($1 eq "Term")
{ my $h;
map {
if (/(.*?): ?(.+)/)
{ $h->{$1} = $2;
}
} grep { /^(id|name|def|is_obsolete):/ } split("\n", $_);
if ($h->{def})
{ ## clip off the def xrefs
if ($h->{def} =~ /^\"(.*)\"\s*(\[.*)/)
{ $h->{def} = $1;
}
else
{ warn "Could not parse def for " . $h->{id};
}
}
$data->{f1}{ $h->{id} } = $h;
}
}
}
}
}
close(FH);
print STDERR "Parsed " . $options->{f1} . "\n" if $options->{verbose};
open(FH, "<" . $options->{'f2'}) or die("Could not open " . $options->{'f2'} . "! $!");
# remove and parse the header
{ local $/ = "\n[";
@arr = split("\n", <FH> );
# $data->{$f}{header} = tag_val_arr_to_hash( \@arr );
# print STDERR "Parsed $f header; starting body\n" if $options->{verbose};
my @lines;
{ local $/ = "\n[";
while (<FH>)
{ if (/^(\S+)\]\s*.*?^id:\s*(\S+)/sm)
{ # extract the interesting data
if ($1 eq "Term")
{ my $h;
map {
if (/(.*?): ?(.+)/)
{ $h->{$1} = $2;
}
} grep { /^(id|name|def|is_obsolete):/ } split("\n", $_);
if ($h->{def})
{ ## clip off the def xrefs
if ($h->{def} =~ /^\"(.*)\"\s*(\[.*)/)
{ $h->{def} = $1;
}
else
{ warn "Could not parse def for " . $h->{id};
}
}
if ($data->{f1}{ $h->{id} })
{ ## existing term
if ($data->{f1}{$h->{id}}{def} && $h->{def} && $h->{def} ne $data->{f1}{ $h->{id} }{def})
{ if ($h->{is_obsolete} && $h->{def} eq "OBSOLETE. " . $data->{f1}{$h->{id}}{def})
{ ## term has been obsoleted. Don't show.
}
else
{ $data->{changed}{$h->{id}}++;
$data->{f2}{$h->{id}} = $h;
}
}
}
}
}
}
}
}
close(FH);
print STDERR "Parsed " . $options->{f2} . "\nGathering results for printing...\n" if $options->{verbose};
# print STDERR "data: " . Dumper($data) . "\n";
if ($data->{changed})
{ foreach my $id (sort keys %{$data->{changed}})
{ print OUT "$id : " . $data->{f2}{$id}{name} . "\n";
if ($data->{f1}{$id}{name} ne $data->{f2}{$id}{name})
{ print OUT " was " . $data->{f1}{$id}{name} . "\n";
}
print OUT "OLD: " . $data->{f1}{$id}{def} . "\nNEW: " . $data->{f2}{$id}{def} . "\n\n";
}
}
close OUT;
}
# parse the options from the command line
sub parse_options {
my $args = shift;
my $opt;
while (@$args && $args->[0] =~ /^\-/) {
my $o = shift @$args;
if ($o eq '-f1' || $o eq '--file_1' || $o eq '--file_one') {
if (@$args && $args->[0] !~ /^\-/)
{ $opt->{f1} = shift @$args;
}
}
elsif ($o eq '-f2' || $o eq '--file_2' || $o eq '--file_two') {
if (@$args && $args->[0] !~ /^\-/)
{ $opt->{f2} = shift @$args;
}
}
elsif ($o eq '-o' || $o eq '--output') {
if (@$args && $args->[0] !~ /^\-/)
{ $opt->{output} = shift @$args;
}
}
elsif ($o eq '-h' || $o eq '--help') {
system("perldoc", $0);
exit(0);
}
elsif ($o eq '-v' || $o eq '--verbose') {
$opt->{verbose} = 1;
}
else {
die_msg( "Error: no such option: $o" );
}
}
return check_options($opt);
}
# process the input params
sub check_options {
my $opt = shift;
my $errs;
if (!$opt)
{ die_msg( "Error: please ensure you have specified two input files, a subset, and an output file." );
}
foreach my $f qw(f1 f2)
{ if (!$opt->{$f})
{ push @$errs, "specify an input file using -$f /path/to/<file_name>";
}
elsif (! -e $opt->{$f})
{ push @$errs, "the file " . $opt->{$f} . " could not be found.\n";
}
elsif (! -r $opt->{$f} || -z $opt->{$f})
{ push @$errs, "the file " . $opt->{$f} . " could not be read.\n";
}
}
if (!$opt->{output})
{ push @$errs, "specify an output file using -o /path/to/<file_name>";
}
if ($errs && @$errs)
{ die_msg( "Error: please correct the following parameters to run the script:\n" . ( join("\n", map { " - " . $_ } @$errs ) ) );
}
## quick 'diff' check of whether the files are identical or not
my $cmd = "diff -w -q -i '" . $opt->{f1} . "' '" . $opt->{f2} . "'";
my $status = `$cmd`;
die "The two files specified appear to be identical!" if ! $status;
if ($ENV{DEBUG})
{ $opt->{verbose} = 1;
}
return $opt;
}
sub die_msg {
my $msg = shift || "";
die join("\n", $msg, "The help documentation can be accessed with the command:\nobo-def-differ.pl --help\n");
}
=head1 AUTHOR
Amelia Ireland
=head1 SEE ALSO
L<GOBO::Graph>, L<GOBO::InferenceEngine>, L<GOBO::Doc::FAQ>
=cut