comparison bamedit.py @ 0:eb0d7fecbe64 draft

Uploaded
author modencode-dcc
date Mon, 21 Jan 2013 13:24:01 -0500
parents
children 38367e8ee3b0
comparison
equal deleted inserted replaced
-1:000000000000 0:eb0d7fecbe64
1 #purpose: python wrapper for bamedit tool
2 #author: Ziru Zhou
3 #date: October, 2012
4
5 import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip
6 from galaxy import eggs
7 import pkg_resources
8 pkg_resources.require( "simplejson" )
9 import simplejson
10
11 CHUNK_SIZE = 1024
12
13 def main():
14 options = simplejson.load( open( sys.argv[1] ) )
15 script_path = sys.argv[2]
16
17 #experiment_name = '_'.join( options['bamout'] )
18
19 if(options['action'] == "merge"):
20 cmdline = "samtools merge %s %s %s" % ( options['bamout'], options['input1'], options['input2'] )
21 if('input3' in options):
22 cmdline = "samtools merge %s %s %s %s" % ( options['bamout'], options['input1'], options['input2'], options['input3'] )
23 elif (options['action'] == "split"):
24 cmdline = "bash %s/split.sh %s %s %s" % ( script__path, options['bamout'], options['bamout2'], options['input1'] )
25 elif (options['action'] == "pileup"):
26 cmdline = "perl %s/pileup.pl %s %s %s %s %s" % ( script_path, options['input1'], options['input2'], options['bamout'], options['bamname'], options['refname'] )
27 elif (options['action'] == "filter"):
28 cmdline = "samtools view -q %s %s -bo %s" % ( options['quality'], options['input1'], options['bamout'] )
29
30 #create tempdir for output files and stderr reports
31 tmp_dir = tempfile.mkdtemp()
32 stderr_name = tempfile.NamedTemporaryFile().name
33 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
34 proc.wait()
35
36 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup
37 if proc.returncode:
38 stderr_f = open( stderr_name )
39 while True:
40 chunk = stderr_f.read( CHUNK_SIZE )
41 if not chunk:
42 stderr_f.close()
43 break
44 sys.stderr.write( chunk )
45
46 if __name__ == "__main__": main()