annotate ctat_analyze_differential_expression.py @ 0:2ca97cd84ce3 draft default tip

Upload ctat tools.
author trinity_ctat
date Tue, 17 Jul 2018 11:49:33 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
1 import sys, os, subprocess
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
2
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
3 TRINITY_BASE_DIR = ""
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
4 if os.environ.has_key('TRINITY_HOME'):
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
5 TRINITY_BASE_DIR = os.environ['TRINITY_HOME'];
2ca97cd84ce3 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.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
7 # 2017-09-26
2ca97cd84ce3 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.
2ca97cd84ce3 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.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
10 # I also found distutils.spawn.find_executable() which might work, but already implemented the below.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
11 try:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
12 pipe1 = subprocess.Popen(["which", "Trinity"], stdout=subprocess.PIPE)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
13 except:
2ca97cd84ce3 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])
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
15 sys.stderr.write(msg)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
16 # t, v, tb = sys.exc_info()
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
17 # raise t, v, tb
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
18 # For some reason the above was giving a syntax error.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
19 # A simple raise should reraise the existing exception.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
20 raise
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
21 else:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
22 TrinityPath, err_info = pipe1.communicate()
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
23 # FIX - probably should be checking err_info for errors...
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
24 # Determine the TRINITY_BASE_DIR from output1.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
25 # If TrinityPath is a link, we need to dereference the link.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
26 TrinityPath = TrinityPath.rstrip() # Need to strip off a newline.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
27 # print "Trinity that was found is: {:s}".format(repr(TrinityPath))
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
28 # print os.path.islink(TrinityPath)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
29 TrinityPath = os.path.abspath(TrinityPath)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
30 # msg = "The Absolute Trinity path that was found is: {:s}".format(TrinityPath)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
31 # print msg
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
32 # print os.path.islink(TrinityPath)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
33 while os.path.islink(TrinityPath):
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
34 # print "That path is a link."
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
35 TrinityPath = os.path.join(os.path.dirname(TrinityPath),os.readlink(TrinityPath))
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
36 # print "The new path is: {:s}".format(TrinityPath)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
37 # Take off the last part of the path (which is the Trinity command)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
38 TRINITY_BASE_DIR = "/".join(TrinityPath.split("/")[0:-1])
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
39 else:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
40 sys.stderr.write("Either set TRINITY_HOME to the trinity base directory, or ensure that directory is in the PATH before running.")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
41 sys.exit(1)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
42
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
43 usage= "usage: " + sys.argv[0] + " " + "edgeR.tar.gz " + "TMM_normalized_FPKM_matrix " + "P-value " + "C-value"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
44 print sys.argv
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
45 print usage
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
46 print " "
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
47
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
48 if len(sys.argv)<5:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
49 print "Require atleast two parameters"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
50 else:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
51 print "All good- command going ahead"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
52 print " "
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
53
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
54 Normalized_Matrix=sys.argv[2]
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
55 Pvalue=sys.argv[3]
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
56 Cvalue=sys.argv[4]
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
57
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
58 def run_command(cmd):
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
59 # 2017-10-02
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
60 # Cicada Dennis put the subprocess command in a try/except statement.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
61 # Errors were going undetected the way it was written previously.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
62 print "Running command:\n\t" + cmd
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
63 try:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
64 pipe = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
65 cmd_stdout, cmd_stderr = pipe.communicate()
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
66 except:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
67 msg = "ERROR while running command:\n\t" + cmd
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
68 sys.stderr.write(msg)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
69 raise
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
70
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
71 sys.stdout.write(cmd_stdout)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
72 ret = pipe.returncode
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
73 if ret:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
74 print "command died: " + str(ret)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
75 sys.stderr.write(cmd_stderr)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
76 sys.exit(ret)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
77 else:
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
78 # Any error output is written to stdout instead of stderr, since no error has occurred.
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
79 sys.stderr.write(cmd_stderr)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
80 return
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
81
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
82 print ""
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
83 Final_tar_gz= "edgeR.tar.gz"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
84 run_command("cp "+ sys.argv[1] + " " + Final_tar_gz)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
85 run_command("tar -xvf " + Final_tar_gz)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
86
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
87 print "Before moving files."
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
88 run_command("pwd")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
89 run_command("ls -lad ./*")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
90 print "\nedgeR_results/"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
91 run_command("ls -la edgeR_results")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
92 print ""
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
93
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
94 run_command("mv edgeR_results/* ." )
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
95
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
96 print "After moving files."
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
97 run_command("pwd")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
98 run_command("ls -lad ./*")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
99 print "\nedgeR_results/"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
100 run_command("ls -la edgeR_results")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
101 print ""
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
102
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
103 # run the analyze command
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
104 cmd= TRINITY_BASE_DIR + "/Analysis/DifferentialExpression/analyze_diff_expr.pl "+ "--matrix " + Normalized_Matrix + " -P " + Pvalue + " -C " + Cvalue
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
105 run_command(cmd)
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
106
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
107 origMatrixName= "diffExpr.P" + Pvalue + "_" + "C" + Cvalue + ".matrix"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
108 # diffExpr.P0.001_C2.0.matrix
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
109 run_command("mv " + origMatrixName + " diffExpr.matrix")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
110
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
111 SampleCorName= "diffExpr.P" + Pvalue + "_" + "C" + Cvalue + ".matrix.log2.centered.sample_cor.dat"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
112 # diffExpr.P0.001_C2.0.matrix.log2.sample_cor.dat
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
113 run_command("mv " + SampleCorName + " diffExpr.matrix.log2..sample_cor.dat")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
114
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
115 CorMatrix= "diffExpr.P" + Pvalue + "_" + "C" + Cvalue + ".matrix.log2.centered.sample_cor_matrix.pdf"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
116 # diffExpr.P0.001_C2.0.matrix.log2.sample_cor_matrix.pdf
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
117 run_command("mv " + CorMatrix + " diffExpr.matrix.log2.sample_cor_matrix.pdf")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
118
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
119 Heatmap= "diffExpr.P" + Pvalue + "_" + "C" + Cvalue + ".matrix.log2.centered.genes_vs_samples_heatmap.pdf"
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
120 #diffExpr.P0.001_C2.0.matrix.log2.centered.genes_vs_samples_heatmap.pdf
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
121 run_command("mv " + Heatmap + " diffExpr.matrix.log2.centered.genes_vs_samples_heatmap.pdf")
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
122
2ca97cd84ce3 Upload ctat tools.
trinity_ctat
parents:
diff changeset
123 sys.exit(0)