line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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<< >> |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 BUGS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Please report any bugs or feature requests to C. |
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
|
|
|
|
|
|
|
our $VERSION = '0.037'; # VERSION |
42
|
|
|
|
|
|
|
|
43
|
4
|
|
|
4
|
|
68694
|
use Mouse; |
|
4
|
|
|
|
|
24433
|
|
|
4
|
|
|
|
|
22
|
|
44
|
|
|
|
|
|
|
|
45
|
4
|
|
|
4
|
|
2932
|
use Clone qw(clone); |
|
4
|
|
|
|
|
8659
|
|
|
4
|
|
|
|
|
225
|
|
46
|
4
|
|
|
4
|
|
1436
|
use Try::Tiny; |
|
4
|
|
|
|
|
5392
|
|
|
4
|
|
|
|
|
193
|
|
47
|
4
|
|
|
4
|
|
26
|
use Carp; |
|
4
|
|
|
|
|
15
|
|
|
4
|
|
|
|
|
156
|
|
48
|
|
|
|
|
|
|
|
49
|
4
|
|
|
4
|
|
1772
|
use PomBase::Chobo::OntologyTerm; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
5972
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
has terms_by_id => (is => 'rw', init_arg => undef, isa => 'HashRef', |
53
|
|
|
|
|
|
|
default => sub { {} }); |
54
|
|
|
|
|
|
|
has terms_by_name => (is => 'rw', init_arg => undef, isa => 'HashRef', |
55
|
|
|
|
|
|
|
default => sub { {} }); |
56
|
|
|
|
|
|
|
has terms_by_cv_name => (is => 'rw', init_arg => undef, isa => 'HashRef', |
57
|
|
|
|
|
|
|
default => sub { {} }); |
58
|
|
|
|
|
|
|
has relationship_terms_by_cv_name => (is => 'rw', init_arg => undef, isa => 'HashRef', |
59
|
|
|
|
|
|
|
default => sub { {} }); |
60
|
|
|
|
|
|
|
has terms_by_db_name => (is => 'rw', init_arg => undef, isa => 'HashRef', |
61
|
|
|
|
|
|
|
default => sub { {} }); |
62
|
|
|
|
|
|
|
has metadata_by_namespace => (is => 'rw', init_arg => undef, isa => 'HashRef', |
63
|
|
|
|
|
|
|
default => sub { {} }); |
64
|
|
|
|
|
|
|
has _term_relationships => (is => 'rw', init_arg => undef, isa => 'HashRef', |
65
|
|
|
|
|
|
|
default => sub { {} }); |
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
|
|
|
|
|
|
|
{ |
79
|
11
|
|
|
11
|
1
|
28
|
my $self = shift; |
80
|
|
|
|
|
|
|
|
81
|
11
|
|
|
|
|
39
|
my %args = @_; |
82
|
|
|
|
|
|
|
|
83
|
11
|
|
|
|
|
30
|
my $metadata = $args{metadata}; |
84
|
11
|
|
|
|
|
21
|
my $terms = $args{terms}; |
85
|
|
|
|
|
|
|
|
86
|
11
|
|
|
|
|
46
|
my $terms_by_id = $self->terms_by_id(); |
87
|
11
|
|
|
|
|
34
|
my $terms_by_name = $self->terms_by_name(); |
88
|
11
|
|
|
|
|
27
|
my $terms_by_cv_name = $self->terms_by_cv_name(); |
89
|
11
|
|
|
|
|
28
|
my $relationship_terms_by_cv_name = $self->relationship_terms_by_cv_name(); |
90
|
|
|
|
|
|
|
|
91
|
11
|
|
|
|
|
29
|
my $metadata_by_namespace = $self->metadata_by_namespace(); |
92
|
|
|
|
|
|
|
|
93
|
11
|
|
|
|
|
30
|
for my $term (@$terms) { |
94
|
72
|
|
|
|
|
186
|
my @new_term_ids = ($term->{id}); |
95
|
|
|
|
|
|
|
|
96
|
72
|
|
|
|
|
160
|
push @new_term_ids, map { $_->{id}; } $term->alt_ids(); |
|
19
|
|
|
|
|
40
|
|
97
|
|
|
|
|
|
|
|
98
|
72
|
|
|
|
|
118
|
my @found_existing_terms = (); |
99
|
|
|
|
|
|
|
|
100
|
72
|
|
|
|
|
95
|
for my $id (@new_term_ids) { |
101
|
91
|
|
|
|
|
126
|
my $existing_term = $terms_by_id->{$id}; |
102
|
|
|
|
|
|
|
|
103
|
91
|
100
|
|
|
|
191
|
if (defined $existing_term) { |
104
|
6
|
100
|
|
|
|
20
|
if (!grep { $_ == $existing_term } @found_existing_terms) { |
|
2
|
|
|
|
|
8
|
|
105
|
4
|
|
|
|
|
10
|
push @found_existing_terms, $existing_term; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
72
|
50
|
|
|
|
127
|
if (@found_existing_terms > 1) { |
111
|
0
|
|
|
|
|
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
|
72
|
100
|
|
|
|
128
|
if (@found_existing_terms == 1) { |
117
|
4
|
|
|
|
|
10
|
my $existing_term = $found_existing_terms[0]; |
118
|
|
|
|
|
|
|
|
119
|
4
|
50
|
33
|
|
|
43
|
if (!$term->is_obsolete() && !$existing_term->is_obsolete()) { |
120
|
4
|
|
|
|
|
24
|
my $old_namespace = $existing_term->namespace(); |
121
|
|
|
|
|
|
|
|
122
|
4
|
|
|
|
|
24
|
$existing_term->merge($term); |
123
|
|
|
|
|
|
|
|
124
|
4
|
100
|
|
|
|
26
|
if ($old_namespace ne $existing_term->namespace()) { |
125
|
2
|
|
|
|
|
14
|
delete $self->terms_by_cv_name()->{$old_namespace}->{$existing_term->name()}; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
4
|
|
|
|
|
10
|
$term = $existing_term; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
72
|
|
|
|
|
138
|
for my $id_details ($term->alt_ids(), |
134
|
|
|
|
|
|
|
{ id => $term->{id}, |
135
|
|
|
|
|
|
|
db_name => $term->{db_name}, |
136
|
|
|
|
|
|
|
accession => $term->{accession}, |
137
|
|
|
|
|
|
|
} ) { |
138
|
93
|
|
|
|
|
246
|
$terms_by_id->{$id_details->{id}} = $term; |
139
|
|
|
|
|
|
|
|
140
|
93
|
|
|
|
|
299
|
$self->terms_by_db_name()->{$id_details->{db_name}}->{$id_details->{accession}} = $term; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
72
|
|
|
|
|
200
|
my $def = $term->def(); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
map { |
146
|
60
|
|
|
|
|
86
|
my $def_dbxref = $_; |
147
|
60
|
50
|
|
|
|
190
|
if ($def_dbxref =~ /^(.+?):(.*)/) { |
148
|
60
|
|
|
|
|
157
|
my ($def_db_name, $def_accession) = ($1, $2); |
149
|
60
|
|
|
|
|
192
|
$self->terms_by_db_name()->{$def_db_name}->{$def_accession} = $term; |
150
|
|
|
|
|
|
|
} else { |
151
|
0
|
|
|
|
|
0
|
die qq(can't parse dbxref from "def:" line: $def_dbxref); |
152
|
|
|
|
|
|
|
} |
153
|
72
|
|
|
|
|
91
|
} @{$def->{dbxrefs}}; |
|
72
|
|
|
|
|
137
|
|
154
|
|
|
|
|
|
|
|
155
|
72
|
|
|
|
|
151
|
my $name = $term->{name}; |
156
|
|
|
|
|
|
|
|
157
|
72
|
50
|
|
|
|
132
|
if (defined $name) { |
158
|
72
|
100
|
66
|
|
|
159
|
if (!exists $terms_by_name->{$name} || |
159
|
4
|
|
|
|
|
22
|
!grep { $_ == $term } @{$terms_by_name->{$name}}) { |
|
4
|
|
|
|
|
11
|
|
160
|
68
|
|
|
|
|
78
|
push @{$terms_by_name->{$name}}, $term; |
|
68
|
|
|
|
|
189
|
|
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} else { |
163
|
0
|
|
|
|
|
0
|
warn "term without a name tag ignored:\n", $term->to_string(), "\n\n"; |
164
|
0
|
|
|
|
|
0
|
next; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
72
|
|
|
|
|
155
|
my $term_namespace = $term->namespace(); |
168
|
|
|
|
|
|
|
|
169
|
72
|
50
|
|
|
|
139
|
if (defined $term_namespace) { |
170
|
72
|
|
|
|
|
125
|
my $existing_term_by_name = $terms_by_cv_name->{$term_namespace}->{$name}; |
171
|
72
|
50
|
66
|
|
|
154
|
if ($existing_term_by_name && $existing_term_by_name != $term) { |
172
|
|
|
|
|
|
|
warn qq(more than one Term with the name "$name" in namespace "$term_namespace" -\n) . |
173
|
|
|
|
|
|
|
"existing:\n" . $term->to_string() . "\n\nand:\n" . |
174
|
0
|
|
|
|
|
0
|
$terms_by_cv_name->{$term_namespace}->{$name}->to_string() . "\n\n"; |
175
|
|
|
|
|
|
|
} else { |
176
|
72
|
|
|
|
|
146
|
$terms_by_cv_name->{$term_namespace}->{$name} = $term; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
72
|
100
|
|
|
|
129
|
if ($term->{is_relationshiptype}) { |
180
|
10
|
|
|
|
|
17
|
$relationship_terms_by_cv_name->{$term_namespace}->{$name} = $term; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
72
|
100
|
|
|
|
136
|
if (!exists $metadata_by_namespace->{$term_namespace}) { |
184
|
12
|
|
|
|
|
156
|
$metadata_by_namespace->{$term_namespace} = clone $metadata; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
72
|
50
|
|
|
|
151
|
if ($term->{relationship}) { |
189
|
72
|
|
|
|
|
84
|
for my $rel (@{$term->{relationship}}) { |
|
72
|
|
|
|
|
163
|
|
190
|
|
|
|
|
|
|
my $key = $term->{id} . '<' . $rel->{relationship_name} . |
191
|
64
|
|
|
|
|
145
|
'>' . $rel->{other_term}; |
192
|
64
|
|
|
|
|
203
|
$self->_term_relationships()->{$key} = 1; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub get_terms_by_name |
199
|
|
|
|
|
|
|
{ |
200
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
201
|
1
|
|
|
|
|
2
|
my $name = shift; |
202
|
|
|
|
|
|
|
|
203
|
1
|
|
50
|
|
|
2
|
return @{$self->terms_by_name()->{$name} // []}; |
|
1
|
|
|
|
|
10
|
|
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub get_term_by_id |
207
|
|
|
|
|
|
|
{ |
208
|
35
|
|
|
35
|
0
|
15901
|
my $self = shift; |
209
|
35
|
|
|
|
|
45
|
my $id = shift; |
210
|
|
|
|
|
|
|
|
211
|
35
|
|
|
|
|
105
|
return $self->terms_by_id()->{$id}; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub get_cv_names |
215
|
|
|
|
|
|
|
{ |
216
|
22
|
|
|
22
|
0
|
42
|
my $self = shift; |
217
|
|
|
|
|
|
|
|
218
|
22
|
|
|
|
|
38
|
return keys %{$self->terms_by_cv_name()}; |
|
22
|
|
|
|
|
142
|
|
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub get_terms_by_cv_name |
222
|
|
|
|
|
|
|
{ |
223
|
21
|
|
|
21
|
0
|
1600
|
my $self = shift; |
224
|
21
|
|
|
|
|
35
|
my $cv_name = shift; |
225
|
|
|
|
|
|
|
|
226
|
21
|
|
|
|
|
36
|
return values %{$self->terms_by_cv_name()->{$cv_name}}; |
|
21
|
|
|
|
|
150
|
|
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub get_db_names |
230
|
|
|
|
|
|
|
{ |
231
|
5
|
|
|
5
|
0
|
1270
|
my $self = shift; |
232
|
|
|
|
|
|
|
|
233
|
5
|
|
|
|
|
8
|
return keys %{$self->terms_by_db_name()}; |
|
5
|
|
|
|
|
33
|
|
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub accessions_by_db_name |
237
|
|
|
|
|
|
|
{ |
238
|
8
|
|
|
8
|
0
|
10
|
my $self = shift; |
239
|
8
|
|
|
|
|
11
|
my $db_name = shift; |
240
|
|
|
|
|
|
|
|
241
|
8
|
|
|
|
|
11
|
return sort keys %{$self->terms_by_db_name()->{$db_name}}; |
|
8
|
|
|
|
|
44
|
|
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub get_terms |
245
|
|
|
|
|
|
|
{ |
246
|
19
|
|
|
19
|
0
|
748
|
my $self = shift; |
247
|
|
|
|
|
|
|
|
248
|
19
|
|
|
|
|
77
|
return map { $self->get_terms_by_cv_name($_); } $self->get_cv_names(); |
|
20
|
|
|
|
|
61
|
|
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub get_namespaces |
252
|
|
|
|
|
|
|
{ |
253
|
3
|
|
|
3
|
0
|
3284
|
my $self = shift; |
254
|
|
|
|
|
|
|
|
255
|
3
|
|
|
|
|
7
|
return keys %{$self->metadata_by_namespace()}; |
|
3
|
|
|
|
|
25
|
|
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub get_metadata_by_namespace |
259
|
|
|
|
|
|
|
{ |
260
|
3
|
|
|
3
|
0
|
7
|
my $self = shift; |
261
|
3
|
|
|
|
|
9
|
my $namespace = shift; |
262
|
|
|
|
|
|
|
|
263
|
3
|
|
|
|
|
18
|
return $self->metadata_by_namespace()->{$namespace}; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
sub relationships |
267
|
|
|
|
|
|
|
{ |
268
|
6
|
|
|
6
|
0
|
1568
|
my $self = shift; |
269
|
|
|
|
|
|
|
|
270
|
6
|
100
|
|
|
|
41
|
if ($self->{_relationships}) { |
271
|
2
|
|
|
|
|
4
|
return @{$self->{_relationships}} |
|
2
|
|
|
|
|
10
|
|
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
$self->{_relationships} = [map { |
275
|
32
|
|
|
|
|
112
|
my ($subject_id, $rel_name, $object_id) = /(.*)<(.*)>(.*)/; |
276
|
|
|
|
|
|
|
|
277
|
32
|
|
|
|
|
62
|
my $object_term = $self->get_term_by_id($object_id); |
278
|
|
|
|
|
|
|
|
279
|
32
|
50
|
|
|
|
53
|
if (!$object_term) { |
280
|
0
|
|
|
|
|
0
|
my $subject_term = $self->get_term_by_id($subject_id); |
281
|
|
|
|
|
|
|
warn qq(ignoring relation where object isn't defined: "$object_id" line ) . |
282
|
|
|
|
|
|
|
$subject_term->{source_file_line_number} . ' of ' . |
283
|
0
|
|
|
|
|
0
|
$subject_term->{source_file} . "\n"; |
284
|
0
|
|
|
|
|
0
|
(); |
285
|
|
|
|
|
|
|
} else { |
286
|
32
|
|
|
|
|
87
|
[$subject_id, $rel_name, $object_id]; |
287
|
|
|
|
|
|
|
} |
288
|
4
|
|
|
|
|
12
|
} sort keys %{$self->_term_relationships()}]; |
|
4
|
|
|
|
|
40
|
|
289
|
|
|
|
|
|
|
|
290
|
4
|
|
|
|
|
9
|
return @{$self->{_relationships}}; |
|
4
|
|
|
|
|
18
|
|
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head2 finish |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
Usage : $self->finish(); |
296
|
|
|
|
|
|
|
Function: remove namespaces that are empty due to merging and check that |
297
|
|
|
|
|
|
|
objects and subjects of relationships exist |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=cut |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
sub finish |
302
|
|
|
|
|
|
|
{ |
303
|
3
|
|
|
3
|
1
|
11
|
my $self = shift; |
304
|
|
|
|
|
|
|
|
305
|
3
|
|
|
|
|
11
|
my @relationships = $self->relationships(); |
306
|
|
|
|
|
|
|
|
307
|
3
|
50
|
|
|
|
21
|
if (@relationships == 0) { |
308
|
0
|
|
|
|
|
0
|
warn "note: no relationships read\n"; |
309
|
|
|
|
|
|
|
} |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
# find and remove namespaces that are empty due to merging |
312
|
|
|
|
|
|
|
my @empty_namespaces = |
313
|
|
|
|
|
|
|
map { |
314
|
4
|
50
|
|
|
|
7
|
if (scalar(keys %{$self->terms_by_cv_name()->{$_}}) == 0) { |
|
4
|
|
|
|
|
22
|
|
315
|
0
|
|
|
|
|
0
|
$_; |
316
|
|
|
|
|
|
|
} else { |
317
|
4
|
|
|
|
|
8
|
(); |
318
|
|
|
|
|
|
|
} |
319
|
3
|
|
|
|
|
7
|
} keys %{$self->terms_by_cv_name()}; |
|
3
|
|
|
|
|
15
|
|
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
map { |
322
|
3
|
|
|
|
|
10
|
delete $self->terms_by_cv_name()->{$_}; |
|
0
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
} @empty_namespaces; |
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
1; |