3 # Copyright (C) 2006-2009 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Routines for manipulation of FASTQ files and FASTQ entries.
27 # http://maq.sourceforge.net/fastq.shtml
30 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
37 use vars qw( @ISA @EXPORT );
45 @ISA = qw( Exporter );
47 use Inline ( C => <<'END_C', DIRECTORY => $ENV{ "BP_TMP" } );
50 About Phred or FASTQ format:
52 This format is an adaption of the fasta format that contains quality scores.
53 However, the fastq format is not completely compatible with the fastq files
54 currently in existence, which is read by various applications (for example,
55 BioPerl). Because a larger dynamic range of quality scores is used, the
56 quality scores are encoded in ASCII as 64+score, instead of the standard
57 32+score. This method is used to avoid running into non-printable characters.
60 About Solexa or SCARF format:
62 SCARF (Solexa compact ASCII read format)—This easy-to-parse text based format,
63 stores all the information for a single read in one line. Allowed values are
64 --numeric and --symbolic. --numeric outputs the quality values as a
65 space-separated string of numbers. --symbolic outputs the quality values as a
66 compact string of ASCII characters. Subtract 64 from the ASCII code to get the
67 corresponding quality values. Under the current numbering scheme, quality
68 values can theoretically be as low as -5, which has an ASCII encoding of 59=';'.
72 http://maq.sourceforge.net/fastq.shtml */
75 int phred2dec( char c )
77 /* Martin A. Hansen, July 2009 */
79 /* Converts a Phred score in octal (a char) to a decimal score, */
80 /* which is returned. */
84 score = ( int ) c - 33;
90 int solexa2dec( char c )
92 /* Martin A. Hansen, July 2009 */
94 /* Converts a Solexa score in octal (a char) to a decimal score, */
95 /* which is returned. */
99 score = ( int ) c - 64;
105 // int solexa2dec( char c )
107 // /* Martin A. Hansen, July 2009 */
109 // /* Converts a Solexa score in octal (a char) to a decimal score, */
110 // /* which is returned. */
112 // /* http://maq.sourceforge.net/fastq.shtml */
113 // /* $Q = 10 * log(1 + 10 ** (ord($sq) - 64) / 10.0)) / log(10); */
116 // int ascii = ( int ) c - 64;
118 // score = 10 * log( 1 + pow( 10, ascii / 10 ) ) / log( 10 );
120 // // printf( "char: %c ascii: %d score: %d\n", c, ascii, score );
126 char dec2phred( int score )
128 /* Martin A. Hansen, July 2009 */
130 /* Converts a decimal score to an octal score (a char) in the Phred range, */
131 /* which is returned. */
135 c = ( char ) score + 33;
141 char dec2solexa( int score )
143 /* Martin A. Hansen, September 2009 */
145 /* Converts a decimal score to an octal score (a char) in the Solexa range, */
146 /* which is returned. */
150 c = ( char ) score + 64;
156 char solexa2phred( char score )
158 /* Martin A. Hansen, September 2009 */
160 /* Converts an octal score in the Solexa range to an octal score */
161 /* in the Pred range, which is returned. */
166 dec = solexa2dec( score );
173 char phred2solexa( char score )
175 /* Martin A. Hansen, September 2009 */
177 /* Converts an octal score in the Solexa range to an octal score */
178 /* in the Pred range, which is returned. */
183 dec = phred2dec( score );
190 void solexa_str2phred_str( char *scores )
192 /* Martin A. Hansen, September 2009 */
194 /* Converts a string of Solexa octal scores to Phred octal scores in-line. */
198 for ( i = 0; i < strlen( scores ); i++ ) {
199 scores[ i ] = solexa2phred( scores[ i ] );
204 void phred_str2solexa_str( char *scores )
206 /* Martin A. Hansen, September 2009 */
208 /* Converts a string of Phred octal scores to Solexa octal scores in-line. */
212 for ( i = 0; i < strlen( scores ); i++ ) {
213 scores[ i ] = phred2solexa( scores[ i ] );
218 double phred_str_mean( char *scores )
220 /* Martin A. Hansen, November 2009 */
222 /* Calculates the mean score as a float which is retuned. */
229 len = strlen( scores );
231 for ( i = 0; i < len; i++ ) {
232 sum += phred2dec( scores[ i ] );
235 mean = ( double ) sum / ( double ) len;
241 double solexa_str_mean( char *scores )
243 /* Martin A. Hansen, November 2009 */
245 /* Calculates the mean score as a float which is retuned. */
252 len = strlen( scores );
254 for ( i = 0; i < len; i++ ) {
255 sum += solexa2dec( scores[ i ] );
258 mean = ( double ) sum / ( double ) len;
264 void softmask_solexa_str( char *seq, char *scores, int threshold )
266 /* Martin A. Hansen, July 2009 */
268 /* Given a sequence string and a score string (in Solexa range) */
269 /* lowercases all residues where the decimal score is below a given threshold. */
273 for ( i = 0; i < strlen( seq ); i++ )
275 if ( solexa2dec( scores[ i ] ) < threshold ) {
276 seq[ i ] = tolower( seq[ i ] );
282 void softmask_phred_str( char *seq, char *scores, int threshold )
284 /* Martin A. Hansen, July 2009 */
286 /* Given a sequence string and a score string (in Phred range) */
287 /* lowercases all residues where the decimal score is below a given threshold. */
291 for ( i = 0; i < strlen( seq ); i++ )
293 if ( phred2dec( scores[ i ] ) < threshold ) {
294 seq[ i ] = tolower( seq[ i ] );
303 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
306 sub solexa_str2dec_str
308 # Martin A. Hansen, September 2009
310 # Converts a string of Solexa octal scores to a ; separated
311 # string of decimal scores.
313 my ( $scores, # Solexa scores
318 $scores =~ s/(.)/ solexa2dec( $1 ) . ";"/eg;
324 sub phred_str2dec_str
326 # Martin A. Hansen, September 2009
328 # Converts a string of Phred octal scores to a ; separated
329 # string of decimal scores.
331 my ( $scores, # Phred scores
336 $scores =~ s/(.)/ phred2dec( $1 ) . ";"/eg;
342 sub dec_str2solexa_str
344 # Martin A. Hansen, May 2010.
346 # Converts a ; separated string of decimal scores to a
347 # string of Solexa octal scores.
349 my ( $scores, # Decimal score string
354 $scores =~ s/(\d{1,2});?/dec2solexa( $1 )/eg;
362 # Martin A. Hansen, July 2009.
364 # Gets the next FASTQ entry from a given filehandle.
366 my ( $fh, # filehandle
371 my ( $seq, $seq_name, $qual, $qual_name );
387 return wantarray ? ( $seq_name, $seq, $qual ) : [ $seq_name, $seq, $qual ];
393 # Martin A. Hansen, July 2009.
395 # Output a FASTQ entry to STDOUT or a filehandle.
397 my ( $entry, # FASTQ entry
398 $fh, # filehandle - OPTIONAL
405 print $fh "@" . $entry->[ SEQ_NAME ] . "\n";
406 print $fh $entry->[ SEQ ] . "\n";
408 print $fh $entry->[ SCORES ] . "\n";
414 # Martin A. Hansen, July 2009.
416 # Converts a FASTQ entry to a Biopiece record, where
417 # the FASTQ quality scores are converted to numerics.
419 my ( $entry, # FASTQ entry,
426 $record->{ 'SEQ' } = $entry->[ SEQ ];
427 $record->{ 'SEQ_NAME' } = $entry->[ SEQ_NAME ];
428 $record->{ 'SCORES' } = $entry->[ SCORES ];
429 $record->{ 'SEQ_LEN' } = length $entry->[ SEQ ];
431 return wantarray ? %{ $record } : $record;
437 # Martin A. Hansen, July 2009.
439 # Converts a Biopiece record to a FASTQ entry.
441 my ( $record, # Biopiece record
448 if ( exists $record->{ 'SEQ' } and exists $record->{ 'SEQ_NAME' } and exists $record->{ 'SCORES' } )
450 $list->[ SEQ_NAME ] = $record->{ 'SEQ_NAME' };
451 $list->[ SEQ ] = $record->{ 'SEQ' };
452 $list->[ SCORES ] = $record->{ 'SCORES' };
454 $list->[ SCORES ] =~ s/(\d+);/chr( ( $1 <= 93 ? $1 : 93 ) + 33 )/ge;
456 return wantarray ? @{ $list } : $list;
463 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<