annotate bamedit.py @ 0:2a516d8a4ff9 draft

Uploaded
author ziru-zhou
date Tue, 18 Dec 2012 09:19:42 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
1 #purpose: python wrapper for bamedit tool
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
2 #author: Ziru Zhou
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
3 #date: October, 2012
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
4
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
5 import sys, subprocess, tempfile, shutil, glob, os, os.path, gzip
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
6 from galaxy import eggs
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
7 import pkg_resources
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
8 pkg_resources.require( "simplejson" )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
9 import simplejson
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
10
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
11 CHUNK_SIZE = 1024
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
12
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
13 def main():
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
14 options = simplejson.load( open( sys.argv[1] ) )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
15 script_path = sys.argv[2]
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
16
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
17 if(options['action'] == "merge"):
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
18 cmdline = "samtools merge %s %s %s" % ( options['bamout'], options['input1'], options['input2'] )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
19 if('input3' in options):
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
20 cmdline = "samtools merge %s %s %s %s" % ( options['bamout'], options['input1'], options['input2'], options['input3'] )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
21 elif (options['action'] == "split"):
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
22 #cmdline = "bash /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC/bamedit/split.sh %s %s %s" % ( options['bamout'], options['bamout2'], options['input1'] )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
23 cmdline = "bash %s/split.sh %s %s %s" % (script_path, options['bamout'], options['bamout2'], options['input1'] )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
24 elif (options['action'] == "pileup"):
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
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'] )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
26 cmdline = "perl %s/pileup.pl %s %s %s %s %s" % (script_path, options['input1'], options['input2'], options['bamout'], options['bamname'], options['refname'] )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
27 elif (options['action'] == "filter"):
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
28 cmdline = "samtools view -q %s %s -bo %s" % ( options['quality'], options['input1'], options['bamout'] )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
29
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
30 #create tempdir for output files and stderr reports
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
31 tmp_dir = tempfile.mkdtemp()
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
32 stderr_name = tempfile.NamedTemporaryFile().name
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
33 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
34 proc.wait()
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
35
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
36 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
37 if proc.returncode:
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
38 stderr_f = open( stderr_name )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
39 while True:
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
40 chunk = stderr_f.read( CHUNK_SIZE )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
41 if not chunk:
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
42 stderr_f.close()
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
43 break
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
44 sys.stderr.write( chunk )
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
45
2a516d8a4ff9 Uploaded
ziru-zhou
parents:
diff changeset
46 if __name__ == "__main__": main()