3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
34 MySQL Database queries to load and store external
object references.
39 $registry->get_adaptor(
'Human',
'Core',
'DBEntry' );
43 my $gene_adaptor = $registry->get_adaptor(
'Human',
'Core',
'Gene' );
45 my $gene = $gene_adaptor->fetch_by_stable_id(
'ENSG00000101367');
47 @db_entries = @{ $db_entry_adaptor->fetch_all_by_Gene($gene) };
48 @gene_ids = $db_entry_adaptor->list_gene_ids_by_extids(
'BAB15482');
54 package Bio::EnsEMBL::DBSQL::DBEntryAdaptor;
73 the unique database identifier
for the
DBEntry to retrieve
74 Example : my $db_entry = $db_entry_adaptor->fetch_by_dbID($dbID);
75 Description: Retrieves a dbEntry from the database via its unique
85 my ( $self, $dbID ) = @_;
87 my $sth = $self->prepare(
100 exDB.secondary_db_name,
101 exDB.secondary_db_table,
103 FROM (xref, external_db exDB)
104 LEFT JOIN external_synonym es ON
105 es.xref_id = xref.xref_id
106 WHERE xref.xref_id = ?
107 AND xref.external_db_id = exDB.external_db_id" );
109 $sth->bind_param( 1, $dbID, SQL_INTEGER );
116 while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
117 #$description refers to the external_db description, while $desc was referring the xref description
118 while ( my $arrayref = shift( @{$rowcache} ) ) {
119 my ( $refID, $dbprimaryId,
120 $displayid, $version,
122 $dbname, $db_display_name,
124 $info_type, $info_text,
125 $type, $secondary_db_name,
126 $secondary_db_table, $description
129 if ( !defined($exDB) ) {
134 -primary_id => $dbprimaryId,
135 -display_id => $displayid,
136 -version => $version,
137 -release => $release,
139 -priority => $priority,
140 -db_display_name => $db_display_name,
141 -info_type => $info_type,
142 -info_text => $info_text,
144 -secondary_db_name => $secondary_db_name,
145 -secondary_db_table => $secondary_db_table,
146 -description => $description
152 if ( defined($synonym) ) { $exDB->
add_synonym($synonym) }
154 } ## end
while ( my $arrayref = shift...
155 } ## end
while ( my $rowcache = $sth...
160 } ## end sub fetch_by_dbID
163 sub _get_all_dm_loc_sth {
164 my ($self, $constraint ,$ensembl_object ) = @_;
166 if($ensembl_object->isa(
"Bio::EnsEMBL::Gene")){
167 $object_type =
"Gene";
169 elsif($ensembl_object->isa(
"Bio::EnsEMBL::Transcript")){
170 $object_type =
"Transcript";
172 elsif($ensembl_object->isa(
"Bio::EnsEMBL::Translation")){
173 $object_type =
"Translation";
175 elsif($ensembl_object->isa(
"Bio::EnsEMBL::Operon")){
176 $object_type =
"Operon";
178 elsif($ensembl_object->isa(
"Bio::EnsEMBL::OperonTranscript")){
179 $object_type =
"OperonTranscript";
182 warn(ref($ensembl_object).
" is not a Gene Transcript or Translation object??\n");
185 my $sql =
"SELECT xref.xref_id,
191 exDB.db_display_name,
197 exDB.secondary_db_name,
198 exDB.secondary_db_table,
200 FROM (xref, external_db exDB, dependent_xref dx, object_xref ox)
201 LEFT JOIN external_synonym es ON
202 es.xref_id = xref.xref_id
203 WHERE xref.external_db_id = exDB.external_db_id AND
204 ox.xref_id = xref.xref_id AND
205 ox.ensembl_object_type = \'$object_type\' AND
206 ox.ensembl_id = ".$ensembl_object->dbID();
209 $sql .=
" AND $constraint";
212 die
"NO constraint???\n";
215 my $sth = $self->prepare($sql) || die
"Could not prepare $sql";
217 return $self->_get_all_dm($sth);
220 sub _get_all_dm_sth {
221 my ( $self, $constraint) = @_;
223 my $sql =
"SELECT xref.xref_id,
229 exDB.db_display_name,
235 exDB.secondary_db_name,
236 exDB.secondary_db_table,
238 FROM (xref, external_db exDB, dependent_xref dx)
239 LEFT JOIN external_synonym es ON
240 es.xref_id = xref.xref_id
241 WHERE xref.external_db_id = exDB.external_db_id ";
244 $sql .=
"AND $constraint";
247 die
"NO constraint???\n";
250 my $sth = $self->prepare($sql) || die
"Could not prepare $sql";
252 return $self->_get_all_dm($sth);
258 my ($self, $sth) = @_;
260 # $sth->bind_param( 1, $dm_dbid, SQL_INTEGER );
263 $sth->execute() || die
"Not able to execute statement handle";
269 while ( my $rowcache = $sth->fetchall_arrayref(undef, $max_rows) ) {
270 while ( my $arrayref = shift( @{$rowcache} ) ) {
271 my ( $dbID, $dbprimaryId,
272 $displayid, $version,
274 $dbname, $db_display_name,
276 $info_type, $info_text,
277 $type, $secondary_db_name,
278 $secondary_db_table, $description
281 if ( !defined($seen{$dbID}) ) {
286 -primary_id => $dbprimaryId,
287 -display_id => $displayid,
288 -version => $version,
289 -release => $release,
291 -priority => $priority,
292 -db_display_name => $db_display_name,
293 -info_type => $info_type,
294 -info_text => $info_text,
296 -secondary_db_name => $secondary_db_name,
297 -secondary_db_table => $secondary_db_table,
298 -description => $description
308 } ## end
while ( my $arrayref = shift...
309 } ## end
while ( my $rowcache = $sth...
318 =head2 get_all_dependents
320 Args[1] : dbID of the DBentry to get the dependents of.
322 Example : my @dependents = @{ $dbe_adaptor->get_all_dependents(1234) };
323 Description: Get a list of DBEntrys that are depenednet on the DBEntry.
324 if an ensembl gene
transcript or translation is given then only
325 the ones on that
object will be given
326 Returntype : listref of DBEntrys. May be empty.
328 Caller : DBEntry->get_all_dependnets
333 sub get_all_dependents {
334 my ( $self, $dbid, $ensembl_object) = @_;
336 if(defined($ensembl_object) and !($ensembl_object->isa(
"Bio::EnsEMBL::Feature") or $ensembl_object->isa(
"Bio::EnsEMBL::Translation"))){
337 die ref($ensembl_object).
" is not an Gene Transcript or Translation";
340 my $constraint =
" dx.master_xref_id = $dbid AND dx.dependent_xref_id = xref.xref_id";
341 if(defined($ensembl_object)){
342 return $self->_get_all_dm_loc_sth($constraint, $ensembl_object);
345 return $self->_get_all_dm_sth($constraint, $ensembl_object);
350 =head2 get_all_masters
352 Args[1] : dbID of the DBentry to get the masters of.
354 Example : my @masters = @{ $dbe_adaptor->get_all_masters(1234) };
355 Description: Get a list of DBEntrys that are the masters of the DBEntry.
356 if an ensembl gene
transcript or translation is given then only
357 the ones on that
object will be given.
358 Returntype : listref of DBEntrys. May be empty.
360 Caller : DBEntry->get_all_masters
365 sub get_all_masters {
366 my ( $self, $dbid, $ensembl_object ) = @_;
368 if(defined($ensembl_object) and !($ensembl_object->isa(
"Bio::EnsEMBL::Feature") or $ensembl_object->isa(
"Bio::EnsEMBL::Translation"))){
369 die ref($ensembl_object).
" is not an Gene Transcript or Translation";
372 my $constraint =
"dx.dependent_xref_id = $dbid AND dx.master_xref_id = xref.xref_id";
374 if(defined($ensembl_object)){
375 return $self->_get_all_dm_loc_sth($constraint, $ensembl_object);
378 return $self->_get_all_dm_sth($constraint, $ensembl_object);
380 # return $self->_get_all_dm($constraint, $ensembl_object);
384 =head fetch_all_by_name
386 Arg [1] :
string $name - The name of the external reference.
387 found in
accession, display_label or synonym
388 Arg [2] : (optional)
string $dbname - The name of the database which
389 the provided name is
for.
391 Example : my $xref = @{$dbea->fetch_all_by_name(
'BRAC2',
'HGNC')}[0];
392 print $xref->description(),
"\n" if($xref);
393 Description: Retrieves list of DBEntrys (xrefs) via a name.
394 The accesion is looked
for first then the synonym and
finally
396 NOTE $dbname
this is optional but adding
this speeds the
397 process up
if you know what you are looking
for.
399 NOTE: In a multi-species database,
this method will
400 return all the entries matching the search criteria, not
401 just the ones associated with the current species.
402 Returntype : Bio::EnsEMBL::DBSQL::DBEntry
403 Exceptions : thrown
if arguments are incorrect
404 Caller : general, domainview
409 sub fetch_all_by_name {
410 my ( $self, $name, $dbname ) = @_;
413 SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label, xref.version,
414 exDB.priority, exDB.db_name, exDB.db_display_name, exDB.db_release,
415 es.synonym, xref.info_type, xref.info_text,
416 exDB.type, exDB.secondary_db_name, exDB.secondary_db_table,
418 FROM (xref, external_db exDB)
419 LEFT JOIN external_synonym es ON
420 es.xref_id = xref.xref_id
421 WHERE (xref.dbprimary_acc = ? or xref.display_label = ?)
422 AND xref.external_db_id = exDB.external_db_id
426 $sql .=
" AND exDB.db_name = ?";
428 my $sth = $self->prepare($sql);
429 $sth->bind_param( 1, $name, SQL_VARCHAR );
430 $sth->bind_param( 2, $name, SQL_VARCHAR );
432 $sth->bind_param( 3 , $dbname, SQL_VARCHAR );
438 my $precache = $sth->fetchall_arrayref( undef, $max_rows ); # need to fetch to ensure rows() works for SQLite
440 if ( !$sth->rows() && lc($dbname) eq 'interpro' ) {
441 # This is a minor hack that means that results still come back even
442 # when a mistake was made and no interpro accessions were loaded into
443 # the xref table. This has happened in the past and had the result of
444 # breaking domainview
448 $sth = $self->prepare(
458 WHERE i.interpro_ac = ?" );
460 $sth->bind_param( 1, $name, SQL_VARCHAR );
467 while ( my $rowcache = $precache || $sth->fetchall_arrayref( undef, $max_rows ) ) {
469 while ( my $arrayref = shift( @{$rowcache} ) ) {
470 my ( $dbID, $dbprimaryId,
471 $displayid, $version,
473 $dbname, $db_display_name,
475 $info_type, $info_text,
476 $type, $secondary_db_name,
477 $secondary_db_table, $description
480 if ( !defined $exDB{$dbID} ) {
485 -primary_id => $dbprimaryId,
486 -display_id => $displayid,
487 -version => $version,
488 -release => $release,
490 -priority => $priority,
491 -db_display_name => $db_display_name,
492 -info_type => $info_type,
493 -info_text => $info_text,
495 -secondary_db_name => $secondary_db_name,
496 -secondary_db_table => $secondary_db_table,
497 -description => $description
499 $exDB{$dbID} = $entrie;
500 push @exDBlist, $entrie;
502 if ($synonym) { $exDB{$dbID}->
add_synonym($synonym) }
504 } ## end
while ( my $arrayref = shift...
505 } ## end
while ( my $rowcache = $sth...
510 } ## end sub fetch_all_by_name
514 =head2 fetch_by_db_accession
516 Arg [1] :
string $dbname - The name of the database which the provided
518 Arg [2] :
string $accession - The accesion of the external reference to
520 Example : my $xref = $dbea->fetch_by_db_accession(
'Interpro',
'IPR003439');
521 print $xref->description(),
"\n" if($xref);
522 Description: Retrieves a DBEntry (xref) via the name of the database
523 it is from and its primary
accession in that database.
524 Undef is returned
if the xref cannot be found in the
526 NOTE: In a multi-species database,
this method will
527 return all the entries matching the search criteria, not
528 just the ones associated with the current species.
529 Returntype : Bio::EnsEMBL::DBSQL::DBEntry
530 Exceptions : thrown
if arguments are incorrect
531 Caller : general, domainview
536 sub fetch_by_db_accession {
537 my ( $self, $dbname, $accession ) = @_;
539 my $sth = $self->prepare(
540 "SELECT xref.xref_id,
546 exDB.db_display_name,
552 exDB.secondary_db_name,
553 exDB.secondary_db_table,
555 FROM (xref, external_db exDB)
556 LEFT JOIN external_synonym es ON
557 es.xref_id = xref.xref_id
558 WHERE xref.dbprimary_acc = ?
559 AND exDB.db_name like ?
560 AND xref.external_db_id = exDB.external_db_id" );
562 $sth->bind_param( 1, $accession, SQL_VARCHAR );
563 $sth->bind_param( 2, $dbname, SQL_VARCHAR );
568 my $precache = $sth->fetchall_arrayref( undef, $max_rows ); # need to fetch to ensure rows() works for SQLite
570 if ( !$sth->rows() && lc($dbname) eq 'interpro' ) {
571 # This is a minor hack that means that results still come back even
572 # when a mistake was made and no interpro accessions were loaded into
573 # the xref table. This has happened in the past and had the result of
574 # breaking domainview
578 $sth = $self->prepare(
588 WHERE i.interpro_ac = ?" );
590 $sth->bind_param( 1, $accession, SQL_VARCHAR );
596 while ( my $rowcache = $precache || $sth->fetchall_arrayref( undef, $max_rows ) ) {
598 while ( my $arrayref = shift( @{$rowcache} ) ) {
599 my ( $dbID, $dbprimaryId,
600 $displayid, $version,
602 $dbname, $db_display_name,
604 $info_type, $info_text,
605 $type, $secondary_db_name,
606 $secondary_db_table, $description
609 if ( !defined($exDB) ) {
614 -primary_id => $dbprimaryId,
615 -display_id => $displayid,
616 -version => $version,
617 -release => $release,
619 -priority => $priority,
620 -db_display_name => $db_display_name,
621 -info_type => $info_type,
622 -info_text => $info_text,
624 -secondary_db_name => $secondary_db_name,
625 -secondary_db_table => $secondary_db_table,
626 -description => $description
634 } ## end
while ( my $arrayref = shift...
635 } ## end
while ( my $rowcache = $sth...
640 } ## end sub fetch_by_db_accession
646 The DBEntry (xref) to be stored
648 The dbID of an
EnsEMBL object to associate with
this external
650 Arg [3] :
string $ensType (
'Transcript',
'Translation',
'Gene')
651 The type of
EnsEMBL object that this external database entry is
652 being associated with.
653 Arg [4] :
boolean $ignore_release
654 If unset or zero, will require that the release
string
655 of the DBEntry
object is identical to the release of the
656 external database. If set and non-zero, will ignore the
658 Example : $dbea->store($db_entry, $transcript_id, 'Transcript');
659 Description: Stores a reference to an external database (if it is not stored
660 already) and associates an
EnsEMBL object of a specified type
661 with the external identifier.
662 Returntype :
int - the dbID of the newly created external refernce
663 Exceptions : thrown when invalid dbID is passed to this method
664 Caller : scripts which load Xrefs and ObjectXrefs, etc. into Ensembl
670 my ( $self, $dbEntry, $ensID, $ensType, $ignore_release, $master_xref ) = @_;
675 # backwards compatibility check:
676 # check if $ensID is an object; if so, use $obj->dbID
680 if ( defined($ensID) ) {
681 if ( $ensID =~ /^\d+$/ ) {
682 $ensembl_id = $ensID;
683 } elsif ( ref($ensID) eq
'Bio::EnsEMBL::Gene'
684 or ref($ensID) eq
'Bio::EnsEMBL::Transcript'
685 or ref($ensID) eq
'Bio::EnsEMBL::Translation'
686 or ref($ensID) eq
'Bio::EnsEMBL::OperonTranscript'
687 or ref($ensID) eq
'Bio::EnsEMBL::Operon'
690 warning(
"You should pass DBEntryAdaptor->store() "
691 .
"a dbID rather than an ensembl object "
692 .
"to store the xref on" );
694 if ( defined( $ensID->dbID() ) ) {
695 $ensembl_id = $ensID->dbID();
697 throw( sprintf(
"%s %s doesn't have a dbID, can't store xref",
698 $ensType, $ensID->display_id() ) );
701 throw(
"Invalid dbID passed to DBEntryAdaptor->store()");
707 # Ensure external_db contains a record of the intended xref source
709 $dbRef = $self->_check_external_db($dbEntry,$ignore_release);
711 # Attempt to insert DBEntry
712 my $xref_id = $self->_store_or_fetch_xref($dbEntry,$dbRef);
713 $dbEntry->dbID($xref_id); #keeps DBEntry in sync with database
714 ### Attempt to create an object->xref mapping
716 if ($ensembl_id) { $object_xref_id = $self->_store_object_xref_mapping($ensembl_id,$dbEntry,$ensType, $ignore_release)};
718 if (defined $master_xref && defined $object_xref_id) { $self->_store_dependent_xref_mapping($object_xref_id, $dbEntry, $master_xref); }
729 The dbentry to update
730 Example : $dbentry_adaptor->update($dbentry);
731 Description: Updates the dbprimary_acc, display_label, version, description, info_type
732 and info_text of a dbentry in the database.
741 my ($self, $dbEntry) = @_;
743 if (!defined $dbEntry || !ref $dbEntry || !$dbEntry->isa(
'Bio::EnsEMBL::DBEntry')) {
744 throw(
"Must update a dbentry object, not a $dbEntry");
747 my $update_dbentry_sql = qq(
749 SET dbprimary_acc = ?,
759 my $dbRef = $self->_check_external_db($dbEntry);
760 my $display_id = $dbEntry->display_id;
761 $display_id =
'' unless defined $display_id; # SQLite doesn
't ignore NOT NULL errors
763 my $sth = $self->prepare($update_dbentry_sql);
765 $sth->bind_param(1, $dbEntry->primary_id,SQL_VARCHAR);
766 $sth->bind_param(2, $display_id,SQL_VARCHAR);
767 $sth->bind_param(3, ($dbEntry->version || q{0}),SQL_VARCHAR);
768 $sth->bind_param(4, $dbEntry->description,SQL_VARCHAR);
769 $sth->bind_param(5, $dbRef,SQL_INTEGER);
770 $sth->bind_param(6, ($dbEntry->info_type || 'NONE
'), SQL_VARCHAR);
771 $sth->bind_param(7, ($dbEntry->info_text || ''), SQL_VARCHAR);
773 $sth->bind_param(8, $dbEntry->dbID(), SQL_INTEGER);
779 sub _store_object_xref_mapping {
781 my $ensembl_id = shift;
783 my $ensembl_type = shift;
784 my $ignore_release = shift;
786 my $dbc = $self->dbc();
787 my $sql_helper = $dbc->sql_helper();
789 if (not defined ($ensembl_type)) { warning("No Ensembl data type provided for new xref");}
792 if ( $dbEntry->analysis() ) {
793 $analysis_id = $self->db()->get_AnalysisAdaptor->store( $dbEntry->analysis() );
796 my $insert_ignore = $self->insert_ignore_clause();
797 my $insert_params = [
798 [$dbEntry->dbID(), SQL_INTEGER],
799 [$ensembl_type, SQL_VARCHAR],
800 [$ensembl_id, SQL_INTEGER],
801 [$dbEntry->linkage_annotation(),SQL_VARCHAR],
802 [$analysis_id, SQL_INTEGER],
804 my $base_object_xref_insert_sql = qq(
805 ${insert_ignore} INTO object_xref
811 VALUES ( ?, ?, ?, ?, ? )
816 #If MySQL we can optimise into a single insert query to deal with object_xref_id retrieval on insert ignore
817 if($dbc->driver() eq 'mysql
') {
818 my $sql = $base_object_xref_insert_sql.'ON DUPLICATE KEY UPDATE object_xref_id=LAST_INSERT_ID(object_xref_id)
';
819 $sql_helper->execute_update(-SQL => $sql, -PARAMS => $insert_params);
820 $object_xref_id = $self->last_insert_id('object_xref_id
', undef, 'object_xref
');
822 #Otherwise we attempt & retrieve on failure
824 my $updated_rows = $sql_helper->execute_update(-SQL => $base_object_xref_insert_sql, -PARAMS => $insert_params);
825 if($updated_rows == 1) {
826 $object_xref_id = $self->last_insert_id('object_xref_id
', undef, 'object_xref
');
829 my $sql = 'select object_xref_id from object_xref where xref_id =? and ensembl_object_type =? and ensembl_id =?
';
831 [$dbEntry->dbID(), SQL_INTEGER],
832 [$ensembl_type, SQL_VARCHAR],
833 [$ensembl_id, SQL_INTEGER],
835 $object_xref_id = $sql_helper->execute_single_result(-SQL => $sql, -PARAMS => $params);
839 $dbEntry->adaptor($self); # hand Adaptor to dbEntry for future use with OntologyXrefs
840 # This is here because everything else assumes it can reuse the $sth scalar ... grrr
843 # Can attempt multiple inserts as object_xref_id is the PRIMARY KEY
844 $sth = $self->prepare( "
845 ${insert_ignore} INTO identity_xref
856 VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" );
857 $sth->bind_param( 1, $object_xref_id, SQL_INTEGER );
858 $sth->bind_param( 2, $dbEntry->xref_identity, SQL_INTEGER );
859 $sth->bind_param( 3, $dbEntry->ensembl_identity, SQL_INTEGER );
860 $sth->bind_param( 4, $dbEntry->xref_start, SQL_INTEGER );
861 $sth->bind_param( 5, $dbEntry->xref_end, SQL_INTEGER );
862 $sth->bind_param( 6, $dbEntry->ensembl_start, SQL_INTEGER );
863 $sth->bind_param( 7, $dbEntry->ensembl_end, SQL_INTEGER );
864 $sth->bind_param( 8, $dbEntry->cigar_line, SQL_LONGVARCHAR );
865 $sth->bind_param( 9, $dbEntry->score, SQL_DOUBLE );
866 $sth->bind_param( 10, $dbEntry->evalue, SQL_DOUBLE );
869 $sth = $self->prepare( "
870 ${insert_ignore} INTO ontology_xref
874 VALUES ( ?, ?, ? )" );
875 foreach my $info ( @{ $dbEntry->get_all_linkage_info() } ) {
876 my ( $linkage_type, $sourceXref ) = @{$info};
877 my $sourceXid = undef;
879 $sourceXref->is_stored( $self->dbc ) || $self->store($sourceXref, undef, undef, $ignore_release);
880 $sourceXid = $sourceXref->dbID;
882 $sth->bind_param( 1, $object_xref_id, SQL_INTEGER );
883 $sth->bind_param( 2, $sourceXid, SQL_INTEGER );
884 $sth->bind_param( 3, $linkage_type, SQL_VARCHAR );
888 $sth = $self->prepare( "
889 ${insert_ignore} INTO associated_xref
896 VALUES ( ?, ?, ?, ?, ?, ? ) " );
898 my $annotext = $dbEntry->get_all_associated_xrefs();
899 foreach my $ax_group (sort keys %{ $annotext }) {
900 my $group = $annotext->{$ax_group};
901 my $gsth = $self->prepare( "
902 INSERT INTO associated_group
905 $gsth->bind_param( 1, $ax_group, SQL_VARCHAR );
907 my $associatedGid = $self->last_insert_id('associated_group_id
', undef, 'associated_group
');
909 foreach my $ax_rank (sort keys %{ $group }) {
910 my @ax = @{ $group->{$ax_rank} };
912 my $associatedXid = undef;
913 my $sourceXid = undef;
916 $self->store($ax[0]);
918 $associatedXid = $ax[0]->dbID;
921 $self->store($ax[1]);
923 $sourceXid = $ax[1]->dbID;
925 if (!defined $associatedXid || !defined $sourceXid) {
928 $sth->bind_param( 1, $object_xref_id, SQL_INTEGER );
929 $sth->bind_param( 2, $associatedXid, SQL_INTEGER );
930 $sth->bind_param( 3, $sourceXid, SQL_INTEGER );
931 $sth->bind_param( 4, $ax[2], SQL_VARCHAR );
932 $sth->bind_param( 5, $associatedGid, SQL_VARCHAR );
933 $sth->bind_param( 6, $ax_rank, SQL_INTEGER );
938 return $object_xref_id;
942 sub _store_dependent_xref_mapping {
944 my $object_xref_id = shift;
946 my $master_xref = shift;
948 my $insert_ignore = $self->insert_ignore_clause();
950 my $sth = $self->prepare( "
951 ${insert_ignore} INTO dependent_xref
955 VALUES ( ?, ?, ?)" );
956 $sth->bind_param( 1, $object_xref_id, SQL_INTEGER );
957 $sth->bind_param( 2, $master_xref->dbID, SQL_INTEGER );
958 $sth->bind_param( 3, $dbEntry->dbID, SQL_INTEGER );
964 =head2 get_external_db_id
967 The external DB name to query by. Supports LIKE statements
968 Arg [2] : String (optional)
969 External DB release to use. If not specified then we
970 will search for NULL db_release entries
971 Arg [3] : Boolean (optional)
972 If true we will never look at the db_release value
973 when querying for an external db id
974 Description: Looks for the internal identifier of an external DB. You can
975 search using direct equality or using like statements specify. We
976 only return one value from this method.
978 If you want more than one entry use get_external_db_ids().
984 sub get_external_db_id {
986 my $db_ids = $self->get_external_db_ids(@_);
987 return shift @$db_ids;
990 =head2 get_external_db_ids
993 The external DB name to query by. Supports LIKE statements
994 Arg [2] : String (optional)
995 External DB release to use. If not specified then we
996 will search for NULL db_release entries
997 Arg [3] : Boolean (optional)
998 If true we will never look at the db_release value
999 when querying for an external db id
1000 Description: Looks for the internal identifier of an external DB. You can
1001 search using direct equality or using like statements specify. We
1002 only return one value from this method. Returns more than one value
1004 Returntype : ArrayRef of Int
1008 sub get_external_db_ids {
1009 my ($self, $db_name, $db_release, $ignore_release) = @_;
1010 my $sql_helper = $self->dbc->sql_helper;
1011 my $sql = 'SELECT external_db_id FROM external_db WHERE db_name
';
1012 if ($db_name =~ /%|_/) {
1018 push @bound_params,$db_name;
1019 unless ($ignore_release) {
1021 $sql .= ' AND db_release = ?
';
1022 push @bound_params,$db_release;
1025 $sql .= ' AND db_release is NULL
';
1029 my @db_ids = @{ $sql_helper->execute_simple(-SQL => $sql, -PARAMS => \@bound_params) };
1033 =head2 get_distinct_external_dbs
1035 Description: Queries the external_db table for all unique
1038 Returntype : ArrayRef of String
1042 sub get_distinct_external_dbs {
1044 my $sql = 'SELECT DISTINCT db_name FROM external_db
';
1045 my $names = $self->dbc->sql_helper->execute_simple(-SQL => $sql);
1046 return [ sort @{$names} ];
1049 =head2 _check_external_db
1051 Arg [1] : DBEntry object
1052 Arg [2] : Ignore version flag
1053 Description: Looks for a record of the given external database
1054 Exceptions : Throws on missing external database entry
1059 sub _check_external_db {
1060 my ($self,$db_entry,$ignore) = @_;
1061 my ($db_name,$db_release);
1062 $db_name = $db_entry->dbname();
1063 $db_release = $db_entry->release();
1064 my $db_id = $self->get_external_db_id($db_name, $db_release, $ignore);
1070 throw( sprintf( "external_db [%s] release [%s] does not exist",
1071 $db_name, $db_release)
1076 =head2 _store_or_fetch_xref
1078 Arg [1] : DBEntry object
1079 Arg [2] : Database accession for external database
1080 Description: Thread-safe method for adding xrefs, or otherwise returning
1081 an xref ID for the inserted or retrieved xref. Also inserts
1082 synonyms for that xref when entire new
1083 Returns : Int - the DB ID of the xref after insertion
1086 sub _store_or_fetch_xref {
1088 my $dbEntry = shift;
1092 my $display_id = $dbEntry->display_id;
1093 $display_id = '' unless defined $display_id; # SQLite doesn't ignore NOT NULL errors
1095 my $insert_ignore = $self->insert_ignore_clause();
1096 my $sth = $self->prepare(
"
1097 ${insert_ignore} INTO xref
1105 VALUES ( ?, ?, ?, ?, ?, ?, ? ) ");
1106 $sth->bind_param(1, $dbEntry->primary_id,SQL_VARCHAR);
1107 $sth->bind_param(2, $display_id,SQL_VARCHAR);
1108 $sth->bind_param(3, ($dbEntry->version || q{0}),SQL_VARCHAR);
1109 $sth->bind_param(4, $dbEntry->description,SQL_VARCHAR);
1110 $sth->bind_param(5, $dbRef,SQL_INTEGER);
1111 $sth->bind_param(6, ($dbEntry->info_type ||
'NONE'), SQL_VARCHAR);
1112 $sth->bind_param(7, ($dbEntry->info_text ||
''), SQL_VARCHAR);
1114 my $count = $sth->execute();
1115 $xref_id = $self->last_insert_id(
'xref_id',undef,
'xref')
if $count > 0;
1118 if ($xref_id) { #insert was successful, store supplementary synonyms
1119 # thread safety no longer an issue.
1120 my $synonym_check_sth = $self->prepare(
1121 "SELECT xref_id, synonym
1122 FROM external_synonym
1126 my $synonym_store_sth = $self->prepare(
1127 "${insert_ignore} INTO external_synonym
1128 ( xref_id, synonym ) VALUES ( ?, ? ) ");
1131 foreach my $syn ( @$synonyms ) {
1132 $synonym_check_sth->bind_param(1,$xref_id,SQL_INTEGER);
1133 $synonym_check_sth->bind_param(2,$syn,SQL_VARCHAR);
1134 $synonym_check_sth->execute();
1135 my ($dbSyn) = $synonym_check_sth->fetchrow_array();
1136 $synonym_store_sth->bind_param(1,$xref_id,SQL_INTEGER);
1137 $synonym_store_sth->bind_param(2,$syn,SQL_VARCHAR);
1138 $synonym_store_sth->execute()
if(!$dbSyn);
1140 $synonym_check_sth->finish();
1141 $synonym_store_sth->finish();
1143 }
else { # xref_id already exists, retrieve it according to fields in the unique key
1144 my $sql =
'SELECT xref_id FROM xref
1145 WHERE dbprimary_acc = ?
1147 AND external_db_id = ?
1150 my $info_type = $dbEntry->info_type() ||
'NONE';
1151 my $info_text = $dbEntry->info_text() || q{};
1152 my $version = $dbEntry->version() || q{0};
1153 $sth = $self->prepare( $sql );
1155 $sth->bind_param(1, $dbEntry->primary_id,SQL_VARCHAR);
1156 $sth->bind_param(2, $version, SQL_VARCHAR);
1157 $sth->bind_param(3, $dbRef, SQL_INTEGER);
1158 $sth->bind_param(4, $info_type, SQL_VARCHAR);
1159 $sth->bind_param(5, $info_text, SQL_VARCHAR);
1164 ($xref_id) = $sth->fetchrow_array();
1167 my $msg =
'Cannot find an xref id for %s (version=%d) with external db id %d.';
1168 throw(sprintf($msg, $dbEntry->primary_id(), $version, $dbRef))
1178 Example :
if($dbID = $db_entry_adaptor->exists($dbe)) {
do stuff; }
1179 Description: Returns the db
id of
this DBEntry
if it exists in
this database
1180 otherwise returns undef. Exists is defined as an entry with
1181 the same external_db and display_id
1183 Exceptions : thrown on incorrect args
1184 Caller : GeneAdaptor::store, TranscriptAdaptor::store
1190 my ($self, $dbe) = @_ ;
1192 unless($dbe && ref $dbe && $dbe->isa(
'Bio::EnsEMBL::DBEntry')) {
1193 throw(
"arg must be a Bio::EnsEMBL::DBEntry not [$dbe]");
1196 my $sth = $self->prepare(
'SELECT x.xref_id
1197 FROM xref x, external_db xdb
1198 WHERE x.external_db_id = xdb.external_db_id
1199 AND x.display_label = ?
1201 AND x.dbprimary_acc = ?');
1203 $sth->bind_param(1,$dbe->display_id,SQL_VARCHAR);
1204 $sth->bind_param(2,$dbe->dbname,SQL_VARCHAR);
1205 $sth->bind_param(3,$dbe->primary_id,SQL_VARCHAR);
1208 my ($dbID) = $sth->fetchrow_array;
1216 =head2 fetch_all_by_Gene
1219 (The gene to retrieve DBEntries
for)
1220 Arg [2] : optional external database name. SQL wildcards are accepted
1221 Arg [3] : optional external_db type. SQL wildcards are accepted
1222 Example : @db_entries = @{$db_entry_adaptor->fetch_all_by_Gene($gene)};
1223 Description: This returns a list of DBEntries associated with
this gene.
1224 Note that
this method was changed in release 15. Previously
1225 it set the DBLinks attribute of the gene passed in to contain
1226 all of the gene,
transcript, and translation xrefs associated
1228 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref
if
1229 there is mapping data, or OntologyXref
if there is linkage data.
1230 Exceptions : thows
if gene
object not passed
1236 sub fetch_all_by_Gene {
1237 my ( $self, $gene, $ex_db_reg, $exdb_type ) = @_;
1239 if(!ref($gene) || !$gene->isa(
'Bio::EnsEMBL::Gene')) {
1240 throw(
"Bio::EnsEMBL::Gene argument expected.");
1243 return $self->_fetch_by_object_type($gene->dbID(),
'Gene', $ex_db_reg, $exdb_type);
1246 =head2 fetch_all_by_Operon
1249 (The operon to retrieve DBEntries
for)
1250 Arg [2] : optional external database name. SQL wildcards are accepted
1251 Arg [3] : optional external_db type. SQL wildcards are accepted
1252 Example : @db_entries = @{$db_entry_adaptor->fetch_all_by_Operon($operon)};
1253 Description: This returns a list of DBEntries associated with
this operon.
1254 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref
if
1255 there is mapping data, or OntologyXref
if there is linkage data.
1256 Exceptions : thows
if operon
object not passed
1261 sub fetch_all_by_Operon {
1262 my ( $self, $gene, $ex_db_reg, $exdb_type ) = @_;
1264 if(!ref($gene) || !$gene->isa(
'Bio::EnsEMBL::Operon')) {
1265 throw(
"Bio::EnsEMBL::Operon argument expected.");
1268 return $self->_fetch_by_object_type($gene->dbID(),
'Operon', $ex_db_reg, $exdb_type);
1272 =head2 fetch_all_by_Transcript
1275 Arg [2] : optional external database name. SQL wildcards are accepted
1276 Arg [3] : optional external_db type. SQL wildcards are accepted
1277 Example : @db_entries = @{$db_entry_adaptor->fetch_all_by_Transcript($trans)};
1278 Description: This returns a list of DBEntries associated with
this
1279 transcript. Note that
this method was changed in release 15.
1280 Previously it set the DBLinks attribute of the gene passed in
1281 to contain all of the gene,
transcript, and translation xrefs
1282 associated with
this gene.
1283 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref
if
1284 there is mapping data, or OntologyXref
if there is linkage data.
1285 Exceptions : throes
if transcript argument not passed
1291 sub fetch_all_by_Transcript {
1292 my ( $self, $trans, $ex_db_reg, $exdb_type ) = @_;
1294 if(!ref($trans) || !$trans->isa(
'Bio::EnsEMBL::Transcript')) {
1295 throw(
"Bio::EnsEMBL::Transcript argument expected.");
1298 return $self->_fetch_by_object_type( $trans->dbID(),
'Transcript', $ex_db_reg, $exdb_type);
1302 =head2 fetch_all_by_Translation
1305 (The translation to fetch database entries
for)
1306 Arg [2] : optional external database name. SQL wildcards are accepted
1307 Arg [3] : optional externaldb type. SQL wildcards are accepted
1308 Example : @db_entries = @{$db_entry_adptr->fetch_all_by_Translation($trans)};
1309 Description: Retrieves external database entries
for an
EnsEMBL translation
1310 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref
if
1311 there is mapping data, or OntologyXref
if there is linkage data.
1312 Exceptions :
throws if translation
object not passed
1318 sub fetch_all_by_Translation {
1319 my ( $self, $trans, $ex_db_reg, $exdb_type ) = @_;
1321 if(!ref($trans) || !$trans->isa(
'Bio::EnsEMBL::Translation')) {
1322 throw(
'Bio::EnsEMBL::Translation argument expected.');
1324 if( ! $trans->dbID ){
1325 warning(
"Cannot fetch_all_by_Translation without a dbID" );
1329 return $self->_fetch_by_object_type( $trans->dbID(),
'Translation', $ex_db_reg, $exdb_type );
1333 =head2 fetch_all_by_RNAProduct
1336 (The rnaproduct to fetch database entries
for)
1337 Arg [2] : optional external database name. SQL wildcards are accepted
1338 Arg [3] : optional externaldb type. SQL wildcards are accepted
1339 Example : @db_entries = @{$db_entry_adptr->fetch_all_by_RNAProduct($rp)};
1340 Description: Retrieves external database entries
for an
EnsEMBL rnaproduct
1341 Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref
if
1342 there is mapping data, or OntologyXref
if there is linkage data.
1343 Exceptions :
throws if rnaproduct
object not passed
1349 sub fetch_all_by_RNAProduct {
1350 my ($self, $rp, $ex_db_reg, $ex_db_type) = @_;
1352 if (!ref($rp) || !$rp->isa(
'Bio::EnsEMBL::RNAProduct')) {
1353 throw(
'Bio::EnsEMBL::RNAProduct argument expected.');
1356 warning(
"Cannot fetch_all_by_RNAProduct without a dbID");
1360 return $self->_fetch_by_object_type($rp->dbID(),
'RNAProduct', $ex_db_reg, $ex_db_type);
1365 =head2 remove_from_object
1368 is to be disassociated from an ensembl
object.
1370 external reference is to be disassociated from
1371 Arg [3] :
string $object_type - The type of the ensembl
object.
1372 E.g.
'Gene',
'Transcript',
'Translation'
1374 # remove all dbentries from this translation
1375 foreach my $dbe (@{$translation->get_all_DBEntries()}) {
1376 $dbe_adaptor->remove($dbe, $translation,
'Translation');
1378 Description: Removes an association between an ensembl
object and a
1379 DBEntry (xref). This does not remove the actual xref from
1380 the database, only its linkage to the ensembl
object.
1382 Exceptions : Throw on incorrect arguments.
1383 Warning
if object or dbentry is not stored in
this database.
1384 Caller : TranscriptAdaptor::remove, GeneAdaptor::remove,
1385 TranslationAdaptor::remove
1390 sub remove_from_object {
1394 my $object_type = shift;
1396 if(!ref($dbe) || !$dbe->isa(
'Bio::EnsEMBL::DBEntry')) {
1397 throw(
"Bio::EnsEMBL::DBEntry argument expected.");
1400 if(!ref($object) || !$dbe->isa(
'Bio::EnsEMBL::Storable')) {
1401 throw(
"Bio::EnsEMBL::Storable argument expected.");
1405 throw(
"object_type string argument expected.");
1408 # make sure both the dbentry and the object it is allegedly linked to
1409 # are stored in this database
1411 if(!$object->is_stored($self->db())) {
1412 warning(
"Cannot remove DBEntries for $object_type " . $object->dbID() .
1413 ". Object is not stored in this database.");
1417 if(!$dbe->is_stored($self->db())) {
1418 warning(
"Cannot remove DBEntry ".$dbe->dbID() .
". Is not stored " .
1419 "in this database.");
1423 # obtain the identifier of the link from the object_xref table
1424 #No need to compare linkage_annotation here
1425 my $sth = $self->prepare
1426 (
"SELECT ox.object_xref_id " .
1427 "FROM object_xref ox ".
1428 "WHERE ox.xref_id = ? " .
1429 "AND ox.ensembl_id = ? " .
1430 "AND ox.ensembl_object_type = ?");
1431 $sth->bind_param(1,$dbe->dbID,SQL_INTEGER);
1432 $sth->bind_param(2,$object->dbID,SQL_INTEGER);
1433 $sth->bind_param(3,$object_type,SQL_VARCHAR);
1436 my ($ox_id) = $sth->fetchrow_array();
1438 if(!$sth->rows() == 1) {
1445 # delete from the tables which contain additional linkage information
1447 $sth = $self->prepare(
"DELETE FROM ontology_xref WHERE object_xref_id = ?");
1448 $sth->bind_param(1,$ox_id,SQL_INTEGER);
1452 $sth = $self->prepare(
"DELETE FROM associated_xref WHERE object_xref_id = ?");
1453 $sth->bind_param(1,$ox_id,SQL_INTEGER);
1457 $sth = $self->prepare(
"DELETE FROM identity_xref WHERE object_xref_id = ?");
1458 $sth->bind_param(1,$ox_id,SQL_INTEGER);
1462 # delete the actual linkage itself
1463 $sth = $self->prepare(
"DELETE FROM object_xref WHERE object_xref_id = ?");
1464 $sth->bind_param(1,$ox_id,SQL_INTEGER);
1472 =head2 _fetch_by_object_type
1474 Arg [1] :
string $ensID
1475 Arg [2] :
string $ensType (
object type to be returned)
1476 Arg [3] : optional $exdbname (external database name)
1477 (may be an SQL pattern containing
'%' which matches any
1478 number of characters)
1479 Arg [4] : optional $exdb_type (external database type)
1480 (may be an SQL pattern containing
'%' which matches any
1481 number of characters)
1482 Example : $self->_fetch_by_object_type( $translation_id,
'Translation' )
1483 Description: Fetches DBEntry by Object type
1484 NOTE: In a multi-species database,
this method will
1485 return all the entries matching the search criteria, not
1486 just the ones associated with the current species.
1489 Returntype : arrayref of DBEntry objects; may be of type IdentityXref
if
1490 there is mapping data, or OntologyXref
if there is linkage data.
1492 Caller : fetch_all_by_Gene
1493 fetch_all_by_Translation
1494 fetch_all_by_Transcript
1499 sub _fetch_by_object_type {
1500 my ( $self, $ensID, $ensType, $exdbname, $exdb_type ) = @_;
1504 if ( !defined($ensID) ) {
1505 throw(
"Can't fetch_by_EnsObject_type without an object");
1508 if ( !defined($ensType) ) {
1509 throw(
"Can't fetch_by_EnsObject_type without a type");
1512 ### TODO - SCHEMA VERSION HACK. Please remove sometime in the future once we do not want backwards compatibility
1513 my $schema_version = $self->schema_version();
1515 # my $sth = $self->prepare("
1516 my $columns_sql = (<<COLUMNS_SQL);
1517 SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label, xref.version,
1519 exDB.db_name, exDB.db_release, exDB.status, exDB.db_display_name,
1520 exDB.secondary_db_name, exDB.secondary_db_table,
1523 idt.xref_identity, idt.ensembl_identity, idt.xref_start,
1524 idt.xref_end, idt.ensembl_start, idt.ensembl_end,
1525 idt.cigar_line, idt.score, idt.evalue, oxr.analysis_id,
1527 xref.info_type, xref.info_text, exDB.type, gx.source_xref_id,
1528 oxr.linkage_annotation, xref.description
1531 my $tables_sql = <<
'TABLES_SQL';
1532 FROM (xref xref, external_db exDB, object_xref oxr)
1533 LEFT JOIN external_synonym es on es.xref_id = xref.xref_id
1534 LEFT JOIN identity_xref idt on idt.object_xref_id = oxr.object_xref_id
1535 LEFT JOIN ontology_xref gx on gx.object_xref_id = oxr.object_xref_id
1538 my $where_sql = <<
'WHERE_SQL';
1539 WHERE xref.xref_id = oxr.xref_id
1540 AND xref.external_db_id = exDB.external_db_id
1541 AND oxr.ensembl_id = ?
1542 AND oxr.ensembl_object_type = ?
1545 if($schema_version >= 72) {
1546 $columns_sql .=
','; #need the extra comma
1547 $columns_sql .= <<
'EXTRA';
1548 ax.xref_id, ax.source_xref_id, ax.condition_type, ax.associated_group_id, ax.rank
1550 $tables_sql .= <<
'EXTRA';
1551 LEFT JOIN associated_xref ax ON ax.object_xref_id = oxr.object_xref_id
1552 LEFT JOIN associated_group ag ON ax.associated_group_id = ag.associated_group_id
1557 #Join the above together into one statement
1558 my $sql =
"$columns_sql
1561 # warn $sql; # uncomment me to see the full SQL generated
1563 if ( defined($exdbname) ) {
1564 if ( index( $exdbname,
'%' ) != -1 ) {
1565 $sql .=
" AND exDB.db_name LIKE "
1566 . $self->dbc()->db_handle()->quote( $exdbname, SQL_VARCHAR );
1568 $sql .=
" AND exDB.db_name = "
1569 . $self->dbc()->db_handle()->quote( $exdbname, SQL_VARCHAR );
1573 if ( defined($exdb_type) ) {
1574 if ( index( $exdb_type,
'%' ) != -1 ) {
1575 $sql .=
" AND exDB.type LIKE "
1576 . $self->dbc()->db_handle()->quote( $exdb_type, SQL_VARCHAR );
1578 $sql .=
" AND exDB.type = "
1579 . $self->dbc()->db_handle()->quote( $exdb_type, SQL_VARCHAR );
1583 my $sth = $self->prepare($sql);
1585 $sth->bind_param( 1, $ensID, SQL_INTEGER );
1586 $sth->bind_param( 2, $ensType, SQL_VARCHAR );
1589 my ( %seen, %linkage_types, %synonyms, %associated_xrefs );
1591 my $max_rows = 1000;
1593 while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
1594 while ( my $arrRef = shift( @{$rowcache} ) ) {
1595 my ( $refID, $dbprimaryId,
1596 $displayid, $version,
1599 $exDB_status, $exDB_db_display_name,
1600 $exDB_secondary_db_name, $exDB_secondary_db_table,
1602 $xrefid, $ensemblid,
1603 $xref_start, $xref_end,
1604 $ensembl_start, $ensembl_end,
1605 $cigar_line, $score,
1606 $evalue, $analysis_id,
1607 $linkage_type, $info_type,
1609 $source_xref_id, $link_annotation,
1610 $description, $associated_xref_id,
1611 $source_associated_xref_id, $condition_type,
1612 $associate_group_id, $associate_group_rank
1616 ( $linkage_type ||
'' ) . ( $source_xref_id ||
'' );
1618 my $associated_key =
1619 ( $condition_type ||
'' )
1620 . ( $source_associated_xref_id ||
'' )
1621 . ( $associated_xref_id ||
'' );
1624 my $analysis = undef;
1625 if ( defined($analysis_id) ) {
1627 $self->db()->get_AnalysisAdaptor()->fetch_by_dbID($analysis_id);
1630 my %obj_hash = (
'adaptor' => $self,
1632 'primary_id' => $dbprimaryId,
1633 'display_id' => $displayid,
1634 'version' => $version,
1635 'release' => $release,
1636 'info_type' => $info_type,
1637 'info_text' => $info_text,
1639 'secondary_db_name' => $exDB_secondary_db_name,
1640 'secondary_db_table' => $exDB_secondary_db_table,
1641 'dbname' => $dbname,
1642 'description' => $description,
1643 'linkage_annotation' => $link_annotation,
1644 'analysis' => $analysis,
1645 'ensembl_object_type' => $ensType,
1646 'ensembl_id' => $ensID );
1648 # Using an outer join on the synonyms as well as on identity_xref,
1649 # we now have to filter out the duplicates (see v.1.18 for
1650 # original). Since there is at most one identity_xref row per
1651 # xref, this is easy enough; all the 'extra' bits are synonyms.
1653 my $associated_xref;
1654 my $source_associated_xref;
1655 if ( !$seen{$refID} ) {
1658 if ( ( defined($xrefid) ) ) { # an xref with similarity scores
1660 $exDB->xref_identity($xrefid);
1661 $exDB->ensembl_identity($ensemblid);
1663 $exDB->cigar_line($cigar_line);
1664 $exDB->xref_start($xref_start);
1665 $exDB->xref_end($xref_end); # was not here before 14th Jan 2009 ????
1666 $exDB->ensembl_start($ensembl_start);
1667 $exDB->ensembl_end($ensembl_end);
1668 $exDB->score($score);
1669 $exDB->evalue($evalue);
1671 } elsif ( defined $linkage_type && $linkage_type ne
"" ) {
1673 $source_xref = ( defined($source_xref_id)
1674 ? $self->fetch_by_dbID($source_xref_id)
1676 $exDB->add_linkage_type( $linkage_type, $source_xref || () );
1677 $linkage_types{$refID}->{$linkage_key} = 1;
1679 # Add associated Xref annotations to the OntologyXref entry.
1680 if ( defined $associated_xref_id && $associated_xref_id ne
"" ) {
1682 $associated_xref = ( defined($associated_xref_id)
1683 ? $self->fetch_by_dbID($associated_xref_id)
1685 $source_associated_xref = ( defined($source_associated_xref_id)
1686 ? $self->fetch_by_dbID($source_associated_xref_id)
1688 if ( defined($associated_xref) ) {
1690 if ( defined $condition_type ) {
1691 $ct = $condition_type;
1693 $exDB->add_linked_associated_xref( $associated_xref, $source_associated_xref, $ct, $associate_group_id, $associate_group_rank );
1701 if ( defined($exDB_status) ) { $exDB->status($exDB_status) }
1703 $exDB->priority($priority);
1704 $exDB->db_display_name($exDB_db_display_name);
1706 push( @out, $exDB );
1707 $seen{$refID} = $exDB;
1709 } ## end
if ( !$seen{$refID} )
1711 # $exDB still points to the same xref, so we can keep adding GO
1712 # evidence tags or synonyms.
1714 if ( defined($synonym) && !$synonyms{$refID}->{$synonym} ) {
1715 if ( defined($synonym) ) {
1716 $seen{$refID}->add_synonym($synonym);
1718 $synonyms{$refID}->{$synonym} = 1;
1721 if ( defined($linkage_type)
1722 && $linkage_type ne
""
1723 && !$linkage_types{$refID}->{$linkage_key}
1724 && check_ref($seen{$refID},
'Bio::EnsEMBL::OntologyXref') )
1726 $source_xref = ( defined($source_xref_id)
1727 ? $self->fetch_by_dbID($source_xref_id)
1730 ->add_linkage_type( $linkage_type, $source_xref || () );
1731 $linkage_types{$refID}->{$linkage_key} = 1;
1734 if ( defined($associated_xref_id)
1735 && $associated_xref_id ne
""
1736 && !$associated_xrefs{$refID}->{$associated_key} )
1738 $associated_xref = ( defined($associated_xref_id)
1739 ? $self->fetch_by_dbID($associated_xref_id)
1741 $source_associated_xref = ( defined($source_associated_xref_id)
1742 ? $self->fetch_by_dbID($source_associated_xref_id)
1744 if ( defined($associated_xref) ) {
1746 if ( defined $condition_type ) {
1747 $ct = $condition_type;
1749 $seen{$refID}->add_linked_associated_xref( $associated_xref, $source_associated_xref, $ct, $associate_group_id, $associate_group_rank );
1752 $linkage_types{$refID}->{$linkage_key} = 1;
1755 } ## end
while ( my $arrRef = shift...
1756 } ## end
while ( my $rowcache = $sth...
1759 } ## end sub _fetch_by_object_type
1761 =head2 list_gene_ids_by_external_db_id
1763 Arg [1] :
string $external_id
1764 Example : @gene_ids = $dbea->list_gene_ids_by_external_db_id(1020);
1765 Description: Retrieve a list of geneid by an external identifier that
1766 is linked to any of the genes transcripts, translations
1768 NOTE: If more than one external identifier has the
1769 same primary
accession then genes
for each of these is
1771 NOTE: In a multi-species database,
this method will
1772 return all the entries matching the search criteria, not
1773 just the ones associated with the current species.
1774 Returntype : list of ints
1781 sub list_gene_ids_by_external_db_id {
1782 my ($self,$external_db_id, $linkage_type) = @_;
1784 my %T =
map { ($_, 1) }
1785 $self->_type_by_external_db_id( $external_db_id,
'Translation',
'gene', $linkage_type ),
1786 $self->_type_by_external_db_id( $external_db_id,
'Transcript',
'gene', $linkage_type ),
1787 $self->_type_by_external_db_id( $external_db_id,
'Gene', undef, $linkage_type );
1791 =head2 list_gene_ids_by_extids
1793 Arg [1] :
string $external_name
1794 Arg [2] : (optional)
string $external_db_name
1795 Arg [3] : Boolean
override, see _type_by_external_id
1796 Example : @gene_ids = $dbea->list_gene_ids_by_extids(
'CDPX');
1797 Description: Retrieve a list of geneid by an external identifier that is
1798 linked to any of the genes transcripts, translations or the
1800 Returntype : list of ints
1807 sub list_gene_ids_by_extids {
1808 my ( $self, $external_name, $external_db_name, $override ) = @_;
1810 my %T =
map { ( $_, 1 ) }
1811 $self->_type_by_external_id( $external_name,
'Translation',
'gene',
1812 $external_db_name, $override ),
1813 $self->_type_by_external_id( $external_name,
'Transcript',
'gene',
1814 $external_db_name, $override ),
1815 $self->_type_by_external_id( $external_name,
'Gene', undef,
1816 $external_db_name, $override );
1822 =head2 list_transcript_ids_by_extids
1824 Arg [1] :
string $external_name
1825 Arg [2] : (optional)
string $external_db_name
1826 Arg [3] : Boolean
override, see _type_by_external_id
1827 Example : @tr_ids = $dbea->list_transcript_ids_by_extids(
'BCRA2');
1828 Description: Retrieve a list
transcript ids by an external identifier that
1829 is linked to any of the genes transcripts, translations or the
1831 Returntype : list of ints
1838 sub list_transcript_ids_by_extids {
1839 my ( $self, $external_name, $external_db_name, $override ) = @_;
1841 my %T =
map { ( $_, 1 ) }
1842 $self->_type_by_external_id( $external_name,
'Translation',
1843 'transcript', $external_db_name, $override
1845 $self->_type_by_external_id( $external_name,
'Transcript', undef,
1846 $external_db_name, $override );
1851 sub list_transcript_ids_by_external_db_id {
1852 my ( $self, $external_db_id, $linkage_type) = @_;
1853 my %T =
map { ( $_, 1 ) }
1854 $self->_type_by_external_db_id( $external_db_id,
'Translation',
'transcript', $linkage_type ),
1855 $self->_type_by_external_db_id( $external_db_id,
'Transcript',
'transcript', $linkage_type );
1859 =head2 list_translation_ids_by_extids
1861 Arg [1] :
string $external_name
1862 Arg [2] : (optional)
string $external_db_name
1863 Arg [3] : Boolean
override, see _type_by_external_id
1864 Example : @tr_ids = $dbea->list_translation_ids_by_extids(
'GO:0004835');
1865 Description: Gets a list of translation IDs by external display IDs
1866 Returntype : list of Ints
1873 sub list_translation_ids_by_extids {
1874 my ( $self, $external_name, $external_db_name, $override ) = @_;
1877 $self->_type_by_external_id( $external_name,
'Translation', undef,
1878 $external_db_name, $override );
1881 sub list_translation_ids_by_external_db_id {
1882 my ( $self, $external_db_id, $linkage_type) = @_;
1883 return $self->_type_by_external_db_id( $external_db_id,
'Translation', undef, $linkage_type ),
1887 =head2 list_rnaproduct_ids_by_extids
1889 Arg [1] :
string $external_name
1890 Arg [2] : (optional)
string $external_db_name
1891 Arg [3] : Boolean
override, see _type_by_external_id
1892 Example : @rp_ids = $dbea->list_rnaproduct_ids_by_extids(
'GO:0004835');
1893 Description: Gets a list of rnaproduct IDs by external display IDs
1894 Returntype : list of Ints
1901 sub list_rnaproduct_ids_by_extids {
1902 my ($self, $external_name, $external_db_name, $override) = @_;
1904 return $self->_type_by_external_id($external_name,
'RNAProduct', undef,
1905 $external_db_name, $override);
1909 =head2 _type_by_external_id
1911 Arg [1] :
string $name - dbprimary_acc
1912 Arg [2] :
string $ensType - ensembl_object_type
1913 Arg [3] : (optional)
string $extraType
1914 Arg [4] : (optional)
string $external_db_name
1915 other
object type to be returned
1916 Arg [5] : Boolean
override to force _ to be treated as an SQL
'any'
1917 This is usually optimised out
for query speed due to
1918 large numbers of names like NM_00...
1919 Example : $self->_type_by_external_id($name,
'Translation');
1920 NOTE: In a multi-species database,
this method will
1921 return all the entries matching the search criteria, not
1922 just the ones associated with the current species.
1923 SQL wildcards can be used in the external id,
1924 but overly
generic queries (two characters) will be prevented.
1926 Returntype : list of dbIDs (gene_id, transcript_id, etc.)
1928 Caller : list_translation_ids_by_extids
1929 translationids_by_extids
1935 sub _type_by_external_id {
1936 my ( $self, $name, $ensType, $extraType, $external_db_name, $override ) = @_;
1938 # $name has SQL wildcard support
1939 # = or LIKE put into SQL statement, and open queries like % or A% are rejected.
1940 my $comparison_operator;
1941 if ($name =~ /[_%\[]/ ) {
1942 $comparison_operator =
"LIKE";
1943 if ($name =~ /^.?%/ && !$override) {
1944 warn
"External $ensType name $name is too vague and will monopolise database resources. Please use a more specific $ensType name.\n";
1947 elsif ($name =~ /^\w\w_/ && !$override) {
1948 # For entries such as NM_00000065, escape the _ so that SQL LIKE does not have to scan entire table
1949 # Escape only the _ in the third character position
1950 $name =~ s/(?<=\w\w)(?=_)/\\/;
1954 $comparison_operator =
"=";
1960 my $ID_sql =
'oxr.ensembl_id';
1962 if ( defined($extraType) ) {
1963 if ( lc($extraType) eq
'translation' ) {
1964 $ID_sql =
'tl.translation_id';
1966 $ID_sql =
"t.${extraType}_id";
1969 if ( lc($ensType) eq
'translation' ) {
1970 $from_sql =
'transcript t, translation tl, ';
1972 t.transcript_id = tl.transcript_id AND
1973 tl.translation_id = oxr.ensembl_id AND
1974 t.is_current = 1 AND
1977 $from_sql =
'transcript t, ';
1980 .
'_id = oxr.ensembl_id AND '
1981 .
't.is_current = 1 AND ';
1985 my $multispecies = $self->db()->is_multispecies();
1987 if ( lc($ensType) eq
'gene' ) {
1988 $from_sql =
'gene g, ';
1989 $from_sql .=
'seq_region s, coord_system cs, ' if $multispecies;
1991 $where_sql =
'g.gene_id = oxr.ensembl_id AND g.is_current = 1 AND ';
1993 $where_sql .= <<
'SQL';
1994 g.seq_region_id = s.seq_region_id AND
1995 s.coord_system_id = cs.coord_system_id AND
1996 cs.species_id = ? AND
2000 elsif ( lc($ensType) eq
'transcript' ) {
2001 $from_sql =
'transcript t, ';
2002 $from_sql .=
'seq_region s, coord_system cs, ' if $multispecies;
2004 $where_sql =
't.transcript_id = oxr.ensembl_id AND t.is_current = 1 AND ';
2006 $where_sql .= <<
'SQL';
2007 t.seq_region_id = s.seq_region_id AND
2008 s.coord_system_id = cs.coord_system_id AND
2009 cs.species_id = ? AND
2013 elsif ( lc($ensType) eq
'translation' ) {
2014 $from_sql =
'translation tl, transcript t, ';
2015 $from_sql .=
'seq_region s, coord_system cs, ' if $multispecies;
2017 $where_sql =
't.transcript_id = tl.transcript_id AND tl.translation_id = oxr.ensembl_id AND t.is_current = 1 AND ';
2019 $where_sql .= <<
'SQL';
2020 t.seq_region_id = s.seq_region_id AND
2021 s.coord_system_id = cs.coord_system_id AND
2022 cs.species_id = ? AND
2027 if ( defined($external_db_name) ) {
2028 # Involve the 'external_db' table to limit the hits to a particular
2029 # external database.
2031 $from_sql .=
'external_db xdb, ';
2034 . $self->dbc()->db_handle()->quote( $external_db_name .
'%' )
2035 .
' AND xdb.external_db_id = x.external_db_id AND';
2045 x.dbprimary_acc $comparison_operator ?
2046 AND x.xref_id = oxr.xref_id
2047 AND oxr.ensembl_object_type = ?
2056 x.display_label $comparison_operator ?
2057 AND x.xref_id = oxr.xref_id
2058 AND oxr.ensembl_object_type = ?
2061 if ( defined($external_db_name) ) {
2062 # If we are given the name of an external database, we need to join
2063 # between the 'xref' and the 'object_xref' tables on 'xref_id'.
2068 external_synonym syn,
2072 syn.synonym $comparison_operator ?
2073 AND syn.xref_id = oxr.xref_id
2074 AND oxr.ensembl_object_type = ?
2075 AND x.xref_id = oxr.xref_id));
2078 # If we weren't given an external database name, we can get away
2079 # with less joins here.
2084 external_synonym syn,
2087 syn.synonym $comparison_operator ?
2088 AND syn.xref_id = oxr.xref_id
2089 AND oxr.ensembl_object_type = ?));
2094 my $h = $self->dbc()->sql_helper();
2095 my @params = ([$name, SQL_VARCHAR], [$ensType, SQL_VARCHAR]);
2096 unshift(@params, [$self->species_id(), SQL_INTEGER] )
if $multispecies;
2097 foreach my $query (@queries) {
2098 $h->execute_no_return(-SQL => $query, -PARAMS => \@params, -CALLBACK => sub {
2105 return keys(%result);
2107 } ## end sub _type_by_external_id
2109 =head2 _type_by_external_db_id
2111 Arg [1] : integer $type - external_db_id
2112 Arg [2] :
string $ensType - ensembl_object_type
2113 Arg [3] : (optional)
string $extraType
2114 other
object type to be returned. This references the _id fields of
2115 the
transcript table, and
if left unset defaults to the translation_id
2116 from the translation table.
2117 Arg [4] : (optional)
string $linkage_type
2118 provides a link to ontology_xref and limits by an ontology linkage type
2119 Example : $self->_type_by_external_db_id(1030,
'Translation');
2121 NOTE: In a multi-species database,
this method will
2122 return all the entries matching the search criteria, not
2123 just the ones associated with the current species.
2124 Returntype : list of dbIDs (gene_id, transcript_id, etc.)
2126 Caller : list_translation_ids_by_extids
2127 translationids_by_extids
2133 sub _type_by_external_db_id {
2134 my ($self, $external_db_id, $ensType, $extraType, $linkage_type) = @_;
2135 throw "No external DB identifer given" unless defined $external_db_id;
2136 throw "No Ensembl type given" unless defined $ensType;
2140 my $ID_sql =
"oxr.ensembl_id";
2141 my $lcEnsType = lc($ensType);
2143 if (defined $extraType) {
2144 if (lc($extraType) eq
'translation') {
2145 $ID_sql =
"tl.translation_id";
2147 $ID_sql =
"t.${extraType}_id";
2150 if ($lcEnsType eq
'translation') {
2151 $from_sql =
'transcript t, translation tl, ';
2153 t.transcript_id = tl.transcript_id AND
2154 tl.translation_id = oxr.ensembl_id AND
2155 t.is_current = 1 AND
2158 $from_sql =
'transcript t, ';
2159 $where_sql =
't.'.$lcEnsType.
'_id = oxr.ensembl_id AND '.
2160 't.is_current = 1 AND ';
2164 if ($lcEnsType eq
'gene') {
2165 $from_sql =
'gene g, ';
2166 $where_sql =
'g.gene_id = oxr.ensembl_id AND g.is_current = 1 AND ';
2167 } elsif ($lcEnsType eq
'transcript') {
2168 $from_sql =
'transcript t, ';
2169 $where_sql =
't.transcript_id = oxr.ensembl_id AND t.is_current = 1 AND ';
2170 } elsif ($lcEnsType eq
'translation') {
2171 $from_sql =
'transcript t, translation tl, ';
2173 t.transcript_id = tl.transcript_id AND
2174 tl.translation_id = oxr.ensembl_id AND
2175 t.is_current = 1 AND
2180 $from_sql .=
'ontology_xref ontx, ';
2181 $where_sql .=
'oxr.object_xref_id = ontx.object_xref_id AND ';
2182 $where_sql .=
'ontx.linkage_type =? AND';
2187 FROM $from_sql xref x, object_xref oxr
2188 WHERE $where_sql x.external_db_id = ? AND
2189 x.xref_id = oxr.xref_id AND oxr.ensembl_object_type= ?";
2192 push(@params, [$linkage_type, SQL_VARCHAR])
if $linkage_type;
2194 [$external_db_id, SQL_INTEGER],
2195 [$ensType, SQL_VARCHAR],
2199 $self->dbc()->sql_helper()->execute_no_return(-SQL => $query, -PARAMS => \@params, -CALLBACK => sub {
2205 return keys(%result);
2206 } ## end _type_by_external_db_id
2209 =head2 fetch_all_by_description
2211 Arg [1] :
string description to search
for. Include % etc in
this string
2212 Arg [2] : <optional>
string $dbname. Name of the database to search
2214 Example : @canc_refs = @{$db_entry_adaptor->fetch_all_by_description(
"%cancer%")};
2215 @db_entries = @{$db_entry_adaptor->fetch_all_by_description(
"%cancer%",
"MIM_MORBID")};
2216 Description: Retrieves DBEntries that match the description.
2217 Optionally you can search on external databases type.
2218 NOTE: In a multi-species database,
this method will
2219 return all the entries matching the search criteria, not
2220 just the ones associated with the current species.
2221 Returntype : ref to array of Bio::EnsEMBL::DBSQL::DBEntry
2228 sub fetch_all_by_description {
2229 my ( $self, $description, $dbname ) = @_;
2234 "SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label,
2237 exDB.db_name, exDB.db_display_name, exDB.db_release, es.synonym,
2238 xref.info_type, xref.info_text, exDB.type, exDB.secondary_db_name,
2239 exDB.secondary_db_table, xref.description
2240 FROM (xref, external_db exDB)
2241 LEFT JOIN external_synonym es on es.xref_id = xref.xref_id
2242 WHERE xref.description like ?
2243 AND xref.external_db_id = exDB.external_db_id";
2245 if ( defined($dbname) ) { $sql .=
" AND exDB.db_name = ? " }
2247 my $sth = $self->prepare($sql);
2249 $sth->bind_param( 1, $description, SQL_VARCHAR );
2251 if ( defined($dbname) ) {
2252 $sth->bind_param( 2, $dbname, SQL_VARCHAR );
2257 my $max_rows = 1000;
2259 while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
2260 while ( my $arrayref = shift( @{$rowcache} ) ) {
2261 my ( $dbID, $dbprimaryId,
2262 $displayid, $version,
2264 $ex_dbname, $db_display_name,
2266 $info_type, $info_text,
2267 $type, $secondary_db_name,
2268 $secondary_db_table, $description
2275 -primary_id => $dbprimaryId,
2276 -display_id => $displayid,
2277 -version => $version,
2278 -release => $release,
2279 -dbname => $ex_dbname,
2280 -priority => $priority,
2281 -db_display_name => $db_display_name,
2282 -info_type => $info_type,
2283 -info_text => $info_text,
2285 -secondary_db_name => $secondary_db_name,
2286 -secondary_db_table => $secondary_db_table,
2287 -description => $description
2292 push @results, $exDB;
2294 } ## end
while ( my $arrayref = shift...
2295 } ## end
while ( my $rowcache = $sth...
2300 } ## end sub fetch_all_by_description
2303 =head2 fetch_all_by_source
2305 Arg [1] :
string source to search
for. Include % etc in
this string
2306 if you want to use SQL patterns
2308 Example : @unigene_refs = @{$db_entry_adaptor->fetch_all_by_source(
"%unigene%")};
2309 Description: Retrieves DBEntrys that match the source name.
2310 Returntype : ref to array of Bio::EnsEMBL::DBSQL::DBEntry
2317 sub fetch_all_by_source {
2318 my ( $self, $source ) = @_;
2323 "SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label,
2326 exDB.db_name, exDB.db_display_name, exDB.db_release, es.synonym,
2327 xref.info_type, xref.info_text, exDB.type, exDB.secondary_db_name,
2328 exDB.secondary_db_table, xref.description
2329 FROM (xref, external_db exDB)
2330 LEFT JOIN external_synonym es on es.xref_id = xref.xref_id
2331 WHERE exDB.db_name like ?
2332 AND xref.external_db_id = exDB.external_db_id";
2335 my $sth = $self->prepare($sql);
2337 $sth->bind_param( 1, $source, SQL_VARCHAR );
2341 my $max_rows = 1000;
2343 while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
2344 while ( my $arrayref = shift( @{$rowcache} ) ) {
2345 my ( $dbID, $dbprimaryId,
2346 $displayid, $version,
2348 $dbname, $db_display_name,
2350 $info_type, $info_text,
2351 $type, $secondary_db_name,
2352 $secondary_db_table, $description
2359 -primary_id => $dbprimaryId,
2360 -display_id => $displayid,
2361 -version => $version,
2362 -release => $release,
2364 -priority => $priority,
2365 -db_display_name => $db_display_name,
2366 -info_type => $info_type,
2367 -info_text => $info_text,
2369 -secondary_db_name => $secondary_db_name,
2370 -secondary_db_table => $secondary_db_table,
2371 -description => $description
2376 push @results, $exDB;
2378 } ## end
while ( my $arrayref = shift...
2379 } ## end
while ( my $rowcache = $sth...
2384 } ## end sub fetch_all_by_source
2387 =head2 fetch_all_synonyms
2389 Arg [1] : dbID of DBEntry to fetch synonyms
for. Used in lazy loading of synonyms.
2391 Example : @canc_refs = @{$db_entry_adaptor->fetch_all_synonyms(1234)};
2392 Description: Fetches the synonyms
for a particular DBEntry.
2393 Returntype : listref of synonyms. List referred to may be empty
if there are no synonyms.
2401 sub fetch_all_synonyms {
2402 my ( $self, $dbID ) = @_;
2407 $self->prepare(
"SELECT synonym "
2408 .
"FROM external_synonym "
2409 .
"WHERE xref_id = ?" );
2411 $sth->bind_param( 1, $dbID, SQL_INTEGER );
2416 $sth->bind_col(1, \$synonym);
2418 while ( $sth->fetch() ) {
2419 push( @synonyms, $synonym );
2426 =head2 get_db_name_from_external_db_id
2428 Arg [1] : external_dbid of database to get the database_name
2429 Example : my $db_name = $db_entry_adaptor->get_db_name_from_external_db_id(1100);
2430 Description: Gets the database name
for a certain external_db_id
2438 sub get_db_name_from_external_db_id{
2440 my $external_db_id = shift;
2442 my $sth = $self->prepare(
"SELECT db_name FROM external_db WHERE external_db_id = ?");
2444 $sth->bind_param(1, $external_db_id, SQL_INTEGER);
2446 my ($db_name) = $sth->fetchrow_array();