SliceAdaptor.pm 12.2 KB
Newer Older
1 2

#
Simon Potter's avatar
pod  
Simon Potter committed
3
# Ensembl module for Bio::EnsEMBL::DBSQL::SliceAdaptor
4 5 6 7 8 9 10 11 12 13 14
#
# Cared for by Ewan Birney <ensembl-dev@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

15
Bio::EnsEMBL::DBSQL::SliceAdaptor - Adaptors for slices
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

=head1 SYNOPSIS
  



=head1 DESCRIPTION

Factory for getting out slices of assemblies. WebSlice is the highly
accelerated version for the web site.

=head1 AUTHOR - Ewan Birney

This modules is part of the Ensembl project http://www.ensembl.org

Email ensembl-dev@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...


46
package Bio::EnsEMBL::DBSQL::SliceAdaptor;
47 48 49
use vars qw(@ISA);
use strict;

50 51

# Object preamble - inherits from Bio::EnsEMBL::Root
52 53
use Bio::EnsEMBL::DBSQL::BaseAdaptor;
use Bio::EnsEMBL::Slice;
54
use Bio::EnsEMBL::DBSQL::DBAdaptor;
55

56

57
@ISA = ('Bio::EnsEMBL::DBSQL::BaseAdaptor');
58 59


Graham McVicker's avatar
Graham McVicker committed
60
# new is inherited from BaseAdaptor
61

62

Graham McVicker's avatar
Graham McVicker committed
63
=head2 fetch_by_chr_start_end
64

Graham McVicker's avatar
Graham McVicker committed
65
 Title   : fetch_by_chr_start_end
66 67 68 69 70 71 72 73 74 75
 Usage   :
 Function: create a Slice based on a segment of a chromosome and
           start/end
 Example :
 Returns : A Slice
 Args    : chromosome, start, end (in Chromosome coordinates)


=cut

Graham McVicker's avatar
Graham McVicker committed
76
sub fetch_by_chr_start_end {
77 78
    my ($self,$chr,$start,$end) = @_;

79 80
    unless($chr) {
      $self->throw("chromosome name argument must be defined and not ''");
81 82
    }

83 84 85 86 87 88 89 90 91
    unless(defined $end) {   # Why defined?  Is '0' a valid end?
      $self->throw("end argument must be defined\n");
    }

    unless(defined $start) {
      $self->throw("start argument must be defined\n");
    }

    if($start > $end) {
92
      $self->throw("start must be less than end: parameters $chr:$start:$end");
93
    }
94
    
95
    my $slice;
96
    my $type = $self->db->assembly_type();
97

Graham McVicker's avatar
Graham McVicker committed
98
    $slice = Bio::EnsEMBL::Slice->new(
99 100 101 102
          -chr_name      => $chr,
          -chr_start     => $start,
          -chr_end       => $end,
          -assembly_type => $type,
Graham McVicker's avatar
Graham McVicker committed
103 104
          -adaptor       => $self->db->get_SliceAdaptor()
	 );
105 106 107 108 109 110

    return $slice;
}



111
=head2 fetch_by_contig_name
112

113 114
 Title   : fetch_by_contig_name
 Usage   : $slice = $slice_adaptor->fetch_by_contig_name('AC000012.00001',1000);
115 116 117 118 119 120 121
 Function: Creates a slice of the specified slice adaptor object.  If a context size is given, the slice is extended by that number of basepairs on either side of the contig.  Throws if the contig is not golden.
 Returns : Slice object 
 Args    : contig id, [context size in bp]


=cut

122
sub fetch_by_contig_name {
123 124 125 126
   my ($self,$contigid,$size) = @_;

   if( !defined $size ) {$size=0;}

127
   my ($chr_name,$start,$end) = $self->_get_chr_start_end_of_contig($contigid);
128

129 130 131 132 133 134 135 136
   $start -= $size;
   $end += $size;

   if($start < 1) {
     $start  = 1;
   }

   return $self->fetch_by_chr_start_end($chr_name, $start, $end);
Graham McVicker's avatar
Graham McVicker committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
 }


=head2 fetch_by_fpc_name

 Title   : fetch_by_fpc_name
 Usage   :
 Function: create a Slice representing a complete FPC contig
 Example :
 Returns : 
 Args    : the FPC contig id.


=cut

