annotate tools/fastq/fastq_trimmer.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -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, fastqWriter
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 input_filename = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 output_filename = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 left_offset = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 right_offset = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 percent_offsets = sys.argv[5] == 'offsets_percent'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 input_type = sys.argv[6] or 'sanger'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 keep_zero_length = sys.argv[7] == 'keep_zero_length'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 out = fastqWriter( open( output_filename, 'wb' ), format = input_type )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 num_reads_excluded = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 num_reads = 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 if percent_offsets:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 left_column_offset = int( round( float( left_offset ) / 100.0 * float( len( fastq_read ) ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 right_column_offset = int( round( float( right_offset ) / 100.0 * float( len( fastq_read ) ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 left_column_offset = int( left_offset )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 right_column_offset = int( right_offset )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 if right_column_offset > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 right_column_offset = -right_column_offset
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 right_column_offset = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 fastq_read = fastq_read.slice( left_column_offset, right_column_offset )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 if keep_zero_length or len( fastq_read ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 out.write( fastq_read )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 num_reads_excluded += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 if num_reads is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 print "No valid fastq reads could be processed."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 print "%i fastq reads were processed." % ( num_reads + 1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if num_reads_excluded:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 print "%i reads of zero length were excluded from the output." % num_reads_excluded
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 if __name__ == "__main__": main()