SliceAdaptor.pm 11.5 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 79 80 81 82 83 84 85 86 87
    my ($self,$chr,$start,$end) = @_;

    if( !defined $end ) {   # Why defined?  Is '0' a valid end?
        $self->throw("must provide chr, start and end");
    }

    if( $start > $end ) {
        $self->throw("start must be less than end: parameters $chr:$start:$end");
    }

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

Graham McVicker's avatar
Graham McVicker committed
90
    $slice = Bio::EnsEMBL::Slice->new(
91 92 93 94
          -chr_name      => $chr,
          -chr_start     => $start,
          -chr_end       => $end,
          -assembly_type => $type,
Graham McVicker's avatar
Graham McVicker committed
95 96
          -adaptor       => $self->db->get_SliceAdaptor()
	 );
97 98 99 100 101 102

    return $slice;
}



103
=head2 fetch_by_contig_name
104

105 106
 Title   : fetch_by_contig_name
 Usage   : $slice = $slice_adaptor->fetch_by_contig_name('AC000012.00001',1000);
107 108 109 110 111 112 113
 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

114
sub fetch_by_contig_name {
115 116 117 118
   my ($self,$contigid,$size) = @_;

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

119
   my ($chr_name,$start,$end) = $self->_get_chr_start_end_of_contig($contigid);
120

121 122 123
   return $self->fetch_by_chr_start_end($chr_name,
					$start-$size,
					$end+$size);
Graham McVicker's avatar
Graham McVicker committed
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 155 156 157 158 159 160 161
 }


=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("
        SELECT chromosome_id, superctg_ori, MIN(chr_start), MAX(chr_end)
        FROM assembly
        WHERE superctg_name = '$fpc_name'
        AND type = '$type'
        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;
162 163 164 165
}



Graham McVicker's avatar
Graham McVicker committed
166 167 168 169
=head2 fetch_by_clone_accession

 Title   : fetch_by_clone_accession
 Usage   : $slice = $slice_adaptor->fetch_by_clone_accession('AC000012',1000);
170 171 172 173 174 175 176
 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
177
sub fetch_by_clone_accession{
178 179 180
   my ($self,$clone,$size) = @_;

   if( !defined $clone ) {
Graham McVicker's avatar
Graham McVicker committed
181
     $self->throw("Must have clone to fetch Slice of clone");
182 183 184
   }
   if( !defined $size ) {$size=0;}

185
   my $type = $self->db->assembly_type()
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    or $self->throw("No assembly type defined");

   my $sth = $self->db->prepare("SELECT  c.name,
                        a.chr_start,
                        a.chr_end,
                        a.chromosome_id 
                    FROM    assembly a, 
                        contig c, 
                        clone  cl
                    WHERE c.clone_id = cl.clone_id
                    AND cl.name = '$clone'  
                    AND c.contig_id = a.contig_id 
                    AND a.type = '$type' 
                    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");
   }
     
Graham McVicker's avatar
Graham McVicker committed
216 217 218
   my $slice = $self->fetch_by_chr_start_end( $chr_name,
					      $first_start-$size,
					      $end+$size );
219 220 221 222 223
   return $slice;
}



Graham McVicker's avatar
Graham McVicker committed
224
=head2 fetch_by_transcript_stable_id
225

Graham McVicker's avatar
Graham McVicker committed
226 227
 Title   : fetch_by_transcript_stable_id
 Usage   : $slice = $slice_adaptor->fetch_by_transcript_stable_id(
228 229 230 231 232 233 234 235 236 237 238
                                       '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
239
sub fetch_by_transcript_stable_id{
240 241 242 243 244 245
  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
246 247
  
  return $self->fetch_by_transcript_id($dbID, $size);
248 249
}

250

Graham McVicker's avatar
Graham McVicker committed
251 252 253 254
=head2 fetch_by_transcript_id

 Title   : fetch_by_transcript_id
 Usage   : $slice = $slice_adaptor->fetch_by_transcript_id(24,1000);
255 256 257 258 259 260 261 262 263
 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
264
sub fetch_by_transcript_id {
265 266 267 268 269 270 271 272 273 274
  my ($self,$transcriptid,$size) = @_;
     if( !defined $transcriptid ) {
       $self->throw("Must have transcriptid id to fetch Slice of transcript");
   }
   if( !defined $size ) {$size=0;}
   my $emptyslice = Bio::EnsEMBL::Slice->new( '-empty'   => 1,
                                              '-adaptor' => $self,
					      '-ASSEMBLY_TYPE' =>
					      $self->db->assembly_type);
   my $ta = $self->db->get_TranscriptAdaptor;
275
   my $transcript_obj = $ta->fetch_by_dbID($transcriptid);
276 277 278 279 280 281 282

   my %exon_transforms;
   for my $exon ( $transcript_obj->get_all_Exons() ) {
     my $newExon = $exon->transform( $emptyslice );
     $exon_transforms{ $exon } = $newExon;
   }
   $transcript_obj->transform( \%exon_transforms );
Graham McVicker's avatar
Graham McVicker committed
283 284 285 286
   my $slice = $self->fetch_by_chr_start_end(
					     $emptyslice->chr_name,
					     $transcript_obj->start-$size,
					     $transcript_obj->end+$size);
287 288 289
   return $slice;
}

290

Graham McVicker's avatar
Graham McVicker committed
291
=head2 fetch_by_gene_stable_id
292

Graham McVicker's avatar
Graham McVicker committed
293 294
 Title   : fetch_by_gene_stable_id
 Usage   : $slice = $slice_adaptor->fetch_by_gene_stable_id('ENSG00000012123',1000);
295 296 297 298 299 300 301
 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
302
sub fetch_by_gene_stable_id{
303 304 305 306 307 308 309
   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
310
   my ($chr_name,$start,$end) = $self->_get_chr_start_end_of_gene($geneid);
311 312

   if( !defined $start ) {
Graham McVicker's avatar
Graham McVicker committed
313 314 315
     my $type = $self->adaptor->db->assembly_type()
       or $self->throw("No assembly type defined");
     $self->throw("Gene is not on the golden path '$type'. Cannot build Slice.");
316 317
   }
     
Graham McVicker's avatar
Graham McVicker committed
318
   return $self->fetch_by_chr_start_end($chr_name, $start-$size, $end+$size);
319 320 321
}


Graham McVicker's avatar
Graham McVicker committed
322
=head2 fetch_by_chr_name
323

Graham McVicker's avatar
Graham McVicker committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
 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();
David Barnwell's avatar
David Barnwell committed
344
   my $chromosome = $ca->fetch_by_chrname($chr_name);
Graham McVicker's avatar
Graham McVicker committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
   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) = @_;
365

Graham McVicker's avatar
Graham McVicker committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
   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,
                        a.chromosome_id 
                    FROM assembly a, contig c 
                    WHERE c.name = '$contigid' 
                    AND c.contig_id = a.contig_id 
                    AND a.type = '$type'"
                    );
   $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) =  @_;
406
  
Graham McVicker's avatar
Graham McVicker committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  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)),
     a.chromosome_id
  
                    FROM    exon e,
                        transcript tr,
                        exon_transcript et,
                        assembly a,
                        gene_stable_id gsi
                    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
                    AND gsi.stable_id = '$geneid';" 
                    );
   $sth->execute();
430

Graham McVicker's avatar
Graham McVicker committed
431 432 433 434 435 436 437 438 439
   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;
440

Graham McVicker's avatar
Graham McVicker committed
441 442
   $start=shift @start_sorted;
   $end=pop @start_sorted;
443

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