EnsEMBL/DBSQL/OntologyTermAdaptor.pm untested

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