diff --git a/modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm index b4151091e79d8c281043fd67a0b404d008ced2ca..fda73e61bf2d4ad4d33a19e36fa7878c032463ff 100644 --- a/modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm +++ b/modules/Bio/EnsEMBL/DBSQL/DBEntryAdaptor.pm @@ -305,6 +305,46 @@ sub store { + +=head2 exists + + Arg [1] : Bio::EnsEMBL::DBEntry $dbe + Example : if($dbID = $db_entry_adaptor->exists($dbe)) { do stuff; } + Description: Returns the db id of this DBEntry if it exists in this database + otherwise returns undef. Exists is defined as an entry with + the same external_db and display_id + Returntype : int + Exceptions : thrown on incorrect args + Caller : GeneAdaptor::store, TranscriptAdaptor::store + +=cut + +sub exists { + my $self = shift; + my $dbe = shift; + + unless($dbe && ref $dbe && $dbe->isa('Bio::EnsEMBL::DBEntry')) { + $self->throw("arg must be a Bio::EnsEMBL::DBEntry not [$dbe]"); + } + + my $sth = $self->prepare('SELECT x.xref_id + FROM xref x, external_db xdb + WHERE x.external_db_id = xdb.external_db_id + AND x.display_label = ? + AND xdb.db_name = ?'); + + $sth->execute($dbe->display_id, $dbe->external_db); + + my ($dbID) = $sth->fetchrow_array; + + $sth->finish; + + return $dbID; +} + + + + =head2 fetch_all_by_Gene Arg [1] : Bio::EnsEMBL::Gene $gene diff --git a/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm index 312c82ce45ce0718d0640fd96ab51e2c5b21a915..99041ae0ba52778e90f049b2b9abd4ea18c8cb5b 100644 --- a/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm +++ b/modules/Bio/EnsEMBL/DBSQL/GeneAdaptor.pm @@ -745,12 +745,27 @@ sub store { } + # + # store the dbentries associated with this gene + # my $dbEntryAdaptor = $self->db->get_DBEntryAdaptor(); foreach my $dbl ( @{$gene->get_all_DBLinks} ) { $dbEntryAdaptor->store( $dbl, $gene_dbID, "Gene" ); } + # + # Update the genes display xref if it is set + # + my $display_xref = $gene->display_xref; + if($display_xref) { + if(my $dbe_id = $dbEntryAdaptor->exists($display_xref)) { + my $dispxref_sth = $self->prepare('UPDATE gene SET display_xref_id = ? + WHERE gene_id = ?'); + $dispxref_sth->execute($dbe_id, $gene_dbID); + } + } + # write exons transcripts and exon_transcript table @@ -912,68 +927,30 @@ sub deleteObj { } -=head2 get_external_name - - Arg [1] : int $dbID - the database identifier of the gene whose external name is - sought - Example : $external_name = $gene_adaptor->get_external_name(42); - Description: Retrieves the external name for a gene. This is implemented - by joining across the xref and gene tables, using the - display_xref_id column. - Returntype : string - Exceptions : thrown if $dbId arg is not defined - Caller : general - -=cut - -sub get_external_name { - my ($self, $dbID) = @_; - - if( !defined $dbID ) { - $self->throw("Must call with a dbID"); - } - - my $sth = $self->prepare("SELECT x.display_label - FROM gene g, - xref x - WHERE g.gene_id = ? - AND g.display_xref_id = x.xref_id - "); - $sth->execute($dbID); - - my ($xref) = $sth->fetchrow_array(); - if( !defined $xref ) { - return undef; - } - - return $xref; -} - + -=head2 get_external_dbname +=head2 get_display_xref - Arg [1] : int $dbID - the database identifier of the gene for which the name of - external db from which its external name is derived. - Example : $external_dbname = $gene_adaptor->get_external_dbname(42); - Description: Retrieves the external db name for a gene from which its external - name is derived.. This is implemented by joining across the xref, - gene and external_db tables, using the display_xref_id column. - Returntype : string + Arg [1] : Bio::EnsEMBL::Gene $gene + retrieve display_xref for this gene + Example : none + Description: Retrieves the display_xref for a gene. + Returntype : Bio::EnsEMBL::DBEntry Exceptions : thrown if $dbId arg is not defined Caller : general =cut -sub get_external_dbname { - my ($self, $dbID) = @_; +sub get_display_xref { + my ($self, $gene ) = @_; - if( !defined $dbID ) { - $self->throw("Must call with a dbID"); + if( !defined $gene ) { + $self->throw("Must call with a Gene object"); } - my $sth = $self->prepare("SELECT e.db_name + my $sth = $self->prepare("SELECT e.db_name, + x.display_label, + x.xref_id FROM gene g, xref x, external_db e @@ -981,49 +958,22 @@ sub get_external_dbname { AND g.display_xref_id = x.xref_id AND x.external_db_id = e.external_db_id "); - $sth->execute($dbID); - - my ($db_name) = $sth->fetchrow_array(); - if( !defined $db_name ) { - return undef; - } - - return $db_name; -} - - -=head2 get_display_xref_id - - Arg [1] : int $dbID - the database identifier of the gene for which the name of - external db from which its external name is derived. - Example : $external_dbname = $gene_adaptor->get_display_xref_id(42); - Description: Retrieves the display_xref_id for a gene. - Returntype : int - Exceptions : thrown if $dbId arg is not defined - Caller : general - -=cut - -sub get_display_xref_id { - my ($self, $dbID) = @_; - - if( !defined $dbID ) { - $self->throw("Must call with a dbID"); - } + $sth->execute( $gene->dbID ); - my $sth = $self->prepare("SELECT display_xref_id - FROM gene - WHERE gene_id = ? - "); - $sth->execute($dbID); - my ($xref_id) = $sth->fetchrow_array(); + my ($db_name, $display_label, $xref_id ) = $sth->fetchrow_array(); if( !defined $xref_id ) { return undef; } - - return $xref_id; + my $db_entry = Bio::EnsEMBL::DBEntry->new + ( + -dbid => $xref_id, + -adaptor => $self->db->get_DBEntryAdaptor(), + -dbname => $db_name, + -display_id => $display_label + ); + + return $db_entry; } @@ -1048,40 +998,30 @@ sub update { my $update = 0; if( !defined $gene || !ref $gene || !$gene->isa('Bio::EnsEMBL::Gene') ) { - $self->throw("Must update a gene object, not a $gene"); + $self->throw("Must update a gene object, not a $gene"); } - my $sth = $self->prepare("SELECT type, analysis_id, transcript_count, display_xref_id - FROM gene - WHERE gene_id = ? - "); - - $sth->execute($gene->dbID); - - if ( my ($type, $analysis, $transcripts, $dxref_id) = $sth->fetchrow_array() ){ - - if ( $dxref_id != $gene->display_xref ) { $update = 1; } - elsif ( $type ne $gene->type ) { $update = 1; } - elsif ( $analysis != $gene->analysis->dbID ) { $update = 1; } - elsif ( $transcripts != scalar(@{$gene->get_all_Transcripts})) { - $self->warn("Trying to update the number of transcripts on a stored gene. Not yet implemented."); - } + my $xref_id; + if($gene->display_xref && $gene->display_xref->dbID) { + $xref_id = $gene->display_xref->dbID; + } else { + $xref_id = 0; + } - if ( $update ) { + my $tcount = scalar @{$gene->get_all_Transcripts}; - $sth = $self->prepare("UPDATE gene - SET type = ?, - analysis_id = ?, - display_xref_id = ? - WHERE gene_id = ? - "); + $sth = $self->prepare("UPDATE gene + SET type = ?, + analysis_id = ?, + display_xref_id = ?, + transcript_count = ? + WHERE gene_id = ?"); - $sth->execute($gene->type, $gene->analysis->dbID, $gene->display_xref, $gene->dbID); - } - } - else { - $self->warn("Trying to update a gene that is not in the database. Try store\'ing first."); - } + $sth->execute($gene->type, + $gene->analysis->dbID, + $xref_id, + $tcout, + $gene->dbID); } diff --git a/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm b/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm index 59170946761f26af3f02455e49ab291683c587da..f9cadca79279514e567a4e2e37881b2fadf7a549 100644 --- a/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm +++ b/modules/Bio/EnsEMBL/DBSQL/TranscriptAdaptor.pm @@ -202,13 +202,13 @@ sub store { # ok - now load this line in my $tst = $self->prepare(" insert into transcript ( gene_id, translation_id, exon_count, display_xref_id ) - values ( ?, ?, ?, ?) + values ( ?, ?, ?, 0) "); if( defined $translation ) { - $tst->execute( $gene_dbID, $translation->dbID(), $exon_count, $xref_id ); + $tst->execute( $gene_dbID, $translation->dbID(), $exon_count ); } else { - $tst->execute( $gene_dbID, 0, $exon_count, $xref_id ); + $tst->execute( $gene_dbID, 0, $exon_count ); } my $transc_dbID = $tst->{'mysql_insertid'}; @@ -218,8 +218,23 @@ sub store { foreach my $dbl ( @{$transcript->get_all_DBLinks} ) { $dbEntryAdaptor->store( $dbl, $transc_dbID, "Transcript" ); + } + # + # Update transcript to point to display xref if it is set + # + if(my $dxref = $transcript->display_xref) { + if(my $dxref_id = $dbEntryAdaptor->exists($dxref)) { + $self->prepare( "update transcript set display_xref_id = ". + $dxref_id . " where transcript_id = ".$transc_dbID); + $self->execute(); + $dxref->dbID($dxref_id); + $dxref->adaptor($dbEntryAdaptor); + } + } + + my $etst = $self->prepare("insert into exon_transcript (exon_id,transcript_id,rank) values (?,?,?)"); my $rank = 1; foreach my $exon ( @{$transcript->get_all_Exons} ) { @@ -374,68 +389,31 @@ sub remove { -=head2 get_external_name - Arg [1] : int $dbID - the database identifier of the transcript whose external name - is sought - Example : $external_name = $transcript_adaptor->get_external_name(42); - Description: Retrieves the external name for a transcript. This is implemented - by joining across the xref and transcript tables, using the - display_xref_id column. - Returntype : string - Exceptions : thrown if $dbId arg is not defined - Caller : general -=cut - -sub get_external_name { - my ($self, $dbID) = @_; - - if( !defined $dbID ) { - $self->throw("Must call with a dbID"); - } - - my $sth = $self->prepare("SELECT x.display_label - FROM transcript t, - xref x - WHERE t.transcript_id = ? - AND t.display_xref_id = x.xref_id - "); - $sth->execute($dbID); - - my ($xref) = $sth->fetchrow_array(); - if( !defined $xref ) { - return undef; - } - - return $xref; -} - - -=head2 get_external_dbname +=head2 get_display_xref Arg [1] : int $dbID - the database identifier of the transcript for which the name of - external db from which its external name is derived. - Example : $external_dbname = $transcript_adaptor->get_external_dbname(42); - Description: Retrieves the external db name for a transcript from which its external - name is derived.. This is implemented by joining across the xref, - transcript and external_db tables, using the display_xref_id column. - Returntype : string + the database identifier of the transcript for which the name + of external db from which its external name is derived. + Example : $external_dbname = $transcript_adaptor->get_display_xref_id(42); + Description: Retrieves the display_xref_id for a transcript. + Returntype : int Exceptions : thrown if $dbId arg is not defined Caller : general =cut -sub get_external_dbname { - my ($self, $dbID) = @_; +sub get_display_xref { + my ($self, $transcript) = @_; - if( !defined $dbID ) { - $self->throw("Must call with a dbID"); + if( !defined $transcript ) { + $self->throw("Must call with a Transcript object"); } - my $sth = $self->prepare("SELECT e.db_name + my $sth = $self->prepare("SELECT e.db_name, + x.display_label, + x.xref_id FROM transcript t, xref x, external_db e @@ -443,49 +421,23 @@ sub get_external_dbname { AND t.display_xref_id = x.xref_id AND x.external_db_id = e.external_db_id "); - $sth->execute($dbID); - - my ($db_name) = $sth->fetchrow_array(); - if( !defined $db_name ) { - return undef; - } - - return $db_name; -} - - -=head2 get_display_xref_id - - Arg [1] : int $dbID - the database identifier of the transcript for which the name - of external db from which its external name is derived. - Example : $external_dbname = $transcript_adaptor->get_display_xref_id(42); - Description: Retrieves the display_xref_id for a transcript. - Returntype : int - Exceptions : thrown if $dbId arg is not defined - Caller : general - -=cut - -sub get_display_xref_id { - my ($self, $dbID) = @_; - - if( !defined $dbID ) { - $self->throw("Must call with a dbID"); - } + $sth->execute( $transcript->dbID ); - my $sth = $self->prepare("SELECT display_xref_id - FROM transcript - WHERE transcript_id = ? - "); - $sth->execute($dbID); - my ($xref_id) = $sth->fetchrow_array(); + my ($db_name, $display_label, $xref_id ) = $sth->fetchrow_array(); if( !defined $xref_id ) { return undef; } - return $xref_id; + my $db_entry = Bio::EnsEMBL::DBEntry->new + ( + -dbid => $xref_id, + -adaptor => $self->db->get_DBEntryAdaptor(), + -dbname => $db_name, + -display_id => $display_label + ); + + return $db_entry; } @@ -512,33 +464,24 @@ sub update { $self->throw("Must update a transcript object, not a $transcript"); } - my $sth = $self->prepare("SELECT exon_count, display_xref_id - FROM transcript - WHERE transcript_id = ? - "); - - $sth->execute($transcript->dbID); - - if ( my ($exons, $dxref_id) = $sth->fetchrow_array() ){ - - if ( $exons != scalar(@{$transcript->get_all_Exons})) { - $self->warn("Trying to update the number of exons on a stored transcript. Not yet implemented."); - } - - if ( $dxref_id != $transcript->display_xref ) { + my $sth = $self->prepare("UPDATE transcript + SET display_xref_id = ?, + exon_count = ? + WHERE transcript_id = ? + "); - $sth = $self->prepare("UPDATE transcript - SET display_xref_id = ? - WHERE transcript_id = ? - "); + my $display_xref = $transcript->display_xref(); + my $display_xref_id; - $sth->execute($transcript->display_xref, $transcript->dbID); - } - } - else { - $self->warn("Trying to update a transcript that is not in the database. Try store\'ing first."); + if( defined $display_xref && $display_xref->dbID() ) { + $display_xref_id = $display_xref->dbID(); + } else { + $display_xref_id = 0; } -} + + my $exon_count = scalar( @{$transcript->get_all_Exons()} ); + $sth->execute( $display_xref_id, $exon_count, $transcript->dbID() ); + } diff --git a/modules/Bio/EnsEMBL/Gene.pm b/modules/Bio/EnsEMBL/Gene.pm index b24b00be9b1ddae209d9453639775ff76c93c017..a4501e917ea70282ae1611d8bf400b6783fd6672 100755 --- a/modules/Bio/EnsEMBL/Gene.pm +++ b/modules/Bio/EnsEMBL/Gene.pm @@ -391,16 +391,20 @@ sub external_name { my ($self, $ext_name) = @_; if(defined $ext_name) { - $self->{'_ext_name'} = $ext_name; + return ( $self->{'_ext_name'} = $ext_name ); } if( exists $self->{'_ext_name'} ) { return $self->{'_ext_name'}; } - $self->{'_ext_name'} = $self->adaptor->get_external_name($self->dbID); - return $self->{'_ext_name'}; + my $display_xref = $self->display_xref(); + if( defined $display_xref ) { + return $display_xref->display_id() + } else { + return undef; + } } @@ -417,19 +421,23 @@ sub external_name { =cut sub external_db { - my ($self, $ext_dbname) = @_; + my ( $self, $ext_dbname ) = @_; if(defined $ext_dbname) { - $self->{'_ext_dbname'} = $ext_dbname; + return ( $self->{'_ext_dbname'} = $ext_dbname ); } if( exists $self->{'_ext_dbname'} ) { return $self->{'_ext_dbname'}; } - $self->{'_ext_dbname'} = $self->adaptor->get_external_dbname($self->dbID); - return $self->{'_ext_dbname'}; + my $display_xref = $self->display_xref(); + if( defined $display_xref ) { + return $display_xref->dbname() + } else { + return undef; + } } @@ -925,20 +933,16 @@ sub species { =cut -sub display_xref{ - - my ($self,$value) = @_; - - if( defined $value ) { - $self->{'display_xref'} = $value; - return; - } +sub display_xref { - if( exists $self->{'display_xref'} ) { + my $self = shift; + if( @_ ) { + $self->{'display_xref'} = shift; + } elsif( exists $self->{'display_xref'} ) { return $self->{'display_xref'}; - } - - $self->{'display_xref'} = $self->adaptor->get_display_xref_id($self->dbID); + } elsif ( defined $self->adaptor() ) { + $self->{'display_xref'} = $self->adaptor->get_display_xref( $self ); + } return $self->{'display_xref'}; } diff --git a/modules/Bio/EnsEMBL/Transcript.pm b/modules/Bio/EnsEMBL/Transcript.pm index 67addd815a314b7cacd9b4a5bf05999374d7db19..7c37d21bc22a02eb17399dfd3d5381d24e8403aa 100755 --- a/modules/Bio/EnsEMBL/Transcript.pm +++ b/modules/Bio/EnsEMBL/Transcript.pm @@ -163,19 +163,23 @@ sub dbID { =cut sub external_db { - my ($self, $ext_dbname) = @_; + my ( $self, $ext_dbname ) = @_; if(defined $ext_dbname) { - $self->{'_ext_dbname'} = $ext_dbname; + return ( $self->{'_ext_dbname'} = $ext_dbname ); } if( exists $self->{'_ext_dbname'} ) { return $self->{'_ext_dbname'}; } - $self->{'_ext_dbname'} = $self->adaptor->get_external_dbname($self->dbID); - return $self->{'_ext_dbname'}; + my $display_xref = $self->display_xref(); + if( defined $display_xref ) { + return $display_xref->dbname() + } else { + return undef; + } } @@ -190,20 +194,25 @@ sub external_db { =cut + sub external_name { my ($self, $ext_name) = @_; if(defined $ext_name) { - $self->{'_ext_name'} = $ext_name; + return ( $self->{'_ext_name'} = $ext_name ); } if( exists $self->{'_ext_name'} ) { return $self->{'_ext_name'}; } - $self->{'_ext_name'} = $self->adaptor->get_external_name($self->dbID); - return $self->{'_ext_name'}; + my $display_xref = $self->display_xref(); + if( defined $display_xref ) { + return $display_xref->display_id() + } else { + return undef; + } } @@ -250,26 +259,23 @@ sub adaptor { =cut -sub display_xref{ - - my ($self,$value) = @_; - - if( defined $value ) { - $self->{'display_xref'} = $value; - return; - } +sub display_xref { - if( exists $self->{'display_xref'} ) { + my $self = shift; + if( @_ ) { + $self->{'display_xref'} = shift; + } elsif( exists $self->{'display_xref'} ) { return $self->{'display_xref'}; - } - - $self->{'display_xref'} = $self->adaptor->get_display_xref_id($self->dbID); + } elsif ( defined $self->adaptor() ) { + $self->{'display_xref'} = $self->adaptor->get_display_xref( $self ); + } return $self->{'display_xref'}; } + =head2 _translation_id Title : _translation_id diff --git a/modules/t/gene.t b/modules/t/gene.t index 1f6503e2b263c0900fa08af0dc2a324c2a09acf1..5bd25a826972b8e6c8335fe3a20083373d3a192c 100644 --- a/modules/t/gene.t +++ b/modules/t/gene.t @@ -62,7 +62,7 @@ debug( "Gene external dbname: " . $gene->external_db ); ok( $gene->external_db eq "SPTREMBL"); debug( "Gene display xref id: " . $gene->display_xref ); -ok( $gene->display_xref == 128324); +ok( $gene->display_xref->dbID() == 128324); # test the getters and setters @@ -385,14 +385,16 @@ $gene = $ga->fetch_by_stable_id( "ENSG00000171456" ); $ga->update($gene); my $newgene = $ga->fetch_by_stable_id( "ENSG00000171456" ); -ok ( $newgene->display_xref == 128324 ); +ok ( $newgene->display_xref->dbID() == 128324 ); ok ( $newgene->type eq 'ensembl' ); # now change the original gene and update it -$gene->display_xref(42); +my $dbEntryAdaptor= $db->get_DBEntryAdaptor(); + +$gene->display_xref( $dbEntryAdaptor->fetch_by_dbID( 614 )); $gene->type('dummy'); $ga->update($gene); $newgene = $ga->fetch_by_stable_id( "ENSG00000171456" ); -ok ( $newgene->display_xref == 42 ); +ok ( $newgene->display_xref->dbID() == 614 ); ok ( $newgene->type eq 'dummy' ); diff --git a/modules/t/transcript.t b/modules/t/transcript.t index abcd1c5c537a10a7b51f30f240469351e90a9c4e..e4debf3f43ad65f060fb04e62a6f77ec0da66967 100644 --- a/modules/t/transcript.t +++ b/modules/t/transcript.t @@ -80,8 +80,8 @@ ok ( $tr->external_name eq "MAPRE1"); debug ( "External transcript dbname: " . $tr->external_db ); ok ( $tr->external_db eq 'HUGO' ); -debug ( "Display_xref_id: " . $tr->display_xref ); -ok ( $tr->display_xref == 97759 ); +debug ( "Display_xref_id: " . $tr->display_xref->dbID() ); +ok ( $tr->display_xref->dbID() == 97759 ); ok( test_getter_setter( $tr, "display_xref", 42 )); ok( test_getter_setter( $tr, "dbID", 100000 )); @@ -175,13 +175,14 @@ $tr = $ta->fetch_by_stable_id( "ENST00000217347" ); $ta->update($tr); my $up_tr = $ta->fetch_by_stable_id( "ENST00000217347" ); -ok ( $up_tr->display_xref == 97759 ); +ok ( $up_tr->display_xref->dbID() == 97759 ); +my $dbentryAdaptor = $db->get_DBEntryAdaptor(); -$tr->display_xref(42); +$tr->display_xref($dbentryAdaptor->fetch_by_dbID( 614 )); $ta->update($tr); $up_tr = $ta->fetch_by_stable_id( "ENST00000217347" ); -ok ( $up_tr->display_xref == 42 ); +ok ( $up_tr->display_xref->dbID() == 614 );