| 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::TranslationAdaptor - Provides a means to fetch and store | ||
| 34 | Translation objects from a database. | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | This adaptor provides a means to retrieve and store | ||
| 39 | Bio::EnsEMBL::Translation objects from/in a database. | ||
| 40 | |||
| 41 | Translation objects only truly make sense in the context of their | ||
| 42 | transcripts so the recommended means to retrieve Translations is | ||
| 43 | by retrieving the Transcript object first, and then fetching the | ||
| 44 | Translation. | ||
| 45 | |||
| 46 | =head1 SYNOPSIS | ||
| 47 | |||
| 48 | use Bio::EnsEMBL::Registry; | ||
| 49 | |||
| 50 | Bio::EnsEMBL::Registry->load_registry_from_db( | ||
| 51 | -host => 'ensembldb.ensembl.org', | ||
| 52 | -user => 'anonymous' | ||
| 53 | ); | ||
| 54 | |||
| 55 | $transcript_adaptor = | ||
| 56 | Bio::EnsEMBL::Registry->get_adaptor( "human", "core", | ||
| 57 | "transcript" ); | ||
| 58 | |||
| 59 | $translation_adaptor = | ||
| 60 | Bio::EnsEMBL::Registry->get_adaptor( "human", "core", | ||
| 61 | "translation" ); | ||
| 62 | |||
| 63 | my $transcript = $transcript_adaptor->fetch_by_dbID(131243); | ||
| 64 | my $translation = | ||
| 65 | $translation_adaptor->fetch_by_Transcript($transcript); | ||
| 66 | |||
| 67 | print("Translation Start Site: " | ||
| 68 | . $translation->start_Exon()->stable_id() . " " | ||
| 69 | . $translation->start() | ||
| 70 | . "\n" ); | ||
| 71 | print("Translation Stop: " | ||
| 72 | . $translation->end_Exon()->stable_id() . " " | ||
| 73 | . $translation->end() ); | ||
| 74 | |||
| 75 | =head1 METHODS | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | package Bio::EnsEMBL::DBSQL::TranslationAdaptor; | ||
| 80 | $Bio::EnsEMBL::DBSQL::TranslationAdaptor::VERSION = '114.0.0_50'; # TRIAL | ||
| 81 | $Bio::EnsEMBL::DBSQL::TranslationAdaptor::VERSION = '114.0.050'; | ||
| 82 | use vars qw(@ISA); | ||
| 83 | use strict; | ||
| 84 | |||
| 85 | use Bio::EnsEMBL::DBSQL::BaseAdaptor; | ||
| 86 | use Bio::EnsEMBL::Translation; | ||
| 87 | use Bio::EnsEMBL::Utils::Exception qw( throw warning ); | ||
| 88 | use Bio::EnsEMBL::Utils::Scalar qw( assert_ref ); | ||
| 89 | |||
| 90 | |||
| 91 | @ISA = qw( Bio::EnsEMBL::DBSQL::BaseAdaptor ); | ||
| 92 | |||
| 93 | =head2 fetch_all_alternative_by_Transcript | ||
| 94 | |||
| 95 | Arg [1] : Bio::EnsEMBL::Transcript $transcript | ||
| 96 | Example : | ||
| 97 | |||
| 98 | @tl = @{ | ||
| 99 | $translation_adaptor->fetch_all_alternative_by_Transcript( | ||
| 100 | $transcript) | ||
| 101 | }; | ||
| 102 | |||
| 103 | Description: Retrieves all alternative translations associated with a | ||
| 104 | particular transcript. If no alternative translation is | ||
| 105 | found, a reference to an empty list is returned. | ||
| 106 | |||
| 107 | Returntype : listref of Bio::EnsEMBL::Translation | ||
| 108 | Exceptions : throw on incorrect argument | ||
| 109 | Caller : Transcript | ||
| 110 | Status : Stable | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub fetch_all_alternative_by_Transcript { | ||
| 115 | my ( $self, $transcript ) = @_; | ||
| 116 | |||
| 117 | assert_ref($transcript, 'Bio::EnsEMBL::Transcript'); | ||
| 118 | |||
| 119 | my $tl_created_date = | ||
| 120 | $self->db()->dbc()->from_date_to_seconds('tl.created_date'); | ||
| 121 | my $tl_modified_date = | ||
| 122 | $self->db()->dbc()->from_date_to_seconds('tl.modified_date'); | ||
| 123 | |||
| 124 | my $sql = | ||
| 125 | sprintf( "SELECT tl.translation_id, tl.start_exon_id, " | ||
| 126 | . "tl.end_exon_id, tl.seq_start, tl.seq_end, " | ||
| 127 | . "tl.stable_id, tl.version, %s, %s " | ||
| 128 | . "FROM translation tl " | ||
| 129 | . "JOIN transcript t " | ||
| 130 | . "ON (t.transcript_id = tl.transcript_id) " | ||
| 131 | . "WHERE tl.transcript_id = ? " | ||
| 132 | . "AND tl.translation_id != t.canonical_translation_id", | ||
| 133 | $tl_created_date, $tl_modified_date ); | ||
| 134 | |||
| 135 | my $transcript_id = $transcript->dbID(); | ||
| 136 | my $sth = $self->prepare($sql); | ||
| 137 | $sth->bind_param( 1, $transcript_id, SQL_INTEGER ); | ||
| 138 | |||
| 139 | $sth->execute(); | ||
| 140 | |||
| 141 | my ( | ||
| 142 | $translation_id, $start_exon_id, $end_exon_id, | ||
| 143 | $seq_start, $seq_end, $stable_id, | ||
| 144 | $version, $created_date, $modified_date | ||
| 145 | ); | ||
| 146 | |||
| 147 | $sth->bind_columns( | ||
| 148 | \( | ||
| 149 | $translation_id, $start_exon_id, $end_exon_id, | ||
| 150 | $seq_start, $seq_end, $stable_id, | ||
| 151 | $version, $created_date, $modified_date | ||
| 152 | ) ); | ||
| 153 | |||
| 154 | # Get all alternative translations. | ||
| 155 | my $translations = []; | ||
| 156 | while ( $sth->fetch() ) { | ||
| 157 | if ( !defined($translation_id) ) { next } | ||
| 158 | |||
| 159 | my ( $start_exon, $end_exon ); | ||
| 160 | |||
| 161 | # this will load all the exons whenever we load the translation | ||
| 162 | # but I guess thats ok .... | ||
| 163 | |||
| 164 | foreach my $exon ( @{ $transcript->get_all_Exons() } ) { | ||
| 165 | if ( $exon->dbID() == $start_exon_id ) { $start_exon = $exon } | ||
| 166 | if ( $exon->dbID() == $end_exon_id ) { $end_exon = $exon } | ||
| 167 | } | ||
| 168 | |||
| 169 | if ( !( defined($start_exon) && defined($end_exon) ) ) { | ||
| 170 | throw( | ||
| 171 | sprintf( | ||
| 172 | "Could not find start or end exon in transcript_id=%d\n", | ||
| 173 | $transcript->dbID() ) ); | ||
| 174 | } | ||
| 175 | |||
| 176 | my $translation = | ||
| 177 | Bio::EnsEMBL::Translation->new_fast( { | ||
| 178 | 'dbID' => $translation_id, | ||
| 179 | 'adaptor' => $self, | ||
| 180 | 'start' => $seq_start, | ||
| 181 | 'end' => $seq_end, | ||
| 182 | 'start_exon' => $start_exon, | ||
| 183 | 'end_exon' => $end_exon, | ||
| 184 | 'stable_id' => $stable_id, | ||
| 185 | 'version' => $version, | ||
| 186 | 'created_date' => $created_date || undef, | ||
| 187 | 'modified_date' => $modified_date || undef, | ||
| 188 | } ); | ||
| 189 | |||
| 190 | $translation->transcript($transcript); | ||
| 191 | |||
| 192 | push( @{$translations}, $translation ); | ||
| 193 | |||
| 194 | } ## end while ( $sth->fetch() ) | ||
| 195 | |||
| 196 | return $translations; | ||
| 197 | } ## end sub fetch_all_by_Transcript | ||
| 198 | |||
| 199 | =head2 fetch_by_Transcript | ||
| 200 | |||
| 201 | Arg [1] : Bio::EnsEMBL::Transcript $transcript | ||
| 202 | Example : $tl = $translation_adaptor->fetch_by_Transcript($transcript); | ||
| 203 | Description: Retrieves a Translation via its associated transcript. | ||
| 204 | If the Translation is not found, undef is returned. | ||
| 205 | Returntype : Bio::EnsEMBL::Translation | ||
| 206 | Exceptions : throw on incorrect argument | ||
| 207 | Caller : Transcript | ||
| 208 | Status : Stable | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | sub fetch_by_Transcript { | ||
| 213 | my ( $self, $transcript ) = @_; | ||
| 214 | |||
| 215 | assert_ref( $transcript, 'Bio::EnsEMBL::Transcript' ); | ||
| 216 | |||
| 217 | my $tl_created_date = | ||
| 218 | $self->db()->dbc()->from_date_to_seconds('tl.created_date'); | ||
| 219 | my $tl_modified_date = | ||
| 220 | $self->db()->dbc()->from_date_to_seconds('tl.modified_date'); | ||
| 221 | |||
| 222 | my $sql = | ||
| 223 | sprintf( "SELECT tl.translation_id, tl.start_exon_id, " | ||
| 224 | . "tl.end_exon_id, tl.seq_start, tl.seq_end, " | ||
| 225 | . "tl.stable_id, tl.version, %s, %s " | ||
| 226 | . "FROM translation tl " | ||
| 227 | . "JOIN transcript tr " | ||
| 228 | . "ON (tl.translation_id = tr.canonical_translation_id) " | ||
| 229 | . "WHERE tr.transcript_id = ?", | ||
| 230 | $tl_created_date, $tl_modified_date ); | ||
| 231 | |||
| 232 | my $transcript_id = $transcript->dbID(); | ||
| 233 | my $sth = $self->prepare($sql); | ||
| 234 | $sth->bind_param( 1, $transcript_id, SQL_INTEGER ); | ||
| 235 | |||
| 236 | $sth->execute(); | ||
| 237 | |||
| 238 | my ( | ||
| 239 | $translation_id, $start_exon_id, $end_exon_id, | ||
| 240 | $seq_start, $seq_end, $stable_id, | ||
| 241 | $version, $created_date, $modified_date | ||
| 242 | ) = $sth->fetchrow_array(); | ||
| 243 | $sth->finish(); | ||
| 244 | |||
| 245 | if ( !defined($translation_id) ) { return undef } | ||
| 246 | |||
| 247 | my ( $start_exon, $end_exon ); | ||
| 248 | |||
| 249 | # this will load all the exons whenever we load the translation | ||
| 250 | # but I guess thats ok .... | ||
| 251 | |||
| 252 | foreach my $exon ( @{ $transcript->get_all_Exons() } ) { | ||
| 253 | if ( $exon->dbID() == $start_exon_id ) { $start_exon = $exon } | ||
| 254 | if ( $exon->dbID() == $end_exon_id ) { $end_exon = $exon } | ||
| 255 | } | ||
| 256 | |||
| 257 | if ( !( defined($start_exon) && defined($end_exon) ) ) { | ||
| 258 | throw( | ||
| 259 | sprintf( "Could not find start or end exon in transcript_id=%d\n", | ||
| 260 | $transcript->dbID() ) ); | ||
| 261 | } | ||
| 262 | |||
| 263 | my $translation = | ||
| 264 | Bio::EnsEMBL::Translation->new_fast( { | ||
| 265 | 'dbID' => $translation_id, | ||
| 266 | 'adaptor' => $self, | ||
| 267 | 'start' => $seq_start, | ||
| 268 | 'end' => $seq_end, | ||
| 269 | 'start_exon' => $start_exon, | ||
| 270 | 'end_exon' => $end_exon, | ||
| 271 | 'stable_id' => $stable_id, | ||
| 272 | 'version' => $version, | ||
| 273 | 'created_date' => $created_date || undef, | ||
| 274 | 'modified_date' => $modified_date || undef, | ||
| 275 | } ); | ||
| 276 | |||
| 277 | $translation->transcript($transcript); | ||
| 278 | |||
| 279 | return $translation; | ||
| 280 | } ## end sub fetch_by_Transcript | ||
| 281 | |||
| 282 | |||
| 283 | |||
| 284 | =head2 fetch_all_by_external_name | ||
| 285 | |||
| 286 | Arg [1] : string $external_name | ||
| 287 | The external identifier for the translation(s) to be | ||
| 288 | obtained. | ||
| 289 | Arg [2] : (optional) string $external_db_name | ||
| 290 | The name of the external database from which the | ||
| 291 | identifier originates. | ||
| 292 | Arg [3] : Boolean override. Force SQL regex matching for users | ||
| 293 | who really do want to find all 'NM%' | ||
| 294 | Example : my @translations = | ||
| 295 | @{ $trl_adaptor->fetch_all_by_external_name('BRCA2') }; | ||
| 296 | my @many_translations = | ||
| 297 | @{ $trl_adaptor->fetch_all_by_external_name('BRCA%') }; | ||
| 298 | Description: Retrieves a list of translations fetched via an | ||
| 299 | external identifier. Note that this may not be a | ||
| 300 | particularly useful method, because translations | ||
| 301 | do not make much sense out of the context of | ||
| 302 | their transcript. It may be better to use the | ||
| 303 | TranscriptAdaptor::fetch_all_by_external_name instead. | ||
| 304 | SQL wildcards % and _ are supported in the $external_name | ||
| 305 | but their use is somewhat restricted for performance reasons. | ||
| 306 | Users that really do want % and _ in the first three characters | ||
| 307 | should use argument 3 to prevent optimisations | ||
| 308 | Returntype : reference to a list of Translations | ||
| 309 | Exceptions : none | ||
| 310 | Caller : general | ||
| 311 | Status : Medium Risk | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | sub fetch_all_by_external_name { | ||
| 316 | my ( $self, $external_name, $external_db_name, $override ) = @_; | ||
| 317 | |||
| 318 | my $entry_adaptor = $self->db->get_DBEntryAdaptor(); | ||
| 319 | |||
| 320 | my @ids = $entry_adaptor->list_translation_ids_by_extids( | ||
| 321 | $external_name, $external_db_name, $override ); | ||
| 322 | |||
| 323 | my $transcript_adaptor = $self->db()->get_TranscriptAdaptor(); | ||
| 324 | |||
| 325 | my @reference; | ||
| 326 | my @non_reference; | ||
| 327 | foreach my $id (@ids) { | ||
| 328 | my $transcript = $transcript_adaptor->fetch_by_translation_id($id); | ||
| 329 | |||
| 330 | if ( defined($transcript) ) { | ||
| 331 | my $translation = $self->fetch_by_Transcript($transcript); | ||
| 332 | if($transcript->slice()->is_reference()) { | ||
| 333 | push(@reference, $translation); | ||
| 334 | } | ||
| 335 | else { | ||
| 336 | push(@non_reference, $translation); | ||
| 337 | } | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | return [@reference, @non_reference]; | ||
| 342 | } | ||
| 343 | |||
| 344 | =head2 fetch_all_by_GOTerm | ||
| 345 | |||
| 346 | Arg [1] : Bio::EnsEMBL::OntologyTerm | ||
| 347 | The GO term for which translations should be fetched. | ||
| 348 | |||
| 349 | Example: @translations = @{ | ||
| 350 | $translation_adaptor->fetch_all_by_GOTerm( | ||
| 351 | $go_adaptor->fetch_by_accession('GO:0030326') ) }; | ||
| 352 | |||
| 353 | Description : Retrieves a list of translations that are | ||
| 354 | associated with the given GO term, or with any of | ||
| 355 | its descendent GO terms. | ||
| 356 | |||
| 357 | Return type : listref of Bio::EnsEMBL::Translation | ||
| 358 | Exceptions : Throws of argument is not a GO term | ||
| 359 | Caller : general | ||
| 360 | Status : Stable | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | sub fetch_all_by_GOTerm { | ||
| 365 | my ( $self, $term ) = @_; | ||
| 366 | |||
| 367 | assert_ref( $term, 'Bio::EnsEMBL::OntologyTerm' ); | ||
| 368 | if ( $term->ontology() ne 'GO' ) { | ||
| 369 | throw('Argument is not a GO term'); | ||
| 370 | } | ||
| 371 | |||
| 372 | my $entryAdaptor = $self->db->get_DBEntryAdaptor(); | ||
| 373 | |||
| 374 | my %unique_dbIDs; | ||
| 375 | foreach my $accession ( map { $_->accession() } | ||
| 376 | ( $term, @{ $term->descendants() } ) ) | ||
| 377 | { | ||
| 378 | my @ids = | ||
| 379 | $entryAdaptor->list_translation_ids_by_extids( $accession, 'GO' ); | ||
| 380 | foreach my $dbID (@ids) { $unique_dbIDs{$dbID} = 1 } | ||
| 381 | } | ||
| 382 | |||
| 383 | my @result; | ||
| 384 | if ( scalar( keys(%unique_dbIDs) ) > 0 ) { | ||
| 385 | my $transcript_adaptor = $self->db()->get_TranscriptAdaptor(); | ||
| 386 | |||
| 387 | foreach my $dbID ( sort { $a <=> $b } keys(%unique_dbIDs) ) { | ||
| 388 | my $transcript = | ||
| 389 | $transcript_adaptor->fetch_by_translation_id($dbID); | ||
| 390 | if ( defined($transcript) ) { | ||
| 391 | push( @result, $self->fetch_by_Transcript($transcript) ); | ||
| 392 | } | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | return \@result; | ||
| 397 | } ## end sub fetch_all_by_GOTerm | ||
| 398 | |||
| 399 | =head2 fetch_all_by_GOTerm_accession | ||
| 400 | |||
| 401 | Arg [1] : String | ||
| 402 | The GO term accession for which genes should be | ||
| 403 | fetched. | ||
| 404 | |||
| 405 | Example : | ||
| 406 | |||
| 407 | @genes = | ||
| 408 | @{ $gene_adaptor->fetch_all_by_GOTerm_accession('GO:0030326') }; | ||
| 409 | |||
| 410 | Description : Retrieves a list of genes that are associated with | ||
| 411 | the given GO term, or with any of its descendent | ||
| 412 | GO terms. The genes returned are in their native | ||
| 413 | coordinate system, i.e. in the coordinate system | ||
| 414 | in which they are stored in the database. If | ||
| 415 | another coordinate system is required then the | ||
| 416 | Gene::transfer or Gene::transform method can be | ||
| 417 | used. | ||
| 418 | |||
| 419 | Return type : listref of Bio::EnsEMBL::Gene | ||
| 420 | Exceptions : Throws of argument is not a GO term accession | ||
| 421 | Caller : general | ||
| 422 | Status : Stable | ||
| 423 | |||
| 424 | =cut | ||
| 425 | |||
| 426 | sub fetch_all_by_GOTerm_accession { | ||
| 427 | my ( $self, $accession ) = @_; | ||
| 428 | |||
| 429 | if ( $accession !~ /^GO:/ ) { | ||
| 430 | throw('Argument is not a GO term accession'); | ||
| 431 | } | ||
| 432 | |||
| 433 | my $goAdaptor = | ||
| 434 | Bio::EnsEMBL::Registry->get_adaptor( 'Multi', 'Ontology', | ||
| 435 | 'OntologyTerm' ); | ||
| 436 | |||
| 437 | my $term = $goAdaptor->fetch_by_accession($accession); | ||
| 438 | |||
| 439 | return $self->fetch_all_by_GOTerm($term); | ||
| 440 | } | ||
| 441 | |||
| 442 | =head2 store | ||
| 443 | |||
| 444 | Arg [1] : Bio::EnsEMBL::Translation $translation | ||
| 445 | The translation object to be stored in the database | ||
| 446 | Example : $transl_id = $translation_adaptor->store($translation); | ||
| 447 | Description: Stores a translation object in the database | ||
| 448 | Returntype : int - the new dbID of the stored translation | ||
| 449 | Exceptions : thrown if the dbID of the start_Exon or end_Exon is not | ||
| 450 | defined. | ||
| 451 | thrown if only partial stable id information is present (e.g. | ||
| 452 | identifier but not version number) | ||
| 453 | Caller : Transcript::store | ||
| 454 | Status : Stable | ||
| 455 | |||
| 456 | =cut | ||
| 457 | |||
| 458 | sub store { | ||
| 459 | my ( $self, $translation, $transcript_id ) = @_; | ||
| 460 | |||
| 461 | my $start_exon = $translation->start_Exon(); | ||
| 462 | my $end_exon = $translation->end_Exon(); | ||
| 463 | |||
| 464 | if(!$start_exon) { | ||
| 465 | throw("Translation must define a start_Exon to be stored."); | ||
| 466 | } | ||
| 467 | |||
| 468 | if(!$end_exon) { | ||
| 469 | throw("Translation must define an end_Exon to be stored."); | ||
| 470 | } | ||
| 471 | |||
| 472 | if(!$start_exon->dbID) { | ||
| 473 | throw("start_Exon must have a dbID for Translation to be stored."); | ||
| 474 | } | ||
| 475 | |||
| 476 | if(!$end_exon->dbID) { | ||
| 477 | throw("end_Exon must have a dbID for Translation to be stored."); | ||
| 478 | } | ||
| 479 | |||
| 480 | my @columns = qw( | ||
| 481 | seq_start | ||
| 482 | start_exon_id | ||
| 483 | seq_end | ||
| 484 | end_exon_id | ||
| 485 | transcript_id | ||
| 486 | ); | ||
| 487 | |||
| 488 | my @canned_columns; | ||
| 489 | my @canned_values; | ||
| 490 | |||
| 491 | if (defined($translation->stable_id)) { | ||
| 492 | push @columns, 'stable_id', 'version'; | ||
| 493 | |||
| 494 | my $created = $self->db->dbc->from_seconds_to_date($translation->created_date()); | ||
| 495 | my $modified = $self->db->dbc->from_seconds_to_date($translation->modified_date()); | ||
| 496 | |||
| 497 | if ($created) { | ||
| 498 | push @canned_columns, 'created_date'; | ||
| 499 | push @canned_values, $created; | ||
| 500 | } | ||
| 501 | if ($modified) { | ||
| 502 | push @canned_columns, 'modified_date'; | ||
| 503 | push @canned_values, $modified; | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | my $columns = join(', ', @columns, @canned_columns); | ||
| 508 | my $values = join(', ', ('?') x @columns, @canned_values); | ||
| 509 | my $store_translation_sql = qq( | ||
| 510 | INSERT INTO translation ( $columns ) VALUES ( $values ) | ||
| 511 | ); | ||
| 512 | |||
| 513 | my $sth = $self->prepare($store_translation_sql); | ||
| 514 | $sth->bind_param(1,$translation->start,SQL_INTEGER); | ||
| 515 | $sth->bind_param(2,$translation->start_Exon->dbID,SQL_INTEGER); | ||
| 516 | $sth->bind_param(3,$translation->end,SQL_INTEGER); | ||
| 517 | $sth->bind_param(4,$translation->end_Exon->dbID,SQL_INTEGER); | ||
| 518 | $sth->bind_param(5,$transcript_id,SQL_INTEGER); | ||
| 519 | |||
| 520 | |||
| 521 | if (defined($translation->stable_id)) { | ||
| 522 | |||
| 523 | $sth->bind_param(6, $translation->stable_id,SQL_VARCHAR); | ||
| 524 | $sth->bind_param(7, $translation->version,SQL_VARCHAR); | ||
| 525 | } | ||
| 526 | |||
| 527 | $sth->execute(); | ||
| 528 | |||
| 529 | my $transl_dbID = $self->last_insert_id('translation_id', undef, 'translation'); | ||
| 530 | |||
| 531 | # | ||
| 532 | # store object xref mappings to translations | ||
| 533 | # | ||
| 534 | |||
| 535 | my $dbEntryAdaptor = $self->db()->get_DBEntryAdaptor(); | ||
| 536 | # store each of the xrefs for this translation | ||
| 537 | foreach my $dbl ( @{$translation->get_all_DBEntries} ) { | ||
| 538 | $dbEntryAdaptor->store( $dbl, $transl_dbID, "Translation", 1 ); | ||
| 539 | } | ||
| 540 | |||
| 541 | #storing the protein features associated with the translation | ||
| 542 | my $pfadaptor = $self->db->get_ProteinFeatureAdaptor(); | ||
| 543 | foreach my $pf(@{$translation->get_all_ProteinFeatures}){ | ||
| 544 | $pfadaptor->store($pf, $transl_dbID); | ||
| 545 | } | ||
| 546 | |||
| 547 | $translation->get_all_Attributes(); | ||
| 548 | |||
| 549 | # store any translation attributes that are defined | ||
| 550 | my $attr_adaptor = $self->db->get_AttributeAdaptor(); | ||
| 551 | $attr_adaptor->store_on_Translation($transl_dbID, | ||
| 552 | $translation->get_all_Attributes()); | ||
| 553 | |||
| 554 | $translation->dbID($transl_dbID); | ||
| 555 | $translation->adaptor($self); | ||
| 556 | |||
| 557 | return $transl_dbID; | ||
| 558 | } | ||
| 559 | |||
| 560 | |||
| 561 | |||
| 562 | =head2 remove | ||
| 563 | |||
| 564 | Arg [1] : Bio::EnsEMBL::Translation $translation | ||
| 565 | Example : $translation_adaptor->remove($translation); | ||
| 566 | Description: Removes a translation completely from the database, and all | ||
| 567 | associated information including protein features etc. | ||
| 568 | Returntype : none | ||
| 569 | Exceptions : throw on incorrect arguments | ||
| 570 | warning if translation is not in this database | ||
| 571 | Caller : TranscriptAdaptor::remove | ||
| 572 | Status : Stable | ||
| 573 | |||
| 574 | =cut | ||
| 575 | |||
| 576 | sub remove { | ||
| 577 | my $self = shift; | ||
| 578 | my $translation = shift; | ||
| 579 | |||
| 580 | if(!ref($translation) || !$translation->isa('Bio::EnsEMBL::Translation')) { | ||
| 581 | throw("Bio::EnsEMBL::Translation argument expected."); | ||
| 582 | } | ||
| 583 | |||
| 584 | if( !$translation->is_stored($self->db()) ) { | ||
| 585 | warning("Cannot remove translation " . $translation->dbID() . | ||
| 586 | ". Is not stored in this database."); | ||
| 587 | return; | ||
| 588 | } | ||
| 589 | |||
| 590 | # remove athe attributes associated with this translation | ||
| 591 | my $attrib_adp = $self->db->get_AttributeAdaptor; | ||
| 592 | $attrib_adp->remove_from_Translation($translation); | ||
| 593 | |||
| 594 | #remove all transcripts links to translation | ||
| 595 | my $sth = $self->prepare | ||
| 596 | ("UPDATE transcript SET canonical_translation_id = NULL WHERE canonical_translation_id = ?"); | ||
| 597 | $sth->bind_param(1,$translation->dbID,SQL_INTEGER); | ||
| 598 | $sth->execute(); | ||
| 599 | $sth->finish(); | ||
| 600 | |||
| 601 | # remove all xref associations to this translation | ||
| 602 | my $dbe_adaptor = $self->db()->get_DBEntryAdaptor(); | ||
| 603 | foreach my $dbe (@{$translation->get_all_DBEntries()}) { | ||
| 604 | $dbe_adaptor->remove_from_object($dbe, $translation, 'Translation'); | ||
| 605 | } | ||
| 606 | |||
| 607 | # remove all protein_features on this translation | ||
| 608 | $sth = $self->prepare | ||
| 609 | ("DELETE FROM protein_feature WHERE translation_id = ?"); | ||
| 610 | $sth->bind_param(1,$translation->dbID,SQL_INTEGER); | ||
| 611 | $sth->execute(); | ||
| 612 | $sth->finish(); | ||
| 613 | |||
| 614 | # remove the translation itself | ||
| 615 | |||
| 616 | $sth = $self->prepare("DELETE FROM translation WHERE translation_id = ?" ); | ||
| 617 | $sth->bind_param(1,$translation->dbID,SQL_INTEGER); | ||
| 618 | $sth->execute(); | ||
| 619 | $sth->finish(); | ||
| 620 | |||
| 621 | $translation->dbID( undef ); | ||
| 622 | $translation->adaptor(undef); | ||
| 623 | |||
| 624 | return | ||
| 625 | } | ||
| 626 | |||
| 627 | |||
| 628 | =head2 list_dbIDs | ||
| 629 | |||
| 630 | Arg [1] : none | ||
| 631 | Example : @translation_ids = @{$translation_adaptor->list_dbIDs()}; | ||
| 632 | Description: Gets an array of internal ids for all translations in the current db | ||
| 633 | Returntype : list of ints | ||
| 634 | Exceptions : none | ||
| 635 | Caller : ? | ||
| 636 | Status : Stable | ||
| 637 | |||
| 638 | =cut | ||
| 639 | |||
| 640 | sub list_dbIDs { | ||
| 641 | my ($self) = @_; | ||
| 642 | |||
| 643 | return $self->_list_dbIDs("translation"); | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | =head2 list_stable_ids | ||
| 648 | |||
| 649 | Arg [1] : none | ||
| 650 | Example : @transl_stable_ids = @{$transl_adaptor->list_stable_dbIDs()}; | ||
| 651 | Description: Gets an array of stable ids for all translations in the current | ||
| 652 | db | ||
| 653 | Returntype : reference to a list of strings | ||
| 654 | Exceptions : none | ||
| 655 | Caller : general | ||
| 656 | Status : Stable | ||
| 657 | |||
| 658 | =cut | ||
| 659 | |||
| 660 | sub list_stable_ids { | ||
| 661 | my ($self) = @_; | ||
| 662 | |||
| 663 | return $self->_list_dbIDs("translation", "stable_id"); | ||
| 664 | } | ||
| 665 | |||
| 666 | =head2 _list_dbIDs | ||
| 667 | |||
| 668 | Arg[1] : String $table | ||
| 669 | Arg[2] : String $column | ||
| 670 | Example : $transl_adaptor->_list_dbIDs('translation','translation_id'); | ||
| 671 | Description : Local reimplementation to ensure multi-species translations | ||
| 672 | are limited to their species alone | ||
| 673 | Returntype : ArrayRef of specified IDs | ||
| 674 | Caller : Internal | ||
| 675 | Status : Unstable | ||
| 676 | =cut | ||
| 677 | |||
| 678 | sub _list_dbIDs { | ||
| 679 | my ($self, $table, $column) = @_; | ||
| 680 | my $ids; | ||
| 681 | if($self->is_multispecies()) { | ||
| 682 | $column ||= "${table}_id"; | ||
| 683 | my $sql = <<SQL; | ||
| 684 | select `tr`.`${column}` | ||
| 685 | from translation tr | ||
| 686 | join transcript t using (transcript_id) | ||
| 687 | join seq_region sr using (seq_region_id) | ||
| 688 | join coord_system cs using (coord_system_id) | ||
| 689 | where cs.species_id =? | ||
| 690 | SQL | ||
| 691 | return $self->dbc()->sql_helper()->execute_simple(-SQL => $sql, -PARAMS => [$self->species_id()]); | ||
| 692 | } | ||
| 693 | else { | ||
| 694 | $ids = $self->SUPER::_list_dbIDs($table, $column); | ||
| 695 | } | ||
| 696 | return $ids; | ||
| 697 | } | ||
| 698 | |||
| 699 | |||
| 700 | |||
| 701 | =head2 fetch_by_dbID | ||
| 702 | |||
| 703 | Arg [1] : int $dbID | ||
| 704 | The internal identifier of the Translation to obtain | ||
| 705 | Example : $translation = $translation_adaptor->fetch_by_dbID(1234); | ||
| 706 | Description: This fetches a Translation object via its internal id. | ||
| 707 | This is only debatably useful since translations do | ||
| 708 | not make much sense outside of the context of their | ||
| 709 | Transcript. Consider using fetch_by_Transcript instead. | ||
| 710 | Returntype : Bio::EnsEMBL::Translation, or undef if the translation is not | ||
| 711 | found. | ||
| 712 | Exceptions : warning if an additional (old style) Transcript argument is | ||
| 713 | provided | ||
| 714 | Caller : ? | ||
| 715 | Status : Stable | ||
| 716 | |||
| 717 | =cut | ||
| 718 | |||
| 719 | sub fetch_by_dbID { | ||
| 720 | my ( $self, $dbID, $transcript ) = @_; | ||
| 721 | |||
| 722 | if ( !defined($dbID) ) { | ||
| 723 | throw("dbID argument is required"); | ||
| 724 | } | ||
| 725 | |||
| 726 | my $transcript_adaptor = $self->db()->get_TranscriptAdaptor(); | ||
| 727 | $transcript = $transcript_adaptor->fetch_by_translation_id($dbID); | ||
| 728 | |||
| 729 | if ( defined($transcript) ) { | ||
| 730 | my $translation = $self->fetch_by_Transcript($transcript); | ||
| 731 | |||
| 732 | if ( defined($translation) && $translation->dbID()==$dbID ) { | ||
| 733 | return $translation; | ||
| 734 | } | ||
| 735 | |||
| 736 | my @alt_translations = | ||
| 737 | @{ $self->fetch_all_alternative_by_Transcript($transcript) }; | ||
| 738 | |||
| 739 | foreach my $alt_translation (@alt_translations) { | ||
| 740 | if ( $alt_translation->dbID() == $dbID ) { | ||
| 741 | return $alt_translation; | ||
| 742 | } | ||
| 743 | } | ||
| 744 | } | ||
| 745 | |||
| 746 | return undef; | ||
| 747 | } ## end sub fetch_by_dbID | ||
| 748 | |||
| 749 | |||
| 750 | =head2 fetch_by_stable_id | ||
| 751 | |||
| 752 | Arg [1] : string $stable_id | ||
| 753 | The stable identifier of the Translation to obtain | ||
| 754 | Example : $translation = $translation_adaptor->fetch_by_stable_id("ENSP00001"); | ||
| 755 | Description: This fetches a Translation object via its stable id. | ||
| 756 | This is only debatably useful since translations do | ||
| 757 | not make much sense outside of the context of their | ||
| 758 | Transcript. Consider using fetch_by_Transcript instead. | ||
| 759 | Returntype : Bio::EnsEMBL::Translation or undef if the translation is not | ||
| 760 | found. | ||
| 761 | Exceptions : warning if an additional (old style) Transcript argument is | ||
| 762 | provided | ||
| 763 | Caller : ? | ||
| 764 | Status : Stable | ||
| 765 | |||
| 766 | =cut | ||
| 767 | |||
| 768 | sub fetch_by_stable_id { | ||
| 769 | my ($self,$stable_id) = @_; | ||
| 770 | |||
| 771 | if(!$stable_id) { | ||
| 772 | throw("stable id argument is required"); | ||
| 773 | } | ||
| 774 | |||
| 775 | my $transcript_adaptor = $self->db()->get_TranscriptAdaptor(); | ||
| 776 | my $transcript = | ||
| 777 | $transcript_adaptor->fetch_by_translation_stable_id($stable_id); | ||
| 778 | |||
| 779 | return undef if(!$transcript); | ||
| 780 | |||
| 781 | return $self->fetch_by_Transcript($transcript); | ||
| 782 | } | ||
| 783 | |||
| 784 | =head2 fetch_by_stable_id_version | ||
| 785 | |||
| 786 | Arg [1] : String $id | ||
| 787 | The stable ID of the gene to retrieve | ||
| 788 | Arg [2] : Integer $version | ||
| 789 | The version of the stable_id to retrieve | ||
| 790 | Example : $gene = $gene_adaptor->fetch_by_stable_id('ENSG00000148944', 14); | ||
| 791 | Description: Retrieves a gene object from the database via its stable id and version. | ||
| 792 | The gene will be retrieved in its native coordinate system (i.e. | ||
| 793 | in the coordinate system it is stored in the database). It may | ||
| 794 | be converted to a different coordinate system through a call to | ||
| 795 | transform() or transfer(). If the gene or exon is not found | ||
| 796 | undef is returned instead. | ||
| 797 | Returntype : Bio::EnsEMBL::Gene or undef | ||
| 798 | Exceptions : if we cant get the gene in given coord system | ||
| 799 | Caller : general | ||
| 800 | Status : Stable | ||
| 801 | |||
| 802 | =cut | ||
| 803 | |||
| 804 | sub fetch_by_stable_id_version { | ||
| 805 | my ($self,$stable_id, $version) = @_; | ||
| 806 | |||
| 807 | if(!$stable_id) { | ||
| 808 | throw("stable id argument is required"); | ||
| 809 | } | ||
| 810 | |||
| 811 | # Enforce that version be numeric | ||
| 812 | return unless($version =~ /^\d+$/); | ||
| 813 | |||
| 814 | my $transcript_adaptor = $self->db()->get_TranscriptAdaptor(); | ||
| 815 | my $transcript = | ||
| 816 | $transcript_adaptor->fetch_by_translation_stable_id_version($stable_id, $version); | ||
| 817 | |||
| 818 | return if(!$transcript); | ||
| 819 | |||
| 820 | return $self->fetch_by_Transcript($transcript); | ||
| 821 | } | ||
| 822 | |||
| 823 | =head2 fetch_all_by_Transcript_list | ||
| 824 | |||
| 825 | Arg [1] : reference to list of Bio::EnsEMBL::Transcripts $transcripts | ||
| 826 | The list of $transcripts to obtain Translation object for. | ||
| 827 | Example : @translations = @{$tla->fetch_all_by_Transcript_list([$t1,$t2]); | ||
| 828 | Description: Fetches all translations associated with the list of transcripts | ||
| 829 | passed to this method. The passed transcripts will also have | ||
| 830 | their translation set by this method. | ||
| 831 | Returntype : Reference to list of Bio::EnsEMBL::Translations | ||
| 832 | Exceptions : None | ||
| 833 | Caller : general | ||
| 834 | Status : Stable | ||
| 835 | |||
| 836 | =cut | ||
| 837 | |||
| 838 | sub fetch_all_by_Transcript_list { | ||
| 839 | my ($self,$transcripts) = @_; | ||
| 840 | |||
| 841 | if(!defined($transcripts) || ref($transcripts) ne 'ARRAY') { | ||
| 842 | throw("reference to list of Transcripts argument is required"); | ||
| 843 | } | ||
| 844 | |||
| 845 | return [] if(!@$transcripts); | ||
| 846 | |||
| 847 | my %trans_hash = map {$_->dbID() => $_} @$transcripts; | ||
| 848 | my @id_list = keys %trans_hash; | ||
| 849 | |||
| 850 | my @out; | ||
| 851 | |||
| 852 | # mysql is faster and we ensure that we do not exceed the max query size by | ||
| 853 | # splitting large queries into smaller queries of 200 ids | ||
| 854 | my $max_size = 200; | ||
| 855 | |||
| 856 | my ( $transcript_id, $translation_id, $start_exon_id, $end_exon_id, | ||
| 857 | $seq_start, $seq_end, $stable_id, $version, | ||
| 858 | $created_date, $modified_date ); | ||
| 859 | |||
| 860 | my %ex_hash; | ||
| 861 | |||
| 862 | while(@id_list) { | ||
| 863 | my @ids; | ||
| 864 | if(@id_list > $max_size) { | ||
| 865 | @ids = splice(@id_list, 0, $max_size); | ||
| 866 | } else { | ||
| 867 | @ids = splice(@id_list, 0); | ||
| 868 | } | ||
| 869 | |||
| 870 | my $id_str; | ||
| 871 | if(@ids > 1) { | ||
| 872 | $id_str = " IN (" . join(',', @ids). ")"; | ||
| 873 | } else { | ||
| 874 | $id_str = " = " . $ids[0]; | ||
| 875 | } | ||
| 876 | |||
| 877 | my $canonical_lookup = $self->dbc()->sql_helper()->execute_into_hash( | ||
| 878 | -SQL => 'SELECT transcript_id, canonical_translation_id FROM transcript WHERE transcript_id '.$id_str | ||
| 879 | ); | ||
| 880 | |||
| 881 | my $created_date = $self->db->dbc->from_date_to_seconds("tl.created_date"); | ||
| 882 | my $modified_date = $self->db->dbc->from_date_to_seconds("tl.modified_date"); | ||
| 883 | |||
| 884 | my $sth = $self->prepare | ||
| 885 | ("SELECT tl.transcript_id, tl.translation_id, tl.start_exon_id, | ||
| 886 | tl.end_exon_id, tl.seq_start, tl.seq_end, | ||
| 887 | tl.stable_id, tl.version, " . $created_date . "," . | ||
| 888 | $modified_date . | ||
| 889 | " FROM translation tl | ||
| 890 | WHERE tl.transcript_id $id_str"); | ||
| 891 | |||
| 892 | $sth->execute(); | ||
| 893 | |||
| 894 | $sth->bind_columns( \$transcript_id, \$translation_id, \$start_exon_id, \$end_exon_id, | ||
| 895 | \$seq_start, \$seq_end, \$stable_id, \$version, | ||
| 896 | \$created_date, \$modified_date ); | ||
| 897 | |||
| 898 | while($sth->fetch()) { | ||
| 899 | my ($start_exon, $end_exon); | ||
| 900 | |||
| 901 | # this will load all the exons whenever we load the translation | ||
| 902 | # but I guess thats ok .... | ||
| 903 | |||
| 904 | my $tr = $trans_hash{$transcript_id}; | ||
| 905 | |||
| 906 | foreach my $exon (@{$tr->get_all_Exons()}) { | ||
| 907 | if(!$start_exon && $exon->dbID() == $start_exon_id ) { | ||
| 908 | $start_exon = $exon; | ||
| 909 | last if($end_exon); | ||
| 910 | } | ||
| 911 | |||
| 912 | if(!$end_exon && $exon->dbID() == $end_exon_id ) { | ||
| 913 | $end_exon = $exon; | ||
| 914 | last if($start_exon); | ||
| 915 | } | ||
| 916 | } | ||
| 917 | |||
| 918 | unless($start_exon && $end_exon) { | ||
| 919 | throw("Could not find start or end exon in transcript\n"); | ||
| 920 | } | ||
| 921 | |||
| 922 | my $tl = Bio::EnsEMBL::Translation->new | ||
| 923 | (-dbID => $translation_id, | ||
| 924 | -seq_start => $seq_start, | ||
| 925 | -seq_end => $seq_end, | ||
| 926 | -start_exon => $start_exon, | ||
| 927 | -end_exon => $end_exon, | ||
| 928 | -stable_id => $stable_id, | ||
| 929 | -version => $version, | ||
| 930 | -created_date => $created_date || undef, | ||
| 931 | -modified_date => $modified_date || undef); | ||
| 932 | |||
| 933 | # Calling the new method will set $tl->version to '1' if $version is not defined. | ||
| 934 | # But if the version in the database is NULL, $version will be undef; and so we | ||
| 935 | # need to override the default version of '1', and set it back to undef. | ||
| 936 | $tl->{version} = undef unless defined $version; | ||
| 937 | |||
| 938 | $tl->adaptor($self); | ||
| 939 | my $canonical_translation_id = $canonical_lookup->{$transcript_id}; | ||
| 940 | $tr->translation($tl) if $translation_id == $canonical_translation_id; | ||
| 941 | |||
| 942 | push @out, $tl; | ||
| 943 | } | ||
| 944 | } | ||
| 945 | |||
| 946 | return \@out; | ||
| 947 | } | ||
| 948 | |||
| 949 | |||
| 950 | =head2 fetch_all | ||
| 951 | |||
| 952 | Example : $translations = $translation_adaptor->fetch_all(); | ||
| 953 | Description : Retrieves all canonical and alternative translations | ||
| 954 | stored in the database. | ||
| 955 | Returntype : listref of Bio::EnsEMBL::Translation | ||
| 956 | Caller : general | ||
| 957 | Status : At Risk | ||
| 958 | |||
| 959 | =cut | ||
| 960 | |||
| 961 | sub fetch_all { | ||
| 962 | my ($self) = @_; | ||
| 963 | my $transcript_adaptor = $self->db()->get_TranscriptAdaptor(); | ||
| 964 | |||
| 965 | my @translations; | ||
| 966 | foreach my $transcript (@{$transcript_adaptor->fetch_all}) { | ||
| 967 | my $translation = $self->fetch_by_Transcript($transcript); | ||
| 968 | if ($translation) { | ||
| 969 | push @translations, $translation; | ||
| 970 | } | ||
| 971 | foreach my $alt_translation (@{$self->fetch_all_alternative_by_Transcript($transcript)}) { | ||
| 972 | push @translations, $alt_translation; | ||
| 973 | } | ||
| 974 | } | ||
| 975 | return \@translations; | ||
| 976 | } | ||
| 977 | |||
| 978 | |||
| 979 | # _tables | ||
| 980 | # Arg [1] : none | ||
| 981 | # Description: PROTECTED implementation of superclass abstract method. | ||
| 982 | # Returns the names, aliases of the tables to use for queries. | ||
| 983 | # Returntype : list of listrefs of strings | ||
| 984 | # Exceptions : none | ||
| 985 | # Caller : internal | ||
| 986 | # Status : Stable | ||
| 987 | |||
| 988 | sub _tables { | ||
| 989 | return (['translation', 'tl']); | ||
| 990 | } | ||
| 991 | |||
| 992 | 1; |