BaseFeatureAdaptor.pm 19.2 KB
Newer Older
1
#
2
# EnsEMBL module for Bio::EnsEMBL::DBSQL::BaseAlignFeatureAdaptor
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# Cared for by Ewan Birney <birney@ebi.ac.uk>
#
# Copyright Ewan Birney
#
# You may distribute this module under the same terms as perl itself

# POD documentation - main docs before the code

=head1 NAME

Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor - Abstract Base class for 
                                          FeatureAdaptors

=head1 SYNOPSIS

Abstract class should not be instantiated.  Implementation of
abstract methods must be performed by subclasses.

=head1 DESCRIPTION

This is a base adaptor for feature adaptors. This base class is simply a way
of eliminating code duplication through the implementation of methods 
common to all feature adaptors.

=head1 AUTHOR - Ewan Birney

Email birney@ebi.ac.uk

Describe contact details here

=head1 APPENDIX

The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _

=cut


# Let the code begin...


package Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
use vars qw(@ISA);
use strict;

# Object preamble - inherits from Bio::EnsEMBL::Root

use Bio::EnsEMBL::DBSQL::BaseAdaptor;
51
use Bio::EnsEMBL::Utils::Cache;
52
53
54

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

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
my $SLICE_FEATURE_CACHE_SIZE = 12;


=head2 new

  Arg [1]    : list of args @args
               Superclass constructor arguments
  Example    : none
  Description: Constructor which just initializes internal cache structures
  Returntype : Bio::EnsEMBL::BaseFeatureAdaptor
  Exceptions : none
  Caller     : implementing subclass constructors

=cut

sub new {
  my ($class, @args) = @_;

  my $self = $class->SUPER::new(@args);

  #initialize caching data structures
  tie(%{$self->{'_slice_feature_cache'}}, 
      'Bio::EnsEMBL::Utils::Cache',
      $SLICE_FEATURE_CACHE_SIZE);

  return $self;
}
82
83
84
85
86
87
88

=head2 generic_fetch

  Arg [1]    : (optional) string $constraint
               An SQL query constraint (i.e. part of the WHERE clause)
  Arg [2]    : (optional) string $logic_name
               the logic_name of the analysis of the features to obtain
89
  Example    : $fts = $a->generic_fetch('contig_id in (1234, 1235)', 'Swall');
90
91
  Description: Performs a database fetch and returns feature objects in
               contig coordinates.
92
  Returntype : listref of Bio::EnsEMBL::SeqFeature in contig coordinates
93
94
95
96
  Exceptions : none
  Caller     : BaseFeatureAdaptor, ProxyDnaAlignFeatureAdaptor::generic_fetch

=cut
97
98
  
sub generic_fetch {
99
  my ($self, $constraint, $logic_name, $mapper, $slice) = @_;
100
101
102
103
104
105
  
  my $tablename = $self->_tablename();
  my $columns = join(', ', $self->_columns());
  
  if($logic_name) {
    #determine the analysis id via the logic_name
106
107
    my $analysis = 
      $self->db->get_AnalysisAdaptor()->fetch_by_logic_name($logic_name);
108
109
    unless(defined $analysis && $analysis->dbID() ) {
      $self->warn("No analysis for logic name $logic_name exists\n");
Web Admin's avatar
Web Admin committed
110
      return [];
111
112
113
114
115
116
117
118
119
120
121
    }
    
    my $analysis_id = $analysis->dbID();
    
    if($constraint) {
      $constraint .= " AND analysis_id = $analysis_id";
    } else {
      $constraint = " analysis_id = $analysis_id";
    }
  } 
      
122
123
  my $sql = "SELECT $columns FROM $tablename " . 
    ($constraint ? " where $constraint " : '' );
124
  my $sth = $self->prepare($sql);
Web Admin's avatar
Web Admin committed
125
  $sth->execute;
126
  return $self->_objs_from_sth($sth, $mapper, $slice);
127
128
129
130
}


