annotate tools/fastq/fastq_manipulation.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, os, shutil
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import imp
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 from galaxy_utils.sequence.fastq import fastqReader, fastqWriter
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 #Read command line arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 input_filename = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 script_filename = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 output_filename = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 additional_files_path = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 input_type = sys.argv[5] or 'sanger'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 #Save script file for debuging/verification info later
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 os.mkdir( additional_files_path )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 shutil.copy( script_filename, os.path.join( additional_files_path, 'debug.txt' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 fastq_manipulator = imp.load_module( 'fastq_manipulator', open( script_filename ), script_filename, ( '', 'r', imp.PY_SOURCE ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 out = fastqWriter( open( output_filename, 'wb' ), format = input_type )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 i = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 reads_manipulated = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 for i, fastq_read in enumerate( fastqReader( open( input_filename ), format = input_type ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 new_read = fastq_manipulator.match_and_manipulate_read( fastq_read )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 if new_read:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 out.write( new_read )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 if new_read != fastq_read:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 reads_manipulated += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 if i is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 print "Your file contains no valid FASTQ reads."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 print 'Manipulated %s of %s reads (%.2f%%).' % ( reads_manipulated, i + 1, float( reads_manipulated ) / float( i + 1 ) * 100.0 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 main()