PageRenderTime 86ms CodeModel.GetById 74ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/tools/fastq/fastq_stats.py

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