=head2 fetch_by_dbID
131
132
133
134
135
136

  Arg [1]    : int $id
               the unique database identifier for the feature to be obtained 
  Example    : $feat = $adaptor->fetch_by_dbID(1234);
  Description: Returns the feature created from the database defined by the
               the id $id. 
137
  Returntype : Bio::EnsEMBL::SeqFeature
138
139
  Exceptions : thrown if $id is not defined
  Caller     : general
140
141
142
143
144
145
146
147
148
149
150
151
152

=cut

sub fetch_by_dbID{
  my ($self,$id) = @_;
  
  unless(defined $id) {
    $self->throw("fetch_by_dbID must have an id");
  }

  my $tablename = $self->_tablename();
  my $constraint = "${tablename}_id = $id";

153
  #return first element of _generic_fetch list
154
  my ($feat) = @{$self->generic_fetch($constraint)}; 
155
  return $feat;
156
157
158
}


159
=head2 fetch_all_by_Contig_constraint
160

161
162
  Arg [1]    : Bio::EnsEMBL::RawContig $contig
               The contig object from which features are to be obtained
163
164
165
166
  Arg [2]    : (optional) string $constraint
               An SQL query constraint (i.e. part of the WHERE clause)
  Arg [3]    : (optional) string $logic_name
               the logic name of the type of features to obtain
167
168
  Example    : $fs = $a->fetch_all_by_Contig_constraint($ctg,'perc_ident>5.0');
  Description: Returns a listref of features created from the database which 
169
170
171
               are on the contig defined by $cid and fulfill the SQL constraint
               defined by $constraint. If logic name is defined, only features
               with an analysis of type $logic_name will be returned. 
172
  Returntype : listref of Bio::EnsEMBL::SeqFeature in contig coordinates
173
174
  Exceptions : thrown if $cid is not defined
  Caller     : general
175
176
177

=cut

178
sub fetch_all_by_Contig_constraint {
179
  my ($self, $contig, $constraint, $logic_name) = @_;
180
  
181
182
  unless( defined $contig ) {
    $self->throw("fetch_by_Contig_constraint must have an contig");
183
184
  }

185
186
187
188
189
190
  unless( ref $contig && $contig->isa('Bio::EnsEMBL::RawContig')) {
    $self->throw("contig argument is not a Bio::EnsEMBL::RawContig object\n");
  }

  my $cid = $contig->dbID();

191
192
193
194
195
196
  if($constraint) {
    $constraint .= " AND contig_id = $cid";
  } else {
    $constraint = "contig_id = $cid";
  }

Graham McVicker's avatar
Graham McVicker committed
197
  return $self->generic_fetch($constraint, $logic_name);
198
199
}

200

Arne Stabenau's avatar
Arne Stabenau committed
201
=head2 fetch_all_by_RawContig
202

203
204
  Arg [1]    : Bio::EnsEMBL::RawContig $contig 
               the contig from which features should be obtained
205
206
  Arg [2]    : (optional) string $logic_name
               the logic name of the type of features to obtain
207
  Example    : @fts = $a->fetch_all_by_Contig($contig, 'wall');
208
209
210
211
  Description: Returns a list of features created from the database which are 
               are on the contig defined by $cid If logic name is defined, 
               only features with an analysis of type $logic_name will be 
               returned. 
212
  Returntype : listref of Bio::EnsEMBL::*Feature in contig coordinates
213
214
215
216
  Exceptions : none
  Caller     : general

=cut
217
   
Arne Stabenau's avatar
Arne Stabenau committed
218
219
220
221
222
223
224
sub fetch_all_by_RawContig {
  my ( $self, $contig, $logic_name ) = @_;

  return $self->fetch_all_by_Contig_constraint($contig, '',$logic_name);
}

