annotate bamedit.py @ 15:eb166cebbe3c draft default tip

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