annotate picard_SamToFastq_wrapper.py @ 4:ab1f60c26526 draft

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