annotate picard_SamToFastq_wrapper.py @ 140:08b3cb5ce651 draft

Uploaded
author devteam
date Thu, 27 Feb 2014 01:03:15 -0500
parents 3321bce585e5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
3321bce585e5 Uploaded
devteam
parents:
diff changeset
1 #!/usr/bin/env python
3321bce585e5 Uploaded
devteam
parents:
diff changeset
2 #Dan Blankenberg
3321bce585e5 Uploaded
devteam
parents:
diff changeset
3
3321bce585e5 Uploaded
devteam
parents:
diff changeset
4 """
3321bce585e5 Uploaded
devteam
parents:
diff changeset
5 A wrapper script for running the Picard SamToFastq command. Allows parsing read groups into separate files.
3321bce585e5 Uploaded
devteam
parents:
diff changeset
6 """
3321bce585e5 Uploaded
devteam
parents:
diff changeset
7
3321bce585e5 Uploaded
devteam
parents:
diff changeset
8 import sys, optparse, os, tempfile, subprocess, shutil
3321bce585e5 Uploaded
devteam
parents:
diff changeset
9
3321bce585e5 Uploaded
devteam
parents:
diff changeset
10 CHUNK_SIZE = 2**20 #1mb
3321bce585e5 Uploaded
devteam
parents:
diff changeset
11
3321bce585e5 Uploaded
devteam
parents:
diff changeset
12
3321bce585e5 Uploaded
devteam
parents:
diff changeset
13 def cleanup_before_exit( tmp_dir ):
3321bce585e5 Uploaded
devteam
parents:
diff changeset
14 if tmp_dir and os.path.exists( tmp_dir ):
3321bce585e5 Uploaded
devteam
parents:
diff changeset
15 shutil.rmtree( tmp_dir )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
16
3321bce585e5 Uploaded
devteam
parents:
diff changeset
17 def open_file_from_option( filename, mode = 'rb' ):
3321bce585e5 Uploaded
devteam
parents:
diff changeset
18 if filename:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
19 return open( filename, mode = mode )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
20 return None
3321bce585e5 Uploaded
devteam
parents:
diff changeset
21
3321bce585e5 Uploaded
devteam
parents:
diff changeset
22 def __main__():
3321bce585e5 Uploaded
devteam
parents:
diff changeset
23 #Parse Command Line
3321bce585e5 Uploaded
devteam
parents:
diff changeset
24 parser = optparse.OptionParser()
3321bce585e5 Uploaded
devteam
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.' )
3321bce585e5 Uploaded
devteam
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' )
3321bce585e5 Uploaded
devteam
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' )
3321bce585e5 Uploaded
devteam
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.' )
3321bce585e5 Uploaded
devteam
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.' )
3321bce585e5 Uploaded
devteam
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')
3321bce585e5 Uploaded
devteam
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')
3321bce585e5 Uploaded
devteam
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')
3321bce585e5 Uploaded
devteam
parents:
diff changeset
33 (options, args) = parser.parse_args()
3321bce585e5 Uploaded
devteam
parents:
diff changeset
34
3321bce585e5 Uploaded
devteam
parents:
diff changeset
35 tmp_dir = tempfile.mkdtemp( prefix='tmp-picard-' )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
36 if options.pass_through_options:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
37 cmd = ' '.join( options.pass_through_options )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
38 else:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
39 cmd = ''
3321bce585e5 Uploaded
devteam
parents:
diff changeset
40 if options.new_files_path is not None:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
41 print 'Creating FASTQ files by Read Group'
3321bce585e5 Uploaded
devteam
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'
3321bce585e5 Uploaded
devteam
parents:
diff changeset
43 cmd = '%s OUTPUT_DIR="%s"' % ( cmd, tmp_dir)
3321bce585e5 Uploaded
devteam
parents:
diff changeset
44 #set up stdout and stderr output options
3321bce585e5 Uploaded
devteam
parents:
diff changeset
45 stdout = open_file_from_option( options.stdout, mode = 'wb' )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
46 if stdout is None:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
47 stdout = sys.stdout
3321bce585e5 Uploaded
devteam
parents:
diff changeset
48 stderr = open_file_from_option( options.stderr, mode = 'wb' )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
49 #if no stderr file is specified, we'll use our own
3321bce585e5 Uploaded
devteam
parents:
diff changeset
50 if stderr is None:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
51 stderr = tempfile.NamedTemporaryFile( prefix="picard-stderr-", dir=tmp_dir )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
52
3321bce585e5 Uploaded
devteam
parents:
diff changeset
53 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
54 return_code = proc.wait()
3321bce585e5 Uploaded
devteam
parents:
diff changeset
55
3321bce585e5 Uploaded
devteam
parents:
diff changeset
56 if return_code:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
57 stderr_target = sys.stderr
3321bce585e5 Uploaded
devteam
parents:
diff changeset
58 else:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
59 stderr_target = sys.stdout
3321bce585e5 Uploaded
devteam
parents:
diff changeset
60 stderr.flush()
3321bce585e5 Uploaded
devteam
parents:
diff changeset
61 stderr.seek(0)
3321bce585e5 Uploaded
devteam
parents:
diff changeset
62 while True:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
63 chunk = stderr.read( CHUNK_SIZE )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
64 if chunk:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
65 stderr_target.write( chunk )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
66 else:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
67 break
3321bce585e5 Uploaded
devteam
parents:
diff changeset
68 stderr.close()
3321bce585e5 Uploaded
devteam
parents:
diff changeset
69 #if rg aware, put files where they belong
3321bce585e5 Uploaded
devteam
parents:
diff changeset
70 if options.new_files_path is not None:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
71 fastq_1_name = options.read_group_file_1
3321bce585e5 Uploaded
devteam
parents:
diff changeset
72 fastq_2_name = options.read_group_file_2
3321bce585e5 Uploaded
devteam
parents:
diff changeset
73 file_id_1 = options.file_id_1
3321bce585e5 Uploaded
devteam
parents:
diff changeset
74 file_id_2 = options.file_id_2
3321bce585e5 Uploaded
devteam
parents:
diff changeset
75 if file_id_2 is None:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
76 file_id_2 = file_id_1
3321bce585e5 Uploaded
devteam
parents:
diff changeset
77 for filename in sorted( os.listdir( tmp_dir ) ):
3321bce585e5 Uploaded
devteam
parents:
diff changeset
78 if filename.endswith( '_1.fastq' ):
3321bce585e5 Uploaded
devteam
parents:
diff changeset
79 if fastq_1_name:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
80 shutil.move( os.path.join( tmp_dir, filename ), fastq_1_name )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
81 fastq_1_name = None
3321bce585e5 Uploaded
devteam
parents:
diff changeset
82 else:
3321bce585e5 Uploaded
devteam
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' )] ) ) )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
84 elif filename.endswith( '_2.fastq' ):
3321bce585e5 Uploaded
devteam
parents:
diff changeset
85 if fastq_2_name:
3321bce585e5 Uploaded
devteam
parents:
diff changeset
86 shutil.move( os.path.join( tmp_dir, filename ), fastq_2_name )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
87 fastq_2_name = None
3321bce585e5 Uploaded
devteam
parents:
diff changeset
88 else:
3321bce585e5 Uploaded
devteam
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' )] ) ) )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
90
3321bce585e5 Uploaded
devteam
parents:
diff changeset
91 cleanup_before_exit( tmp_dir )
3321bce585e5 Uploaded
devteam
parents:
diff changeset
92
3321bce585e5 Uploaded
devteam
parents:
diff changeset
93 if __name__=="__main__": __main__()