# old naming convention, replace calls to this.
225
sub fetch_all_by_Contig{
226
  my ($self, $contig, $logic_name) = @_;
227

Arne Stabenau's avatar
Arne Stabenau committed
228
  $self->warn( "Please use ...by_RawContig instead of ...by_Contig" );
229
  #fetch by contig id constraint with empty constraint
230
  return $self->fetch_all_by_Contig_constraint($contig, '',$logic_name);
231
232
233
}


Arne Stabenau's avatar
Arne Stabenau committed
234

235
=head2 fetch_all_by_Contig_and_score
236

237
238
  Arg [1]    : Bio::EnsEMBL::RawContig $contig 
               the contig from which features should be obtained
239
  Arg [2]    : (optional) float $score
240
241
242
               the lower bound of the score of the features to obtain
  Arg [3]    : (optional) string $logic_name
               the logic name of the type of features to obtain
243
  Example    : @fts = $a->fetch_by_Contig_and_score(1, 50.0, 'Swall');
244
245
246
247
  Description: Returns a list of features created from the database which are 
               are on the contig defined by $cid and which have score greater
               than score.  If logic name is defined, only features with an 
               analysis of type $logic_name will be returned. 
248
  Returntype : listref of Bio::EnsEMBL::*Feature in contig coordinates
249
250
251
252
253
  Exceptions : thrown if $score is not defined
  Caller     : general

=cut

254
sub fetch_all_by_Contig_and_score{
255
  my($self, $contig, $score, $logic_name) = @_;
256
257
258

  my $constraint;

259
  if(defined $score){
260
261
262
    $constraint = "score > $score";
  }
    
263
264
  return $self->fetch_all_by_Contig_constraint($contig, $constraint, 
					       $logic_name);
265
266
267
}


268
=head2 fetch_all_by_Slice
269
270
271
272
273

  Arg [1]    : Bio::EnsEMBL::Slice $slice
               the slice from which to obtain features
  Arg [2]    : (optional) string $logic_name
               the logic name of the type of features to obtain
274
275
276
277
278
279
  Example    : $fts = $a->fetch_all_by_Slice($slice, 'Swall');
  Description: Returns a listref of features created from the database 
               which are on the Slice defined by $slice. If $logic_name is 
               defined only features with an analysis of type $logic_name 
               will be returned. 
  Returntype : listref of Bio::EnsEMBL::SeqFeatures in Slice coordinates
280
281
282
283
284
  Exceptions : none
  Caller     : Bio::EnsEMBL::Slice

=cut

285
sub fetch_all_by_Slice {
286
287
288
  my ($self, $slice, $logic_name) = @_;
  
  #fetch by constraint with empty constraint
289
  return $self->fetch_all_by_Slice_constraint($slice, '', $logic_name);
290
291
292
}


293
=head2 fetch_all_by_Slice_and_score
294
295
296

  Arg [1]    : Bio::EnsEMBL::Slice $slice
               the slice from which to obtain features
297
  Arg [2]    : (optional) float $score
298
299
300
               lower bound of the the score of the features retrieved
  Arg [3]    : (optional) string $logic_name
               the logic name of the type of features to obtain
301
  Example    : $fts = $a->fetch_all_by_Slice($slice, 'Swall');
302
303
304
305
306
  Description: Returns a list of features created from the database which are 
               are on the Slice defined by $slice and which have a score 
               greated than $score. If $logic_name is defined, 
               only features with an analysis of type $logic_name will be 
               returned. 
307
  Returntype : listref of Bio::EnsEMBL::SeqFeatures in Slice coordinates
308
309
310
311
312
  Exceptions : none
  Caller     : Bio::EnsEMBL::Slice

=cut

313
sub fetch_all_by_Slice_and_score {
314
315
316
  my ($self, $slice, $score, $logic_name) = @_;
  my $constraint;

317
  if(defined $score) {
318
319
320
    $constraint = "score > $score";
  }

321
322
  return $self->fetch_all_by_Slice_constraint($slice, $constraint, 
					      $logic_name);
323
324
325
}  


