changeset 0:2a516d8a4ff9 draft

Uploaded
author ziru-zhou
date Tue, 18 Dec 2012 09:19:42 -0500
parents
children b8a15b4e7c98
files bamedit.py
diffstat 1 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bamedit.py	Tue Dec 18 09:19:42 2012 -0500
@@ -0,0 +1,46 @@
+#purpose: python wrapper for bamedit tool
+#author: Ziru Zhou
+#date: October, 2012
+
+import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip
+from galaxy import eggs
+import pkg_resources
+pkg_resources.require( "simplejson" )
+import simplejson
+
+CHUNK_SIZE = 1024
+
+def main():
+    options = simplejson.load( open( sys.argv[1] ) )
+    script_path = sys.argv[2]
+
+    if(options['action'] == "merge"):
+	cmdline = "samtools merge  %s %s %s" % ( options['bamout'], options['input1'], options['input2'] )
+	if('input3' in options):
+		cmdline = "samtools merge  %s %s %s %s" % ( options['bamout'], options['input1'], options['input2'], options['input3'] )
+    elif (options['action'] == "split"):
+	#cmdline = "bash /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC/bamedit/split.sh %s %s %s" % ( options['bamout'], options['bamout2'], options['input1'] )
+	cmdline = "bash %s/split.sh %s %s %s" % (script_path, options['bamout'], options['bamout2'], options['input1'] )
+    elif (options['action'] == "pileup"):
+	#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'] )
+	cmdline = "perl %s/pileup.pl %s %s %s %s %s" % (script_path, options['input1'], options['input2'], options['bamout'], options['bamname'], options['refname'] )
+    elif (options['action'] == "filter"):
+	cmdline = "samtools view -q %s %s -bo %s" % ( options['quality'], options['input1'], options['bamout'] )
+
+    #create tempdir for output files and stderr reports
+    tmp_dir = tempfile.mkdtemp() 
+    stderr_name = tempfile.NamedTemporaryFile().name
+    proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
+    proc.wait()
+
+    #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup
+    if proc.returncode:
+        stderr_f = open( stderr_name )
+        while True:
+            chunk = stderr_f.read( CHUNK_SIZE )
+            if not chunk:
+                stderr_f.close()
+                break
+            sys.stderr.write( chunk )
+    
+if __name__ == "__main__": main()