| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::CircularSlice - Arbitary Slice of a genome | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | $sa = $db->get_SliceAdaptor; | ||
| 38 | |||
| 39 | $slice = | ||
| 40 | $sa->fetch_by_region( 'chromosome', 'X', 1_000_000, 2_000_000 ); | ||
| 41 | |||
| 42 | # get some attributes of the slice | ||
| 43 | my $seqname = $slice->seq_region_name(); | ||
| 44 | my $start = $slice->start(); | ||
| 45 | my $end = $slice->end(); | ||
| 46 | |||
| 47 | # get the sequence from the slice | ||
| 48 | my $seq = $slice->seq(); | ||
| 49 | |||
| 50 | # get some features from the slice | ||
| 51 | foreach $gene ( @{ $slice->get_all_Genes } ) { | ||
| 52 | # do something with a gene | ||
| 53 | } | ||
| 54 | |||
| 55 | foreach my $feature ( @{ $slice->get_all_DnaAlignFeatures } ) { | ||
| 56 | # do something with dna-dna alignments | ||
| 57 | } | ||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | A Slice object represents a region of a genome. It can be used to | ||
| 62 | retrieve sequence or features from an area of interest. | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | package Bio::EnsEMBL::CircularSlice; | ||
| 69 | $Bio::EnsEMBL::CircularSlice::VERSION = '114.0.0_50'; # TRIAL | ||
| 70 | $Bio::EnsEMBL::CircularSlice::VERSION = '114.0.050'; | ||
| 71 | use vars qw(@ISA); | ||
| 72 | use strict; | ||
| 73 | |||
| 74 | use Bio::PrimarySeqI; | ||
| 75 | |||
| 76 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 77 | use Bio::EnsEMBL::Utils::Exception | ||
| 78 | qw(throw warning); | ||
| 79 | use Bio::EnsEMBL::RepeatMaskedSlice; | ||
| 80 | use Bio::EnsEMBL::Utils::Sequence qw(reverse_comp); | ||
| 81 | use Bio::EnsEMBL::Utils::Scalar qw( assert_ref ); | ||
| 82 | use Bio::EnsEMBL::ProjectionSegment; | ||
| 83 | use Bio::EnsEMBL::Registry; | ||
| 84 | use Bio::EnsEMBL::DBSQL::MergedAdaptor; | ||
| 85 | |||
| 86 | #use Bio::EnsEMBL::IndividualSlice; | ||
| 87 | #use Bio::EnsEMBL::IndividualSliceFactory; | ||
| 88 | use Bio::EnsEMBL::Mapper::RangeRegistry; | ||
| 89 | use Bio::EnsEMBL::Slice; | ||
| 90 | use Data::Dumper; | ||
| 91 | use Scalar::Util qw(weaken isweak); | ||
| 92 | |||
| 93 | my $reg = "Bio::EnsEMBL::Registry"; | ||
| 94 | |||
| 95 | @ISA = qw(Bio::EnsEMBL::Slice); | ||
| 96 | |||
| 97 | =head2 new | ||
| 98 | |||
| 99 | Arg [...] : List of named arguments | ||
| 100 | Bio::EnsEMBL::CoordSystem COORD_SYSTEM | ||
| 101 | string SEQ_REGION_NAME, | ||
| 102 | int START, | ||
| 103 | int END, | ||
| 104 | int SEQ_REGION_LENGTH, (optional) | ||
| 105 | string SEQ (optional) | ||
| 106 | int STRAND, (optional, defaults to 1) | ||
| 107 | Bio::EnsEMBL::DBSQL::SliceAdaptor ADAPTOR (optional) | ||
| 108 | Example : | ||
| 109 | |||
| 110 | $slice = | ||
| 111 | Bio::EnsEMBL::CircularSlice->new( -coord_system => $cs, | ||
| 112 | -start => 1, | ||
| 113 | -end => 10000, | ||
| 114 | -strand => 1, | ||
| 115 | -seq_region_name => 'X', | ||
| 116 | -seq_region_length => 12e6, | ||
| 117 | -adaptor => $slice_adaptor ); | ||
| 118 | |||
| 119 | Description: Creates a new slice object. A slice represents a | ||
| 120 | region of sequence in a particular coordinate system. | ||
| 121 | Slices can be used to retrieve sequence and features | ||
| 122 | from an area of interest in a genome. | ||
| 123 | |||
| 124 | Coordinates start at 1 and are inclusive. Negative | ||
| 125 | coordinates or coordinates exceeding the length of | ||
| 126 | the seq_region are permitted. Start must be less | ||
| 127 | than or equal. to end regardless of the strand. | ||
| 128 | |||
| 129 | Slice objects are immutable. Once instantiated their | ||
| 130 | attributes (with the exception of the adaptor) may | ||
| 131 | not be altered. To change the attributes a new slice | ||
| 132 | must be created. | ||
| 133 | |||
| 134 | Returntype : Bio::EnsEMBL::CircularSlice | ||
| 135 | Exceptions : throws if start, end, coordsystem or seq_region_name not | ||
| 136 | specified or not of the correct type | ||
| 137 | Caller : general, Bio::EnsEMBL::SliceAdaptor | ||
| 138 | Status : Stable | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub new { | ||
| 143 | my $caller = shift; | ||
| 144 | |||
| 145 | #new can be called as a class or object method | ||
| 146 | my $class = ref($caller) || $caller; | ||
| 147 | |||
| 148 | my ( $seq, $coord_system, $seq_region_name, $seq_region_length, | ||
| 149 | $start, $end, $strand, $adaptor, $empty ) | ||
| 150 | = rearrange( [ | ||
| 151 | qw(SEQ COORD_SYSTEM SEQ_REGION_NAME SEQ_REGION_LENGTH | ||
| 152 | START END STRAND ADAPTOR EMPTY) ], | ||
| 153 | @_ ); | ||
| 154 | |||
| 155 | if ( !defined($seq_region_name) ) { | ||
| 156 | throw('SEQ_REGION_NAME argument is required'); | ||
| 157 | } | ||
| 158 | if ( !defined($start) ) { throw('START argument is required') } | ||
| 159 | if ( !defined($end) ) { throw('END argument is required') } | ||
| 160 | |||
| 161 | if ( !defined($seq_region_length) ) { $seq_region_length = $end } | ||
| 162 | |||
| 163 | if ( $seq_region_length <= 0 ) { | ||
| 164 | throw('SEQ_REGION_LENGTH must be > 0'); | ||
| 165 | } | ||
| 166 | |||
| 167 | if ( defined($coord_system) ) { | ||
| 168 | assert_ref( $coord_system, 'Bio::EnsEMBL::CoordSystem' ); | ||
| 169 | |||
| 170 | if ( $coord_system->is_top_level() ) { | ||
| 171 | throw('Cannot create circular slice on toplevel CoordSystem.'); | ||
| 172 | } | ||
| 173 | } else { | ||
| 174 | warning("CircularSlice without coordinate system"); | ||
| 175 | } | ||
| 176 | |||
| 177 | $strand ||= 1; | ||
| 178 | |||
| 179 | if ( $strand != 1 && $strand != -1 ) { | ||
| 180 | throw('STRAND argument must be -1 or 1'); | ||
| 181 | } | ||
| 182 | |||
| 183 | if ( defined($adaptor) ) { | ||
| 184 | assert_ref( $adaptor, 'Bio::EnsEMBL::DBSQL::SliceAdaptor' ); | ||
| 185 | } | ||
| 186 | |||
| 187 | my $seq1 = { 'coord_system' => $coord_system, | ||
| 188 | 'seq' => $seq, | ||
| 189 | 'seq_region_name' => $seq_region_name, | ||
| 190 | 'seq_region_length' => $seq_region_length, | ||
| 191 | 'start' => int($start), | ||
| 192 | 'end' => int($end), | ||
| 193 | 'strand' => $strand }; | ||
| 194 | |||
| 195 | bless $seq1, $class; | ||
| 196 | $seq1->adaptor($adaptor); | ||
| 197 | return $seq1; | ||
| 198 | } ## end sub new | ||
| 199 | |||
| 200 | |||
| 201 | =head2 centrepoint | ||
| 202 | |||
| 203 | Arg [1] : none | ||
| 204 | Example : $cp = $slice->centrepoint(); | ||
| 205 | Description: Returns the mid position of this slice relative to the | ||
| 206 | start of the sequence region that it was created on. | ||
| 207 | Coordinates are inclusive and start at 1. | ||
| 208 | Returntype : int | ||
| 209 | Exceptions : none | ||
| 210 | Caller : general | ||
| 211 | Status : Stable | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub centrepoint { | ||
| 216 | my $self = shift; | ||
| 217 | |||
| 218 | my ( $s, $e, $length ) = | ||
| 219 | ( $self->{'start'}, $self->{'end'}, $self->{'seq_region_length'} ); | ||
| 220 | |||
| 221 | if ( $s < $e ) { | ||
| 222 | return ( $s + $e )/2; | ||
| 223 | } | ||
| 224 | |||
| 225 | my $r1 = $length - $s; | ||
| 226 | my $r2 = $e; | ||
| 227 | my $r = ( $r1 + $r2 )/2; | ||
| 228 | my $m = $s + $r; | ||
| 229 | |||
| 230 | if ( $m > $length ) { | ||
| 231 | $m = $m - $length; | ||
| 232 | } | ||
| 233 | |||
| 234 | return $m; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head2 length | ||
| 238 | |||
| 239 | Arg [1] : none | ||
| 240 | Example : $length = $slice->length(); | ||
| 241 | Description: Returns the length of this slice in basepairs | ||
| 242 | Returntype : int | ||
| 243 | Exceptions : none | ||
| 244 | Caller : general | ||
| 245 | Status : Stable | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | sub length { | ||
| 250 | my ($self) = @_; | ||
| 251 | |||
| 252 | if ( $self->{'start'} < $self->{'end'} ) { | ||
| 253 | return $self->{'end'} - $self->{'start'} + 1; | ||
| 254 | } | ||
| 255 | |||
| 256 | my $r1 = $self->{'seq_region_length'} - $self->{'start'}; | ||
| 257 | my $r2 = $self->{'end'}; | ||
| 258 | my $ln = $r1 + $r2 + 1; | ||
| 259 | |||
| 260 | return $ln; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub _split { | ||
| 264 | my $self = shift; | ||
| 265 | |||
| 266 | my $sl1 = | ||
| 267 | Bio::EnsEMBL::CircularSlice->new( | ||
| 268 | -COORD_SYSTEM => $self->{'coord_system'}, | ||
| 269 | -SEQ_REGION_NAME => $self->{'seq_region_name'}, | ||
| 270 | -SEQ_REGION_LENGTH => $self->{'seq_region_length'}, | ||
| 271 | -START => $self->{'start'}, | ||
| 272 | -END => $self->{'seq_region_length'}, | ||
| 273 | -STRAND => $self->{'strand'}, | ||
| 274 | -ADAPTOR => $self->adaptor() ); | ||
| 275 | |||
| 276 | my $sl2 = | ||
| 277 | Bio::EnsEMBL::CircularSlice->new( | ||
| 278 | -COORD_SYSTEM => $self->{'coord_system'}, | ||
| 279 | -SEQ_REGION_NAME => $self->{'seq_region_name'}, | ||
| 280 | -SEQ_REGION_LENGTH => $self->{'seq_region_length'}, | ||
| 281 | -START => 1, | ||
| 282 | -END => $self->{'end'}, | ||
| 283 | -STRAND => $self->{'strand'}, | ||
| 284 | -ADAPTOR => $self->adaptor() ); | ||
| 285 | |||
| 286 | return ($sl1, $sl2); | ||
| 287 | } | ||
| 288 | |||
| 289 | =head2 seq | ||
| 290 | |||
| 291 | Arg [1] : none | ||
| 292 | Example : print "SEQUENCE = ", $slice->seq(); | ||
| 293 | Description: Returns the sequence of the region represented by this | ||
| 294 | slice formatted as a string. | ||
| 295 | Returntype : string | ||
| 296 | Exceptions : none | ||
| 297 | Caller : general | ||
| 298 | Status : Stable | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub seq { | ||
| 303 | my $self = shift; | ||
| 304 | |||
| 305 | # special case for in-between (insert) coordinates | ||
| 306 | return '' if ( $self->start() == $self->end() + 1 ); | ||
| 307 | return $self->{'seq'} if ( $self->{'seq'} ); | ||
| 308 | |||
| 309 | if ( $self->adaptor() ) { | ||
| 310 | |||
| 311 | my $seqAdaptor = $self->adaptor()->db()->get_SequenceAdaptor(); | ||
| 312 | if ( $self->{'start'} > $self->{'end'} ) { | ||
| 313 | my $length = $self->{'seq_region_length'}; | ||
| 314 | |||
| 315 | my ($sl1, $sl2) = $self->_split; | ||
| 316 | |||
| 317 | my $seq1 = ${ | ||
| 318 | $seqAdaptor->fetch_by_Slice_start_end_strand( $sl1, 1, $sl1->end - $sl1->start + 1, $sl1->strand) }; | ||
| 319 | |||
| 320 | my $seq2 = ${ | ||
| 321 | $seqAdaptor->fetch_by_Slice_start_end_strand( $sl2, 1, $sl2->end - $sl2->start + 1, $sl2->strand) }; | ||
| 322 | |||
| 323 | return $seq1 . $seq2; | ||
| 324 | |||
| 325 | } else { | ||
| 326 | my $seq1 = ${ | ||
| 327 | $seqAdaptor->fetch_by_Slice_start_end_strand( $self, 1, undef, | ||
| 328 | 1 ) }; | ||
| 329 | return $seq1; | ||
| 330 | } | ||
| 331 | } ## end if ( $self->adaptor() ) | ||
| 332 | |||
| 333 | # no attached sequence, and no db, so just return Ns | ||
| 334 | return 'N' x $self->length(); | ||
| 335 | } ## end sub seq | ||
| 336 | |||
| 337 | =head2 subseq | ||
| 338 | |||
| 339 | Arg [1] : int $startBasePair | ||
| 340 | relative to start of slice, which is 1. | ||
| 341 | Arg [2] : int $endBasePair | ||
| 342 | relative to start of slice. | ||
| 343 | Arg [3] : (optional) int $strand | ||
| 344 | The strand of the slice to obtain sequence from. Default | ||
| 345 | value is 1. | ||
| 346 | Description: returns string of dna sequence | ||
| 347 | Returntype : txt | ||
| 348 | Exceptions : end should be at least as big as start | ||
| 349 | strand must be set | ||
| 350 | Caller : general | ||
| 351 | Status : Stable | ||
| 352 | |||
| 353 | =cut | ||
| 354 | |||
| 355 | sub subseq { | ||
| 356 | my ( $self, $start, $end, $strand ) = @_; | ||
| 357 | |||
| 358 | # handle 'between' case for insertions | ||
| 359 | return '' if ( $start == $end + 1 ); | ||
| 360 | |||
| 361 | $strand = 1 unless ( defined $strand ); | ||
| 362 | |||
| 363 | if ( $strand != -1 && $strand != 1 ) { | ||
| 364 | throw("Invalid strand [$strand] in call to Slice::subseq."); | ||
| 365 | } | ||
| 366 | my $subseq; | ||
| 367 | my $length = $self->{'seq_region_length'}; | ||
| 368 | |||
| 369 | if ( $self->adaptor ) { | ||
| 370 | |||
| 371 | my $seqAdaptor = $self->adaptor->db->get_SequenceAdaptor(); | ||
| 372 | if ( $end < $start ) { | ||
| 373 | my $subseq1 = ${ | ||
| 374 | $seqAdaptor->fetch_by_Slice_start_end_strand( $self, $start, | ||
| 375 | $length, $strand ) | ||
| 376 | }; | ||
| 377 | my $subseq2 = ${ | ||
| 378 | $seqAdaptor->fetch_by_Slice_start_end_strand( $self, 1, $end, | ||
| 379 | $strand ) }; | ||
| 380 | $subseq = $subseq1 . $subseq2; | ||
| 381 | |||
| 382 | } else { | ||
| 383 | $subseq = ${ | ||
| 384 | $seqAdaptor->fetch_by_Slice_start_end_strand( $self, $start, | ||
| 385 | $end, $strand ) }; | ||
| 386 | } | ||
| 387 | } else { | ||
| 388 | ## check for gap at the beginning and pad it with Ns | ||
| 389 | if ( $start < 1 ) { | ||
| 390 | $subseq = "N" x ( 1 - $start ); | ||
| 391 | $start = 1; | ||
| 392 | } | ||
| 393 | $subseq .= substr( $self->seq(), $start - 1, $end - $start + 1 ); | ||
| 394 | ## check for gap at the end and pad it with Ns | ||
| 395 | if ( $end > $self->length() ) { | ||
| 396 | $subseq .= "N" x ( $end - $self->length() ); | ||
| 397 | } | ||
| 398 | reverse_comp( \$subseq ) if ( $strand == -1 ); | ||
| 399 | } | ||
| 400 | return $subseq; | ||
| 401 | } ## end sub subseq | ||
| 402 | |||
| 403 | |||
| 404 | |||
| 405 | |||
| 406 | =head2 expand | ||
| 407 | |||
| 408 | Arg [1] : (optional) int $five_prime_expand | ||
| 409 | The number of basepairs to shift this slices five_prime | ||
| 410 | coordinate by. Positive values make the slice larger, | ||
| 411 | negative make the slice smaller. | ||
| 412 | coordinate left. | ||
| 413 | Default = 0. | ||
| 414 | Arg [2] : (optional) int $three_prime_expand | ||
| 415 | The number of basepairs to shift this slices three_prime | ||
| 416 | coordinate by. Positive values make the slice larger, | ||
| 417 | negative make the slice smaller. | ||
| 418 | Default = 0. | ||
| 419 | Arg [3] : (optional) bool $force_expand | ||
| 420 | if set to 1, then the slice will be contracted even in the case | ||
| 421 | when shifts $five_prime_expand and $three_prime_expand overlap. | ||
| 422 | In that case $five_prime_expand and $three_prime_expand will be set | ||
| 423 | to a maximum possible number and that will result in the slice | ||
| 424 | which would have only 2pbs. | ||
| 425 | Default = 0. | ||
| 426 | Arg [4] : (optional) int* $fpref | ||
| 427 | The reference to a number of basepairs to shift this slices five_prime | ||
| 428 | coordinate by. Normally it would be set to $five_prime_expand. | ||
| 429 | But in case when $five_prime_expand shift can not be applied and | ||
| 430 | $force_expand is set to 1, then $$fpref will contain the maximum possible | ||
| 431 | shift | ||
| 432 | Arg [5] : (optional) int* $tpref | ||
| 433 | The reference to a number of basepairs to shift this slices three_prime | ||
| 434 | coordinate by. Normally it would be set to $three_prime_expand. | ||
| 435 | But in case when $five_prime_expand shift can not be applied and | ||
| 436 | $force_expand is set to 1, then $$tpref will contain the maximum possible | ||
| 437 | shift | ||
| 438 | Example : my $expanded_slice = $slice->expand( 1000, 1000); | ||
| 439 | my $contracted_slice = $slice->expand(-1000,-1000); | ||
| 440 | my $shifted_right_slice = $slice->expand(-1000, 1000); | ||
| 441 | my $shifted_left_slice = $slice->expand( 1000,-1000); | ||
| 442 | my $forced_contracted_slice = $slice->expand(-1000,-1000, 1, \$five_prime_shift, \$three_prime_shift); | ||
| 443 | |||
| 444 | Description: Returns a slice which is a resized copy of this slice. The | ||
| 445 | start and end are moved outwards from the center of the slice | ||
| 446 | if positive values are provided and moved inwards if negative | ||
| 447 | values are provided. This slice remains unchanged. A slice | ||
| 448 | may not be contracted below 1bp but may grow to be arbitrarily | ||
| 449 | large. | ||
| 450 | Returntype : Bio::EnsEMBL::Slice | ||
| 451 | Exceptions : warning if an attempt is made to contract the slice below 1bp | ||
| 452 | Caller : general | ||
| 453 | Status : Stable | ||
| 454 | |||
| 455 | =cut | ||
| 456 | |||
| 457 | sub expand { | ||
| 458 | my $self = shift; | ||
| 459 | my $five_prime_shift = shift || 0; | ||
| 460 | my $three_prime_shift = shift || 0; | ||
| 461 | my $force_expand = shift || 0; | ||
| 462 | my $fpref = shift; | ||
| 463 | my $tpref = shift; | ||
| 464 | |||
| 465 | if ( $self->{'seq'} ) { | ||
| 466 | warning( | ||
| 467 | "Cannot expand a slice which has a manually attached sequence "); | ||
| 468 | return undef; | ||
| 469 | } | ||
| 470 | |||
| 471 | my $new_start; | ||
| 472 | my $new_end; | ||
| 473 | my $sshift = $five_prime_shift; | ||
| 474 | my $eshift = $three_prime_shift; | ||
| 475 | |||
| 476 | if ( $self->{'strand'} != 1 ) { | ||
| 477 | $eshift = $five_prime_shift; | ||
| 478 | $sshift = $three_prime_shift; | ||
| 479 | } | ||
| 480 | |||
| 481 | $new_start = $self->{'start'} - $sshift; | ||
| 482 | $new_end = $self->{'end'} + $eshift; | ||
| 483 | |||
| 484 | # if($new_start > $new_end) { | ||
| 485 | # if ($force_expand) { # Apply max possible shift, if force_expand is set | ||
| 486 | # if ($sshift < 0) { # if we are contracting the slice from the start - move the start just before the end | ||
| 487 | # $new_start = $new_end - 1; | ||
| 488 | # $sshift = $self->{start} - $new_start; | ||
| 489 | # } | ||
| 490 | |||
| 491 | # if($new_start > $new_end) { # if the slice still has a negative length - try to move the end | ||
| 492 | # if ($eshift < 0) { | ||
| 493 | # $new_end = $new_start + 1; | ||
| 494 | # $eshift = $new_end - $self->{end}; | ||
| 495 | # } | ||
| 496 | # } | ||
| 497 | # return the values by which the primes were actually shifted | ||
| 498 | # $$tpref = $self->{strand} == 1 ? $eshift : $sshift; | ||
| 499 | # $$fpref = $self->{strand} == 1 ? $sshift : $eshift; | ||
| 500 | # } | ||
| 501 | # if($new_start > $new_end) { | ||
| 502 | # throw('Slice start cannot be greater than slice end'); | ||
| 503 | # } | ||
| 504 | # } | ||
| 505 | |||
| 506 | #fastest way to copy a slice is to do a shallow hash copy | ||
| 507 | my %new_slice = %$self; | ||
| 508 | $new_slice{'start'} = int($new_start); | ||
| 509 | $new_slice{'end'} = int($new_end); | ||
| 510 | |||
| 511 | return bless \%new_slice, ref($self); | ||
| 512 | } ## end sub expand | ||
| 513 | |||
| 514 | |||
| 515 | |||
| 516 | =head2 get_all_VariationFeatures | ||
| 517 | |||
| 518 | Args : $filter [optional] | ||
| 519 | Description:returns all variation features on this slice. This function will only work | ||
| 520 | correctly if the variation database has been attached to the core database. | ||
| 521 | If $filter is "genotyped" return genotyped Snps only... (nice likkle hack); | ||
| 522 | ReturnType : listref of Bio::EnsEMBL::Variation::VariationFeature | ||
| 523 | Exceptions : none | ||
| 524 | Caller : contigview, snpview | ||
| 525 | Status : At Risk | ||
| 526 | : Variation database is under development. | ||
| 527 | |||
| 528 | =cut | ||
| 529 | |||
| 530 | sub get_all_VariationFeatures { | ||
| 531 | my $self = shift; | ||
| 532 | my $filter = shift; | ||
| 533 | |||
| 534 | $filter ||= ''; | ||
| 535 | if ( !$self->adaptor() ) { | ||
| 536 | warning('Cannot get variation features without attached adaptor'); | ||
| 537 | return []; | ||
| 538 | } | ||
| 539 | |||
| 540 | my $vf_adaptor = | ||
| 541 | Bio::EnsEMBL::DBSQL::MergedAdaptor->new( | ||
| 542 | -species => $self->adaptor()->db()->species, | ||
| 543 | -type => "VariationFeature" ); | ||
| 544 | if ($vf_adaptor) { | ||
| 545 | if ( $filter eq 'genotyped' ) { | ||
| 546 | return $vf_adaptor->fetch_all_genotyped_by_Slice($self); | ||
| 547 | } else { | ||
| 548 | return $vf_adaptor->fetch_all_by_Slice($self); | ||
| 549 | } | ||
| 550 | } else { | ||
| 551 | warning( "Variation database must be attached to core database to " | ||
| 552 | . "retrieve variation information" ); | ||
| 553 | return []; | ||
| 554 | } | ||
| 555 | } | ||
| 556 | |||
| 557 | |||
| 558 | |||
| 559 | |||
| 560 | =head2 get_all_genotyped_VariationFeatures | ||
| 561 | |||
| 562 | Args : none | ||
| 563 | Description: returns all variation features on this slice that have been genotyped. | ||
| 564 | This function will only work correctly if the variation database has | ||
| 565 | been attached to the core database. | ||
| 566 | ReturnType : listref of Bio::EnsEMBL::Variation::VariationFeature | ||
| 567 | Exceptions : none | ||
| 568 | Caller : contigview, snpview, ldview | ||
| 569 | Status : At Risk | ||
| 570 | : Variation database is under development. | ||
| 571 | |||
| 572 | =cut | ||
| 573 | |||
| 574 | sub get_all_genotyped_VariationFeatures { | ||
| 575 | my $self = shift; | ||
| 576 | my $vfa; | ||
| 577 | if ( !$self->adaptor() ) { | ||
| 578 | warning('Cannot get variation features without attached adaptor'); | ||
| 579 | return []; | ||
| 580 | } | ||
| 581 | |||
| 582 | my $vf_adaptor = | ||
| 583 | Bio::EnsEMBL::DBSQL::MergedAdaptor->new( | ||
| 584 | -species => $self->adaptor()->db()->species, | ||
| 585 | -type => "VariationFeature" ); | ||
| 586 | |||
| 587 | if ($vf_adaptor) { | ||
| 588 | return $vf_adaptor->fetch_all_genotyped_by_Slice($self); | ||
| 589 | } else { | ||
| 590 | warning( "Variation database must be attached to core database to " | ||
| 591 | . "retrieve variation information" ); | ||
| 592 | return []; | ||
| 593 | } | ||
| 594 | } | ||
| 595 | |||
| 596 | |||
| 597 | =head2 get_all_DASFeatures | ||
| 598 | |||
| 599 | Arg [1] : none | ||
| 600 | Example : $features = $slice->get_all_DASFeatures; | ||
| 601 | Description: Retrieves a hash reference to a hash of DAS feature | ||
| 602 | sets, keyed by the DNS, NOTE the values of this hash | ||
| 603 | are an anonymous array containing: | ||
| 604 | (1) a pointer to an array of features; | ||
| 605 | (2) a pointer to the DAS stylesheet | ||
| 606 | Returntype : hashref of Bio::SeqFeatures | ||
| 607 | Exceptions : ? | ||
| 608 | Caller : webcode | ||
| 609 | Status : Stable | ||
| 610 | |||
| 611 | =cut | ||
| 612 | sub get_all_DASFeatures { | ||
| 613 | my ( $self, $source_type ) = @_; | ||
| 614 | |||
| 615 | if ( !$self->adaptor() ) { | ||
| 616 | warning("Cannot retrieve features without attached adaptor"); | ||
| 617 | return []; | ||
| 618 | } | ||
| 619 | |||
| 620 | my %genomic_features = map { | ||
| 621 | ( $_->adaptor->dsn => | ||
| 622 | [ $_->fetch_all_Features( $self, $source_type ) ] ) | ||
| 623 | } $self->adaptor()->db()->_each_DASFeatureFactory; | ||
| 624 | return \%genomic_features; | ||
| 625 | |||
| 626 | } | ||
| 627 | |||
| 628 | |||
| 629 | =head2 project_to_slice | ||
| 630 | |||
| 631 | Arg [1] : Slice to project to. | ||
| 632 | Example : my $chr_projection = $clone_slice->project_to_slice($chrom_slice); | ||
| 633 | foreach my $segment ( @$chr_projection ){ | ||
| 634 | $chr_slice = $segment->to_Slice(); | ||
| 635 | print $clone_slice->seq_region_name(). ':'. $segment->from_start(). '-'. | ||
| 636 | $segment->from_end(). ' -> '.$chr_slice->seq_region_name(). ':'. $chr_slice->start(). | ||
| 637 | '-'.$chr_slice->end(). | ||
| 638 | $chr_slice->strand(). " length: ".($chr_slice->end()-$chr_slice->start()+1). "\n"; | ||
| 639 | } | ||
| 640 | Description: Projection of slice to another specific slice. Needed for where we have multiple mappings | ||
| 641 | and we want to state which one to project to. | ||
| 642 | Returntype : list reference of Bio::EnsEMBL::ProjectionSegment objects which | ||
| 643 | can also be used as [$start,$end,$slice] triplets. | ||
| 644 | Exceptions : none | ||
| 645 | Caller : none | ||
| 646 | Status : At Risk | ||
| 647 | |||
| 648 | =cut | ||
| 649 | |||
| 650 | sub project_to_slice { | ||
| 651 | my $self = shift; | ||
| 652 | my $to_slice = shift; | ||
| 653 | |||
| 654 | throw('Slice argument is required') if ( !$to_slice ); | ||
| 655 | |||
| 656 | my $slice_adaptor = $self->adaptor(); | ||
| 657 | |||
| 658 | if ( !$slice_adaptor ) { | ||
| 659 | warning("Cannot project without attached adaptor."); | ||
| 660 | return []; | ||
| 661 | } | ||
| 662 | |||
| 663 | my $mapper_aptr = $slice_adaptor->db->get_AssemblyMapperAdaptor(); | ||
| 664 | |||
| 665 | my $cs = $to_slice->coord_system(); | ||
| 666 | my $slice_cs = $self->coord_system(); | ||
| 667 | |||
| 668 | my @projection; | ||
| 669 | my $current_start = 1; | ||
| 670 | |||
| 671 | # decompose this slice into its symlinked components. | ||
| 672 | # this allows us to handle haplotypes and PARs | ||
| 673 | my $normal_slice_proj = | ||
| 674 | $slice_adaptor->fetch_normalized_slice_projection($self); | ||
| 675 | foreach my $segment (@$normal_slice_proj) { | ||
| 676 | my $normal_slice = $segment->[2]; | ||
| 677 | |||
| 678 | $slice_cs = $normal_slice->coord_system(); | ||
| 679 | |||
| 680 | my $asma = $self->adaptor->db->get_AssemblyMapperAdaptor(); | ||
| 681 | my $asm_mapper = $asma->fetch_by_CoordSystems( $slice_cs, $cs ); | ||
| 682 | |||
| 683 | # perform the mapping between this slice and the requested system | ||
| 684 | my @coords; | ||
| 685 | |||
| 686 | if ( defined $asm_mapper ) { | ||
| 687 | @coords = $asm_mapper->map( $normal_slice->seq_region_name(), | ||
| 688 | $normal_slice->start(), | ||
| 689 | $normal_slice->end(), | ||
| 690 | $normal_slice->strand(), | ||
| 691 | $slice_cs, | ||
| 692 | undef, | ||
| 693 | $to_slice ); | ||
| 694 | } else { | ||
| 695 | $coords[0] = | ||
| 696 | Bio::EnsEMBL::Mapper::Gap->new( $normal_slice->start(), | ||
| 697 | $normal_slice->end() ); | ||
| 698 | } | ||
| 699 | |||
| 700 | #construct a projection from the mapping results and return it | ||
| 701 | foreach my $coord (@coords) { | ||
| 702 | my $coord_start = $coord->start(); | ||
| 703 | my $coord_end = $coord->end(); | ||
| 704 | my $length = $coord_end - $coord_start + 1; | ||
| 705 | |||
| 706 | #skip gaps | ||
| 707 | if ( $coord->isa('Bio::EnsEMBL::Mapper::Coordinate') ) { | ||
| 708 | my $coord_cs = $coord->coord_system(); | ||
| 709 | |||
| 710 | # If the normalised projection just ended up mapping to the | ||
| 711 | # same coordinate system we were already in then we should just | ||
| 712 | # return the original region. This can happen for example, if we | ||
| 713 | # were on a PAR region on Y which refered to X and a projection to | ||
| 714 | # 'toplevel' was requested. | ||
| 715 | # if($coord_cs->equals($slice_cs)) { | ||
| 716 | # # trim off regions which are not defined | ||
| 717 | # return $self->_constrain_to_region(); | ||
| 718 | # } | ||
| 719 | |||
| 720 | #create slices for the mapped-to coord system | ||
| 721 | my $slice = | ||
| 722 | $slice_adaptor->fetch_by_seq_region_id( $coord->id(), | ||
| 723 | $coord_start, $coord_end, $coord->strand() ); | ||
| 724 | |||
| 725 | my $current_end = $current_start + $length - 1; | ||
| 726 | |||
| 727 | push @projection, | ||
| 728 | bless( [ $current_start, $current_end, $slice ], | ||
| 729 | "Bio::EnsEMBL::ProjectionSegment" ); | ||
| 730 | } | ||
| 731 | |||
| 732 | $current_start += $length; | ||
| 733 | } ## end foreach my $coord (@coords) | ||
| 734 | } ## end foreach my $segment (@$normal_slice_proj) | ||
| 735 | |||
| 736 | # delete the cache as we may want to map to different set next time and old | ||
| 737 | # results will be cached. | ||
| 738 | |||
| 739 | $mapper_aptr->delete_cache; | ||
| 740 | |||
| 741 | return \@projection; | ||
| 742 | } ## end sub project_to_slice | ||
| 743 | |||
| 744 | # | ||
| 745 | # Bioperl Bio::PrimarySeqI methods: | ||
| 746 | # | ||
| 747 | |||
| 748 | =head2 id | ||
| 749 | |||
| 750 | Description: Included for Bio::PrimarySeqI interface compliance (0.7) | ||
| 751 | |||
| 752 | =cut | ||
| 753 | |||
| 754 | sub id { name(@_); } | ||
| 755 | |||
| 756 | =head2 display_id | ||
| 757 | |||
| 758 | Description: Included for Bio::PrimarySeqI interface compliance (1.2) | ||
| 759 | |||
| 760 | =cut | ||
| 761 | |||
| 762 | sub display_id { name(@_); } | ||
| 763 | |||
| 764 | =head2 primary_id | ||
| 765 | |||
| 766 | Description: Included for Bio::PrimarySeqI interface compliance (1.2) | ||
| 767 | |||
| 768 | =cut | ||
| 769 | |||
| 770 | sub primary_id { name(@_); } | ||
| 771 | |||
| 772 | =head2 desc | ||
| 773 | |||
| 774 | Description: Included for Bio::PrimarySeqI interface compliance (1.2) | ||
| 775 | |||
| 776 | =cut | ||
| 777 | |||
| 778 | sub desc { | ||
| 779 | return $_[0]->coord_system->name() . ' ' . $_[0]->seq_region_name(); | ||
| 780 | } | ||
| 781 | |||
| 782 | =head2 moltype | ||
| 783 | |||
| 784 | Description: Included for Bio::PrimarySeqI interface compliance (0.7) | ||
| 785 | |||
| 786 | =cut | ||
| 787 | |||
| 788 | sub moltype { return 'dna'; } | ||
| 789 | |||
| 790 | =head2 alphabet | ||
| 791 | |||
| 792 | Description: Included for Bio::PrimarySeqI interface compliance (1.2) | ||
| 793 | |||
| 794 | =cut | ||
| 795 | |||
| 796 | sub alphabet { return 'dna'; } | ||
| 797 | |||
| 798 | =head2 accession_number | ||
| 799 | |||
| 800 | Description: Included for Bio::PrimarySeqI interface compliance (1.2) | ||
| 801 | |||
| 802 | =cut | ||
| 803 | |||
| 804 | sub accession_number { name(@_); } | ||
| 805 | |||
| 806 | =head2 is_circular | ||
| 807 | |||
| 808 | Description: Included for Bio::PrimarySeqI interface compliance (1.2) | ||
| 809 | |||
| 810 | =cut | ||
| 811 | |||
| 812 | sub is_circular { | ||
| 813 | my ($self) = @_; | ||
| 814 | |||
| 815 | if ( !defined( $self->{'circular'} ) ) { | ||
| 816 | my @attrs = | ||
| 817 | grep { $_ } @{ $self->get_all_Attributes('circular_seq') }; | ||
| 818 | $self->{'circular'} = @attrs ? 1 : 0; | ||
| 819 | } | ||
| 820 | |||
| 821 | return $self->{'circular'}; | ||
| 822 | } | ||
| 823 | |||
| 824 | |||
| 825 | 1; |