annotate picard_SamToFastq_wrapper.py @ 4:f4d018471628 draft default tip

Uploaded
author jpruab
date Tue, 13 Aug 2013 12:09:14 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
f4d018471628 Uploaded
jpruab
parents:
diff changeset
1 #!/usr/bin/env python
f4d018471628 Uploaded
jpruab
parents:
diff changeset
2 #Dan Blankenberg
f4d018471628 Uploaded
jpruab
parents:
diff changeset
3
f4d018471628 Uploaded
jpruab
parents:
diff changeset
4 """
f4d018471628 Uploaded
jpruab
parents:
diff changeset
5 A wrapper script for running the Picard SamToFastq command. Allows parsing read groups into separate files.
f4d018471628 Uploaded
jpruab
parents:
diff changeset
6 """
f4d018471628 Uploaded
jpruab
parents:
diff changeset
7
f4d018471628 Uploaded
jpruab
parents:
diff changeset
8 import sys, optparse, os, tempfile, subprocess, shutil
f4d018471628 Uploaded
jpruab
parents:
diff changeset
9
f4d018471628 Uploaded
jpruab
parents:
diff changeset
10 CHUNK_SIZE = 2**20 #1mb
f4d018471628 Uploaded
jpruab
parents:
diff changeset
11
f4d018471628 Uploaded
jpruab
parents:
diff changeset
12
f4d018471628 Uploaded
jpruab
parents:
diff changeset
13 def cleanup_before_exit( tmp_dir ):
f4d018471628 Uploaded
jpruab
parents:
diff changeset
14 if tmp_dir and os.path.exists( tmp_dir ):
f4d018471628 Uploaded
jpruab
parents:
diff changeset
15 shutil.rmtree( tmp_dir )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
16
f4d018471628 Uploaded
jpruab
parents:
diff changeset
17 def open_file_from_option( filename, mode = 'rb' ):
f4d018471628 Uploaded
jpruab
parents:
diff changeset
18 if filename:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
19 return open( filename, mode = mode )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
20 return None
f4d018471628 Uploaded
jpruab
parents:
diff changeset
21
f4d018471628 Uploaded
jpruab
parents:
diff changeset
22 def __main__():
f4d018471628 Uploaded
jpruab
parents:
diff changeset
23 #Parse Command Line
f4d018471628 Uploaded
jpruab
parents:
diff changeset
24 parser = optparse.OptionParser()
f4d018471628 Uploaded
jpruab
parents:
diff changeset
25 parser.add_option( '-p', '--pass_through', dest='pass_through_options', action='append', type="string", help='These options are passed through directly to PICARD, without any modification.' )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
26 parser.add_option( '-1', '--read_group_file_1', dest='read_group_file_1', action='store', type="string", default=None, help='Read Group 1 output file, when using multiple readgroups' )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
27 parser.add_option( '-2', '--read_group_file_2', dest='read_group_file_2', action='store', type="string", default=None, help='Read Group 2 output file, when using multiple readgroups and paired end' )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
28 parser.add_option( '', '--stdout', dest='stdout', action='store', type="string", default=None, help='If specified, the output of stdout will be written to this file.' )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
29 parser.add_option( '', '--stderr', dest='stderr', action='store', type="string", default=None, help='If specified, the output of stderr will be written to this file.' )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
30 parser.add_option( '-n', '--new_files_path', dest='new_files_path', action='store', type="string", default=None, help='new_files_path')
f4d018471628 Uploaded
jpruab
parents:
diff changeset
31 parser.add_option( '-i', '--file_id_1', dest='file_id_1', action='store', type="string", default=None, help='file_id_1')
f4d018471628 Uploaded
jpruab
parents:
diff changeset
32 parser.add_option( '-f', '--file_id_2', dest='file_id_2', action='store', type="string", default=None, help='file_id_2')
f4d018471628 Uploaded
jpruab
parents:
diff changeset
33 (options, args) = parser.parse_args()
f4d018471628 Uploaded
jpruab
parents:
diff changeset
34
f4d018471628 Uploaded
jpruab
parents:
diff changeset
35 tmp_dir = tempfile.mkdtemp( prefix='tmp-picard-' )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
36 if options.pass_through_options:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
37 cmd = ' '.join( options.pass_through_options )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
38 else:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
39 cmd = ''
f4d018471628 Uploaded
jpruab
parents:
diff changeset
40 if options.new_files_path is not None:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
41 print 'Creating FASTQ files by Read Group'
f4d018471628 Uploaded
jpruab
parents:
diff changeset
42 assert None not in [ options.read_group_file_1, options.new_files_path, options.file_id_1 ], 'When using read group aware, you need to specify --read_group_file_1, --read_group_file_2 (when paired end), --new_files_path, and --file_id'
f4d018471628 Uploaded
jpruab
parents:
diff changeset
43 cmd = '%s OUTPUT_DIR="%s"' % ( cmd, tmp_dir)
f4d018471628 Uploaded
jpruab
parents:
diff changeset
44 #set up stdout and stderr output options
f4d018471628 Uploaded
jpruab
parents:
diff changeset
45 stdout = open_file_from_option( options.stdout, mode = 'wb' )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
46 if stdout is None:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
47 stdout = sys.stdout
f4d018471628 Uploaded
jpruab
parents:
diff changeset
48 stderr = open_file_from_option( options.stderr, mode = 'wb' )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
49 #if no stderr file is specified, we'll use our own
f4d018471628 Uploaded
jpruab
parents:
diff changeset
50 if stderr is None:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
51 stderr = tempfile.NamedTemporaryFile( prefix="picard-stderr-", dir=tmp_dir )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
52
f4d018471628 Uploaded
jpruab
parents:
diff changeset
53 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
54 return_code = proc.wait()
f4d018471628 Uploaded
jpruab
parents:
diff changeset
55
f4d018471628 Uploaded
jpruab
parents:
diff changeset
56 if return_code:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
57 stderr_target = sys.stderr
f4d018471628 Uploaded
jpruab
parents:
diff changeset
58 else:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
59 stderr_target = sys.stdout
f4d018471628 Uploaded
jpruab
parents:
diff changeset
60 stderr.flush()
f4d018471628 Uploaded
jpruab
parents:
diff changeset
61 stderr.seek(0)
f4d018471628 Uploaded
jpruab
parents:
diff changeset
62 while True:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
63 chunk = stderr.read( CHUNK_SIZE )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
64 if chunk:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
65 stderr_target.write( chunk )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
66 else:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
67 break
f4d018471628 Uploaded
jpruab
parents:
diff changeset
68 stderr.close()
f4d018471628 Uploaded
jpruab
parents:
diff changeset
69 #if rg aware, put files where they belong
f4d018471628 Uploaded
jpruab
parents:
diff changeset
70 if options.new_files_path is not None:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
71 fastq_1_name = options.read_group_file_1
f4d018471628 Uploaded
jpruab
parents:
diff changeset
72 fastq_2_name = options.read_group_file_2
f4d018471628 Uploaded
jpruab
parents:
diff changeset
73 file_id_1 = options.file_id_1
f4d018471628 Uploaded
jpruab
parents:
diff changeset
74 file_id_2 = options.file_id_2
f4d018471628 Uploaded
jpruab
parents:
diff changeset
75 if file_id_2 is None:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
76 file_id_2 = file_id_1
f4d018471628 Uploaded
jpruab
parents:
diff changeset
77 for filename in sorted( os.listdir( tmp_dir ) ):
f4d018471628 Uploaded
jpruab
parents:
diff changeset
78 if filename.endswith( '_1.fastq' ):
f4d018471628 Uploaded
jpruab
parents:
diff changeset
79 if fastq_1_name:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
80 shutil.move( os.path.join( tmp_dir, filename ), fastq_1_name )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
81 fastq_1_name = None
f4d018471628 Uploaded
jpruab
parents:
diff changeset
82 else:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
83 shutil.move( os.path.join( tmp_dir, filename ), os.path.join( options.new_files_path, 'primary_%s_%s - 1_visible_fastqsanger' % ( file_id_1, filename[:-len( '_1.fastq' )] ) ) )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
84 elif filename.endswith( '_2.fastq' ):
f4d018471628 Uploaded
jpruab
parents:
diff changeset
85 if fastq_2_name:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
86 shutil.move( os.path.join( tmp_dir, filename ), fastq_2_name )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
87 fastq_2_name = None
f4d018471628 Uploaded
jpruab
parents:
diff changeset
88 else:
f4d018471628 Uploaded
jpruab
parents:
diff changeset
89 shutil.move( os.path.join( tmp_dir, filename ), os.path.join( options.new_files_path, 'primary_%s_%s - 2_visible_fastqsanger' % ( file_id_2, filename[:-len( '_2.fastq' )] ) ) )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
90
f4d018471628 Uploaded
jpruab
parents:
diff changeset
91 cleanup_before_exit( tmp_dir )
f4d018471628 Uploaded
jpruab
parents:
diff changeset
92
f4d018471628 Uploaded
jpruab
parents:
diff changeset
93 if __name__=="__main__": __main__()