PredictionTranscriptAdaptor.pm 13.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# EnsEMBL Exon reading writing adaptor for mySQL
#
# Author: Arne Stabenau
# 
# Date : 22.11.2001
#

=head1 NAME

Bio::EnsEMBL::DBSQL::PredictionTranscriptAdaptor - 
11
MySQL Database queries to load and store PredictionExons
12
13
14

=head1 SYNOPSIS

15
16
17
18
19
20
21
22
#get a prediction transcript adaptor from the database
$pta = $database_adaptor->get_PredictionTranscriptAdaptor();

#get a slice on a region of chromosome 1
$sa = $database_adaptor->get_SliceAdaptor();
$slice = $sa->fetch_by_chr_start_end('1', 100000, 200000);

#get all the prediction transcripts from the slice region
23
$prediction_transcripts = @{$pta->fetch_all_by_Slice($slice)};
24

25
26
=head1 CONTACT

27
Email questions to the EnsEMBL developer list: <ensembl-dev@ebi.ac.uk>
28
29
30
31
32
33
34
35

=cut

package Bio::EnsEMBL::DBSQL::PredictionTranscriptAdaptor;

use vars qw( @ISA );
use strict;

36
use Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor;
37
use Bio::EnsEMBL::DBSQL::DBAdaptor;
38
use Bio::EnsEMBL::DBSQL::AnalysisAdaptor;
39
40
use Bio::EnsEMBL::PredictionTranscript;

41
@ISA = qw( Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor );
42
43


44
=head2 _tables
45

46
47
48
49
50
  Arg [1]    : none
  Example    : none
  Description: Implements abstract superclass method to define the table used
               to retrieve prediction transcripts from the database
  Returntype : string
51
  Exceptions : none
52
  Caller     : generic_fetch
53
54
55

=cut

56
sub _tables {
57
  my $self = shift;
58

59
  return ['prediction_transcript', 'p'];
60
}
61

62
63


64
=head2 _columns
65

66
67
68
69
70
71
72
73
  Arg [1]    : none
  Example    : none
  Description: Implements abstract superclass method to define the columns
               retrieved in database queries used to create prediction 
               transcripts.
  Returntype : list of strings
  Exceptions : none
  Caller     : generic_fetch
74
75
76

=cut

77
sub _columns {
78
79
  my $self = shift;

80
81
82
83
84
85
86
87
88
89
90
  return qw( p.prediction_transcript_id
       p.contig_id
       p.contig_start
       p.contig_end
       p.contig_strand
       p.start_phase
       p.exon_rank
       p.score
       p.p_value	
       p.analysis_id
       p.exon_count);
91
92
}

93

94

95
=head2 _final_clause
96

97
98
99
100
101
  Arg [1]    : none
  Example    : none
  Description: Overrides superclass method to provide an additional table
               joining coinstraint before the SQL query is performed.
  Returntype : string
102
  Exceptions : none
103
  Caller     : generic_fetch
104
105
106

=cut

107
108
109
110
sub _final_clause {
  my $self = shift;
 
  return  'order by p.prediction_transcript_id, p.exon_rank';
111
112
}

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
=head2 fetch_by_stable_id

Arg [1]    : string $stable_id
             The stable id of the transcript to retrieve
Example    : $trans = $trans_adptr->fetch_by_stable_id('3.10.190');
Description: Retrieves a prediction transcript via its stable id.  Note that
             the stable id is not actually stored in the database and is 
             calculated upon retrieval as the contig.start.end of the 
             prediction transcript
Returntype : Bio::EnsEMBL::PredictionTranscript
Caller     : general

=cut

sub fetch_by_stable_id {
  my $self = shift;
  my $id = shift;

  $self->throw('ID argument is required') if(!$id);

  #strip the start/end off the end of the id to get the contig name
  my @components = split(/\./, $id);
  pop(@components);
  pop(@components);

  my $contig_name = join('.', @components);
  my $contig = $self->db->get_RawContigAdaptor->fetch_by_name($contig_name);

  $self->throw("unknown contig [$contig_name] in identifier [$id]") 
    if(!$contig);
  
  #
  # Retrieve all the prediction transcripts on the contig it is known to be on.
  # Return the one found with the matching 'id'
  #
  foreach my $pt (@{$self->fetch_all_by_RawContig($contig)}) {
    return $pt if($id eq $pt->stable_id);
  }

  #prediction transcript does not exist
  $self->throw("No prediction transcipt exists with stable id [$id]");
}
155

