Mercurial > repos > devteam > samtools_mpileup
comparison samtools_wrapper.py @ 0:44a18a94d7a9
Uploaded tool tarball.
| author | devteam |
|---|---|
| date | Mon, 26 Aug 2013 14:23:36 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:44a18a94d7a9 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 #Dan Blankenberg | |
| 3 | |
| 4 """ | |
| 5 A wrapper script for running SAMTools commands. | |
| 6 """ | |
| 7 | |
| 8 import sys, optparse, os, tempfile, subprocess, shutil | |
| 9 from string import Template | |
| 10 | |
| 11 GALAXY_EXT_TO_SAMTOOLS_EXT = { 'bam_index':'bam.bai', } #items not listed here will use the galaxy extension as-is | |
| 12 GALAXY_EXT_TO_SAMTOOLS_FILE_TYPE = GALAXY_EXT_TO_SAMTOOLS_EXT #for now, these are the same, but could be different if needed | |
| 13 DEFAULT_SAMTOOLS_PREFIX = "SAMTools_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 SAMTOOLS_filename_from_galaxy( galaxy_filename, galaxy_ext, target_dir = None, prefix = None ): | |
| 22 suffix = GALAXY_EXT_TO_SAMTOOLS_EXT.get( galaxy_ext, galaxy_ext ) | |
| 23 if prefix is None: | |
| 24 prefix = DEFAULT_SAMTOOLS_PREFIX | |
| 25 if target_dir is None: | |
| 26 target_dir = os.getcwd() | |
| 27 SAMTools_filename = os.path.join( target_dir, "%s.%s" % ( prefix, suffix ) ) | |
| 28 os.symlink( galaxy_filename, SAMTools_filename ) | |
| 29 return SAMTools_filename | |
| 30 | |
| 31 def SAMTOOLS_filetype_argument_substitution( argument, galaxy_ext ): | |
| 32 return argument % dict( file_type = GALAXY_EXT_TO_SAMTOOLS_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 - SAMTOOLS 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 SAMTOOLS, 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( prefix='tmp-SAMTOOLS-' ) | |
| 56 | |
| 57 #set up stdout and stderr output options | |
| 58 stdout = open_file_from_option( options.stdout, mode = 'wb' ) | |
| 59 stderr = open_file_from_option( options.stderr, mode = 'wb' ) | |
| 60 #if no stderr file is specified, we'll use our own | |
| 61 if stderr is None: | |
| 62 stderr = tempfile.NamedTemporaryFile( prefix="SAMTOOLS-stderr-", dir=tmp_dir ) | |
| 63 | |
| 64 if options.pass_through_options: | |
| 65 cmd = ' '.join( options.pass_through_options ) | |
| 66 else: | |
| 67 cmd = '' | |
| 68 return_code = None | |
| 69 if options.datasets: | |
| 70 for ( dataset_arg, filename, galaxy_ext, prefix ) in options.datasets: | |
| 71 SAMTools_filename = SAMTOOLS_filename_from_galaxy( filename, galaxy_ext, target_dir = tmp_dir, prefix = prefix ) | |
| 72 if dataset_arg: | |
| 73 if '>' in cmd: | |
| 74 cmd = cmd.replace( '>', ' %s "%s" >' % ( SAMTOOLS_filetype_argument_substitution( dataset_arg, galaxy_ext ), SAMTools_filename ), 1 ) | |
| 75 else: | |
| 76 cmd = '%s %s "%s"' % ( cmd, SAMTOOLS_filetype_argument_substitution( dataset_arg, galaxy_ext ), SAMTools_filename ) | |
| 77 #auto index fasta files: | |
| 78 if galaxy_ext == 'fa': | |
| 79 index_cmd = 'samtools faidx %s' % ( SAMTools_filename ) | |
| 80 proc = subprocess.Popen( args=index_cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir ) | |
| 81 return_code = proc.wait() | |
| 82 if return_code: | |
| 83 break | |
| 84 if return_code is None or not return_code: | |
| 85 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir ) | |
| 86 return_code = proc.wait() | |
| 87 if return_code: | |
| 88 stderr_target = sys.stderr | |
| 89 else: | |
| 90 if stdout: | |
| 91 stderr_target = stdout | |
| 92 else: | |
| 93 stderr_target = sys.stdout | |
| 94 stderr.flush() | |
| 95 stderr.seek(0) | |
| 96 while True: | |
| 97 chunk = stderr.read( CHUNK_SIZE ) | |
| 98 if chunk: | |
| 99 stderr_target.write( chunk ) | |
| 100 else: | |
| 101 break | |
| 102 stderr.close() | |
| 103 #generate html reports | |
| 104 if options.html_report_from_directory: | |
| 105 for ( html_filename, html_dir ) in options.html_report_from_directory: | |
| 106 html_report_from_directory( open( html_filename, 'wb' ), html_dir ) | |
| 107 | |
| 108 cleanup_before_exit( tmp_dir ) | |
| 109 | |
| 110 if __name__=="__main__": __main__() |
