SliceAdaptor.pm 13.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
use Bio::EnsEMBL::Utils::Eprof qw(eprof_start eprof_end);
48 49 50
use vars qw(@ISA);
use strict;

51 52

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

57

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


61 62
# new is inherieted from BaseAdaptor

63 64 65 66 67 68 69 70 71 72 73 74 75
=head2 new_slice

 Title   : new_slice
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub new_slice{
76
    my ($self,$chr,$start,$end,$strand,$type) = @_;
77 78


79 80 81
    my $slice = Bio::EnsEMBL::Slice->new( -chr_name  => $chr,
					  -chr_start => $start,
					  -chr_end   => $end,
82
					  -strand    => $strand,
83 84 85 86
					  -assembly_type      => $type,
					-adaptor => $self);

    return $slice;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}


=head2 new_web_slice

 Title   : new_web_slice
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub new_web_slice{
103
    my ($self,$chr,$start,$end,$strand,$type) = @_;
104 105 106 107
    
    die "Not implemented new slice yet";
    
}
108 109 110 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 155 156


sub fetch_all_repeat_features{
  my($self, $slice, $logic_name) = @_;

  if(!$slice){
    $self->throw("can't fetch all repeat features if con't have a slice to fetch them for\n");
  }

  my @repeats = $self->db->get_RepeatFeatureAdaptor->fetch_by_Slice($slice, $logic_name);

  return @repeats;

}

sub fetch_all_simple_features{
  my($self, $slice, $logic_name) = @_;

  if(!$slice){
    $self->throw("can't fetch all simple features if con't have a slice to fetch them for\n");
  }

  my @simple = $self->db->get_SimpleFeatureAdaptor->fetch_by_Slice($slice, $logic_name);

  return @simple;

}




sub fetch_all_similarity_features{
  my($self, $slice, $logic_name) = @_;

  if(!$slice){
    $self->throw("can't fetch all simple features if con't have a slice to fetch them for\n");
  }
  
  my @out;

  my @dnaalign = $self->db->get_DnaAlignFeatureAdaptor->fetch_by_Slice($slice, $logic_name);
  my @pepalign = $self->db->get_ProteinAlignFeatureAdaptor->fetch_by_Slice($slice, $logic_name);

  push(@out, @dnaalign);
  push(@out, @pepalign);

  return @out;
}

157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
sub fetch_all_similarity_features_above_score{
  my($self, $slice, $score, $logic_name) = @_;

  if(!$slice){
    $self->throw("can't fetch all simple features if con't have a slice to fetch them for\n");
  }
  if(!$score){
    $self->throw("need score even if it 0\n");
  }
  my @out;

  my @dnaalign = $self->db->get_DnaAlignFeatureAdaptor->fetch_by_Slice_and_score($slice, $score, $logic_name);
  my @pepalign = $self->db->get_ProteinAlignFeatureAdaptor->fetch_by_Slice_and_score($slice, $score, $logic_name);

  push(@out, @dnaalign);
  push(@out, @pepalign);

  return @out;
}


sub fetch_all_similarity_features_above_pid{
  my($self, $slice, $pid, $logic_name) = @_;

  if(!$slice){
    $self->throw("can't fetch all simple features if con't have a slice to fetch them for\n");
  }
  if(!$pid){
    $self->throw("need percent_id even if it 0\n");
  }
  my @out;

  my @dnaalign = $self->db->get_DnaAlignFeatureAdaptor->fetch_by_Slice_and_pid($slice, $pid, $logic_name);
  my @pepalign = $self->db->get_ProteinAlignFeatureAdaptor->fetch_by_Slice_and_pid($slice, $pid, $logic_name);

  push(@out, @dnaalign);
  push(@out, @pepalign);

  return @out;
}
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218



=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) = @_;

   if( !defined $contigid ) {
       $self->throw("Must have contig id to fetch Slice of contig");
   }
   
219
   my $type = $self->db->assembly_type()
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    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 fetch_Slice_by_chr_start_end

 Title   : fetch_Slice_by_chr_start_end
 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

