annotate tools/fastq/fastq_stats.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 from galaxy_utils.sequence.fastq import fastqReader, fastqAggregator
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 VALID_NUCLEOTIDES = [ 'A', 'C', 'G', 'T', 'N' ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 VALID_COLOR_SPACE = map( str, range( 7 ) ) + [ '.' ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 SUMMARY_STAT_ORDER = ['read_count', 'min_score', 'max_score', 'sum_score', 'mean_score', 'q1', 'med_score', 'q3', 'iqr', 'left_whisker', 'right_whisker' ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 input_filename = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 output_filename = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 input_type = sys.argv[3] or 'sanger'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 aggregator = fastqAggregator()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 num_reads = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 fastq_read = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 for num_reads, fastq_read in enumerate( fastqReader( open( input_filename ), format = input_type ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 aggregator.consume_read( fastq_read )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 out = open( output_filename, 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 valid_nucleotides = VALID_NUCLEOTIDES
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 if fastq_read:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 if fastq_read.sequence_space == 'base':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 out.write( '#column\tcount\tmin\tmax\tsum\tmean\tQ1\tmed\tQ3\tIQR\tlW\trW\toutliers\tA_Count\tC_Count\tG_Count\tT_Count\tN_Count\tother_bases\tother_base_count\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 out.write( '#column\tcount\tmin\tmax\tsum\tmean\tQ1\tmed\tQ3\tIQR\tlW\trW\toutliers\t0_Count\t1_Count\t2_Count\t3_Count\t4_Count\t5_Count\t6_Count\t._Count\tother_bases\tother_base_count\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 valid_nucleotides = VALID_COLOR_SPACE
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 for i in range( aggregator.get_max_read_length() ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 column_stats = aggregator.get_summary_statistics_for_column( i )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 out.write( '%i\t' % ( i + 1 ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 out.write( '%s\t' * len( SUMMARY_STAT_ORDER ) % tuple( [ column_stats[ key ] for key in SUMMARY_STAT_ORDER ] ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 out.write( '%s\t' % ','.join( map( str, column_stats['outliers'] ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 base_counts = aggregator.get_base_counts_for_column( i )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 for nuc in valid_nucleotides:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 out.write( "%s\t" % base_counts.get( nuc, 0 ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 extra_nucs = sorted( [ nuc for nuc in base_counts.keys() if nuc not in valid_nucleotides ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 out.write( "%s\t%s\n" % ( ','.join( extra_nucs ), ','.join( str( base_counts[nuc] ) for nuc in extra_nucs ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if num_reads is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 print "No valid fastq reads could be processed."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 print "%i fastq reads were processed." % ( num_reads + 1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 print "Based upon quality values and sequence characters, the input data is valid for: %s" % ( ", ".join( aggregator.get_valid_formats() ) or "None" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 ascii_range = aggregator.get_ascii_range()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 decimal_range = aggregator.get_decimal_range()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 print "Input ASCII range: %s(%i) - %s(%i)" % ( repr( ascii_range[0] ), ord( ascii_range[0] ), repr( ascii_range[1] ), ord( ascii_range[1] ) ) #print using repr, since \x00 (null) causes info truncation in galaxy when printed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 print "Input decimal range: %i - %i" % ( decimal_range[0], decimal_range[1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 if __name__ == "__main__": main()