| 1 | package Bio::Phylo::Parsers::Fastq; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use base 'Bio::Phylo::Parsers::Abstract'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Bio::Phylo::Util::Logger ':simple'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Bio::Phylo::Util::Exceptions 'throw'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Bio::Phylo::Util::CONSTANT ':objecttypes'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | Bio::Phylo::Parsers::Fastq - Parser used by Bio::Phylo::IO, no serviceable parts inside | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | A FASTQ file parser. To use it, you need to pass an argument | ||||||||||||||||||||||||||||||||||||||||||
| 16 | that specifies the data type of the phred scores into the parse function, i.e. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | my $handler_type = _DATUM_; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | parse( | ||||||||||||||||||||||||||||||||||||||||||
| 20 | -format => 'fastq', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | -type => 'illumina', # to indicate how phred scores are scaled | ||||||||||||||||||||||||||||||||||||||||||
| 22 | -file => 'infile.fastq', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | -flush => 1, # don't store record, flush and move on | ||||||||||||||||||||||||||||||||||||||||||
| 24 | -handlers => { | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | # specifies a handler that is executed on each newly created datum | ||||||||||||||||||||||||||||||||||||||||||
| 27 | $handler_type => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | my $seq = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | my @char = $seq->get_char; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | my @anno = @{ $seq->get_annotations }; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # print fasta, omit bases with low phred scores | ||||||||||||||||||||||||||||||||||||||||||
| 33 | print ">$seq\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | for my $i ( 0 .. $#char ) { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | if ( $anno[$i]->{phred} > 20 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 36 | print $char[$i]; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | print "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=15 SCAR=27.3
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | my $fh = $self->_handle; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | my $fac = $self->_factory; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | my $type = $self->_args->{'-type'} or throw 'BadArgs' => 'No data type specified!'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my $to = $fac->create_datatype($type); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | my $matrix; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | $matrix = $fac->create_matrix( '-type' => 'dna' ) unless $self->_flush; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | my ( $readseq, $readphred ); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my ( $id, $seq, $phred ); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | LINE: while( my $line = $fh->getline ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2336 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | # found the FASTQ id line | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 2336 | ▶ | if ( $line =~ /^\@(.+)$/ and not $readphred ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition100%3/3
$line =~ /^\@(.+)$/ and not $readphred
MC/DC100%2/2
$line =~ /^\@(.+)$/ and not $readphred
$line =~ /^\@(.+)$/
not $readphred
Truth table100%3/3
A and not(B)
A= $line =~ /^\@(.+)$/B= not $readphred
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 584 | my $capture = $1; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # process previous record | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 584 | ▶ | if ( $id && $seq && $phred ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$id and $seq and $phred
$id and $seq
MC/DC33%1/3
$id and $seq and $phred
$id
$seq
$phred
Truth table50%2/4
A and B and C
A= $idB= $seqC= $phred
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 583 | $self->_process_seq( | |||||||||||||||||||||||||||||||||||||||||
| 67 | 'phred' => $phred, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 'seq' => $seq, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 'id' => $id, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 'to' => $to, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # start new record | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 584 | $id = $capture; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 584 | $readseq = 1; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 584 | $readphred = 0; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 584 | $seq = ''; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 584 | INFO "found record ID $id, going to read sequence"; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 584 | next LINE; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | # found the FASTQ plus line | ||||||||||||||||||||||||||||||||||||||||||
| 84 | ▶ | elsif ( $line =~ /^\+/ and not $readphred ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$line =~ /^\+/ and not $readphred
MC/DC50%1/2
$line =~ /^\+/ and not $readphred
$line =~ /^\+/
not $readphred
Truth table66%2/3
A and not(B)
A= $line =~ /^\+/B= not $readphred
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 584 | $readseq = 0; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 584 | $readphred = 1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 584 | $phred = ''; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 584 | INFO "found plus line, going to read sequence quality"; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 584 | next LINE; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # concatenate sequence | ||||||||||||||||||||||||||||||||||||||||||
| 93 | elsif ( $readseq ) { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 584 | $seq .= $line; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 584 | next LINE; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | # concatenate quality line | ||||||||||||||||||||||||||||||||||||||||||
| 99 | elsif ( $readphred ) { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 584 | $phred .= $line; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 584 | ▶ | if ( length($phred) == length($seq) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 584 | INFO "found all phred characters"; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 584 | $readphred = 0; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 584 | next LINE; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # process last record | ||||||||||||||||||||||||||||||||||||||||||
| 110 | $self->_process_seq( | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | 'phred' => $phred, | |||||||||||||||||||||||||||||||||||||||||
| 112 | 'seq' => $seq, | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 'id' => $id, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 'to' => $to, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # done | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | return $self->_flush ? undef : $matrix; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 584 | ▶ | sub _process_seq { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_seq: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 584 | my ($self,%args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 584 | my $sh = $self->_handlers(_DATUM_); | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # turn the phred line into column-level annotations | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 82243 | my @scores = map { { 'phred' => $_ } } | |||||||||||||||||||||||||||||||||||||||||
| 127 | 82243 | map { @{ $args{to}->get_states_for_symbol($_) } } | |||||||||||||||||||||||||||||||||||||||||
| 128 | 584 | @{ $args{to}->split($args{phred}) }; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | # create the sequence object | ||||||||||||||||||||||||||||||||||||||||||
| 131 | my $datum = $self->_factory->create_datum( | ||||||||||||||||||||||||||||||||||||||||||
| 132 | '-type' => 'dna', | ||||||||||||||||||||||||||||||||||||||||||
| 133 | '-name' => $args{id}, | ||||||||||||||||||||||||||||||||||||||||||
| 134 | '-char' => $args{seq}, | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 584 | '-annotations' => \@scores, | |||||||||||||||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 584 | ▶ | $sh->($datum) if $sh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 584 | ▶ | $args{'matrix'}->insert($datum) unless $self->_flush; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | # podinherit_insert_token | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo> | ||||||||||||||||||||||||||||||||||||||||||
| 147 | for any user or developer questions and discussions. | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =item L<Bio::Phylo::IO> | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | The fasta parser is called by the L<Bio::Phylo::IO|Bio::Phylo::IO> object. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | Look there to learn more about parsing. | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =item L<Bio::Phylo::Manual> | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com> | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =head1 CITATION | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | If you use Bio::Phylo in published research, please cite it: | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen> | ||||||||||||||||||||||||||||||||||||||||||
| 167 | and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl. | ||||||||||||||||||||||||||||||||||||||||||
| 168 | I<BMC Bioinformatics> B<12>:63. | ||||||||||||||||||||||||||||||||||||||||||
| 169 | L<http://dx.doi.org/10.1186/1471-2105-12-63> | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||