| 1 | #! /usr/bin/perl | ||
| 2 | |||
| 3 | package Bio::SeqReader::Fastq; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Bio::SeqReader::Fastq - Class providing a reader for files in FASTQ format. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Bio::SeqReader::Fastq; | ||
| 12 | my $in1 = new Bio::SeqReader::Fastq(); # from stdin | ||
| 13 | my $fh = ... | ||
| 14 | my $in2 = new Bio::SeqReader::Fastq( fh => $fh ); # from filehandle | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Bio::SeqReader::Fastq provides a method for reading a file or stream in FASTQ format. | ||
| 19 | |||
| 20 | This format is described in P. J. A. Cock, C. J. Fields, N. Goto, M. L. Heuer, | ||
| 21 | P. M. Rice. (2010) I<The Sanger FASTQ file format for sequences with quality scores, and | ||
| 22 | the Solexa/Illumina FASTQ variants>, Nucleic Acids Research 38. It specifically allows for multiline | ||
| 23 | sequence and quality score information, which are handled correctly by this class. | ||
| 24 | |||
| 25 | =head1 CLASS METHODS | ||
| 26 | |||
| 27 | Bio::SeqReader::Fastq provides no class methods. | ||
| 28 | |||
| 29 | =head1 INSTANCE METHODS | ||
| 30 | |||
| 31 | Bio::SeqReader::Fastq provides the following instance methods. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | use strict; | ||
| 36 | use IO::Handle; | ||
| 37 | use Bio::SeqReader::FastqRecord; | ||
| 38 | |||
| 39 | =over 12 | ||
| 40 | |||
| 41 | =item B<new()> | ||
| 42 | |||
| 43 | Constructor. Returns a new Bio::SeqReader::Fastq object associated with stdin (by default) | ||
| 44 | or with a filehandle. Understands optional specification of an IO::File-compatible filehandle | ||
| 45 | via C<< fh => $fh >>. | ||
| 46 | |||
| 47 | # From an IO::File filehandle | ||
| 48 | my $fh1 = new IO::File( 'in.fq' ); | ||
| 49 | my $in1 = new Bio::SeqReader::Fastq( fh => $fh1); | ||
| 50 | |||
| 51 | # From an IO::Uncompress::AnyUncompress filehandle | ||
| 52 | my $fh2 = new IO::File( 'in.fq.gz' ); | ||
| 53 | my $in2 = new Bio::SeqReader::Fastq( fh => $fh2); | ||
| 54 | |||
| 55 | # From stdin | ||
| 56 | my $in3 = new Bio::SeqReader::Fastq(); | ||
| 57 | |||
| 58 | A specified filehandle must be compatible with those produced by IO::File filehandle; for example, | ||
| 59 | |||
| 60 | $fh1 = new IO::File( 'in.fastq' ) | ||
| 61 | $fh2 = new IO::Uncompress::AnyUncompress( 'in.fastq.gz' ) | ||
| 62 | $fh3 = new IO::Uncompress::AnyUncompress( 'in.fastq' ). | ||
| 63 | |||
| 64 | =back | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my ( $class, %parms ) = @_; | ||
| 70 | |||
| 71 | my $self = {}; | ||
| 72 | bless( $self, $class ); | ||
| 73 | |||
| 74 | if ( exists $parms{ fh } ) { | ||
| 75 | $self->{ _FH } = $parms{ fh }; | ||
| 76 | } | ||
| 77 | |||
| 78 | else { | ||
| 79 | $self->{ _FH } = \*STDIN; | ||
| 80 | } | ||
| 81 | |||
| 82 | $self->{ _READER_STATE } = 'start'; | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | =over 12 | ||
| 88 | |||
| 89 | =item B<next()> | ||
| 90 | |||
| 91 | Returns the next sequence as a Bio::SeqReader::FastqRecord object. | ||
| 92 | |||
| 93 | while ( my $so = $in->next() ) { | ||
| 94 | ... work with $so here ... | ||
| 95 | } | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub next { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | undef $self->{ _HEADER1 }; | ||
| 105 | undef $self->{ _SEQTEXT }; | ||
| 106 | undef $self->{ _HEADER2 }; | ||
| 107 | undef $self->{ _QUALTEXT }; | ||
| 108 | $self->{ _READER_STATE } = 'start'; | ||
| 109 | |||
| 110 | while ( my $line = $self->{ _FH }->getline() ) { | ||
| 111 | |||
| 112 | #print "$line\n"; | ||
| 113 | chomp $line; | ||
| 114 | next if $line =~ /^\s*$/; | ||
| 115 | $line =~ s/^\s+//; | ||
| 116 | $line =~ s/\s+$//; | ||
| 117 | |||
| 118 | if ( $self->{ _READER_STATE } eq 'start' ) { | ||
| 119 | die if $line !~ /^\@/; | ||
| 120 | $line =~ s/^\@//; | ||
| 121 | $self->{ _HEADER1 } = $line; | ||
| 122 | $self->{ _READER_STATE } = 'seqtext'; | ||
| 123 | } | ||
| 124 | |||
| 125 | elsif ( $self->{ _READER_STATE } eq 'seqtext' ) { | ||
| 126 | |||
| 127 | if ( $line =~ /^\+/ ) { | ||
| 128 | $line =~ s/^\+//; | ||
| 129 | $self->{ _HEADER2 } = $line; | ||
| 130 | $self->{ _READER_STATE } = 'qualtext'; | ||
| 131 | } | ||
| 132 | |||
| 133 | else { | ||
| 134 | $self->{ _SEQTEXT } .= $line; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | elsif ( $self->{ _READER_STATE } eq 'qualtext' ) { | ||
| 139 | |||
| 140 | $self->{ _QUALTEXT } .= $line; | ||
| 141 | |||
| 142 | die if length $self->{ _QUALTEXT } > length $self->{ _SEQTEXT }; | ||
| 143 | |||
| 144 | if ( length $self->{ _QUALTEXT } == length $self->{ _SEQTEXT } ) { | ||
| 145 | my $id = $self->{ _HEADER1 }; | ||
| 146 | $id =~ s/\s+.*//; | ||
| 147 | my $so = new Bio::SeqReader::FastqRecord(); | ||
| 148 | $so->display_id( $id ); | ||
| 149 | $so->seq( $self->{ _SEQTEXT } ); | ||
| 150 | $so->quals( $self->{ _QUALTEXT } ); | ||
| 151 | $so->header1( $self->{ _HEADER1 } ); | ||
| 152 | $so->header2( $self->{ _HEADER2 } ); | ||
| 153 | return $so; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | |||
| 161 | =head1 EXTERNAL DEPENDENCIES | ||
| 162 | |||
| 163 | Perl core. | ||
| 164 | |||
| 165 | =head1 EXAMPLES | ||
| 166 | |||
| 167 | # Open and read a file in FASTQ format | ||
| 168 | my $fh = new IO::File( 'foo.fastq' ); | ||
| 169 | my $in = new Bio::SeqReader( fh => $fh ); | ||
| 170 | while ( my $so = $in->next() ) { | ||
| 171 | my $s = $so->seq(); # $so is a Bio::SeqReader::FastqRecord | ||
| 172 | . . . | ||
| 173 | } | ||
| 174 | |||
| 175 | # Open and read a gzipped file in FASTQ format | ||
| 176 | my $fh = new IO::Uncompress::AnyUncompress( 'foo.fastq.gz' ); | ||
| 177 | my $in = new Bio::SeqReader( fh => $fh ); | ||
| 178 | while ( my $so = $in->next() ) { | ||
| 179 | my $s = $so->seq(); # $so is a Bio::SeqReader::FastqRecord | ||
| 180 | . . . | ||
| 181 | } | ||
| 182 | |||
| 183 | =head1 BUGS | ||
| 184 | |||
| 185 | None reported yet, but let me know. | ||
| 186 | |||
| 187 | =head1 SEE ALSO | ||
| 188 | |||
| 189 | Bio::SeqReader::FastqRecord, Bio::SeqReader. | ||
| 190 | |||
| 191 | =head1 AUTHOR | ||
| 192 | |||
| 193 | John A. Crow E<lt>jac_at_cpan_dot_orgE<gt> | ||
| 194 | |||
| 195 | =head1 COPYRIGHT AND LICENSE | ||
| 196 | |||
| 197 | This library is free software; you can redistribute it and/or modify | ||
| 198 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||
| 199 | at your option, any later version of Perl 5 you may have available. | ||
| 200 | |||
| 201 | Copyright (C) 2012 by John A. Crow | ||
| 202 | Copyright (C) 2012 by National Center for Genome Resources | ||
| 203 | |||
| 204 | =cut | ||
| 205 |