comparison gatk2_wrapper.py @ 6:35c00763cb5c draft default tip

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/gatk2 commit cf399638ebca4250bcc15f468238a9964de97b33
author iuc
date Mon, 04 Jun 2018 05:38:15 -0400
parents f244b8209eb8
children
comparison
equal deleted inserted replaced
5:84584664264c 6:35c00763cb5c
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 #David Hoover, based on gatk by Dan Blankenberg 2 # David Hoover, based on gatk by Dan Blankenberg
3
4 """ 3 """
5 A wrapper script for running the GenomeAnalysisTK.jar commands. 4 A wrapper script for running the GenomeAnalysisTK.jar commands.
6 """ 5 """
7 6
8 import sys, optparse, os, tempfile, subprocess, shutil 7 import optparse
8 import os
9 import shutil
10 import subprocess
11 import sys
12 import tempfile
9 from binascii import unhexlify 13 from binascii import unhexlify
10 14
11 GALAXY_EXT_TO_GATK_EXT = { 'gatk_interval':'intervals', 'bam_index':'bam.bai', 'gatk_dbsnp':'dbSNP', 'picard_interval_list':'interval_list' } #items not listed here will use the galaxy extension as-is 15 GALAXY_EXT_TO_GATK_EXT = { 'gatk_interval': 'intervals', 'bam_index': 'bam.bai', 'gatk_dbsnp': 'dbSNP', 'picard_interval_list': 'interval_list' } # items not listed here will use the galaxy extension as-is
12 GALAXY_EXT_TO_GATK_FILE_TYPE = GALAXY_EXT_TO_GATK_EXT #for now, these are the same, but could be different if needed 16 GALAXY_EXT_TO_GATK_FILE_TYPE = GALAXY_EXT_TO_GATK_EXT # for now, these are the same, but could be different if needed
13 DEFAULT_GATK_PREFIX = "gatk_file" 17 DEFAULT_GATK_PREFIX = "gatk_file"
14 CHUNK_SIZE = 2**20 #1mb 18 CHUNK_SIZE = 2**20 # 1mb
15 19
16 20
17 def cleanup_before_exit( tmp_dir ): 21 def cleanup_before_exit( tmp_dir ):
18 if tmp_dir and os.path.exists( tmp_dir ): 22 if tmp_dir and os.path.exists( tmp_dir ):
19 shutil.rmtree( tmp_dir ) 23 shutil.rmtree( tmp_dir )
20 24
21 25
22 def gatk_filename_from_galaxy( galaxy_filename, galaxy_ext, target_dir = None, prefix = None ): 26 def gatk_filename_from_galaxy( galaxy_filename, galaxy_ext, target_dir=None, prefix=None ):
23 suffix = GALAXY_EXT_TO_GATK_EXT.get( galaxy_ext, galaxy_ext ) 27 suffix = GALAXY_EXT_TO_GATK_EXT.get( galaxy_ext, galaxy_ext )
24 if prefix is None: 28 if prefix is None:
25 prefix = DEFAULT_GATK_PREFIX 29 prefix = DEFAULT_GATK_PREFIX
26 if target_dir is None: 30 if target_dir is None:
27 target_dir = os.getcwd() 31 target_dir = os.getcwd()
29 os.symlink( galaxy_filename, gatk_filename ) 33 os.symlink( galaxy_filename, gatk_filename )
30 return gatk_filename 34 return gatk_filename
31 35
32 36
33 def gatk_filetype_argument_substitution( argument, galaxy_ext ): 37 def gatk_filetype_argument_substitution( argument, galaxy_ext ):
34 return argument % dict( file_type = GALAXY_EXT_TO_GATK_FILE_TYPE.get( galaxy_ext, galaxy_ext ) ) 38 return argument % dict( file_type=GALAXY_EXT_TO_GATK_FILE_TYPE.get( galaxy_ext, galaxy_ext ) )
35 39
36 40
37 def open_file_from_option( filename, mode = 'rb' ): 41 def open_file_from_option( filename, mode='rb' ):
38 if filename: 42 if filename:
39 return open( filename, mode = mode ) 43 return open( filename, mode=mode )
40 return None 44 return None
41 45
42 46
43 def html_report_from_directory( html_out, dir ): 47 def html_report_from_directory( html_out, dir ):
44 html_out.write( '<html>\n<head>\n<title>Galaxy - GATK Output</title>\n</head>\n<body>\n<p/>\n<ul>\n' ) 48 html_out.write( '<html>\n<head>\n<title>Galaxy - GATK Output</title>\n</head>\n<body>\n<p/>\n<ul>\n' )
49 53
50 def index_bam_files( bam_filenames ): 54 def index_bam_files( bam_filenames ):
51 for bam_filename in bam_filenames: 55 for bam_filename in bam_filenames:
52 bam_index_filename = "%s.bai" % bam_filename 56 bam_index_filename = "%s.bai" % bam_filename
53 if not os.path.exists( bam_index_filename ): 57 if not os.path.exists( bam_index_filename ):
54 #need to index this bam file 58 # need to index this bam file
55 stderr_name = tempfile.NamedTemporaryFile( prefix = "bam_index_stderr" ).name 59 stderr_name = tempfile.NamedTemporaryFile( prefix="bam_index_stderr" ).name
56 command = 'samtools index %s %s' % ( bam_filename, bam_index_filename ) 60 command = 'samtools index %s %s' % ( bam_filename, bam_index_filename )
57 try: 61 try:
58 subprocess.check_call( args=command, shell=True, stderr=open( stderr_name, 'wb' ) ) 62 subprocess.check_call( args=command, shell=True, stderr=open( stderr_name, 'wb' ) )
59 except: 63 except:
60 for line in open( stderr_name ): 64 for line in open( stderr_name ):
61 print >> sys.stderr, line 65 print >> sys.stderr, line
62 raise Exception( "Error indexing BAM file" ) 66 raise Exception( "Error indexing BAM file" )
63 finally: 67 finally:
64 os.unlink( stderr_name ) 68 os.unlink( stderr_name )
65 69
70
66 def __main__(): 71 def __main__():
67 #Parse Command Line 72 # Parse Command Line
68 parser = optparse.OptionParser() 73 parser = optparse.OptionParser()
69 parser.add_option( '-p', '--pass_through', dest='pass_through_options', action='append', type="string", help='These options are passed through directly to GATK, without any modification.' ) 74 parser.add_option( '-p', '--pass_through', dest='pass_through_options', action='append', type="string", help='These options are passed through directly to GATK, without any modification.' )
70 parser.add_option( '-o', '--pass_through_options', dest='pass_through_options_encoded', action='append', type="string", help='These options are passed through directly to GATK, with decoding from binascii.unhexlify.' ) 75 parser.add_option( '-o', '--pass_through_options', dest='pass_through_options_encoded', action='append', type="string", help='These options are passed through directly to GATK, with decoding from binascii.unhexlify.' )
71 parser.add_option( '-d', '--dataset', dest='datasets', action='append', type="string", nargs=4, help='"-argument" "original_filename" "galaxy_filetype" "name_prefix"' ) 76 parser.add_option( '-d', '--dataset', dest='datasets', action='append', type="string", nargs=4, help='"-argument" "original_filename" "galaxy_filetype" "name_prefix"' )
72 parser.add_option( '', '--max_jvm_heap', dest='max_jvm_heap', action='store', type="string", default=None, help='If specified, the maximum java virtual machine heap size will be set to the provide value.' ) 77 parser.add_option( '', '--max_jvm_heap', dest='max_jvm_heap', action='store', type="string", default=None, help='If specified, the maximum java virtual machine heap size will be set to the provide value.' )
91 bam_filenames = [] 96 bam_filenames = []
92 tmp_dir = tempfile.mkdtemp( prefix='tmp-gatk-' ) 97 tmp_dir = tempfile.mkdtemp( prefix='tmp-gatk-' )
93 try: 98 try:
94 if options.datasets: 99 if options.datasets:
95 for ( dataset_arg, filename, galaxy_ext, prefix ) in options.datasets: 100 for ( dataset_arg, filename, galaxy_ext, prefix ) in options.datasets:
96 gatk_filename = gatk_filename_from_galaxy( filename, galaxy_ext, target_dir = tmp_dir, prefix = prefix ) 101 gatk_filename = gatk_filename_from_galaxy( filename, galaxy_ext, target_dir=tmp_dir, prefix=prefix )
97 if dataset_arg: 102 if dataset_arg:
98 cmd = '%s %s "%s"' % ( cmd, gatk_filetype_argument_substitution( dataset_arg, galaxy_ext ), gatk_filename ) 103 cmd = '%s %s "%s"' % ( cmd, gatk_filetype_argument_substitution( dataset_arg, galaxy_ext ), gatk_filename )
99 if galaxy_ext == "bam": 104 if galaxy_ext == "bam":
100 bam_filenames.append( gatk_filename ) 105 bam_filenames.append( gatk_filename )
101 if galaxy_ext == 'fasta': 106 if galaxy_ext == 'fasta':
102 subprocess.check_call( 'samtools faidx "%s"' % gatk_filename, shell=True ) 107 subprocess.check_call( 'samtools faidx "%s"' % gatk_filename, shell=True )
103 subprocess.check_call( 'java -jar %s R=%s O=%s QUIET=true' % ( os.path.join(os.environ['JAVA_JAR_PATH'], 'CreateSequenceDictionary.jar'), gatk_filename, os.path.splitext(gatk_filename)[0] + '.dict' ), shell=True ) 108 subprocess.check_call( 'java -jar %s R=%s O=%s QUIET=true' % ( os.path.join(os.environ['JAVA_JAR_PATH'], 'CreateSequenceDictionary.jar'), gatk_filename, os.path.splitext(gatk_filename)[0] + '.dict' ), shell=True )
104 index_bam_files( bam_filenames ) 109 index_bam_files( bam_filenames )
105 #set up stdout and stderr output options 110 # set up stdout and stderr output options
106 stdout = open_file_from_option( options.stdout, mode = 'wb' ) 111 stdout = open_file_from_option( options.stdout, mode='wb' )
107 stderr = open_file_from_option( options.stderr, mode = 'wb' ) 112 stderr = open_file_from_option( options.stderr, mode='wb' )
108 #if no stderr file is specified, we'll use our own 113 # if no stderr file is specified, we'll use our own
109 if stderr is None: 114 if stderr is None:
110 stderr = tempfile.NamedTemporaryFile( prefix="gatk-stderr-", dir=tmp_dir ) 115 stderr = tempfile.NamedTemporaryFile( prefix="gatk-stderr-", dir=tmp_dir )
111 116
112 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir ) 117 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir )
113 return_code = proc.wait() 118 return_code = proc.wait()
126 break 131 break
127 stderr.close() 132 stderr.close()
128 finally: 133 finally:
129 cleanup_before_exit( tmp_dir ) 134 cleanup_before_exit( tmp_dir )
130 135
131 #generate html reports 136 # generate html reports
132 if options.html_report_from_directory: 137 if options.html_report_from_directory:
133 for ( html_filename, html_dir ) in options.html_report_from_directory: 138 for ( html_filename, html_dir ) in options.html_report_from_directory:
134 html_report_from_directory( open( html_filename, 'wb' ), html_dir ) 139 html_report_from_directory( open( html_filename, 'wb' ), html_dir )
135 140
136 141