annotate fastq_trimmer.py @ 0:0b9feb0ed628 draft

Imported from capsule None
author devteam
date Mon, 27 Jan 2014 09:27:32 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
1 #Dan Blankenberg
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
2 import sys
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
3 from galaxy_utils.sequence.fastq import fastqReader, fastqWriter
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
4
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
5 def main():
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
6 input_filename = sys.argv[1]
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
7 output_filename = sys.argv[2]
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
8 left_offset = sys.argv[3]
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
9 right_offset = sys.argv[4]
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
10 percent_offsets = sys.argv[5] == 'offsets_percent'
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
11 input_type = sys.argv[6] or 'sanger'
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
12 keep_zero_length = sys.argv[7] == 'keep_zero_length'
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
13
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
14 out = fastqWriter( open( output_filename, 'wb' ), format = input_type )
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
15 num_reads_excluded = 0
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
16 num_reads = None
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
17 for num_reads, fastq_read in enumerate( fastqReader( open( input_filename ), format = input_type ) ):
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
18 if percent_offsets:
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
19 left_column_offset = int( round( float( left_offset ) / 100.0 * float( len( fastq_read ) ) ) )
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
20 right_column_offset = int( round( float( right_offset ) / 100.0 * float( len( fastq_read ) ) ) )
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
21 else:
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
22 left_column_offset = int( left_offset )
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
23 right_column_offset = int( right_offset )
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
24 if right_column_offset > 0:
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
25 right_column_offset = -right_column_offset
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
26 else:
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
27 right_column_offset = None
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
28 fastq_read = fastq_read.slice( left_column_offset, right_column_offset )
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
29 if keep_zero_length or len( fastq_read ):
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
30 out.write( fastq_read )
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
31 else:
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
32 num_reads_excluded += 1
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
33 out.close()
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
34 if num_reads is None:
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
35 print "No valid fastq reads could be processed."
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
36 else:
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
37 print "%i fastq reads were processed." % ( num_reads + 1 )
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
38 if num_reads_excluded:
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
39 print "%i reads of zero length were excluded from the output." % num_reads_excluded
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
40
0b9feb0ed628 Imported from capsule None
devteam
parents:
diff changeset
41 if __name__ == "__main__": main()