DBEntryAdaptor.pm 53.6 KB
Newer Older
1
2
=head1 LICENSE

3
  Copyright (c) 1999-2010 The European Bioinformatics Institute and
4
5
6
7
8
9
10
11
12
13
  Genome Research Limited.  All rights reserved.

  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
14
  developers list at <dev@ensembl.org>.
15
16
17
18
19

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=cut
20
21
22

=head1 NAME

23
Bio::EnsEMBL::DBSQL::DBEntryAdaptor -
24
25
26
27
MySQL Database queries to load and store external object references.

=head1 SYNOPSIS

28
29
  $db_entry_adaptor =
    $registry->get_adaptor( 'Human', 'Core', 'DBEntry' );
30

31
  $db_entry = $db_entry_adaptor->fetch_by_dbID($id);
32

33
34
35
36
37
38
  my $gene_adaptor = $registry->get_adaptor( 'Human', 'Core', 'Gene' );

  my $gene = $gene_adaptor->fetch_by_stable_id('ENSG00000101367');

  @db_entries = @{ $db_entry_adaptor->fetch_all_by_Gene($gene) };
  @gene_ids   = $db_entry_adaptor->list_gene_ids_by_extids('BAB15482');
Graham McVicker's avatar
Graham McVicker committed
39

40
41
=head1 METHODS

42
43
44
45
46
=cut

package Bio::EnsEMBL::DBSQL::DBEntryAdaptor;

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
47

48
use Bio::EnsEMBL::DBEntry;
49
use Bio::EnsEMBL::IdentityXref;
50
use Bio::EnsEMBL::OntologyXref;
51

52
use Bio::EnsEMBL::Utils::Exception qw(deprecate throw warning);
53

54
55
56
57
58
use vars qw(@ISA);
use strict;

@ISA = qw( Bio::EnsEMBL::DBSQL::BaseAdaptor );

Graham McVicker's avatar
Graham McVicker committed
59
60
61
62
63
=head2 fetch_by_dbID

  Arg [1]    : int $dbID
               the unique database identifier for the DBEntry to retrieve
  Example    : my $db_entry = $db_entry_adaptor->fetch_by_dbID($dbID);
64
65
  Description: Retrieves a dbEntry from the database via its unique
               identifier.
Graham McVicker's avatar
Graham McVicker committed
66
67
  Returntype : Bio::EnsEMBL::DBEntry
  Exceptions : none
68
  Caller     : general
69
  Status     : Stable
Graham McVicker's avatar
Graham McVicker committed
70
71
72

=cut

73
sub fetch_by_dbID {
74
  my ( $self, $dbID ) = @_;
75

76
  my $sth = $self->prepare(
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    "SELECT  xref.xref_id,
            xref.dbprimary_acc,
            xref.display_label,
            xref.version,
            exDB.dbprimary_acc_linkable,
            exDB.display_label_linkable,
            exDB.priority,
            exDB.db_name,
            exDB.db_display_name,
            exDB.db_release,
            es.synonym,
            xref.info_type,
            xref.info_text,
            exDB.type,
            exDB.secondary_db_name,
92
            exDB.secondary_db_table,
Nathan Johnson's avatar
Nathan Johnson committed
93
            xref.description
94
95
96
97
98
99
100
    FROM    (xref, external_db exDB)
    LEFT JOIN external_synonym es ON
            es.xref_id = xref.xref_id
    WHERE   xref.xref_id = ?
    AND     xref.external_db_id = exDB.external_db_id" );

  $sth->bind_param( 1, $dbID, SQL_INTEGER );
101
  $sth->execute();
102

103
  my $exDB;
104

105
106
107
  my $max_rows = 1000;

  while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
108
      #$description refers to the external_db description, while $desc was referring the xref description
109
110
111
    while ( my $arrayref = shift( @{$rowcache} ) ) {
      my ( $refID,               $dbprimaryId,
           $displayid,           $version,
112
	   $primary_id_linkable,
113
114
115
116
117
           $display_id_linkable, $priority,
           $dbname,              $db_display_name,
           $release,             $synonym,
           $info_type,           $info_text,
           $type,                $secondary_db_name,
118
           $secondary_db_table,  $description
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
      ) = @$arrayref;

      if ( !defined($exDB) ) {
        $exDB =
          Bio::EnsEMBL::DBEntry->new(
                           -adaptor             => $self,
                           -dbID                => $dbID,
                           -primary_id          => $dbprimaryId,
                           -display_id          => $displayid,
                           -version             => $version,
                           -release             => $release,
                           -dbname              => $dbname,
                           -primary_id_linkable => $primary_id_linkable,
                           -display_id_linkable => $display_id_linkable,
                           -priority            => $priority,
                           -db_display_name     => $db_display_name,
                           -info_type           => $info_type,
                           -info_text           => $info_text,
                           -type                => $type,
                           -secondary_db_name   => $secondary_db_name,
139
140
                           -secondary_db_table  => $secondary_db_table,
			   -description         => $description
141
142
143
144
          );


      }
145

146
      if ( defined($synonym) ) { $exDB->add_synonym($synonym) }
147

148
149
    } ## end while ( my $arrayref = shift...
  } ## end while ( my $rowcache = $sth...
150
151
152
153

  $sth->finish();

  return $exDB;
154
} ## end sub fetch_by_dbID
155
156
157
158
159
160
161
162
163
164