sub fetch_by_fpc_name {
    my ($self,$fpc_name) = @_;

    my $type = $self->db->assembly_type();

    my $sth = $self->db->prepare("
158 159
        SELECT chr.name, a.superctg_ori, MIN(a.chr_start), MAX(a.chr_end)
        FROM assembly a, chromosome chr
Graham McVicker's avatar
Graham McVicker committed
160 161
        WHERE superctg_name = '$fpc_name'
        AND type = '$type'
162
        AND chr.chromosome_id = a.chromosome_id
Graham McVicker's avatar
Graham McVicker committed
163 164 165 166 167 168 169 170 171 172 173 174 175
        GROUP by superctg_name
        ");

    $sth->execute;

    my ($chr, $strand, $slice_start, $slice_end) = $sth->fetchrow_array;

    my $slice;

    $slice = new Bio::EnsEMBL::Slice($chr,$slice_start,$slice_end,
                                     $strand,$type);

    return $slice;
176 177 178 179
}



Graham McVicker's avatar
Graham McVicker committed
180 181 182 183
=head2 fetch_by_clone_accession

 Title   : fetch_by_clone_accession
 Usage   : $slice = $slice_adaptor->fetch_by_clone_accession('AC000012',1000);
184 185 186 187 188 189 190
 Function: Creates a Slice of the specified object.  If a context size is given, the Slice is extended by that number of basepairs on either side of the clone.  Throws if the clone is not golden.
 Returns : Slice object 
 Args    : clone id, [context size in bp]


=cut

Graham McVicker's avatar
Graham McVicker committed
191
sub fetch_by_clone_accession{
192 193 194
   my ($self,$clone,$size) = @_;

   if( !defined $clone ) {
Graham McVicker's avatar
Graham McVicker committed
195
     $self->throw("Must have clone to fetch Slice of clone");
196 197 198
   }
   if( !defined $size ) {$size=0;}

199
   my $type = $self->db->assembly_type()
200 201 202 203 204
    or $self->throw("No assembly type defined");

   my $sth = $self->db->prepare("SELECT  c.name,
                        a.chr_start,
                        a.chr_end,
205
                        chr.name 
206 207
                    FROM    assembly a, 
                        contig c, 
208 209
                        clone  cl,
                        chromosome chr
210 211 212
                    WHERE c.clone_id = cl.clone_id
                    AND cl.name = '$clone'  
                    AND c.contig_id = a.contig_id 
213 214
                    AND a.type = '$type'
                    AND chr.chromosome_id = a.chromosome_id
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                    ORDER BY a.chr_start"
                    );
   $sth->execute();
 
   my ($contig,$start,$end,$chr_name); 
   my $counter; 
   my $first_start;
   while ( my @row=$sth->fetchrow_array){
       $counter++;
       ($contig,$start,$end,$chr_name)=@row;
       if ($counter==1){$first_start=$start;}      
   }

   if( !defined $contig ) {
       $self->throw("Clone is not on the golden path. Cannot build Slice");
   }
     
232 233 234 235 236 237 238 239
   $first_start -= $size;
   $end += $size;

   if($first_start < 1) {
     $first_start = 1;
   }

   my $slice = $self->fetch_by_chr_start_end($chr_name, $first_start, $end);
240 241 242 243 244
   return $slice;
}



Graham McVicker's avatar
Graham McVicker committed
245
=head2 fetch_by_transcript_stable_id
246

Graham McVicker's avatar
Graham McVicker committed
247 248
 Title   : fetch_by_transcript_stable_id
 Usage   : $slice = $slice_adaptor->fetch_by_transcript_stable_id(
249 250 251 252 253 254 255 256 257 258 259
                                       'ENST00000302930',1000);
 Function: Creates a slice of the specified object.  If a context
           size is given, the slice is extended by that number of
	   basepairs on either side of the transcript.  Throws if
	   the transcript is not golden.
 Returns : Slice object 
 Args    : transcript stable ID, [context size in bp]


=cut

Graham McVicker's avatar
Graham McVicker committed
260
sub fetch_by_transcript_stable_id{
261 262 263 264 265 266
  my ($self,$transcriptid,$size) = @_;

  # Just get the dbID, then fetch slice by that
  my $ta = $self->db->get_TranscriptAdaptor;
  my $transcript_obj = $ta->fetch_by_stable_id($transcriptid);
  my $dbID = $transcript_obj->dbID;
Graham McVicker's avatar
Graham McVicker committed
267 268
  
  return $self->fetch_by_transcript_id($dbID, $size);
269 270
}

271

Graham McVicker's avatar
Graham McVicker committed
272 273 274 275
=head2 fetch_by_transcript_id

 Title   : fetch_by_transcript_id
 Usage   : $slice = $slice_adaptor->fetch_by_transcript_id(24,1000);
276 277 278 279 280 281 282 283 284
 Function: Creates a slice of the specified object.  If a context
           size is given, the slice is extended by that number of
	   basepairs on either side of the transcript.  Throws if
	   the transcript is not golden.
 Returns : Slice object 
 Args    : transcript dbID, [context size in bp]

=cut

Graham McVicker's avatar
Graham McVicker committed
285
sub fetch_by_transcript_id {
286 287 288 289 290
  my ($self,$transcriptid,$size) = @_;
     if( !defined $transcriptid ) {
       $self->throw("Must have transcriptid id to fetch Slice of transcript");
   }
   if( !defined $size ) {$size=0;}
291

292
   my $ta = $self->db->get_TranscriptAdaptor;
293
   my $transcript_obj = $ta->fetch_by_dbID($transcriptid);
294 295

   my %exon_transforms;
296 297

  my $emptyslice;
298
   for my $exon ( $transcript_obj->get_all_Exons() ) {
299 300 301 302
     $emptyslice = Bio::EnsEMBL::Slice->new( '-empty'   => 1,
						'-adaptor' => $self,
						'-ASSEMBLY_TYPE' =>
						$self->db->assembly_type);     
303 304 305 306
     my $newExon = $exon->transform( $emptyslice );
     $exon_transforms{ $exon } = $newExon;
   }
   $transcript_obj->transform( \%exon_transforms );
307 308 309 310 311 312 313

  my $start = $transcript_obj->start() - $size;
  my $end = $transcript_obj->end() + $size;

  if($start < 1) {
    $start = 1;
  }
314
  
315 316 317
  my $slice = $self->fetch_by_chr_start_end($emptyslice->chr_name,
					    $start, $end);
  return $slice;
318 319
}

320

Graham McVicker's avatar
Graham McVicker committed
321
=head2 fetch_by_gene_stable_id
322

Graham McVicker's avatar
Graham McVicker committed
323 324
 Title   : fetch_by_gene_stable_id
 Usage   : $slice = $slice_adaptor->fetch_by_gene_stable_id('ENSG00000012123',1000);
325 326 327 328 329 330 331
 Function: Creates a slice of the specified object.  If a context size is given, the slice is extended by that number of basepairs on either side of the gene.  Throws if the gene is not golden.
 Returns : Slice object 
 Args    : gene id, [context size in bp]


=cut

Graham McVicker's avatar
Graham McVicker committed
332
sub fetch_by_gene_stable_id{
333 334 335 336 337 338 339
   my ($self,$geneid,$size) = @_;

   if( !defined $geneid ) {
       $self->throw("Must have gene id to fetch Slice of gene");
   }
   if( !defined $size ) {$size=0;}

Graham McVicker's avatar
Graham McVicker committed
340
   my ($chr_name,$start,$end) = $self->_get_chr_start_end_of_gene($geneid);
341 342

   if( !defined $start ) {
343
     my $type = $self->db->assembly_type()
Graham McVicker's avatar
Graham McVicker committed
344 345
       or $self->throw("No assembly type defined");
     $self->throw("Gene is not on the golden path '$type'. Cannot build Slice.");
346 347
   }
     
348 349 350 351 352 353 354 355
   $start -= $size;
   $end += $size;
   
   if($start < 1) {
     $start = 1;
   }

   return $self->fetch_by_chr_start_end($chr_name, $start, $end);
356 357 358
}


Graham McVicker's avatar
Graham McVicker committed
359
=head2 fetch_by_chr_name
360

Graham McVicker's avatar
Graham McVicker committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
 Title   : fetch_by_chr_name
 Usage   : $slice = $slice_adaptor->fetch_by_chr_name('20');
 Function: Creates a slice of an entire chromosome 
 Returns : Slice object 
 Args    : chromosome name


=cut

sub fetch_by_chr_name{
   my ($self,$chr_name) = @_;

   unless( $chr_name ) {
       $self->throw("Chromosome name argument required");
   }

   my $chr_start = 1;
   
   #set the end of the slice to the end of the chromosome
   my $ca = $self->db()->get_ChromosomeAdaptor();
381
   my $chromosome = $ca->fetch_by_chr_name($chr_name);
Graham McVicker's avatar
Graham McVicker committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
   my $chr_end = $chromosome->length();

   return $self->fetch_by_chr_start_end($chr_name, $chr_start, $chr_end);
}



=head2 _get_chr_start_end_of_contig

 Title   : _get_chr_start_end_of_contig
 Usage   :
 Function: returns the chromosome name, absolute start and absolute end of the 
           specified contig
 Returns : returns chr,start,end
 Args    : contig id

=cut

sub _get_chr_start_end_of_contig {
    my ($self,$contigid) = @_;
402

Graham McVicker's avatar
Graham McVicker committed
403 404 405 406 407 408 409 410 411 412
   if( !defined $contigid ) {
       $self->throw("Must have contig id to fetch Slice of contig");
   }
   
   my $type = $self->db->assembly_type()
    or $self->throw("No assembly type defined");

   my $sth = $self->db->prepare("SELECT  c.name,
                        a.chr_start,
                        a.chr_end,
413 414
                        chr.name 
                    FROM assembly a, contig c, chromosome chr 
Graham McVicker's avatar
Graham McVicker committed
415 416
                    WHERE c.name = '$contigid' 
                    AND c.contig_id = a.contig_id 
417 418
                    AND a.type = '$type'
                    AND chr.chromosome_id = a.chromosome_id"
Graham McVicker's avatar
Graham McVicker committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
                    );
   $sth->execute();
   my ($contig,$start,$end,$chr_name) = $sth->fetchrow_array;

   if( !defined $contig ) {
     $self->throw("Contig $contigid is not on the golden path of type $type");
   }

   return ($chr_name,$start,$end);
}

=head2 _get_chr_start_end_of_gene

 Title   : get_Gene_chr_bp
 Usage   : 
 Function: 
 Returns :  
 Args    :


=cut


sub _get_chr_start_end_of_gene {
  my ($self,$geneid) =  @_;
444
  
Graham McVicker's avatar
Graham McVicker committed
445 446 447 448 449 450 451 452
  my $type = $self->db->assembly_type()
    or $self->throw("No assembly type defined");
  
  my $sth = $self->db->prepare("SELECT  
   if(a.contig_ori=1,(e.contig_start-a.contig_start+a.chr_start),
                    (a.chr_start+a.contig_end-e.contig_end)),
   if(a.contig_ori=1,(e.contig_end-a.contig_start+a.chr_start),
                    (a.chr_start+a.contig_end-e.contig_start)),
453
     chr.name
Graham McVicker's avatar
Graham McVicker committed
454 455 456 457 458
  
                    FROM    exon e,
                        transcript tr,
                        exon_transcript et,
                        assembly a,
459
                        gene_stable_id gsi,
460
                        chromosome chr
Graham McVicker's avatar
Graham McVicker committed
461 462 463 464 465
                    WHERE e.exon_id=et.exon_id 
                    AND et.transcript_id =tr.transcript_id 
                    AND a.contig_id=e.contig_id 
                    AND a.type = '$type' 
                    AND tr.gene_id = gsi.gene_id
466
                    AND gsi.stable_id = '$geneid'
467
                    AND a.chromosome_id = chr.chromosome_id" 
Graham McVicker's avatar
Graham McVicker committed
468 469
                    );
   $sth->execute();
470

Graham McVicker's avatar
Graham McVicker committed
471 472 473 474 475 476 477 478 479
   my ($start,$end,$chr);
   my @start;
   while ( my @row=$sth->fetchrow_array){
      ($start,$end,$chr)=@row;
       push @start,$start;
       push @start,$end;
   }   
   
   my @start_sorted=sort { $a <=> $b } @start;
480

Graham McVicker's avatar
Graham McVicker committed
481 482
   $start=shift @start_sorted;
   $end=pop @start_sorted;
483

Graham McVicker's avatar
Graham McVicker committed
484
   return ($chr,$start,$end);      
485
}