comparison ctat_trinity_tool_wrapper.py @ 0:5eca0c75b178 draft default tip

Upload ctat tools.
author trinity_ctat
date Tue, 17 Jul 2018 11:47:32 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5eca0c75b178
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
13 print sys.version
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'];
20 elif hasattr(os, 'symlink'): # symlink was implemented to always return false when it was not implemented in earlier versions.
21 # 2017-09-26
22 # Cicada Dennis added looking for the location of the Trinity program using the Unix "which" utility.
23 # I tried using "command -v Trinity" but for some reason, I was getting a OS permission error with that.
24 # I just found distutils.spawn.find_executable() which might work, but already implemented the below.
25 try:
26 pipe1 = subprocess.Popen(["which", "Trinity"], stdout=subprocess.PIPE)
27 except:
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])
29 sys.stderr.write(msg)
30 # t, v, tb = sys.exc_info()
31 # raise t, v, tb
32 # For some reason the above was giving a syntax error.
33 # A simple raise should reraise the existing exception.
34 raise
35 else:
36 TrinityPath, err_info = pipe1.communicate()
37 # FIX - probably should be checking err_info for errors...
38 # Determine the TRINITY_BASE_DIR from output1.
39 # If TrinityPath is a link, we need to dereference the link.
40 TrinityPath = TrinityPath.rstrip() # Need to strip off a newline.
41 # print "Trinity that was found is: {:s}".format(repr(TrinityPath))
42 # print os.path.islink(TrinityPath)
43 TrinityPath = os.path.abspath(TrinityPath)
44 # msg = "The Absolute Trinity path that was found is: {:s}".format(TrinityPath)
45 # print msg
46 # print os.path.islink(TrinityPath)
47 while os.path.islink(TrinityPath):
48 # print "That path is a link."
49 TrinityPath = os.path.join(os.path.dirname(TrinityPath),os.readlink(TrinityPath))
50 # print "The new path is: {:s}".format(TrinityPath)
51 # Take off the last part of the path (which is the Trinity command)
52 TRINITY_BASE_DIR = "/".join(TrinityPath.split("/")[0:-1])
53 else:
54 sys.stderr.write("Either set TRINITY_HOME to the trinity base directory, or ensure that directory is in the PATH before running.")
55 sys.exit(1)
56
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.
69 #TOOL_PATHS_FILE = bindir + "/__add_to_PATH_setting.txt";
70 #for line in open(TOOL_PATHS_FILE):
71 # line = line.rstrip()
72 # os.environ['PATH'] += ":" + line
73
74 # Add TrinityPath and its utils to the PATH environment variable.
75 # print "Initially the PATH env variable is:\n\t{:s}".format(os.environ['PATH'])
76 os.environ['PATH'] = os.environ['PATH'] + ":{:s}:{:s}".format(TRINITY_BASE_DIR,TRINITY_BASE_DIR+"/util")
77 # print "Now the PATH env variable is:\n\t{:s}".format(os.environ['PATH'])
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" )
96 print "The TRINITY_BASE_DIR is:\n\t{:s}".format(TRINITY_BASE_DIR)
97 print "The PATH env variable is:\n\t{:s}".format(os.environ['PATH'])
98
99 args[0] = "".join([TRINITY_BASE_DIR, '/', args[0]]);
100
101 cmdline = " ".join(args)
102
103 print "The command being invoked is:\n\t{:s}".format(cmdline)
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__()