Mercurial > repos > trinity_ctat > align_and_estimate_abundance
annotate trinityToolWrapper.py @ 16:e27d87a63bb2 draft default tip
Adding profile info and exit code behavior. Making version numbers consistent. Upping test number.
| author | trinity_ctat |
|---|---|
| date | Fri, 06 Oct 2017 10:58:53 -0400 |
| parents | c76d9b3e67b2 |
| children |
| rev | line source |
|---|---|
| 0 | 1 #!/usr/bin/env python |
| 2 | |
| 3 | |
| 4 # borrowed from: http://wiki.g2.bx.psu.edu/Future/Job%20Failure%20When%20stderr and modified for use with Trinity tools. | |
| 5 | |
| 6 """ | |
| 7 Wrapper that execute a program and its arguments but reports standard error | |
| 8 messages only if the program exit status was not 0 | |
| 9 Example: ./stderr_wrapper.py myprog arg1 -f arg2 | |
| 10 """ | |
| 11 | |
| 12 import sys, subprocess, os | |
|
11
2e0e45063fad
Trying to fix a syntax error. Seems like the wrong python is being used.
trinity_ctat
parents:
10
diff
changeset
|
13 print sys.version |
| 0 | 14 |
| 15 assert sys.version_info[:2] >= ( 2, 4 ) | |
| 16 | |
| 17 TRINITY_BASE_DIR = "" | |
| 18 if os.environ.has_key('TRINITY_HOME'): | |
| 19 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
|
20 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
|
21 # 2017-09-26 |
|
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
22 # Cicada Dennis added looking for the location of the Trinity program using the Unix "which" utility. |
| 0 | 23 # 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
|
24 # I just found distutils.spawn.find_executable() which might work, but already implemented the below. |
| 0 | 25 try: |
| 26 pipe1 = subprocess.Popen(["which", "Trinity"], stdout=subprocess.PIPE) | |
| 27 except: | |
| 10 | 28 msg = "You must set the environmental variable TRINITY_HOME to the base installation directory of Trinity before running {:s}.\n".format(sys.argv[0]) |
| 8 | 29 sys.stderr.write(msg) |
| 9 | 30 # t, v, tb = sys.exc_info() |
| 31 # raise t, v, tb | |
| 32 # For some reason the above was giving a syntax error. | |
| 10 | 33 # A simple raise should reraise the existing exception. |
| 9 | 34 raise |
| 0 | 35 else: |
|
2
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
36 TrinityPath, err_info = pipe1.communicate() |
|
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
37 # 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
|
38 # Determine the TRINITY_BASE_DIR from output1. |
|
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
39 # 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
|
40 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
|
41 # 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
|
42 # print os.path.islink(TrinityPath) |
|
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
43 TrinityPath = os.path.abspath(TrinityPath) |
| 10 | 44 # msg = "The Absolute Trinity path that was found is: {:s}".format(TrinityPath) |
| 45 # print msg | |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
46 # print os.path.islink(TrinityPath) |
| 4 | 47 while os.path.islink(TrinityPath): |
| 10 | 48 # print "That path is a link." |
|
2
9e5c3f162eca
Needed to dereference links when searching for TRINITY_BASE_DIR.
trinity_ctat
parents:
0
diff
changeset
|
49 TrinityPath = os.path.join(os.path.dirname(TrinityPath),os.readlink(TrinityPath)) |
| 10 | 50 # print "The new path is: {:s}".format(TrinityPath) |
| 0 | 51 # 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
|
52 TRINITY_BASE_DIR = "/".join(TrinityPath.split("/")[0:-1]) |
|
6
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
53 else: |
|
7
78d12a725443
Adding additional requirements for align_and_estimate_abundance.
trinity_ctat
parents:
6
diff
changeset
|
54 sys.stderr.write("Either set TRINITY_HOME to the trinity base directory, or ensure that directory is in the PATH before running.") |
| 15 | 55 sys.exit(1) |
|
7
78d12a725443
Adding additional requirements for align_and_estimate_abundance.
trinity_ctat
parents:
6
diff
changeset
|
56 |
| 0 | 57 |
| 58 # get bindir | |
| 59 bindir = sys.argv[0] | |
| 60 bindir = bindir.split("/") | |
| 61 if len(bindir) > 1: | |
| 62 bindir.pop() | |
| 63 bindir = "/".join(bindir) | |
| 64 else: | |
| 65 bindir = "." | |
| 66 | |
| 67 | |
| 68 ## add locations of tools to path setting. | |
|
6
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
69 #TOOL_PATHS_FILE = bindir + "/__add_to_PATH_setting.txt"; |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
70 #for line in open(TOOL_PATHS_FILE): |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
71 # line = line.rstrip() |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
72 # os.environ['PATH'] += ":" + line |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
73 |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
74 # Add TrinityPath and its utils to the PATH environment variable. |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
75 # print "Initially the PATH env variable is:\n\t{:s}".format(os.environ['PATH']) |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
76 os.environ['PATH'] = os.environ['PATH'] + ":{:s}:{:s}".format(TRINITY_BASE_DIR,TRINITY_BASE_DIR+"/util") |
|
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
77 # print "Now the PATH env variable is:\n\t{:s}".format(os.environ['PATH']) |
| 0 | 78 |
| 79 | |
| 80 def stop_err( msg ): | |
| 81 sys.stderr.write( "%s\n" % msg ) | |
| 82 sys.exit() | |
| 83 | |
| 84 def __main__(): | |
| 85 # Get command-line arguments | |
| 86 args = sys.argv | |
| 87 # Remove name of calling program, i.e. ./stderr_wrapper.py | |
| 88 args.pop(0) | |
| 89 # If there are no arguments left, we're done | |
| 90 if len(args) == 0: | |
| 91 return | |
| 92 | |
| 93 # If one needs to silence stdout | |
| 94 #args.append( ">" ) | |
| 95 #args.append( "/dev/null" ) | |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
96 print "The TRINITY_BASE_DIR is:\n\t{:s}".format(TRINITY_BASE_DIR) |
|
6
12bc09b4a26d
Adding TrinityPath/util to the PATH so other utilities can be found.
trinity_ctat
parents:
5
diff
changeset
|
97 print "The PATH env variable is:\n\t{:s}".format(os.environ['PATH']) |
| 0 | 98 |
| 99 args[0] = "".join([TRINITY_BASE_DIR, '/', args[0]]); | |
| 100 | |
| 101 cmdline = " ".join(args) | |
| 102 | |
|
5
98d99fed9d35
Strip eol off of TrinityPath, so os.path.islink() works correctly.
trinity_ctat
parents:
4
diff
changeset
|
103 print "The command being invoked is:\n\t{:s}".format(cmdline) |
| 0 | 104 |
| 105 | |
| 106 try: | |
| 107 # Run program | |
| 108 err_capture = open("stderr.txt", 'w') | |
| 109 proc = subprocess.Popen( args=cmdline, shell=True, stderr=err_capture, stdout=sys.stdout ) | |
| 110 returncode = proc.wait() | |
| 111 err_capture.close() | |
| 112 | |
| 113 | |
| 114 if returncode != 0: | |
| 115 raise Exception | |
| 116 | |
| 117 except Exception: | |
| 118 # Running Grinder failed: write error message to stderr | |
| 119 err_text = open("stderr.txt").readlines() | |
| 120 stop_err( "ERROR:\n" + "\n".join(err_text)) | |
| 121 | |
| 122 | |
| 123 if __name__ == "__main__": __main__() |
