Mercurial > repos > sjung > bdds
comparison hint_wrapper.py @ 3:43e4ba4796c1 draft
Uploaded
author | sjung |
---|---|
date | Wed, 24 May 2017 00:35:42 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:0ea254c514b7 | 3:43e4ba4796c1 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import optparse, os, shutil, sys, tempfile, glob, shlex, vcf, pysam, tarfile | |
4 from subprocess import * | |
5 import subprocess | |
6 | |
7 CHUNK_SIZE = 2**20 #1mb | |
8 | |
9 def cleanup_before_exit( tmp_dir ): | |
10 if tmp_dir and os.path.exists( tmp_dir ): | |
11 shutil.rmtree( tmp_dir ) | |
12 | |
13 def __main__(): | |
14 parser = optparse.OptionParser() | |
15 parser.add_option('','--input', dest="inputF", action='store', type="string", default=None, help='') | |
16 parser.add_option('','--region', dest="regionF", action='store', type="string", default=None, help='') | |
17 parser.add_option( '', '--output', dest='outputF', action='store', type="string", default=None, help='') | |
18 parser.add_option( '', '--out-dir', dest='output_dir', action='store', type="string", default=None, help='If specified, the output directory for extra files.' ) | |
19 | |
20 (options, args) = parser.parse_args() | |
21 | |
22 if not os.path.exists(options.output_dir): | |
23 os.mkdir(options.output_dir) | |
24 input_dir = "%s/input" % options.output_dir | |
25 output_dir = "%s/output" % options.output_dir | |
26 config_dir = "%s/config" % options.output_dir | |
27 | |
28 if not os.path.exists(output_dir): | |
29 os.mkdir(input_dir) | |
30 os.mkdir(output_dir) | |
31 os.mkdir(config_dir) | |
32 # region input file | |
33 linked_bed_name = "%s/regions.bed" % input_dir | |
34 os.symlink(options.regionF, linked_bed_name) | |
35 linked_bam_name="%s/sample.bam" % input_dir | |
36 os.symlink(options.inputF, linked_bam_name) | |
37 pysam.index(linked_bam_name) | |
38 | |
39 input_config = open("%s/input.txt" % config_dir, 'w') | |
40 input_config.write("name\ttype\tfile\tdata\tgroup\n") | |
41 input_config.write("HS2\tregions\t%s\tHS\tDU_K562_HINT\n" % linked_bed_name) | |
42 input_config.write("DNase\treads\t%s\tDNASE\tDU_K562_HINT\n" % linked_bam_name) | |
43 input_config.close() | |
44 | |
45 hint_cmd = "rgt-hint --output-location %s/ %s/input.txt" % (output_dir, config_dir) | |
46 print "hint cmd:%s" % hint_cmd | |
47 stdout = tempfile.NamedTemporaryFile( prefix="hint-stdout-", dir=options.output_dir) | |
48 stderr = tempfile.NamedTemporaryFile( prefix="hint-stderr-", dir=options.output_dir) | |
49 proc = subprocess.Popen( args=hint_cmd, stdout=stdout, stderr=stderr, shell=True, cwd=options.output_dir ) | |
50 return_code = proc.wait() | |
51 | |
52 if return_code: | |
53 stderr_target = sys.stderr | |
54 else: | |
55 stderr_target = sys.stdout | |
56 stderr.flush() | |
57 stderr.seek(0) | |
58 while True: | |
59 chunk = stderr.read( CHUNK_SIZE ) | |
60 if chunk: | |
61 stderr_target.write( chunk ) | |
62 else: | |
63 break | |
64 stderr.close() | |
65 stdout.close() | |
66 | |
67 # copy files to final output locations | |
68 shutil.copy('%s/DU_K562_HINT.bed' % output_dir, options.outputF) | |
69 cleanup_before_exit( options.output_dir ) | |
70 | |
71 if __name__=="__main__": __main__() | |
72 |