comparison mothur/tools/mothur/TreeVector.py @ 0:3202a38e44d9

Migrated tool version 1.15.1 from old tool shed archive to new tool shed repository
author jjohnson
date Tue, 07 Jun 2011 17:32:23 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3202a38e44d9
1 #!/usr/bin/env python
2
3 """
4 Runs TreeVector on a newick file;
5 TODO: more documentation
6 """
7
8 import optparse, os, shutil, subprocess, sys, tempfile, re, string
9
10 def stop_err( msg ):
11 sys.stderr.write( '%s\n' % msg )
12 sys.exit()
13
14 def __main__():
15 #Parse Command Line
16 parser = optparse.OptionParser()
17 parser.add_option( '-i', '--input', dest='input', help='The sequence input file' )
18 parser.add_option( '-s', '--shape', dest='shape', help='Branch shape' )
19 parser.add_option( '-l', '--length', dest='length', help='Branch length' )
20 parser.add_option( '-g', '--svg', dest='svg', help='Graph in SVG format' )
21 parser.add_option( '-p', '--jarBin', dest='jarBin', default='', help='The path to where jars are stored' )
22 parser.add_option( '-j', '--jarFile', dest='jarFile', help='The file name of the jar file to use')
23 parser.add_option( '-x', '--jvmArgs', dest='jvmArgs', help='Java JVM arguments, e.g -Xmx250m')
24 (options, args) = parser.parse_args()
25 if options.jarBin == None:
26 stop_err("Misssing option --jarBin")
27 elif options.jarFile == None:
28 stop_err("Misssing option --jarFile")
29 elif options.input == None:
30 stop_err("Misssing option --input")
31 params = []
32 props = []
33 if options.jvmArgs != None:
34 props.append(options.jvmArgs)
35 if options.shape != None and options.shape != 'None':
36 params.append('-%s' % options.shape)
37 if options.length != None and options.length != 'None':
38 params.append('-%s' % options.length)
39 if options.svg != None and options.svg != 'None':
40 params.append('-out %s' % options.svg)
41 # make temp directory
42 buffsize = 1048576
43 tmp_dir = tempfile.mkdtemp()
44 # print("tmp_dir %s" % tmp_dir)
45 # generate commandline
46 cmd = 'java %s -jar %s %s %s' % (' '.join(props), os.path.join( options.jarBin, options.jarFile ), options.input, ' '.join(params))
47 # print >> sys.stderr, cmd
48 # need to nest try-except in try-finally to handle 2.4
49 try:
50 try:
51 proc = subprocess.Popen( args=cmd, shell=True, stderr=subprocess.PIPE )
52 returncode = proc.wait()
53 stderr = proc.stderr.read()
54 if returncode != 0:
55 raise Exception, stderr
56 except Exception, e:
57 raise Exception, 'Error executing TeeVector. ' + str( e )
58 except Exception, e:
59 stop_err( 'TreeVector failed.\n' + str( e ) )
60
61 if __name__=="__main__": __main__()