FAST/Bio/Seq/Quality.pm untested

filter: Statementstmt 0.0% 0 / 162 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 319 CC 28 SCAR 27.4
CC
28
Cov
0%
_find_clear_ranges
42.0
get_trace_graph
30.0
threshold
20.0
CRAP 15.5
1 #
2 # BioPerl module for FAST::Bio::Seq::Quality
3 #
4 # Please direct questions and support issues to
5 # <bioperl-l@bioperl.org>
6 #
7 # Cared for by Heikki Lehvaslaiho
8 #
9 # Copyright Heikki Lehvaslaiho
10 #
11 # You may distribute this module under the same terms as perl itself
12  
13 # POD documentation - main docs before the code
14  
15 =head1 NAME
16  
17 FAST::Bio::Seq::Quality - Implementation of sequence with residue quality
18 and trace values
19  
20 =head1 SYNOPSIS
21  
22 use FAST::Bio::Seq::Quality;
23  
24 # input can be space delimited string or array ref
25 my $qual = '0 1 2 3 4 5 6 7 8 9 11 12';
26 my $trace = '0 5 10 15 20 25 30 35 40 45 50 55';
27  
28 my $seq = FAST::Bio::Seq::Quality->new
29 ( -qual => $qual,
30 -trace_indices => $trace,
31 -seq => 'atcgatcgatcg',
32 -id => 'human_id',
33 -accession_number => 'S000012',
34 -verbose => -1 # to silence deprecated methods
35 );
36  
37 my $quals = $seq->qual; # array ref
38 my $traces = $seq->trace; # array ref
39  
40 my $quals = $seq->qual_text; # string
41 my $traces = $seq->trace_text; # string
42  
43  
44 # get sub values
45 $quals = $seq->subqual(2, 3); # array ref
46 $traces = $seq->subtrace(2, 3); # array ref
47 $quals = $seq->subqual_text(2, 3); # string
48 $quals = $seq->subtrace_text(2, 3); # string
49  
50 # set sub values
51 $seq->subqual(2, 3, "9 9");
52 $seq->subtrace(2, 3, "9 9");
53  
54  
55  
56 =head1 DESCRIPTION
57  
58 This object stores base quality values together with the sequence
59 string.
60  
61 It is a reimplementation of Chad Matsalla's FAST::Bio::Seq::SeqWithQuality
62 module using FAST::Bio::Seq::MetaI.
63  
64 The implementation is based on FAST::Bio::Seq::Meta::Array. qual() and
65 trace() are base methods to store and retrieve information that have
66 extensions to retrieve values as a scalar (e.g. qual_text() ), or get
67 or set subvalues (e.g. subqual() ). See L<FAST::Bio::Seq::MetaI> for more
68 details.
69  
70 All the functional code is in FAST::Bio::Seq::Meta::Array.
71  
72 There deprecated methods that are included for compatibility with
73 FAST::Bio::Seq::SeqWithQuality. These will print a warning unless verbosity
74 of the object is set to be less than zero.
75  
76 =head2 Differences from FAST::Bio::Seq::SeqWithQuality
77  
78 It is not possible to fully follow the interface of
79 FAST::Bio::Seq::SeqWithQuality since internally a FAST::Bio::Seq::SeqWithQuality
80 object is a composite of two independent objects: a FAST::Bio::PrimarySeq
81 object and FAST::Bio::Seq::PrimaryQual object. Both of these objects can be
82 created separately and merged into FAST::Bio::Seq::SeqWithQuality.
83  
84 This implementation is based on FAST::Bio::Seq::Meta::Array that is a
85 subclass of FAST::Bio::PrimarySeq that stores any number of meta information
86 in unnamed arrays.
87  
88 Here we assume that two meta sets, called 'qual' and 'trace_indices'
89 are attached to a sequence. (But there is nothing that prevents you to
90 add as many named meta sets as you need using normal meta() methods).
91  
92 qual() is an alias to meta(), qualat($loc) is an alias to
93 submeta($loc,$loc).
94  
95 trace_indices() in FAST::Bio::Seq::SeqWithQuality has been abbreviated to
96 trace() and is an alias to named_meta('trace').
97  
98 You can create an object without passing any arguments to the
99 constructor (FAST::Bio::Seq::SeqWithQuality fails without alphabet). It will
100 warn about not being able to set alphabet unless you set verbosity of
101 the object to a negative value.
102  
103 After the latest rewrite, the meta information sets (quality and
104 trace) no longer cover all the residues automatically. Methods to
105 check the length of meta information (L<quality_length>,
106 L<trace_length>)and to see if the ends are flushed to the sequence
107 have been added (L<quality_is_flush>, L<trace_is_flush>). To force the
108 old functinality, set L<force_flush> to true.
109  
110 qual_obj() and seq_obj() methods do not exist!
111  
112 Finally, there is only one set of descriptors (primary_id, display_id,
113 accession_number) for the object.
114  
115  
116 =head1 SEE ALSO
117  
118 L<FAST::Bio::Seq::MetaI>,
119 L<FAST::Bio::Seq::Meta::Array>
120  
121 =head1 FEEDBACK
122  
123 =head2 Mailing Lists
124  
125 User feedback is an integral part of the evolution of this and other
126 Bioperl modules. Send your comments and suggestions preferably to one
127 of the Bioperl mailing lists. Your participation is much appreciated.
128  
129 bioperl-l@bioperl.org - General discussion
130 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
131  
132 =head2 Support
133  
134 Please direct usage questions or support issues to the mailing list:
135  
136 I<bioperl-l@bioperl.org>
137  
138 rather than to the module maintainer directly. Many experienced and
139 reponsive experts will be able look at the problem and quickly
140 address it. Please include a thorough description of the problem
141 with code and data examples if at all possible.
142  
143 =head2 Reporting Bugs
144  
145 Report bugs to the Bioperl bug tracking system to help us keep track
146 the bugs and their resolution. Bug reports can be submitted via the
147 web:
148  
149 https://redmine.open-bio.org/projects/bioperl/
150  
151 =head1 AUTHOR - Heikki Lehvaslaiho
152  
153 Email heikki-at-bioperl-dot-org
154  
155 =head1 CONTRIBUTORS
156  
157 Chad Matsalla, bioinformatics at dieselwurks dot com
158  
159 Dan Bolser, dan dot bolser at gmail dot com
160  
161 =head1 APPENDIX
162  
163 The rest of the documentation details each of the object methods.
164 Internal methods are usually preceded with a _
165  
166 =cut
167  
168  
169 # Let the code begin...
170  
171  
172 package FAST::Bio::Seq::Quality;
173 use strict;
174  
175 use base qw(FAST::Bio::Seq::Meta::Array);
176  
177 ## Is this the right place (and way) to define this?
178 our $MASK_CHAR = 'X';
179 our $DEFAULT_NAME = 'DEFAULT';
180 our $GAP = '-';
181 our $META_GAP = ' ';
182  
183 =head2 new
184  
185 Title : new
186 Usage : $metaseq = FAST::Bio::Seq::Quality->new
187 ( -qual => '0 1 2 3 4 5 6 7 8 9 11 12',
188 -trace => '0 5 10 15 20 25 30 35 40 45 50 55',
189 -seq => 'atcgatcgatcg',
190 -id => 'human_id',
191 -accession_number => 'S000012',
192 );
193 Function: Constructor for FAST::Bio::Seq::Quality class. Note that you can
194 provide an empty quality and trace strings.
195  
196 Returns : a new FAST::Bio::Seq::Quality object
197  
198 =cut
199  
200  
201 sub new {
202 my ($class, @args) = @_;
203  
204 my $self = $class->SUPER::new(@args);
205  
206 my($meta, $qual, $trace, $trace_indices, $trace_data) =
207 $self->_rearrange([qw(META
208 QUAL
209 TRACE
210 TRACE_INDICES
211 TRACE_DATA)],
212 @args);
213  
214 $self->{'_meta'}->{$DEFAULT_NAME} = [];
215 $self->{'_meta'}->{'trace'} = [];
216 $self->{trace_data} = $trace_data;
217  
218 $meta && $self->meta($meta);
219 $qual && $self->qual($qual);
220 $trace && $self->named_meta('trace', $trace);
221 $trace_indices && $self->named_meta('trace', $trace_indices);
222  
223 return $self;
224 }
225  
226  
227  
228 ## QUAL
229  
230 =head2 qual
231  
232 Title : qual
233 Usage : $qual_values = $obj->qual($values_string);
234 Function:
235  
236 Get and set method for the meta data starting from residue
237 position one. Since it is dependent on the length of the
238 sequence, it needs to be manipulated after the sequence.
239  
240 The length of the returned value always matches the length
241 of the sequence.
242  
243 Returns : reference to an array of meta data
244 Args : new value, string or array ref or FAST::Bio::Seq::PrimaryQual, optional
245  
246 Setting quality values resets the clear range.
247  
248 =cut
249  
250 sub qual {
251 my $self = shift;
252 my $value = shift;
253 $value = $value->qual
254 if ref($value) and ref($value) ne 'ARRAY' and
255 $value->isa('FAST::Bio::Seq::PrimaryQual');
256 $self->_empty_cache if $value;
257 return $self->named_meta($DEFAULT_NAME, $value);
258 }
259  
260 =head2 qual_text
261  
262 Title : qual_text
263 Usage : $qual_values = $obj->qual_text($values_arrayref);
264 Function: Variant of meta() and qual() guarantied to return a string
265 representation of meta data. For details, see L<meta>.
266 Returns : a string
267 Args : new value, optional
268  
269 =cut
270  
271 sub qual_text {
272 return join ' ', @{shift->named_submeta($DEFAULT_NAME, @_)};
273 }
274  
275 =head2 subqual
276  
277 Title : subqual
278 Usage : $subset_of_qual_values = $obj->subqual(10, 20, $value_string);
279 $subset_of_qual_values = $obj->subqual(10, undef, $value_string);
280 Function:
281  
282 Get and set method for meta data for subsequences.
283  
284 Numbering starts from 1 and the number is inclusive, ie 1-2
285 are the first two residue of the sequence. Start cannot be
286 larger than end but can be equal.
287  
288 If the second argument is missing the returned values
289 should extend to the end of the sequence.
290  
291 Returns : A reference to an array
292 Args : integer, start position
293 integer, end position, optional when a third argument present
294 new value, optional
295  
296 =cut
297  
298 sub subqual {
299 shift->named_submeta($DEFAULT_NAME, @_);
300 }
301  
302 =head2 subqual_text
303  
304 Title : subqual_text
305 Usage : $meta_values = $obj->subqual_text(20, $value_string);
306 Function: Variant of subqual() returning a stringified
307 representation of meta data. For details, see L<FAST::Bio::Seq::MetaI>.
308 Returns : a string
309 Args : new value, optional
310  
311 =cut
312  
313 sub subqual_text {
314 return join ' ', @{shift->named_submeta($DEFAULT_NAME, @_)};
315 }
316  
317 =head2 quality_length
318  
319 Title : quality_length()
320 Usage : $qual_len = $obj->quality_length();
321 Function: return the number of elements in the quality array
322 Returns : integer
323 Args : -
324  
325 =cut
326  
327 sub quality_length {
328 my ($self) = @_;
329 return $self->named_meta_length($DEFAULT_NAME);
330 }
331  
332 =head2 quality_is_flush
333  
334 Title : quality_is_flush
335 Usage : $quality_is_flush = $obj->quality_is_flush()
336 Function: Boolean to tell if the trace length equals the sequence length.
337 Returns true if force_flush() is set.
338 Returns : boolean 1 or 0
339 Args : none
340  
341 =cut
342  
343 sub quality_is_flush {
344 return shift->is_flush('quality');
345 }
346  
347  
348  
349 ## TRACE
350  
351 =head2 trace
352  
353 Title : trace
354 Usage : $trace_values = $obj->trace($values_string);
355 Function:
356  
357 Get and set method for the meta data starting from residue
358 position one. Since it is dependent on the length of the
359 sequence, it needs to be manipulated after the sequence.
360  
361 The length of the returned value always matches the length
362 of the sequence.
363  
364 Returns : reference to an array of meta data
365 Args : new value, string or array ref, optional
366  
367 =cut
368  
369 sub trace {
370 my $self = shift;
371 my $value = shift;
372 return $self->named_meta('trace', $value);
373 }
374  
375 =head2 trace_text
376  
377 Title : trace_text
378 Usage : $trace_values = $obj->trace_text($values_arrayref);
379 Function: Variant of meta() and trace() guarantied to return a string
380 representation of meta data. For details, see L<meta>.
381 Returns : a string
382 Args : new value, optional
383  
384 =cut
385  
386 sub trace_text {
387 return join ' ', @{shift->named_submeta('trace', @_)};
388 }
389  
390 =head2 subtrace
391  
392 Title : subtrace
393 Usage : $subset_of_trace_values = $obj->subtrace(10, 20, $value_string);
394 $subset_of_trace_values = $obj->subtrace(10, undef, $value_string);
395 Function:
396  
397 Get and set method for meta data for subsequences.
398  
399 Numbering starts from 1 and the number is inclusive, ie 1-2
400 are the first two residue of the sequence. Start cannot be
401 larger than end but can be equal.
402  
403 If the second argument is missing the returned values
404 should extend to the end of the sequence.
405  
406 Returns : A reference to an array
407 Args : integer, start position
408 integer, end position, optional when a third argument present
409 new value, optional
410  
411  
412 =cut
413  
414 sub subtrace {
415 return shift->named_submeta('trace', @_);
416 }
417  
418 =head2 subtrace_text
419  
420 Title : subtrace_text
421 Usage : $meta_values = $obj->subtrace_text(20, $value_string);
422 Function: Variant of subtrace() returning a stringified
423 representation of meta data. For details, see L<FAST::Bio::Seq::MetaI>.
424 Returns : a string
425 Args : new value, optional
426  
427 =cut
428  
429 sub subtrace_text {
430 return join ' ', @{shift->named_submeta('trace', @_)};
431 }
432  
433 =head2 trace_length
434  
435 Title : trace_length()
436 Usage : $trace_len = $obj->trace_length();
437 Function: return the number of elements in the trace set
438 Returns : integer
439 Args : -
440  
441 =cut
442  
443 sub trace_length {
444 my ($self) = @_;
445 return $self->named_meta_length('trace');
446 }
447  
448 =head2 trace_is_flush
449  
450 Title : trace_is_flush
451 Usage : $trace_is_flush = $obj->trace_is_flush()
452 Function: Boolean to tell if the trace length equals the sequence length.
453 Returns true if force_flush() is set.
454 Returns : boolean 1 or 0
455 Args : none
456  
457 =cut
458  
459 sub trace_is_flush {
460 return shift->is_flush('trace');
461 }
462  
463  
464  
465 =head2 get_trace_graph
466  
467 Title : get_trace_graph
468 Usage : @trace_values = $obj->get_trace_graph( -trace => 'a',
469 -scale => 100)
470 Function : Returns array of raw trace values for a trace file, or
471 false if no trace data exists. Requires a value for trace
472 to get either the a, g, c or t trace information, and an
473 optional value for scale, which rescales the data between
474 0 and the provided value, a scale value of '0' performs no
475 scaling
476 Returns : Array or 0
477 Args : string, trace to retrieve, one of a, g, c or t integer,
478 scale, for scaling of trace between 0 and scale, or 0 for
479 no scaling, optional
480  
481 =cut
482  
483 sub get_trace_graph
484 {
485 my $self = shift;
486 my($trace, $scale) =
487 $self->_rearrange([qw(TRACE
488 SCALE
489 )],
490 @_);
491 unless (defined($self->{trace_data})) { return 0 }
492 unless (grep { lc($trace) eq $_ } ('a', 'g', 'c', 't')) { return 0 }
493 $trace = lc($trace) . "_trace";
494 my @trace_data = exists $self->{trace_data}->{$trace} &&
495 ref $self->{trace_data}->{$trace} eq 'ARRAY' ?
496 @{$self->{trace_data}->{$trace}} : ();
497 my $max = $self->{trace_data}->{max_height};
498 if (defined($scale) and $scale != 0)
499 {
500 @trace_data = map { $_ / $max * $scale } @trace_data;
501 }
502 return @trace_data;
503 }
504  
505  
506 =head2 threshold
507  
508 Title : threshold
509 Usage : $qual->threshold($value);
510 Function: Sets the quality threshold.
511 Returns : an integer
512 Args : new value, optional
513  
514 Value used by *clear_range* method below.
515  
516 =cut
517  
518 sub threshold {
519 my $self = shift;
520 my $value = shift;
521 if (defined $value) {
522 $self->throw("Threshold needs to be an integer [$value]")
523 unless $value =~ /^[-+]?\d+$/;
524 $self->_empty_cache
525 if defined $self->{_threshold} and $self->{_threshold} ne $value;
526 $self->{_threshold} = $value;
527 }
528 return $self->{_threshold};
529 }
530  
531  
532 =head2 mask_below_threshold
533  
534 Title : mask_below_threshold
535 Usage : $count = $obj->count_clear_ranges($threshold);
536 Function: Counts number of ranges in the sequence where quality
537 values are above the threshold
538 Returns : count integer
539 Args : threshold integer, optional
540  
541 Set threshold first using method L<threshold>.
542  
543 =cut
544  
545 sub mask_below_threshold {
546 my $self = shift;
547 my $threshold = shift;
548  
549 $self->threshold($threshold) if defined $threshold;
550  
551 # populate the cache if needed
552 $self->_find_clear_ranges unless defined $self->{_ranges};
553  
554 my $maskSeq = $self->seq;
555 my $maskQual = $self->qual;
556  
557 ## There must be a more efficient way than this!
558 for(my $i=0; $i<length($maskSeq); $i++){
559 #print join ("\t", $i, $maskQual->[$i]), "\n";
560 substr($maskSeq, $i, 1, $MASK_CHAR)
561 if $maskQual->[$i] < $self->{_threshold};
562 }
563  
564 ## This is the *wrong* way to do it!
565 #for my $r (@{$self->{_ranges}} ){
566 # substr($maskSeq, $r->{start}, $r->{length}, $MASK_CHAR x $r->{length});
567 #}
568  
569 return $maskSeq;
570 }
571  
572 =head2 count_clear_ranges
573  
574 Title : count_clear_ranges
575 Usage : $count = $obj->count_clear_ranges($threshold);
576 Function: Counts number of ranges in the sequence where quality
577 values are above the threshold
578 Returns : count integer
579 Args : threshold integer, optional
580  
581 Set threshold first using method L<threshold>.
582  
583 =cut
584  
585 sub count_clear_ranges {
586 my $self = shift;
587 my $threshold = shift;
588 $self->threshold($threshold) if defined $threshold;
589  
590 # populate the cache if needed
591 $self->_find_clear_ranges unless defined $self->{_ranges};
592  
593 return scalar @{$self->{_ranges}};
594 }
595  
596 =head2 clear_ranges_length
597  
598 Title : clear_ranges_length
599 Usage : $total_lenght = $obj->clear_ranges_length($threshold);
600 Function: Return number of residues with quality values above
601 the threshold in all clear ranges
602 Returns : an integer
603 Args : threshold, optional
604  
605 Set threshold first using method L<threshold>.
606  
607 I think this method needs a better name! count_high_quality_bases? or
608 sum_clear_ranges?
609  
610 =cut
611  
612 sub clear_ranges_length {
613 my $self = shift;
614 my $threshold = shift;
615 $self->threshold($threshold) if defined $threshold;
616  
617 # populate the cache if needed
618 $self->_find_clear_ranges unless defined $self->{_ranges};
619  
620 my $sum;
621 map {$sum += $_->{length}} @{$self->{_ranges}};
622 return $sum;
623 }
624  
625 =head2 get_clear_range
626  
627 Title : get_clear_range
628 Usage : $newqualobj = $obj->get_clear_range($threshold);
629 Function: Return longest subsequence that has quality values above
630 the given threshold, or a default value of 13
631 Returns : a new FAST::Bio::Seq::Quality object
632 Args : threshold, optional
633  
634 Set threshold first using method L<threshold>.
635  
636 Note, this method could be implemented using some gaussian smoothing
637 of the quality scores. Currently one base below the threshold is
638 enough to end the clear range.
639  
640 =cut
641  
642 sub get_clear_range {
643 my $self = shift;
644 my $threshold = shift;
645 $self->threshold($threshold) if defined $threshold;
646  
647 # populate the cache if needed
648 $self->_find_clear_ranges unless defined $self->{_ranges};
649  
650 # fix for bug 2847
651 return unless defined $self->{_ranges};
652  
653 # pick the longest
654 for (sort {$b->{length} <=> $a->{length} } @{$self->{_ranges}} ){
655 my $newqualobj = FAST::Bio::Seq::Quality->new(
656 -seq => $self->subseq( $_->{start}, $_->{end}),
657 -qual => $self->subqual($_->{start}, $_->{end}),
658 -id => $self->id);
659
660 $newqualobj->threshold($threshold);
661
662 return $newqualobj;
663 }
664 }
665  
666  
667  
668 =head2 get_all_clean_ranges
669  
670 Title : get_all_clean_ranges
671 Usage : @ranges = $obj->get_all_clean_ranges($minlength);
672 Function: Return all ranges where quality values are above
673 the threshold. Original ordering.
674 Returns : an ordered array of new FAST::Bio::Seq::Quality objects
675 Args : minimum length , optional
676  
677 Set threshold first using method L<threshold>.
678  
679 =cut
680  
681 sub get_all_clean_ranges {
682 my $self = shift;
683 my $minl = shift || 0;
684  
685 $self->throw("Mimimum length needs to be zero or a positive integer [$minl]")
686 unless $minl =~ /^\+?\d+$/;
687  
688 # populate the cache if needed
689 $self->_find_clear_ranges unless defined $self->{_ranges};
690  
691 # return in the order of occurrence
692 my @ranges;
693 for my $r (sort {$b->{start} <=> $a->{start} } @{$self->{_ranges}} ){
694 next if $r->{length} < $minl;
695
696 ## Constructor should allow "-threshold => ..."!
697 push @ranges, FAST::Bio::Seq::Quality->new
698 ( -seq => $self->subseq( $r->{start}, $r->{end}),
699 -qual => $self->subqual($r->{start}, $r->{end}),
700 -id => $self->id
701 );
702 }
703 return @ranges;
704 }
705  
706  
707 #
708 # _find_clear_ranges: where range/threshold calculations happen
709 #
710  
711 sub _find_clear_ranges {
712 my $self = shift;
713 my $qual = $self->qual;
714
715 $self->throw("You need to set the threshold value first")
716 unless defined $self->threshold;
717
718 my $threshold = $self->threshold;
719
720 my $rangeFlag = 0;
721
722 for(my $i=0; $i<@$qual; $i++){
723 ## Are we currently within a clear range or not?
724 if($rangeFlag){
725 ## Did we just leave the clear range?
726 if($qual->[$i]<$threshold){
727 ## Log the range
728 my $range;
729 $range->{end} = $i-1;
730 $range->{start} = $rangeFlag;
731 $range->{length} = $i - $rangeFlag;
732 push @{$self->{_ranges}}, $range;
733 ## and reset the range flag.
734 $rangeFlag = 0;
735 }
736 ## else nothing changes
737 }
738 else{
739 ## Did we just enter a clear range?
740 if($qual->[$i]>=$threshold){
741 ## then set the range flag!
742 $rangeFlag = $i;
743 }
744 ## else nothing changes
745 }
746 }
747 ## Did we exit the last clear range?
748 if($rangeFlag){
749 my $i = scalar(@$qual);
750 ## Log the range
751 my $range;
752 $range->{end} = $i-1;
753 $range->{start} = $rangeFlag;
754 $range->{length} = $i - $rangeFlag;
755 push @{$self->{_ranges}}, $range;
756 }
757
758 1;
759 }
760  
761  
762 sub _empty_cache {
763 my $self = shift;
764 undef $self->{_ranges};
765 }
766  
767  
768  
769  
770 ################## deprecated methods ##################
771  
772  
773 sub trace_indices {
774 my $self = shift;
775 return $self->named_meta('trace');
776 }
777  
778 sub trace_index_at {
779 my ($self, $val) =@_;
780 return shift @{$self->named_submeta('trace', $val, $val)};
781 }
782  
783  
784 sub sub_trace_index {
785 my $self = shift;
786 return $self->named_submeta('trace', @_);
787 }
788  
789  
790 sub qualat {
791 my ($self, $val) =@_;
792 return shift @{$self->submeta($val, $val)};
793 }
794  
795  
796 sub baseat {
797 my ($self,$val) = @_;
798 return $self->subseq($val,$val);
799 }
800  
801  
802  
803 1;
804