EnsEMBL/DnaDnaAlignFeature.pm untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 27 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 331 CC 52 SCAR 51.8
CC
52
Cov
0%
_ensembl_alignment_strings
380.0
restrict_between_positions
240.0
_ensembl_SimpleAlign
132.0
CRAP 178.1
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::DnaDnaAlignFeature - Ensembl specific dna-dna pairwise
34 alignment feature
35  
36 =head1 SYNOPSIS
37  
38 See BaseAlignFeature
39  
40 =cut
41  
42  
43 package Bio::EnsEMBL::DnaDnaAlignFeature;
44 $Bio::EnsEMBL::DnaDnaAlignFeature::VERSION = '114.0.0_50'; # TRIAL
45 $Bio::EnsEMBL::DnaDnaAlignFeature::VERSION = '114.0.050';
46 use strict;
47  
48 use Bio::EnsEMBL::BaseAlignFeature;
49  
50 use vars qw(@ISA);
51 use Bio::SimpleAlign;
52 use Bio::LocatableSeq;
53 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
54 use Bio::EnsEMBL::Utils::Exception qw(throw);
55  
56 @ISA = qw( Bio::EnsEMBL::BaseAlignFeature );
57  
58 use constant SEQUENCE_ONTOLOGY => {
59 acc => 'SO:0000347',
60 term => 'nucleotide_match',
61 };
62  
63 =head2 new
64  
65 Arg [..] : List of named arguments. defined
66 in this constructor, others defined in BaseFeaturePair and
67 SeqFeature superclasses.
68 Example : $daf = new DnaDnaAlignFeature(-cigar_string => '3M3I12M');
69 Description: Creates a new DnaDnaAlignFeature using either a cigarstring or
70 a list of ungapped features.
71 Returntype : Bio::EnsEMBL::DnaDnaAlignFeature
72 Exceptions : none
73 Caller : general
74 Status : Stable
75  
76 =cut
77  
78 sub new {
79
80 my $caller = shift;
81  
82 my $class = ref($caller) || $caller;
83  
84 my $self = $class->SUPER::new(@_);
85  
86 return $self;
87 }
88  
89  
90 =head2 _hit_unit
91  
92 Arg [1] : none
93 Description: PRIVATE implementation of abstract superclass method. Returns
94 1 as the 'unit' used for the hit sequence.
95 Returntype : int
96 Exceptions : none
97 Caller : Bio::EnsEMBL::BaseAlignFeature
98 Status : Stable
99  
100 =cut
101  
102 sub _hit_unit {
103 return 1;
104 }
105  
106  
107  
108 =head2 _query_unit
109  
110 Arg [1] : none
111 Description: PRIVATE implementation of abstract superclass method Returns
112 1 as the 'unit' used for the hit sequence.
113 Returntype : int
114 Exceptions : none
115 Caller : Bio::EnsEMBL::BaseAlignFeature
116 Status : Stable
117  
118 =cut
119  
120 sub _query_unit {
121 return 1;
122 }
123  
124 =head2 restrict_between_positions
125  
126 Arg [1] : int $start
127 Arg [2] : int $end
128 Arg [3] : string $flags
129 SEQ = $start and $end apply to the seq sequence
130 i.e. start and end methods
131 HSEQ = $start and $end apply to the hseq sequence
132 i.e. hstart and hend methods
133 Example : $daf->restrict_between_positions(150,543,"SEQ")
134 Description: Build a new DnaDnaAlignFeature object that fits within
135 the new specified coordinates and sequence reference, cutting
136 any pieces hanging upstream and downstream.
137 Returntype : Bio::EnsEMBL::DnaDnaAlignFeature object
138 Exceptions :
139 Caller :
140 Status : Stable
141  
142 =cut
143  
144 sub restrict_between_positions {
145 my ($self,$start,$end,$seqref) = @_;
146  
147 unless (defined $start && $start =~ /^\d+$/) {
148 $self->throw("The first argument is not defined or is not an integer");
149 }
150 unless (defined $end && $end =~ /^\d+$/) {
151 $self->throw("The second argument is not defined or is not an integer");
152 }
153 unless (defined $seqref &&
154 ($seqref eq "SEQ" || $seqref eq "HSEQ")) {
155 $self->throw("The third argument is not defined or is not equal to 'SEQ' or 'HSEQ'");
156 }
157  
158 # symbolic method references should be forbidden!
159 # need to be rewrite at some stage.
160  
161 my ($start_method1,$end_method1,$strand_method1,$start_method2,$end_method2,$strand_method2) =
162 qw(start end strand hstart hend hstrand);
163  
164 if ($seqref eq "HSEQ") {
165 ($start_method1,$end_method1,$strand_method1,$start_method2,$end_method2,$strand_method2) =
166 qw(hstart hend hstrand start end strand);
167 }
168  
169 my @restricted_features;
170  
171 foreach my $ungapped_feature ($self->ungapped_features) {
172  
173 if ($ungapped_feature->$start_method1() > $end ||
174 $ungapped_feature->$end_method1() < $start) {
175  
176 next;
177  
178 } elsif ($ungapped_feature->$end_method1() <= $end &&
179 $ungapped_feature->$start_method1() >= $start) {
180  
181 push @restricted_features, $ungapped_feature;
182  
183 } else {
184  
185 if ($ungapped_feature->$strand_method1() eq $ungapped_feature->$strand_method2()) {
186  
187 if ($ungapped_feature->$start_method1() < $start) {
188  
189 my $offset = $start - $ungapped_feature->$start_method1();
190 $ungapped_feature->$start_method1($start);
191 $ungapped_feature->$start_method2($ungapped_feature->$start_method2() + $offset);
192  
193 }
194 if ($ungapped_feature->$end_method1() > $end) {
195  
196 my $offset = $ungapped_feature->$end_method1() - $end;
197 $ungapped_feature->$end_method1($end);
198 $ungapped_feature->$end_method2($ungapped_feature->$end_method2() - $offset);
199  
200 }
201 } else {
202  
203 if ($ungapped_feature->$start_method1() < $start) {
204  
205 my $offset = $start - $ungapped_feature->$start_method1();
206 $ungapped_feature->$start_method1($start);
207 $ungapped_feature->$end_method2($ungapped_feature->$end_method2() - $offset);
208  
209 }
210 if ($ungapped_feature->$end_method1() > $end) {
211  
212 my $offset = $ungapped_feature->$end_method1() - $end;
213 $ungapped_feature->$end_method1($end);
214 $ungapped_feature->$start_method2($ungapped_feature->$start_method2() + $offset);
215  
216 }
217 }
218
219 push @restricted_features, $ungapped_feature;
220 }
221 }
222  
223 if (scalar @restricted_features) {
224 my $DnaDnaAlignFeature = new Bio::EnsEMBL::DnaDnaAlignFeature('-features' =>\@restricted_features);
225 if (defined $self->slice) {
226 $DnaDnaAlignFeature->slice($self->slice);
227 }
228 if (defined $self->hslice) {
229 $DnaDnaAlignFeature->hslice($self->hslice);
230 }
231 return $DnaDnaAlignFeature;
232 } else {
233 return undef;
234 }
235 }
236  
237 =head2 alignment_strings
238  
239 Arg [1] : list of string $flags
240 FIX_SEQ = does not introduce gaps (dashes) in seq aligned sequence
241 and delete the corresponding insertions in hseq aligned sequence
242 FIX_HSEQ = does not introduce gaps (dashes) in hseq aligned sequence
243 and delete the corresponding insertions in seq aligned sequence
244 NO_SEQ = return the seq aligned sequence as an empty string
245 NO_HSEQ = return the hseq aligned sequence as an empty string
246 This 2 last flags would save a bit of time as doing so no querying to the core
247 database in done to get the sequence.
248 Example : $daf->alignment_strings or
249 $daf->alignment_strings("FIX_HSEQ") or
250 $daf->alignment_strings("NO_SEQ","FIX_SEQ")
251 Description: Allows to rebuild the alignment string of both the seq and hseq sequence
252 using the cigar_string information and the slice and hslice objects
253 Returntype : array reference containing 2 strings
254 the first corresponds to seq
255 the second corresponds to hseq
256 Exceptions :
257 Caller :
258 Status : Stable
259  
260 =cut
261  
262  
263 sub alignment_strings {
264 my ($self, @flags) = @_;
265 if ($self->align_type eq 'ensembl') {
266 return $self->_ensembl_alignment_strings(@flags);
267 } else {
268 throw("alignment_strings method not implemented for " . $self->align_type);
269 }
270 }
271  
272 =head2 _ensembl_alignment_strings
273  
274 Arg [1] : list of string $flags
275 Description: Allows to rebuild the alignment string of both the seq and hseq sequence
276 using the cigar_string information for ensembl cigar strings
277 Returntype : array reference containing 2 strings
278 the first corresponds to seq
279 the second corresponds to hseq
280 Exceptions :
281 Caller :
282 Status : Stable
283  
284 =cut
285  
286  
287 sub _ensembl_alignment_strings {
288 my ( $self, @flags ) = @_;
289  
290 # set the flags
291 my $seq_flag = 1;
292 my $hseq_flag = 1;
293 my $fix_seq_flag = 0;
294 my $fix_hseq_flag = 0;
295  
296 for my $flag ( @flags ) {
297 $seq_flag = 0 if ($flag eq "NO_SEQ");
298 $hseq_flag = 0 if ($flag eq "NO_HSEQ");
299 $fix_seq_flag = 1 if ($flag eq "FIX_SEQ");
300 $fix_hseq_flag = 1 if ($flag eq "FIX_HSEQ");
301 }
302  
303 my ($seq, $hseq);
304 $seq = $self->slice->subseq($self->start, $self->end, $self->strand) if ($seq_flag || $fix_seq_flag);
305 $hseq = $self->hslice->subseq($self->hstart, $self->hend, $self->hstrand) if ($hseq_flag || $fix_hseq_flag);
306
307 my $rseq= "";
308 # rseq - result sequence
309 my $rhseq= "";
310 # rhseq - result hsequence
311  
312 my $seq_pos = 0;
313 my $hseq_pos = 0;
314  
315 my @cig = ( $self->cigar_string =~ /(\d*[DIM])/g );
316  
317 for my $cigElem ( @cig ) {
318 my $cigType = substr( $cigElem, -1, 1 );
319 my $cigCount = substr( $cigElem, 0 ,-1 );
320 $cigCount = 1 unless $cigCount;
321  
322 if( $cigType eq "M" ) {
323 $rseq .= substr( $seq, $seq_pos, $cigCount ) if ($seq_flag);
324 $rhseq .= substr( $hseq, $hseq_pos, $cigCount ) if ($hseq_flag);
325 $seq_pos += $cigCount;
326 $hseq_pos += $cigCount;
327 } elsif( $cigType eq "D" ) {
328 if( ! $fix_seq_flag ) {
329 $rseq .= "-" x $cigCount if ($seq_flag);
330 $rhseq .= substr( $hseq, $hseq_pos, $cigCount ) if ($hseq_flag);
331 }
332 $hseq_pos += $cigCount;
333 } elsif( $cigType eq "I" ) {
334 if( ! $fix_hseq_flag ) {
335 $rseq .= substr( $seq, $seq_pos, $cigCount ) if ($seq_flag);
336 $rhseq .= "-" x $cigCount if ($hseq_flag);
337 }
338 $seq_pos += $cigCount;
339 }
340 }
341 return [ $rseq,$rhseq ];
342 }
343  
344 =head2 get_SimpleAlign
345  
346 Arg [1] : list of string $flags
347 translated = by default, the sequence alignment will be on nucleotide. With translated flag
348 the aligned sequences are translated.
349 uc = by default aligned sequences are given in lower cases. With uc flag, the aligned
350 sequences are given in upper cases.
351 Example : $daf->get_SimpleAlign or
352 $daf->get_SimpleAlign("translated") or
353 $daf->get_SimpleAlign("translated","uc")
354 Description: Allows to rebuild the alignment string of both the seq and hseq sequence
355 using the cigar_string information and the slice and hslice objects
356 Returntype : a Bio::SimpleAlign object
357 Exceptions :
358 Caller :
359 Status : Stable
360  
361 =cut
362  
363 sub get_SimpleAlign {
364 my ( $self, @flags ) = @_;
365  
366 if ($self->align_type eq 'ensembl') {
367 return $self->_ensembl_SimpleAlign();
368 } else {
369 throw("No get_SimpleAlign method implemented for " . $self->align_type);
370 }
371 }
372  
373 =head2 _ensembl_SimpleAlign
374  
375 Arg [1] : list of string $flags
376 Description: Internal method to build alignment string
377 for ensembl type cigar strings
378 using the cigar_string information and the slice and hslice objects
379 Returntype : a Bio::SimpleAlign object
380 Exceptions :
381 Caller :
382 Status : Stable
383  
384 =cut
385  
386 sub _ensembl_SimpleAlign {
387 my ( $self, @flags ) = @_;
388  
389 # setting the flags
390 my $uc = 0;
391 my $translated = 0;
392  
393 for my $flag ( @flags ) {
394 $uc = 1 if ($flag =~ /^uc$/i);
395 $translated = 1 if ($flag =~ /^translated$/i);
396 }
397  
398 my $sa = Bio::SimpleAlign->new();
399  
400 #Hack to try to work with both bioperl 0.7 and 1.2:
401 #Check to see if the method is called 'addSeq' or 'add_seq'
402 my $bio07 = 0;
403 if(!$sa->can('add_seq')) {
404 $bio07 = 1;
405 }
406  
407 my ($sb_seq,$qy_seq) = @{$self->alignment_strings};
408  
409 my $loc_sb_seq = Bio::LocatableSeq->new(-SEQ => $uc ? uc $sb_seq : lc $sb_seq,
410 -START => $self->seq_region_start,
411 -END => $self->seq_region_end,
412 -ID => $self->seqname,
413 -STRAND => $self->strand);
414  
415 $loc_sb_seq->seq($uc ? uc $loc_sb_seq->translate->seq
416 : lc $loc_sb_seq->translate->seq) if ($translated);
417  
418 my $loc_qy_seq = Bio::LocatableSeq->new(-SEQ => $uc ? uc $qy_seq : lc $qy_seq,
419 -START => $self->hseq_region_start,
420 -END => $self->hseq_region_end,
421 -ID => $self->hseqname,
422 -STRAND => $self->hstrand);
423  
424 $loc_qy_seq->seq($uc ? uc $loc_qy_seq->translate->seq
425 : lc $loc_qy_seq->translate->seq) if ($translated);
426  
427 if($bio07) {
428 $sa->addSeq($loc_sb_seq);
429 $sa->addSeq($loc_qy_seq);
430 } else {
431 $sa->add_seq($loc_sb_seq);
432 $sa->add_seq($loc_qy_seq);
433 }
434  
435 return $sa;
436 }
437  
438  
439 =head2 get_all_Attributes
440  
441 Arg [1] : (optional) String $attrib_code
442 The code of the attribute type to retrieve values for
443 Example : my ($description) = @{ $feature->get_all_Attributes('description') };
444 my @attributes = @{ $feature->get_all_Attributes };
445 Description: Gets a list of Attributes of this gene.
446 Optionally just get Attributes for given code.
447 Returntype : Listref of Bio::EnsEMBL::Attribute
448 Exceptions :
449 Caller : general
450 Status : Stable
451  
452 =cut
453  
454 sub get_all_Attributes {
455 my $self = shift;
456 my $attrib_code = shift;
457  
458 if ( ! exists $self->{'attributes' } ) {
459 if (!$self->adaptor() ) {
460 return [];
461 }
462  
463 my $attribute_adaptor = $self->adaptor->db->get_AttributeAdaptor();
464 $self->{'attributes'} = $attribute_adaptor->fetch_all_by_DnaDnaAlignFeature($self);
465 }
466  
467 if ( defined $attrib_code ) {
468 my @results = grep { uc($_->code()) eq uc($attrib_code) }
469 @{$self->{'attributes'}};
470 return \@results;
471 } else {
472 return $self->{'attributes'};
473 }
474 }
475  
476  
477 =head2 add_Attributes
478  
479 Arg [1-N] : list of Bio::EnsEMBL::Attribute's @attribs
480 Attribute(s) to add
481 Example : my $attrib = Bio::EnsEMBL::Attribute->new(...);
482 $gene->add_Attributes($attrib);
483 Description: Adds an Attribute to the feature.
484 Returntype : none
485 Exceptions : throw on incorrect arguments
486 Caller : general
487 Status : Stable
488  
489 =cut
490  
491 sub add_Attributes {
492 my $self = shift;
493 my @attribs = @_;
494  
495 if( ! exists $self->{'attributes'} ) {
496 $self->{'attributes'} = [];
497 }
498  
499 for my $attrib ( @attribs ) {
500 if( ! $attrib->isa( "Bio::EnsEMBL::Attribute" )) {
501 throw( "Argument to add_Attribute has to be an Bio::EnsEMBL::Attribute" );
502 }
503 push( @{$self->{'attributes'}}, $attrib );
504 }
505  
506 return;
507 }
508 =head2 transfer
509  
510 Arg [1] : Bio::EnsEMBL::Slice $destination_slice
511 Example : my $new_feature = $feature->transfer($slice);
512 Description: Moves this feature to given target slice coordinates.
513 Returns a new feature.
514 Returntype : Bio::EnsEMBL::DnaDnaAlignFeature
515 Exceptions : none
516 Caller : general
517 Status : Stable
518  
519 =cut
520  
521 sub transfer {
522 my $self = shift;
523
524 my $new_feature = $self->SUPER::transfer( @_ );
525 return undef unless $new_feature;
526  
527 if(exists $self->{attributes}) {
528 $new_feature->{attributes} = [@{$self->{attributes}}];
529 }
530
531 return $new_feature;
532 }
533  
534 1;