Chobo/OntologyData.pm

filter: Statementstmt 92.4% 147 / 159 Branchbran 71.0% 27 / 38 Conditioncond 64.2% 9 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 24 / 24 Podpod 16.6% 2 / 12 totaltotal 82.8% 212 / 256 CC 29 SCAR 25.6
CC
29
Cov
87%
add
26.3
finish
3.3
relationships
3.1
CRAP 13.0
« Chobo/ParseOBO.pm ↑ Summary Chobo/OntologyTerm.pm »
1 package PomBase::Chobo::OntologyData;
2  
3 =head1 NAME
4  
5 PomBase::Chobo::OntologyData - An in memory representation of an Ontology
6  
7 =head1 SYNOPSIS
8  
9 Objects of this class represent the part of an ontology that can be stored in
10 a Chado database.
11  
12 =head1 AUTHOR
13  
14 Kim Rutherford C<< <kmr44@cam.ac.uk> >>
15  
16 =head1 BUGS
17  
18 Please report any bugs or feature requests to C<kmr44@cam.ac.uk>.
19  
20 =head1 SUPPORT
21  
22 You can find documentation for this module with the perldoc command.
23  
24 perldoc PomBase::Chobo::OntologyData
25  
26 =over 4
27  
28 =back
29  
30 =head1 COPYRIGHT & LICENSE
31  
32 Copyright 2012 Kim Rutherford, all rights reserved.
33  
34 This program is free software; you can redistribute it and/or modify it
35 under the same terms as Perl itself.
36  
37 =head1 FUNCTIONS
38  
39 =cut
40  
41 5 our $VERSION = '0.041'; # VERSION
42  
43 5 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 5 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 5 ▶use PomBase::Chobo::OntologyTerm;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51  
52 has terms_by_id => (is => 'rw', init_arg => undef, isa => 'HashRef',
53 9 ▶ default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
54 has terms_by_name => (is => 'rw', init_arg => undef, isa => 'HashRef',
55 9 ▶ default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
56 has terms_by_cv_name => (is => 'rw', init_arg => undef, isa => 'HashRef',
57 9 ▶ default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
58 has relationship_terms_by_cv_name => (is => 'rw', init_arg => undef, isa => 'HashRef',
59 9 ▶ default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
60 has terms_by_db_name => (is => 'rw', init_arg => undef, isa => 'HashRef',
61 9 ▶ default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
62 has metadata_by_namespace => (is => 'rw', init_arg => undef, isa => 'HashRef',
63 9 ▶ default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
64 has _term_relationships => (is => 'rw', init_arg => undef, isa => 'HashRef',
65 9 ▶ default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
66  
67 =head2 add
68  
69 Usage : $ontology_data->add(metadata => {..}, terms => [...]);
70 Function: Add some terms, often all terms from one OBO file
71 Args : metadata - the metadata for the terms
72 terms - an array of OntologyTerm objects
73 Return : Nothing, dies on error
74  
75 =cut
76  
77 sub add
78 13 ▶{
Subroutine add: called CC=24 SCAR=32.7
Pod documented
79 13 my $self = shift;
80  
81 13 my %args = @_;
82  
83 13 my $metadata = $args{metadata};
84 13 my $terms = $args{terms};
85  
86 13 my $terms_by_id = $self->terms_by_id();
87 13 my $terms_by_name = $self->terms_by_name();
88 13 my $terms_by_cv_name = $self->terms_by_cv_name();
89 13 my $relationship_terms_by_cv_name = $self->relationship_terms_by_cv_name();
90  
91 13 my $metadata_by_namespace = $self->metadata_by_namespace();
92  
93 13 for my $term (@$terms) {
94 81 my @new_term_ids = ($term->{id});
95  
96 81 push @new_term_ids, map { $_->{id}; } $term->alt_ids();
97  
98 81 my @found_existing_terms = ();
99  
100 81 for my $id (@new_term_ids) {
101 101 my $existing_term = $terms_by_id->{$id};
102  
103 101 ▶ if (defined $existing_term && !$existing_term->{is_obsolete}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $existing_term and not $existing_term->{"is_obsolete"}) 7 94 101
Condition100%3/3
defined $existing_term and not $existing_term->{"is_obsolete"}
!ll&&!rl&&r
9317
MC/DC100%2/2
defined $existing_term and not $existing_term->{"is_obsolete"}
defined $existing_term not $existing_term->{"is_obsolete"}
Truth table100%3/3
A and not(B)
A= defined $existing_termB= not $existing_term->{"is_obsolete"}
ABresult
0X0
100
111
104 7 ▶ if (!grep { $_ == $existing_term } @found_existing_terms) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ == $existing_term;} @found_existing_terms) 5 2 7
105 5 push @found_existing_terms, $existing_term;
106 }
107 }
108 }
109  
110 81 ▶ if (@found_existing_terms > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found_existing_terms > 1) { } 0 81 81
111 0 die "two previously read terms match an alt_id field from:\n" .
112 $term->to_string() . "\n\nmatching term 1:\n" .
113 $found_existing_terms[0]->to_string() . "\n\nmatching term 2:\n" .
114 $found_existing_terms[1]->to_string() . "\n";
115 } else {
116 81 ▶ if (@found_existing_terms == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found_existing_terms == 1) 5 76 81
117 5 my $existing_term = $found_existing_terms[0];
118  
119 5 ▶ if (!$term->is_obsolete() && !$existing_term->is_obsolete()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term->is_obsolete or $existing_term->is_obsolete) 5 0 5
Condition33%1/3
$term->is_obsolete or $existing_term->is_obsolete
l!l&&r!l&&!r
005
MC/DC0%0/2
$term->is_obsolete or $existing_term->is_obsolete
$term->is_obsolete $existing_term->is_obsolete
Truth table33%1/3
A or B
A= $term->is_obsoleteB= $existing_term->is_obsolete
ABresult
1X1
011
000
120 5 my $old_namespace = $existing_term->namespace();
121  
122 5 $existing_term->merge($term);
123  
124 5 ▶ if ($old_namespace ne $existing_term->namespace()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_namespace ne $existing_term->namespace) 3 2 5
125 3 delete $self->terms_by_cv_name()->{$old_namespace}->{$existing_term->name()};
126 }
127  
128 5 $term = $existing_term;
129 }
130 }
131 }
132  
133 81 for my $id_details ($term->alt_ids(),
134 { id => $term->{id},
135 db_name => $term->{db_name},
136 accession => $term->{accession},
137 } ) {
138 103 $terms_by_id->{$id_details->{id}} = $term;
139  
140 103 $self->terms_by_db_name()->{$id_details->{db_name}}->{$id_details->{accession}} = $term;
141 }
142  
143 81 my $def = $term->def();
144  
145 map {
146 64 my $def_dbxref = $_;
147 64 ▶ if ($def_dbxref =~ /^(.+?):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def_dbxref =~ /^(.+?):(.*)/) { } 64 0 64
148 64 my ($def_db_name, $def_accession) = ($1, $2);
149 64 $self->terms_by_db_name()->{$def_db_name}->{$def_accession} = $term;
150 } else {
151 0 die qq(can't parse dbxref from "def:" line: $def_dbxref);
152 }
153 81 } @{$def->{dbxrefs}};
154  
155 map {
156 81 my $xref = $_;
157  
158 15 ▶ if ($xref =~ /^(.+?):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xref =~ /^(.+?):(.*)/) { } 15 0 15
159 15 my ($def_db_name, $def_accession) = ($1, $2);
160 15 $self->terms_by_db_name()->{$def_db_name}->{$def_accession} = $term;
161 } else {
162 0 die qq(can't parse "xref:" line: $xref);
163 }
164 } $term->xrefs();
165  
166 81 my $name = $term->{name};
167  
168 81 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 81 0 81
169 81 ▶ if (!exists $terms_by_name->{$name} ||
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $terms_by_name->{$name} and grep {$_ == $term;} @{$$terms_by_name{$name};}) 76 5 81
Condition66%2/3
exists $terms_by_name->{$name} and grep {$_ == $term;} @{$$terms_by_name{$name};}
!ll&&!rl&&r
7605
MC/DC50%1/2
exists $terms_by_name->{$name} and grep {$_ == $term;} @{$$terms_by_name{$name};}
exists $terms_by_name->{$name} grep {$_ == $term;} @{$$terms_by_name{$name};}
Truth table66%2/3
A and B
A= exists $terms_by_name->{$name}B= grep {$_ == $term;} @{$$terms_by_name{$name};}
ABresult
0X0
100
111
170 5 !grep { $_ == $term } @{$terms_by_name->{$name}}) {
171 76 push @{$terms_by_name->{$name}}, $term;
172 }
173 } else {
174 0 warn "term without a name tag ignored:\n", $term->to_string(), "\n\n";
175 0 next;
176 }
177  
178 81 my $term_namespace = $term->namespace();
179  
180 81 ▶ if (defined $term_namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $term_namespace) 81 0 81
181 81 my $existing_term_by_name = $terms_by_cv_name->{$term_namespace}->{$name};
182 81 ▶ if ($existing_term_by_name && $existing_term_by_name != $term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_term_by_name and $existing_term_by_name != $term) { } 0 81 81
Condition66%2/3
$existing_term_by_name and $existing_term_by_name != $term
!ll&&!rl&&r
7920
MC/DC0%0/2
$existing_term_by_name and $existing_term_by_name != $term
$existing_term_by_name $existing_term_by_name != $term
Truth table66%2/3
A and B
A= $existing_term_by_nameB= $existing_term_by_name != $term
ABresult
0X0
100
111
183 warn qq(more than one Term with the name "$name" in namespace "$term_namespace":\n) .
184 " " . $term->id() . "\nand:\n " .
185 0 $terms_by_cv_name->{$term_namespace}->{$name}->id() . "\n\n";
186 } else {
187 81 $terms_by_cv_name->{$term_namespace}->{$name} = $term;
188 }
189  
190 81 ▶ if ($term->{is_relationshiptype}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->{"is_relationshiptype"}) 14 67 81
191 14 $relationship_terms_by_cv_name->{$term_namespace}->{$name} = $term;
192 }
193  
194 81 ▶ if (!exists $metadata_by_namespace->{$term_namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $metadata_by_namespace->{$term_namespace}) 17 64 81
195 17 $metadata_by_namespace->{$term_namespace} = clone $metadata;
196 }
197 }
198  
199 81 ▶ if ($term->{relationship}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->{"relationship"}) 81 0 81
200 81 for my $rel (@{$term->{relationship}}) {
201 my $key = $term->{id} . '<' . $rel->{relationship_name} .
202 65 '>' . $rel->{other_term};
203 65 $self->_term_relationships()->{$key} = 1;
204 }
205 }
206 }
207 }
208  
209 sub get_terms_by_name
210 1 ▶{
Subroutine get_terms_by_name: called CC=2 SCAR=7.0
Pod undocumented
211 1 my $self = shift;
212 1 my $name = shift;
213  
214 1 ▶ return @{$self->terms_by_name()->{$name} // []};
Condition50%1/2
$self->terms_by_name->{$name} // []
l!l
10
MC/DC0%0/1
$self->terms_by_name->{$name} // []
$self->terms_by_name->{$name}
Truth table50%1/2
A
A= $self->terms_by_name->{$name}
Aresult
11
00
215 }
216  
217 sub get_term_by_id
218 35 ▶{
Subroutine get_term_by_id: called CC=1 SCAR=0.0
Pod undocumented
219 35 my $self = shift;
220 35 my $id = shift;
221  
222 35 return $self->terms_by_id()->{$id};
223 }
224  
225 sub get_cv_names
226 23 ▶{
Subroutine get_cv_names: called CC=1 SCAR=0.0
Pod undocumented
227 23 my $self = shift;
228  
229 23 return keys %{$self->terms_by_cv_name()};
230 }
231  
232 sub get_terms_by_cv_name
233 32 ▶{
Subroutine get_terms_by_cv_name: called CC=1 SCAR=0.0
Pod undocumented
234 32 my $self = shift;
235 32 my $cv_name = shift;
236  
237 32 return values %{$self->terms_by_cv_name()->{$cv_name}};
238 }
239  
240 sub get_db_names
241 5 ▶{
Subroutine get_db_names: called CC=1 SCAR=0.0
Pod undocumented
242 5 my $self = shift;
243  
244 5 return keys %{$self->terms_by_db_name()};
245 }
246  
247 sub accessions_by_db_name
248 12 ▶{
Subroutine accessions_by_db_name: called CC=1 SCAR=0.0
Pod undocumented
249 12 my $self = shift;
250 12 my $db_name = shift;
251  
252 12 return sort keys %{$self->terms_by_db_name()->{$db_name}};
253 }
254  
255 sub get_terms
256 20 ▶{
Subroutine get_terms: called CC=1 SCAR=0.0
Pod undocumented
257 20 my $self = shift;
258  
259 20 return map { $self->get_terms_by_cv_name($_); } $self->get_cv_names();
260 }
261  
262 sub get_namespaces
263 3 ▶{
Subroutine get_namespaces: called CC=1 SCAR=0.0
Pod undocumented
264 3 my $self = shift;
265  
266 3 return keys %{$self->metadata_by_namespace()};
267 }
268  
269 sub get_metadata_by_namespace
270 4 ▶{
Subroutine get_metadata_by_namespace: called CC=1 SCAR=0.0
Pod undocumented
271 4 my $self = shift;
272 4 my $namespace = shift;
273  
274 4 return $self->metadata_by_namespace()->{$namespace};
275 }
276  
277 sub relationships
278 6 ▶{
Subroutine relationships: called CC=3 SCAR=11.3
Pod undocumented
279 6 my $self = shift;
280  
281 6 ▶ if ($self->{_relationships}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_relationships"}) 2 4 6
282 2 return @{$self->{_relationships}}
283 }
284  
285 $self->{_relationships} = [map {
286 32 my ($subject_id, $rel_name, $object_id) = /(.*)<(.*)>(.*)/;
287  
288 32 my $object_term = $self->get_term_by_id($object_id);
289  
290 32 ▶ if (!$object_term) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $object_term) { } 0 32 32
291 0 my $subject_term = $self->get_term_by_id($subject_id);
292 warn qq(ignoring relation where object isn't defined: "$object_id" line ) .
293 $subject_term->{source_file_line_number} . ' of ' .
294 0 $subject_term->{source_file} . "\n";
295 0 ();
296 } else {
297 32 [$subject_id, $rel_name, $object_id];
298 }
299 4 } sort keys %{$self->_term_relationships()}];
300  
301 4 return @{$self->{_relationships}};
302 }
303  
304 =head2 finish
305  
306 Usage : $self->finish();
307 Function: remove namespaces that are empty due to merging and check that
308 objects and subjects of relationships exist
309  
310 =cut
311  
312 sub finish
313 3 ▶{
Subroutine finish: called CC=3 SCAR=11.9
Pod documented
314 3 my $self = shift;
315  
316 3 my @relationships = $self->relationships();
317  
318 3 ▶ if (@relationships == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@relationships == 0) 0 3 3
319 0 warn "note: no relationships read\n";
320 }
321  
322 # find and remove namespaces that are empty due to merging
323 my @empty_namespaces =
324 map {
325 5 ▶ if (scalar(keys %{$self->terms_by_cv_name()->{$_}}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->terms_by_cv_name->{$_};} == 0) { } 0 5 5
326 0 $_;
327 } else {
328 5 ();
329 }
330 3 } keys %{$self->terms_by_cv_name()};
331  
332 map {
333 0 delete $self->terms_by_cv_name()->{$_};
334 } @empty_namespaces;
335 }
336  
337 5 1;
« Chobo/ParseOBO.pm ↑ Summary Chobo/OntologyTerm.pm »