326
=head2 fetch_all_by_Slice_constraint
327

328
329
330
331
332
  Arg [1]    : Bio::EnsEMBL::Slice $slice
               the slice from which to obtain features
  Arg [2]    : (optional) string $constraint
               An SQL query constraint (i.e. part of the WHERE clause)
  Arg [3]    : (optional) string $logic_name
333
               the logic name of the type of features to obtain
334
335
  Example    : $fs = $a->fetch_all_by_Slice_constraint($slc, 'perc_ident > 5');
  Description: Returns a listref of features created from the database which 
336
337
338
339
               are on the Slice defined by $slice and fulfill the SQL 
               constraint defined by $constraint. If logic name is defined, 
               only features with an analysis of type $logic_name will be 
               returned. 
340
  Returntype : listref of Bio::EnsEMBL::SeqFeatures in Slice coordinates
341
342
  Exceptions : thrown if $slice is not defined
  Caller     : Bio::EnsEMBL::Slice
343
344
345

=cut

346
sub fetch_all_by_Slice_constraint {
347
  my($self, $slice, $constraint, $logic_name) = @_;
348

349
350
  unless(defined $slice && ref $slice && $slice->isa("Bio::EnsEMBL::Slice")) {
    $self->throw("Slice arg must be a Bio::EnsEMBL::Slice not a [$slice]\n");
351
352
  }

353
  #check the cache and return if we have already done this query
Web Admin's avatar
Web Admin committed
354
  $logic_name = '' unless defined $logic_name;
355

Web Admin's avatar
Web Admin committed
356
  my $key = join($slice->name, $constraint, $logic_name);
357
358
  return $self->{'_slice_feature_cache'}{$key} 
    if $self->{'_slice_feature_cache'}{$key};
Web Admin's avatar
Web Admin committed
359
    
360
361
362
  my $chr_start = $slice->chr_start();
  my $chr_end   = $slice->chr_end();
  				 
363
364
  my $mapper = 
    $self->db->get_AssemblyMapperAdaptor->fetch_by_type($slice->assembly_type);
365
366

  #get the list of contigs this slice is on
367
368
  my @cids = 
    $mapper->list_contig_ids( $slice->chr_name, $chr_start ,$chr_end );
369
  
Web Admin's avatar
Web Admin committed
370
  return [] unless scalar(@cids);
371
372
373

  my $cid_list = join(',',@cids);

374
  #construct the SQL constraint for the contig ids 
375
376
377
378
379
  if($constraint) {
    $constraint .= " AND contig_id IN ($cid_list)";
  } else {
    $constraint = "contig_id IN ($cid_list)";
  }
380

381
382
  my $features = 
    $self->generic_fetch($constraint, $logic_name, $mapper, $slice); 
Graham McVicker's avatar
Graham McVicker committed
383
  
Graham McVicker's avatar
typo  
Graham McVicker committed
384
  if(@$features && $features->[0]->contig == $slice) {
Graham McVicker's avatar
Graham McVicker committed
385
    #features have been converted to slice coords already, cache and return
Web Admin's avatar
Web Admin committed
386
    return $self->{'_slice_feature_cache'}{$key} = $features;
387
  }
388

389
  my @out = ();
390
  
391
  #convert the features to slice coordinates from raw contig coordinates
Web Admin's avatar
Web Admin committed
392

393
  foreach my $f (@$features) {
394
    #since feats were obtained in contig coords, attached seq is a contig
395
    my $contig_id = $f->contig->dbID();
396

397
    my ($chr_name, $start, $end, $strand) = 
398
399
      $mapper->fast_to_assembly($contig_id, $f->start(), 
				$f->end(),$f->strand(),"rawcontig");
400

401
402
403
404
    # not defined start means gap
    next unless defined $start;     
    # maps to region outside desired area 
    next if ($start > $chr_end) || ($end <= $chr_start);  
405
406
    
    #shift the feature start, end and strand in one call
Web Admin's avatar
Web Admin committed
407
    $f->move( $start - $chr_start, $end - $chr_start, $strand );
408
    $f->contig($slice);
409
    
Web Admin's avatar
Web Admin committed
410
    push @out,$f;
411
412
  }
  
413
  #update the cache
Web Admin's avatar
Web Admin committed
414
  return $self->{'_slice_feature_cache'}{$key} = \@out;
415
416
417
}


