| 1 | package BioX::Seq::Stream::FASTQ; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use parent qw/BioX::Seq::Stream/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | sub _check_type { | ||||||||||||||||||||||||||
|
Subroutine
_check_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 15 | my ($class,$self) = @_; | |||||||||||||||||||||||||||
| 11 | 15 | return substr($self->{buffer},0,1) eq '@'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # First two bytes should not contain line ending chars | ||||||||||||||||||||||||||||
| 20 | die "Missing ID in initial header (check file format)\n" | ||||||||||||||||||||||||||||
| 21 | 3 | ▶ | if ($self->{buffer} =~ /[\r\n]/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 3 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||
| 23 | 3 | $self->{buffer} .= <$fh>; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # detect line endings for text files based on first line | ||||||||||||||||||||||||||||
| 26 | # (other solutions, such as using the :crlf layer or s/// | ||||||||||||||||||||||||||||
| 27 | # instead of chomp may be marginally more robust but slow | ||||||||||||||||||||||||||||
| 28 | # things down too much) | ||||||||||||||||||||||||||||
| 29 | 3 | ▶ | if ($self->{buffer} =~ /([\r\n]{1,2})$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 2 | $self->{rec_sep} = $1; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | else { | ||||||||||||||||||||||||||||
| 33 | 1 | die "Failed to detect line endings\n"; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | return; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | sub next_seq { | ||||||||||||||||||||||||||
|
Subroutine
next_seq: called CC=11 SCAR=24.3
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 43 | 4 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | local $/ = $self->{rec_sep}; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | my $line = $self->{buffer} // <$fh>; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"buffer"} // readline $fh
MC/DC0%0/2
$self->{"buffer"} // readline $fh
$self->{"buffer"}
readline $fh
Truth table66%2/3
A // B
A= $self->{"buffer"}B= readline $fh
| |||||||||||||||||||||||||||||
| 48 | 4 | ▶ | return if (! defined $line); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 4 | chomp $line; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | my ($id, $desc) = ($line =~ /^\@(\S+)\s*(.+)?$/); | |||||||||||||||||||||||||||
| 52 | 4 | ▶ | die "Bad FASTQ ID line\n" if (! defined $id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # seq and qual can be multiline (although rare) | ||||||||||||||||||||||||||||
| 55 | # qual is tricky since it can contain '@' but we compare to the | ||||||||||||||||||||||||||||
| 56 | # sequence length to know when to stop parsing (must be equal lengths) | ||||||||||||||||||||||||||||
| 57 | 4 | ▶ | my $seq = <$fh> // die "Bad or missing FASTQ sequence"; | ||||||||||||||||||||||||||
|
Condition50%1/2
readline $fh // die("Bad or missing FASTQ sequence")
MC/DC0%0/1
readline $fh // die("Bad or missing FASTQ sequence")
readline $fh
Truth table50%1/2
A
A= readline $fh
| |||||||||||||||||||||||||||||
| 58 | 4 | chomp $seq; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | SEQ: | ||||||||||||||||||||||||||||
| 61 | 4 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 5 | chomp $line; | |||||||||||||||||||||||||||
| 63 | 5 | ▶ | last SEQ if ($line =~ /^\+/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | $seq .= $line; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | my $seq_len = length $seq; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | QUAL: | ||||||||||||||||||||||||||
|
Condition50%1/2
readline $fh // die("Bad or missing FASTQ format")
MC/DC0%0/1
readline $fh // die("Bad or missing FASTQ format")
readline $fh
Truth table50%1/2
A
A= readline $fh
| |||||||||||||||||||||||||||||
| 70 | my $qual = <$fh> // die "Bad or missing FASTQ format"; | ||||||||||||||||||||||||||||
| 71 | 4 | chomp $qual; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | ▶ | while ( (length($qual) < $seq_len) && defined (my $line = <$fh>) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $qual < $seq_len and defined(my $line = readline $fh)
MC/DC50%1/2
length $qual < $seq_len and defined(my $line = readline $fh)
length $qual < $seq_len
defined(my $line = readline $fh)
Truth table66%2/3
A and B
A= length $qual < $seq_lenB= defined(my $line = readline $fh)
| |||||||||||||||||||||||||||||
| 74 | 2 | chomp $line; | |||||||||||||||||||||||||||
| 75 | 2 | $qual .= $line; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 4 | ▶ | die "Bad FASTQ quality length" if ($seq_len != length($qual)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | $self->{buffer} = undef; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | my $obj = BioX::Seq->new($seq, $id, $desc, $qual); | |||||||||||||||||||||||||||
| 82 | 3 | $obj->{_input_format} = 'fastq'; | |||||||||||||||||||||||||||
| 83 | 3 | return $obj; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | 1; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||