Mercurial > repos > sjung > bdds
comparison fseq_wrapper.py @ 1:29760e9a840b draft
Uploaded
| author | sjung |
|---|---|
| date | Wed, 24 May 2017 00:35:08 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:2d6315e241e4 | 1:29760e9a840b |
|---|---|
| 1 #/usr/bin/python | |
| 2 | |
| 3 import optparse, sys, subprocess, tempfile | |
| 4 import shutil, os | |
| 5 | |
| 6 CHUNK_SIZE = 2**20 #1mbi | |
| 7 | |
| 8 def cleanup_before_exit( tmp_dir ): | |
| 9 if tmp_dir and os.path.exists( tmp_dir ): | |
| 10 shutil.rmtree( tmp_dir ) | |
| 11 | |
| 12 def __main__(): | |
| 13 parser = optparse.OptionParser() | |
| 14 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.' ) | |
| 15 parser.add_option('', '--of', dest='output_format', action='store', type="string", help='output format') | |
| 16 parser.add_option('-i', '--input-files', dest='input_files', action='store', type="string", help='input files') | |
| 17 parser.add_option( '-o', '--output-file', dest='output_file', action='store', type="string", default=None, help='bed output' ) | |
| 18 | |
| 19 (options, args) = parser.parse_args() | |
| 20 pass_through = "" | |
| 21 if options.pass_through_options: | |
| 22 pass_through = ' '.join( options.pass_through_options ) | |
| 23 | |
| 24 cmd = "fseq %s -of %s %s" % (pass_through, options.output_format, options.input_files) | |
| 25 print cmd | |
| 26 tmp_dir = tempfile.mkdtemp( prefix='tmp-tool-' ) | |
| 27 | |
| 28 stdout = tempfile.NamedTemporaryFile( prefix="fseq-stdout-", dir=tmp_dir ) | |
| 29 stderr = tempfile.NamedTemporaryFile( prefix="fseq-stderr-", dir=tmp_dir ) | |
| 30 proc = subprocess.Popen( args=cmd, stdout=stdout, stderr=stderr, shell=True, cwd=tmp_dir ) | |
| 31 return_code = proc.wait() | |
| 32 | |
| 33 if return_code: | |
| 34 stderr_target = sys.stderr | |
| 35 else: | |
| 36 stderr_target = sys.stdout | |
| 37 stderr.flush() | |
| 38 stderr.seek(0) | |
| 39 | |
| 40 while True: | |
| 41 chunk = stderr.read( CHUNK_SIZE ) | |
| 42 if chunk: | |
| 43 stderr_target.write( chunk ) | |
| 44 else: | |
| 45 break | |
| 46 stderr.close() | |
| 47 stdout.close() | |
| 48 if options.output_format == 'bed': | |
| 49 os.system("cat %s/*.bed > %s/output.bed" % (tmp_dir, tmp_dir)) | |
| 50 shutil.copy("%s/output.bed" % tmp_dir, options.output_file) | |
| 51 else: | |
| 52 os.system("cat %s/*.wig > %s/output.wig" % (tmp_dir, tmp_dir)) | |
| 53 shutil.copy("%s/output.wig" % tmp_dir, options.output_file) | |
| 54 | |
| 55 cleanup_before_exit( tmp_dir ) | |
| 56 | |
| 57 if __name__ == "__main__": __main__() |
