comparison bamedit.py @ 0:2a516d8a4ff9 draft

Uploaded
author ziru-zhou
date Tue, 18 Dec 2012 09:19:42 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2a516d8a4ff9
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 if(options['action'] == "merge"):
18 cmdline = "samtools merge %s %s %s" % ( options['bamout'], options['input1'], options['input2'] )
19 if('input3' in options):
20 cmdline = "samtools merge %s %s %s %s" % ( options['bamout'], options['input1'], options['input2'], options['input3'] )
21 elif (options['action'] == "split"):
22 #cmdline = "bash /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC/bamedit/split.sh %s %s %s" % ( options['bamout'], options['bamout2'], options['input1'] )
23 cmdline = "bash %s/split.sh %s %s %s" % (script_path, options['bamout'], options['bamout2'], options['input1'] )
24 elif (options['action'] == "pileup"):
25 #cmdline = "perl /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC/bamedit/pileup.pl %s %s %s %s %s" % ( options['input1'], options['input2'], options['bamout'], options['bamname'], options['refname'] )
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()