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