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