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