annotate fastq_manipulation.py @ 0:de14b969d713 draft

Imported from capsule None
author devteam
date Thu, 23 Jan 2014 12:31:03 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
1 #Dan Blankenberg
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
2 import sys, os, shutil
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
3 import imp
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
4 from galaxy_utils.sequence.fastq import fastqReader, fastqWriter
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
5
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
6 def main():
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
7 #Read command line arguments
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
8 input_filename = sys.argv[1]
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
9 script_filename = sys.argv[2]
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
10 output_filename = sys.argv[3]
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
11 additional_files_path = sys.argv[4]
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
12 input_type = sys.argv[5] or 'sanger'
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
13
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
14 #Save script file for debuging/verification info later
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
15 os.mkdir( additional_files_path )
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
16 shutil.copy( script_filename, os.path.join( additional_files_path, 'debug.txt' ) )
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
17
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
18 fastq_manipulator = imp.load_module( 'fastq_manipulator', open( script_filename ), script_filename, ( '', 'r', imp.PY_SOURCE ) )
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
19
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
20 out = fastqWriter( open( output_filename, 'wb' ), format = input_type )
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
21
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
22 i = None
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
23 reads_manipulated = 0
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
24 for i, fastq_read in enumerate( fastqReader( open( input_filename ), format = input_type ) ):
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
25 new_read = fastq_manipulator.match_and_manipulate_read( fastq_read )
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
26 if new_read:
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
27 out.write( new_read )
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
28 if new_read != fastq_read:
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
29 reads_manipulated += 1
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
30 out.close()
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
31 if i is None:
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
32 print "Your file contains no valid FASTQ reads."
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
33 else:
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
34 print 'Manipulated %s of %s reads (%.2f%%).' % ( reads_manipulated, i + 1, float( reads_manipulated ) / float( i + 1 ) * 100.0 )
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
35
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
36 if __name__ == "__main__":
de14b969d713 Imported from capsule None
devteam
parents:
diff changeset
37 main()