annotate gatk_wrapper.py @ 1:5b8eaae854da default tip

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