0
|
1 # This is a wrapper for JAMM to use it in Galaxy
|
|
2 # It will be accompanied with a jamm.xml file, which specifies the interface that Galaxy is showing to the user
|
|
3 # as well as the JAMM software including JAMM.sh, the bash script that is actually called.
|
|
4
|
|
5 # This wrapper does the following things:
|
|
6 # map the files (from Galaxy history) to a directory
|
|
7 # pass the parameters from the GUI to JAMM.sh
|
|
8 # call JAMM.sh
|
1
|
9 # map the resulting tabular files back to history items
|
0
|
10
|
|
11 #import optparse
|
|
12 import argparse, os, shutil, subprocess, sys, tempfile
|
|
13 import shlex
|
|
14 # importing some of the modules used, especially for symlinking, dir manipulation and tool calling.
|
|
15 # since python2.7, argparse is preferred over optparse.
|
|
16
|
|
17 # for reference wrappers, see
|
|
18 # https://bitbucket.org/fubar/rossdev/src/4a91f99b5e1953270c9b35d2ca70c325a10fcfcb/rgdev/bwa_wrapper.py?at=default
|
|
19 # https://www.e-biogenouest.org/wiki/WrapperpythonforGalaxy:Asyntaxexample
|
|
20
|
|
21 def main():
|
|
22
|
|
23 #Read command line arguments
|
|
24 parser = argparse.ArgumentParser()
|
1
|
25 parser.add_argument('-i', dest = 'input', nargs='+') # + allows for 1 or more arguments
|
|
26 # parser.add_argument('-c', dest = 'control', nargs='*', default=None) # allow for not supplying -c
|
|
27 parser.add_argument('-c', dest = 'control', action='append') # allow for not supplying -c
|
0
|
28 parser.add_argument('-g', dest = 'gsize')
|
1
|
29 parser.add_argument('-o', dest = 'peakfile')
|
|
30 parser.add_argument('-of', dest = 'filteredpeakfile')
|
0
|
31
|
|
32 parser.add_argument('-m', dest = 'mode')
|
|
33 parser.add_argument('-r', dest = 'resolution')
|
|
34 parser.add_argument('-p', dest = 'processes')
|
1
|
35 parser.add_argument('-t', dest = 'type')
|
|
36 parser.add_argument('-f', dest = 'fraglen')
|
|
37 parser.add_argument('-b', dest = 'binsize')
|
0
|
38
|
|
39 args = parser.parse_args()
|
|
40
|
1
|
41 print "################################################"
|
0
|
42 print "Wrapper debugging"
|
1
|
43 print "################################################"
|
|
44 print "Sample files:"
|
0
|
45 for j in args.input:
|
|
46 print j
|
1
|
47 if args.control is not None:
|
|
48 print "Control files:"
|
|
49 for j in args.control:
|
|
50 print j
|
0
|
51
|
1
|
52 print "output files:"
|
0
|
53 print args.peakfile
|
1
|
54 print args.filteredpeakfile
|
|
55 print "current working dir:"
|
|
56 print os.getcwd()
|
|
57 print "dir with jammwrapper in it:"
|
|
58 path = os.path.abspath(os.path.dirname(sys.argv[0]))
|
|
59 print path
|
0
|
60
|
1
|
61 # optparse was depracted, can still be found in may of the example wrappers
|
0
|
62 #parser = optparse.OptionParser()
|
|
63 #parser.add_option( '-i', dest='input', help='input bed files' )
|
|
64 #parser.add_option( '-c', dest='csize', help='chr sizes' )
|
|
65 #(options, args) = parser.parse_args()
|
1
|
66
|
0
|
67 # create temp dir
|
|
68 tmp_dir = tempfile.mkdtemp()
|
1
|
69 os.mkdir(tmp_dir + "/sample")
|
0
|
70 # symlink creation
|
|
71 for file in args.input:
|
1
|
72 filen = tmp_dir + "/sample/" + os.path.basename(os.path.splitext(file)[0])+".bed"
|
|
73 print "input files mapped: %s" % filen
|
0
|
74 os.symlink(file, filen)
|
1
|
75
|
|
76 # Here comes some unnecessary repetition
|
|
77 # if control files are supplied, we make another tmp subdir and put those files there.
|
|
78 # JAMM.sh is then called with one additional switch -c
|
|
79 if args.control is not None:
|
|
80 # symlink creation
|
|
81 os.mkdir(tmp_dir + "/control")
|
|
82 for file in args.control:
|
|
83 filen = tmp_dir + "/control/" + os.path.basename(os.path.splitext(file)[0])+".bed"
|
|
84 print "input files mapped: %s" % filen
|
|
85 os.symlink(file, filen)
|
|
86 command = ( "bash %s/JAMM.sh -s %s/sample -c %s/control -g %s -o results -m %s -r %s -p %s -t %s -f %s -b %s"
|
|
87 % ( path, tmp_dir, tmp_dir, args.gsize, args.mode, args.resolution, args.processes, \
|
|
88 args.type, args.fraglen, args.binsize ) )
|
|
89 else:
|
|
90 command = ( "bash %s/JAMM.sh -s %s/sample -g %s -o results -m %s -r %s -p %s -t %s -f %s -b %s"
|
|
91 % ( path, tmp_dir, args.gsize, args.mode, args.resolution, args.processes, \
|
|
92 args.type, args.fraglen, args.binsize ) )
|
|
93
|
|
94
|
|
95 print "Command called by bash:"
|
0
|
96 print command
|
|
97 # depending on how your programm is called, it may be necessary to use shlex.split on the command string before
|
|
98 # in this case, this was actually harmful. idk why
|
|
99 # command = shlex.split(command)
|
|
100 # Please read the docs for subprocess.Popen. You can't just pass as string as variables need to be extended
|
|
101 # Note that shell = True can be a security hazard.
|
|
102 # proc = subprocess.Popen( command, executable = '/bin/bash', shell=True )
|
|
103 proc = subprocess.Popen( command, shell=True)
|
|
104 returncode = proc.wait()
|
|
105
|
|
106 #mv files to a place where galaxy wrapper can find them
|
1
|
107 # mvcommand = "mv %s/results/peaks/all.peaks.narrowPeak %s" % ( tmp_dir, args.peakfile )
|
|
108 mvcommand = "mv results/peaks/all.peaks.narrowPeak %s" % args.peakfile
|
|
109 os.system(mvcommand)
|
|
110 mvcommand = "mv results/peaks/filtered.peaks.narrowPeak %s" % args.filteredpeakfile
|
0
|
111 os.system(mvcommand)
|
|
112
|
|
113 # clean up temp dir
|
|
114 if os.path.exists( tmp_dir ):
|
|
115 shutil.rmtree( tmp_dir )
|
|
116
|
|
117 if __name__ == "__main__":
|
|
118 main()
|
|
119
|