|
4
|
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():
|
|
53
|
14 options = simplejson.load( open( sys.argv[1] ) )
|
|
81
|
15 script_path = sys.argv[2]
|
|
|
16 print script_path
|
|
18
|
17
|
|
53
|
18 if(options['action'] == "merge"):
|
|
4
|
19 cmdline = "samtools merge %s %s %s" % ( options['bamout'], options['input1'], options['input2'] )
|
|
|
20 if('input3' in options):
|
|
|
21 cmdline = "samtools merge %s %s %s %s" % ( options['bamout'], options['input1'], options['input2'], options['input3'] )
|
|
|
22 elif (options['action'] == "split"):
|
|
18
|
23 #cmdline = "bash /mnt/galaxyTools/galaxy-central/tools/modENCODE_DCC/bamedit/split.sh %s %s %s" % ( options['bamout'], options['bamout2'], options['input1'] )
|
|
81
|
24 cmdline = "bash %s/split.sh %s %s %s" % (script_path, options['bamout'], options['bamout2'], options['input1'] )
|
|
4
|
25 elif (options['action'] == "pileup"):
|
|
81
|
26 #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'] )
|
|
|
27 cmdline = "perl %s/pileup.pl %s %s %s %s %s" % (script_path, options['input1'], options['input2'], options['bamout'], options['bamname'], options['refname'] )
|
|
4
|
28 elif (options['action'] == "filter"):
|
|
|
29 cmdline = "samtools view -q %s %s -bo %s" % ( options['quality'], options['input1'], options['bamout'] )
|
|
|
30
|
|
|
31 #create tempdir for output files and stderr reports
|
|
|
32 tmp_dir = tempfile.mkdtemp()
|
|
|
33 stderr_name = tempfile.NamedTemporaryFile().name
|
|
|
34 proc = subprocess.Popen( args=cmdline, shell=True, cwd=tmp_dir, stderr=open( stderr_name, 'wb' ) )
|
|
|
35 proc.wait()
|
|
|
36
|
|
|
37 #Do not terminate if error code, allow dataset (e.g. log) creation and cleanup
|
|
|
38 if proc.returncode:
|
|
|
39 stderr_f = open( stderr_name )
|
|
|
40 while True:
|
|
|
41 chunk = stderr_f.read( CHUNK_SIZE )
|
|
|
42 if not chunk:
|
|
|
43 stderr_f.close()
|
|
|
44 break
|
|
|
45 sys.stderr.write( chunk )
|
|
|
46
|
|
|
47 if __name__ == "__main__": main()
|