annotate bamedit.py @ 4:a4b7eb9c87d9 draft

Uploaded
author modencode-dcc
date Fri, 18 Jan 2013 10:59:13 -0500
parents 34450ff137d3
children 1ceeba7b95a4
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] ) )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
15
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
16 #experiment_name = '_'.join( options['bamout'] )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
17
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
18 if(options['action'] == "merge"):
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
19 cmdline = "samtools merge %s %s %s" % ( options['bamout'], options['input1'], options['input2'] )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
20 if('input3' in options):
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
21 cmdline = "samtools merge %s %s %s %s" % ( options['bamout'], options['input1'], options['input2'], options['input3'] )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
22 elif (options['action'] == "split"):
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
23 cmdline = "bash /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC_tools/bamedit/split.sh %s %s %s" % ( options['bamout'], options['bamout2'], options['input1'] )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
24 elif (options['action'] == "pileup"):
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
25 cmdline = "perl /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC_tools/bamedit/pileup.pl %s %s %s %s %s" % ( options['input1'], options['input2'], options['bamout'], options['bamname'], options['refname'] )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
26 elif (options['action'] == "filter"):
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
27 cmdline = "samtools view -q %s %s -bo %s" % ( options['quality'], options['input1'], options['bamout'] )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
28
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
29 #create tempdir for output files and stderr reports
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
30 tmp_dir = tempfile.mkdtemp()
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
31 stderr_name = tempfile.NamedTemporaryFile().name
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
32 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
33 proc.wait()
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
34
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
35 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
36 if proc.returncode:
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
37 stderr_f = open( stderr_name )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
38 while True:
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
39 chunk = stderr_f.read( CHUNK_SIZE )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
40 if not chunk:
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
41 stderr_f.close()
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
42 break
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
43 sys.stderr.write( chunk )
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
44
34450ff137d3 Uploaded
modencode-dcc
parents:
diff changeset
45 if __name__ == "__main__": main()