comparison jammwrapper.py @ 0:d42f4d78c85e draft

Uploaded
author messersc
date Wed, 17 Dec 2014 10:40:23 -0500
parents
children 243f75d0ed6e
comparison
equal deleted inserted replaced
-1:000000000000 0:d42f4d78c85e
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
9 # maybe map the resulting tabular files back to history items, the XML could take care of that if output is fixed
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()
25 parser.add_argument('-i', dest = 'input', nargs='+')
26 parser.add_argument('-g', dest = 'gsize')
27 parser.add_argument('-z', dest = 'peakfile')
28
29 parser.add_argument('-m', dest = 'mode')
30 parser.add_argument('-r', dest = 'resolution')
31 parser.add_argument('-p', dest = 'processes')
32 #parser.add_argument('-b', dest = 'binSize')
33
34 args = parser.parse_args()
35
36 print "\n"
37 print "Wrapper debugging"
38 print "Files to be used:"
39 for j in args.input:
40 print j
41
42 print "output file:"
43 print args.peakfile
44
45 # depracted, can still be found in may of the example wrappers
46 #parser = optparse.OptionParser()
47 #parser.add_option( '-i', dest='input', help='input bed files' )
48 #parser.add_option( '-c', dest='csize', help='chr sizes' )
49 #(options, args) = parser.parse_args()
50
51 # create temp dir
52 tmp_dir = tempfile.mkdtemp()
53 os.chdir(tmp_dir)
54 # symlink creation
55 for file in args.input:
56 filen = tmp_dir + "/" + os.path.basename(os.path.splitext(file)[0])+".bed"
57 os.symlink(file, filen)
58
59 # in case temp files should have random names
60 # ref_file = tempfile.NamedTemporaryFile( dir=tmp_dir )
61 # ref_file_name = ref_file.name
62 # ref_file.close()
63 # os.symlink( file, ref_file_name )
64
65 command = ( "/home/cmesser/galaxy/tools/jamm/JAMM.sh -s %s -g %s -o results -m %s -r %s -p %s"
66 % ( tmp_dir, args.gsize, args.mode, args.resolution, args.processes ) )
67 print command
68 # depending on how your programm is called, it may be necessary to use shlex.split on the command string before
69 # in this case, this was actually harmful. idk why
70 # command = shlex.split(command)
71 # Please read the docs for subprocess.Popen. You can't just pass as string as variables need to be extended
72 # Note that shell = True can be a security hazard.
73 # proc = subprocess.Popen( command, executable = '/bin/bash', shell=True )
74 proc = subprocess.Popen( command, shell=True)
75 returncode = proc.wait()
76
77 #mv files to a place where galaxy wrapper can find them
78 mvcommand = "mv %s/results/peaks/all.peaks.narrowPeak %s" % ( tmp_dir, args.peakfile )
79 os.system(mvcommand)
80
81 # clean up temp dir
82 if os.path.exists( tmp_dir ):
83 shutil.rmtree( tmp_dir )
84
85
86 if __name__ == "__main__":
87 main()
88