annotate gatk2_wrapper.py @ 15:01ff8dd37d4d draft default tip

Uploaded
author lz_hust
date Sat, 01 Jun 2019 07:20:41 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
1 #!/usr/bin/env python
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
2 # David Hoover, based on gatk by Dan Blankenberg
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
3 """
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
4 A wrapper script for running the GenomeAnalysisTK.jar commands.
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
5 """
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
6
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
7 import optparse
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
8 import os
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
9 import shutil
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
10 import subprocess
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
11 import sys
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
12 import tempfile
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
13 from binascii import unhexlify
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
14
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
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
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
16 GALAXY_EXT_TO_GATK_FILE_TYPE = GALAXY_EXT_TO_GATK_EXT # for now, these are the same, but could be different if needed
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
17 DEFAULT_GATK_PREFIX = "gatk_file"
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
18 CHUNK_SIZE = 2**20 # 1mb
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
19
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
20
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
21 def cleanup_before_exit( tmp_dir ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
22 if tmp_dir and os.path.exists( tmp_dir ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
23 shutil.rmtree( tmp_dir )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
24
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
25
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
26 def gatk_filename_from_galaxy( galaxy_filename, galaxy_ext, target_dir=None, prefix=None ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
27 suffix = GALAXY_EXT_TO_GATK_EXT.get( galaxy_ext, galaxy_ext )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
28 if prefix is None:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
29 prefix = DEFAULT_GATK_PREFIX
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
30 if target_dir is None:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
31 target_dir = os.getcwd()
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
32 gatk_filename = os.path.join( target_dir, "%s.%s" % ( prefix, suffix ) )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
33 os.symlink( galaxy_filename, gatk_filename )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
34 return gatk_filename
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
35
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
36
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
37 def gatk_filetype_argument_substitution( argument, galaxy_ext ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
38 return argument % dict( file_type=GALAXY_EXT_TO_GATK_FILE_TYPE.get( galaxy_ext, galaxy_ext ) )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
39
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
40
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
41 def open_file_from_option( filename, mode='rb' ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
42 if filename:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
43 return open( filename, mode=mode )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
44 return None
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
45
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
46
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
47 def html_report_from_directory( html_out, dir ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
48 html_out.write( '<html>\n<head>\n<title>Galaxy - GATK Output</title>\n</head>\n<body>\n<p/>\n<ul>\n' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
49 for fname in sorted( os.listdir( dir ) ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
50 html_out.write( '<li><a href="%s">%s</a></li>\n' % ( fname, fname ) )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
51 html_out.write( '</ul>\n</body>\n</html>\n' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
52
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
53
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
54 def index_bam_files( bam_filenames ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
55 for bam_filename in bam_filenames:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
56 bam_index_filename = "%s.bai" % bam_filename
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
57 if not os.path.exists( bam_index_filename ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
58 # need to index this bam file
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
59 stderr_name = tempfile.NamedTemporaryFile( prefix="bam_index_stderr" ).name
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
60 command = 'samtools index %s %s' % ( bam_filename, bam_index_filename )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
61 try:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
62 subprocess.check_call( args=command, shell=True, stderr=open( stderr_name, 'wb' ) )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
63 except:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
64 for line in open( stderr_name ):
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
65 print >> sys.stderr, line
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
66 raise Exception( "Error indexing BAM file" )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
67 finally:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
68 os.unlink( stderr_name )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
69
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
70
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
71 def __main__():
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
72 # Parse Command Line
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
73 parser = optparse.OptionParser()
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
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.' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
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.' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
76 parser.add_option( '-d', '--dataset', dest='datasets', action='append', type="string", nargs=4, help='"-argument" "original_filename" "galaxy_filetype" "name_prefix"' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
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.' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
78 parser.add_option( '', '--max_jvm_heap_fraction', dest='max_jvm_heap_fraction', action='store', type="int", default=None, help='If specified, the maximum java virtual machine heap size will be set to the provide value as a fraction of total physical memory.' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
79 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.' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
80 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.' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
81 parser.add_option( '', '--html_report_from_directory', dest='html_report_from_directory', action='append', type="string", nargs=2, help='"Target HTML File" "Directory"')
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
82 parser.add_option( '-e', '--phone_home', dest='phone_home', action='store', type="string", default='STANDARD', help='What kind of GATK run report should we generate(NO_ET|STANDARD|STDOUT)' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
83 parser.add_option( '-K', '--gatk_key', dest='gatk_key', action='store', type="string", default=None, help='What kind of GATK run report should we generate(NO_ET|STANDARD|STDOUT)' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
84 (options, args) = parser.parse_args()
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
85
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
86 if options.pass_through_options:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
87 cmd = ' '.join( options.pass_through_options )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
88 else:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
89 cmd = ''
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
90 if options.pass_through_options_encoded:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
91 cmd = '%s %s' % ( cmd, ' '.join( map( unhexlify, options.pass_through_options_encoded ) ) )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
92 if options.max_jvm_heap is not None:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
93 cmd = cmd.replace( 'java ', 'java -Xmx%s ' % ( options.max_jvm_heap ), 1 )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
94 elif options.max_jvm_heap_fraction is not None:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
95 cmd = cmd.replace( 'java ', 'java -XX:DefaultMaxRAMFraction=%s -XX:+UseParallelGC ' % ( options.max_jvm_heap_fraction ), 1 )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
96 bam_filenames = []
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
97 tmp_dir = tempfile.mkdtemp( prefix='tmp-gatk-' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
98 try:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
99 if options.datasets:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
100 for ( dataset_arg, filename, galaxy_ext, prefix ) in options.datasets:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
101 gatk_filename = gatk_filename_from_galaxy( filename, galaxy_ext, target_dir=tmp_dir, prefix=prefix )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
102 if dataset_arg:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
103 cmd = '%s %s "%s"' % ( cmd, gatk_filetype_argument_substitution( dataset_arg, galaxy_ext ), gatk_filename )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
104 if galaxy_ext == "bam":
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
105 bam_filenames.append( gatk_filename )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
106 if galaxy_ext == 'fasta':
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
107 subprocess.check_call( 'samtools faidx "%s"' % gatk_filename, shell=True )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
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 )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
109 index_bam_files( bam_filenames )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
110 # set up stdout and stderr output options
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
111 stdout = open_file_from_option( options.stdout, mode='wb' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
112 stderr = open_file_from_option( options.stderr, mode='wb' )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
113 # if no stderr file is specified, we'll use our own
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
114 if stderr is None:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
115 stderr = tempfile.NamedTemporaryFile( prefix="gatk-stderr-", dir=tmp_dir )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
116
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
117 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
118 return_code = proc.wait()
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
119
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
120 if return_code:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
121 stderr_target = sys.stderr
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
122 else:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
123 stderr_target = sys.stdout
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
124 stderr.flush()
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
125 stderr.seek(0)
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
126 while True:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
127 chunk = stderr.read( CHUNK_SIZE )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
128 if chunk:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
129 stderr_target.write( chunk )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
130 else:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
131 break
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
132 stderr.close()
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
133 finally:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
134 cleanup_before_exit( tmp_dir )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
135
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
136 # generate html reports
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
137 if options.html_report_from_directory:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
138 for ( html_filename, html_dir ) in options.html_report_from_directory:
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
139 html_report_from_directory( open( html_filename, 'wb' ), html_dir )
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
140
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
141
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
142 if __name__ == "__main__":
01ff8dd37d4d Uploaded
lz_hust
parents:
diff changeset
143 __main__()