418
=head2 store
419

420
  Arg [1]    : list of Bio::EnsEMBL::SeqFeature
421
422
423
424
425
426
427
  Example    : $adaptor->store(@feats);
  Description: ABSTRACT  Subclasses are responsible for implementing this 
               method.  It should take a list of features and store them in 
               the database.
  Returntype : none
  Exceptions : thrown method is not implemented by subclass
  Caller     : general
428
429
430
431
432
433
434
435
436
437

=cut

sub store{
  my $self = @_;

  $self->throw("Abstract method store not defined by implementing subclass\n");
}


438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
=head2 remove

  Arg [1]    : A feature $feature 
  Example    : $feature_adaptor->remove($feature);
  Description: This removes a feature from the database.  The table the
               feature is removed from is defined by the abstract method
               _tablename, and the primary key of the table is assumed
               to be _tablename() . '_id'.  The feature argument must 
               be an object implementing the dbID method, and for the
               feature to be removed from the datasbase a dbID value must
               be returned.
  Returntype : none
  Exceptions : thrown if $feature arg does not implement dbID(), or if 
               $feature->dbID is not a true value               
  Caller     : general

=cut

456

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
sub remove {
  my ($self, $feature) = @_;

  unless($feature->can('dbID')) {
    $self->throw("Feature [$feature] does not implement method dbID");
  }

  unless($feature->dbID) {
    $self->warn("BaseFeatureAdaptor::remove - dbID not defined - " .
                "feature could not be removed");
  }

  my $table = $self->_tablename();

  my $sth = $self->prepare("DELETE FROM $table WHERE ${table}_id = ?");
  $sth->execute($feature->dbID());

  #unset the feature dbID
  $feature->dbID('');
  
  return;
}


481
482
=head2 _tablename

483
484
485
486
487
488
489
490
  Args       : none
  Example    : $tablename = $self->_table_name()
  Description: ABSTRACT PROTECTED Subclasses are responsible for implementing
               this method.  It should return the name of the table to be
               used to obtain features.  
  Returntype : string
  Exceptions : thrown if not implemented by subclass
  Caller     : BaseFeatureAdaptor::generic_fetch
491
492
493
494
495
496
497
498
499
500
501

=cut

sub _tablename {
  my $self = shift;

  $self->throw("abstract method _tablename not defined by implementing" .
               " subclass of AlignFeatureAdaptor");
  return undef;
}

502

503
504
=head2 _columns

505
506
507
508
509
510
511
512
  Args       : none
  Example    : $tablename = $self->_columns()
  Description: ABSTRACT PROTECTED Subclasses are responsible for implementing
               this method.  It should return a list of columns to be used
               for feature creation
  Returntype : list of strings
  Exceptions : thrown if not implemented by subclass
  Caller     : BaseFeatureAdaptor::generic_fetch
513
514
515
516
517
518
519
520
521
522
523

=cut

sub _columns {
  my $self = shift;

  $self->throw("abstract method _columns not defined by implementing" .
               " subclass of AlignFeatureAdaptor");
}


524
=head2 _objs_from_sth
525
526
527
528
529
530
531
532
533
534
535
536
537

  Arg [1]    : DBI::row_hashref $hashref containing key-value pairs 
               for each of the columns specified by the _columns method
  Example    : my @feats = $self->_obj_from_hashref
  Description: ABSTRACT PROTECTED The subclass is responsible for implementing
               this method.  It should take in a DBI row hash reference and
               return a list of created features in contig coordinates.
  Returntype : list of Bio::EnsEMBL::*Features in contig coordinates
  Exceptions : thrown if not implemented by subclass
  Caller     : BaseFeatureAdaptor::generic_fetch

