Chobo/ParseOBO.pm

filter: Statementstmt 85.0% 119 / 140 Branchbran 79.1% 57 / 72 Conditioncond 73.6% 14 / 19 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 3 totaltotal 79.2% 199 / 251 CC 46 SCAR 31.7
CC
46
Cov
82%
parse
29.1
_finish_stanza
22.9
die_line
2.0
CRAP 23.9
↑ Summary Chobo/OntologyData.pm »
1 package PomBase::Chobo::ParseOBO;
2  
3 =head1 NAME
4  
5 PomBase::Chobo::ParseOBO - Parse the bits of an OBO file needed for
6 loading Chado
7  
8 =head1 SYNOPSIS
9  
10 =head1 AUTHOR
11  
12 Kim Rutherford C<< <kmr44@cam.ac.uk> >>
13  
14 =head1 BUGS
15  
16 Please report any bugs or feature requests to C<kmr44@cam.ac.uk>.
17  
18 =head1 SUPPORT
19  
20 You can find documentation for this module with the perldoc command.
21  
22 perldoc PomBase::Chobo::ParseOBO
23  
24 =over 4
25  
26 =back
27  
28 =head1 COPYRIGHT & LICENSE
29  
30 Copyright 2012 Kim Rutherford, all rights reserved.
31  
32 This program is free software; you can redistribute it and/or modify it
33 under the same terms as Perl itself.
34  
35 =head1 FUNCTIONS
36  
37 =cut
38  
39 5 our $VERSION = '0.041'; # VERSION
40  
41 5 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 5 ▶use PomBase::Chobo::OntologyData;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 sub die_line
47 0 ▶{
Subroutine die_line: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my $filename = shift;
49 0 my $linenum = shift;
50 0 my $message = shift;
51  
52 0 die "$filename:$linenum:$message\n";
53 }
54  
55 sub _finish_stanza
56 81 ▶{
Subroutine _finish_stanza: called CC=21 SCAR=31.3
57 81 my $filename = shift;
58 81 my $current = shift;
59 81 my $terms_ref = shift;
60 81 my $metadata_ref = shift;
61  
62 81 ▶ if ($current->{is_obsolete} && $current->{is_relationshiptype}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{"is_obsolete"} and $current->{"is_relationshiptype"}) 0 81 81
Condition66%2/3
$current->{"is_obsolete"} and $current->{"is_relationshiptype"}
!ll&&!rl&&r
7560
MC/DC0%0/2
$current->{"is_obsolete"} and $current->{"is_relationshiptype"}
$current->{"is_obsolete"} $current->{"is_relationshiptype"}
Truth table66%2/3
A and B
A= $current->{"is_obsolete"}B= $current->{"is_relationshiptype"}
ABresult
0X0
100
111
63 0 return;
64 }
65  
66 81 ▶ if (!defined $current->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current->{"name"}) 0 81 81
67 0 warn "term without a name tag ignored: ", $current->{id}, "\n";
68 0 return;
69 }
70  
71 81 ▶ if (!defined $current->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current->{"id"}) 0 81 81
72 0 die_line $filename, $current->{line}, "stanza has no id\n";
73 0 return;
74 }
75  
76 81 ▶ if ($current->{id} !~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current->{"id"} =~ /:/) 12 69 81
77 # try to find a sensible ID if the ID looks like a name ("id: output_of")
78 12 ▶ if ($current->{xref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{"xref"}) 6 6 12
79 6 for my $xref (@{$current->{xref}}) {
80 6 ▶ if ($xref =~ /^RO:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref =~ /^RO:/) 2 4 6
81 2 $current->{id} = $xref;
82 goto ID_FIXED
83 2 }
84 }
85  
86 4 for my $xref (@{$current->{xref}}) {
87 4 ▶ if ($xref =~ /^BFO:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref =~ /^BFO:/) 0 4 4
88 0 $current->{id} = $xref;
89 goto ID_FIXED
90 0 }
91 }
92 }
93 }
94  
95 ID_FIXED:
96 81 ▶ if ($current->{is_obsolete}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{"is_obsolete"}) 6 75 81
97 6 delete $current->{alt_id};
98 }
99  
100 81 $current->{metadata} = $metadata_ref;
101 81 $current->{source_file} = $filename;
102 81 ▶ $current->{relationship} //= [];
Condition100%2/2
$current->{"relationship"} //= []
l!l
873
MC/DC100%1/1
$current->{"relationship"} //= []
$current->{"relationship"}
Truth table100%2/2
A
A= $current->{"relationship"}
Aresult
11
00
103  
104 81 my $namespace_from_metadata = 0;
105  
106 81 ▶ if (!defined $current->{namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current->{"namespace"}) 40 41 81
107 $current->{namespace} =
108 $metadata_ref->{'default-namespace'} //
109 $metadata_ref->{'ontology'} //
110 40 ▶ $current->{source_file} . '::' . $current->{id} =~ s/:.*//r;
Condition60%3/5
($metadata_ref->{"default-namespace"} // $metadata_ref->{"ontology"}) // $current->{"source_file"} . '::' . $current->{"id"} =~ s/:.*//r
l!l
400
$metadata_ref->{"default-namespace"} // $metadata_ref->{"ontology"}
l!l&&r!l&&!r
3370
MC/DC0%0/2
($metadata_ref->{"default-namespace"} // $metadata_ref->{"ontology"}) // $current->{"source_file"} . '::' . $current->{"id"} =~ s/:.*//r
$metadata_ref->{"default-namespace"} $metadata_ref->{"ontology"}
Truth table66%2/3
A // B
A= $metadata_ref->{"default-namespace"}B= $metadata_ref->{"ontology"}
ABresult
1X1
011
000
111  
112 40 ▶ if ($current->{namespace} eq 'ro') {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{"namespace"} eq "ro") 4 36 40
113 4 $current->{namespace} = 'relations';
114 }
115  
116 40 $namespace_from_metadata = 1;
117 }
118  
119 81 ▶ if ($current->{is_a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{"is_a"}) 48 33 81
120 map {
121 55 push @{$current->{relationship}},
122 {
123 'relationship_name' => 'is_a',
124 'other_term' => $_,
125 };
126 48 } @{$current->{is_a}};
127  
128 48 delete $current->{is_a};
129 }
130  
131 81 ▶ if ($current->{synonym}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{"synonym"}) 37 44 81
132 37 my %seen_synonyms = ();
133  
134 $current->{synonym} = [
135 map {
136  
137 66 my $seen_synonym = $seen_synonyms{$_->{synonym}};
138 66 ▶ if ($seen_synonym && lc $seen_synonym->{scope} eq 'exact') {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_synonym and lc $seen_synonym->{'scope'} eq 'exact') { } 3 63 66
Condition66%2/3
$seen_synonym and lc $seen_synonym->{"scope"} eq "exact"
!ll&&!rl&&r
6303
MC/DC50%1/2
$seen_synonym and lc $seen_synonym->{"scope"} eq "exact"
$seen_synonym lc $seen_synonym->{"scope"} eq "exact"
Truth table66%2/3
A and B
A= $seen_synonymB= lc $seen_synonym->{"scope"} eq "exact"
ABresult
0X0
100
111
139 # keep it
140 } else {
141 63 $seen_synonyms{$_->{synonym}} = $_;
142 }
143 37 } @{$current->{synonym}}
144 ];
145  
146 37 $current->{synonym} = [sort { $a->{synonym} cmp $b->{synonym} } values %seen_synonyms];
147 }
148  
149 81 my $options = { namespace_from_metadata => $namespace_from_metadata };
150  
151 81 my $new_term = PomBase::Chobo::OntologyTerm->make_object($current, $options);
152  
153 81 push @$terms_ref, $new_term;
154 }
155  
156 sub fatal
157 0 ▶{
Subroutine fatal: not called CC=1 SCAR=6.9
Pod undocumented
158 0 my $message = shift;
159  
160 0 die "fatal: $message\n";
161 }
162  
163 5 my %interesting_metadata = (
164 'default-namespace' => 1,
165 'ontology' => 1,
166 'date' => 1,
167 'data-version' => 1,
168 );
169  
170 sub parse
171 13 ▶{
Subroutine parse: called CC=26 SCAR=33.7
Pod undocumented
172 13 my $self = shift;
173 13 my %args = @_;
174  
175 13 my $filename = $args{filename};
176 13 ▶ if (!defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 13 13
177 0 die 'no filename passed to parse()';
178 }
179  
180 13 my $ontology_data = $args{ontology_data};
181 13 ▶ if (!defined $ontology_data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ontology_data) 0 13 13
182 0 die 'no ontology_data passed to parse()';
183 }
184  
185 13 my %metadata = ();
186 13 my @terms = ();
187  
188 13 my $current = undef;
189 13 my @synonyms = ();
190  
191 13 my %meta = ();
192  
193 13 ▶ open my $fh, '<:utf8', $filename or die "can't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:utf8", $filename or die "can't open ${filename}: $!" 13 0 13
194  
195 13 my $line_number = 0;;
196  
197 13 ▶ while (defined (my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 899 13 912
198 899 $line_number++;
199 899 chomp $line;
200 899 $line =~ s/![^"\n]*$//;
201 899 $line =~ s/\s+$//;
202 899 $line =~ s/^\s+//;
203  
204 899 ▶ next if length $line == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $line == 0 87 812 899
205  
206 812 ▶ if ($line =~ /^\[(.*)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\[(.*)\]$/) { } 81 731 812
207 81 my $stanza_type = $1;
208  
209 81 ▶ if (defined $current) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current) 68 13 81
210 68 _finish_stanza($filename, $current, \@terms, \%metadata);
211 }
212  
213 81 my $is_relationshiptype = 0;
214  
215 81 ▶ if ($stanza_type eq 'Typedef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stanza_type eq 'Typedef') { } 14 67 81
216 14 $is_relationshiptype = 1;
217 } else {
218 67 ▶ if ($stanza_type ne 'Term') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stanza_type ne "Term") 0 67 67
219 0 die "unknown stanza type '[$stanza_type]'\n";
220 }
221 }
222 81 $current = {
223 is_relationshiptype => $is_relationshiptype,
224 source_file_line_number => $line_number,
225 };
226 } else {
227 731 ▶ if ($current) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current) { } 622 109 731
228 622 my @bits = split /: /, $line, 2;
229 622 ▶ if (@bits == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bits == 2) { } 622 0 622
230 622 my $field_name = $bits[0];
231 622 my $field_value = $bits[1];
232  
233 # ignored for now
234 622 my $modifier_string;
235  
236 622 ▶ if ($field_value =~ /\}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_value =~ /\}$/) 2 620 622
237 2 $field_value =~ s/(.*)\{(.*)\}$/$1/;
238 2 $modifier_string = $2;
239 2 $field_value =~ s/\s+$//;
240 }
241  
242 622 my $field_conf = $PomBase::Chobo::OntologyConf::field_conf{$field_name};
243  
244 622 ▶ if (defined $field_conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_conf) 487 135 622
245 487 ▶ if (defined $field_conf->{process}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_conf->{"process"}) 240 247 487
246 240 eval {
247 240 $field_value = $field_conf->{process}->($field_value);
248 };
249 240 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 240 240
250 0 warn qq(warning "$@" at $filename line $.\n);
251 }
252 }
253 487 ▶ if (defined $field_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_value) 486 1 487
254 486 ▶ if (defined $field_conf->{type} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_conf->{'type'} and $field_conf->{'type'} eq 'SINGLE' || $field_conf->{'type'} eq 'SINGLE_HASH') { } 294 192 486
Condition83%5/6
defined $field_conf->{"type"} and $field_conf->{"type"} eq "SINGLE" || $field_conf->{"type"} eq "SINGLE_HASH"
!ll&&!rl&&r
0192294
$field_conf->{"type"} eq "SINGLE" or $field_conf->{"type"} eq "SINGLE_HASH"
l!l&&r!l&&!r
23757192
MC/DC66%2/3
defined $field_conf->{"type"} and $field_conf->{"type"} eq "SINGLE" || $field_conf->{"type"} eq "SINGLE_HASH"
defined $field_conf->{"type"} $field_conf->{"type"} eq "SINGLE" $field_conf->{"type"} eq "SINGLE_HASH"
Truth table75%3/4
A and B or C
A= defined $field_conf->{"type"}B= $field_conf->{"type"} eq "SINGLE"C= $field_conf->{"type"} eq "SINGLE_HASH"
ABCresult
0XX0
1000
11X1
1011
255 ($field_conf->{type} eq 'SINGLE' || $field_conf->{type} eq 'SINGLE_HASH')) {
256 294 $current->{$field_name} = $field_value;
257 } else {
258 192 push @{$current->{$field_name}}, $field_value;
259 }
260 }
261 }
262 } else {
263 0 die "can't parse line - no colon: $line\n";
264 }
265 } else {
266 # we're parsing metadata
267 109 ▶ if ($line =~ /^(.+?):\s*(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(.+?):\s*(.*)/) { } 109 0 109
268 109 my ($key, $value) = ($1, $2);
269  
270 109 ▶ if ($interesting_metadata{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interesting_metadata{$key}) 40 69 109
271 40 ▶ if (defined $metadata{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metadata{$key}) { } 0 40 40
272 0 warn qq(metadata key "$key" occurs more than once in header\n);
273 } else {
274 40 $metadata{$key} = $value;
275 }
276 }
277 } else {
278 0 fatal "can't parse header line: $line";
279 }
280 }
281 }
282 }
283  
284 13 ▶ if (defined $current) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current) 13 0 13
285 13 _finish_stanza($filename, $current, \@terms, \%metadata);
286 }
287  
288 13 ▶ close $fh or die "can't close $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "can't close ${filename}: $!" 13 0 13
289  
290 13 eval {
291 13 $ontology_data->add(metadata => \%metadata,
292 terms => \@terms);
293 };
294 13 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
295 0 die "failed while reading $filename: $@\n";
296 }
297 }
298  
299 5 1;
↑ Summary Chobo/OntologyData.pm »