comparison analyze_diff_exp_wrapper.py @ 0:5bbc80dbfb5f draft

Adding analyze differential expression.
author trinity_ctat
date Fri, 29 Sep 2017 14:36:11 -0400
parents
children 6f54e220d035
comparison
equal deleted inserted replaced
-1:000000000000 0:5bbc80dbfb5f
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 print "The command used: " + cmd
60 pipe= subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
61 pipe.wait()
62 ret= pipe.returncode
63 if ret:
64 print "command died: " + str(ret)
65 print pipe.stderr.readlines()
66 sys.exit(1)
67 else:
68 return
69 print " "
70
71 Final_tar_gz= "edgeR.tar.gz"
72 run_command("cp "+ sys.argv[1] + " " + "Final_tar_gz")
73 run_command("tar -xvf " + "Final_tar_gz")
74 run_command("mv " + "edgeR_results" + "/* ." )
75
76 # run the analyze command
77 cmd= TRINITY_BASE_DIR + "/Analysis/DifferentialExpression/analyze_diff_expr.pl "+ "--matrix " + Normalized_Matrix + " -P " + Pvalue + " -C " + Cvalue
78 run_command(cmd)
79
80 origMatrixName= "diffExpr.P" + Pvalue + "_" + "C" + Cvalue + ".matrix"
81 # diffExpr.P0.001_C2.0.matrix
82 run_command("mv " + origMatrixName + " diffExpr.matrix")
83
84 SampleCorName= "diffExpr.P" + Pvalue + "_" + "C" + Cvalue + ".matrix.log2.sample_cor.dat"
85 # diffExpr.P0.001_C2.0.matrix.log2.sample_cor.dat
86 run_command("mv " + SampleCorName + " diffExpr.matrix.log2.sample_cor.dat")
87
88 CorMatrix= "diffExpr.P" + Pvalue + "_" + "C" + Cvalue + ".matrix.log2.sample_cor_matrix.pdf"
89 # diffExpr.P0.001_C2.0.matrix.log2.sample_cor_matrix.pdf
90 run_command("mv " + CorMatrix + " diffExpr.matrix.log2.sample_cor_matrix.pdf")
91
92 Heatmap= "diffExpr.P" + Pvalue + "_" + "C" + Cvalue + ".matrix.log2.centered.genes_vs_samples_heatmap.pdf"
93 #diffExpr.P0.001_C2.0.matrix.log2.centered.genes_vs_samples_heatmap.pdf
94 run_command("mv " + Heatmap + " diffExpr.matrix.log2.centered.genes_vs_samples_heatmap.pdf")
95
96 sys.exit(0)