annotate fastq_trimmer.py @ 0:feb5479a48ff draft

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