annotate ctat_edger_differential_expression.py @ 0:f36264827fb4 draft default tip

Upload ctat tools.
author trinity_ctat
date Tue, 17 Jul 2018 11:52:09 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
1 import sys, os, subprocess
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
2
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
3 TRINITY_BASE_DIR = ""
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
4 if os.environ.has_key('TRINITY_HOME'):
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
5 TRINITY_BASE_DIR = os.environ['TRINITY_HOME'];
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
6 elif hasattr(os, 'symlink'): # symlink was implemented to always return false when it was not implemented in earlier versions of python.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
7 # 2017-09-26
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
8 # Cicada Dennis added looking for the location of the Trinity program using the Unix "which" utility.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
9 # I tried using "command -v Trinity" but for some reason, I was getting a OS permission error with that.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
10 # I also found distutils.spawn.find_executable() which might work, but already implemented the below.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
11 try:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
12 pipe1 = subprocess.Popen(["which", "Trinity"], stdout=subprocess.PIPE)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
13 except:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
14 msg = "You must set the environmental variable TRINITY_HOME to the base installation directory of Trinity before running {:s}.\n".format(sys.argv[0])
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
15 sys.stderr.write(msg)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
16 # t, v, tb = sys.exc_info()
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
17 # raise t, v, tb
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
18 # For some reason the above was giving a syntax error.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
19 # A simple raise should reraise the existing exception.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
20 raise
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
21 else:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
22 TrinityPath, err_info = pipe1.communicate()
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
23 # FIX - probably should be checking err_info for errors...
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
24 # Determine the TRINITY_BASE_DIR from output1.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
25 # If TrinityPath is a link, we need to dereference the link.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
26 TrinityPath = TrinityPath.rstrip() # Need to strip off a newline.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
27 # print "Trinity that was found is: {:s}".format(repr(TrinityPath))
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
28 # print os.path.islink(TrinityPath)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
29 TrinityPath = os.path.abspath(TrinityPath)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
30 # msg = "The Absolute Trinity path that was found is: {:s}".format(TrinityPath)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
31 # print msg
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
32 # print os.path.islink(TrinityPath)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
33 while os.path.islink(TrinityPath):
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
34 # print "That path is a link."
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
35 TrinityPath = os.path.join(os.path.dirname(TrinityPath),os.readlink(TrinityPath))
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
36 # print "The new path is: {:s}".format(TrinityPath)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
37 # Take off the last part of the path (which is the Trinity command)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
38 TRINITY_BASE_DIR = "/".join(TrinityPath.split("/")[0:-1])
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
39 else:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
40 sys.stderr.write("Either set TRINITY_HOME to the trinity base directory, " + \
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
41 "or ensure that directory is in the PATH before running.")
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
42 sys.exit(1)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
43
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
44 usage= "usage: " + " $counts_matrix" + " $dispersion"
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
45
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
46 if len(sys.argv)<2:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
47 print "Require at least two parameters, " + usage
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
48 else:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
49 print "All good- command going ahead"
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
50 print " "
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
51
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
52 def run_command(cmd):
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
53 # 2017-10-02
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
54 # Cicada Dennis put the subprocess command in a try/except statement.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
55 # Errors were going undetected the way it was written previously.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
56 print "Running command:\n\t" + cmd
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
57 try:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
58 pipe = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
59 cmd_stdout, cmd_stderr = pipe.communicate()
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
60 except:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
61 msg = "ERROR while running command:\n\t" + cmd
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
62 sys.stderr.write(msg)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
63 raise
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
64
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
65 sys.stdout.write(cmd_stdout)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
66 ret = pipe.returncode
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
67 if ret:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
68 print "command died: " + str(ret)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
69 sys.stderr.write(cmd_stderr)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
70 sys.exit(ret)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
71 else:
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
72 # Any error output is written to stdout instead of stderr, since no error has occurred.
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
73 sys.stderr.write(cmd_stderr)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
74 return
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
75
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
76 print ""
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
77
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
78 countmatrix= "counts_matrix"
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
79
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
80 cmd= "cp " + sys.argv[1] + " " + countmatrix
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
81 run_command(cmd)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
82
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
83 cmd= TRINITY_BASE_DIR + "/Analysis/DifferentialExpression/run_DE_analysis.pl "+ " --matrix "+ countmatrix + " --method edgeR " + " --output edgeR_results "+ " --dispersion " + sys.argv[2] + " --tar_gz_outdir"
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
84
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
85 run_command(cmd)
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
86
f36264827fb4 Upload ctat tools.
trinity_ctat
parents:
diff changeset
87 sys.exit(0)