156

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
=head2 _objs_from_sth

  Arg [1]    : DBI:st $sth 
               An executed DBI statement handle
  Arg [2]    : (optional) Bio::EnsEMBL::Mapper $mapper 
               An mapper to be used to convert contig coordinates
               to assembly coordinates.
  Arg [3]    : (optional) Bio::EnsEMBL::Slice $slice
               A slice to map the prediction transcript to.   
  Example    : $p_transcripts = $self->_objs_from_sth($sth);
  Description: Creates a list of Prediction transcripts from an executed DBI
               statement handle.  The columns retrieved via the statement 
               handle must be in the same order as the columns defined by the
               _columns method.  If the slice argument is provided then the
               the prediction transcripts will be in returned in the coordinate
               system of the $slice argument.  Otherwise the prediction 
               transcripts will be returned in the RawContig coordinate system.
  Returntype : reference to a list of Bio::EnsEMBL::PredictionTranscripts
  Exceptions : none
  Caller     : superclass generic_fetch
177

178
=cut
179

180
181
sub _objs_from_sth {
  my ($self, $sth, $mapper, $slice) = @_;
182
  
183
  my @out = ();
184
  
185
186
187
188
189
190
191
192
  my ($prediction_transcript_id, 
      $contig_id, $contig_start, $contig_end, $contig_strand,
      $start_phase, $exon_rank, $score, $p_value, $analysis_id,
      $exon_count );

  $sth->bind_columns(\$prediction_transcript_id, 
		    \$contig_id, \$contig_start, \$contig_end, \$contig_strand,
		    \$start_phase, \$exon_rank, \$score, \$p_value, 
193
		    \$analysis_id,\$exon_count);
194
195
196

  my $rca = $self->db->get_RawContigAdaptor;
  my $aa  = $self->db->get_AnalysisAdaptor;
197
  
198
199
200
  my ($analysis, $contig, $pre_trans, $ptid, $on_slice_flag, $last_end,
      $chr, $start, $end, $strand, 
      $slice_start, $slice_end, $slice_strand,
201
      $exon, $exon_start, $exon_end, $exon_strand,
202
203
      $stable_start, $stable_end, $stable_ctg,
      $transcript_slice_start, $transcript_slice_end );
204
205
206
207
208
209
  my (%analysis_hash, %contig_hash);

  if($slice) {
    $slice_start  = $slice->chr_start;
    $slice_end    = $slice->chr_end;
    $slice_strand = $slice->strand;
210
211
  }

212
  $on_slice_flag = 0;
213

214
215
  my $prev_exon;
  my $already_merged;
216
  
217
218
219
220
  while($sth->fetch) {
    #create a new transcript for each new prediction transcript id
    unless(defined $pre_trans && $ptid == $prediction_transcript_id) {
      $pre_trans = Bio::EnsEMBL::PredictionTranscript->new;
221

222
223
224
225
226
227
      $ptid = $prediction_transcript_id;
      $pre_trans->dbID($ptid);
      
      unless($analysis = $analysis_hash{$analysis_id}) {
	$analysis = $aa->fetch_by_dbID($analysis_id);
	$analysis_hash{$analysis_id} = $analysis;
228
      }
229
230
231
232
      
      $pre_trans->analysis($analysis);
      $pre_trans->set_exon_count($exon_count);
  
233
234
235
      $prev_exon = undef;
      $already_merged = 0;

236
      if(@out) {
237
238
239
	#throw away last pt if no exons or introns were on the slice
	if($slice && ( $transcript_slice_end < 1 || 
		       $transcript_slice_start > $slice->length() )) {
240
241
242
243
244
	  pop @out;
	} else {
	  #set the stable_id of the previous prediction
	  $out[$#out]->stable_id("$stable_ctg.$stable_start.$stable_end");
	}
245
      }
246
247
      
      push( @out, $pre_trans );
248

249
250
251
252
      #reset values used for last predtrans
      $stable_start = -1;
      $stable_end   = -1;
      $stable_ctg = '';
253
254
255

      $transcript_slice_end = undef;
      $transcript_slice_start = undef;
256
    }
257

258
259
260
261
262
263
264
265
266
267
268
269
270
    #recalculate stable id values
    if($stable_start == -1 || $contig_start < $stable_start) {
      $stable_start = $contig_start;
    }
    if($contig_end > $stable_end) {
      $stable_end = $contig_end;
    }
    unless($contig = $contig_hash{$contig_id}) {
      $contig = $rca->fetch_by_dbID($contig_id);
      $contig_hash{$contig_id} = $contig;
    }
    $stable_ctg = $contig->name;

271
272
    if($slice) {
      #a slice was passed in so we want slice coords
273

274
275
276
277
278
279
280
281
282
283
284
285
286
287
      #convert contig coords to assembly coords
      ($chr, $start, $end, $strand) = 
	$mapper->fast_to_assembly($contig_id, $contig_start,
				  $contig_end, $contig_strand);
      
      #if mapped to gap skip
      next unless(defined $start);

      
      #convert to slice coordinates
      if($slice_strand == -1) {
	$exon_start  = $slice_end - $end   + 1;
	$exon_end    = $slice_end - $start + 1;
	$exon_strand = $strand * -1;
288
289
290
291
292
293
294

	#merge adjacent exons into a single exon
	if($prev_exon && $prev_exon->start == $exon->end + 1) {
	  $exon->end($prev_exon->end);
	  $already_merged++;
	}

295
296
      } else {
	$exon_start  = $start - $slice_start + 1;
297
	$exon_end    = $end   - $slice_start   + 1;
298
	$exon_strand = $strand;
299
300
301
302
303
304

	#merge adjacent exons into a single exon
	if($prev_exon && $exon->start == $prev_exon->end +1) {
	  $exon->start($prev_exon->start);
	  $already_merged++;
	}
305
      }   
306
307
308
309
310

      if( !defined $transcript_slice_start || 
	  $transcript_slice_start > $exon_start ) {
	$transcript_slice_start = $exon_start;
      }
311
      
312
313
314
315
      if( ! defined $transcript_slice_end ||
	  $transcript_slice_end < $exon_end ) {
	$transcript_slice_end = $exon_end;
      }
316
      #use slice as the contig instead of the raw contig
317
318
319
320
321
322
      $contig = $slice;
    } else {
      #we just want plain old contig coords
      $exon_start =  $contig_start;
      $exon_end   =  $contig_end;
      $exon_strand = $contig_strand;
323
324
    }

325
326
327
328
329
330
331
332
333
334
    #create an exon and add it to the prediction transcript
    $exon = Bio::EnsEMBL::Exon->new_fast($contig, 
					 $exon_start, 
					 $exon_end,
					 $exon_strand);
    $exon->phase( $start_phase );
    $exon->end_phase( ($exon_end - $exon_start + 1 + $start_phase) % 3 );
    $exon->score( $score );
    $exon->p_value( $p_value );

335
336
    $prev_exon = $exon;
    $pre_trans->add_Exon($exon, $exon_rank - $already_merged);
337
  }
338
  
339
  #throw away last  pred_transcript if it had no exons overlapping the slice
340
341
342
343
344
345
346
347
  if(@out) {
    if($slice && ( $transcript_slice_end < 1 || 
		   $transcript_slice_start > $slice->length() )) {
      pop @out;
    } else {
      #set the stable id of the last prediction transcript
      $out[$#out]->stable_id("$stable_ctg.$stable_start.$stable_end");
    }
348
  }
349

350
  return \@out;
351
352
353
354
355
356
}



=head2 store

357
358
359
360
361
  Arg [1]    : list of Bio::EnsEMBL::PredictionTranscript @pre_transcripts 
  Example    : $prediction_transcript_adaptor->store(@pre_transcripts);
  Description: Stores a list of given prediction transcripts in database. 
               Puts dbID and Adaptor into each object stored object.
  Returntype : none
362
363
  Exceptions : on wrong argument type 
  Caller     : general 
364
365
366
367

=cut

sub store {
368
  my ( $self, @pre_transcripts ) = @_;
369
370

  my $exon_sql = q{
371
372
373
374
375
376
      INSERT INTO prediction_transcript ( prediction_transcript_id, exon_rank, 
					  contig_id, contig_start, contig_end, 
					  contig_strand, start_phase, score, 
					  p_value, analysis_id, exon_count )
	VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
      };
377

378
379
  my $exonst = $self->prepare($exon_sql);

380
381
382
383
384
  foreach my $pre_trans (@pre_transcripts) {
    if( ! $pre_trans->isa('Bio::EnsEMBL::PredictionTranscript') ) {
      $self->throw("$pre_trans is not a EnsEMBL PredictionTranscript " 
		   . "- not dumping!");
    }
385
    
386
387
388
389
390
    if( $pre_trans->dbID && $pre_trans->adaptor == $self ) {
      $self->warn("Already stored");
    }
        
    my $exonId = undef;    
391
    my @pt_exons = @{$pre_trans->get_all_Exons()};
392
393
    my $dbID = undef;
    my $rank = 1;
394
    
395
396
397
398
399
400
401
402
403
404
405
406
407
408
    my @exons;
    foreach my $e (@pt_exons) {
      if($e && (!$e->contig || $e->contig->isa('Bio::EnsEMBL::Slice'))) {
	$self->throw('PredictionTranscript must be in contig coords to store');
      }
      
      if($e && $e->isa('Bio::EnsEMBL::StickyExon')) {
	push @exons, @{$e->get_all_component_Exons};
      } else {
	push @exons, $e;
      }
    }

    for my $exon ( @exons ) {
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
      if( ! defined $exon ) { $rank++; next; }
      
      my $contig_id = $exon->contig->dbID();
      my $contig_start = $exon->start();
      my $contig_end = $exon->end();
      my $contig_strand = $exon->strand();
      my $start_phase = $exon->phase();
      my $end_phase = $exon->end_phase();
      my $score = $exon->score();
      my $p_value = $exon->p_value();
      my $analysis = $pre_trans->analysis->dbID;
      
      if( $rank == 1 ) {
	$exonst->execute( undef, 1, $contig_id, $contig_start, 
			  $contig_end, $contig_strand,
			  $start_phase, $score, $p_value, $analysis, 
Arne Stabenau's avatar
Arne Stabenau committed
425
			  scalar( @exons ));
426
427
428
429
430
	$dbID = $exonst->{'mysql_insertid'};
      } else {
	$exonst->execute( $dbID, $rank, $contig_id, $contig_start, 
			  $contig_end, $contig_strand,
			  $start_phase, $score, $p_value, $analysis, 
Arne Stabenau's avatar
Arne Stabenau committed
431
			  scalar( @exons ) );
432
433
      }
      $rank++;
434
    }
435
436
437
    
    $pre_trans->dbID( $dbID );
    $pre_trans->adaptor( $self );
438
439
  }

440
  $exonst->finish;
441
442
443
}


444
445
446

=head2 remove

447
448
449
450
451
452
  Arg [1]    : Bio::EnsEMBL::PredictionTranscript $pt 
  Example    : $prediction_transcript_adaptor->remove($pt);
  Description: removes given prediction transcript $pt from database. 
  Returntype : none
  Exceptions : none 
  Caller     : general
453
454
455

=cut

456
457
458
459
460
461
462
463
sub remove {
  my $self = shift;
  my $pre_trans = shift;
  
  if ( ! defined $pre_trans->dbID() ) {
    return;
  }

464
465
  my $sth = $self->prepare( "DELETE FROM prediction_transcript 
                             WHERE prediction_transcript_id = ?" );
466
467
468
469
  $sth->execute( $pre_trans->dbID );

  # uhh, didnt know another way of resetting to undef ...
  $pre_trans->{dbID} = undef;
470
  $pre_trans->{adaptor} = undef;
471
472
473
}


Glenn Proctor's avatar
Glenn Proctor committed
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
=head2 list_dbIDs

  Arg [1]    : none
  Example    : @feature_ids = @{$prediction_transcript_adaptor->list_dbIDs()};
  Description: Gets an array of internal ids for all prediction transcript features in the current db
  Returntype : list of ints
  Exceptions : none
  Caller     : ?

=cut

sub list_dbIDs {
   my ($self) = @_;

   return $self->_list_dbIDs("prediction_transcript");
}
490

491
1;