Mercurial > repos > sanbi-uwc > gatk2
annotate gatk2_wrapper.py @ 8:3012e466d7fc draft default tip
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit bb4bc974dacb3db49db5f22d937f520bdddd9190
author | sanbi-uwc |
---|---|
date | Tue, 29 May 2018 05:56:19 -0400 |
parents | 6e985e2e0802 |
children |
rev | line source |
---|---|
0
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
1 #!/usr/bin/env python |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
2 #David Hoover, based on gatk by Dan Blankenberg |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
3 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
4 """ |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
5 A wrapper script for running the GenomeAnalysisTK.jar commands. |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
6 """ |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
7 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
8 import sys, optparse, os, tempfile, subprocess, shutil |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
9 from binascii import unhexlify |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
10 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
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 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
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 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
13 DEFAULT_GATK_PREFIX = "gatk_file" |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
14 CHUNK_SIZE = 2**20 #1mb |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
15 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
16 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
17 def cleanup_before_exit( tmp_dir ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
18 if tmp_dir and os.path.exists( tmp_dir ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
19 shutil.rmtree( tmp_dir ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
20 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
21 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
22 def gatk_filename_from_galaxy( galaxy_filename, galaxy_ext, target_dir = None, prefix = None ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
23 suffix = GALAXY_EXT_TO_GATK_EXT.get( galaxy_ext, galaxy_ext ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
24 if prefix is None: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
25 prefix = DEFAULT_GATK_PREFIX |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
26 if target_dir is None: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
27 target_dir = os.getcwd() |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
28 gatk_filename = os.path.join( target_dir, "%s.%s" % ( prefix, suffix ) ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
29 os.symlink( galaxy_filename, gatk_filename ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
30 return gatk_filename |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
31 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
32 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
33 def gatk_filetype_argument_substitution( argument, galaxy_ext ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
34 return argument % dict( file_type = GALAXY_EXT_TO_GATK_FILE_TYPE.get( galaxy_ext, galaxy_ext ) ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
35 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
36 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
37 def open_file_from_option( filename, mode = 'rb' ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
38 if filename: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
39 return open( filename, mode = mode ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
40 return None |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
41 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
42 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
43 def html_report_from_directory( html_out, dir ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
44 html_out.write( '<html>\n<head>\n<title>Galaxy - GATK Output</title>\n</head>\n<body>\n<p/>\n<ul>\n' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
45 for fname in sorted( os.listdir( dir ) ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
46 html_out.write( '<li><a href="%s">%s</a></li>\n' % ( fname, fname ) ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
47 html_out.write( '</ul>\n</body>\n</html>\n' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
48 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
49 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
50 def index_bam_files( bam_filenames ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
51 for bam_filename in bam_filenames: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
52 bam_index_filename = "%s.bai" % bam_filename |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
53 if not os.path.exists( bam_index_filename ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
54 #need to index this bam file |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
55 stderr_name = tempfile.NamedTemporaryFile( prefix = "bam_index_stderr" ).name |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
56 command = 'samtools index %s %s' % ( bam_filename, bam_index_filename ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
57 try: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
58 subprocess.check_call( args=command, shell=True, stderr=open( stderr_name, 'wb' ) ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
59 except: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
60 for line in open( stderr_name ): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
61 print >> sys.stderr, line |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
62 raise Exception( "Error indexing BAM file" ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
63 finally: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
64 os.unlink( stderr_name ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
65 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
66 def __main__(): |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
67 #Parse Command Line |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
68 parser = optparse.OptionParser() |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
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.' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
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.' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
71 parser.add_option( '-d', '--dataset', dest='datasets', action='append', type="string", nargs=4, help='"-argument" "original_filename" "galaxy_filetype" "name_prefix"' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
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.' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
73 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.' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
74 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.' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
75 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.' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
76 parser.add_option( '', '--html_report_from_directory', dest='html_report_from_directory', action='append', type="string", nargs=2, help='"Target HTML File" "Directory"') |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
77 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)' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
78 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)' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
79 (options, args) = parser.parse_args() |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
80 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
81 if options.pass_through_options: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
82 cmd = ' '.join( options.pass_through_options ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
83 else: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
84 cmd = '' |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
85 if options.pass_through_options_encoded: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
86 cmd = '%s %s' % ( cmd, ' '.join( map( unhexlify, options.pass_through_options_encoded ) ) ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
87 if options.max_jvm_heap is not None: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
88 cmd = cmd.replace( 'java ', 'java -Xmx%s ' % ( options.max_jvm_heap ), 1 ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
89 elif options.max_jvm_heap_fraction is not None: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
90 cmd = cmd.replace( 'java ', 'java -XX:DefaultMaxRAMFraction=%s -XX:+UseParallelGC ' % ( options.max_jvm_heap_fraction ), 1 ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
91 bam_filenames = [] |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
92 tmp_dir = tempfile.mkdtemp( prefix='tmp-gatk-' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
93 try: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
94 if options.datasets: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
95 for ( dataset_arg, filename, galaxy_ext, prefix ) in options.datasets: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
96 gatk_filename = gatk_filename_from_galaxy( filename, galaxy_ext, target_dir = tmp_dir, prefix = prefix ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
97 if dataset_arg: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
98 cmd = '%s %s "%s"' % ( cmd, gatk_filetype_argument_substitution( dataset_arg, galaxy_ext ), gatk_filename ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
99 if galaxy_ext == "bam": |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
100 bam_filenames.append( gatk_filename ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
101 if galaxy_ext == 'fasta': |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
102 subprocess.check_call( 'samtools faidx "%s"' % gatk_filename, shell=True ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
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 ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
104 index_bam_files( bam_filenames ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
105 #set up stdout and stderr output options |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
106 stdout = open_file_from_option( options.stdout, mode = 'wb' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
107 stderr = open_file_from_option( options.stderr, mode = 'wb' ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
108 #if no stderr file is specified, we'll use our own |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
109 if stderr is None: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
110 stderr = tempfile.NamedTemporaryFile( prefix="gatk-stderr-", dir=tmp_dir ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
111 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
112 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
113 return_code = proc.wait() |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
114 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
115 if return_code: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
116 stderr_target = sys.stderr |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
117 else: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
118 stderr_target = sys.stdout |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
119 stderr.flush() |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
120 stderr.seek(0) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
121 while True: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
122 chunk = stderr.read( CHUNK_SIZE ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
123 if chunk: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
124 stderr_target.write( chunk ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
125 else: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
126 break |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
127 stderr.close() |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
128 finally: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
129 cleanup_before_exit( tmp_dir ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
130 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
131 #generate html reports |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
132 if options.html_report_from_directory: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
133 for ( html_filename, html_dir ) in options.html_report_from_directory: |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
134 html_report_from_directory( open( html_filename, 'wb' ), html_dir ) |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
135 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
136 |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
137 if __name__ == "__main__": |
6e985e2e0802
planemo upload for repository https://github.com/SANBI-SA/tools-sanbi-uwc/tree/master/tools/gatk2 commit f53888603dbab77d16c36cceffe0f2060052d204
sanbi-uwc
parents:
diff
changeset
|
138 __main__() |