EnsEMBL/DBSQL/DBEntryAdaptor.pm untested

filter: Statementstmt 0.0% 0 / 749 Branchbran 0.0% 0 / 246 Conditioncond 0.0% 0 / 148 Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 1227 CC 124 SCAR 53.4
CC
124
Cov
0%
_fetch_by_object_type
812.0
_type_by_external_id
380.0
_store_object_xref_mapping
132.0
CRAP 208.0
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::DBEntryAdaptor -
34 MySQL Database queries to load and store external object references.
35  
36 =head1 SYNOPSIS
37  
38 $db_entry_adaptor =
39 $registry->get_adaptor( 'Human', 'Core', 'DBEntry' );
40  
41 $db_entry = $db_entry_adaptor->fetch_by_dbID($id);
42  
43 my $gene_adaptor = $registry->get_adaptor( 'Human', 'Core', 'Gene' );
44  
45 my $gene = $gene_adaptor->fetch_by_stable_id('ENSG00000101367');
46  
47 @db_entries = @{ $db_entry_adaptor->fetch_all_by_Gene($gene) };
48 @gene_ids = $db_entry_adaptor->list_gene_ids_by_extids('BAB15482');
49  
50 =head1 METHODS
51  
52 =cut
53  
54 package Bio::EnsEMBL::DBSQL::DBEntryAdaptor;
55 $Bio::EnsEMBL::DBSQL::DBEntryAdaptor::VERSION = '114.0.0_50'; # TRIAL
56 $Bio::EnsEMBL::DBSQL::DBEntryAdaptor::VERSION = '114.0.050';
57 use Bio::EnsEMBL::DBSQL::BaseAdaptor;
58  
59 use Bio::EnsEMBL::DBEntry;
60 use Bio::EnsEMBL::IdentityXref;
61 use Bio::EnsEMBL::OntologyXref;
62  
63 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
64 use Bio::EnsEMBL::Utils::Scalar qw(check_ref);
65  
66 use vars qw(@ISA);
67 use strict;
68  
69 @ISA = qw( Bio::EnsEMBL::DBSQL::BaseAdaptor );
70  
71 =head2 fetch_by_dbID
72  
73 Arg [1] : int $dbID
74 the unique database identifier for the DBEntry to retrieve
75 Example : my $db_entry = $db_entry_adaptor->fetch_by_dbID($dbID);
76 Description: Retrieves a dbEntry from the database via its unique
77 identifier.
78 Returntype : Bio::EnsEMBL::DBEntry
79 Exceptions : none
80 Caller : general
81 Status : Stable
82  
83 =cut
84  
85 sub fetch_by_dbID {
86 my ( $self, $dbID ) = @_;
87  
88 my $sth = $self->prepare(
89 "SELECT xref.xref_id,
90 xref.dbprimary_acc,
91 xref.display_label,
92 xref.version,
93 exDB.priority,
94 exDB.db_name,
95 exDB.db_display_name,
96 exDB.db_release,
97 es.synonym,
98 xref.info_type,
99 xref.info_text,
100 exDB.type,
101 exDB.secondary_db_name,
102 exDB.secondary_db_table,
103 xref.description
104 FROM (xref, external_db exDB)
105 LEFT JOIN external_synonym es ON
106 es.xref_id = xref.xref_id
107 WHERE xref.xref_id = ?
108 AND xref.external_db_id = exDB.external_db_id" );
109  
110 $sth->bind_param( 1, $dbID, SQL_INTEGER );
111 $sth->execute();
112  
113 my $exDB;
114  
115 my $max_rows = 1000;
116  
117 while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
118 #$description refers to the external_db description, while $desc was referring the xref description
119 while ( my $arrayref = shift( @{$rowcache} ) ) {
120 my ( $refID, $dbprimaryId,
121 $displayid, $version,
122 $priority,
123 $dbname, $db_display_name,
124 $release, $synonym,
125 $info_type, $info_text,
126 $type, $secondary_db_name,
127 $secondary_db_table, $description
128 ) = @$arrayref;
129  
130 if ( !defined($exDB) ) {
131 $exDB =
132 Bio::EnsEMBL::DBEntry->new(
133 -adaptor => $self,
134 -dbID => $dbID,
135 -primary_id => $dbprimaryId,
136 -display_id => $displayid,
137 -version => $version,
138 -release => $release,
139 -dbname => $dbname,
140 -priority => $priority,
141 -db_display_name => $db_display_name,
142 -info_type => $info_type,
143 -info_text => $info_text,
144 -type => $type,
145 -secondary_db_name => $secondary_db_name,
146 -secondary_db_table => $secondary_db_table,
147 -description => $description
148 );
149  
150  
151 }
152  
153 if ( defined($synonym) ) { $exDB->add_synonym($synonym) }
154  
155 } ## end while ( my $arrayref = shift...
156 } ## end while ( my $rowcache = $sth...
157  
158 $sth->finish();
159  
160 return $exDB;
161 } ## end sub fetch_by_dbID
162  
163  
164 sub _get_all_dm_loc_sth {
165 my ($self, $constraint ,$ensembl_object ) = @_;
166 my $object_type;
167 if($ensembl_object->isa("Bio::EnsEMBL::Gene")){
168 $object_type = "Gene";
169 }
170 elsif($ensembl_object->isa("Bio::EnsEMBL::Transcript")){
171 $object_type = "Transcript";
172 }
173 elsif($ensembl_object->isa("Bio::EnsEMBL::Translation")){
174 $object_type = "Translation";
175 }
176 elsif($ensembl_object->isa("Bio::EnsEMBL::Operon")){
177 $object_type = "Operon";
178 }
179 elsif($ensembl_object->isa("Bio::EnsEMBL::OperonTranscript")){
180 $object_type = "OperonTranscript";
181 }
182 else{
183 warn(ref($ensembl_object)." is not a Gene Transcript or Translation object??\n");
184 return undef;
185 }
186 my $sql = "SELECT xref.xref_id,
187 xref.dbprimary_acc,
188 xref.display_label,
189 xref.version,
190 exDB.priority,
191 exDB.db_name,
192 exDB.db_display_name,
193 exDB.db_release,
194 es.synonym,
195 xref.info_type,
196 xref.info_text,
197 exDB.type,
198 exDB.secondary_db_name,
199 exDB.secondary_db_table,
200 xref.description
201 FROM (xref, external_db exDB, dependent_xref dx, object_xref ox)
202 LEFT JOIN external_synonym es ON
203 es.xref_id = xref.xref_id
204 WHERE xref.external_db_id = exDB.external_db_id AND
205 ox.xref_id = xref.xref_id AND
206 ox.ensembl_object_type = \'$object_type\' AND
207 ox.ensembl_id = ".$ensembl_object->dbID();
208  
209 if($constraint){
210 $sql .= " AND $constraint";
211 }
212 else{
213 die "NO constraint???\n";
214 }
215  
216 my $sth = $self->prepare($sql) || die "Could not prepare $sql";
217  
218 return $self->_get_all_dm($sth);
219 }
220  
221 sub _get_all_dm_sth {
222 my ( $self, $constraint) = @_;
223  
224 my $sql = "SELECT xref.xref_id,
225 xref.dbprimary_acc,
226 xref.display_label,
227 xref.version,
228 exDB.priority,
229 exDB.db_name,
230 exDB.db_display_name,
231 exDB.db_release,
232 es.synonym,
233 xref.info_type,
234 xref.info_text,
235 exDB.type,
236 exDB.secondary_db_name,
237 exDB.secondary_db_table,
238 xref.description
239 FROM (xref, external_db exDB, dependent_xref dx)
240 LEFT JOIN external_synonym es ON
241 es.xref_id = xref.xref_id
242 WHERE xref.external_db_id = exDB.external_db_id ";
243  
244 if($constraint){
245 $sql .= "AND $constraint";
246 }
247 else{
248 die "NO constraint???\n";
249 }
250  
251 my $sth = $self->prepare($sql) || die "Could not prepare $sql";
252  
253 return $self->_get_all_dm($sth);
254 }
255  
256  
257 sub _get_all_dm{
258  
259 my ($self, $sth) = @_;
260  
261 # $sth->bind_param( 1, $dm_dbid, SQL_INTEGER );
262  
263 # print $sth."\n";
264 $sth->execute() || die "Not able to execute statement handle";
265  
266 my @list =();
267 my %seen;
268  
269 my $max_rows = 1000;
270 while ( my $rowcache = $sth->fetchall_arrayref(undef, $max_rows) ) {
271 while ( my $arrayref = shift( @{$rowcache} ) ) {
272 my ( $dbID, $dbprimaryId,
273 $displayid, $version,
274 $priority,
275 $dbname, $db_display_name,
276 $release, $synonym,
277 $info_type, $info_text,
278 $type, $secondary_db_name,
279 $secondary_db_table, $description
280 ) = @$arrayref;
281  
282 if ( !defined($seen{$dbID}) ) {
283 my $exDB =
284 Bio::EnsEMBL::DBEntry->new(
285 -adaptor => $self,
286 -dbID => $dbID,
287 -primary_id => $dbprimaryId,
288 -display_id => $displayid,
289 -version => $version,
290 -release => $release,
291 -dbname => $dbname,
292 -priority => $priority,
293 -db_display_name => $db_display_name,
294 -info_type => $info_type,
295 -info_text => $info_text,
296 -type => $type,
297 -secondary_db_name => $secondary_db_name,
298 -secondary_db_table => $secondary_db_table,
299 -description => $description
300 );
301  
302 if ($synonym) { $exDB->add_synonym($synonym) };
303 $seen{$dbID} = 1;
304 push @list, $exDB;
305 }
306  
307  
308  
309 } ## end while ( my $arrayref = shift...
310 } ## end while ( my $rowcache = $sth...
311  
312 $sth->finish();
313  
314 return \@list;
315  
316 }
317  
318  
319 =head2 get_all_dependents
320  
321 Args[1] : dbID of the DBentry to get the dependents of.
322 Args[2] : (optional) Bio::EnsEMBL::Gene, Transcript or Translation object
323 Example : my @dependents = @{ $dbe_adaptor->get_all_dependents(1234) };
324 Description: Get a list of DBEntrys that are depenednet on the DBEntry.
325 if an ensembl gene transcript or translation is given then only
326 the ones on that object will be given
327 Returntype : listref of DBEntrys. May be empty.
328 Exceptions : none
329 Caller : DBEntry->get_all_dependnets
330 Status : UnStable
331  
332 =cut
333  
334 sub get_all_dependents {
335 my ( $self, $dbid, $ensembl_object) = @_;
336
337 if(defined($ensembl_object) and !($ensembl_object->isa("Bio::EnsEMBL::Feature") or $ensembl_object->isa("Bio::EnsEMBL::Translation"))){
338 die ref($ensembl_object)." is not an Gene Transcript or Translation";
339 }
340
341 my $constraint = " dx.master_xref_id = $dbid AND dx.dependent_xref_id = xref.xref_id";
342 if(defined($ensembl_object)){
343 return $self->_get_all_dm_loc_sth($constraint, $ensembl_object);
344 }
345 else{
346 return $self->_get_all_dm_sth($constraint, $ensembl_object);
347 }
348  
349 }
350  
351 =head2 get_all_masters
352  
353 Args[1] : dbID of the DBentry to get the masters of.
354 Args[2] : (optional) Bio::EnsEMBL::Gene, Transcript or Translation object
355 Example : my @masters = @{ $dbe_adaptor->get_all_masters(1234) };
356 Description: Get a list of DBEntrys that are the masters of the DBEntry.
357 if an ensembl gene transcript or translation is given then only
358 the ones on that object will be given.
359 Returntype : listref of DBEntrys. May be empty.
360 Exceptions : none
361 Caller : DBEntry->get_all_masters
362 Status : UnStable
363  
364 =cut
365  
366 sub get_all_masters {
367 my ( $self, $dbid, $ensembl_object ) = @_;
368
369 if(defined($ensembl_object) and !($ensembl_object->isa("Bio::EnsEMBL::Feature") or $ensembl_object->isa("Bio::EnsEMBL::Translation"))){
370 die ref($ensembl_object)." is not an Gene Transcript or Translation";
371 }
372
373 my $constraint = "dx.dependent_xref_id = $dbid AND dx.master_xref_id = xref.xref_id";
374  
375 if(defined($ensembl_object)){
376 return $self->_get_all_dm_loc_sth($constraint, $ensembl_object);
377 }
378 else{
379 return $self->_get_all_dm_sth($constraint, $ensembl_object);
380 }
381 # return $self->_get_all_dm($constraint, $ensembl_object);
382 }
383  
384  
385 =head fetch_all_by_name
386  
387 Arg [1] : string $name - The name of the external reference.
388 found in accession, display_label or synonym
389 Arg [2] : (optional) string $dbname - The name of the database which
390 the provided name is for.
391  
392 Example : my $xref = @{$dbea->fetch_all_by_name('BRAC2','HGNC')}[0];
393 print $xref->description(), "\n" if($xref);
394 Description: Retrieves list of DBEntrys (xrefs) via a name.
395 The accesion is looked for first then the synonym and finally
396 the display_label.
397 NOTE $dbname this is optional but adding this speeds the
398 process up if you know what you are looking for.
399  
400 NOTE: In a multi-species database, this method will
401 return all the entries matching the search criteria, not
402 just the ones associated with the current species.
403 Returntype : Bio::EnsEMBL::DBSQL::DBEntry
404 Exceptions : thrown if arguments are incorrect
405 Caller : general, domainview
406 Status : Stable
407  
408 =cut
409  
410 sub fetch_all_by_name {
411 my ( $self, $name, $dbname ) = @_;
412  
413 my $sql = (<<SQL);
414 SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label, xref.version,
415 exDB.priority, exDB.db_name, exDB.db_display_name, exDB.db_release,
416 es.synonym, xref.info_type, xref.info_text,
417 exDB.type, exDB.secondary_db_name, exDB.secondary_db_table,
418 xref.description
419 FROM (xref, external_db exDB)
420 LEFT JOIN external_synonym es ON
421 es.xref_id = xref.xref_id
422 WHERE (xref.dbprimary_acc = ? or xref.display_label = ?)
423 AND xref.external_db_id = exDB.external_db_id
424 SQL
425  
426 if(defined $dbname){
427 $sql .= " AND exDB.db_name = ?";
428 }
429 my $sth = $self->prepare($sql);
430 $sth->bind_param( 1, $name, SQL_VARCHAR );
431 $sth->bind_param( 2, $name, SQL_VARCHAR );
432 if(defined $dbname){
433 $sth->bind_param( 3 , $dbname, SQL_VARCHAR );
434 }
435 $sth->execute();
436  
437 my $max_rows = 1000;
438  
439 my $precache = $sth->fetchall_arrayref( undef, $max_rows ); # need to fetch to ensure rows() works for SQLite
440  
441 if ( !$sth->rows() && lc($dbname) eq 'interpro' ) {
442 # This is a minor hack that means that results still come back even
443 # when a mistake was made and no interpro accessions were loaded into
444 # the xref table. This has happened in the past and had the result of
445 # breaking domainview
446  
447 $precache = undef;
448 $sth->finish();
449 $sth = $self->prepare(
450 "SELECT NULL,
451 i.interpro_ac,
452 i.id,
453 NULL,
454 NULL,
455 'Interpro',
456 NULL,
457 NULL
458 FROM interpro i
459 WHERE i.interpro_ac = ?" );
460  
461 $sth->bind_param( 1, $name, SQL_VARCHAR );
462 $sth->execute();
463 }
464  
465 my %exDB;
466 my @exDBlist;
467  
468 while ( my $rowcache = $precache || $sth->fetchall_arrayref( undef, $max_rows ) ) {
469 $precache = undef;
470 while ( my $arrayref = shift( @{$rowcache} ) ) {
471 my ( $dbID, $dbprimaryId,
472 $displayid, $version,
473 $priority,
474 $dbname, $db_display_name,
475 $release, $synonym,
476 $info_type, $info_text,
477 $type, $secondary_db_name,
478 $secondary_db_table, $description
479 ) = @$arrayref;
480  
481 if ( !defined $exDB{$dbID} ) {
482 my $entrie =
483 Bio::EnsEMBL::DBEntry->new(
484 -adaptor => $self,
485 -dbID => $dbID,
486 -primary_id => $dbprimaryId,
487 -display_id => $displayid,
488 -version => $version,
489 -release => $release,
490 -dbname => $dbname,
491 -priority => $priority,
492 -db_display_name => $db_display_name,
493 -info_type => $info_type,
494 -info_text => $info_text,
495 -type => $type,
496 -secondary_db_name => $secondary_db_name,
497 -secondary_db_table => $secondary_db_table,
498 -description => $description
499 );
500 $exDB{$dbID} = $entrie;
501 push @exDBlist, $entrie;
502 }
503 if ($synonym) { $exDB{$dbID}->add_synonym($synonym) }
504  
505 } ## end while ( my $arrayref = shift...
506 } ## end while ( my $rowcache = $sth...
507  
508 $sth->finish();
509  
510 return \@exDBlist;
511 } ## end sub fetch_all_by_name
512  
513  
514  
515 =head2 fetch_by_db_accession
516  
517 Arg [1] : string $dbname - The name of the database which the provided
518 accession is for.
519 Arg [2] : string $accession - The accesion of the external reference to
520 retrieve.
521 Example : my $xref = $dbea->fetch_by_db_accession('Interpro','IPR003439');
522 print $xref->description(), "\n" if($xref);
523 Description: Retrieves a DBEntry (xref) via the name of the database
524 it is from and its primary accession in that database.
525 Undef is returned if the xref cannot be found in the
526 database.
527 NOTE: In a multi-species database, this method will
528 return all the entries matching the search criteria, not
529 just the ones associated with the current species.
530 Returntype : Bio::EnsEMBL::DBSQL::DBEntry
531 Exceptions : thrown if arguments are incorrect
532 Caller : general, domainview
533 Status : Stable
534  
535 =cut
536  
537 sub fetch_by_db_accession {
538 my ( $self, $dbname, $accession ) = @_;
539  
540 my $sth = $self->prepare(
541 "SELECT xref.xref_id,
542 xref.dbprimary_acc,
543 xref.display_label,
544 xref.version,
545 exDB.priority,
546 exDB.db_name,
547 exDB.db_display_name,
548 exDB.db_release,
549 es.synonym,
550 xref.info_type,
551 xref.info_text,
552 exDB.type,
553 exDB.secondary_db_name,
554 exDB.secondary_db_table,
555 xref.description
556 FROM (xref, external_db exDB)
557 LEFT JOIN external_synonym es ON
558 es.xref_id = xref.xref_id
559 WHERE xref.dbprimary_acc = ?
560 AND exDB.db_name like ?
561 AND xref.external_db_id = exDB.external_db_id" );
562  
563 $sth->bind_param( 1, $accession, SQL_VARCHAR );
564 $sth->bind_param( 2, $dbname, SQL_VARCHAR );
565 $sth->execute();
566  
567 my $max_rows = 1000;
568  
569 my $precache = $sth->fetchall_arrayref( undef, $max_rows ); # need to fetch to ensure rows() works for SQLite
570  
571 if ( !$sth->rows() && lc($dbname) eq 'interpro' ) {
572 # This is a minor hack that means that results still come back even
573 # when a mistake was made and no interpro accessions were loaded into
574 # the xref table. This has happened in the past and had the result of
575 # breaking domainview
576  
577 $precache = undef;
578 $sth->finish();
579 $sth = $self->prepare(
580 "SELECT NULL,
581 i.interpro_ac,
582 i.id,
583 NULL,
584 NULL,
585 'Interpro',
586 NULL,
587 NULL
588 FROM interpro i
589 WHERE i.interpro_ac = ?" );
590  
591 $sth->bind_param( 1, $accession, SQL_VARCHAR );
592 $sth->execute();
593 }
594  
595 my $exDB;
596  
597 while ( my $rowcache = $precache || $sth->fetchall_arrayref( undef, $max_rows ) ) {
598 $precache = undef;
599 while ( my $arrayref = shift( @{$rowcache} ) ) {
600 my ( $dbID, $dbprimaryId,
601 $displayid, $version,
602 $priority,
603 $dbname, $db_display_name,
604 $release, $synonym,
605 $info_type, $info_text,
606 $type, $secondary_db_name,
607 $secondary_db_table, $description
608 ) = @$arrayref;
609  
610 if ( !defined($exDB) ) {
611 $exDB =
612 Bio::EnsEMBL::DBEntry->new(
613 -adaptor => $self,
614 -dbID => $dbID,
615 -primary_id => $dbprimaryId,
616 -display_id => $displayid,
617 -version => $version,
618 -release => $release,
619 -dbname => $dbname,
620 -priority => $priority,
621 -db_display_name => $db_display_name,
622 -info_type => $info_type,
623 -info_text => $info_text,
624 -type => $type,
625 -secondary_db_name => $secondary_db_name,
626 -secondary_db_table => $secondary_db_table,
627 -description => $description
628 );
629  
630  
631 }
632  
633 if ($synonym) { $exDB->add_synonym($synonym) }
634  
635 } ## end while ( my $arrayref = shift...
636 } ## end while ( my $rowcache = $sth...
637  
638 $sth->finish();
639  
640 return $exDB;
641 } ## end sub fetch_by_db_accession
642  
643  
644 =head2 store
645  
646 Arg [1] : Bio::EnsEMBL::DBEntry $dbEntry
647 The DBEntry (xref) to be stored
648 Arg [2] : Int $ensID
649 The dbID of an EnsEMBL object to associate with this external
650 database entry
651 Arg [3] : string $ensType ('Transcript', 'Translation', 'Gene')
652 The type of EnsEMBL object that this external database entry is
653 being associated with.
654 Arg [4] : boolean $ignore_release
655 If unset or zero, will require that the release string
656 of the DBEntry object is identical to the release of the
657 external database. If set and non-zero, will ignore the
658 release information.
659 Example : $dbea->store($db_entry, $transcript_id, 'Transcript');
660 Description: Stores a reference to an external database (if it is not stored
661 already) and associates an EnsEMBL object of a specified type
662 with the external identifier.
663 Returntype : int - the dbID of the newly created external refernce
664 Exceptions : thrown when invalid dbID is passed to this method
665 Caller : scripts which load Xrefs and ObjectXrefs, etc. into Ensembl
666 Status : Stable
667  
668 =cut
669  
670 sub store {
671 my ( $self, $dbEntry, $ensID, $ensType, $ignore_release, $master_xref ) = @_;
672  
673 my $dbJustInserted;
674  
675 #
676 # backwards compatibility check:
677 # check if $ensID is an object; if so, use $obj->dbID
678 #
679 my $ensembl_id;
680  
681 if ( defined($ensID) ) {
682 if ( $ensID =~ /^\d+$/ ) {
683 $ensembl_id = $ensID;
684 } elsif ( ref($ensID) eq 'Bio::EnsEMBL::Gene'
685 or ref($ensID) eq 'Bio::EnsEMBL::Transcript'
686 or ref($ensID) eq 'Bio::EnsEMBL::Translation'
687 or ref($ensID) eq 'Bio::EnsEMBL::OperonTranscript'
688 or ref($ensID) eq 'Bio::EnsEMBL::Operon'
689 )
690 {
691 warning( "You should pass DBEntryAdaptor->store() "
692 . "a dbID rather than an ensembl object "
693 . "to store the xref on" );
694  
695 if ( defined( $ensID->dbID() ) ) {
696 $ensembl_id = $ensID->dbID();
697 } else {
698 throw( sprintf( "%s %s doesn't have a dbID, can't store xref",
699 $ensType, $ensID->display_id() ) );
700 }
701 } else {
702 throw("Invalid dbID passed to DBEntryAdaptor->store()");
703 }
704 }
705
706
707
708 # Ensure external_db contains a record of the intended xref source
709 my $dbRef;
710 $dbRef = $self->_check_external_db($dbEntry,$ignore_release);
711  
712 # Attempt to insert DBEntry
713 my $xref_id = $self->_store_or_fetch_xref($dbEntry,$dbRef);
714 $dbEntry->dbID($xref_id); #keeps DBEntry in sync with database
715 ### Attempt to create an object->xref mapping
716 my $object_xref_id;
717 if ($ensembl_id) { $object_xref_id = $self->_store_object_xref_mapping($ensembl_id,$dbEntry,$ensType, $ignore_release)};
718  
719 if (defined $master_xref && defined $object_xref_id) { $self->_store_dependent_xref_mapping($object_xref_id, $dbEntry, $master_xref); }
720
721 return $xref_id;
722 }
723  
724  
725  
726  
727 =head2 update
728  
729 Arg [1] : Bio::EnsEMBL::DBEntry $dbentry
730 The dbentry to update
731 Example : $dbentry_adaptor->update($dbentry);
732 Description: Updates the dbprimary_acc, display_label, version, description, info_type
733 and info_text of a dbentry in the database.
734 Returntype : None
735 Exceptions : thrown if the $dbentry is not a Bio::EnsEMBL::DBEntry
736 Caller : general
737 Status : Stable
738  
739 =cut
740  
741 sub update {
742 my ($self, $dbEntry) = @_;
743  
744 if (!defined $dbEntry || !ref $dbEntry || !$dbEntry->isa('Bio::EnsEMBL::DBEntry')) {
745 throw("Must update a dbentry object, not a $dbEntry");
746 }
747  
748 my $update_dbentry_sql = qq(
749 UPDATE xref
750 SET dbprimary_acc = ?,
751 display_label = ?,
752 version = ?,
753 description = ?,
754 external_db_id = ?,
755 info_type = ?,
756 info_text = ?
757 WHERE xref_id = ?
758 );
759  
760 my $dbRef = $self->_check_external_db($dbEntry);
761 my $display_id = $dbEntry->display_id;
762 $display_id = '' unless defined $display_id; # SQLite doesn't ignore NOT NULL errors
763  
764 my $sth = $self->prepare($update_dbentry_sql);
765  
766 $sth->bind_param(1, $dbEntry->primary_id,SQL_VARCHAR);
767 $sth->bind_param(2, $display_id,SQL_VARCHAR);
768 $sth->bind_param(3, ($dbEntry->version || q{0}),SQL_VARCHAR);
769 $sth->bind_param(4, $dbEntry->description,SQL_VARCHAR);
770 $sth->bind_param(5, $dbRef,SQL_INTEGER);
771 $sth->bind_param(6, ($dbEntry->info_type || 'NONE'), SQL_VARCHAR);
772 $sth->bind_param(7, ($dbEntry->info_text || ''), SQL_VARCHAR);
773  
774 $sth->bind_param(8, $dbEntry->dbID(), SQL_INTEGER);
775  
776 $sth->execute();
777  
778 } ## end sub update
779
780 sub _store_object_xref_mapping {
781 my $self = shift;
782 my $ensembl_id = shift;
783 my $dbEntry = shift;
784 my $ensembl_type = shift;
785 my $ignore_release = shift;
786
787 my $dbc = $self->dbc();
788 my $sql_helper = $dbc->sql_helper();
789
790 if (not defined ($ensembl_type)) { warning("No Ensembl data type provided for new xref");}
791
792 my $analysis_id;
793 if ( $dbEntry->analysis() ) {
794 $analysis_id = $self->db()->get_AnalysisAdaptor->store( $dbEntry->analysis() );
795 }
796
797 my $insert_ignore = $self->insert_ignore_clause();
798 my $insert_params = [
799 [$dbEntry->dbID(), SQL_INTEGER],
800 [$ensembl_type, SQL_VARCHAR],
801 [$ensembl_id, SQL_INTEGER],
802 [$dbEntry->linkage_annotation(),SQL_VARCHAR],
803 [$analysis_id, SQL_INTEGER],
804 ];
805 my $base_object_xref_insert_sql = qq(
806 ${insert_ignore} INTO object_xref
807 ( xref_id,
808 ensembl_object_type,
809 ensembl_id,
810 linkage_annotation,
811 analysis_id )
812 VALUES ( ?, ?, ?, ?, ? )
813 );
814
815 my $object_xref_id;
816
817 #If MySQL we can optimise into a single insert query to deal with object_xref_id retrieval on insert ignore
818 if($dbc->driver() eq 'mysql') {
819 my $sql = $base_object_xref_insert_sql.'ON DUPLICATE KEY UPDATE object_xref_id=LAST_INSERT_ID(object_xref_id)';
820 $sql_helper->execute_update(-SQL => $sql, -PARAMS => $insert_params);
821 $object_xref_id = $self->last_insert_id('object_xref_id', undef, 'object_xref');
822 }
823 #Otherwise we attempt & retrieve on failure
824 else {
825 my $updated_rows = $sql_helper->execute_update(-SQL => $base_object_xref_insert_sql, -PARAMS => $insert_params);
826 if($updated_rows == 1) {
827 $object_xref_id = $self->last_insert_id('object_xref_id', undef, 'object_xref');
828 }
829 else {
830 my $sql = 'select object_xref_id from object_xref where xref_id =? and ensembl_object_type =? and ensembl_id =?';
831 my $params = [
832 [$dbEntry->dbID(), SQL_INTEGER],
833 [$ensembl_type, SQL_VARCHAR],
834 [$ensembl_id, SQL_INTEGER],
835 ];
836 $object_xref_id = $sql_helper->execute_single_result(-SQL => $sql, -PARAMS => $params);
837 }
838 }
839
840 $dbEntry->adaptor($self); # hand Adaptor to dbEntry for future use with OntologyXrefs
841 # This is here because everything else assumes it can reuse the $sth scalar ... grrr
842 my $sth;
843 if ( $dbEntry->isa('Bio::EnsEMBL::IdentityXref') ) {
844 # Can attempt multiple inserts as object_xref_id is the PRIMARY KEY
845 $sth = $self->prepare( "
846 ${insert_ignore} INTO identity_xref
847 ( object_xref_id,
848 xref_identity,
849 ensembl_identity,
850 xref_start,
851 xref_end ,
852 ensembl_start,
853 ensembl_end,
854 cigar_line,
855 score,
856 evalue )
857 VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" );
858 $sth->bind_param( 1, $object_xref_id, SQL_INTEGER );
859 $sth->bind_param( 2, $dbEntry->xref_identity, SQL_INTEGER );
860 $sth->bind_param( 3, $dbEntry->ensembl_identity, SQL_INTEGER );
861 $sth->bind_param( 4, $dbEntry->xref_start, SQL_INTEGER );
862 $sth->bind_param( 5, $dbEntry->xref_end, SQL_INTEGER );
863 $sth->bind_param( 6, $dbEntry->ensembl_start, SQL_INTEGER );
864 $sth->bind_param( 7, $dbEntry->ensembl_end, SQL_INTEGER );
865 $sth->bind_param( 8, $dbEntry->cigar_line, SQL_LONGVARCHAR );
866 $sth->bind_param( 9, $dbEntry->score, SQL_DOUBLE );
867 $sth->bind_param( 10, $dbEntry->evalue, SQL_DOUBLE );
868 $sth->execute();
869 } elsif ( $dbEntry->isa('Bio::EnsEMBL::OntologyXref') ) {
870 $sth = $self->prepare( "
871 ${insert_ignore} INTO ontology_xref
872 ( object_xref_id,
873 source_xref_id,
874 linkage_type )
875 VALUES ( ?, ?, ? )" );
876 foreach my $info ( @{ $dbEntry->get_all_linkage_info() } ) {
877 my ( $linkage_type, $sourceXref ) = @{$info};
878 my $sourceXid = undef;
879 if ($sourceXref) {
880 $sourceXref->is_stored( $self->dbc ) || $self->store($sourceXref, undef, undef, $ignore_release);
881 $sourceXid = $sourceXref->dbID;
882 }
883 $sth->bind_param( 1, $object_xref_id, SQL_INTEGER );
884 $sth->bind_param( 2, $sourceXid, SQL_INTEGER );
885 $sth->bind_param( 3, $linkage_type, SQL_VARCHAR );
886 $sth->execute();
887 } #end foreach
888
889 $sth = $self->prepare( "
890 ${insert_ignore} INTO associated_xref
891 ( object_xref_id,
892 xref_id,
893 source_xref_id,
894 condition_type,
895 associated_group_id,
896 `rank` )
897 VALUES ( ?, ?, ?, ?, ?, ? ) " );
898
899 my $annotext = $dbEntry->get_all_associated_xrefs();
900 foreach my $ax_group (sort keys %{ $annotext }) {
901 my $group = $annotext->{$ax_group};
902 my $gsth = $self->prepare( "
903 INSERT INTO associated_group
904 ( description )
905 VALUES ( ? )" );
906 $gsth->bind_param( 1, $ax_group, SQL_VARCHAR );
907 $gsth->execute();
908 my $associatedGid = $self->last_insert_id('associated_group_id', undef, 'associated_group');
909
910 foreach my $ax_rank (sort keys %{ $group }) {
911 my @ax = @{ $group->{$ax_rank} };
912
913 my $associatedXid = undef;
914 my $sourceXid = undef;
915
916 if (!$ax[0]->dbID) {
917 $self->store($ax[0]);
918 }
919 $associatedXid = $ax[0]->dbID;
920  
921 if (!$ax[1]->dbID) {
922 $self->store($ax[1]);
923 }
924 $sourceXid = $ax[1]->dbID;
925
926 if (!defined $associatedXid || !defined $sourceXid) {
927 next;
928 }
929 $sth->bind_param( 1, $object_xref_id, SQL_INTEGER );
930 $sth->bind_param( 2, $associatedXid, SQL_INTEGER );
931 $sth->bind_param( 3, $sourceXid, SQL_INTEGER );
932 $sth->bind_param( 4, $ax[2], SQL_VARCHAR );
933 $sth->bind_param( 5, $associatedGid, SQL_VARCHAR );
934 $sth->bind_param( 6, $ax_rank, SQL_INTEGER );
935 $sth->execute();
936 }
937 } #end foreach
938 } #end elsif
939 return $object_xref_id;
940 }
941  
942  
943 sub _store_dependent_xref_mapping {
944 my $self = shift;
945 my $object_xref_id = shift;
946 my $dbEntry = shift;
947 my $master_xref = shift;
948  
949 my $insert_ignore = $self->insert_ignore_clause();
950  
951 my $sth = $self->prepare( "
952 ${insert_ignore} INTO dependent_xref
953 ( object_xref_id,
954 master_xref_id,
955 dependent_xref_id )
956 VALUES ( ?, ?, ?)" );
957 $sth->bind_param( 1, $object_xref_id, SQL_INTEGER );
958 $sth->bind_param( 2, $master_xref->dbID, SQL_INTEGER );
959 $sth->bind_param( 3, $dbEntry->dbID, SQL_INTEGER );
960  
961 $sth->execute();
962  
963 }
964  
965 =head2 get_external_db_id
966  
967 Arg [1] : String
968 The external DB name to query by. Supports LIKE statements
969 Arg [2] : String (optional)
970 External DB release to use. If not specified then we
971 will search for NULL db_release entries
972 Arg [3] : Boolean (optional)
973 If true we will never look at the db_release value
974 when querying for an external db id
975 Description: Looks for the internal identifier of an external DB. You can
976 search using direct equality or using like statements specify. We
977 only return one value from this method.
978  
979 If you want more than one entry use get_external_db_ids().
980 Exceptions : None
981 Returntype : Int
982  
983 =cut
984  
985 sub get_external_db_id {
986 my $self = shift;
987 my $db_ids = $self->get_external_db_ids(@_);
988 return shift @$db_ids;
989 }
990  
991 =head2 get_external_db_ids
992  
993 Arg [1] : String
994 The external DB name to query by. Supports LIKE statements
995 Arg [2] : String (optional)
996 External DB release to use. If not specified then we
997 will search for NULL db_release entries
998 Arg [3] : Boolean (optional)
999 If true we will never look at the db_release value
1000 when querying for an external db id
1001 Description: Looks for the internal identifier of an external DB. You can
1002 search using direct equality or using like statements specify. We
1003 only return one value from this method. Returns more than one value
1004 Exceptions : None
1005 Returntype : ArrayRef of Int
1006  
1007 =cut
1008  
1009 sub get_external_db_ids {
1010 my ($self, $db_name, $db_release, $ignore_release) = @_;
1011 my $sql_helper = $self->dbc->sql_helper;
1012 my $sql = 'SELECT external_db_id FROM external_db WHERE db_name';
1013 if ($db_name =~ /%|_/) {
1014 $sql .= ' LIKE ?';
1015 } else {
1016 $sql .= ' = ?';
1017 }
1018 my @bound_params;
1019 push @bound_params,$db_name;
1020 unless ($ignore_release) {
1021 if ($db_release) {
1022 $sql .= ' AND db_release = ?';
1023 push @bound_params,$db_release;
1024 }
1025 else {
1026 $sql .= ' AND db_release is NULL';
1027 }
1028 }
1029
1030 my @db_ids = @{ $sql_helper->execute_simple(-SQL => $sql, -PARAMS => \@bound_params) };
1031 return \@db_ids;
1032 }
1033  
1034 =head2 get_distinct_external_dbs
1035  
1036 Description: Queries the external_db table for all unique
1037 DB names
1038 Exceptions : None
1039 Returntype : ArrayRef of String
1040  
1041 =cut
1042  
1043 sub get_distinct_external_dbs {
1044 my ($self) = @_;
1045 my $sql = 'SELECT DISTINCT db_name FROM external_db';
1046 my $names = $self->dbc->sql_helper->execute_simple(-SQL => $sql);
1047 return [ sort @{$names} ];
1048 }
1049  
1050 =head2 _check_external_db
1051  
1052 Arg [1] : DBEntry object
1053 Arg [2] : Ignore version flag
1054 Description: Looks for a record of the given external database
1055 Exceptions : Throws on missing external database entry
1056 Returntype : Int
1057  
1058 =cut
1059  
1060 sub _check_external_db {
1061 my ($self,$db_entry,$ignore) = @_;
1062 my ($db_name,$db_release);
1063 $db_name = $db_entry->dbname();
1064 $db_release = $db_entry->release();
1065 my $db_id = $self->get_external_db_id($db_name, $db_release, $ignore);
1066
1067 if ($db_id) {
1068 return $db_id;
1069 }
1070 else {
1071 throw( sprintf( "external_db [%s] release [%s] does not exist",
1072 $db_name, $db_release)
1073 );
1074 }
1075 }
1076  
1077 =head2 _store_or_fetch_xref
1078  
1079 Arg [1] : DBEntry object
1080 Arg [2] : Database accession for external database
1081 Description: Thread-safe method for adding xrefs, or otherwise returning
1082 an xref ID for the inserted or retrieved xref. Also inserts
1083 synonyms for that xref when entire new
1084 Returns : Int - the DB ID of the xref after insertion
1085 =cut
1086  
1087 sub _store_or_fetch_xref {
1088 my $self = shift;
1089 my $dbEntry = shift;
1090 my $dbRef = shift;
1091 my $xref_id;
1092  
1093 my $display_id = $dbEntry->display_id;
1094 $display_id = '' unless defined $display_id; # SQLite doesn't ignore NOT NULL errors
1095  
1096 my $insert_ignore = $self->insert_ignore_clause();
1097 my $sth = $self->prepare( "
1098 ${insert_ignore} INTO xref
1099 ( dbprimary_acc,
1100 display_label,
1101 version,
1102 description,
1103 external_db_id,
1104 info_type,
1105 info_text )
1106 VALUES ( ?, ?, ?, ?, ?, ?, ? ) ");
1107 $sth->bind_param(1, $dbEntry->primary_id,SQL_VARCHAR);
1108 $sth->bind_param(2, $display_id,SQL_VARCHAR);
1109 $sth->bind_param(3, ($dbEntry->version || q{0}),SQL_VARCHAR);
1110 $sth->bind_param(4, $dbEntry->description,SQL_VARCHAR);
1111 $sth->bind_param(5, $dbRef,SQL_INTEGER);
1112 $sth->bind_param(6, ($dbEntry->info_type || 'NONE'), SQL_VARCHAR);
1113 $sth->bind_param(7, ($dbEntry->info_text || ''), SQL_VARCHAR);
1114  
1115 my $count = $sth->execute();
1116 $xref_id = $self->last_insert_id('xref_id',undef,'xref') if $count > 0;
1117 $sth->finish();
1118
1119 if ($xref_id) { #insert was successful, store supplementary synonyms
1120 # thread safety no longer an issue.
1121 my $synonym_check_sth = $self->prepare(
1122 "SELECT xref_id, synonym
1123 FROM external_synonym
1124 WHERE xref_id = ?
1125 AND synonym = ?");
1126
1127 my $synonym_store_sth = $self->prepare(
1128 "${insert_ignore} INTO external_synonym
1129 ( xref_id, synonym ) VALUES ( ?, ? ) ");
1130
1131 my $synonyms = $dbEntry->get_all_synonyms();
1132 foreach my $syn ( @$synonyms ) {
1133 $synonym_check_sth->bind_param(1,$xref_id,SQL_INTEGER);
1134 $synonym_check_sth->bind_param(2,$syn,SQL_VARCHAR);
1135 $synonym_check_sth->execute();
1136 my ($dbSyn) = $synonym_check_sth->fetchrow_array();
1137 $synonym_store_sth->bind_param(1,$xref_id,SQL_INTEGER);
1138 $synonym_store_sth->bind_param(2,$syn,SQL_VARCHAR);
1139 $synonym_store_sth->execute() if(!$dbSyn);
1140 }
1141 $synonym_check_sth->finish();
1142 $synonym_store_sth->finish();
1143
1144 } else { # xref_id already exists, retrieve it according to fields in the unique key
1145 my $sql = 'SELECT xref_id FROM xref
1146 WHERE dbprimary_acc = ?
1147 AND version =?
1148 AND external_db_id = ?
1149 AND info_type = ?
1150 AND info_text = ?';
1151 my $info_type = $dbEntry->info_type() || 'NONE';
1152 my $info_text = $dbEntry->info_text() || q{};
1153 my $version = $dbEntry->version() || q{0};
1154 $sth = $self->prepare( $sql );
1155  
1156 $sth->bind_param(1, $dbEntry->primary_id,SQL_VARCHAR);
1157 $sth->bind_param(2, $version, SQL_VARCHAR);
1158 $sth->bind_param(3, $dbRef, SQL_INTEGER);
1159 $sth->bind_param(4, $info_type, SQL_VARCHAR);
1160 $sth->bind_param(5, $info_text, SQL_VARCHAR);
1161 $sth->execute();
1162  
1163  
1164  
1165 ($xref_id) = $sth->fetchrow_array();
1166 $sth->finish;
1167 if(!$xref_id) {
1168 my $msg = 'Cannot find an xref id for %s (version=%d) with external db id %d.';
1169 throw(sprintf($msg, $dbEntry->primary_id(), $version, $dbRef))
1170 }
1171 }
1172
1173 return $xref_id;
1174 }
1175  
1176 =head2 exists
1177  
1178 Arg [1] : Bio::EnsEMBL::DBEntry $dbe
1179 Example : if($dbID = $db_entry_adaptor->exists($dbe)) { do stuff; }
1180 Description: Returns the db id of this DBEntry if it exists in this database
1181 otherwise returns undef. Exists is defined as an entry with
1182 the same external_db and display_id
1183 Returntype : int
1184 Exceptions : thrown on incorrect args
1185 Caller : GeneAdaptor::store, TranscriptAdaptor::store
1186 Status : Stable
1187  
1188 =cut
1189  
1190 sub exists {
1191 my ($self, $dbe) = @_ ;
1192  
1193 unless($dbe && ref $dbe && $dbe->isa('Bio::EnsEMBL::DBEntry')) {
1194 throw("arg must be a Bio::EnsEMBL::DBEntry not [$dbe]");
1195 }
1196  
1197 my $sth = $self->prepare('SELECT x.xref_id
1198 FROM xref x, external_db xdb
1199 WHERE x.external_db_id = xdb.external_db_id
1200 AND x.display_label = ?
1201 AND xdb.db_name = ?
1202 AND x.dbprimary_acc = ?');
1203  
1204 $sth->bind_param(1,$dbe->display_id,SQL_VARCHAR);
1205 $sth->bind_param(2,$dbe->dbname,SQL_VARCHAR);
1206 $sth->bind_param(3,$dbe->primary_id,SQL_VARCHAR);
1207 $sth->execute();
1208  
1209 my ($dbID) = $sth->fetchrow_array;
1210  
1211 $sth->finish;
1212  
1213 return $dbID;
1214 }
1215  
1216  
1217 =head2 fetch_all_by_Gene
1218  
1219 Arg [1] : Bio::EnsEMBL::Gene $gene
1220 (The gene to retrieve DBEntries for)
1221 Arg [2] : optional external database name. SQL wildcards are accepted
1222 Arg [3] : optional external_db type. SQL wildcards are accepted
1223 Example : @db_entries = @{$db_entry_adaptor->fetch_all_by_Gene($gene)};
1224 Description: This returns a list of DBEntries associated with this gene.
1225 Note that this method was changed in release 15. Previously
1226 it set the DBLinks attribute of the gene passed in to contain
1227 all of the gene, transcript, and translation xrefs associated
1228 with this gene.
1229 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref if
1230 there is mapping data, or OntologyXref if there is linkage data.
1231 Exceptions : thows if gene object not passed
1232 Caller : Bio::EnsEMBL::Gene
1233 Status : Stable
1234  
1235 =cut
1236  
1237 sub fetch_all_by_Gene {
1238 my ( $self, $gene, $ex_db_reg, $exdb_type ) = @_;
1239  
1240 if(!ref($gene) || !$gene->isa('Bio::EnsEMBL::Gene')) {
1241 throw("Bio::EnsEMBL::Gene argument expected.");
1242 }
1243  
1244 return $self->_fetch_by_object_type($gene->dbID(), 'Gene', $ex_db_reg, $exdb_type);
1245 }
1246  
1247 =head2 fetch_all_by_Operon
1248  
1249 Arg [1] : Bio::EnsEMBL::Operon $operon
1250 (The operon to retrieve DBEntries for)
1251 Arg [2] : optional external database name. SQL wildcards are accepted
1252 Arg [3] : optional external_db type. SQL wildcards are accepted
1253 Example : @db_entries = @{$db_entry_adaptor->fetch_all_by_Operon($operon)};
1254 Description: This returns a list of DBEntries associated with this operon.
1255 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref if
1256 there is mapping data, or OntologyXref if there is linkage data.
1257 Exceptions : thows if operon object not passed
1258 Caller : general
1259  
1260 =cut
1261  
1262 sub fetch_all_by_Operon {
1263 my ( $self, $gene, $ex_db_reg, $exdb_type ) = @_;
1264  
1265 if(!ref($gene) || !$gene->isa('Bio::EnsEMBL::Operon')) {
1266 throw("Bio::EnsEMBL::Operon argument expected.");
1267 }
1268  
1269 return $self->_fetch_by_object_type($gene->dbID(), 'Operon', $ex_db_reg, $exdb_type);
1270 }
1271  
1272  
1273 =head2 fetch_all_by_Transcript
1274  
1275 Arg [1] : Bio::EnsEMBL::Transcript
1276 Arg [2] : optional external database name. SQL wildcards are accepted
1277 Arg [3] : optional external_db type. SQL wildcards are accepted
1278 Example : @db_entries = @{$db_entry_adaptor->fetch_all_by_Transcript($trans)};
1279 Description: This returns a list of DBEntries associated with this
1280 transcript. Note that this method was changed in release 15.
1281 Previously it set the DBLinks attribute of the gene passed in
1282 to contain all of the gene, transcript, and translation xrefs
1283 associated with this gene.
1284 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref if
1285 there is mapping data, or OntologyXref if there is linkage data.
1286 Exceptions : throes if transcript argument not passed
1287 Caller : Bio::EnsEMBL::Transcript
1288 Status : Stable
1289  
1290 =cut
1291  
1292 sub fetch_all_by_Transcript {
1293 my ( $self, $trans, $ex_db_reg, $exdb_type ) = @_;
1294  
1295 if(!ref($trans) || !$trans->isa('Bio::EnsEMBL::Transcript')) {
1296 throw("Bio::EnsEMBL::Transcript argument expected.");
1297 }
1298  
1299 return $self->_fetch_by_object_type( $trans->dbID(), 'Transcript', $ex_db_reg, $exdb_type);
1300 }
1301  
1302  
1303 =head2 fetch_all_by_Translation
1304  
1305 Arg [1] : Bio::EnsEMBL::Translation $trans
1306 (The translation to fetch database entries for)
1307 Arg [2] : optional external database name. SQL wildcards are accepted
1308 Arg [3] : optional externaldb type. SQL wildcards are accepted
1309 Example : @db_entries = @{$db_entry_adptr->fetch_all_by_Translation($trans)};
1310 Description: Retrieves external database entries for an EnsEMBL translation
1311 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref if
1312 there is mapping data, or OntologyXref if there is linkage data.
1313 Exceptions : throws if translation object not passed
1314 Caller : general
1315 Status : Stable
1316  
1317 =cut
1318  
1319 sub fetch_all_by_Translation {
1320 my ( $self, $trans, $ex_db_reg, $exdb_type ) = @_;
1321  
1322 if(!ref($trans) || !$trans->isa('Bio::EnsEMBL::Translation')) {
1323 throw('Bio::EnsEMBL::Translation argument expected.');
1324 }
1325 if( ! $trans->dbID ){
1326 warning( "Cannot fetch_all_by_Translation without a dbID" );
1327 return [];
1328 }
1329  
1330 return $self->_fetch_by_object_type( $trans->dbID(), 'Translation', $ex_db_reg, $exdb_type );
1331 }
1332  
1333  
1334 =head2 fetch_all_by_RNAProduct
1335  
1336 Arg [1] : Bio::EnsEMBL::RNAProduct $rp
1337 (The rnaproduct to fetch database entries for)
1338 Arg [2] : optional external database name. SQL wildcards are accepted
1339 Arg [3] : optional externaldb type. SQL wildcards are accepted
1340 Example : @db_entries = @{$db_entry_adptr->fetch_all_by_RNAProduct($rp)};
1341 Description: Retrieves external database entries for an EnsEMBL rnaproduct
1342 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref if
1343 there is mapping data, or OntologyXref if there is linkage data.
1344 Exceptions : throws if rnaproduct object not passed
1345 Caller : general
1346 Status : Stable
1347  
1348 =cut
1349  
1350 sub fetch_all_by_RNAProduct {
1351 my ($self, $rp, $ex_db_reg, $ex_db_type) = @_;
1352  
1353 if (!ref($rp) || !$rp->isa('Bio::EnsEMBL::RNAProduct')) {
1354 throw('Bio::EnsEMBL::RNAProduct argument expected.');
1355 }
1356 if (!$rp->dbID()){
1357 warning("Cannot fetch_all_by_RNAProduct without a dbID");
1358 return [];
1359 }
1360  
1361 return $self->_fetch_by_object_type($rp->dbID(), 'RNAProduct', $ex_db_reg, $ex_db_type);
1362 }
1363  
1364  
1365  
1366 =head2 remove_from_object
1367  
1368 Arg [1] : Bio::EnsEMBL::DBEntry $dbe - The external reference which
1369 is to be disassociated from an ensembl object.
1370 Arg [2] : Bio::EnsEMBL::Storable $object - The ensembl object the
1371 external reference is to be disassociated from
1372 Arg [3] : string $object_type - The type of the ensembl object.
1373 E.g. 'Gene', 'Transcript', 'Translation'
1374 Example :
1375 # remove all dbentries from this translation
1376 foreach my $dbe (@{$translation->get_all_DBEntries()}) {
1377 $dbe_adaptor->remove($dbe, $translation, 'Translation');
1378 }
1379 Description: Removes an association between an ensembl object and a
1380 DBEntry (xref). This does not remove the actual xref from
1381 the database, only its linkage to the ensembl object.
1382 Returntype : none
1383 Exceptions : Throw on incorrect arguments.
1384 Warning if object or dbentry is not stored in this database.
1385 Caller : TranscriptAdaptor::remove, GeneAdaptor::remove,
1386 TranslationAdaptor::remove
1387 Status : Stable
1388  
1389 =cut
1390  
1391 sub remove_from_object {
1392 my $self = shift;
1393 my $dbe = shift;
1394 my $object = shift;
1395 my $object_type = shift;
1396  
1397 if(!ref($dbe) || !$dbe->isa('Bio::EnsEMBL::DBEntry')) {
1398 throw("Bio::EnsEMBL::DBEntry argument expected.");
1399 }
1400  
1401 if(!ref($object) || !$dbe->isa('Bio::EnsEMBL::Storable')) {
1402 throw("Bio::EnsEMBL::Storable argument expected.");
1403 }
1404  
1405 if(!$object_type) {
1406 throw("object_type string argument expected.");
1407 }
1408  
1409 # make sure both the dbentry and the object it is allegedly linked to
1410 # are stored in this database
1411  
1412 if(!$object->is_stored($self->db())) {
1413 warning("Cannot remove DBEntries for $object_type " . $object->dbID() .
1414 ". Object is not stored in this database.");
1415 return;
1416 }
1417  
1418 if(!$dbe->is_stored($self->db())) {
1419 warning("Cannot remove DBEntry ".$dbe->dbID() . ". Is not stored " .
1420 "in this database.");
1421 return;
1422 }
1423  
1424 # obtain the identifier of the link from the object_xref table
1425 #No need to compare linkage_annotation here
1426 my $sth = $self->prepare
1427 ("SELECT ox.object_xref_id " .
1428 "FROM object_xref ox ".
1429 "WHERE ox.xref_id = ? " .
1430 "AND ox.ensembl_id = ? " .
1431 "AND ox.ensembl_object_type = ?");
1432 $sth->bind_param(1,$dbe->dbID,SQL_INTEGER);
1433 $sth->bind_param(2,$object->dbID,SQL_INTEGER);
1434 $sth->bind_param(3,$object_type,SQL_VARCHAR);
1435 $sth->execute();
1436  
1437 my ($ox_id) = $sth->fetchrow_array();
1438  
1439 if(!$sth->rows() == 1) {
1440 $sth->finish();
1441 return;
1442 }
1443  
1444 $sth->finish();
1445  
1446 # delete from the tables which contain additional linkage information
1447  
1448 $sth = $self->prepare("DELETE FROM ontology_xref WHERE object_xref_id = ?");
1449 $sth->bind_param(1,$ox_id,SQL_INTEGER);
1450 $sth->execute();
1451 $sth->finish();
1452
1453 $sth = $self->prepare("DELETE FROM associated_xref WHERE object_xref_id = ?");
1454 $sth->bind_param(1,$ox_id,SQL_INTEGER);
1455 $sth->execute();
1456 $sth->finish();
1457  
1458 $sth = $self->prepare("DELETE FROM identity_xref WHERE object_xref_id = ?");
1459 $sth->bind_param(1,$ox_id,SQL_INTEGER);
1460 $sth->execute();
1461 $sth->finish();
1462  
1463 # delete the actual linkage itself
1464 $sth = $self->prepare("DELETE FROM object_xref WHERE object_xref_id = ?");
1465 $sth->bind_param(1,$ox_id,SQL_INTEGER);
1466 $sth->execute();
1467 $sth->finish();
1468  
1469 return;
1470 }
1471  
1472  
1473 =head2 _fetch_by_object_type
1474  
1475 Arg [1] : string $ensID
1476 Arg [2] : string $ensType (object type to be returned)
1477 Arg [3] : optional $exdbname (external database name)
1478 (may be an SQL pattern containing '%' which matches any
1479 number of characters)
1480 Arg [4] : optional $exdb_type (external database type)
1481 (may be an SQL pattern containing '%' which matches any
1482 number of characters)
1483 Example : $self->_fetch_by_object_type( $translation_id, 'Translation' )
1484 Description: Fetches DBEntry by Object type
1485 NOTE: In a multi-species database, this method will
1486 return all the entries matching the search criteria, not
1487 just the ones associated with the current species.
1488  
1489  
1490 Returntype : arrayref of DBEntry objects; may be of type IdentityXref if
1491 there is mapping data, or OntologyXref if there is linkage data.
1492 Exceptions : none
1493 Caller : fetch_all_by_Gene
1494 fetch_all_by_Translation
1495 fetch_all_by_Transcript
1496 Status : Stable
1497  
1498 =cut
1499  
1500 sub _fetch_by_object_type {
1501 my ( $self, $ensID, $ensType, $exdbname, $exdb_type ) = @_;
1502  
1503 my @out;
1504  
1505 if ( !defined($ensID) ) {
1506 throw("Can't fetch_by_EnsObject_type without an object");
1507 }
1508  
1509 if ( !defined($ensType) ) {
1510 throw("Can't fetch_by_EnsObject_type without a type");
1511 }
1512  
1513 ### TODO - SCHEMA VERSION HACK. Please remove sometime in the future once we do not want backwards compatibility
1514 my $schema_version = $self->schema_version();
1515  
1516 # my $sth = $self->prepare("
1517 my $columns_sql = (<<COLUMNS_SQL);
1518 SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label, xref.version,
1519 exDB.priority,
1520 exDB.db_name, exDB.db_release, exDB.status, exDB.db_display_name,
1521 exDB.secondary_db_name, exDB.secondary_db_table,
1522 oxr.object_xref_id,
1523 es.synonym,
1524 idt.xref_identity, idt.ensembl_identity, idt.xref_start,
1525 idt.xref_end, idt.ensembl_start, idt.ensembl_end,
1526 idt.cigar_line, idt.score, idt.evalue, oxr.analysis_id,
1527 gx.linkage_type,
1528 xref.info_type, xref.info_text, exDB.type, gx.source_xref_id,
1529 oxr.linkage_annotation, xref.description
1530 COLUMNS_SQL
1531  
1532 my $tables_sql = <<'TABLES_SQL';
1533 FROM (xref xref, external_db exDB, object_xref oxr)
1534 LEFT JOIN external_synonym es on es.xref_id = xref.xref_id
1535 LEFT JOIN identity_xref idt on idt.object_xref_id = oxr.object_xref_id
1536 LEFT JOIN ontology_xref gx on gx.object_xref_id = oxr.object_xref_id
1537 TABLES_SQL
1538
1539 my $where_sql = <<'WHERE_SQL';
1540 WHERE xref.xref_id = oxr.xref_id
1541 AND xref.external_db_id = exDB.external_db_id
1542 AND oxr.ensembl_id = ?
1543 AND oxr.ensembl_object_type = ?
1544 WHERE_SQL
1545  
1546 if($schema_version >= 72) {
1547 $columns_sql .= ','; #need the extra comma
1548 $columns_sql .= <<'EXTRA';
1549 ax.xref_id, ax.source_xref_id, ax.condition_type, ax.associated_group_id, ax.rank
1550 EXTRA
1551 $tables_sql .= <<'EXTRA';
1552 LEFT JOIN associated_xref ax ON ax.object_xref_id = oxr.object_xref_id
1553 LEFT JOIN associated_group ag ON ax.associated_group_id = ag.associated_group_id
1554 EXTRA
1555  
1556 }
1557  
1558 #Join the above together into one statement
1559 my $sql = "$columns_sql
1560 $tables_sql
1561 $where_sql";
1562 # warn $sql; # uncomment me to see the full SQL generated
1563  
1564 if ( defined($exdbname) ) {
1565 if ( index( $exdbname, '%' ) != -1 ) {
1566 $sql .= " AND exDB.db_name LIKE "
1567 . $self->dbc()->db_handle()->quote( $exdbname, SQL_VARCHAR );
1568 } else {
1569 $sql .= " AND exDB.db_name = "
1570 . $self->dbc()->db_handle()->quote( $exdbname, SQL_VARCHAR );
1571 }
1572 }
1573  
1574 if ( defined($exdb_type) ) {
1575 if ( index( $exdb_type, '%' ) != -1 ) {
1576 $sql .= " AND exDB.type LIKE "
1577 . $self->dbc()->db_handle()->quote( $exdb_type, SQL_VARCHAR );
1578 } else {
1579 $sql .= " AND exDB.type = "
1580 . $self->dbc()->db_handle()->quote( $exdb_type, SQL_VARCHAR );
1581 }
1582 }
1583  
1584 my $sth = $self->prepare($sql);
1585  
1586 $sth->bind_param( 1, $ensID, SQL_INTEGER );
1587 $sth->bind_param( 2, $ensType, SQL_VARCHAR );
1588 $sth->execute();
1589  
1590 my ( %seen, %linkage_types, %synonyms, %associated_xrefs );
1591  
1592 my $max_rows = 1000;
1593  
1594 while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
1595 while ( my $arrRef = shift( @{$rowcache} ) ) {
1596 my ( $refID, $dbprimaryId,
1597 $displayid, $version,
1598 $priority,
1599 $dbname, $release,
1600 $exDB_status, $exDB_db_display_name,
1601 $exDB_secondary_db_name, $exDB_secondary_db_table,
1602 $objid, $synonym,
1603 $xrefid, $ensemblid,
1604 $xref_start, $xref_end,
1605 $ensembl_start, $ensembl_end,
1606 $cigar_line, $score,
1607 $evalue, $analysis_id,
1608 $linkage_type, $info_type,
1609 $info_text, $type,
1610 $source_xref_id, $link_annotation,
1611 $description, $associated_xref_id,
1612 $source_associated_xref_id, $condition_type,
1613 $associate_group_id, $associate_group_rank
1614 ) = @$arrRef;
1615  
1616 my $linkage_key =
1617 ( $linkage_type || '' ) . ( $source_xref_id || '' );
1618
1619 my $associated_key =
1620 ( $condition_type || '' )
1621 . ( $source_associated_xref_id || '' )
1622 . ( $associated_xref_id || '' );
1623  
1624  
1625 my $analysis = undef;
1626 if ( defined($analysis_id) ) {
1627 $analysis =
1628 $self->db()->get_AnalysisAdaptor()->fetch_by_dbID($analysis_id);
1629 }
1630  
1631 my %obj_hash = ( 'adaptor' => $self,
1632 'dbID' => $refID,
1633 'primary_id' => $dbprimaryId,
1634 'display_id' => $displayid,
1635 'version' => $version,
1636 'release' => $release,
1637 'info_type' => $info_type,
1638 'info_text' => $info_text,
1639 'type' => $type,
1640 'secondary_db_name' => $exDB_secondary_db_name,
1641 'secondary_db_table' => $exDB_secondary_db_table,
1642 'dbname' => $dbname,
1643 'description' => $description,
1644 'linkage_annotation' => $link_annotation,
1645 'analysis' => $analysis,
1646 'ensembl_object_type' => $ensType,
1647 'ensembl_id' => $ensID );
1648  
1649 # Using an outer join on the synonyms as well as on identity_xref,
1650 # we now have to filter out the duplicates (see v.1.18 for
1651 # original). Since there is at most one identity_xref row per
1652 # xref, this is easy enough; all the 'extra' bits are synonyms.
1653 my $source_xref;
1654 my $associated_xref;
1655 my $source_associated_xref;
1656 if ( !$seen{$refID} ) {
1657
1658 my $exDB;
1659 if ( ( defined($xrefid) ) ) { # an xref with similarity scores
1660 $exDB = Bio::EnsEMBL::IdentityXref->new_fast( \%obj_hash );
1661 $exDB->xref_identity($xrefid);
1662 $exDB->ensembl_identity($ensemblid);
1663  
1664 $exDB->cigar_line($cigar_line);
1665 $exDB->xref_start($xref_start);
1666 $exDB->xref_end($xref_end); # was not here before 14th Jan 2009 ????
1667 $exDB->ensembl_start($ensembl_start);
1668 $exDB->ensembl_end($ensembl_end);
1669 $exDB->score($score);
1670 $exDB->evalue($evalue);
1671  
1672 } elsif ( defined $linkage_type && $linkage_type ne "" ) {
1673 $exDB = Bio::EnsEMBL::OntologyXref->new_fast( \%obj_hash );
1674 $source_xref = ( defined($source_xref_id)
1675 ? $self->fetch_by_dbID($source_xref_id)
1676 : undef );
1677 $exDB->add_linkage_type( $linkage_type, $source_xref || () );
1678 $linkage_types{$refID}->{$linkage_key} = 1;
1679
1680 # Add associated Xref annotations to the OntologyXref entry.
1681 if ( defined $associated_xref_id && $associated_xref_id ne "" ) {
1682 $exDB = Bio::EnsEMBL::OntologyXref->new_fast( \%obj_hash );
1683 $associated_xref = ( defined($associated_xref_id)
1684 ? $self->fetch_by_dbID($associated_xref_id)
1685 : undef );
1686 $source_associated_xref = ( defined($source_associated_xref_id)
1687 ? $self->fetch_by_dbID($source_associated_xref_id)
1688 : undef );
1689 if ( defined($associated_xref) ) {
1690 my $ct = '';
1691 if ( defined $condition_type ) {
1692 $ct = $condition_type;
1693 }
1694 $exDB->add_linked_associated_xref( $associated_xref, $source_associated_xref, $ct, $associate_group_id, $associate_group_rank );
1695 }
1696 }
1697  
1698 } else {
1699 $exDB = Bio::EnsEMBL::DBEntry->new_fast( \%obj_hash );
1700 }
1701  
1702 if ( defined($exDB_status) ) { $exDB->status($exDB_status) }
1703  
1704 $exDB->priority($priority);
1705 $exDB->db_display_name($exDB_db_display_name);
1706  
1707 push( @out, $exDB );
1708 $seen{$refID} = $exDB;
1709  
1710 } ## end if ( !$seen{$refID} )
1711  
1712 # $exDB still points to the same xref, so we can keep adding GO
1713 # evidence tags or synonyms.
1714  
1715 if ( defined($synonym) && !$synonyms{$refID}->{$synonym} ) {
1716 if ( defined($synonym) ) {
1717 $seen{$refID}->add_synonym($synonym);
1718 }
1719 $synonyms{$refID}->{$synonym} = 1;
1720 }
1721  
1722 if ( defined($linkage_type)
1723 && $linkage_type ne ""
1724 && !$linkage_types{$refID}->{$linkage_key}
1725 && check_ref($seen{$refID}, 'Bio::EnsEMBL::OntologyXref') )
1726 {
1727 $source_xref = ( defined($source_xref_id)
1728 ? $self->fetch_by_dbID($source_xref_id)
1729 : undef );
1730 $seen{$refID}
1731 ->add_linkage_type( $linkage_type, $source_xref || () );
1732 $linkage_types{$refID}->{$linkage_key} = 1;
1733 }
1734
1735 if ( defined($associated_xref_id)
1736 && $associated_xref_id ne ""
1737 && !$associated_xrefs{$refID}->{$associated_key} )
1738 {
1739 $associated_xref = ( defined($associated_xref_id)
1740 ? $self->fetch_by_dbID($associated_xref_id)
1741 : undef );
1742 $source_associated_xref = ( defined($source_associated_xref_id)
1743 ? $self->fetch_by_dbID($source_associated_xref_id)
1744 : undef );
1745 if ( defined($associated_xref) ) {
1746 my $ct = '';
1747 if ( defined $condition_type ) {
1748 $ct = $condition_type;
1749 }
1750 $seen{$refID}->add_linked_associated_xref( $associated_xref, $source_associated_xref, $ct, $associate_group_id, $associate_group_rank );
1751 }
1752
1753 $linkage_types{$refID}->{$linkage_key} = 1;
1754 }
1755
1756 } ## end while ( my $arrRef = shift...
1757 } ## end while ( my $rowcache = $sth...
1758  
1759 return \@out;
1760 } ## end sub _fetch_by_object_type
1761  
1762 =head2 list_gene_ids_by_external_db_id
1763  
1764 Arg [1] : string $external_id
1765 Example : @gene_ids = $dbea->list_gene_ids_by_external_db_id(1020);
1766 Description: Retrieve a list of geneid by an external identifier that
1767 is linked to any of the genes transcripts, translations
1768 or the gene itself.
1769 NOTE: If more than one external identifier has the
1770 same primary accession then genes for each of these is
1771 returned.
1772 NOTE: In a multi-species database, this method will
1773 return all the entries matching the search criteria, not
1774 just the ones associated with the current species.
1775 Returntype : list of ints
1776 Exceptions : none
1777 Caller : unknown
1778 Status : Stable
1779  
1780 =cut
1781  
1782 sub list_gene_ids_by_external_db_id {
1783 my ($self,$external_db_id, $linkage_type) = @_;
1784  
1785 my %T = map { ($_, 1) }
1786 $self->_type_by_external_db_id( $external_db_id, 'Translation', 'gene', $linkage_type ),
1787 $self->_type_by_external_db_id( $external_db_id, 'Transcript', 'gene', $linkage_type ),
1788 $self->_type_by_external_db_id( $external_db_id, 'Gene', undef, $linkage_type );
1789 return keys %T;
1790 }
1791  
1792 =head2 list_gene_ids_by_extids
1793  
1794 Arg [1] : string $external_name
1795 Arg [2] : (optional) string $external_db_name
1796 Arg [3] : Boolean override, see _type_by_external_id
1797 Example : @gene_ids = $dbea->list_gene_ids_by_extids('CDPX');
1798 Description: Retrieve a list of geneid by an external identifier that is
1799 linked to any of the genes transcripts, translations or the
1800 gene itself
1801 Returntype : list of ints
1802 Exceptions : none
1803 Caller : unknown
1804 Status : Stable
1805  
1806 =cut
1807  
1808 sub list_gene_ids_by_extids {
1809 my ( $self, $external_name, $external_db_name, $override ) = @_;
1810  
1811 my %T = map { ( $_, 1 ) }
1812 $self->_type_by_external_id( $external_name, 'Translation', 'gene',
1813 $external_db_name, $override ),
1814 $self->_type_by_external_id( $external_name, 'Transcript', 'gene',
1815 $external_db_name, $override ),
1816 $self->_type_by_external_id( $external_name, 'Gene', undef,
1817 $external_db_name, $override );
1818  
1819 return keys %T;
1820 }
1821  
1822  
1823 =head2 list_transcript_ids_by_extids
1824  
1825 Arg [1] : string $external_name
1826 Arg [2] : (optional) string $external_db_name
1827 Arg [3] : Boolean override, see _type_by_external_id
1828 Example : @tr_ids = $dbea->list_transcript_ids_by_extids('BCRA2');
1829 Description: Retrieve a list transcript ids by an external identifier that
1830 is linked to any of the genes transcripts, translations or the
1831 gene itself
1832 Returntype : list of ints
1833 Exceptions : none
1834 Caller : unknown
1835 Status : Stable
1836  
1837 =cut
1838  
1839 sub list_transcript_ids_by_extids {
1840 my ( $self, $external_name, $external_db_name, $override ) = @_;
1841  
1842 my %T = map { ( $_, 1 ) }
1843 $self->_type_by_external_id( $external_name, 'Translation',
1844 'transcript', $external_db_name, $override
1845 ),
1846 $self->_type_by_external_id( $external_name, 'Transcript', undef,
1847 $external_db_name, $override );
1848  
1849 return keys %T;
1850 }
1851  
1852 sub list_transcript_ids_by_external_db_id {
1853 my ( $self, $external_db_id, $linkage_type) = @_;
1854 my %T = map { ( $_, 1 ) }
1855 $self->_type_by_external_db_id( $external_db_id, 'Translation', 'transcript', $linkage_type ),
1856 $self->_type_by_external_db_id( $external_db_id, 'Transcript', 'transcript', $linkage_type );
1857 return keys %T;
1858 }
1859  
1860 =head2 list_translation_ids_by_extids
1861  
1862 Arg [1] : string $external_name
1863 Arg [2] : (optional) string $external_db_name
1864 Arg [3] : Boolean override, see _type_by_external_id
1865 Example : @tr_ids = $dbea->list_translation_ids_by_extids('GO:0004835');
1866 Description: Gets a list of translation IDs by external display IDs
1867 Returntype : list of Ints
1868 Exceptions : none
1869 Caller : unknown
1870 Status : Stable
1871  
1872 =cut
1873  
1874 sub list_translation_ids_by_extids {
1875 my ( $self, $external_name, $external_db_name, $override ) = @_;
1876  
1877 return
1878 $self->_type_by_external_id( $external_name, 'Translation', undef,
1879 $external_db_name, $override );
1880 }
1881  
1882 sub list_translation_ids_by_external_db_id {
1883 my ( $self, $external_db_id, $linkage_type) = @_;
1884 return $self->_type_by_external_db_id( $external_db_id, 'Translation', undef, $linkage_type ),
1885 }
1886  
1887  
1888 =head2 list_rnaproduct_ids_by_extids
1889  
1890 Arg [1] : string $external_name
1891 Arg [2] : (optional) string $external_db_name
1892 Arg [3] : Boolean override, see _type_by_external_id
1893 Example : @rp_ids = $dbea->list_rnaproduct_ids_by_extids('GO:0004835');
1894 Description: Gets a list of rnaproduct IDs by external display IDs
1895 Returntype : list of Ints
1896 Exceptions : none
1897 Caller : unknown
1898 Status : Stable
1899  
1900 =cut
1901  
1902 sub list_rnaproduct_ids_by_extids {
1903 my ($self, $external_name, $external_db_name, $override) = @_;
1904  
1905 return $self->_type_by_external_id($external_name, 'RNAProduct', undef,
1906 $external_db_name, $override);
1907 }
1908  
1909  
1910 =head2 _type_by_external_id
1911  
1912 Arg [1] : string $name - dbprimary_acc
1913 Arg [2] : string $ensType - ensembl_object_type
1914 Arg [3] : (optional) string $extraType
1915 Arg [4] : (optional) string $external_db_name
1916 other object type to be returned
1917 Arg [5] : Boolean override to force _ to be treated as an SQL 'any'
1918 This is usually optimised out for query speed due to
1919 large numbers of names like NM_00...
1920 Example : $self->_type_by_external_id($name, 'Translation');
1921 NOTE: In a multi-species database, this method will
1922 return all the entries matching the search criteria, not
1923 just the ones associated with the current species.
1924 SQL wildcards can be used in the external id,
1925 but overly generic queries (two characters) will be prevented.
1926 Description: Gets
1927 Returntype : list of dbIDs (gene_id, transcript_id, etc.)
1928 Exceptions : none
1929 Caller : list_translation_ids_by_extids
1930 translationids_by_extids
1931 geneids_by_extids
1932 Status : Stable
1933  
1934 =cut
1935  
1936 sub _type_by_external_id {
1937 my ( $self, $name, $ensType, $extraType, $external_db_name, $override ) = @_;
1938  
1939 # $name has SQL wildcard support
1940 # = or LIKE put into SQL statement, and open queries like % or A% are rejected.
1941 my $comparison_operator;
1942 if ($name =~ /[_%\[]/ ) {
1943 $comparison_operator = "LIKE";
1944 if ($name =~ /^.?%/ && !$override) {
1945 warn "External $ensType name $name is too vague and will monopolise database resources. Please use a more specific $ensType name.\n";
1946 return;
1947 }
1948 elsif ($name =~ /^\w\w_/ && !$override) {
1949 # For entries such as NM_00000065, escape the _ so that SQL LIKE does not have to scan entire table
1950 # Escape only the _ in the third character position
1951 $name =~ s/(?<=\w\w)(?=_)/\\/;
1952 }
1953 }
1954 else {
1955 $comparison_operator = "=";
1956 }
1957  
1958  
1959 my $from_sql = '';
1960 my $where_sql = '';
1961 my $ID_sql = 'oxr.ensembl_id';
1962  
1963 if ( defined($extraType) ) {
1964 if ( lc($extraType) eq 'translation' ) {
1965 $ID_sql = 'tl.translation_id';
1966 } else {
1967 $ID_sql = "t.${extraType}_id";
1968 }
1969  
1970 if ( lc($ensType) eq 'translation' ) {
1971 $from_sql = 'transcript t, translation tl, ';
1972 $where_sql = qq(
1973 t.transcript_id = tl.transcript_id AND
1974 tl.translation_id = oxr.ensembl_id AND
1975 t.is_current = 1 AND
1976 );
1977 } else {
1978 $from_sql = 'transcript t, ';
1979 $where_sql = 't.'
1980 . lc($ensType)
1981 . '_id = oxr.ensembl_id AND '
1982 . 't.is_current = 1 AND ';
1983 }
1984 }
1985
1986 my $multispecies = $self->db()->is_multispecies();
1987  
1988 if ( lc($ensType) eq 'gene' ) {
1989 $from_sql = 'gene g, ';
1990 $from_sql .= 'seq_region s, coord_system cs, ' if $multispecies;
1991
1992 $where_sql = 'g.gene_id = oxr.ensembl_id AND g.is_current = 1 AND ';
1993 if($multispecies) {
1994 $where_sql .= <<'SQL';
1995 g.seq_region_id = s.seq_region_id AND
1996 s.coord_system_id = cs.coord_system_id AND
1997 cs.species_id = ? AND
1998 SQL
1999 }
2000 }
2001 elsif ( lc($ensType) eq 'transcript' ) {
2002 $from_sql = 'transcript t, ';
2003 $from_sql .= 'seq_region s, coord_system cs, ' if $multispecies;
2004
2005 $where_sql = 't.transcript_id = oxr.ensembl_id AND t.is_current = 1 AND ';
2006 if($multispecies) {
2007 $where_sql .= <<'SQL';
2008 t.seq_region_id = s.seq_region_id AND
2009 s.coord_system_id = cs.coord_system_id AND
2010 cs.species_id = ? AND
2011 SQL
2012 }
2013 }
2014 elsif ( lc($ensType) eq 'translation' ) {
2015 $from_sql = 'translation tl, transcript t, ';
2016 $from_sql .= 'seq_region s, coord_system cs, ' if $multispecies;
2017  
2018 $where_sql = 't.transcript_id = tl.transcript_id AND tl.translation_id = oxr.ensembl_id AND t.is_current = 1 AND ';
2019 if($multispecies) {
2020 $where_sql .= <<'SQL';
2021 t.seq_region_id = s.seq_region_id AND
2022 s.coord_system_id = cs.coord_system_id AND
2023 cs.species_id = ? AND
2024 SQL
2025 }
2026 }
2027  
2028 if ( defined($external_db_name) ) {
2029 # Involve the 'external_db' table to limit the hits to a particular
2030 # external database.
2031  
2032 $from_sql .= 'external_db xdb, ';
2033 $where_sql .=
2034 'xdb.db_name LIKE '
2035 . $self->dbc()->db_handle()->quote( $external_db_name . '%' )
2036 . ' AND xdb.external_db_id = x.external_db_id AND';
2037 }
2038  
2039 my @queries;
2040 push (@queries, qq(
2041 SELECT $ID_sql
2042 FROM $from_sql
2043 xref x,
2044 object_xref oxr
2045 WHERE $where_sql
2046 x.dbprimary_acc $comparison_operator ?
2047 AND x.xref_id = oxr.xref_id
2048 AND oxr.ensembl_object_type = ?
2049 ));
2050  
2051 push (@queries, qq(
2052 SELECT $ID_sql
2053 FROM $from_sql
2054 xref x,
2055 object_xref oxr
2056 WHERE $where_sql
2057 x.display_label $comparison_operator ?
2058 AND x.xref_id = oxr.xref_id
2059 AND oxr.ensembl_object_type = ?
2060 ));
2061  
2062 if ( defined($external_db_name) ) {
2063 # If we are given the name of an external database, we need to join
2064 # between the 'xref' and the 'object_xref' tables on 'xref_id'.
2065  
2066 push (@queries, qq(
2067 SELECT $ID_sql
2068 FROM $from_sql
2069 external_synonym syn,
2070 object_xref oxr,
2071 xref x
2072 WHERE $where_sql
2073 syn.synonym $comparison_operator ?
2074 AND syn.xref_id = oxr.xref_id
2075 AND oxr.ensembl_object_type = ?
2076 AND x.xref_id = oxr.xref_id));
2077  
2078 } else {
2079 # If we weren't given an external database name, we can get away
2080 # with less joins here.
2081  
2082 push (@queries, qq(
2083 SELECT $ID_sql
2084 FROM $from_sql
2085 external_synonym syn,
2086 object_xref oxr
2087 WHERE $where_sql
2088 syn.synonym $comparison_operator ?
2089 AND syn.xref_id = oxr.xref_id
2090 AND oxr.ensembl_object_type = ?));
2091  
2092 }
2093  
2094 my %result;
2095 my $h = $self->dbc()->sql_helper();
2096 my @params = ([$name, SQL_VARCHAR], [$ensType, SQL_VARCHAR]);
2097 unshift(@params, [$self->species_id(), SQL_INTEGER] ) if $multispecies;
2098 foreach my $query (@queries) {
2099 $h->execute_no_return(-SQL => $query, -PARAMS => \@params, -CALLBACK => sub {
2100 my ($row) = @_;
2101 my ($id) = @{$row};
2102 $result{$id} = 1;
2103 });
2104 }
2105  
2106 return keys(%result);
2107  
2108 } ## end sub _type_by_external_id
2109  
2110 =head2 _type_by_external_db_id
2111  
2112 Arg [1] : integer $type - external_db_id
2113 Arg [2] : string $ensType - ensembl_object_type
2114 Arg [3] : (optional) string $extraType
2115 other object type to be returned. This references the _id fields of
2116 the transcript table, and if left unset defaults to the translation_id
2117 from the translation table.
2118 Arg [4] : (optional) string $linkage_type
2119 provides a link to ontology_xref and limits by an ontology linkage type
2120 Example : $self->_type_by_external_db_id(1030, 'Translation');
2121 Description: Gets.
2122 NOTE: In a multi-species database, this method will
2123 return all the entries matching the search criteria, not
2124 just the ones associated with the current species.
2125 Returntype : list of dbIDs (gene_id, transcript_id, etc.)
2126 Exceptions : none
2127 Caller : list_translation_ids_by_extids
2128 translationids_by_extids
2129 geneids_by_extids
2130 Status : Stable
2131  
2132 =cut
2133  
2134 sub _type_by_external_db_id {
2135 my ($self, $external_db_id, $ensType, $extraType, $linkage_type) = @_;
2136 throw "No external DB identifer given" unless defined $external_db_id;
2137 throw "No Ensembl type given" unless defined $ensType;
2138  
2139 my $from_sql = '';
2140 my $where_sql = '';
2141 my $ID_sql = "oxr.ensembl_id";
2142 my $lcEnsType = lc($ensType);
2143  
2144 if (defined $extraType) {
2145 if (lc($extraType) eq 'translation') {
2146 $ID_sql = "tl.translation_id";
2147 } else {
2148 $ID_sql = "t.${extraType}_id";
2149 }
2150  
2151 if ($lcEnsType eq 'translation') {
2152 $from_sql = 'transcript t, translation tl, ';
2153 $where_sql = qq(
2154 t.transcript_id = tl.transcript_id AND
2155 tl.translation_id = oxr.ensembl_id AND
2156 t.is_current = 1 AND
2157 );
2158 } else {
2159 $from_sql = 'transcript t, ';
2160 $where_sql = 't.'.$lcEnsType.'_id = oxr.ensembl_id AND '.
2161 't.is_current = 1 AND ';
2162 }
2163 }
2164  
2165 if ($lcEnsType eq 'gene') {
2166 $from_sql = 'gene g, ';
2167 $where_sql = 'g.gene_id = oxr.ensembl_id AND g.is_current = 1 AND ';
2168 } elsif ($lcEnsType eq 'transcript') {
2169 $from_sql = 'transcript t, ';
2170 $where_sql = 't.transcript_id = oxr.ensembl_id AND t.is_current = 1 AND ';
2171 } elsif ($lcEnsType eq 'translation') {
2172 $from_sql = 'transcript t, translation tl, ';
2173 $where_sql = qq(
2174 t.transcript_id = tl.transcript_id AND
2175 tl.translation_id = oxr.ensembl_id AND
2176 t.is_current = 1 AND
2177 );
2178 }
2179  
2180 if($linkage_type) {
2181 $from_sql .= 'ontology_xref ontx, ';
2182 $where_sql .= 'oxr.object_xref_id = ontx.object_xref_id AND ';
2183 $where_sql .= 'ontx.linkage_type =? AND';
2184 }
2185  
2186 my $query =
2187 "SELECT $ID_sql
2188 FROM $from_sql xref x, object_xref oxr
2189 WHERE $where_sql x.external_db_id = ? AND
2190 x.xref_id = oxr.xref_id AND oxr.ensembl_object_type= ?";
2191  
2192 my @params;
2193 push(@params, [$linkage_type, SQL_VARCHAR]) if $linkage_type;
2194 push(@params,
2195 [$external_db_id, SQL_INTEGER],
2196 [$ensType, SQL_VARCHAR],
2197 );
2198  
2199 my %result;
2200 $self->dbc()->sql_helper()->execute_no_return(-SQL => $query, -PARAMS => \@params, -CALLBACK => sub {
2201 my ($row) = @_;
2202 my ($id) = @{$row};
2203 $result{$id} = 1;
2204 });
2205  
2206 return keys(%result);
2207 } ## end _type_by_external_db_id
2208  
2209  
2210 =head2 fetch_all_by_description
2211  
2212 Arg [1] : string description to search for. Include % etc in this string
2213 Arg [2] : <optional> string $dbname. Name of the database to search
2214  
2215 Example : @canc_refs = @{$db_entry_adaptor->fetch_all_by_description("%cancer%")};
2216 @db_entries = @{$db_entry_adaptor->fetch_all_by_description("%cancer%","MIM_MORBID")};
2217 Description: Retrieves DBEntries that match the description.
2218 Optionally you can search on external databases type.
2219 NOTE: In a multi-species database, this method will
2220 return all the entries matching the search criteria, not
2221 just the ones associated with the current species.
2222 Returntype : ref to array of Bio::EnsEMBL::DBSQL::DBEntry
2223 Exceptions : None.
2224 Caller : General
2225 Status : At Risk
2226  
2227 =cut
2228  
2229 sub fetch_all_by_description {
2230 my ( $self, $description, $dbname ) = @_;
2231  
2232 my @results = ();
2233  
2234 my $sql =
2235 "SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label,
2236 xref.version,
2237 exDB.priority,
2238 exDB.db_name, exDB.db_display_name, exDB.db_release, es.synonym,
2239 xref.info_type, xref.info_text, exDB.type, exDB.secondary_db_name,
2240 exDB.secondary_db_table, xref.description
2241 FROM (xref, external_db exDB)
2242 LEFT JOIN external_synonym es on es.xref_id = xref.xref_id
2243 WHERE xref.description like ?
2244 AND xref.external_db_id = exDB.external_db_id";
2245  
2246 if ( defined($dbname) ) { $sql .= " AND exDB.db_name = ? " }
2247  
2248 my $sth = $self->prepare($sql);
2249  
2250 $sth->bind_param( 1, $description, SQL_VARCHAR );
2251  
2252 if ( defined($dbname) ) {
2253 $sth->bind_param( 2, $dbname, SQL_VARCHAR );
2254 }
2255  
2256 $sth->execute();
2257  
2258 my $max_rows = 1000;
2259  
2260 while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
2261 while ( my $arrayref = shift( @{$rowcache} ) ) {
2262 my ( $dbID, $dbprimaryId,
2263 $displayid, $version,
2264 $priority,
2265 $ex_dbname, $db_display_name,
2266 $release, $synonym,
2267 $info_type, $info_text,
2268 $type, $secondary_db_name,
2269 $secondary_db_table, $description
2270 ) = @$arrayref;
2271  
2272 my $exDB =
2273 Bio::EnsEMBL::DBEntry->new(
2274 -adaptor => $self,
2275 -dbID => $dbID,
2276 -primary_id => $dbprimaryId,
2277 -display_id => $displayid,
2278 -version => $version,
2279 -release => $release,
2280 -dbname => $ex_dbname,
2281 -priority => $priority,
2282 -db_display_name => $db_display_name,
2283 -info_type => $info_type,
2284 -info_text => $info_text,
2285 -type => $type,
2286 -secondary_db_name => $secondary_db_name,
2287 -secondary_db_table => $secondary_db_table,
2288 -description => $description
2289 );
2290  
2291 if ($synonym) { $exDB->add_synonym($synonym) }
2292  
2293 push @results, $exDB;
2294  
2295 } ## end while ( my $arrayref = shift...
2296 } ## end while ( my $rowcache = $sth...
2297  
2298 $sth->finish();
2299  
2300 return \@results;
2301 } ## end sub fetch_all_by_description
2302  
2303  
2304 =head2 fetch_all_by_source
2305  
2306 Arg [1] : string source to search for. Include % etc in this string
2307 if you want to use SQL patterns
2308  
2309 Example : @unigene_refs = @{$db_entry_adaptor->fetch_all_by_source("%unigene%")};
2310 Description: Retrieves DBEntrys that match the source name.
2311 Returntype : ref to array of Bio::EnsEMBL::DBSQL::DBEntry
2312 Exceptions : None.
2313 Caller : General
2314 Status : At Risk
2315  
2316 =cut
2317  
2318 sub fetch_all_by_source {
2319 my ( $self, $source ) = @_;
2320  
2321 my @results = ();
2322  
2323 my $sql =
2324 "SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label,
2325 xref.version,
2326 exDB.priority,
2327 exDB.db_name, exDB.db_display_name, exDB.db_release, es.synonym,
2328 xref.info_type, xref.info_text, exDB.type, exDB.secondary_db_name,
2329 exDB.secondary_db_table, xref.description
2330 FROM (xref, external_db exDB)
2331 LEFT JOIN external_synonym es on es.xref_id = xref.xref_id
2332 WHERE exDB.db_name like ?
2333 AND xref.external_db_id = exDB.external_db_id";
2334  
2335  
2336 my $sth = $self->prepare($sql);
2337  
2338 $sth->bind_param( 1, $source, SQL_VARCHAR );
2339  
2340 $sth->execute();
2341  
2342 my $max_rows = 1000;
2343  
2344 while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
2345 while ( my $arrayref = shift( @{$rowcache} ) ) {
2346 my ( $dbID, $dbprimaryId,
2347 $displayid, $version,
2348 $priority,
2349 $dbname, $db_display_name,
2350 $release, $synonym,
2351 $info_type, $info_text,
2352 $type, $secondary_db_name,
2353 $secondary_db_table, $description
2354 ) = @$arrayref;
2355  
2356 my $exDB =
2357 Bio::EnsEMBL::DBEntry->new(
2358 -adaptor => $self,
2359 -dbID => $dbID,
2360 -primary_id => $dbprimaryId,
2361 -display_id => $displayid,
2362 -version => $version,
2363 -release => $release,
2364 -dbname => $dbname,
2365 -priority => $priority,
2366 -db_display_name => $db_display_name,
2367 -info_type => $info_type,
2368 -info_text => $info_text,
2369 -type => $type,
2370 -secondary_db_name => $secondary_db_name,
2371 -secondary_db_table => $secondary_db_table,
2372 -description => $description
2373 );
2374  
2375 if ($synonym) { $exDB->add_synonym($synonym) }
2376  
2377 push @results, $exDB;
2378  
2379 } ## end while ( my $arrayref = shift...
2380 } ## end while ( my $rowcache = $sth...
2381  
2382 $sth->finish();
2383  
2384 return \@results;
2385 } ## end sub fetch_all_by_source
2386  
2387  
2388 =head2 fetch_all_synonyms
2389  
2390 Arg [1] : dbID of DBEntry to fetch synonyms for. Used in lazy loading of synonyms.
2391  
2392 Example : @canc_refs = @{$db_entry_adaptor->fetch_all_synonyms(1234)};
2393 Description: Fetches the synonyms for a particular DBEntry.
2394 Returntype : listref of synonyms. List referred to may be empty if there are no synonyms.
2395 Exceptions : None.
2396 Caller : General
2397 Status : At Risk
2398  
2399 =cut
2400  
2401  
2402 sub fetch_all_synonyms {
2403 my ( $self, $dbID ) = @_;
2404  
2405 my @synonyms = ();
2406  
2407 my $sth =
2408 $self->prepare( "SELECT synonym "
2409 . "FROM external_synonym "
2410 . "WHERE xref_id = ?" );
2411  
2412 $sth->bind_param( 1, $dbID, SQL_INTEGER );
2413  
2414 $sth->execute();
2415  
2416 my $synonym;
2417 $sth->bind_col(1, \$synonym);
2418  
2419 while ( $sth->fetch() ) {
2420 push( @synonyms, $synonym );
2421 }
2422  
2423 return \@synonyms;
2424 }
2425  
2426  
2427 =head2 get_db_name_from_external_db_id
2428  
2429 Arg [1] : external_dbid of database to get the database_name
2430 Example : my $db_name = $db_entry_adaptor->get_db_name_from_external_db_id(1100);
2431 Description: Gets the database name for a certain external_db_id
2432 Returntype : scalar
2433 Exceptions : None.
2434 Caller : General
2435 Status : At Risk
2436  
2437 =cut
2438  
2439 sub get_db_name_from_external_db_id{
2440 my $self = shift;
2441 my $external_db_id = shift;
2442  
2443 my $sth = $self->prepare("SELECT db_name FROM external_db WHERE external_db_id = ?");
2444  
2445 $sth->bind_param(1, $external_db_id, SQL_INTEGER);
2446 $sth->execute();
2447 my ($db_name) = $sth->fetchrow_array();
2448 $sth->finish();
2449 return $db_name;
2450  
2451 }
2452  
2453 1;
2454