EnsEMBL/DBSQL/OntologyTermAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 297 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 434 CC 29 SCAR 28.7
CC
29
Cov
0%
fetch_all_by_descendant_term
56.0
fetch_all_by_name
20.0
fetch_all_by_parent_term
20.0
CRAP 17.7
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::OntologyTermAdaptor
34  
35 =head1 SYNOPSIS
36  
37 my $goa =
38 $registry->get_adaptor( 'Multi', 'Ontology', 'OntologyTerm' );
39  
40 my $term = $goa->fetch_by_accession('GO:0010885');
41  
42 my @children = @{ $goa->fetch_all_by_parent_term($term) };
43 my @descendants = @{ $goa->fetch_all_by_ancestor_term($term) };
44  
45 my @parents = @{ $goa->fetch_all_by_child_term($term) };
46 my @ancestors = @{ $goa->fetch_all_by_descendant_term($term) };
47  
48 my %ancestor_chart = %{ $goa->_fetch_ancestor_chart($term) };
49  
50 =head1 DESCRIPTION
51  
52 An abstract adaptor class for fetching ontology
53 terms, creates Bio::EnsEMBL::OntologyTerm objects.
54  
55 =head1 METHODS
56  
57 =cut
58  
59 package Bio::EnsEMBL::DBSQL::OntologyTermAdaptor;
60 $Bio::EnsEMBL::DBSQL::OntologyTermAdaptor::VERSION = '114.0.0';
61 use strict;
62 use warnings;
63  
64 use DBI qw( :sql_types );
65  
66 use Bio::EnsEMBL::Utils::Exception qw( throw );
67 use Bio::EnsEMBL::Utils::Scalar qw( assert_ref );
68  
69 use Bio::EnsEMBL::OntologyTerm;
70  
71 use base qw( Bio::EnsEMBL::DBSQL::BaseAdaptor );
72  
73 =head2 fetch_all_by_name
74  
75 Arg [1] : String, name of term, or SQL pattern
76 Arg [2] : (optional) String, name of ontology
77 Arg [3] : (optional) Boolean, search through obsolete terms as well
78  
79 Description : Fetches ontology term(s) given a name, a synonym, or a
80 SQL pattern like "%splice_site%"
81  
82 Example :
83  
84 my ($term) =
85 @{ $ot_adaptor->fetch_by_name( 'DNA_binding_site', 'SO' ) };
86  
87 # Will find terms in both SO and GO:
88 my @terms = @{ $ot_adaptor->fetch_by_name('%splice_site%') };
89  
90 Return type : listref of Bio::EnsEMBL::OntologyTerm
91  
92 =cut
93  
94 sub fetch_all_by_name {
95 my ( $this, $pattern, $ontology, $include_obsolete ) = @_;
96  
97 my $statement = q(
98 SELECT DISTINCT
99 term.term_id,
100 term.accession,
101 term.name,
102 term.definition,
103 term.subsets,
104 term.is_root,
105 term.is_obsolete,
106 ontology.name,
107 ontology.namespace,
108 ontology.data_version
109 FROM ontology
110 JOIN term USING (ontology_id)
111 LEFT JOIN synonym USING (term_id)
112 WHERE ( term.name LIKE ? OR synonym.name LIKE ? ));
113  
114 if ( defined($ontology) ) {
115 $statement .= " AND ontology.name = ?";
116 }
117 $statement .= " AND term.is_obsolete = 0" unless $include_obsolete;
118  
119 my $sth = $this->prepare($statement);
120 $sth->bind_param( 1, $pattern, SQL_VARCHAR );
121 $sth->bind_param( 2, $pattern, SQL_VARCHAR );
122  
123 if ( defined($ontology) ) {
124 $sth->bind_param( 3, $ontology, SQL_VARCHAR );
125 }
126  
127 $sth->execute();
128  
129 my ( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $namespace, $ontology_version );
130 $sth->bind_columns(
131 \( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version ) );
132  
133 my @terms;
134  
135 while ( $sth->fetch() ) {
136 $subsets ||= '';
137  
138 push @terms,
139 Bio::EnsEMBL::OntologyTerm->new(
140 '-dbid' => $dbid,
141 '-adaptor' => $this,
142 '-accession' => $accession,
143 '-is_root' => $is_root,
144 '-is_obsolete' => $is_obsolete,
145 '-ontology' => $ontology,
146 '-ontology_version' => $ontology_version,
147 '-namespace' => $namespace,
148 '-subsets' => [ split( /,/, $subsets ) ],
149 '-name' => $name,
150 '-definition' => $definition,
151 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
152 );
153  
154 }
155  
156 return \@terms;
157 } ## end sub fetch_all_by_name
158  
159  
160 =head2 fetch_by_accession
161  
162 Arg [1] : String
163 Arg [2] : (optional) Boolean, search through obsolete terms as well
164  
165 Description : Fetches an ontology term given an accession.
166  
167 Example :
168  
169 my $term = $ot_adaptor->fetch_by_accession('GO:0030326');
170  
171 Return type : Bio::EnsEMBL::OntologyTerm
172  
173 =cut
174  
175 sub fetch_by_accession {
176 my ( $this, $accession, $include_obsolete ) = @_;
177  
178 my $statement = q(
179 SELECT term.term_id,
180 term.accession,
181 term.name,
182 term.definition,
183 term.subsets,
184 term.is_root,
185 term.is_obsolete,
186 ontology.name,
187 ontology.namespace,
188 ontology.data_version
189 FROM ontology
190 JOIN term USING (ontology_id)
191 WHERE term.accession = ?);
192  
193 $statement .= " AND term.is_obsolete = 0" unless $include_obsolete;
194  
195 my $sth = $this->prepare($statement);
196 $sth->bind_param( 1, $accession, SQL_VARCHAR );
197  
198 $sth->execute();
199  
200 my ( $dbid, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version );
201 $sth->bind_columns(
202 \( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version ) );
203  
204 $sth->fetch();
205 $sth->finish();
206  
207 my $term;
208 if (!$dbid) {
209 $term = $this->fetch_by_alt_id($accession);
210 } else {
211 $subsets ||= '';
212  
213 $term =
214 Bio::EnsEMBL::OntologyTerm->new(
215 '-dbid' => $dbid,
216 '-adaptor' => $this,
217 '-accession' => $accession,
218 '-is_root' => $is_root,
219 '-is_obsolete'=> $is_obsolete,
220 '-ontology' => $ontology,
221 '-ontology_version' => $ontology_version,
222 '-namespace' => $namespace,
223 '-subsets' => [ split( /,/, $subsets ) ],
224 '-name' => $name,
225 '-definition' => $definition,
226 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
227 );
228 }
229  
230 return $term;
231 } ## end sub fetch_by_accession
232  
233  
234 =head2 fetch_by_alt_id
235  
236 Arg [1] : String
237  
238 Description : Fetches an ontology term given an alt_id.
239  
240 Example :
241  
242 my $term = $ot_adaptor->fetch_by_alt_id('GO:0019952');
243  
244 Return type : Bio::EnsEMBL::OntologyTerm
245  
246 =cut
247  
248 sub fetch_by_alt_id {
249 my ( $this, $accession ) = @_;
250  
251 my $statement = q(
252 SELECT term.term_id,
253 alt_id.accession,
254 term.name,
255 term.definition,
256 term.subsets,
257 term.is_root,
258 term.is_obsolete,
259 ontology.name,
260 ontology.namespace,
261 ontology.data_version
262 FROM ontology
263 JOIN term USING (ontology_id)
264 JOIN alt_id USING (term_id)
265 WHERE alt_id.accession = ?);
266  
267 my $sth = $this->prepare($statement);
268 $sth->bind_param( 1, $accession, SQL_VARCHAR );
269  
270 $sth->execute();
271  
272 my ( $dbid, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version );
273 $sth->bind_columns(
274 \( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version ) );
275  
276 $sth->fetch();
277  
278 # early exit in the event of bad $accession
279 unless ($dbid) {return;}
280  
281 $subsets ||= '';
282 my $term =
283 Bio::EnsEMBL::OntologyTerm->new(
284 '-dbid' => $dbid,
285 '-adaptor' => $this,
286 '-accession' => $accession,
287 '-ontology' => $ontology,
288 '-ontology_version' => $ontology_version,
289 '-namespace' => $namespace,
290 '-subsets' => [ split( /,/, $subsets ) ],
291 '-name' => $name,
292 '-definition' => $definition,
293 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
294 );
295 $sth->finish();
296  
297 return $term;
298 } ## end sub fetch_by_alt_id
299  
300  
301  
302 =head2 fetch_all_by_parent_term
303  
304 Arg [1] : Bio::EnsEMBL::OntologyTerm
305 The term whose children terms should be fetched.
306  
307 Description : Given a parent ontology term, returns a list of
308 its immediate children terms.
309  
310 Example :
311  
312 my @children =
313 @{ $ot_adaptor->fetch_all_by_parent_term($term) };
314  
315 Return type : listref of Bio::EnsEMBL::OntologyTerm
316  
317 =cut
318  
319 sub fetch_all_by_parent_term {
320 my ( $this, $term, $ontology, $include_obsolete ) = @_;
321  
322 assert_ref( $term, 'Bio::EnsEMBL::OntologyTerm' );
323  
324 my @terms;
325  
326 if ( !$term->{'child_terms_fetched'} ) {
327 my $statement = q(
328 SELECT child_term.term_id,
329 child_term.accession,
330 child_term.name,
331 child_term.definition,
332 child_term.subsets,
333 child_term.is_root,
334 child_term.is_obsolete,
335 rt.name,
336 ontology.data_version
337 FROM term child_term
338 JOIN relation ON (relation.child_term_id = child_term.term_id)
339 JOIN relation_type rt USING (relation_type_id)
340 JOIN ontology ON (ontology.ontology_id = relation.ontology_id)
341 WHERE relation.parent_term_id = ?
342 AND ontology.name = ?);
343  
344 $statement .= " AND child_term.is_obsolete = 0" unless $include_obsolete;
345  
346 my $sth = $this->prepare($statement);
347 $sth->bind_param( 1, $term->dbID(), SQL_INTEGER );
348  
349 if (!defined $ontology) {
350 $ontology = $term->{'ontology'};
351 }
352 $sth->bind_param( 2, $ontology, SQL_VARCHAR );
353  
354  
355 $sth->execute();
356  
357 my ( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $relation, $ontology_version );
358 $sth->bind_columns(
359 \( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $relation, $ontology_version ) );
360  
361 while ( $sth->fetch() ) {
362 $subsets ||= '';
363  
364 my $child_term =
365 Bio::EnsEMBL::OntologyTerm->new(
366 '-dbid' => $dbid,
367 '-adaptor' => $this,
368 '-accession' => $accession,
369 '-is_root' => $is_root,
370 '-is_obsolete' => $is_obsolete,
371 '-ontology' => $term->{'ontology'},
372 '-ontology_version' => $ontology_version,
373 '-namespace' => $term->{'namespace'},
374 '-subsets' => [ split( /,/, $subsets ) ],
375 '-name' => $name,
376 '-definition' => $definition,
377 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
378 );
379  
380 push( @terms, $child_term );
381 push( @{ $term->{'children'}{$relation} }, $child_term );
382 }
383  
384 $term->{'child_terms_fetched'} = 1;
385 } else {
386 foreach my $relation ( values( %{ $term->{'children'} } ) ) {
387 push( @terms, @{$relation} );
388 }
389 }
390  
391 return \@terms;
392 } ## end sub fetch_all_by_parent_term
393  
394 =head2 fetch_all_by_ancestor_term
395  
396 Arg [1] : Bio::EnsEMBL::OntologyTerm
397 The term whose descendant terms should be fetched.
398  
399 Description : Given a parent ontology term, returns a list of
400 all its descendant terms, down to and including
401 any leaf terms. Relations of the type 'is_a' and
402 'part_of' are followed.
403  
404 Example :
405  
406 my @descendants =
407 @{ $ot_adaptor->fetch_all_by_ancestor_term($term) };
408  
409 Return type : listref of Bio::EnsEMBL::OntologyTerm
410  
411 =cut
412  
413 sub fetch_all_by_ancestor_term {
414 my ( $this, $term, $ontology ) = @_;
415  
416 assert_ref( $term, 'Bio::EnsEMBL::OntologyTerm' );
417  
418 my $statement = q(
419 SELECT DISTINCT
420 child_term.term_id,
421 child_term.accession,
422 child_term.name,
423 child_term.definition,
424 child_term.subsets,
425 child_term.is_root,
426 child_term.is_obsolete,
427 ontology.data_version,
428 closure.distance
429 FROM term child_term
430 JOIN closure ON (closure.child_term_id = child_term.term_id)
431 JOIN ontology ON (closure.ontology_id = ontology.ontology_id)
432 WHERE closure.parent_term_id = ?
433 AND closure.distance > 0
434 AND closure.ontology_id = child_term.ontology_id
435 AND ontology.name = ?
436 ORDER BY closure.distance, child_term.accession);
437  
438 my $sth = $this->prepare($statement);
439 $sth->bind_param( 1, $term->dbID(), SQL_INTEGER );
440 if (!defined $ontology) {
441 $ontology = $term->{'ontology'};
442 }
443 $sth->bind_param( 2, $ontology, SQL_VARCHAR );
444 $sth->execute();
445  
446 my ( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology_version, $closure_distance );
447 $sth->bind_columns(
448 \( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology_version, $closure_distance ) );
449  
450 my @terms;
451  
452 while ( $sth->fetch() ) {
453 $subsets ||= '';
454  
455 push( @terms,
456 Bio::EnsEMBL::OntologyTerm->new(
457 '-dbid' => $dbid,
458 '-adaptor' => $this,
459 '-accession' => $accession,
460 '-is_root' => $is_root,
461 '-is_obsolete' => $is_obsolete,
462 '-ontology' => $term->{'ontology'},
463 '-ontology_version' => $ontology_version,
464 '-namespace' => $term->{'namespace'},
465 '-subsets' => [ split( /,/, $subsets ) ],
466 '-name' => $name,
467 '-definition' => $definition,
468 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
469 ) );
470 }
471  
472 return \@terms;
473 } ## end sub fetch_all_by_ancestor_term
474  
475 =head2 fetch_all_by_child_term
476  
477 Arg [1] : Bio::EnsEMBL::OntologyTerm
478 The term whose parent terms should be fetched.
479  
480 Description : Given a child ontology term, returns a list of
481 its immediate parent terms.
482  
483 Example :
484  
485 my @parents = @{ $ot_adaptor->fetch_all_by_child_term($term) };
486  
487 Return type : listref of Bio::EnsEMBL::OntologyTerm
488  
489 =cut
490  
491 sub fetch_all_by_child_term {
492 my ( $this, $term, $ontology ) = @_;
493  
494 assert_ref( $term, 'Bio::EnsEMBL::OntologyTerm' );
495  
496 my @terms;
497  
498 if ( !$term->{'parent_terms_fetched'} ) {
499 my $statement = q(
500 SELECT parent_term.term_id,
501 parent_term.accession,
502 parent_term.name,
503 parent_term.definition,
504 parent_term.subsets,
505 parent_term.is_root,
506 parent_term.is_obsolete,
507 rt.name,
508 ontology.data_version
509 FROM term parent_term
510 JOIN relation ON (relation.parent_term_id = parent_term.term_id)
511 JOIN relation_type rt USING (relation_type_id)
512 JOIN ontology ON (ontology.ontology_id = relation.ontology_id)
513 WHERE relation.child_term_id = ?
514 AND ontology.name = ?);
515  
516 my $sth = $this->prepare($statement);
517 $sth->bind_param( 1, $term->dbID(), SQL_INTEGER );
518  
519 if (!defined $ontology) {
520 $ontology = $term->{'ontology'};
521 }
522 $sth->bind_param( 2, $ontology, SQL_VARCHAR );
523  
524 $sth->execute();
525  
526 my ( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $relation, $ontology_version );
527 $sth->bind_columns(
528 \( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $relation, $ontology_version ) );
529  
530 while ( $sth->fetch() ) {
531 $subsets ||= '';
532  
533 my $parent_term =
534 Bio::EnsEMBL::OntologyTerm->new(
535 '-dbid' => $dbid,
536 '-adaptor' => $this,
537 '-accession' => $accession,
538 '-is_root' => $is_root,
539 '-is_obsolete' => $is_obsolete,
540 '-ontology' => $term->{'ontology'},
541 '-ontology_version' => $ontology_version,
542 '-namespace' => $term->{'namespace'},
543 '-subsets' => [ split( /,/, $subsets ) ],
544 '-name' => $name,
545 '-definition' => $definition,
546 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
547 );
548  
549 push( @terms, $parent_term );
550 push( @{ $term->{'parents'}{$relation} }, $parent_term );
551 }
552  
553 $term->{'parent_terms_fetched'} = 1;
554 } else {
555 foreach my $relation ( values( %{ $term->{'parents'} } ) ) {
556 push( @terms, @{$relation} );
557 }
558 }
559  
560 return \@terms;
561 } ## end sub fetch_all_by_child_term
562  
563 =head2 fetch_all_by_descendant_term
564  
565 Arg [1] : Bio::EnsEMBL::OntologyTerm
566 The term whose ancestor terms should be fetched.
567  
568 Arg [2] : (optional) String
569 The subset within the ontolgy to which the query
570 should be restricted. The subset may be specified as
571 a SQL pattern, e.g., "%goslim%" (but "goslim%" might
572 not do what you expect), or as a specific subset name,
573 e.g., "goslim_generic".
574  
575 Arg [3] : (optional) Boolean
576 If true (non-zero), only return the closest
577 term(s). If this argument is true, and the
578 previous argument is left undefined, this method
579 will return the parent(s) of the given term.
580
581 Arg [4] : (optional) Boolean
582 If true we will allow the retrieval of terms whose distance
583 to the current term is 0. If false then we will only return
584 those which are above the current term in the ontology
585  
586 Description : Given a child ontology term, returns a list of
587 all its ancestor terms, up to and including any
588 root term. Relations of the type 'is_a' and
589 'part_of' are followed. Optionally, only terms in
590 a given subset of the ontology may be returned,
591 and additionally one may ask to only get the
592 closest term(s) to the given child term.
593  
594 Example :
595  
596 my @ancestors =
597 @{ $ot_adaptor->fetch_all_by_descendant_term($term) };
598  
599 Return type : listref of Bio::EnsEMBL::OntologyTerm
600  
601 =cut
602  
603 sub fetch_all_by_descendant_term {
604 my ( $this, $term, $subset, $closest_only, $allow_zero_distance, $ontology ) = @_;
605  
606 assert_ref( $term, 'Bio::EnsEMBL::OntologyTerm' );
607  
608 $closest_only ||= 0;
609  
610 my $statement = q(
611 SELECT DISTINCT
612 parent_term.term_id,
613 parent_term.accession,
614 parent_term.name,
615 parent_term.definition,
616 parent_term.subsets,
617 parent_term.is_root,
618 parent_term.is_obsolete,
619 closure.distance,
620 ontology.data_version
621 FROM term parent_term
622 JOIN closure ON (closure.parent_term_id = parent_term.term_id)
623 JOIN ontology ON (closure.ontology_id = ontology.ontology_id)
624 WHERE closure.child_term_id = ?
625 AND closure.distance > ?
626 AND closure.ontology_id = parent_term.ontology_id
627 AND ontology.name = ?);
628  
629 if ( defined($subset) ) {
630 if ( index( $subset, '%' ) != -1 ) {
631 $statement .= q(
632 AND parent_term.subsets LIKE ?);
633 } else {
634 $statement .= q(
635 AND FIND_IN_SET(?, parent_term.subsets) > 0);
636 }
637 }
638  
639 $statement .= q(
640 ORDER BY closure.distance, parent_term.accession);
641  
642 my $sth = $this->prepare($statement);
643 $sth->bind_param( 1, $term->dbID(), SQL_INTEGER );
644 my $query_distance = ($allow_zero_distance) ? -1 : 0;
645 $sth->bind_param( 2, $query_distance, SQL_INTEGER );
646 if (!defined $ontology) {
647 $ontology = $term->{'ontology'};
648 }
649 $sth->bind_param( 3, $ontology, SQL_VARCHAR );
650  
651 if ( defined($subset) ) {
652 $sth->bind_param( 4, $subset, SQL_VARCHAR );
653 }
654  
655 $sth->execute();
656  
657 my ( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $distance, $ontology_version );
658 $sth->bind_columns(
659 \( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $distance, $ontology_version ) );
660  
661 my @terms;
662 my $min_distance;
663  
664 while ( $sth->fetch() ) {
665 $subsets ||= '';
666 $min_distance ||= $distance;
667  
668 if ( !$closest_only || $distance == $min_distance ) {
669 push( @terms,
670 Bio::EnsEMBL::OntologyTerm->new(
671 '-dbid' => $dbid,
672 '-adaptor' => $this,
673 '-accession' => $accession,
674 '-is_root' => $is_root,
675 '-is_obsolete' => $is_obsolete,
676 '-ontology' => $term->{'ontology'},
677 '-ontology_version' => $ontology_version,
678 '-namespace' => $term->{'namespace'},
679 '-subsets' => [ split( /,/, $subsets ) ],
680 '-name' => $name,
681 '-definition' => $definition,
682 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
683 ) );
684 } else {
685 $sth->finish();
686 last;
687 }
688 }
689  
690 return \@terms;
691 } ## end sub fetch_all_by_descendant_term
692  
693 sub _fetch_synonyms_by_dbID {
694 my ( $this, $dbID ) = @_;
695  
696 my $statement = q(
697 SELECT synonym.name
698 FROM synonym
699 WHERE synonym.term_id = ?);
700  
701 my $sth = $this->prepare($statement);
702 $sth->bind_param( 1, $dbID, SQL_INTEGER );
703  
704 $sth->execute();
705  
706 my $synonym;
707 $sth->bind_col( 1, \$synonym );
708  
709 my @synonyms;
710 while ( $sth->fetch() ) {
711 push( @synonyms, $synonym );
712 }
713  
714 return \@synonyms;
715 }
716  
717  
718  
719 =head2 _fetch_ancestor_chart
720  
721 Arg [1] : Bio::EnsEMBL::OntologyTerm
722 The term whose ancestor terms should be fetched.
723  
724 Description : Given a child ontology term, returns a hash
725 structure containing its ancestor terms, up to and
726 including any root term. Relations of the type
727 'is_a' and 'part_of' are included.
728  
729 Example :
730  
731 my %chart = %{ $ot_adaptor->_fetch_ancestor_chart($term) };
732  
733 Return type : A reference to a hash structure like this:
734  
735 {
736 'GO:XXXXXXX' => {
737 'term' => # ref to Bio::EnsEMBL::OntologyTerm object
738 'is_a' => [...], # listref of Bio::EnsEMBL::OntologyTerm
739 'part_of' => [...], # listref of Bio::EnsEMBL::OntologyTerm
740 },
741 'GO:YYYYYYY' => {
742 # Similarly for all ancestors,
743 # and including the query term itself.
744 }
745 }
746  
747 =cut
748  
749 sub _fetch_ancestor_chart {
750 my ( $this, $term, $ontology ) = @_;
751  
752 assert_ref( $term, 'Bio::EnsEMBL::OntologyTerm' );
753  
754 my $statement = q(
755 SELECT subparent_term.term_id,
756 parent_term.term_id,
757 relation_type.name
758 FROM closure
759 JOIN relation
760 ON (relation.parent_term_id = closure.parent_term_id
761 AND relation.child_term_id = closure.subparent_term_id
762 AND closure.ontology_id = relation.ontology_id)
763 JOIN relation_type USING (relation_type_id)
764 JOIN term subparent_term
765 ON (subparent_term.term_id = closure.subparent_term_id)
766 JOIN term parent_term ON (parent_term.term_id = closure.parent_term_id)
767 JOIN ontology ON (ontology.ontology_id = closure.ontology_id)
768 WHERE closure.child_term_id = ?
769 AND ontology.name = ?
770 ORDER BY closure.distance);
771  
772 my $sth = $this->prepare($statement);
773 $sth->bind_param( 1, $term->dbID(), SQL_INTEGER );
774 if (!defined $ontology) {
775 $ontology = $term->{'ontology'};
776 }
777 $sth->bind_param( 2, $ontology, SQL_VARCHAR );
778  
779 $sth->execute();
780  
781 my ( $subparent_id, $parent_id, $relation );
782 $sth->bind_columns( \( $subparent_id, $parent_id, $relation ) );
783  
784 my %id_chart;
785 my %acc_chart;
786  
787 while ( $sth->fetch() ) {
788 if ( !exists( $id_chart{$parent_id} ) ) {
789 $id_chart{$parent_id} = {};
790 }
791 push( @{ $id_chart{$subparent_id}{$relation} }, $parent_id );
792 }
793  
794 my @terms = @{ $this->fetch_all_by_dbID_list( [ keys(%id_chart) ] ) };
795  
796 foreach my $term (@terms) {
797 $id_chart{ $term->dbID() }{'term'} = $term;
798 $acc_chart{ $term->accession() }{'term'} = $term;
799 }
800  
801 foreach my $term (@terms) {
802 my $accession = $term->accession();
803 my $dbID = $term->dbID();
804  
805 foreach my $relation ( keys( %{ $id_chart{$dbID} } ) ) {
806 if ( $relation eq 'term' ) { next }
807  
808 foreach my $id ( @{ $id_chart{$dbID}{$relation} } ) {
809 push( @{ $acc_chart{$accession}{$relation} },
810 $id_chart{$id}{'term'} );
811 }
812 }
813 }
814  
815 return \%acc_chart;
816 } ## end sub _fetch_ancestor_chart
817  
818 #-----------------------------------------------------------------------
819 # Useful public methods that implement functionality not properly
820 # provided by the parent class Bio::EnsEMBL::DBSQL::BaseAdaptor.
821  
822 sub fetch_by_dbID {
823 my ( $this, $dbid, $include_obsolete ) = @_;
824  
825 my $statement = q(
826 SELECT term.term_id,
827 term.accession,
828 term.name,
829 term.definition,
830 term.subsets,
831 term.is_root,
832 term.is_obsolete,
833 ontology.name,
834 ontology.namespace,
835 ontology.data_version
836 FROM ontology
837 JOIN term USING (ontology_id)
838 WHERE term.term_id = ?);
839  
840 $statement .= " AND term.is_obsolete = 0" unless $include_obsolete;
841  
842 my $sth = $this->prepare($statement);
843 $sth->bind_param( 1, $dbid, SQL_INTEGER );
844  
845 $sth->execute();
846  
847 my ( $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology,
848 $namespace, $ontology_version );
849 $sth->bind_columns(
850 \( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version
851 ) );
852  
853 $sth->fetch();
854
855 unless ($accession) {return;}
856
857 $subsets ||= '';
858  
859 my $term =
860 Bio::EnsEMBL::OntologyTerm->new(
861 '-dbid' => $dbid,
862 '-adaptor' => $this,
863 '-accession' => $accession,
864 '-is_root' => $is_root,
865 '-is_obsolete' => $is_obsolete,
866 '-ontology' => $ontology,
867 '-ontology_version' => $ontology_version,
868 '-namespace' => $namespace,
869 '-subsets' => [ split( /,/, $subsets ) ],
870 '-name' => $name,
871 '-definition' => $definition,
872 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
873 );
874 $sth->finish();
875  
876 return $term;
877 } ## end sub fetch_by_dbID
878  
879 sub fetch_all_by_dbID_list {
880 my ( $this, $dbids, $include_obsolete ) = @_;
881  
882 if ( !@{$dbids} ) { return [] }
883  
884 my $stmt = q(
885 SELECT term.term_id,
886 term.accession,
887 term.name,
888 term.definition,
889 term.subsets,
890 term.is_root,
891 term.is_obsolete,
892 ontology.name,
893 ontology.namespace,
894 ontology.data_version
895 FROM ontology
896 JOIN term USING (ontology_id)
897 WHERE term.term_id IN (%s));
898  
899 my $statement = sprintf(
900 $stmt,
901 join(
902 ',',
903 map {
904 $this->dbc()->db_handle()->quote( $_, SQL_INTEGER )
905 } @{$dbids} ) );
906  
907 $statement .= " AND term.is_obsolete = 0" unless $include_obsolete;
908  
909 my $sth = $this->prepare($statement);
910  
911 $sth->execute();
912  
913 my ( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology,
914 $namespace, $ontology_version );
915 $sth->bind_columns( \( $dbid, $accession, $name, $definition,
916 $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version ) );
917  
918 my @terms;
919  
920 while ( $sth->fetch() ) {
921 $subsets ||= '';
922  
923 push( @terms,
924 Bio::EnsEMBL::OntologyTerm->new(
925 '-dbid' => $dbid,
926 '-adaptor' => $this,
927 '-accession' => $accession,
928 '-is_root' => $is_root,
929 '-is_obsolete' => $is_obsolete,
930 '-ontology' => $ontology,
931 '-ontology_version' => $ontology_version,
932 '-namespace' => $namespace,
933 '-subsets' => [ split( /,/, $subsets ) ],
934 '-name' => $name,
935 '-definition' => $definition,
936 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
937 ) );
938 }
939  
940 return \@terms;
941 } ## end sub fetch_all_by_dbID_list
942  
943  
944 =head2 fetch_all_alt_ids
945  
946 Arg [1] : String
947  
948 Description : Fetches all alt_ids for a given ontology term
949  
950 Example :
951  
952 my ($accessions) =
953 @{ $ot_adaptor->fetch_all_alt_ids( 'GO:0000003' ) };
954  
955 Return type : listref of accessions
956  
957 =cut
958 sub fetch_all_alt_ids {
959 my ($this, $accession) = @_;
960  
961 my $statement = q(
962 SELECT alt_id.accession
963 FROM term
964 JOIN alt_id USING (term_id)
965 WHERE term.accession = ?);
966  
967 my $sth = $this->prepare($statement);
968 $sth->bind_param(1, $accession, SQL_VARCHAR);
969 $sth->execute();
970  
971 my (@accessions, $alt_id);
972 $sth->bind_columns( \$alt_id);
973  
974 while ( $sth->fetch() ) {
975 push( @accessions, $alt_id);
976 }
977  
978 return \@accessions;
979 }
980  
981  
982  
983  
984 =head2 fetch_all_roots
985  
986 Arg [1] : (optional) String, name of ontology
987  
988 Description : Fetches all roots for all ontologies
989 Optionally, can be restricted to a given ontology
990  
991 Example :
992  
993 my ($terms) =
994 @{ $ot_adaptor->fetch_all_roots( 'SO' ) };
995  
996 # Will find terms in EFO, SO and GO:
997 my @terms = @{ $ot_adaptor->fetch_all_roots() };
998  
999 Return type : listref of Bio::EnsEMBL::OntologyTerm
1000  
1001 =cut
1002  
1003 sub fetch_all_roots {
1004 my ($this, $ontology_name) = @_;
1005  
1006 my $statement = q(
1007 SELECT term.term_id,
1008 term.accession,
1009 term.name,
1010 term.definition,
1011 term.subsets,
1012 term.is_root,
1013 term.is_obsolete,
1014 ontology.name,
1015 ontology.namespace,
1016 ontology.data_version
1017 FROM ontology
1018 JOIN term USING (ontology_id)
1019 WHERE is_root = 1);
1020  
1021 if (defined $ontology_name) {
1022 $statement .= " AND ontology.name = ?";
1023 }
1024  
1025 my $sth = $this->prepare($statement);
1026 if (defined $ontology_name) {
1027 $sth->bind_param( 1, $ontology_name, SQL_VARCHAR );
1028 }
1029 $sth->execute();
1030  
1031 my ( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology,
1032 $namespace, $ontology_version );
1033 $sth->bind_columns( \( $dbid, $accession, $name, $definition,
1034 $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version ) );
1035  
1036 my @terms;
1037  
1038 while ( $sth->fetch() ) {
1039 $subsets ||= '';
1040  
1041 push( @terms,
1042 Bio::EnsEMBL::OntologyTerm->new(
1043 '-dbid' => $dbid,
1044 '-adaptor' => $this,
1045 '-accession' => $accession,
1046 '-is_root' => $is_root,
1047 '-is_obsolete' => $is_obsolete,
1048 '-ontology' => $ontology,
1049 '-ontology_version' => $ontology_version,
1050 '-namespace' => $namespace,
1051 '-subsets' => [ split( /,/, $subsets ) ],
1052 '-name' => $name,
1053 '-definition' => $definition,
1054 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
1055  
1056 ));
1057 }
1058  
1059 return \@terms;
1060 }
1061  
1062  
1063 sub fetch_all {
1064 my ($this, $include_obsolete) = @_;
1065  
1066 my $statement = q(
1067 SELECT term.term_id,
1068 term.accession,
1069 term.name,
1070 term.definition,
1071 term.subsets,
1072 term.is_root,
1073 term.is_obsolete,
1074 ontology.name,
1075 ontology.namespace,
1076 ontology.data_version
1077 FROM ontology
1078 JOIN term USING (ontology_id));
1079  
1080 $statement .= " WHERE term.is_obsolete = 0" unless $include_obsolete;
1081  
1082 my $sth = $this->prepare($statement);
1083 $sth->execute();
1084  
1085 my ( $dbid, $accession, $name, $definition, $subsets, $is_root, $is_obsolete, $ontology,
1086 $namespace, $ontology_version );
1087 $sth->bind_columns( \( $dbid, $accession, $name, $definition,
1088 $subsets, $is_root, $is_obsolete, $ontology, $namespace, $ontology_version ) );
1089  
1090 my @terms;
1091  
1092 while ( $sth->fetch() ) {
1093 $subsets ||= '';
1094  
1095 push( @terms,
1096 Bio::EnsEMBL::OntologyTerm->new(
1097 '-dbid' => $dbid,
1098 '-adaptor' => $this,
1099 '-accession' => $accession,
1100 '-is_root' => $is_root,
1101 '-is_obsolete' => $is_obsolete,
1102 '-ontology' => $ontology,
1103 '-ontology_version' => $ontology_version,
1104 '-namespace' => $namespace,
1105 '-subsets' => [ split( /,/, $subsets ) ],
1106 '-name' => $name,
1107 '-definition' => $definition,
1108 '-synonyms' => $this->_fetch_synonyms_by_dbID($dbid)
1109 ) );
1110 }
1111  
1112 return \@terms;
1113 } ## end sub fetch_all
1114  
1115 1;