sub fetch_Slice_by_chr_start_end {
    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;

    &eprof_start('Slice: staticcontig build');

270
    my $type = $self->db->assembly_type();
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

    eval {
      $slice = Bio::EnsEMBL::Slice->new(
          -chr_name      => $chr,
          -chr_start     => $start,
          -chr_end       => $end,
          -assembly_type => $type,
          -adaptor       => $self->db->get_SliceAdaptor
      );
    } ;
    if( $@ ) {
      $self->throw("Unable to build a slice for $chr, $start,$end\n\nUnderlying exception $@\n");
    }
    &eprof_end('Slice: staticcontig build');

    return $slice;
}



=head2 fetch_Slice_by_contig

 Title   : fetch_Slice_by_contig
 Usage   : $slice = $slice_adaptor->fetch_Slice_by_contig('AC000012.00001',1000);
 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

sub fetch_Slice_by_contig{
   my ($self,$contigid,$size) = @_;

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

   my ($chr_name,$start,$end) = $self->get_chr_start_end_of_contig($contigid); 

   return $self->fetch_Slice_by_chr_start_end(  $chr_name,
                            $start-$size,
                            $end+$size
                            );
  
}


=head2 fetch_Slice_by_clone

 Title   : fetch_Slice_by_clone
 Usage   : $slice = $slice_adaptor->fetch_Slice_by_clone('AC000012',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 clone.  Throws if the clone is not golden.
 Returns : Slice object 
 Args    : clone id, [context size in bp]


=cut

sub fetch_Slice_by_clone{
   my ($self,$clone,$size) = @_;

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

336
   my $type = $self->db->assembly_type()
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 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
    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");
   }
     
   my $slice = $self->fetch_Slice_by_chr_start_end(    $chr_name,
                            $first_start-$size,
                            $end+$size
                            );
   $slice->adaptor->db($self->db);
   return $slice;

}


=head2 get_Gene_chr_bp

 Title   : get_Gene_chr_bp
 Usage   : 
 Function: 
 Returns :  
 Args    :


=cut


sub get_Gene_chr_bp {
    my ($self,$geneid) =  @_;
   
392
   my $type = $self->db->assembly_type()
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    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();

   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;

   $start=shift @start_sorted;
   $end=pop @start_sorted;

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

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
=head2 fetch_Slice_by_transcript_dbID

 Title   : fetch_Slice_by_transcript_dbID
 Usage   : $slice = $slice_adaptor->fetch_Slice_by_transcript_dbID(
                                       24,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 dbID, [context size in bp]


=cut

sub fetch_Slice_by_transcript_dbID{
  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;
   my $transcript_obj;
   $transcript_obj = $ta->fetch_by_dbID($transcriptid);

   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 );
   my $slice = $self->fetch_Slice_by_chr_start_end(
                                            $emptyslice->chr_name,
                                            $transcript_obj->start-$size,
					    $transcript_obj->end+$size);
   return $slice;

}

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

=head2 fetch_Slice_by_gene

 Title   : fetch_Slice_by_gene
 Usage   : $slice = $slice_adaptor->fetch_Slice_by_gene('ENSG00000012123',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 gene.  Throws if the gene is not golden.
 Returns : Slice object 
 Args    : gene id, [context size in bp]


=cut

sub fetch_Slice_by_gene{
   my ($self,$geneid,$size) = @_;

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

   my ($chr_name,$start,$end) = $self->get_Gene_chr_bp($geneid);

   if( !defined $start ) {
499
       my $type = $self->adaptor->db->assembly_type()
500 501 502 503 504 505 506 507 508 509 510
        or $self->throw("No assembly type defined");
       $self->throw("Gene is not on the golden path '$type'. Cannot build Slice.");
   }
     
   return $self->fetch_Slice_by_chr_start_end(  $chr_name,
                            $start-$size,
                            $end+$size
                            );
}


511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

#
# Deprecated, Just use the slice method get_all_PredictionTranscripts instead
#
sub fetch_all_prediction_transcripts{
  my($self, $slice, $logic_name) = @_;

  $self->throw("SliceAdaptor->fetch_all_predication_transcripts deprecated");
  
  return ();

#  if(!$slice){
#    $self->throw("can't fetch all simple features if con't have a slice to fetch them for\n");
#  }

#  my @prediction = $self->db->get_PredictionTranscriptAdaptor->fetch_by_Slice($slice, $logic_name);

#  return @prediction;
}