annotate tools/gatk/gatk_wrapper.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 A wrapper script for running the GenomeAnalysisTK.jar commands.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 import sys, optparse, os, tempfile, subprocess, shutil
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 from string import Template
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
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
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 GALAXY_EXT_TO_GATK_FILE_TYPE = GALAXY_EXT_TO_GATK_EXT #for now, these are the same, but could be different if needed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 DEFAULT_GATK_PREFIX = "gatk_file"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 CHUNK_SIZE = 2**20 #1mb
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 def cleanup_before_exit( tmp_dir ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 if tmp_dir and os.path.exists( tmp_dir ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 shutil.rmtree( tmp_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 def gatk_filename_from_galaxy( galaxy_filename, galaxy_ext, target_dir = None, prefix = None ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 suffix = GALAXY_EXT_TO_GATK_EXT.get( galaxy_ext, galaxy_ext )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 if prefix is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 prefix = DEFAULT_GATK_PREFIX
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 if target_dir is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 target_dir = os.getcwd()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 gatk_filename = os.path.join( target_dir, "%s.%s" % ( prefix, suffix ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 os.symlink( galaxy_filename, gatk_filename )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 return gatk_filename
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 def gatk_filetype_argument_substitution( argument, galaxy_ext ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 return argument % dict( file_type = GALAXY_EXT_TO_GATK_FILE_TYPE.get( galaxy_ext, galaxy_ext ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 def open_file_from_option( filename, mode = 'rb' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 if filename:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 return open( filename, mode = mode )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 return None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 def html_report_from_directory( html_out, dir ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 html_out.write( '<html>\n<head>\n<title>Galaxy - GATK Output</title>\n</head>\n<body>\n<p/>\n<ul>\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 for fname in sorted( os.listdir( dir ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 html_out.write( '<li><a href="%s">%s</a></li>\n' % ( fname, fname ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 html_out.write( '</ul>\n</body>\n</html>\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 #Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 parser = optparse.OptionParser()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 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.' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 parser.add_option( '-d', '--dataset', dest='datasets', action='append', type="string", nargs=4, help='"-argument" "original_filename" "galaxy_filetype" "name_prefix"' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 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.' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 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.' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 parser.add_option( '', '--html_report_from_directory', dest='html_report_from_directory', action='append', type="string", nargs=2, help='"Target HTML File" "Directory"')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 (options, args) = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 tmp_dir = tempfile.mkdtemp()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 if options.pass_through_options:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 cmd = ' '.join( options.pass_through_options )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 cmd = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 if options.datasets:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 for ( dataset_arg, filename, galaxy_ext, prefix ) in options.datasets:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 gatk_filename = gatk_filename_from_galaxy( filename, galaxy_ext, target_dir = tmp_dir, prefix = prefix )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if dataset_arg:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 cmd = '%s %s "%s"' % ( cmd, gatk_filetype_argument_substitution( dataset_arg, galaxy_ext ), gatk_filename )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 #set up stdout and stderr output options
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 stdout = open_file_from_option( options.stdout, mode = 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 stderr = open_file_from_option( options.stderr, mode = 'wb' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 #if no stderr file is specified, we'll use our own
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 if stderr is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 stderr = tempfile.NamedTemporaryFile( dir=tmp_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 stderr = open( stderr.name, 'w+b' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 return_code = proc.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 if return_code:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 stderr_target = sys.stderr
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 stderr_target = sys.stdout
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 stderr.flush()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 stderr.seek(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 while True:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 chunk = stderr.read( CHUNK_SIZE )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 if chunk:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 stderr_target.write( chunk )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 stderr.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 #generate html reports
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 if options.html_report_from_directory:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 for ( html_filename, html_dir ) in options.html_report_from_directory:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 html_report_from_directory( open( html_filename, 'wb' ), html_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 cleanup_before_exit( tmp_dir )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if __name__=="__main__": __main__()