annotate fseq_wrapper.py @ 7:9920c489a5c3 draft

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