Chobo/OntologyTerm.pm

filter: Statementstmt 93.7% 164 / 175 Branchbran 84.4% 49 / 58 Conditioncond 80.3% 41 / 51 MC/DCmcdc 64.2% 18 / 28 Subroutinesub 100% 17 / 17 Podpod 25.0% 2 / 8 totaltotal 86.3% 291 / 337 CC 55 SCAR 23.5
CC
55
Cov
89%
make_object
20.1
__ANON__
15.5
merge
7.1
CRAP 10.5
« Chobo/OntologyData.pm ↑ Summary Chobo/OntologyConf.pm »
1 package PomBase::Chobo::OntologyTerm;
2  
3 =head1 NAME
4  
5 PomBase::Chobo::OntologyTerm - Code for holding term data read from an OBO file
6  
7 =head1 SYNOPSIS
8  
9 =head1 AUTHOR
10  
11 Kim Rutherford C<< <kmr44@cam.ac.uk> >>
12  
13 =head1 BUGS
14  
15 Please report any bugs or feature requests to C<kmr44@cam.ac.uk>.
16  
17 =head1 SUPPORT
18  
19 You can find documentation for this module with the perldoc command.
20  
21 perldoc PomBase::Chobo::OntologyTerm
22  
23 =over 4
24  
25 =back
26  
27 =head1 COPYRIGHT & LICENSE
28  
29 Copyright 2012 Kim Rutherford, all rights reserved.
30  
31 This program is free software; you can redistribute it and/or modify it
32 under the same terms as Perl itself.
33  
34 =head1 FUNCTIONS
35  
36 =cut
37  
38 6 our $VERSION = '0.041'; # VERSION
39  
40 6 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 6 ▶use PomBase::Chobo::OntologyConf;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 6 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 ▶use List::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 6 has id => (is => 'ro', isa => 'Int', required => 1);
50 6 has cvterm_id => (is => 'ro', isa => 'Int', required => 0);
51 6 has cv_id => (is => 'ro', isa => 'Int', required => 0);
52 6 has name => (is => 'ro', isa => 'Str');
53 6 has def => (is => 'ro', isa => 'Str');
54 6 has namespace => (is => 'ro', isa => 'Str');
55 6 has comment => (is => 'ro', isa => 'Str');
56 6 has alt_id => (is => 'ro', isa => 'ArrayRef');
57 6 has xref => (is => 'ro', isa => 'ArrayRef');
58 6 has subset => (is => 'ro', isa => 'ArrayRef');
59 6 has is_relationshiptype => (is => 'ro', isa => 'Bool');
60 6 has is_obsolete => (is => 'ro', isa => 'Bool');
61 6 has replaced_by => (is => 'ro', isa => 'Str');
62 6 has consider => (is => 'ro', isa => 'Str');
63 6 has property_value => (is => 'ro', isa => 'ArrayRef');
64 6 has source_file => (is => 'ro', isa => 'Str', required => 1);
65 6 has source_file_line_number => (is => 'ro', isa => 'Str', required => 1);
66 6 has metadata => (is => 'ro');
67  
68 6 our @field_names;
69 6 our %field_conf;
70  
71 6 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
72 6 %field_conf = %PomBase::Chobo::OntologyConf::field_conf;
73 6 @field_names = qw(id name);
74  
75 6 ▶ for my $field_name (sort grep { $_ ne 'id' && $_ ne 'name' } keys %field_conf) {
Condition100%3/3
$_ ne 'id' && $_ ne 'name'
!ll&&!rl&&r
6684
MC/DC100%2/2
$_ ne 'id' && $_ ne 'name'
$_ ne 'id' $_ ne 'name'
Truth table100%3/3
A && B
A= $_ ne 'id'B= $_ ne 'name'
ABresult
0X0
100
111
76 84 push @field_names, $field_name;
77 }
78 }
79  
80 sub synonyms
81 8 ▶{
Subroutine synonyms: called CC=2 SCAR=6.9
Pod undocumented
82 8 my $self = shift;
83  
84 8 ▶ return @{$self->{synonym} // []};
Condition100%2/2
$self->{"synonym"} // []
l!l
44
MC/DC100%1/1
$self->{"synonym"} // []
$self->{"synonym"}
Truth table100%2/2
A
A= $self->{"synonym"}
Aresult
11
00
85 }
86  
87 sub alt_ids
88 169 ▶{
Subroutine alt_ids: called CC=5 SCAR=16.8
Pod undocumented
89 169 my $self = shift;
90  
91 return map {
92 43 my $val = $_;
93  
94 43 ▶ if ($val =~ /(\S+):(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /(\S+):(\S+)/) { } 39 4 43
95 {
96 39 id => $val,
97 db_name => $1,
98 accession => $2,
99 };
100 } else {
101 4 my $db_name;
102 4 ▶ if (defined $self->metadata()->{ontology} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->metadata->{'ontology'} and $self->metadata->{'ontology'} eq 'ro') { } 0 4 4
Condition33%1/3
defined $self->metadata->{"ontology"} and $self->metadata->{"ontology"} eq "ro"
!ll&&!rl&&r
040
MC/DC0%0/2
defined $self->metadata->{"ontology"} and $self->metadata->{"ontology"} eq "ro"
defined $self->metadata->{"ontology"} $self->metadata->{"ontology"} eq "ro"
Truth table33%1/3
A and B
A= defined $self->metadata->{"ontology"}B= $self->metadata->{"ontology"} eq "ro"
ABresult
0X0
100
111
103 $self->metadata()->{ontology} eq 'ro') {
104 0 $db_name = 'OBO_REL'
105 } else {
106 4 $db_name = '_global';
107 }
108 {
109 4 id => $val,
110 db_name => $db_name,
111 accession => $val,
112 };
113 }
114 169 ▶ } @{$self->{alt_id} // []};
Condition50%1/2
$self->{"alt_id"} // []
l!l
1690
MC/DC0%0/1
$self->{"alt_id"} // []
$self->{"alt_id"}
Truth table50%1/2
A
A= $self->{"alt_id"}
Aresult
11
00
115 }
116  
117 sub property_values
118 8 ▶{
Subroutine property_values: called CC=2 SCAR=6.9
Pod undocumented
119 8 my $self = shift;
120  
121 8 ▶ return @{$self->{property_value} // []};
Condition100%2/2
$self->{"property_value"} // []
l!l
26
MC/DC100%1/1
$self->{"property_value"} // []
$self->{"property_value"}
Truth table100%2/2
A
A= $self->{"property_value"}
Aresult
11
00
122 }
123  
124 sub subsets
125 2 ▶{
Subroutine subsets: called CC=2 SCAR=6.9
Pod undocumented
126 2 my $self = shift;
127  
128 2 ▶ return @{$self->{subset} // []};
Condition100%2/2
$self->{"subset"} // []
l!l
11
MC/DC100%1/1
$self->{"subset"} // []
$self->{"subset"}
Truth table100%2/2
A
A= $self->{"subset"}
Aresult
11
00
129 }
130  
131 sub xrefs
132 88 ▶{
Subroutine xrefs: called CC=2 SCAR=6.9
Pod undocumented
133 88 my $self = shift;
134  
135 88 ▶ return @{$self->{xref} // []};
Condition100%2/2
$self->{"xref"} // []
l!l
1375
MC/DC100%1/1
$self->{"xref"} // []
$self->{"xref"}
Truth table100%2/2
A
A= $self->{"xref"}
Aresult
11
00
136 }
137  
138 =head2 make_object
139  
140 Usage : my $object = PomBase::Chobo::OntologyTerm->make_object($args);
141 Function: Turn $args into an OntologyTerm
142  
143 =cut
144  
145 sub make_object
146 88 ▶{
Subroutine make_object: called CC=18 SCAR=30.0
Pod documented
147 88 my $class = shift;
148 88 my $object = shift;
149 88 my $options = shift;
150  
151 88 ▶ if (!defined $object) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 0 88 88
152 0 croak "no argument passed to new()";
153 }
154  
155 88 ▶ if ($object->{def} && $object->{def}->{dbxrefs} && $object->{alt_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->{"def"} and $object->{"def"}{"dbxrefs"} and $object->{"alt_id"}) 11 77 88
Condition83%5/6
$object->{"def"} and $object->{"def"}{"dbxrefs"} and $object->{"alt_id"}
!ll&&!rl&&r
314611
$object->{"def"} and $object->{"def"}{"dbxrefs"}
!ll&&!rl&&r
31057
MC/DC66%2/3
$object->{"def"} and $object->{"def"}{"dbxrefs"} and $object->{"alt_id"}
$object->{"def"} $object->{"def"}{"dbxrefs"} $object->{"alt_id"}
Truth table75%3/4
A and B and C
A= $object->{"def"}B= $object->{"def"}{"dbxrefs"}C= $object->{"alt_id"}
ABCresult
0XX0
10X0
1100
1111
156 11 for my $alt_id (@{$object->{alt_id}}) {
157 # filter alt_ids from the definition xrefs to avoid:
158 # duplicate key value violates unique constraint "cvterm_dbxref_c1"
159 # see also: https://github.com/kimrutherford/go-ontology/commit/92dca313a69ffb073c226b94242faa8f321efcf2
160 16 @{$object->{def}->{dbxrefs}} =
161 grep {
162 12 my $xref = $_;
163 12 $alt_id ne $xref;
164 16 } @{$object->{def}->{dbxrefs}};
165 }
166 }
167  
168 88 ▶ if ($object->{is_obsolete} && $object->{name} && $object->{name} !~ /^obsolete/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->{"is_obsolete"} and $object->{"name"} and not $object->{"name"} =~ /^obsolete/i) 2 86 88
Condition83%5/6
$object->{"is_obsolete"} and $object->{"name"} and not $object->{"name"} =~ /^obsolete/i
!ll&&!rl&&r
8242
$object->{"is_obsolete"} and $object->{"name"}
!ll&&!rl&&r
8206
MC/DC66%2/3
$object->{"is_obsolete"} and $object->{"name"} and not $object->{"name"} =~ /^obsolete/i
$object->{"is_obsolete"} $object->{"name"} not $object->{"name"} =~ /^obsolete/i
Truth table75%3/4
A and B and not(C)
A= $object->{"is_obsolete"}B= $object->{"name"}C= not $object->{"name"} =~ /^obsolete/i
ABCresult
0XX0
10X0
1100
1111
169 2 $object->{name} = "OBSOLETE " . $object->{id} . " " . $object->{name};
170 }
171  
172 88 ▶ if ($object->{is_relationshiptype} && $object->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->{"is_relationshiptype"} and $object->{"name"}) 14 74 88
Condition66%2/3
$object->{"is_relationshiptype"} and $object->{"name"}
!ll&&!rl&&r
74014
MC/DC50%1/2
$object->{"is_relationshiptype"} and $object->{"name"}
$object->{"is_relationshiptype"} $object->{"name"}
Truth table66%2/3
A and B
A= $object->{"is_relationshiptype"}B= $object->{"name"}
ABresult
0X0
100
111
173 14 $object->{name} =~ s/ /_/g;
174 }
175  
176 88 $object->{_namespace_from_metadata} = 0;
177  
178 88 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 81 7 88
179 81 ▶ if ($options->{namespace_from_metadata}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"namespace_from_metadata"}) 40 41 81
180 40 $object->{_namespace_from_metadata} = 1;
181 }
182 }
183  
184 88 ▶ $object->{alt_id} //= [];
Condition100%2/2
$object->{"alt_id"} //= []
l!l
2068
MC/DC100%1/1
$object->{"alt_id"} //= []
$object->{"alt_id"}
Truth table100%2/2
A
A= $object->{"alt_id"}
Aresult
11
00
185  
186 88 my ($db_name, $accession);
187  
188 88 ▶ unless (($db_name, $accession) = $object->{id} =~ /^(\S+):(.+?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $object->{"id"} =~ /^(\S+):(.+?)\s*$/) 10 78 88
189 10 ▶ if ($object->{id} eq 'part_of') {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->{'id'} eq 'part_of') { } 0 10 10
190 # special case to make sure all the part_of terms are merged - the "part_of"
191 # in the GO and FYPO OBO files has the namespace "external" (and a variety of
192 # others) and the ID is "part_of"
193 # we normalise the id and namespace to match RO
194 0 $db_name = 'BFO';
195 0 $accession = '0000050';
196  
197 0 $object->{id} = "$db_name:$accession";
198 0 $object->{namespace} = "relationship";
199 } else {
200 10 $db_name = '_global';
201 10 $accession = $object->{id};
202 }
203 }
204  
205 88 $object->{accession} = $accession;
206 88 $object->{db_name} = $db_name;
207  
208 88 ▶ if (!defined $object->{source_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object->{"source_file"}) 0 88 88
209 0 confess "source_file attribute of object is required\n";
210 }
211  
212 88 ▶ if (!defined $object->{source_file_line_number}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object->{"source_file_line_number"}) 0 88 88
213 0 confess "source_file_line attribute of object is required\n";
214 }
215  
216 88 return bless $object, $class;
217 }
218  
219 =head2 merge
220  
221 Usage : my $merged_term = $term->merge($other_term);
222 Function: Attempt to merge $other_term into this term. Only merges if at least
223 one of the ID or alt_ids from this term match the ID or an alt_id
224 from $other_term
225 Args : $other_term - the term to merge with
226 Return : undef - if no id from this term matches one from $other_term
227 $self - if there is a match
228 =cut
229  
230 sub merge
231 15 ▶{
Subroutine merge: called CC=7 SCAR=19.5
Pod documented
232 15 my $self = shift;
233 15 my $other_term = shift;
234  
235 15 my $orig_term = clone $self;
236  
237 15 ▶ return if $self == $other_term;
Branch: 1 branch
BranchTrueFalseTotal
if $self == $other_term 1 14 15
238  
239 14 my $lc = List::Compare->new([$self->{id}, @{$self->{alt_id}}],
240 14 [$other_term->{id}, @{$other_term->{alt_id}}]);
241  
242 14 ▶ if (scalar($lc->get_intersection()) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $lc->get_intersection == 0) 0 14 14
243 0 return undef;
244 }
245  
246 14 my @new_alt_id = List::Compare->new([$lc->get_union()], [$self->id()])->get_unique(1);
247  
248 14 $self->{alt_id} = \@new_alt_id;
249  
250 32 ▶ my $merge_field = sub {
Subroutine __ANON__: called CC=15 SCAR=27.4
251 32 my $name = shift;
252 32 my $other_term = shift;
253  
254 32 my $field_conf = $PomBase::Chobo::OntologyConf::field_conf{$name};
255  
256 32 ▶ if (defined $field_conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_conf) { } 32 0 32
257 32 ▶ 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') { } 18 14 32
Condition83%5/6
defined $field_conf->{"type"} and $field_conf->{"type"} eq "SINGLE" || $field_conf->{"type"} eq "SINGLE_HASH"
!ll&&!rl&&r
01418
$field_conf->{"type"} eq "SINGLE" or $field_conf->{"type"} eq "SINGLE_HASH"
l!l&&r!l&&!r
16214
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
258 ($field_conf->{type} eq 'SINGLE' || $field_conf->{type} eq 'SINGLE_HASH')) {
259 18 my $res = undef;
260 18 ▶ if (defined $field_conf->{merge}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_conf->{"merge"}) 9 9 18
261 9 $res = $field_conf->{merge}->($self, $other_term);
262 }
263  
264 18 ▶ if (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) { } 2 16 18
265 2 $self->{$name} = $res;
266 } else {
267 16 my $new_field_value = $other_term->{$name};
268  
269 16 ▶ if (defined $new_field_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_field_value) { } 12 4 16
270 12 ▶ if (!defined $self->{$name} ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{$name} or $name eq 'namespace' and $self->{'_namespace_from_metadata'}) { } 9 3 12
Condition83%5/6
not defined $self->{$name} or $name eq "namespace" and $self->{"_namespace_from_metadata"}
l!l&&r!l&&!r
633
$name eq "namespace" and $self->{"_namespace_from_metadata"}
!ll&&!rl&&r
303
MC/DC66%2/3
not defined $self->{$name} or $name eq "namespace" and $self->{"_namespace_from_metadata"}
not defined $self->{$name} $name eq "namespace" $self->{"_namespace_from_metadata"}
Truth table75%3/4
not(A) or B and C
A= not defined $self->{$name}B= $name eq "namespace"C= $self->{"_namespace_from_metadata"}
ABCresult
1XX1
0111
00X0
0100
271 ($name eq 'namespace' &&
272 $self->{_namespace_from_metadata})) {
273 9 $self->{$name} = $new_field_value;
274 } else {
275 3 ▶ if ($name ne 'namespace' || !$other_term->{_namespace_from_metadata}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne "namespace" or not $other_term->{"_namespace_from_metadata"}) 3 0 3
Condition33%1/3
$name ne "namespace" or not $other_term->{"_namespace_from_metadata"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$name ne "namespace" or not $other_term->{"_namespace_from_metadata"}
$name ne "namespace" not $other_term->{"_namespace_from_metadata"}
Truth table33%1/3
A or not(B)
A= $name ne "namespace"B= not $other_term->{"_namespace_from_metadata"}
ABresult
1X1
011
000
276 warn qq|new "$name" tag of this stanza (from |,
277 $other_term->source_file(), " line ",
278 $other_term->source_file_line_number(), ") ",
279 "differs from previously ",
280 "seen value (from ", $self->source_file(),
281 " line ", $self->source_file_line_number(), q|) "|,
282 3 $orig_term->{$name}, '" ',
283 qq(- ignoring new value: "$new_field_value"\n\n),
284 "while merging: \n" . $other_term->to_string() . "\n\n",
285 "into existing term:\n",
286 $orig_term->to_string(), "\n\n";
287 }
288 }
289 } else {
290 # no merging to do
291 }
292 }
293 } else {
294 14 my $new_field_value = $other_term->{$name};
295 14 for my $single_value (@$new_field_value) {
296 10 ▶ if (!grep { Compare($_, $single_value) } @{$self->{$name}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {Compare($_, $single_value);} @{$$self{$name};}) 9 1 10
297 9 push @{$self->{$name}}, clone $single_value;
298 }
299 }
300 }
301 } else {
302 0 die "unhandled field in merge(): $name\n";
303 }
304 14 };
305  
306 14 for my $field_name (@field_names) {
307 224 ▶ next if $field_name eq 'id' or $field_name eq 'alt_id';
Branch: 1 branch
BranchTrueFalseTotal
if $field_name eq "id" or $field_name eq "alt_id" 28 196 224
Condition100%3/3
$field_name eq "id" or $field_name eq "alt_id"
l!l&&r!l&&!r
1414196
MC/DC100%2/2
$field_name eq "id" or $field_name eq "alt_id"
$field_name eq "id" $field_name eq "alt_id"
Truth table100%3/3
A or B
A= $field_name eq "id"B= $field_name eq "alt_id"
ABresult
1X1
011
000
308  
309 196 ▶ if (!Compare($self->{$field_name}, $other_term->{$field_name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Compare($self->{$field_name}, $other_term->{$field_name})) 32 164 196
310 32 $merge_field->($field_name, $other_term);
311 }
312 }
313  
314 14 return $self;
315 }
316  
317 sub to_string
318 12 ▶{
Subroutine to_string: called CC=4 SCAR=13.9
Pod undocumented
319 12 my $self = shift;
320  
321 12 my @lines = ();
322  
323 12 ▶ if ($self->is_relationshiptype()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_relationshiptype) { } 0 12 12
324 0 push @lines, "[Typedef]";
325 } else {
326 12 push @lines, "[Term]";
327 }
328  
329 60 ▶ my $line_maker = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
330 60 my $name = shift;
331 60 my $value = shift;
332  
333 60 my @ret_lines = ();
334  
335 60 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 26 34 60
336 26 my @values;
337 26 ▶ if ($field_conf{$name}->{type} eq 'SINGLE_HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($PomBase::Chobo::OntologyTerm::field_conf{$name}{'type'} eq 'SINGLE_HASH') { } 2 24 26
338 2 push @values, $value;
339 } else {
340 24 @values = @$value;
341 }
342 26 for my $single_value (@values) {
343 40 my $to_string_proc = $field_conf{$name}->{to_string};
344 40 my $value_as_string;
345 40 ▶ if (defined $to_string_proc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $to_string_proc) { } 19 21 40
346 19 $value_as_string = $to_string_proc->($single_value);
347 } else {
348 21 $value_as_string = $single_value;
349 }
350 40 push @ret_lines, "$name: $value_as_string";
351 }
352 } else {
353 34 push @ret_lines, "$name: $value";
354 }
355  
356 60 return @ret_lines;
357 12 };
358  
359 12 for my $field_name (@field_names) {
360 192 my $field_value = $self->{$field_name};
361  
362 192 ▶ if (defined $field_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_value) 60 132 192
363 60 push @lines, $line_maker->($field_name, $field_value);
364 }
365 }
366  
367 12 return join "\n", @lines;
368 }
369  
370 6 1;
« Chobo/OntologyData.pm ↑ Summary Chobo/OntologyConf.pm »