Bio/SeqIO/fastq.pm

filter: Statementstmt 83.6% 123 / 147 Branchbran 70.2% 52 / 74 Conditioncond 79.4% 31 / 39 MC/DCmcdc 70.8% 17 / 24 Subroutinesub 69.2% 9 / 13 Podpod 100% 8 / 8 totaltotal 78.6% 240 / 305 CC 56 SCAR 27.9
CC
56
Cov
79%
next_dataset
25.4
write_seq
20.7
quality_header
12.0
CRAP 16.4
« Bio/Search/Hit/HitI.pm ↑ Summary Bio/SeqIO/gcg.pm »
1 # POD at __END__, let the code begin...
2  
3 package Bio::SeqIO::fastq;
4 3 $Bio::SeqIO::fastq::VERSION = '1.7.8';
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Bio::Seq::SeqFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use base qw(Bio::SeqIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our %variant = (
12 sanger => {
13 'offset' => 33,
14 'qual_start' => 0,
15 'qual_end' => 93
16 },
17 solexa => {
18 'offset' => 64,
19 'qual_start' => -5,
20 'qual_end' => 62
21 },
22 illumina => {
23 'offset' => 64,
24 'qual_start' => 0,
25 'qual_end' => 62
26 },
27 );
28  
29 37 ▶sub _initialize {
Subroutine _initialize: called CC=5 SCAR=16.3
30 37 my($self,@args) = @_;
31 37 $self->SUPER::_initialize(@args);
32 37 my ($variant, $validate, $header) = $self->_rearrange([qw(VARIANT
33 VALIDATE
34 QUALITY_HEADER)], @args);
35 37 ▶ $variant ||= 'sanger';
Condition100%2/2
$variant ||= "sanger"
l!l
361
MC/DC100%1/1
$variant ||= "sanger"
$variant
Truth table100%2/2
A
A= $variant
Aresult
11
00
36 37 $self->variant($variant);
37 37 $self->_init_tables($variant);
38 37 ▶ $validate = defined $validate ? $validate : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $validate ? : 0 37 37
39 37 $self->validate($validate);
40 37 ▶ $header && $self->quality_header($header);
Branch: 1 branch
BranchTrueFalseTotal
$header and $self->quality_header($header) 0 37 37
41  
42 37 ▶ if( ! defined $self->sequence_factory ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->sequence_factory) 37 0 37
43 37 $self->sequence_factory(Bio::Seq::SeqFactory->new(
44 -verbose => $self->verbose(),
45 -type => 'Bio::Seq::Quality')
46 );
47 }
48 }
49  
50 320 ▶sub next_seq {
Subroutine next_seq: called CC=2 SCAR=6.9
Pod documented
51 320 my( $self ) = @_;
52 320 ▶ while (defined(my $data = $self->next_dataset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $data = $self->next_dataset)) 306 13 319
53 # Are FASTQ sequences w/o any sequence valid? Removing for now
54 # -cjfields 6.22.09
55 306 my $seq = $self->sequence_factory->create(%$data);
56 306 return $seq;
57 }
58 13 return;
59 }
60  
61 # pure perl version
62 329 ▶sub next_dataset {
Subroutine next_dataset: called CC=23 SCAR=32.3
Pod documented
63 329 my $self = shift;
64 329 local $/ = "\n";
65 329 my $data;
66 329 my $mode = '-seq';
67 # speed this up by directly accessing the filehandle and in-lining the
68 # _readline stuff vs. making the repeated method calls. Tradeoff is speed
69 # over repeated code.
70  
71 # we can probably normalize line endings using PerlIO::eol or
72 # Encode::Newlines
73  
74 329 my $fh = $self->_fh;
75 329 ▶ my $line = $self->{lastline} || <$fh>;
Condition100%3/3
$self->{'lastline'} || readline $fh
l!l&&r!l&&!r
2902613
MC/DC100%2/2
$self->{'lastline'} || readline $fh
$self->{'lastline'} readline $fh
Truth table100%3/3
A || B
A= $self->{'lastline'}B= readline $fh
ABresult
1X1
011
000
76  
77 FASTQ:
78 329 ▶ while (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 1567 13 1580
79 1567 $line =~ s/\015\012/\012/;
80 1567 $line =~ tr/\015/\n/;
81 1567 ▶ if ($mode eq '-seq' && $line =~ m{^@([^\n]+)$}xmso) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq '-seq' and $line =~ /^@([^\n]+)$/mosx) { } 316 1251 1567
elsif ($mode eq '-seq' and $line =~ /^\+([^\n]*)/mosx) { } 316 935 1251
Condition100%3/3
$mode eq "-seq" and $line =~ /^@([^\n]+)$/mosx
!ll&&!rl&&r
618633316
MC/DC100%2/2
$mode eq "-seq" and $line =~ /^@([^\n]+)$/mosx
$mode eq "-seq" $line =~ /^@([^\n]+)$/mosx
Truth table100%3/3
A and B
A= $mode eq "-seq"B= $line =~ /^@([^\n]+)$/mosx
ABresult
0X0
100
111
82 316 $data->{-descriptor} = $1;
83 316 my ($id,$fulldesc);
84 316 ▶ if ($data->{-descriptor} =~ /^\s*(\S+)\s*(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'-descriptor'} =~ /^\s*(\S+)\s*(.*)/) { } 316 0 316
85 316 ($id,$fulldesc) = ($1, $2);
86 } else {
87 0 $self->throw("Can't parse fastq header");
88 }
89 316 $data->{-id} = $id;
90 316 $data->{-desc} = $fulldesc;
91 316 $data->{-namespace} = $self->variant;
92 ▶ } elsif ($mode eq '-seq' && $line =~ m{^\+([^\n]*)}xmso) {
Condition100%3/3
$mode eq "-seq" and $line =~ /^\+([^\n]*)/mosx
!ll&&!rl&&r
618317316
MC/DC100%2/2
$mode eq "-seq" and $line =~ /^\+([^\n]*)/mosx
$mode eq "-seq" $line =~ /^\+([^\n]*)/mosx
Truth table100%3/3
A and B
A= $mode eq "-seq"B= $line =~ /^\+([^\n]*)/mosx
ABresult
0X0
100
111
93 316 my $desc = $1;
94 316 ▶ $self->throw("No description line parsed") unless $data->{-descriptor};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"-descriptor"} 0 316 316
95 316 ▶ if ($desc && $data->{-descriptor} ne $desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc and $data->{"-descriptor"} ne $desc) 1 315 316
Condition100%3/3
$desc and $data->{"-descriptor"} ne $desc
!ll&&!rl&&r
252901
MC/DC100%2/2
$desc and $data->{"-descriptor"} ne $desc
$desc $data->{"-descriptor"} ne $desc
Truth table100%3/3
A and B
A= $descB= $data->{"-descriptor"} ne $desc
ABresult
0X0
100
111
96 $self->throw("Quality descriptor [$desc] doesn't match seq ".
97 1 "descriptor ".$data->{-descriptor}.", line: $." );
98 }
99 315 $mode = '-raw_quality';
100 } else {
101 935 ▶ if ($mode eq '-raw_quality' && defined($data->{-raw_quality}) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "-raw_quality" and defined $data->{"-raw_quality"} and length $data->{"-raw_quality"} >= length $data->{"-seq"}) 290 645 935
Condition100%6/6
$mode eq "-raw_quality" and defined $data->{"-raw_quality"} and length $data->{"-raw_quality"} >= length $data->{"-seq"}
!ll&&!rl&&r
63213290
$mode eq "-raw_quality" and defined $data->{"-raw_quality"}
!ll&&!rl&&r
317315303
MC/DC100%3/3
$mode eq "-raw_quality" and defined $data->{"-raw_quality"} and length $data->{"-raw_quality"} >= length $data->{"-seq"}
$mode eq "-raw_quality" defined $data->{"-raw_quality"} length $data->{"-raw_quality"} >= length $data->{"-seq"}
Truth table100%4/4
A and B and C
A= $mode eq "-raw_quality"B= defined $data->{"-raw_quality"}C= length $data->{"-raw_quality"} >= length $data->{"-seq"}
ABCresult
0XX0
10X0
1100
1111
102 (length($data->{-raw_quality}) >= length($data->{-seq}))) {
103 290 $self->{lastline} = $line;
104 last FASTQ
105 290 }
106 645 chomp $line;
107 645 ▶ if ($line =~ /^$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$/) 0 645 645
108 0 delete $self->{lastline};
109 0 last FASTQ;
110 }
111 645 $data->{$mode} .= $line
112 }
113 1276 $line = <$fh>;
114 1276 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 25 1251 1276
115 25 delete $self->{lastline};
116 25 last FASTQ;
117 }
118 }
119  
120 328 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 13 315 328
121 315 ▶ if (!$data->{-seq} || !defined($data->{-raw_quality})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"-seq"} and defined $data->{"-raw_quality"}) 0 315 315
Condition33%1/3
$data->{"-seq"} and defined $data->{"-raw_quality"}
!ll&&!rl&&r
00315
MC/DC0%0/2
$data->{"-seq"} and defined $data->{"-raw_quality"}
$data->{"-seq"} defined $data->{"-raw_quality"}
Truth table33%1/3
A and B
A= $data->{"-seq"}B= defined $data->{"-raw_quality"}
ABresult
0X0
100
111
122 0 $self->throw("Missing sequence and/or quality data; line: $.");
123 }
124  
125 # simple quality control tests
126 315 ▶ if (length $data->{-seq} != length $data->{-raw_quality}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data->{"-seq"} != length $data->{"-raw_quality"}) 0 315 315
127 $self->throw("Quality string [".$data->{-raw_quality}."] of length [".
128 length($data->{-raw_quality})."]\ndoesn't match length of sequence ".
129 0 $data->{-seq}."\n[".length($data->{-seq})."], line: $.");
130 }
131  
132 $data->{-qual} = [map {
133 68245 ▶ if ($self->{_validate_qual} && !exists($self->{chr2qual}->{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_validate_qual"} and not exists $self->{"chr2qual"}{$_}) 0 68245 68245
Condition33%1/3
$self->{"_validate_qual"} and not exists $self->{"chr2qual"}{$_}
!ll&&!rl&&r
0682450
MC/DC0%0/2
$self->{"_validate_qual"} and not exists $self->{"chr2qual"}{$_}
$self->{"_validate_qual"} not exists $self->{"chr2qual"}{$_}
Truth table33%1/3
A and not(B)
A= $self->{"_validate_qual"}B= not exists $self->{"chr2qual"}{$_}
ABresult
0X0
100
111
134 0 $self->throw("Unknown symbol with ASCII value ".ord($_)." outside ".
135 "of quality range")
136 # TODO: fallback?
137 }
138 $self->variant eq 'solexa' ?
139 $self->{sol2phred}->{$self->{chr2qual}->{$_}}:
140 68245 ▶ $self->{chr2qual}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
$self->variant eq 'solexa' ? : 523 67722 68245
141 315 } unpack("A1" x length($data->{-raw_quality}), $data->{-raw_quality})];
142 315 return $data;
143 }
144  
145 # This should be creating fastq output only. Bio::SeqIO::fasta and
146 # Bio::SeqIO::qual should be used for that output
147  
148 9 ▶sub write_seq {
Subroutine write_seq: called CC=19 SCAR=30.3
Pod documented
149 9 my ($self,@seq) = @_;
150 9 my $var = $self->variant;
151 9 foreach my $seq (@seq) {
152 9 ▶ unless ($seq->isa("Bio::Seq::Quality")){
Branch: 1 branch
BranchTrueFalseTotal
unless ($seq->isa("Bio::Seq::Quality")) 0 9 9
153 0 $self->warn("You can't write FASTQ without supplying a Bio::Seq::".
154 "Quality object! ".ref($seq)."\n");
155 0 next;
156 }
157 9 ▶ my $str = $seq->seq || '';
Condition50%1/2
$seq->seq || ''
l!l
90
MC/DC0%0/1
$seq->seq || ''
$seq->seq
Truth table50%1/2
A
A= $seq->seq
Aresult
11
00
158 9 my @qual = @{$seq->qual};
159  
160 # this should be the origin of the sequence (illumina, solexa, sanger)
161 9 my $ns= $seq->namespace;
162  
163 9 my $top = $seq->display_id();
164 9 ▶ if (my $desc = $seq->desc()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $desc = $seq->desc) 6 3 9
165 6 $desc =~ s/\n//g;
166 6 $top .= " $desc";
167 }
168 9 my $qual = '';
169 my $qual_map =
170 ($ns eq 'solexa' && $var eq 'solexa') ? $self->{phred_fp2chr} :
171 ($var eq 'solexa') ? $self->{phred_int2chr} :
172 9 ▶ $self->{qual2chr};
Branch: 2 branches
BranchTrueFalseTotal
$ns eq 'solexa' && $var eq 'solexa' ? : 1 8 9
$var eq 'solexa' ? : 2 6 8
Condition100%3/3
$ns eq "solexa" and $var eq "solexa"
!ll&&!rl&&r
621
MC/DC100%2/2
$ns eq "solexa" and $var eq "solexa"
$ns eq "solexa" $var eq "solexa"
Truth table100%3/3
A and B
A= $ns eq "solexa"B= $var eq "solexa"
ABresult
0X0
100
111
173  
174 9 my %bad_qual;
175 9 for my $q (@qual) {
176 543 ▶ $q = sprintf("%.0f", $q) if ($var ne 'solexa' && $ns eq 'solexa');
Branch: 1 branch
BranchTrueFalseTotal
if $var ne "solexa" and $ns eq "solexa" 92 451 543
Condition100%3/3
$var ne "solexa" and $ns eq "solexa"
!ll&&!rl&&r
18127092
MC/DC100%2/2
$var ne "solexa" and $ns eq "solexa"
$var ne "solexa" $ns eq "solexa"
Truth table100%3/3
A and B
A= $var ne "solexa"B= $ns eq "solexa"
ABresult
0X0
100
111
177 543 ▶ if (exists $qual_map->{$q}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $qual_map->{$q}) { } 481 62 543
178 481 $qual .= $qual_map->{$q};
179 481 next;
180 } else {
181 # fuzzy mapping, for edited qual scores
182 62 my $qr = sprintf("%.0f",$q);
183 62 my $bounds = sprintf("%.1f-%.1f",$qr-0.5, $qr+0.5);
184 62 ▶ if (exists $self->{fuzzy_qual2chr}->{$bounds}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'fuzzy_qual2chr'}{$bounds}) { } 0 62 62
185 0 $qual .= $self->{fuzzy_qual2chr}->{$bounds};
186 0 next;
187 } else {
188 my $rep = ($q <= $self->{qual_start}) ?
189 62 ▶ $qual_map->{$self->{qual_start}} : $qual_map->{$self->{qual_end}};
Branch: 1 branch
BranchTrueFalseTotal
$q <= $self->{'qual_start'} ? : 0 62 62
190 62 $qual .= $rep;
191 62 $bad_qual{$q}++;
192 }
193 }
194 }
195 9 ▶ if ($self->{_validate_qual} && %bad_qual) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_validate_qual"} and %bad_qual) 2 7 9
Condition66%2/3
$self->{"_validate_qual"} and %bad_qual
!ll&&!rl&&r
072
MC/DC50%1/2
$self->{"_validate_qual"} and %bad_qual
$self->{"_validate_qual"} %bad_qual
Truth table66%2/3
A and B
A= $self->{"_validate_qual"}B= %bad_qual
ABresult
0X0
100
111
196 $self->warn("Data loss for $var: following values not found\n".
197 2 join(',',sort {$a <=> $b} keys %bad_qual))
198 }
199 9 ▶ $self->_print("\@",$top,"\n",$str,"\n") or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_print("\@", $top, "\n", $str, "\n") or return 9 0 9
200 9 ▶ $self->_print("+",($self->{_quality_header} ? $top : ''),"\n",$qual,"\n") or return;
Branch: 2 branches
BranchTrueFalseTotal
$self->_print("+", $self->{"_quality_header"} ? $top : "", "\n", $qual, "\n") or return 9 0 9
$self->{'_quality_header'} ? : 0 9 9
201 }
202 9 return 1;
203 }
204  
205 0 ▶sub write_fastq {
Subroutine write_fastq: not called CC=1 SCAR=6.9
Pod documented
206 0 my ($self,@seq) = @_;
207 0 return $self->write_seq(@seq);
208 }
209  
210 0 ▶sub write_fasta {
Subroutine write_fasta: not called CC=2 SCAR=17.9
Pod documented
211 0 my ($self,@seq) = @_;
212 0 ▶ if (!exists($self->{fasta_proxy})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"fasta_proxy"}) 0 0 0
213 0 $self->{fasta_proxy} = Bio::SeqIO->new(-format => 'fasta', -fh => $self->_fh);
214 }
215 0 return $self->{fasta_proxy}->write_seq(@seq);
216 }
217  
218 0 ▶sub write_qual {
Subroutine write_qual: not called CC=2 SCAR=17.9
Pod documented
219 0 my ($self,@seq) = @_;
220 0 ▶ if (!exists($self->{qual_proxy})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"qual_proxy"}) 0 0 0
221 0 $self->{qual_proxy} = Bio::SeqIO->new(-format => 'qual', -fh => $self->_fh);
222 }
223 0 return $self->{qual_proxy}->write_seq(@seq);
224 }
225  
226 # variant() method inherited from Bio::Root::IO
227  
228 37 ▶sub _init_tables {
Subroutine _init_tables: called CC=6 SCAR=17.9
229 37 my ($self, $var) = @_;
230 # cache encode/decode values for quicker accession
231 ($self->{qual_start}, $self->{qual_end}, $self->{qual_offset}) =
232 37 @{ $variant{$var} }{qw(qual_start qual_end offset)};
233 37 ▶ if ($var eq 'solexa') {
Branch: 1 branch
BranchTrueFalseTotal
if ($var eq 'solexa') { } 10 27 37
234 10 for my $q ($self->{qual_start} .. $self->{qual_end}) {
235 680 my $char = chr($q + $self->{qual_offset});
236 680 $self->{chr2qual}->{$char} = $q;
237 680 $self->{qual2chr}->{$q} = $char;
238 680 my $s2p = 10 * log(1 + 10 ** ($q / 10.0)) / log(10);
239  
240 # solexa <=> solexa mapping speedup (retain floating pt precision)
241 680 $self->{phred_fp2chr}->{$s2p} = $char;
242 680 $self->{sol2phred}->{$q} = $s2p;
243  
244 # this is for mapping values fuzzily (fallback)
245 680 $self->{fuzzy_qual2chr}->{sprintf("%.1f-%.1f",$q - 0.5, $q + 0.5)} = $char;
246  
247 680 ▶ next if $q < 0; # skip loop; PHRED scores greater than 0
Branch: 1 branch
BranchTrueFalseTotal
if $q < 0 50 630 680
248 630 ▶ my $p2s = sprintf("%.0f",($q <= 1) ? -5 : 10 * log(-1 + 10 ** ($q / 10.0)) / log(10));
Branch: 1 branch
BranchTrueFalseTotal
$q <= 1 ? : 20 610 630
249 # sanger/illumina PHRED <=> Solexa char mapping speedup
250 630 $self->{phred_int2chr}->{$q} = chr($p2s + $self->{qual_offset});
251 }
252 } else {
253 27 for my $c ($self->{qual_start}..$self->{qual_end}) {
254 # PHRED mapping
255 2228 my $char = chr($c + $self->{qual_offset});
256 2228 $self->{chr2qual}->{$char} = $c;
257 2228 $self->{qual2chr}->{$c} = $char;
258 # this is for mapping values not found with above
259 2228 $self->{fuzzy_qual2chr}->{sprintf("%.1f-%.1f",$c - 0.5, $c + 0.5)} = $char;
260 }
261 }
262 }
263  
264 37 ▶sub validate {
Subroutine validate: called CC=2 SCAR=7.0
Pod documented
265 37 my ($self, $val) = @_;
266 37 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 37 0 37
267 37 $self->{_validate_qual} = $val;
268 }
269 37 return $self->{_validate_qual};
270 }
271  
272 0 ▶sub quality_header{
Subroutine quality_header: not called CC=3 SCAR=24.8
Pod documented
273 0 my ($self, $val) = @_;
274 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
275 0 $self->{_quality_header} = $val;
276 }
277 0 ▶ return $self->{_quality_header} || 0;
Condition0%0/2
$self->{'_quality_header'} || 0
l!l
00
MC/DC0%0/1
$self->{'_quality_header'} || 0
$self->{'_quality_header'}
Truth table0%0/2
A
A= $self->{'_quality_header'}
Aresult
11
00
278 }
279  
280 3 1;
281  
282 __END__
« Bio/Search/Hit/HitI.pm ↑ Summary Bio/SeqIO/gcg.pm »