annotate ctat_edger_differential_expression.py @ 1:57907c40385c draft default tip

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