annotate trinityToolWrapper.py @ 5:98d99fed9d35 draft

Strip eol off of TrinityPath, so os.path.islink() works correctly.
author trinity_ctat
date Tue, 26 Sep 2017 13:33:20 -0400
parents cb71c6b0bf66
children 12bc09b4a26d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
1 #!/usr/bin/env python
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
2
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
3
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
4 # borrowed from: http://wiki.g2.bx.psu.edu/Future/Job%20Failure%20When%20stderr and modified for use with Trinity tools.
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
5
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
6 """
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
7 Wrapper that execute a program and its arguments but reports standard error
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
8 messages only if the program exit status was not 0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
9 Example: ./stderr_wrapper.py myprog arg1 -f arg2
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
10 """
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
11
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
12 import sys, subprocess, os
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
13
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
14 assert sys.version_info[:2] >= ( 2, 4 )
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
15
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
16 TRINITY_BASE_DIR = ""
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
17 if os.environ.has_key('TRINITY_HOME'):
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
18 TRINITY_BASE_DIR = os.environ['TRINITY_HOME'];
5
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
19 elif hasattr(os, 'symlink'): # symlink was implemented to always return false when it was not implemented in earlier versions.
2
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
20 # 2017-09-26
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
21 # Cicada Dennis added looking for the location of the Trinity program using the Unix "which" utility.
0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
22 # I tried using "command -v Trinity" but for some reason, I was getting a OS permission error with that.
2
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
23 # I just found distutils.spawn.find_executable() which might work, but already implemented the below.
0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
24 try:
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
25 pipe1 = subprocess.Popen(["which", "Trinity"], stdout=subprocess.PIPE)
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
26 except:
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
27 t, v, tb = sys.exc_info()
3
32b6a47c9312 Fix python syntax error.
trinity_ctat
parents: 2
diff changeset
28 sys.stderr.write("You must set the environmental variable TRINITY_HOME to the base installation directory of Trinity before running {:s}.".format(sys.argv[0]))
0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
29 raise t, v, tb
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
30 else:
2
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
31 TrinityPath, err_info = pipe1.communicate()
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
32 # FIX - probably should be checking err_info for errors...
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
33 # Determine the TRINITY_BASE_DIR from output1.
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
34 # If TrinityPath is a link, we need to dereference the link.
5
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
35 TrinityPath = TrinityPath.rstrip() # Need to strip off a newline.
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
36 # print "Trinity that was found is: {:s}".format(repr(TrinityPath))
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
37 # print os.path.islink(TrinityPath)
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
38 TrinityPath = os.path.abspath(TrinityPath)
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
39 print "The Absolute Trinity path that was found is: {:s}".format(TrinityPath)
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
40 # print os.path.islink(TrinityPath)
4
cb71c6b0bf66 Fix another python syntax error.
trinity_ctat
parents: 3
diff changeset
41 while os.path.islink(TrinityPath):
5
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
42 print "That path is a link."
2
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
43 TrinityPath = os.path.join(os.path.dirname(TrinityPath),os.readlink(TrinityPath))
5
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
44 print "The new path is: {:s}".format(TrinityPath)
0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
45 # Take off the last part of the path (which is the Trinity command)
2
9e5c3f162eca Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents: 0
diff changeset
46 TRINITY_BASE_DIR = "/".join(TrinityPath.split("/")[0:-1])
0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
47
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
48 # get bindir
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
49 bindir = sys.argv[0]
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
50 bindir = bindir.split("/")
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
51 if len(bindir) > 1:
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
52 bindir.pop()
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
53 bindir = "/".join(bindir)
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
54 else:
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
55 bindir = "."
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
56
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
57
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
58 ## add locations of tools to path setting.
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
59 TOOL_PATHS_FILE = bindir + "/__add_to_PATH_setting.txt";
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
60 for line in open(TOOL_PATHS_FILE):
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
61 line = line.rstrip()
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
62 os.environ['PATH'] += ":" + line
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
63
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
64
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
65 def stop_err( msg ):
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
66 sys.stderr.write( "%s\n" % msg )
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
67 sys.exit()
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
68
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
69 def __main__():
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
70 # Get command-line arguments
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
71 args = sys.argv
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
72 # Remove name of calling program, i.e. ./stderr_wrapper.py
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
73 args.pop(0)
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
74 # If there are no arguments left, we're done
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
75 if len(args) == 0:
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
76 return
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
77
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
78 # If one needs to silence stdout
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
79 #args.append( ">" )
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
80 #args.append( "/dev/null" )
5
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
81 print "The TRINITY_BASE_DIR is:\n\t{:s}".format(TRINITY_BASE_DIR)
0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
82
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
83 args[0] = "".join([TRINITY_BASE_DIR, '/', args[0]]);
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
84
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
85 cmdline = " ".join(args)
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
86
5
98d99fed9d35 Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents: 4
diff changeset
87 print "The command being invoked is:\n\t{:s}".format(cmdline)
0
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
88
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
89
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
90 try:
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
91 # Run program
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
92 err_capture = open("stderr.txt", 'w')
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
93 proc = subprocess.Popen( args=cmdline, shell=True, stderr=err_capture, stdout=sys.stdout )
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
94 returncode = proc.wait()
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
95 err_capture.close()
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
96
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
97
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
98 if returncode != 0:
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
99 raise Exception
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
100
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
101 except Exception:
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
102 # Running Grinder failed: write error message to stderr
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
103 err_text = open("stderr.txt").readlines()
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
104 stop_err( "ERROR:\n" + "\n".join(err_text))
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
105
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
106
a9723c18d389 Adding align_and_estimate_abundance tool.
trinity_ctat
parents:
diff changeset
107 if __name__ == "__main__": __main__()