Bio/Seq/SimulatedRead.pm

filter: Statementstmt 91.0% 224 / 246 Branchbran 81.5% 119 / 146 Conditioncond 56.8% 25 / 44 MC/DCmcdc 17.8% 5 / 28 Subroutinesub 100% 22 / 22 Podpod 100% 7 / 7 totaltotal 81.5% 402 / 493 CC 96 SCAR 25.0
CC
96
Cov
84%
_validate_error_specs
34.8
errors
17.5
reference
11.6
CRAP 12.2
« Bio/Search/Result/PullResultI.pm ↑ Summary Bio/Tree/Compatible.pm »
1 package Bio::Seq::SimulatedRead;
2 2 $Bio::Seq::SimulatedRead::VERSION = '1.7.8';
3  
4 =head1 NAME
5  
6 Bio::Seq::SimulatedRead - Read with sequencing errors taken from a reference sequence
7  
8 =head1 SYNOPSIS
9  
10 use Bio::Seq::SimulatedRead;
11 use Bio::PrimarySeq;
12
13 # Create a reference sequence
14 my $genome = Bio::PrimarySeq->new( -id => 'human_chr2',
15 -seq => 'TAAAAAAACCCCTG',
16 -desc => 'The human genome' );
17  
18 # A 10-bp error-free read taken from a genome
19 my $read = Bio::Seq::SimulatedRead->new(
20 -reference => $genome , # sequence to generate the read from
21 -id => 'read001', # read ID
22 -start => 3 , # start of the read on the genome forward strand
23 -end => 12 , # end of the read on the genome forward strand
24 -strand => 1 , # genome strand that the read is on
25 );
26  
27 # Display the sequence of the read
28 print $read->seq."\n";
29  
30 # Add a tag or MID to the beginning of the read
31 $read->mid('ACGT');
32  
33 # Add sequencing errors (error positions are 1-based and relative to the
34 # error-free MID-containing read)
35 my $errors = {};
36 $errors->{'8'}->{'+'} = 'AAA'; # insertion of AAA after residue 8
37 $errors->{'1'}->{'%'} = 'G'; # substitution of residue 1 by a G
38 $errors->{'4'}->{'-'} = undef; # deletion of residue 4
39 $read->errors($errors);
40  
41 # Display the sequence of the read with errors
42 print $read->seq."\n";
43  
44 # String representation of where the read came from and its errors
45 print $read->desc."\n";
46  
47 =head1 DESCRIPTION
48  
49 This object is a simulated read with sequencing errors. The user can provide a
50 reference sequence to take a read from, the position and orientation of the
51 read on the reference sequence, and the sequencing errors to generate.
52  
53 The sequence of the read is automatically calculated based on this information.
54 By default, the description of the reads contain tracking information and will
55 look like this (Bioperl-style):
56  
57 reference=human_chr2 start=3 end=12 strand=-1 mid=ACGT errors=1%G,4-,8+AAA description="The human genome"
58  
59 or Genbank-style:
60  
61 reference=human_chr2 position=complement(3..12) mid=ACGT errors=1%G,4-,8+AAA description="The human genome"
62  
63 Creating a simulated read follows these steps:
64 1/ Define the read start(), end(), strand() and qual_levels() if you want
65 quality scores to be generated. Do not change these values once set because
66 the read will not be updated.
67 2/ Specify the reference sequence that the read should be taken from. Once
68 this is done, you have a fully functional read. Do not use the reference()
69 method again after you have gone to the next step.
70 3/ Use mid() to input a MID (or tag or barcode) to add to the beginning of the
71 read. You can change the MID until you go to next step.
72 4/ Give sequencing error specifications using errors() as the last step. You
73 can do that as many times as you like, and the read will be updated.
74  
75 =head1 AUTHOR
76  
77 Florent E Angly E<lt>florent . angly @ gmail-dot-comE<gt>.
78  
79 Copyright (c) 2011 Florent E Angly.
80  
81 This library is free software; you can redistribute it under the GNU General
82 Public License version 3.
83  
84 =cut
85  
86  
87 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use Bio::LocatableSeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use base qw( Bio::Seq::Quality Bio::LocatableSeq );
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92  
93 =head2 new
94  
95 Title : new
96 Function : Create a new simulated read object
97 Usage : my $read = Bio::Seq::SimulatedRead->new(
98 -id => 'read001',
99 -reference => $seq_obj ,
100 -errors => $errors ,
101 -start => 10 ,
102 -end => 135 ,
103 -strand => 1 ,
104 );
105 Arguments: -reference => Bio::SeqI, Bio::PrimarySeqI object representing the
106 reference sequence to take the read from. See
107 reference().
108 -errors => Hashref representing the position of errors in the read
109 See errors().
110 -mid => String of a MID to prepend to the read. See mid().
111 -track => Track where the read came from in the read description?
112 See track().
113 -coord_style => Define what coordinate system to use. See coord_style().
114 All other methods from Bio::LocatableSeq are available.
115 Returns : new Bio::Seq::SimulatedRead object
116  
117 =cut
118  
119 29 ▶sub new {
Subroutine new: called CC=8 SCAR=20.8
Pod documented
120 29 my ($class, @args) = @_;
121 29 my $self = $class->SUPER::new(@args);
122 29 my ($qual_levels, $reference, $mid, $errors, $track, $coord_style) =
123 $self->_rearrange([qw(QUAL_LEVELS REFERENCE MID ERRORS TRACK COORD_STYLE)], @args);
124 29 ▶ $coord_style = defined $coord_style ? $coord_style : 'bioperl';
Branch: 1 branch
BranchTrueFalseTotal
defined $coord_style ? : 4 25 29
125 29 $self->coord_style($coord_style);
126 29 ▶ $track = defined $track ? $track : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $track ? : 4 25 29
127 29 $self->track($track);
128 29 ▶ $qual_levels = defined $qual_levels ? $qual_levels : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $qual_levels ? : 14 15 29
129 29 ▶ $self->qual_levels($qual_levels) if defined $qual_levels;
Branch: 1 branch
BranchTrueFalseTotal
if defined $qual_levels 29 0 29
130 29 ▶ $self->reference($reference) if defined $reference;
Branch: 1 branch
BranchTrueFalseTotal
if defined $reference 26 3 29
131 29 ▶ $self->mid($mid) if defined $mid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mid 5 24 29
132 29 $self->{_mutated} = 0;
133 29 ▶ $self->errors($errors) if defined $errors;
Branch: 1 branch
BranchTrueFalseTotal
if defined $errors 7 22 29
134 29 return $self;
135 }
136  
137  
138 =head2 qual_levels
139  
140 Title : qual_levels
141 Function : Get or set the quality scores to give to the read. By default, if your
142 reference sequence does not have quality scores, no quality scores
143 are generated for the simulated read. The generated quality scores
144 are very basic. If a residue is error-free, it gets the quality score
145 defined for good residues. If the residue has an error (is an
146 addition or a mutation), the residue gets the quality score specified
147 for bad residues. Call the qual_levels() method before using the
148 reference() method.
149 Usage : my $qual_levels = $read->qual_levels( );
150 Arguments: Array reference containing the quality scores to use for:
151 1/ good residues (e.g. 30)
152 2/ bad residues (e.g. 10)
153 Returns : Array reference containing the quality scores to use.
154  
155 =cut
156  
157 102 ▶sub qual_levels {
Subroutine qual_levels: called CC=4 SCAR=14.3
Pod documented
158 102 my ($self, $qual_levels) = @_;
159 102 ▶ if (defined $qual_levels) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $qual_levels) 31 71 102
160 31 ▶ if ( (scalar @$qual_levels != 0) && (scalar @$qual_levels != 2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$qual_levels != 0 and scalar @$qual_levels != 2) 0 31 31
Condition66%2/3
scalar @$qual_levels != 0 and scalar @$qual_levels != 2
!ll&&!rl&&r
18130
MC/DC0%0/2
scalar @$qual_levels != 0 and scalar @$qual_levels != 2
scalar @$qual_levels != 0 scalar @$qual_levels != 2
Truth table66%2/3
A and B
A= scalar @$qual_levels != 0B= scalar @$qual_levels != 2
ABresult
0X0
100
111
161 0 $self->throw("The quality score specification must define the score".
162 " to use for good and for bad residues\n");
163 }
164 31 $self->{qual_levels} = $qual_levels;
165 }
166 102 return $self->{qual_levels};
167 }
168  
169  
170 =head2 reference
171  
172 Title : reference
173 Function : Get or set the reference sequence that the read comes from. Once the
174 reference has been set, you have a functional simulated read which
175 supports all the Bio::LocatableSeq methods. This method must be
176 called after qual_levels() but before mid() or errors().
177 Usage : my $seq_obj = $read->reference();
178 Arguments: Bio::SeqI or Bio::PrimarySeqI object
179 Returns : Bio::SeqI or Bio::PrimarySeqI object
180  
181 =cut
182  
183 217 ▶sub reference {
Subroutine reference: called CC=11 SCAR=24.6
Pod documented
184 217 my ($self, $reference) = @_;
185 217 ▶ if (defined $reference) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reference) 28 189 217
186 # Sanity check 1
187 28 ▶ if ( (not $reference->isa('Bio::SeqI')) && (not $reference->isa('Bio::PrimarySeqI')) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reference->isa("Bio::SeqI") or $reference->isa("Bio::PrimarySeqI")) 0 28 28
Condition66%2/3
$reference->isa("Bio::SeqI") or $reference->isa("Bio::PrimarySeqI")
l!l&&r!l&&!r
2530
MC/DC0%0/2
$reference->isa("Bio::SeqI") or $reference->isa("Bio::PrimarySeqI")
$reference->isa("Bio::SeqI") $reference->isa("Bio::PrimarySeqI")
Truth table66%2/3
A or B
A= $reference->isa("Bio::SeqI")B= $reference->isa("Bio::PrimarySeqI")
ABresult
1X1
011
000
188 0 $self->throw("Expected a Bio::SeqI object as reference, but got: $reference\n");
189 }
190 # Sanity check 2
191 28 ▶ if ($self->{mid} || $self->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mid"} or $self->{"errors"}) 0 28 28
Condition33%1/3
$self->{"mid"} or $self->{"errors"}
l!l&&r!l&&!r
0028
MC/DC0%0/2
$self->{"mid"} or $self->{"errors"}
$self->{"mid"} $self->{"errors"}
Truth table33%1/3
A or B
A= $self->{"mid"}B= $self->{"errors"}
ABresult
1X1
011
000
192 0 $self->throw("Cannot change the reference sequence after an MID or ".
193 "sequencing errors have been added to the read\n");
194 }
195 # Use beginning of reference sequence as start default
196 28 ▶ if (not defined $self->start) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->start) 23 5 28
197 23 $self->start(1);
198 }
199 # Use end of reference sequence as end default
200 28 ▶ if (not defined $self->end) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->end) 23 5 28
201 23 $self->end($reference->length);
202 }
203 # Use strand 1 as strand default
204 28 ▶ if (not defined $self->strand) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->strand) 22 6 28
205 22 $self->strand(1);
206 }
207 # Set the reference sequence object
208 28 $self->{reference} = $reference;
209 # Create a sequence, quality scores and description from the reference
210 28 $self->_create_seq;
211 28 ▶ $self->_create_qual if scalar @{$self->qual_levels};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->qual_levels;} 13 15 28
212 28 ▶ $self->_create_desc if $self->track;
Branch: 1 branch
BranchTrueFalseTotal
if $self->track 27 1 28
213 }
214 217 return $self->{reference};
215 }
216  
217  
218 28 ▶sub _create_seq {
Subroutine _create_seq: called CC=2 SCAR=6.9
219 28 my $self = shift;
220 # Get a truncation of the reference sequence
221 28 my $reference = $self->reference;
222 28 my $read_obj = $reference->trunc( $self->start, $self->end );
223 # Reverse complement the read if needed
224 28 ▶ if ($self->strand == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strand == -1) 4 24 28
225 4 $read_obj = $read_obj->revcom();
226 }
227 28 $self->seq($read_obj->seq);
228 28 return 1;
229 }
230  
231  
232 13 ▶sub _create_qual {
Subroutine _create_qual: called CC=1 SCAR=0.0
233 13 my $self = shift;
234 13 $self->qual([ ($self->qual_levels->[0]) x ($self->end - $self->start + 1) ]);
235 13 return 1;
236 }
237  
238  
239 28 ▶sub _create_desc {
Subroutine _create_desc: called CC=9 SCAR=22.0
240 # Create the read description of the error-free read
241 28 my $self = shift;
242 # Reference sequence ID
243 28 my $desc_str = '';
244 28 my $ref_id = $self->reference->id;
245 28 ▶ if (defined $ref_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref_id) 26 2 28
246 26 $desc_str .= 'reference='.$ref_id.' ';
247 }
248 # Position of read on reference sequence: start, end and strand
249 28 my $strand = $self->strand;
250 28 ▶ if ($self->coord_style eq 'bioperl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->coord_style eq 'bioperl') { } 24 4 28
251 24 $desc_str .= 'start='.$self->start.' end='.$self->end.' ';
252 24 ▶ if (defined $strand) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $strand) 24 0 24
253 # Strand of the reference sequence that the read is on
254 24 ▶ $strand = '+1' if $strand == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $strand == 1 21 3 24
255 24 $desc_str .= 'strand='.$strand.' ';
256 }
257 } else {
258 4 ▶ if ( (defined $strand) && ($strand == -1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $strand and $strand == -1) { } 1 3 4
Condition66%2/3
defined $strand and $strand == -1
!ll&&!rl&&r
031
MC/DC50%1/2
defined $strand and $strand == -1
defined $strand $strand == -1
Truth table66%2/3
A and B
A= defined $strandB= $strand == -1
ABresult
0X0
100
111
259 # Reverse complemented
260 1 $desc_str .= 'position=complement('.$self->start.'..'.$self->end.') ';
261 } else {
262 # Regular (forward) orientation
263 3 $desc_str .= 'position='.$self->start.'..'.$self->end.' ';
264 }
265 }
266 # Description of the original sequence
267 28 my $ref_desc = $self->reference->desc;
268 28 ▶ if ( (defined $self->reference->desc) && ($self->reference->desc !~ m/^\s*$/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->reference->desc and not $self->reference->desc =~ /^\s*$/) 25 3 28
Condition66%2/3
defined $self->reference->desc and not $self->reference->desc =~ /^\s*$/
!ll&&!rl&&r
3025
MC/DC50%1/2
defined $self->reference->desc and not $self->reference->desc =~ /^\s*$/
defined $self->reference->desc not $self->reference->desc =~ /^\s*$/
Truth table66%2/3
A and not(B)
A= defined $self->reference->descB= not $self->reference->desc =~ /^\s*$/
ABresult
0X0
100
111
269 25 $ref_desc =~ s/"/\\"/g; # escape double-quotes to \"
270 25 $desc_str .= 'description="'.$ref_desc.'" ';
271 }
272 28 $desc_str =~ s/\s$//g;
273 # Record new description
274 28 $self->desc($desc_str);
275 28 return 1;
276 }
277  
278  
279 =head2 mid
280  
281 Title : mid
282 Function : Get or set a multiplex identifier (or MID, or tag, or barcode) to
283 add to the read. By default, no MID is used. This method must be
284 called after reference() but before errors().
285 Usage : my $mid = read->mid();
286 Arguments: MID sequence string (e.g. 'ACGT')
287 Returns : MID sequence string
288  
289 =cut
290  
291 11 ▶sub mid {
Subroutine mid: called CC=7 SCAR=20.7
Pod documented
292 11 my ($self, $mid) = @_;
293 11 ▶ if (defined $mid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mid) 8 3 11
294 # Sanity check 1
295 8 ▶ if (not defined $self->reference) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->reference) 0 8 8
296 0 $self->throw("Cannot add MID because the reference sequence was not ".
297 "set\n");
298 }
299 # Sanity check 2
300 8 ▶ if ($self->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"errors"}) 0 8 8
301 0 $self->throw("Cannot add an MID after sequencing errors have been ".
302 "introduced in the read\n");
303 }
304 # Sanity check 3
305 8 ▶ if (not $self->validate_seq($mid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->validate_seq($mid)) 0 8 8
306 0 $self->throw("MID is not a valid DNA sequence\n");
307 }
308 # Update sequence, quality scores and description with the MID
309 8 $self->_update_seq_mid($mid);
310 8 ▶ $self->_update_qual_mid($mid) if scalar @{$self->qual_levels};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->qual_levels;} 4 4 8
311 8 ▶ $self->_update_desc_mid($mid) if $self->track;
Branch: 1 branch
BranchTrueFalseTotal
if $self->track 8 0 8
312 # Set the MID value
313 8 $self->{mid} = $mid;
314 }
315 return $self->{mid}
316 11 }
317  
318  
319 8 ▶sub _update_seq_mid {
Subroutine _update_seq_mid: called CC=2 SCAR=6.9
320 # Update the MID of a sequence
321 8 my ($self, $mid) = @_;
322 # Remove old MID
323 8 my $seq = $self->seq;
324 8 my $old_mid = $self->{mid};
325 8 ▶ if (defined $old_mid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_mid) 2 6 8
326 2 $seq =~ s/^$old_mid//;
327 }
328 # Add new MID
329 8 $seq = $mid . $seq;
330 8 $self->seq( $seq );
331 8 return 1;
332 }
333  
334  
335 4 ▶sub _update_qual_mid {
Subroutine _update_qual_mid: called CC=2 SCAR=6.9
336 # Update the MID of a quality scores
337 4 my ($self, $mid) = @_;
338 # Remove old MID
339 4 my $qual = $self->qual;
340 4 my $old_mid = $self->{mid};
341 4 ▶ if (defined $old_mid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_mid) 1 3 4
342 1 splice @$qual, 0, length($old_mid);
343 }
344 4 $qual = [($self->qual_levels->[0]) x length($mid), @$qual];
345 4 $self->qual( $qual );
346 4 return 1;
347 }
348  
349  
350 9 ▶sub _update_desc_mid {
Subroutine _update_desc_mid: called CC=2 SCAR=6.9
351 # Update MID specifications in the read description
352 9 my ($self, $mid) = @_;
353 9 ▶ if ($mid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid) 7 2 9
354 # Sequencing errors introduced in the read
355 7 my $mid_str = "mid=".$mid;
356 7 my $desc_str = $self->desc;
357 7 $desc_str =~ s/((position|strand)=\S+)( mid=\S+)?/$1 $mid_str/g;
358 7 $self->desc( $desc_str );
359 }
360 9 return 1;
361 }
362  
363  
364 =head2 errors
365  
366 Title : errors
367 Function : Get or set the sequencing errors and update the read. By default, no
368 errors are made. This method must be called after the mid() method.
369 Usage : my $errors = $read->errors();
370 Arguments: Reference to a hash of the position and nature of sequencing errors.
371 The positions are 1-based relative to the error-free MID-containing
372 read (not relative to the reference sequence). For example:
373 $errors->{34}->{'%'} = 'T' ; # substitution of residue 34 by a T
374 $errors->{23}->{'+'} = 'GG' ; # insertion of GG after residue 23
375 $errors->{45}->{'-'} = undef; # deletion of residue 45
376 Substitutions and deletions are for a single residue, but additions
377 can be additions of several residues.
378 An alternative way to specify errors is by using array references
379 instead of scalar for the hash values. This allows one to specify
380 redundant mutations. For example, the case presented above would
381 result in the same read sequence as the example below:
382 $errors->{34}->{'%'} = ['C', 'T'] ; # substitution by a C and then a T
383 $errors->{23}->{'+'} = ['G', 'G'] ; # insertion of G and then a G
384 $errors->{45}->{'-'} = [undef, undef]; # deletion of residue, and again
385 Returns : Reference to a hash of the position and nature of sequencing errors.
386  
387 =cut
388  
389 42 ▶sub errors {
Subroutine errors: called CC=10 SCAR=28.6
Pod documented
390 42 my ($self, $errors) = @_;
391 42 ▶ if (defined $errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $errors) 11 31 42
392 # Verify that we have a hashref
393 11 ▶ if ( (not defined ref $errors) || (not ref $errors eq 'HASH') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ref $errors and ref $errors eq "HASH") 0 11 11
Condition33%1/3
defined ref $errors and ref $errors eq "HASH"
!ll&&!rl&&r
0011
MC/DC0%0/2
defined ref $errors and ref $errors eq "HASH"
defined ref $errors ref $errors eq "HASH"
Truth table33%1/3
A and B
A= defined ref $errorsB= ref $errors eq "HASH"
ABresult
0X0
100
111
394 0 $self->throw("Error specification has to be a hashref. Got: $errors\n");
395 }
396 # Verify that we have a reference sequence
397 11 ▶ if (not defined $self->reference) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->reference) 0 11 11
398 0 $self->throw("Cannot add errors because the reference sequence was not set\n");
399 }
400 # Convert scalar error specs to arrayref specs
401 11 $errors = $self->_scalar_to_arrayref($errors);
402 # Check validity of error specifications
403 11 $errors = $self->_validate_error_specs($errors);
404 # Set the error specifications
405 11 $self->{errors} = $errors;
406 # Need to recalculate the read from the reference if previously mutated
407 11 ▶ if ($self->{_mutated}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_mutated"}) 0 11 11
408 0 $self->_create_seq;
409 0 ▶ $self->_create_qual if scalar @{$self->qual_levels};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->qual_levels;} 0 0 0
410 0 ▶ $self->_create_desc if $self->track;
Branch: 1 branch
BranchTrueFalseTotal
if $self->track 0 0 0
411 }
412 # Now mutate the read, quality score and description
413 11 $self->_update_seq_errors;
414 11 ▶ $self->_update_qual_errors if scalar @{$self->qual_levels};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->qual_levels;} 6 5 11
415 11 ▶ $self->_update_desc_errors if $self->track;
Branch: 1 branch
BranchTrueFalseTotal
if $self->track 11 0 11
416  
417 }
418 42 return $self->{errors};
419 }
420  
421  
422 11 ▶sub _scalar_to_arrayref {
Subroutine _scalar_to_arrayref: called CC=6 SCAR=17.9
423 # Replace the scalar values in the error specs by more versatile arrayrefs
424 11 my ($self, $errors) = @_;
425 11 ▶ while ( my ($pos, $ops) = each %$errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$errors) 14 11 25
426 14 ▶ while ( my ($op, $res) = each %$ops ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ops) 14 14 28
427 14 ▶ if (ref $res eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "") 10 4 14
428 10 ▶ my $arr = [ split //, ($res || '') ];
Condition100%2/2
$res || ''
l!l
82
MC/DC100%1/1
$res || ''
$res
Truth table100%2/2
A
A= $res
Aresult
11
00
429 10 ▶ $arr = [undef] if scalar @$arr == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$arr == 0 2 8 10
430 10 $$errors{$pos}{$op} = $arr;
431 }
432 }
433 }
434 11 return $errors;
435 }
436  
437  
438 11 ▶sub _validate_error_specs {
Subroutine _validate_error_specs: called CC=18 SCAR=35.5
439 # Clean error specifications and warn of any issues encountered
440 11 my ($self, $errors) = @_;
441 11 my %valid_ops = ('%' => undef, '-' => undef, '+' => undef); # valid operations
442  
443 # Calculate read length
444 11 my $read_length = $self->length;
445 11 ▶ while ( my ($pos, $ops) = each %$errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$errors) 14 11 25
446  
447 # Position cannot be no longer than the read length
448 14 ▶ if ( (defined $read_length) && ($pos > $read_length) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_length and $pos > $read_length) 0 14 14
Condition33%1/3
defined $read_length and $pos > $read_length
!ll&&!rl&&r
0140
MC/DC0%0/2
defined $read_length and $pos > $read_length
defined $read_length $pos > $read_length
Truth table33%1/3
A and B
A= defined $read_lengthB= $pos > $read_length
ABresult
0X0
100
111
449 0 $self->warn("Position $pos is beyond end of read ($read_length ".
450 "residues). Skipping errors specified at this position.\n");
451 0 delete $errors->{$pos};
452 }
453  
454 # Position has to be 0+ for addition, 1+ for substitution and deletion
455 14 ▶ if ( $pos < 1 && (exists $ops->{'%'} || exists $ops->{'-'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 1 and exists $ops->{"%"} || exists $ops->{"-"}) 1 13 14
Condition50%3/6
$pos < 1 and exists $ops->{"%"} || exists $ops->{"-"}
!ll&&!rl&&r
1301
exists $ops->{"%"} or exists $ops->{"-"}
l!l&&r!l&&!r
010
MC/DC33%1/3
$pos < 1 and exists $ops->{"%"} || exists $ops->{"-"}
$pos < 1 exists $ops->{"%"} exists $ops->{"-"}
Truth table50%2/4
A and B or C
A= $pos < 1B= exists $ops->{"%"}C= exists $ops->{"-"}
ABCresult
0XX0
1000
11X1
1011
456 1 $self->warn("Positions of substitutions and deletions have to be ".
457 "strictly positive but got $pos. Skipping substitution or deletion".
458 " at this position\n");
459 1 delete $ops->{'%'};
460 1 delete $ops->{'-'};
461 }
462 14 ▶ if ( $pos < 0 && exists $ops->{'+'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0 and exists $ops->{"+"}) 0 14 14
Condition33%1/3
$pos < 0 and exists $ops->{"+"}
!ll&&!rl&&r
1400
MC/DC0%0/2
$pos < 0 and exists $ops->{"+"}
$pos < 0 exists $ops->{"+"}
Truth table33%1/3
A and B
A= $pos < 0B= exists $ops->{"+"}
ABresult
0X0
100
111
463 0 $self->warn("Positions of additions have to be zero or more. ".
464 "Skipping addition at position $pos.\n");
465 0 delete $ops->{'+'};
466 }
467  
468 # Valid operations are '%', '+' and '-'
469 14 ▶ while ( my ($op, $res) = each %$ops ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ops) 13 14 27
470 13 ▶ if (not exists $valid_ops{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $valid_ops{$op}) { } 0 13 13
471 0 $self->warn("Skipping unknown error operation '$op' at position".
472 " $pos\n");
473 0 delete $ops->{$op};
474 } else {
475 # Substitutions: have to have at least one residue to substitute
476 13 ▶ if ( ($op eq '%') && (scalar @$res < 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "%" and scalar @$res < 1) 0 13 13
Condition66%2/3
$op eq "%" and scalar @$res < 1
!ll&&!rl&&r
1030
MC/DC0%0/2
$op eq "%" and scalar @$res < 1
$op eq "%" scalar @$res < 1
Truth table66%2/3
A and B
A= $op eq "%"B= scalar @$res < 1
ABresult
0X0
100
111
477 0 $self->warn("At least one residue must be provided for substitutions,".
478 "but got ".scalar(@$res)." at position $pos.\n");
479 }
480 # Additions: have to have at least one residue to add
481 13 ▶ if ( ($op eq '+') && (scalar @$res < 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "+" and scalar @$res < 1) 0 13 13
Condition66%2/3
$op eq "+" and scalar @$res < 1
!ll&&!rl&&r
580
MC/DC0%0/2
$op eq "+" and scalar @$res < 1
$op eq "+" scalar @$res < 1
Truth table66%2/3
A and B
A= $op eq "+"B= scalar @$res < 1
ABresult
0X0
100
111
482 0 $self->warn("At least one residue must be provided for additions,".
483 "but got ".scalar(@$res)." at position $pos.\n");
484 }
485 # Deletions
486 13 ▶ if ( ($op eq '-') && (scalar @$res < 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "-" and scalar @$res < 1) 0 13 13
Condition66%2/3
$op eq "-" and scalar @$res < 1
!ll&&!rl&&r
1120
MC/DC0%0/2
$op eq "-" and scalar @$res < 1
$op eq "-" scalar @$res < 1
Truth table66%2/3
A and B
A= $op eq "-"B= scalar @$res < 1
ABresult
0X0
100
111
487 0 $self->warn("At least one 'undef' must be provided for deletions,".
488 "but got ".scalar(@$res)." at position $pos.\n");
489 }
490 }
491 }
492  
493 14 ▶ delete $errors->{$pos} unless scalar keys %$ops;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$ops 1 13 14
494 }
495  
496 11 return $errors;
497 }
498  
499  
500 11 ▶sub _update_seq_errors {
Subroutine _update_seq_errors: called CC=8 SCAR=20.8
501 11 my $self = shift;
502 11 my $seq_str = $self->seq;
503 11 my $errors = $self->errors;
504 11 ▶ if (scalar keys %$errors > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$errors > 0) { } 9 2 11
505 9 my $off = 0;
506 9 for my $pos ( sort {$a <=> $b} (keys %$errors) ) {
507 # Process sequencing errors at that position
508 13 for my $type ( '%', '-', '+' ) {
509 39 ▶ next if not exists $$errors{$pos}{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $errors->{$pos}{$type} 26 13 39
510 13 my $arr = $$errors{$pos}{$type};
511 13 ▶ if ($type eq '%') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq '%') { } 3 10 13
elsif ($type eq '-') { } 2 8 10
elsif ($type eq '+') { } 8 0 8
512 # Substitution at residue position. If there are multiple
513 # substitutions to do, directly skip to the last one.
514 3 substr $seq_str, $pos - 1 + $off, 1, $$arr[-1];
515 } elsif ($type eq '-') {
516 # Deletion at residue position
517 2 substr $seq_str, $pos - 1 + $off, 1, '';
518 2 $off--;
519 } elsif ($type eq '+') {
520 # Insertion after residue position
521 8 substr $seq_str, $pos + $off, 0, join('', @$arr);
522 8 $off += scalar @$arr;
523 }
524 }
525 }
526 9 $self->{_mutated} = 1;
527 } else {
528 2 $self->{_mutated} = 0;
529 }
530 11 $self->seq($seq_str);
531 11 return 1;
532 }
533  
534  
535 6 ▶sub _update_qual_errors {
Subroutine _update_qual_errors: called CC=8 SCAR=20.8
536 6 my $self = shift;
537 6 my $qual = $self->qual;
538 6 my $errors = $self->errors;
539 6 my $bad_qual = $self->qual_levels->[1];
540 6 ▶ if (scalar keys %$errors > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$errors > 0) 6 0 6
541 6 my $off = 0;
542 6 for my $pos ( sort {$a <=> $b} (keys %$errors) ) {
543 # Process sequencing errors at that position
544 10 for my $type ( '%', '-', '+' ) {
545 30 ▶ next if not exists $$errors{$pos}{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $errors->{$pos}{$type} 20 10 30
546 10 my $arr = $$errors{$pos}{$type};
547 10 ▶ if ($type eq '%') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq '%') { } 3 7 10
elsif ($type eq '-') { } 2 5 7
elsif ($type eq '+') { } 5 0 5
548 # Substitution at residue position
549 3 splice @$qual, $pos - 1 + $off, 1, $bad_qual;
550 } elsif ($type eq '-') {
551 # Deletion at residue position
552 2 splice @$qual, $pos - 1 + $off, 1;
553 2 $off--;
554 } elsif ($type eq '+') {
555 # Insertion after residue position
556 5 splice @$qual, $pos + $off, 0, ($bad_qual) x scalar(@$arr);
557 5 $off += scalar @$arr;
558 }
559 }
560 }
561 }
562 6 $self->qual($qual);
563 6 return 1;
564 }
565  
566  
567 12 ▶sub _update_desc_errors {
Subroutine _update_desc_errors: called CC=8 SCAR=20.8
568 # Add or update error specifications in the read description
569 12 my $self = shift;
570 12 my $errors = $self->errors;
571 12 ▶ if (defined $errors and scalar keys %$errors > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $errors and scalar keys %$errors > 0) 10 2 12
Condition66%2/3
defined $errors and scalar keys %$errors > 0
!ll&&!rl&&r
0210
MC/DC50%1/2
defined $errors and scalar keys %$errors > 0
defined $errors scalar keys %$errors > 0
Truth table66%2/3
A and B
A= defined $errorsB= scalar keys %$errors > 0
ABresult
0X0
100
111
572 # Sequencing errors introduced in the read
573 10 my $err_str = 'errors=';
574 10 for my $pos ( sort {$a <=> $b} (keys %$errors) ) {
575 # Process sequencing errors at that position
576 14 for my $type ( '%', '-', '+' ) {
577 42 ▶ next if not exists $$errors{$pos}{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $errors->{$pos}{$type} 28 14 42
578 14 for my $val ( @{$$errors{$pos}{$type}} ) {
579 25 ▶ $val = '' if not defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 3 22 25
580 25 $err_str .= $pos . $type . $val . ',';
581 }
582 }
583 }
584 10 $err_str =~ s/,$//;
585 10 my $desc_str = $self->desc;
586 10 $desc_str =~ s/((position|strand)=\S+( mid=\S+)?)( errors=\S+)?/$1 $err_str/g;
587 10 $self->desc( $desc_str );
588 }
589 12 return 1;
590 }
591  
592  
593 =head2 track
594  
595 Title : track
596 Function : Get or set the tracking status in the read description. By default,
597 tracking is on. This method can be called at any time.
598 Usage : my $track = $read->track();
599 Arguments: 1 for tracking, 0 otherwise
600 Returns : 1 for tracking, 0 otherwise
601  
602 =cut
603  
604 83 ▶sub track {
Subroutine track: called CC=4 SCAR=13.9
Pod documented
605 83 my ($self, $track) = @_;
606 83 ▶ if (defined $track) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $track) 31 52 83
607 31 ▶ if (defined $self->reference) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->reference) 2 29 31
608 2 ▶ if ($track == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($track == 1) { } 1 1 2
609 1 $self->_create_desc;
610 1 $self->_update_desc_mid($self->mid);
611 1 $self->_update_desc_errors;
612 } else {
613 1 $self->desc(undef);
614 }
615 }
616 31 $self->{track} = $track;
617 }
618 83 return $self->{track};
619 }
620  
621  
622 =head2 coord_style
623  
624 Title : coord_style
625 Function : When tracking is on, define which 1-based coordinate system to use
626 in the read description:
627 * 'bioperl' uses the start, end and strand keywords (default),
628 similarly to the GFF3 format. Example:
629 start=1 end=10 strand=+1
630 start=1 end=10 strand=-1
631 * 'genbank' does only provide the position keyword. Example:
632 position=1..10
633 position=complement(1..10)
634 Usage : my $coord_style = $read->track();
635 Arguments: 'bioperl' or 'genbank'
636 Returns : 'bioperl' or 'genbank'
637  
638 =cut
639  
640 57 ▶sub coord_style {
Subroutine coord_style: called CC=3 SCAR=11.2
Pod documented
641 57 my ($self, $coord_style) = @_;
642 57 my %styles = ( 'bioperl' => undef, 'genbank' => undef );
643 57 ▶ if (defined $coord_style) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $coord_style) 29 28 57
644 29 ▶ if (not exists $styles{$coord_style}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $styles{$coord_style}) 0 29 29
645 0 die "Error: Invalid coordinate style '$coord_style'\n";
646 }
647 29 $self->{coord_style} = $coord_style;
648 }
649 57 return $self->{coord_style};
650 }
651  
652  
653 2 1;
« Bio/Search/Result/PullResultI.pm ↑ Summary Bio/Tree/Compatible.pm »