/tools/fastq/fastq_stats.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 48 lines · 43 code · 4 blank · 1 comment · 15 complexity · e712ca0211760fe2748f474db95f456b MD5 · raw file

  1. #Dan Blankenberg
  2. import sys
  3. from galaxy_utils.sequence.fastq import fastqReader, fastqAggregator
  4. VALID_NUCLEOTIDES = [ 'A', 'C', 'G', 'T', 'N' ]
  5. VALID_COLOR_SPACE = map( str, range( 7 ) ) + [ '.' ]
  6. SUMMARY_STAT_ORDER = ['read_count', 'min_score', 'max_score', 'sum_score', 'mean_score', 'q1', 'med_score', 'q3', 'iqr', 'left_whisker', 'right_whisker' ]
  7. def main():
  8. input_filename = sys.argv[1]
  9. output_filename = sys.argv[2]
  10. input_type = sys.argv[3] or 'sanger'
  11. aggregator = fastqAggregator()
  12. num_reads = None
  13. fastq_read = None
  14. for num_reads, fastq_read in enumerate( fastqReader( open( input_filename ), format = input_type ) ):
  15. aggregator.consume_read( fastq_read )
  16. out = open( output_filename, 'wb' )
  17. valid_nucleotides = VALID_NUCLEOTIDES
  18. if fastq_read:
  19. if fastq_read.sequence_space == 'base':
  20. 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' )
  21. else:
  22. 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' )
  23. valid_nucleotides = VALID_COLOR_SPACE
  24. for i in range( aggregator.get_max_read_length() ):
  25. column_stats = aggregator.get_summary_statistics_for_column( i )
  26. out.write( '%i\t' % ( i + 1 ) )
  27. out.write( '%s\t' * len( SUMMARY_STAT_ORDER ) % tuple( [ column_stats[ key ] for key in SUMMARY_STAT_ORDER ] ) )
  28. out.write( '%s\t' % ','.join( map( str, column_stats['outliers'] ) ) )
  29. base_counts = aggregator.get_base_counts_for_column( i )
  30. for nuc in valid_nucleotides:
  31. out.write( "%s\t" % base_counts.get( nuc, 0 ) )
  32. extra_nucs = sorted( [ nuc for nuc in base_counts.keys() if nuc not in valid_nucleotides ] )
  33. out.write( "%s\t%s\n" % ( ','.join( extra_nucs ), ','.join( str( base_counts[nuc] ) for nuc in extra_nucs ) ) )
  34. out.close()
  35. if num_reads is None:
  36. print "No valid fastq reads could be processed."
  37. else:
  38. print "%i fastq reads were processed." % ( num_reads + 1 )
  39. print "Based upon quality values and sequence characters, the input data is valid for: %s" % ( ", ".join( aggregator.get_valid_formats() ) or "None" )
  40. ascii_range = aggregator.get_ascii_range()
  41. decimal_range = aggregator.get_decimal_range()
  42. 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
  43. print "Input decimal range: %i - %i" % ( decimal_range[0], decimal_range[1] )
  44. if __name__ == "__main__": main()