=cut

538
sub _objs_from_sth {
539
540
541
542
543
544
  my $self = shift;

  $self->throw("abstract method _obj_from_hashref not defined by implementing"
             . " subclass of AlignFeatureAdaptor");
} 

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

=head2 deleteObj

  Arg [1]    : none
  Example    : none
  Description: Cleans up internal caches and references to other objects so
               that correct garbage collection may occur.
  Returntype : none
  Exceptions : none
  Caller     : Bio::EnsEMBL::DBConnection::deleteObj

=cut

sub deleteObj {
  my $self = shift;

  #flush feature cache
  %{$self->{'_slice_feature_cache'}} = ();
}

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691



=head2 fetch_by_Contig_constraint

  Arg [1]    : none
  Example    : none
  Description: DEPRECATED use fetch_all_by_RawContig_constraint instead
  Returntype : none
  Exceptions : none
  Caller     : none

=cut

sub fetch_by_Contig_constraint {
  my ($self, @args) = @_;

  $self->warn("fetch_by_Contig_constraint has been renamed fetch_all_by_RawContig_constraint\n" . caller);

  return $self->fetch_all_by_RawContig_constraint(@args);
}



=head2 fetch_by_Contig

  Arg [1]    : none
  Example    : none
  Description: DEPRECATED use fetch_all_by_RawContig instead
  Returntype : none
  Exceptions : none
  Caller     : none

=cut

sub fetch_by_Contig {
  my ($self, @args) = @_;

  $self->warn("fetch_by_Contig has been renamed fetch_all_by_RawContig\n" . caller);

  return $self->fetch_all_by_RawContig(@args);
}


=head2 fetch_by_Contig_and_score

  Arg [1]    : none
  Example    : none
  Description: DEPRECATED use fetch_all_by_RawContig_and_score instead
  Returntype : none
  Exceptions : none
  Caller     : none

=cut

sub fetch_by_Contig_and_score {
  my ($self, @args) = @_;

  $self->warn("fetch_by_Contig_and_score has been renamed fetch_all_by_RawContig_and_score\n" . caller);

  return $self->fetch_all_by_RawContig_and_score(@args);
}


=head2 fetch_by_Slice_and_score

  Arg [1]    : none
  Example    : none
  Description: DEPRECATED use fetch_all_by_Slice_and_score instead
  Returntype : none
  Exceptions : none
  Caller     : none

=cut

sub fetch_by_Slice_and_score {
  my ($self, @args) = @_;

  $self->warn("fetch_by_Slice_and_score has been renamed fetch_all_by_Slice_and_score\n" . caller);

  return $self->fetch_all_by_Slice_and_score(@args);
}


=head2 fetch_by_Slice_constraint

  Arg [1]    : none
  Example    : none
  Description: DEPRECATED use fetch_all_by_Slice_constraint instead
  Returntype : none
  Exceptions : none
  Caller     : none

=cut

sub fetch_by_Slice_constraint {
  my ($self, @args) = @_;

  $self->warn("fetch_by_Slice_constraint has been renamed fetch_all_by_Slice_constraint\n" . caller);

  return $self->fetch_all_by_Slice_constraint(@args);
}



=head2 fetch_by_Slice

  Arg [1]    : none
  Example    : none
  Description: DEPRECATED use fetch_all_by_Slice instead
  Returntype : none
  Exceptions : none
  Caller     : none

=cut

sub fetch_by_Slice {
  my ($self, @args) = @_;

  $self->warn("fetch_by_Slice has been renamed fetch_all_by_Slice\n" . caller);

  return $self->fetch_all_by_Slice(@args);
}




692
693
694
1;