annotate fastq_stats.py @ 0:9b7b4e0ca9db draft

Imported from capsule None
author devteam
date Mon, 27 Jan 2014 09:29:03 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
1 #Dan Blankenberg
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
2 import sys
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
3 from galaxy_utils.sequence.fastq import fastqReader, fastqAggregator
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
4
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
5 VALID_NUCLEOTIDES = [ 'A', 'C', 'G', 'T', 'N' ]
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
6 VALID_COLOR_SPACE = map( str, range( 7 ) ) + [ '.' ]
9b7b4e0ca9db Imported from capsule None
devteam
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' ]
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
8
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
9 def main():
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
10 input_filename = sys.argv[1]
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
11 output_filename = sys.argv[2]
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
12 input_type = sys.argv[3] or 'sanger'
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
13
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
14 aggregator = fastqAggregator()
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
15 num_reads = None
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
16 fastq_read = None
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
17 for num_reads, fastq_read in enumerate( fastqReader( open( input_filename ), format = input_type ) ):
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
18 aggregator.consume_read( fastq_read )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
19 out = open( output_filename, 'wb' )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
20 valid_nucleotides = VALID_NUCLEOTIDES
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
21 if fastq_read:
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
22 if fastq_read.sequence_space == 'base':
9b7b4e0ca9db Imported from capsule None
devteam
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' )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
24 else:
9b7b4e0ca9db Imported from capsule None
devteam
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' )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
26 valid_nucleotides = VALID_COLOR_SPACE
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
27 for i in range( aggregator.get_max_read_length() ):
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
28 column_stats = aggregator.get_summary_statistics_for_column( i )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
29 out.write( '%i\t' % ( i + 1 ) )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
30 out.write( '%s\t' * len( SUMMARY_STAT_ORDER ) % tuple( [ column_stats[ key ] for key in SUMMARY_STAT_ORDER ] ) )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
31 out.write( '%s\t' % ','.join( map( str, column_stats['outliers'] ) ) )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
32 base_counts = aggregator.get_base_counts_for_column( i )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
33 for nuc in valid_nucleotides:
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
34 out.write( "%s\t" % base_counts.get( nuc, 0 ) )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
35 extra_nucs = sorted( [ nuc for nuc in base_counts.keys() if nuc not in valid_nucleotides ] )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
36 out.write( "%s\t%s\n" % ( ','.join( extra_nucs ), ','.join( str( base_counts[nuc] ) for nuc in extra_nucs ) ) )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
37 out.close()
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
38 if num_reads is None:
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
39 print "No valid fastq reads could be processed."
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
40 else:
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
41 print "%i fastq reads were processed." % ( num_reads + 1 )
9b7b4e0ca9db Imported from capsule None
devteam
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" )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
43 ascii_range = aggregator.get_ascii_range()
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
44 decimal_range = aggregator.get_decimal_range()
9b7b4e0ca9db Imported from capsule None
devteam
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
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
46 print "Input decimal range: %i - %i" % ( decimal_range[0], decimal_range[1] )
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
47
9b7b4e0ca9db Imported from capsule None
devteam
parents:
diff changeset
48 if __name__ == "__main__": main()