lib/FASTX/Reader.pm

filter: Statementstmt 84.2% 220 / 261 Branchbran 66.1% 83 / 124 Conditioncond 57.1% 32 / 56 MC/DCmcdc 23.5% 8 / 34 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 73.1% 364 / 496 CC 85 SCAR 28.8
CC
85
Cov
76%
new
29.1
getRead
23.5
getIlluminaRead
21.3
CRAP 17.8
« lib/FASTX/PE.pm ↑ Summary lib/FASTX/ScriptHelper.pm »
1 package FASTX::Reader;
2 30 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use PerlIO::encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 $Data::Dumper::Sortkeys = 1;
8 30 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 $FASTX::Reader::VERSION = '1.5.1';
10 30 require Exporter;
11 30 our @ISA = qw(Exporter);
12 #ABSTRACT: A simple module to parse FASTA and FASTQ files, supporting compressed files and paired-ends.
13  
14 30 ▶use constant GZIP_SIGNATURE => pack('C3', 0x1f, 0x8b, 0x08);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 sub new {
18 # Instantiate object
19 40 ▶ my ($class, $args) = @_;
Subroutine new: called CC=19 SCAR=33.7
Pod documented
20 40 my $self = bless {} => $class;
21  
22 40 ▶ if (defined $args->{loadseqs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"loadseqs"}) 2 38 40
23 2 ▶ if ($args->{loadseqs} eq 'name' or $args->{loadseqs} eq 'names' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'loadseqs'} eq 'name' or $args->{'loadseqs'} eq 'names') { } 1 1 2
elsif ($args->{'loadseqs'} eq 'seq' or $args->{'loadseqs'} eq 'seqs' or $args->{'loadseqs'} == 1) { } 1 0 1
Condition66%2/3
$args->{"loadseqs"} eq "name" or $args->{"loadseqs"} eq "names"
l!l&&r!l&&!r
101
MC/DC50%1/2
$args->{"loadseqs"} eq "name" or $args->{"loadseqs"} eq "names"
$args->{"loadseqs"} eq "name" $args->{"loadseqs"} eq "names"
Truth table66%2/3
A or B
A= $args->{"loadseqs"} eq "name"B= $args->{"loadseqs"} eq "names"
ABresult
1X1
011
000
24 1 $args->{loadseqs} = 'name';
25 ▶ } elsif ($args->{loadseqs} eq 'seq' or $args->{loadseqs} eq 'seqs' or $args->{loadseqs} == 1) {
Condition33%2/6
$args->{"loadseqs"} eq "seq" or $args->{"loadseqs"} eq "seqs" or $args->{"loadseqs"} == 1
l!l&&r!l&&!r
100
$args->{"loadseqs"} eq "seq" or $args->{"loadseqs"} eq "seqs"
l!l&&r!l&&!r
100
MC/DC0%0/3
$args->{"loadseqs"} eq "seq" or $args->{"loadseqs"} eq "seqs" or $args->{"loadseqs"} == 1
$args->{"loadseqs"} eq "seq" $args->{"loadseqs"} eq "seqs" $args->{"loadseqs"} == 1
Truth table25%1/4
A or B or C
A= $args->{"loadseqs"} eq "seq"B= $args->{"loadseqs"} eq "seqs"C= $args->{"loadseqs"} == 1
ABCresult
1XX1
01X1
0011
0000
26 1 $args->{loadseqs} = 'seq';
27 } else {
28 0 confess("attribute <loadseqs> should be 'name' or 'seq' to specify the key of the hash.");
29 }
30 }
31 40 $self->{filename} = $args->{filename};
32 40 $self->{loadseqs} = $args->{loadseqs};
33 40 $self->{aux} = [undef];
34 40 $self->{compressed} = 0;
35 40 $self->{fh} = undef;
36  
37  
38 # Check if a filename was provided and not {{STDIN}}
39 # uncoverable branch false
40  
41 40 ▶ if ( defined $self->{filename} and -d $self->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"filename"} and -d $self->{"filename"}) 0 40 40
Condition66%2/3
defined $self->{"filename"} and -d $self->{"filename"}
!ll&&!rl&&r
2380
MC/DC0%0/2
defined $self->{"filename"} and -d $self->{"filename"}
defined $self->{"filename"} -d $self->{"filename"}
Truth table66%2/3
A and B
A= defined $self->{"filename"}B= -d $self->{"filename"}
ABresult
0X0
100
111
42 0 confess "Directory provide where file was expected\n";
43 }
44  
45 40 ▶ if (defined $self->{filename} and $self->{filename} ne '{{STDIN}}') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'filename'} and $self->{'filename'} ne '{{STDIN}}') { } 38 2 40
Condition66%2/3
defined $self->{"filename"} and $self->{"filename"} ne "{{STDIN}}"
!ll&&!rl&&r
2038
MC/DC50%1/2
defined $self->{"filename"} and $self->{"filename"} ne "{{STDIN}}"
defined $self->{"filename"} $self->{"filename"} ne "{{STDIN}}"
Truth table66%2/3
A and B
A= defined $self->{"filename"}B= $self->{"filename"} ne "{{STDIN}}"
ABresult
0X0
100
111
46 38 ▶ open my $initial_fh, '<', $self->{filename} or confess "Unable to read file ", $self->{filename}, "\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $initial_fh, "<", $self->{"filename"} or confess("Unable to read file ", $self->{"filename"}, "\n") 4 34 38
47 34 read( $initial_fh, my $magic_byte, 4 );
48 34 close $initial_fh;
49  
50 # See: __BioX::Seq::Stream__ for GZIP (and other) compressed file reader
51 34 ▶ if (substr($magic_byte,0,3) eq GZIP_SIGNATURE) {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($magic_byte, 0, 3) eq "\037\213\cH") { } 17 17 34
elsif (-B $self->{'filename'}) { } 0 17 17
52 17 $self->{compressed} = 1;
53 17 our $GZIP_BIN = _which('pigz', 'gzip');
54 #close $fh;
55 17 ▶ if (! defined $GZIP_BIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $FASTX::Reader::GZIP_BIN) { } 0 17 17
56 0 require IO::Uncompress::Gunzip;
57 0 my $fh = IO::Uncompress::Gunzip->new($self->{filename}, MultiStream => 1);
58 0 $self->{fh} = $fh;
59 } else {
60 17 ▶ open my $fh, '-|', "$GZIP_BIN -dc $self->{filename}" or confess "Error opening gzip file ", $self->{filename}, ": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", qq[$FASTX::Reader::GZIP_BIN -dc $self->{"filename"}] or confess("Error opening gzip file ", $self->{"filename"}, ": $!\n") 0 17 17
61 17 $self->{fh} = $fh;
62 }
63 } elsif (-B $self->{filename}) {
64  
65 # BINARY FILE NOT SUPPORTED?
66 #close $fh;
67 0 $self->{fh} = undef;
68 0 $self->{status} = 1;
69 0 $self->{message} = 'Binary file not supported';
70 } else {
71  
72 #close $fh;
73 17 ▶ open (my $fh, '<:encoding(utf8):crlf', $self->{filename}) or confess "Unable to read file ", $self->{filename}, ": ", $!, "\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8):crlf", $self->{"filename"} or confess("Unable to read file ", $self->{"filename"}, ": ", $!, "\n") 0 17 17
74 17 $self->{fh} = $fh;
75 }
76  
77  
78 } else {
79 # Filename not provided, use STDIN
80 2 $self->{fh} = \*STDIN;
81 2 ▶ if ($self->{loadseqs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"loadseqs"}) 0 2 2
82 0 confess("Load sequences not supported for STDIN");
83 }
84 }
85  
86  
87  
88  
89 36 ▶ if ($self->{loadseqs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"loadseqs"}) 2 34 36
90 2 _load_seqs($self);
91 }
92  
93 36 return $self;
94  
95 }
96  
97  
98  
99 sub getRead {
100 72 ▶ my $self = shift;
Subroutine getRead: called CC=21 SCAR=31.6
Pod documented
101  
102 #@<instrument>:<run number>:<flowcell ID>:<lane>:<tile>:<x-pos>:<y-pos>:<UMI> <read>:<is filtered>:<control number>:<index>
103  
104  
105 72 ▶ return if (defined $self->{status} and $self->{status} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"status"} and $self->{"status"} == 0 0 72 72
Condition33%1/3
defined $self->{"status"} and $self->{"status"} == 0
!ll&&!rl&&r
7200
MC/DC0%0/2
defined $self->{"status"} and $self->{"status"} == 0
defined $self->{"status"} $self->{"status"} == 0
Truth table33%1/3
A and B
A= defined $self->{"status"}B= $self->{"status"} == 0
ABresult
0X0
100
111
106  
107 #my $aux = $self->{aux};
108 72 my $sequence_data;
109 72 ▶ @{ $self->{aux} } = [undef, 0] if (!(@{ $self->{aux} }));
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"aux"};} 0 72 72
110  
111  
112 72 ▶ if ($self->{aux}->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"aux"}[1]) 5 67 72
113 5 $self->{return_0}++;
114 }
115  
116 72 ▶ if (!defined($self->{aux}->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"aux"}[0]) 48 24 72
117 48 ▶ while ($self->{line} = readline($self->{fh})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($self->{"line"} = readline $self->{"fh"})) 42 7 49
118  
119 42 chomp($self->{line});
120 42 ▶ if (substr($self->{line}, 0, 1) eq '>' || substr($self->{line}, 0, 1) eq '@') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->{"line"}, 0, 1) eq ">" or substr($self->{"line"}, 0, 1) eq "\@") 41 1 42
Condition100%3/3
substr($self->{"line"}, 0, 1) eq ">" or substr($self->{"line"}, 0, 1) eq "\@"
l!l&&r!l&&!r
9321
MC/DC100%2/2
substr($self->{"line"}, 0, 1) eq ">" or substr($self->{"line"}, 0, 1) eq "\@"
substr($self->{"line"}, 0, 1) eq ">" substr($self->{"line"}, 0, 1) eq "\@"
Truth table100%3/3
A or B
A= substr($self->{"line"}, 0, 1) eq ">"B= substr($self->{"line"}, 0, 1) eq "\@"
ABresult
1X1
011
000
121 41 $self->{aux}->[0] = $self->{line};
122 41 last;
123 }
124 }
125 48 ▶ if (!defined($self->{aux}->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"aux"}[0]) 7 41 48
126 7 $self->{aux}->[1] = 1;
127 7 $self->{return_1}++;
128 7 return;
129 }
130 }
131  
132  
133 # Comments can have more spaces:
134 65 ▶ return unless defined $self->{line};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"line"} 0 65 65
135 my ($name, $comm) = $self->{line}=~/^.(\S+)(?:\s+)(.+)/ ? ($1, $2) :
136 65 ▶ $self->{line}=~/^.(\S+)/ ? ($1, '') : ('?', '');
Branch: 2 branches
BranchTrueFalseTotal
$self->{'line'} =~ /^.(\S+)(?:\s+)(.+)/u ? : 25 40 65
$self->{'line'} =~ /^.(\S+)/u ? : 40 0 40
137 65 my $seq = '';
138 65 my $c;
139 65 $self->{aux}->[0] = undef;
140 65 ▶ while ($self->{line} = readline($self->{fh})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($self->{"line"} = readline $self->{"fh"})) 147 7 154
141 # PARSE SEQx
142 147 chomp($self->{line});
143 147 $c = substr($self->{line}, 0, 1);
144 147 ▶ last if ($c eq '>' || $c eq '@' || $c eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $c eq ">" or $c eq "\@" or $c eq "+" 32 89 121
Condition83%5/6
$c eq ">" or $c eq "\@" or $c eq "+"
l!l&&r!l&&!r
263289
$c eq ">" or $c eq "\@"
l!l&&r!l&&!r
260121
MC/DC66%2/3
$c eq ">" or $c eq "\@" or $c eq "+"
$c eq ">" $c eq "\@" $c eq "+"
Truth table75%3/4
A or B or C
A= $c eq ">"B= $c eq "\@"C= $c eq "+"
ABCresult
1XX1
01X1
0011
0000
145 89 $seq .= $self->{line};
146 }
147 65 $self->{aux}->[0] = $self->{line};
148 65 ▶ $self->{aux}->[1] = 1 if (!defined($self->{aux}->[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"aux"}[0] 7 58 65
149 65 $sequence_data->{name} = $name;
150 65 $sequence_data->{comment} = $comm;
151 65 $sequence_data->{seq} = $seq;
152 65 $self->{counter}++;
153 # Return FASTA
154 65 ▶ if ($c ne '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "+") 33 32 65
155 33 $self->{return_fasta1}++;
156 33 return $sequence_data;
157 }
158 32 my $qual = '';
159  
160  
161 32 ▶ while ($self->{line} = readline($self->{fh})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($self->{"line"} = readline $self->{"fh"})) 32 0 32
162 # PARSE QUALITY
163 32 chomp($self->{line});
164 32 $qual .= $self->{line};
165 32 ▶ if (length($qual) >= length($seq)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $qual >= length $seq) 32 0 32
166 32 $self->{aux}->[0] = undef;
167 32 $sequence_data->{name} = $name;
168 32 $sequence_data->{seq} = $seq;
169 32 $sequence_data->{comment} = $comm;
170 32 $sequence_data->{qual} = $qual;
171 # return FASTQ
172 32 $self->{return_fastq}++;
173 32 return $sequence_data;
174 }
175 }
176 # PROCH
177 0 close $self->{fh};
178  
179 0 $self->{aux}->[1] = 1;
180 0 $sequence_data->{name} = $name;
181 0 $sequence_data->{seq} = $seq;
182 0 $sequence_data->{comment} = $comm;
183 0 $self->{counter}++;
184 # return FASTA
185 0 $self->{return_fasta2}++;
186 0 return $sequence_data;
187  
188 }
189  
190  
191 sub next {
192 1 ▶ my $self = shift;
Subroutine next: called CC=6 SCAR=19.2
Pod documented
193 1 my $class = "FASTX::Seq";
194 1 my $scalar_read = $self->getRead();
195 1 ▶ if (not defined $scalar_read->{seq} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scalar_read->{"seq"}) 0 1 1
196 0 return;
197 }
198 1 my $seq = bless {}, $class;
199 1 ▶ $seq->{seq} = $scalar_read->{seq} // '';
Condition50%1/2
$scalar_read->{"seq"} // ""
l!l
10
MC/DC0%0/1
$scalar_read->{"seq"} // ""
$scalar_read->{"seq"}
Truth table50%1/2
A
A= $scalar_read->{"seq"}
Aresult
11
00
200 1 ▶ $seq->{name} = $scalar_read->{name} // undef;
Condition50%1/2
$scalar_read->{"name"} // undef
l!l
10
MC/DC0%0/1
$scalar_read->{"name"} // undef
$scalar_read->{"name"}
Truth table50%1/2
A
A= $scalar_read->{"name"}
Aresult
11
00
201 1 ▶ $seq->{comment} = $scalar_read->{comment} // undef;
Condition50%1/2
$scalar_read->{"comment"} // undef
l!l
10
MC/DC0%0/1
$scalar_read->{"comment"} // undef
$scalar_read->{"comment"}
Truth table50%1/2
A
A= $scalar_read->{"comment"}
Aresult
11
00
202 1 ▶ $seq->{qual} = $scalar_read->{qual} // undef;
Condition50%1/2
$scalar_read->{"qual"} // undef
l!l
10
MC/DC0%0/1
$scalar_read->{"qual"} // undef
$scalar_read->{"qual"}
Truth table50%1/2
A
A= $scalar_read->{"qual"}
Aresult
11
00
203
204 1 return $seq;
205
206 }
207  
208  
209 sub getFastqRead {
210 7 ▶ my $self = shift;
Subroutine getFastqRead: called CC=10 SCAR=23.4
Pod documented
211 7 my $seq_object = undef;
212  
213 7 ▶ return if (defined $self->{status} and $self->{status} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"status"} and $self->{"status"} == 0 0 7 7
Condition66%2/3
defined $self->{"status"} and $self->{"status"} == 0
!ll&&!rl&&r
430
MC/DC0%0/2
defined $self->{"status"} and $self->{"status"} == 0
defined $self->{"status"} $self->{"status"} == 0
Truth table66%2/3
A and B
A= defined $self->{"status"}B= $self->{"status"} == 0
ABresult
0X0
100
111
214  
215 7 $self->{status} = 1;
216 7 my $header = readline($self->{fh});
217 7 my $seq = readline($self->{fh});
218 7 my $check = readline($self->{fh});
219 7 my $qual = readline($self->{fh});
220  
221  
222 # Check 4 lines were found (FASTQ)
223  
224 7 ▶ unless (defined $qual) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $qual) 1 6 7
225 1 ▶ if (defined $header) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header) 0 1 1
226 0 $self->{message} = "Unknown format: FASTQ truncated at " . $header . "?";
227 0 $self->{status} = 0;
228 }
229 1 return;
230 }
231  
232 # Fast format control: header and separator
233 6 ▶ if ( (substr($header, 0, 1) eq '@') and (substr($check, 0, 1) eq '+') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($header, 0, 1) eq '@' and substr($check, 0, 1) eq '+') { } 5 1 6
Condition66%2/3
substr($header, 0, 1) eq "\@" and substr($check, 0, 1) eq "+"
!ll&&!rl&&r
105
MC/DC50%1/2
substr($header, 0, 1) eq "\@" and substr($check, 0, 1) eq "+"
substr($header, 0, 1) eq "\@" substr($check, 0, 1) eq "+"
Truth table66%2/3
A and B
A= substr($header, 0, 1) eq "\@"B= substr($check, 0, 1) eq "+"
ABresult
0X0
100
111
234 5 chomp($header);
235 5 chomp($seq);
236 5 chomp($qual);
237 # Also control sequence integrity
238 5 ▶ if ($seq=~/^[ACGTNacgtn]+$/ and length($seq) == length($qual) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq =~ /^[ACGTNacgtn]+$/u and length $seq == length $qual) { } 4 1 5
Condition66%2/3
$seq =~ /^[ACGTNacgtn]+$/u and length $seq == length $qual
!ll&&!rl&&r
104
MC/DC50%1/2
$seq =~ /^[ACGTNacgtn]+$/u and length $seq == length $qual
$seq =~ /^[ACGTNacgtn]+$/u length $seq == length $qual
Truth table66%2/3
A and B
A= $seq =~ /^[ACGTNacgtn]+$/uB= length $seq == length $qual
ABresult
0X0
100
111
239 4 my ($name, $comments) = split /\s+/, substr($header, 1);
240 4 $seq_object->{name} = $name;
241 4 $seq_object->{comments} = $comments;
242 4 $seq_object->{seq} = $seq;
243 4 $seq_object->{qual} = $qual;
244 4 $self->{counter}++;
245  
246 } else {
247 # Return error (corrupted FASTQ)
248 1 $self->{message} = "Unknown format: expecting FASTQ (corrupted?)";
249 1 $self->{status} = 0;
250  
251 }
252 } else {
253 # Return error (not a FASTQ)
254 1 $self->{message} = "Unknown format: expecting FASTQ but @ header not found";
255  
256 1 ▶ if (substr($header, 0,1 ) eq '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($header, 0, 1) eq ">") 1 0 1
257 # HINT: is FASTA?
258 1 $self->{message} .= " (might be FASTA instead)";
259 }
260 1 $self->{status} = 0;
261 }
262  
263 6 return $seq_object;
264 }
265  
266  
267 sub getIlluminaRead {
268 16 ▶ my $self = shift;
Subroutine getIlluminaRead: called CC=15 SCAR=30.6
Pod documented
269 16 my $seq_object = undef;
270  
271 16 ▶ return if (defined $self->{status} and $self->{status} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"status"} and $self->{"status"} == 0 0 16 16
Condition66%2/3
defined $self->{"status"} and $self->{"status"} == 0
!ll&&!rl&&r
2140
MC/DC0%0/2
defined $self->{"status"} and $self->{"status"} == 0
defined $self->{"status"} $self->{"status"} == 0
Truth table66%2/3
A and B
A= defined $self->{"status"}B= $self->{"status"} == 0
ABresult
0X0
100
111
272  
273 16 $self->{status} = 1;
274 16 my $header = readline($self->{fh});
275 16 my $seq = readline($self->{fh});
276 16 my $check = readline($self->{fh});
277 16 my $qual = readline($self->{fh});
278  
279  
280 # Check 4 lines were found (FASTQ)
281  
282 16 ▶ unless (defined $qual) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $qual) 2 14 16
283 2 ▶ if (defined $header) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header) 0 2 2
284 0 $self->{message} = "Unknown format: FASTQ truncated at " . $header . "?";
285 0 $self->{status} = 0;
286 }
287 2 return;
288 }
289  
290 # Fast format control: header and separator
291 14 ▶ if ( (substr($header, 0, 1) eq '@') and (substr($check, 0, 1) eq '+') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($header, 0, 1) eq '@' and substr($check, 0, 1) eq '+') { } 14 0 14
Condition33%1/3
substr($header, 0, 1) eq "\@" and substr($check, 0, 1) eq "+"
!ll&&!rl&&r
0014
MC/DC0%0/2
substr($header, 0, 1) eq "\@" and substr($check, 0, 1) eq "+"
substr($header, 0, 1) eq "\@" substr($check, 0, 1) eq "+"
Truth table33%1/3
A and B
A= substr($header, 0, 1) eq "\@"B= substr($check, 0, 1) eq "+"
ABresult
0X0
100
111
292 14 chomp($header);
293 14 chomp($seq);
294 14 chomp($qual);
295 # Also control sequence integrity
296 14 ▶ if ($seq=~/^[ACGTNacgtn]+$/ and length($seq) == length($qual) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq =~ /^[ACGTNacgtn]+$/u and length $seq == length $qual) { } 14 0 14
Condition33%1/3
$seq =~ /^[ACGTNacgtn]+$/u and length $seq == length $qual
!ll&&!rl&&r
0014
MC/DC0%0/2
$seq =~ /^[ACGTNacgtn]+$/u and length $seq == length $qual
$seq =~ /^[ACGTNacgtn]+$/u length $seq == length $qual
Truth table33%1/3
A and B
A= $seq =~ /^[ACGTNacgtn]+$/uB= length $seq == length $qual
ABresult
0X0
100
111
297 14 my ($name, $comments) = split /\s+/, substr($header, 1);
298 #@<instrument>:<run number>:<flowcell ID>:<lane>:<tile>:<x-pos>:<y-pos>:<UMI> <read>:<is filtered>:<control number>:<index>
299 14 my ($instrument, $run, $flowcell, $lane, $tile, $x, $y, $umi) = split /:/, $name;
300 14 my ($read, $filtered, $ctrl, $index1, $index2);
301  
302 14 ▶ if (not defined $y) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $y) 0 14 14
303 0 $self->{message} = "Unknown format: not Illumina naming: <instrument>:<run number>:<flowcell ID>:<lane>:<tile>:<x-pos>:<y-pos>";
304 0 $self->{status} = 0;
305 }
306  
307 14 $seq_object->{name} = $name;
308 14 $seq_object->{comments} = $comments;
309 14 $seq_object->{seq} = $seq;
310 14 $seq_object->{qual} = $qual;
311  
312 14 $seq_object->{instrument} = $instrument;
313 14 $seq_object->{run} = $run;
314 14 $seq_object->{flowcell} = $flowcell;
315 14 $seq_object->{lane} = $lane;
316 14 $seq_object->{tile} = $tile;
317 14 $seq_object->{x} = $x;
318 14 $seq_object->{y} = $y;
319 14 $seq_object->{umi} = $umi;
320 14 $seq_object->{instrument} = $instrument;
321 14 $seq_object->{run} = $run;
322 14 $seq_object->{flowcell} = $flowcell;
323  
324 14 ▶ if (defined $comments) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comments) 7 7 14
325 7 ($read, $filtered, $ctrl, $index1, $index2) = split /[:+]/, $comments;
326 7 ▶ if ( defined $ctrl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctrl) 7 0 7
327 7 $seq_object->{read} = $read;
328 7 ▶ $filtered eq 'N' ? $seq_object->{filtered} = 0 : $seq_object->{filtered} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$filtered eq 'N' ? : 7 0 7
329 7 $seq_object->{control} = $ctrl;
330 7 ▶ if ($read eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($read eq '1') { } 7 0 7
331 7 $seq_object->{index} = $index1;
332 7 $seq_object->{paired_index} = $index2;
333 } else {
334 0 $seq_object->{paired_index} = $index1;
335 0 $seq_object->{index} = $index2;
336 }
337 }
338 }
339 14 $self->{counter}++;
340  
341 } else {
342 # Return error (corrupted FASTQ)
343 0 $self->{message} = "Unknown format: expecting FASTQ (corrupted?)";
344 0 $self->{status} = 0;
345  
346 }
347 } else {
348 # Return error (not a FASTQ)
349 0 $self->{message} = "Unknown format: expecting FASTQ but @ header not found";
350  
351 0 ▶ if (substr($header, 0,1 ) eq '>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($header, 0, 1) eq ">") 0 0 0
352 # HINT: is FASTA?
353 0 $self->{message} .= " (might be FASTA instead)";
354 }
355 0 $self->{status} = 0;
356 }
357  
358 14 return $seq_object;
359 }
360  
361 sub getFileFormat {
362 4 ▶ my $self = shift;
Subroutine getFileFormat: called CC=10 SCAR=26.8
Pod documented
363 4 my ($filename) = shift;
364 4 ▶ return 0 if (not defined $filename);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 4 4
365  
366 4 ▶ open my $fh, '<', $filename or confess "Unable to read file ", $filename, "\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or confess("Unable to read file ", $filename, "\n") 0 4 4
367 4 read( $fh, my $magic_byte, 4 );
368 4 close $fh;
369  
370 4 ▶ if (substr($magic_byte,0,3) eq GZIP_SIGNATURE) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($magic_byte, 0, 3) eq "\037\213\cH") { } 0 4 4
371 # GZIPPED FILE
372 0 ▶ if (! defined $self->{GZIP_BIN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'GZIP_BIN'}) { } 0 0 0
373 0 require IO::Uncompress::Gunzip;
374 0 $fh = IO::Uncompress::Gunzip->new($filename, MultiStream => 1);
375 } else {
376 0 ▶ open $fh, '-|', "$self->{GZIP_BIN} -dc $filename" or confess "Error opening gzip file ", $filename, ": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "-|", qq[$self->{"GZIP_BIN"} -dc $filename] or confess("Error opening gzip file ", $filename, ": $!\n") 0 0 0
377 }
378 } else {
379 # NOT COMPRESSED
380 4 ▶ open $fh, '<:encoding(utf8)', "$filename" || confess "Unable to read $filename\n$!\n";
Condition33%1/3
"$filename" || confess("Unable to read $filename\n$!\n")
l!l&&r!l&&!r
400
MC/DC0%0/2
"$filename" || confess("Unable to read $filename\n$!\n")
"$filename" confess("Unable to read $filename\n$!\n")
Truth table33%1/3
A || B
A= "$filename"B= confess("Unable to read $filename\n$!\n")
ABresult
1X1
011
000
381 }
382 4 my $first = readline($fh);
383 4 ▶ if (substr($first, 0,1) eq '>') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($first, 0, 1) eq '>') { } 1 3 4
elsif (substr($first, 0, 1) eq '@') { } 2 1 3
384 #should be FASTA
385 1 return 'fasta';
386 } elsif (substr($first, 0, 1) eq '@') {
387 #should be fastq
388 2 readline($fh);
389 2 my $sep = readline($fh);
390 2 ▶ if ( substr($sep, 0, 1) eq '+' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($sep, 0, 1) eq "+") 2 0 2
391 #second check for fastq
392 2 return 'fastq';
393 }
394 } else {
395 #unknown format
396 1 return;
397 }
398 }
399 sub _load_seqs {
400 2 ▶ my ($self) = @_;
Subroutine _load_seqs: called CC=4 SCAR=13.9
401 2 ▶ return 0 unless (defined $self->{loadseqs});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"loadseqs"} 0 2 2
402  
403 2 my $seqs = undef;
404 2 ▶ while (my $s = $self->getRead() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $self->getRead) 6 2 8
405 6 my ($name, $seq) = ($s->{name}, $s->{seq});
406 6 ▶ if ($self->{loadseqs} eq 'name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'loadseqs'} eq 'name') { } 3 3 6
407 3 $seqs->{$name} = $seq;
408 } else {
409 3 $seqs->{$seq} = $name;
410 }
411  
412 }
413 2 $self->{seqs} = $seqs;
414 }
415  
416  
417 sub _which {
418 17 ▶ return if ($^O eq 'MSWin32');
Subroutine _which: called CC=7 SCAR=24.4
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 17 17
419 17 my $has_which = eval { require File::Which; File::Which->import(); 1};
420 17 ▶ if ($has_which) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_which) { } 17 0 17
421 17 foreach my $cmd (@_) {
422 17 ▶ return which($cmd) if (which($cmd));
Branch: 1 branch
BranchTrueFalseTotal
if which($cmd) 17 0 17
423 }
424 } else {
425 0 foreach my $cmd (@_) {
426 0 `which $cmd 2> /dev/null`;
427 0 ▶ return $cmd if (not $?);
Branch: 1 branch
BranchTrueFalseTotal
unless $? 0 0 0
428 }
429 }
430 0 return;
431 }
432 30 1;
433  
434 __END__
« lib/FASTX/PE.pm ↑ Summary lib/FASTX/ScriptHelper.pm »