=head2 fetch_by_db_accession

  Arg [1]    : string $dbname - The name of the database which the provided
               accession is for.
  Arg [2]    : string $accession - The accesion of the external reference to
               retrieve.
  Example    : my $xref = $dbea->fetch_by_db_accession('Interpro','IPR003439');
               print $xref->description(), "\n" if($xref);
165
166
167
168
169
170
171
  Description: Retrieves a DBEntry (xref) via the name of the database
               it is from and its primary accession in that database.
               Undef is returned if the xref cannot be found in the
               database.
               NOTE:  In a multi-species database, this method will
               return all the entries matching the search criteria, not
               just the ones associated with the current species.
172
173
174
  Returntype : Bio::EnsEMBL::DBSQL::DBEntry
  Exceptions : thrown if arguments are incorrect
  Caller     : general, domainview
175
  Status     : Stable
176
177
178
179

=cut

sub fetch_by_db_accession {
180
  my ( $self, $dbname, $accession ) = @_;
181

182
  my $sth = $self->prepare(
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    "SELECT xref.xref_id,
            xref.dbprimary_acc,
            xref.display_label,
            xref.version,
            exDB.dbprimary_acc_linkable,
            exDB.display_label_linkable,
            exDB.priority,
            exDB.db_name,
            exDB.db_display_name,
            exDB.db_release,
            es.synonym,
            xref.info_type,
            xref.info_text,
            exDB.type,
            exDB.secondary_db_name,
198
            exDB.secondary_db_table,
Nathan Johnson's avatar
Nathan Johnson committed
199
            xref.description
200
201
202
    FROM    (xref, external_db exDB)
    LEFT JOIN external_synonym es ON
            es.xref_id = xref.xref_id
203
204
    WHERE  xref.dbprimary_acc = ?
    AND    exDB.db_name = ?
205
    AND    xref.external_db_id = exDB.external_db_id" );
206

207
208
  $sth->bind_param( 1, $accession, SQL_VARCHAR );
  $sth->bind_param( 2, $dbname,    SQL_VARCHAR );
209
  $sth->execute();
210

211
212
213
214
215
216
  if ( !$sth->rows() && lc($dbname) eq 'interpro' ) {
  # This is a minor hack that means that results still come back even
  # when a mistake was made and no interpro accessions were loaded into
  # the xref table.  This has happened in the past and had the result of
  # breaking domainview

217
    $sth->finish();
218
219
220
221
222
223
224
225
226
227
228
229
230
    $sth = $self->prepare(
      "SELECT   NULL,
                i.interpro_ac,
                i.id,
                NULL,
                NULL,
                'Interpro',
                NULL,
                NULL
        FROM    interpro i
        WHERE   i.interpro_ac = ?" );

    $sth->bind_param( 1, $accession, SQL_VARCHAR );
231
    $sth->execute();
232
233
234
235
  }

  my $exDB;

236
237
238
239
240
241
  my $max_rows = 1000;

  while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
    while ( my $arrayref = shift( @{$rowcache} ) ) {
      my ( $dbID,                $dbprimaryId,
           $displayid,           $version,
242
           $primary_id_linkable,
243
244
245
246
247
           $display_id_linkable, $priority,
           $dbname,              $db_display_name,
           $release,             $synonym,
           $info_type,           $info_text,
           $type,                $secondary_db_name,
248
           $secondary_db_table,  $description
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
      ) = @$arrayref;

      if ( !defined($exDB) ) {
        $exDB =
          Bio::EnsEMBL::DBEntry->new(
                           -adaptor             => $self,
                           -dbID                => $dbID,
                           -primary_id          => $dbprimaryId,
                           -display_id          => $displayid,
                           -version             => $version,
                           -release             => $release,
                           -dbname              => $dbname,
                           -primary_id_linkable => $primary_id_linkable,
                           -display_id_linkable => $display_id_linkable,
                           -priority            => $priority,
                           -db_display_name     => $db_display_name,
                           -info_type           => $info_type,
                           -info_text           => $info_text,
                           -type                => $type,
                           -secondary_db_name   => $secondary_db_name,
269
270
                           -secondary_db_table  => $secondary_db_table,
			   -description         => $description
271
272
          );

273

274
275
276
277
278
279
      }

      if ($synonym) { $exDB->add_synonym($synonym) }

    } ## end while ( my $arrayref = shift...
  } ## end while ( my $rowcache = $sth...
280
281

  $sth->finish();
282
283

  return $exDB;
284
} ## end sub fetch_by_db_accession
285
286


Graham McVicker's avatar
Graham McVicker committed
287
288
=head2 store

289
290
  Arg [1]    : Bio::EnsEMBL::DBEntry $exObj
               The DBEntry (xref) to be stored
291
292
293
294
  Arg [2]    : Int $ensID
               The dbID of an EnsEMBL object to associate with this external
               database entry
  Arg [3]    : string $ensType ('Transcript', 'Translation', 'Gene')
295
296
               The type of EnsEMBL object that this external database entry is
               being associated with.
297
298
299
300
301
  Arg [4]    : boolean $ignore_release
               If unset or zero, will require that the release string
               of the DBEntry object is identical to the release of the
               external database.  If set and non-zero, will ignore the
               release information.
302
  Example    : $dbea->store($db_entry, $transcript_id, 'Transcript');
303
304
305
  Description: Stores a reference to an external database (if it is not stored
               already) and associates an EnsEMBL object of a specified type
               with the external identifier.
306
307
308
  Returntype : int - the dbID of the newly created external refernce
  Exceptions : thrown when invalid dbID is passed to this method
  Caller     : scripts which load Xrefs and ObjectXrefs, etc. into Ensembl
309
  Status     : Stable
Graham McVicker's avatar
Graham McVicker committed
310
311
312

=cut

313
sub store {
314
315
  my ( $self, $exObj, $ensID, $ensType, $ignore_release ) = @_;

316
  my $dbJustInserted;
317

318
319
320
321
322
323
324
325
  #
  # backwards compatibility check:
  # check if $ensID is an object; if so, use $obj->dbID
  #
  my $ensembl_id;

  if ($ensID =~ /^\d+$/) {
    $ensembl_id = $ensID;
326

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  } elsif ( ref($ensID) eq 'Bio::EnsEMBL::Gene' or
            ref($ensID) eq 'Bio::EnsEMBL::Transcript' or
            ref($ensID) eq 'Bio::EnsEMBL::Translation') {

    warning("You should pass DBEntryAdaptor->store() a dbID rather than an ensembl object to store the xref on");

    if ($ensID->dbID) {
      $ensembl_id = $ensID->dbID;
    } else {
      throw("$ensType ".$ensID->display_id." doesn't have a dbID, can't store xref");
    }

  } else {
    throw("Invalid dbID passed to DBEntryAdaptor->store()");
  }

343
344
345
  #
  # Check for the existance of the external_db, throw if it does not exist
  #
346

347
  my ($dbRef, $release_clause);
348
349

  if ( !$ignore_release ) {
350
351
352
353
354
355
356
357
358

	if(defined $exObj->release()){
	  $release_clause = " AND db_release = ?";
	}
	else{
	  $release_clause = " AND db_release is NULL";
	}


359
    my $sth = $self->prepare( "
360
361
     SELECT external_db_id
       FROM external_db
362
      WHERE db_name    = ?
363
        $release_clause" );
364
365

    $sth->bind_param( 1, $exObj->dbname(),  SQL_VARCHAR );
366
    $sth->bind_param( 2, $exObj->release(), SQL_VARCHAR ) if defined $exObj->release();
367
368
369
370
371
372
373
374
375
376
377

    $sth->execute();

    ($dbRef) = $sth->fetchrow_array();

    if ( !$dbRef ) {
      throw(
             sprintf( "external_db [%s] release [%s] does not exist",
                      $exObj->dbname(), $exObj->release() ) );
    }
  } else {
Nathan Johnson's avatar
Nathan Johnson committed
378

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
    my $sth = $self->prepare( "
     SELECT external_db_id
       FROM external_db
      WHERE db_name = ? " );

    $sth->bind_param( 1, $exObj->dbname(), SQL_VARCHAR );

    $sth->execute();

    ($dbRef) = $sth->fetchrow_array();

    if ( !$dbRef ) {
      throw(
          sprintf( "external_db [%s] does not exist", $exObj->dbname() )
      );
    }
395
  }
396

397
398
399
  #
  # Check for the existance of the external reference, add it if not present
  #
400
  my $sth = $self->prepare( "
401
402
403
       SELECT xref_id
         FROM xref
        WHERE external_db_id = ?
404
405
406
407
          AND dbprimary_acc  = ?
          AND version        = ?
          AND info_type      = ?
          AND info_text      = ?" );
408

409
410
411
  $sth->bind_param(1,$dbRef,SQL_INTEGER);
  $sth->bind_param(2,$exObj->primary_id,SQL_VARCHAR);
  $sth->bind_param(3,$exObj->version,SQL_VARCHAR);
412
413
414
  $sth->bind_param(4,$exObj->info_type,SQL_VARCHAR);
  $sth->bind_param(5,$exObj->info_text,SQL_VARCHAR);

415
  $sth->execute();
416
  my ($dbX) = $sth->fetchrow_array();
417

418
419
  $sth->finish();
  if(!$dbX) {
420
421
422
423
424
425
426
    if(!$exObj->primary_id()) {
      throw("DBEntry cannot be stored without a primary_id attribute.");
    }

    #
    # store the new xref
    #
427
    $sth = $self->prepare( "
428
       INSERT ignore INTO xref
429
430
       SET dbprimary_acc = ?,
           display_label = ?,
Arne Stabenau's avatar
Arne Stabenau committed
431
432
           version = ?,
           description = ?,
433
434
           external_db_id = ?,
           info_type = ?,
Glenn Proctor's avatar
Glenn Proctor committed
435
           info_text = ?");
436
437
438
439
440
441
442
443
    $sth->bind_param(1, $exObj->primary_id,SQL_VARCHAR);
    $sth->bind_param(2, $exObj->display_id,SQL_VARCHAR);
    $sth->bind_param(3, $exObj->version,SQL_VARCHAR);
    $sth->bind_param(4, $exObj->description,SQL_VARCHAR);
    $sth->bind_param(5, $dbRef,SQL_INTEGER);
    $sth->bind_param(6, $exObj->info_type, SQL_VARCHAR);
    $sth->bind_param(7, $exObj->info_text, SQL_VARCHAR);

444
    $sth->execute();
445

446
447
448
449
    $dbX = $sth->{'mysql_insertid'};
    $sth->finish();
    #
    # store the synonyms for the new xref
450
    #
451
452
453
454
455
456
457
458
    my $synonym_check_sth = $self->prepare(
              "SELECT xref_id, synonym
               FROM external_synonym
               WHERE xref_id = ?
               AND synonym = ?");

    my $synonym_store_sth = $self->prepare(
        "INSERT ignore INTO external_synonym
459
         SET xref_id = ?, synonym = ?");
460
461

    my $synonyms = $exObj->get_all_synonyms();
462
    foreach my $syn ( @$synonyms ) {
463
	$synonym_check_sth->bind_param(1,$dbX,SQL_INTEGER);
Glenn Proctor's avatar
Glenn Proctor committed
464
	$synonym_check_sth->bind_param(2,$syn,SQL_VARCHAR);
465
	$synonym_check_sth->execute();
466
      my ($dbSyn) = $synonym_check_sth->fetchrow_array();
467
468
	$synonym_store_sth->bind_param(1,$dbX,SQL_INTEGER);
	$synonym_store_sth->bind_param(2,$syn,SQL_VARCHAR);
469
	$synonym_store_sth->execute() if(!$dbSyn);
470
471
472
473
474
475
476
    }
    $synonym_check_sth->finish();
    $synonym_store_sth->finish();
  }
  #
  # check if the object mapping was already stored
  #
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
  $sth = $self->prepare(
    qq(
SELECT  xref_id
FROM    object_xref
WHERE   xref_id = ?
  AND   ensembl_object_type = ?
  AND   ensembl_id = ?
  AND   (   linkage_annotation = ?
    OR      linkage_annotation IS NULL  )) );

  $sth->bind_param( 1, $dbX,                         SQL_INTEGER );
  $sth->bind_param( 2, $ensType,                     SQL_VARCHAR );
  $sth->bind_param( 3, $ensembl_id,                  SQL_INTEGER );
  $sth->bind_param( 4, $exObj->linkage_annotation(), SQL_VARCHAR );

492
  $sth->execute();
493
494
495

  my ($tst) = $sth->fetchrow_array();

496
  $sth->finish();
497

498
499
500
501
  if(!$tst) {
    #
    # Store the reference to the internal ensembl object
    #
502
503
504
505
506
507
508
509
    my $analysis_id;
    if($exObj->analysis()) {
      $analysis_id =
	$self->db()->get_AnalysisAdaptor->store($exObj->analysis());
    } else {
      $analysis_id = undef;
    }

510
    $sth = $self->prepare(
511
512
513
514
515
      qq(
INSERT IGNORE INTO object_xref
  SET   xref_id = ?,
        ensembl_object_type = ?,
        ensembl_id = ?,
516
517
        linkage_annotation = ?,
        analysis_id = ? ) );
518
519
520
521
522

    $sth->bind_param( 1, $dbX,                         SQL_INTEGER );
    $sth->bind_param( 2, $ensType,                     SQL_VARCHAR );
    $sth->bind_param( 3, $ensembl_id,                  SQL_INTEGER );
    $sth->bind_param( 4, $exObj->linkage_annotation(), SQL_VARCHAR );
523
    $sth->bind_param( 5, $analysis_id,                 SQL_INTEGER);
524

525
   #print "stored xref id $dbX in obejct_xref\n";
526
    $sth->execute();
527
528
    $exObj->dbID($dbX);
    $exObj->adaptor($self);
529
530
531
532
    my $Xidt = $sth->{'mysql_insertid'};

    #
    # If this is an IdentityXref need to store in that table too
533
    # If its OntologyXref add the linkage type to ontology_xref table
534
535
536
    #
    if ($exObj->isa('Bio::EnsEMBL::IdentityXref')) {
      $sth = $self->prepare( "
537
538
             INSERT ignore INTO identity_xref
             SET object_xref_id = ?,
539
540
541
542
543
544
             xref_identity = ?,
             ensembl_identity = ?,
             xref_start = ?,
             xref_end   = ?,
             ensembl_start = ?,
             ensembl_end = ?,
545
546
             cigar_line = ?,
             score = ?,
547
             evalue = ?" );
548
      $sth->bind_param(1,$Xidt,SQL_INTEGER);
549
550
551
552
553
554
      $sth->bind_param(2,$exObj->xref_identity,SQL_INTEGER);
      $sth->bind_param(3,$exObj->ensembl_identity,SQL_INTEGER);
      $sth->bind_param(4,$exObj->xref_start,SQL_INTEGER);
      $sth->bind_param(5,$exObj->xref_end,SQL_INTEGER);
      $sth->bind_param(6,$exObj->ensembl_start,SQL_INTEGER);
      $sth->bind_param(7,$exObj->ensembl_end,SQL_INTEGER);
555
556
557
558
      $sth->bind_param(8,$exObj->cigar_line,SQL_LONGVARCHAR);
      $sth->bind_param(9,$exObj->score,SQL_DOUBLE);
      $sth->bind_param(10,$exObj->evalue,SQL_DOUBLE);
      $sth->execute();
559
    } elsif( $exObj->isa( 'Bio::EnsEMBL::OntologyXref' )) {
560
      $sth = $self->prepare( "
561
             INSERT ignore INTO ontology_xref
562
                SET object_xref_id = ?,
563
                    source_xref_id = ?,
564
                    linkage_type = ? " );
565
566
567
      foreach my $info (@{$exObj->get_all_linkage_info()}) {
	  my( $lt, $sourceXref ) = @{$info};
	  my $sourceXid = undef;
568
	  if( $sourceXref ){
569
570
571
	      $sourceXref->is_stored($self->dbc) || $sourceXref->store;
	      $sourceXid = $sourceXref->dbID;
	  }
572
	  $sth->bind_param(1,$Xidt,SQL_INTEGER);
573
574
575
	  $sth->bind_param(2,$sourceXid,SQL_INTEGER);
	  $sth->bind_param(3,$lt,SQL_VARCHAR);
	  $sth->execute();
576
      }
577
    }
578
  }
579
  return $dbX;
580
581
}

582

583
584
585
586
587
=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
588
               otherwise returns undef.  Exists is defined as an entry with
589
590
591
592
               the same external_db and display_id
  Returntype : int
  Exceptions : thrown on incorrect args
  Caller     : GeneAdaptor::store, TranscriptAdaptor::store
593
  Status     : Stable
594
595
596
597

=cut

sub exists {
598
  my ($self, $dbe) = @_ ;
599
600

  unless($dbe && ref $dbe && $dbe->isa('Bio::EnsEMBL::DBEntry')) {
601
    throw("arg must be a Bio::EnsEMBL::DBEntry not [$dbe]");
602
  }
603

604
  my $sth = $self->prepare('SELECT x.xref_id
605
606
                            FROM   xref x, external_db xdb
                            WHERE  x.external_db_id = xdb.external_db_id
607
                            AND    x.display_label = ?
608
609
                            AND    xdb.db_name = ?
                            AND    x.dbprimary_acc = ?');
610

611
  $sth->bind_param(1,$dbe->display_id,SQL_VARCHAR);
Glenn Proctor's avatar
Glenn Proctor committed
612
  $sth->bind_param(2,$dbe->dbname,SQL_VARCHAR);
613
  $sth->bind_param(3,$dbe->primary_id,SQL_VARCHAR);
614
  $sth->execute();
615
616
617
618
619
620
621
622
623

  my ($dbID) = $sth->fetchrow_array;

  $sth->finish;

  return $dbID;
}


624
=head2 fetch_all_by_Gene
Graham McVicker's avatar
Graham McVicker committed
625

626
  Arg [1]    : Bio::EnsEMBL::Gene $gene
627
               (The gene to retrieve DBEntries for)
628
  Arg [2]    : optional external database name
629
  Arg [3]    : optional external_db type
Graham McVicker's avatar
Graham McVicker committed
630
  Example    : @db_entries = @{$db_entry_adaptor->fetch_by_Gene($gene)};
631
632
633
  Description: This returns a list of DBEntries associated with this gene.
               Note that this method was changed in release 15.  Previously
               it set the DBLinks attribute of the gene passed in to contain
634
               all of the gene, transcript, and translation xrefs associated
635
               with this gene.
636
  Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref if
637
               there is mapping data, or OntologyXref if there is linkage data.
638
  Exceptions : thows if gene object not passed
Graham McVicker's avatar
Graham McVicker committed
639
  Caller     : Bio::EnsEMBL::Gene
640
  Status     : Stable
Graham McVicker's avatar
Graham McVicker committed
641
642
643

=cut

644
sub fetch_all_by_Gene {
645
  my ( $self, $gene, $ex_db_reg, $exdb_type ) = @_;
646

647
648
649
  if(!ref($gene) || !$gene->isa('Bio::EnsEMBL::Gene')) {
    throw("Bio::EnsEMBL::Gene argument expected.");
  }
650

651
  return $self->_fetch_by_object_type($gene->dbID(), 'Gene', $ex_db_reg, $exdb_type);
Arne Stabenau's avatar
Arne Stabenau committed
652
653
}

Graham McVicker's avatar
Graham McVicker committed
654

655
=head2 fetch_all_by_Transcript
Graham McVicker's avatar
Graham McVicker committed
656

Graham McVicker's avatar
Graham McVicker committed
657
  Arg [1]    : Bio::EnsEMBL::Transcript
658
  Arg [2]    : optional external database name
659
  Arg [3]    : optional external_db type
660
  Example    : @db_entries = @{$db_entry_adaptor->fetch_by_Gene($trans)};
661
  Description: This returns a list of DBEntries associated with this
662
663
664
               transcript. Note that this method was changed in release 15.
               Previously it set the DBLinks attribute of the gene passed in
               to contain all of the gene, transcript, and translation xrefs
665
               associated with this gene.
666
  Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref if
667
               there is mapping data, or OntologyXref if there is linkage data.
668
  Exceptions : throes if transcript argument not passed
669
  Caller     : Bio::EnsEMBL::Gene
670
  Status     : Stable
Graham McVicker's avatar
Graham McVicker committed
671
672
673

=cut

674
sub fetch_all_by_Transcript {
675
  my ( $self, $trans, $ex_db_reg, $exdb_type ) = @_;
676

677
678
679
680
  if(!ref($trans) || !$trans->isa('Bio::EnsEMBL::Transcript')) {
    throw("Bio::EnsEMBL::Transcript argument expected.");
  }

681
  return $self->_fetch_by_object_type( $trans->dbID(), 'Transcript', $ex_db_reg, $exdb_type);
682
683
}

Graham McVicker's avatar
Graham McVicker committed
684

685
=head2 fetch_all_by_Translation
Graham McVicker's avatar
Graham McVicker committed
686

Graham McVicker's avatar
Graham McVicker committed
687
  Arg [1]    : Bio::EnsEMBL::Translation $trans
688
               (The translation to fetch database entries for)
689
  Arg [2]    : optional external database name
690
  Arg [3]    : optional externaldb type
691
  Example    : @db_entries = @{$db_entry_adptr->fetch_all_by_Translation($trans)};
Graham McVicker's avatar
Graham McVicker committed
692
  Description: Retrieves external database entries for an EnsEMBL translation
693
  Returntype : listref of Bio::EnsEMBL::DBEntries; may be of type IdentityXref if
694
               there is mapping data, or OntologyXref if there is linkage data.
695
  Exceptions : throws if translation object not passed
Graham McVicker's avatar
Graham McVicker committed
696
  Caller     : general
697
  Status     : Stable
Graham McVicker's avatar
Graham McVicker committed
698
699
700

=cut

701
sub fetch_all_by_Translation {
702
  my ( $self, $trans, $ex_db_reg, $exdb_type ) = @_;
703

704
705
706
  if(!ref($trans) || !$trans->isa('Bio::EnsEMBL::Translation')) {
    throw('Bio::EnsEMBL::Translation argument expected.');
  }
707
  if( ! $trans->dbID ){
708
709
710
    warning( "Cannot fetch_all_by_Translation without a dbID" );
    return [];
  }
Glenn Proctor's avatar
Glenn Proctor committed
711

712
  return $self->_fetch_by_object_type( $trans->dbID(), 'Translation', $ex_db_reg, $exdb_type );
713
714
715
}


716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737

=head2 remove_from_object

  Arg [1]    : Bio::EnsEMBL::DBEntry $dbe - The external reference which
               is to be disassociated from an ensembl object.
  Arg [2]    : Bio::EnsEMBL::Storable $object - The ensembl object the
               external reference is to be disassociated from
  Arg [3]    : string $object_type - The type of the ensembl object.
               E.g. 'Gene', 'Transcript', 'Translation'
  Example    :
               # remove all dbentries from this translation
               foreach my $dbe (@{$translation->get_all_DBEntries()}) {
                 $dbe_adaptor->remove($dbe, $translation, 'Translation');
               }
  Description: Removes an association between an ensembl object and a
               DBEntry (xref).  This does not remove the actual xref from
               the database, only its linkage to the ensembl object.
  Returntype : none
  Exceptions : Throw on incorrect arguments.
               Warning if object or dbentry is not stored in this database.
  Caller     : TranscriptAdaptor::remove, GeneAdaptor::remove,
               TranslationAdaptor::remove
738
  Status     : Stable
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775

=cut

sub remove_from_object {
  my $self = shift;
  my $dbe  = shift;
  my $object = shift;
  my $object_type = shift;

  if(!ref($dbe) || !$dbe->isa('Bio::EnsEMBL::DBEntry')) {
    throw("Bio::EnsEMBL::DBEntry argument expected.");
  }

  if(!ref($object) || !$dbe->isa('Bio::EnsEMBL::Storable')) {
    throw("Bio::EnsEMBL::Storable argument expected.");
  }

  if(!$object_type) {
    throw("object_type string argument expected.");
  }

  # make sure both the dbentry and the object it is allegedly linked to
  # are stored in this database

  if(!$object->is_stored($self->db())) {
    warning("Cannot remove DBEntries for $object_type " . $object->dbID() .
            ". Object is not stored in this database.");
    return;
  }

  if(!$dbe->is_stored($self->db())) {
    warning("Cannot remove DBEntry ".$dbe->dbID() . ". Is not stored " .
            "in this database.");
    return;
  }

  # obtain the identifier of the link from the object_xref table
776
  #No need to compare linkage_annotation here
777
778
779
780
781
782
  my $sth = $self->prepare
    ("SELECT ox.object_xref_id " .
     "FROM   object_xref ox ".
     "WHERE  ox.xref_id = ? " .
     "AND    ox.ensembl_id = ? " .
     "AND    ox.ensembl_object_type = ?");
783
784
785
786
  $sth->bind_param(1,$dbe->dbID,SQL_INTEGER);
  $sth->bind_param(2,$object->dbID,SQL_INTEGER);
  $sth->bind_param(3,$object_type,SQL_VARCHAR);
  $sth->execute();
787
788
789
790
791
792
793
794
795
796
797

  if(!$sth->rows() == 1) {
    $sth->finish();
    return;
  }

  my ($ox_id) = $sth->fetchrow_array();
  $sth->finish();

  # delete from the tables which contain additional linkage information

798
  $sth = $self->prepare("DELETE FROM ontology_xref WHERE object_xref_id = ?");
799
800
  $sth->bind_param(1,$ox_id,SQL_INTEGER);
  $sth->execute();
801
802
803
  $sth->finish();

  $sth = $self->prepare("DELETE FROM identity_xref WHERE object_xref_id = ?");
804
805
  $sth->bind_param(1,$ox_id,SQL_INTEGER);
  $sth->execute();
806
807
808
809
  $sth->finish();

  # delete the actual linkage itself
  $sth = $self->prepare("DELETE FROM object_xref WHERE object_xref_id = ?");
810
811
  $sth->bind_param(1,$ox_id,SQL_INTEGER);
  $sth->execute();
812
813
814
815
816
817
818
  $sth->finish();

  return;
}


=head2 _fetch_by_object_type
Graham McVicker's avatar
Graham McVicker committed
819

820
  Arg [1]    : string $ensID
821
  Arg [2]    : string $ensType (object type to be returned)
822
  Arg [3]    : optional $exdbname (external database name)
823
824
825
826
827
               (may be an SQL pattern containing '%' which matches any
               number of characters)
  Arg [4]    : optional $exdb_type (external database type)
               (may be an SQL pattern containing '%' which matches any
               number of characters)
828
829
  Example    : $self->_fetch_by_object_type( $translation_id, 'Translation' )
  Description: Fetches DBEntry by Object type
830
831
832
               NOTE:  In a multi-species database, this method will
               return all the entries matching the search criteria, not
               just the ones associated with the current species.
833
834


835
  Returntype : arrayref of DBEntry objects; may be of type IdentityXref if
836
               there is mapping data, or OntologyXref if there is linkage data.
837
838
  Exceptions : none
  Caller     : fetch_all_by_Gene
839
840
               fetch_all_by_Translation
               fetch_all_by_Transcript
841
  Status     : Stable
Graham McVicker's avatar
Graham McVicker committed
842
843
844

=cut

Graham McVicker's avatar
Graham McVicker committed
845
sub _fetch_by_object_type {
846
  my ( $self, $ensID, $ensType, $exdbname, $exdb_type ) = @_;
847

848
  my @out;
849

850
  if ( !defined($ensID) ) {
851
    throw("Can't fetch_by_EnsObject_type without an object");
Michele Clamp's avatar
Michele Clamp committed
852
  }
853
854

  if ( !defined($ensType) ) {
855
    throw("Can't fetch_by_EnsObject_type without a type");
Michele Clamp's avatar
Michele Clamp committed
856
  }
857
858

  #  my $sth = $self->prepare("
859
  my $sql = (<<SSQL);
Graham McVicker's avatar
Graham McVicker committed
860
    SELECT xref.xref_id, xref.dbprimary_acc, xref.display_label, xref.version,
861
           exDB.dbprimary_acc_linkable, exDB.display_label_linkable,
862
           exDB.priority,
Glenn Proctor's avatar
Glenn Proctor committed
863
           exDB.db_name, exDB.db_release, exDB.status, exDB.db_display_name,
864
           exDB.secondary_db_name, exDB.secondary_db_table,
865
           oxr.object_xref_id,
866
           es.synonym,
867
868
           idt.xref_identity, idt.ensembl_identity, idt.xref_start,
           idt.xref_end, idt.ensembl_start, idt.ensembl_end,
869
           idt.cigar_line, idt.score, idt.evalue, oxr.analysis_id,
870
           gx.linkage_type,
871
           xref.info_type, xref.info_text, exDB.type, gx.source_xref_id,
Nathan Johnson's avatar
Nathan Johnson committed
872
           oxr.linkage_annotation, xref.description
873
    FROM   (xref xref, external_db exDB, object_xref oxr)
874
    LEFT JOIN external_synonym es on es.xref_id = xref.xref_id
Graham McVicker's avatar
Graham McVicker committed
875
    LEFT JOIN identity_xref idt on idt.object_xref_id = oxr.object_xref_id
876
    LEFT JOIN ontology_xref gx on gx.object_xref_id = oxr.object_xref_id
Graham McVicker's avatar
Graham McVicker committed
877
    WHERE  xref.xref_id = oxr.xref_id
878
      AND  xref.external_db_id = exDB.external_db_id
879
880
      AND  oxr.ensembl_id = ?
      AND  oxr.ensembl_object_type = ?
881
SSQL
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902

  if ( defined($exdbname) ) {
    if ( index( $exdbname, '%' ) != -1 ) {
      $sql .= " AND exDB.db_name = "
        . $self->dbc()->db_handle()->quote( $exdbname, SQL_VARCHAR );
    } else {
      $sql .= " AND exDB.db_name LIKE "
        . $self->dbc()->db_handle()->quote( $exdbname, SQL_VARCHAR );
    }
  }

  if ( defined($exdb_type) ) {
    if ( index( $exdb_type, '%' ) != -1 ) {
      $sql .= " AND exDB.type = "
        . $self->dbc()->db_handle()->quote( $exdb_type, SQL_VARCHAR );
    } else {
      $sql .= " AND exDB.type LIKE "
        . $self->dbc()->db_handle()->quote( $exdb_type, SQL_VARCHAR );
    }
  }

903
  my $sth = $self->prepare($sql);
904

905
906
  $sth->bind_param( 1, $ensID,   SQL_INTEGER );
  $sth->bind_param( 2, $ensType, SQL_VARCHAR );
907
  $sth->execute();
908
909
910
911
912
913
914
915
916

  my ( %seen, %linkage_types, %synonyms );

  my $max_rows = 1000;

  while ( my $rowcache = $sth->fetchall_arrayref( undef, $max_rows ) ) {
    while ( my $arrRef = shift( @{$rowcache} ) ) {
      my ( $refID,                  $dbprimaryId,
           $displayid,              $version,
917
           $primary_id_linkable,
918
919
920
921
922
           $display_id_linkable,    $priority,
           $dbname,                 $release,
           $exDB_status,            $exDB_db_display_name,
           $exDB_secondary_db_name, $exDB_secondary_db_table,
           $objid,                  $synonym,
923
924
925
           $xrefid,                 $ensemblid,
           $xref_start,             $xref_end,
           $ensembl_start,          $ensembl_end,
926
927
928
929
           $cigar_line,             $score,
           $evalue,                 $analysis_id,
           $linkage_type,           $info_type,
           $info_text,              $type,
930
           $source_xref_id,         $link_annotation,
931
	   $description
932
933
934
935
936
      ) = @$arrRef;

      my $linkage_key =
        ( $linkage_type || '' ) . ( $source_xref_id || '' );

937
938
939
940
941
942
943

      my $analysis = undef;
      if ( defined($analysis_id) ) {
	$analysis =
	  $self->db()->get_AnalysisAdaptor()->fetch_by_dbID($analysis_id);
      }

944
945
946
947
948
949
950
951
952
953
954
      my %obj_hash = ( 'adaptor'            => $self,
                       'dbID'               => $refID,
                       'primary_id'         => $dbprimaryId,
                       'display_id'         => $displayid,
                       'version'            => $version,
                       'release'            => $release,
                       'info_type'          => $info_type,
                       'info_text'          => $info_text,
                       'type'               => $type,
                       'secondary_db_name'  => $exDB_secondary_db_name,
                       'secondary_db_table' => $exDB_secondary_db_table,
955
                       'dbname'             => $dbname,
956
                       'description'        => $description,
957
958
                       'linkage_annotation' => $link_annotation,
                       'analysis'           => $analysis);
959
960
961
962
963

      # Using an outer join on the synonyms as well as on identity_xref,
      # we now have to filter out the duplicates (see v.1.18 for
      # original). Since there is at most one identity_xref row per
      # xref, this is easy enough; all the 'extra' bits are synonyms.
964
      my $source_xref;
965
      if ( !$seen{$refID} ) {
966
967
	
	my $exDB;
968
        if ( ( defined($xrefid) ) ) {  # an xref with similarity scores
969
          $exDB = Bio::EnsEMBL::IdentityXref->new_fast( \%obj_hash );
970
971
          $exDB->xref_identity($xrefid);
          $exDB->ensembl_identity($ensemblid);
972
973

          $exDB->cigar_line($cigar_line);
974
975
976
977
          $exDB->xref_start($xref_start);
          $exDB->xref_end($xref_end); # was not here before 14th Jan 2009 ????
          $exDB->ensembl_start($ensembl_start);
          $exDB->ensembl_end($ensembl_end);
978
979
980
981
          $exDB->score($score);
          $exDB->evalue($evalue);

        } elsif ( defined $linkage_type && $linkage_type ne "" ) {
982
          $exDB = Bio::EnsEMBL::OntologyXref->new_fast( \%obj_hash );
983
          $source_xref = ( defined($source_xref_id)
984
985
986
987
988
989
990
                              ? $self->fetch_by_dbID($source_xref_id)
                              : undef );
          $exDB->add_linkage_type( $linkage_type, $source_xref || () );
          $linkage_types{$refID}->{$linkage_key} = 1;

        } else {
          $exDB = Bio::EnsEMBL::DBEntry->new_fast( \%obj_hash );
991
        }
992

993
        if ( defined($exDB_status) ) { $exDB->status($exDB_status) }
994

995
996
997
998
        $exDB->primary_id_linkable($primary_id_linkable);
        $exDB->display_id_linkable($display_id_linkable);
        $exDB->priority($priority);
        $exDB->db_display_name($exDB_db_display_name);
999

1000
1001
        push( @out, $exDB );
        $seen{$refID} = $exDB;
1002