annotate rgDGE.py @ 2:e019d4e43537

Initial checkin of test files - move to test-data when installed.
author fubar
date Fri, 09 Sep 2011 01:10:19 -0400
parents 1959becd0592
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
1 # Copyright ross lazarus
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
2 # september 2011
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
3 # all rights reserved
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
4 # for the Rgenetics project
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
5 # all rights reserved
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
6 # licensed to you under the terms of the LGPL as documented at http://www.gnu.org/copyleft/lesser.html
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
7
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
8 import sys
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
9 import shutil
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
10 import subprocess
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
11 import os
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
12 import time
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
13 import tempfile
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
14 import optparse
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
15
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
16 progname = os.path.split(sys.argv[0])[1]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
17 myversion = 'V000.2 September 2011'
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
18 verbose = False
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
19 debug = False
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
20
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
21
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
22 galhtmlprefix = """<?xml version="1.0" encoding="utf-8" ?>
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
23 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
24 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
25 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
26 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" />
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
27 <title></title>
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
28 <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
29 </head>
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
30 <body>
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
31 <div class="document">
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
32 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
33 galhtmlattr = """<b><a href="http://rgenetics.org">Galaxy Rgenetics</a> tool output %s run at %s</b><br/>"""
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
34 galhtmlpostfix = """</div></body></html>\n"""
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
35
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
36 DGESCRIPT="""
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
37 #### edgeR.Rscript
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
38 #
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
39 # Performs DGE on a count table containing n replicates of two conditions
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
40 #
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
41 # Parameters
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
42 #
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
43 # 1 - Output Dir
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
44
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
45 # Writen by: S.Lunke and A.Kaspi
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
46 ####
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
47
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
48
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
49 usage <- function(){
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
50 print("#### edgeR.R", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
51 print("", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
52 print("Performs DGE on a count table containing n replicates of two conditions", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
53 print("", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
54 print("USAGE: Rscript edgeR.R <OUT_DIR> <INPUT> <TreatmentName> <ControlName> <Treatcol1,2,3> <Controlcol1,2,3>", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
55 print("", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
56 print(" Parameters", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
57 print("", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
58 print(" 1 - Output Dir", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
59 print(" 2 - Input File", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
60 print(" 3 - Treatment name", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
61 print(" 4 - Treatment Columns i.e. 3,4,6", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
62 print(" 5 - Control name", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
63 print(" 6 - Control Columns i.e. 2,7,8", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
64 print(" 7 - Output tabular file name", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
65
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
66
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
67 print("", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
68 print(" Interface writen by: S.Lunke and A.Kaspi", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
69 print(" Makes extensive use of the edgeR software - Mark D. Robinson, Davis J. McCarthy, Gordon K. Smyth, PMCID: PMC2796818", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
70 print("####", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
71 q()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
72 }
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
73
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
74
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
75 ## edgeIt runs edgeR
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
76 edgeIt <- function (Input,group,outputfilename) {
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
77
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
78
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
79 ## Error handling
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
80 if (length(unique(group))!=2){
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
81 print("Number of conditions identified in experiment does not equal 2")
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
82 q()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
83 }
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
84
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
85 #The workhorse
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
86 require(edgeR)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
87
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
88 ## Setup DGEList object
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
89 DGEList <- DGEList(Count_Matrix, group = group)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
90
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
91 #Extract T v C names
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
92 TName=unique(group)[1]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
93 CName=unique(group)[2]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
94
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
95 # Outfile name - we need something more predictable than
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
96 # outfile <- paste(Out_Dir,"/",TName,"_Vs_",CName, sep="")
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
97 # since it needs to be renamed and squirted into the history so added as a paramter
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
98
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
99 # Filter all tags that have less than one read per million in half + 1 or more libraries. n = ceiling(length(colnames(DGEList))/2)+1
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
100 n = ceiling(length(colnames(DGEList))/2)+1
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
101 DGEList <- DGEList[rowSums(1e+06 * DGEList$counts/expandAsMatrix(DGEList$samples$lib.size, dim(DGEList)) > 1) >= n, ]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
102
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
103
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
104 ## Run tagwise dispersion test and DGE analysis
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
105 prior.n <- ceiling(50/(length(group)-length(2)))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
106 DGEList <- calcNormFactors(DGEList)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
107 DGEList <- estimateCommonDisp(DGEList)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
108 DGEList <- estimateTagwiseDisp(DGEList, prior.n=prior.n, trend=T, grid.length=1000)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
109 DE <- exactTest(DGEList, common.disp=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
110
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
111 ## Normalized data RPM
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
112 normData <- (1e+06 * DGEList$counts/expandAsMatrix(DGEList$samples$lib.size, dim(DGEList)))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
113 colnames(normData) <- paste( "norm",colnames(normData),sep="_")
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
114
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
115
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
116 #Prepare our output file
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
117 output <- cbind(
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
118 Name=as.character(rownames(DGEList$counts)),
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
119 DE$table,
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
120 adj.p.value=p.adjust(DE$table$p.value, method="fdr"),
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
121 Dispersion=DGEList$tagwise.dispersion,normData,
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
122 DGEList$counts
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
123 )
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
124 soutput = output[order(output$p.val),] # sorted into p value order - for quick toptable
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
125 #Write output
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
126 write.table(soutput,outputfilename, quote=FALSE, sep="\t",row.names=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
127
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
128
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
129 ## Plot MAplot
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
130 pdf("Smearplot.pdf")
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
131 #TODO colour siggenes
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
132 nsig = sum(output$adj.p.value < 0.05)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
133 deTags = rownames(topTags(DE,n=nsig)$table)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
134 plotSmear(DGEList,de.tags=deTags,main=paste("Smear Plot for ",TName,' Vs ',CName,' (FDR@0.05, N = ',nsig,')',sep=''))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
135 grid(col="blue")
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
136 dev.off()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
137
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
138 ## Plot MDS
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
139 pdf("MDSplot.pdf")
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
140 plotMDS.dge(DGEList,xlim=c(-2,1),main=paste("MDS Plot for",TName,'Vs',CName),cex=0.5)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
141 grid(col="blue")
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
142 dev.off()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
143
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
144 #Return our main table
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
145 output
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
146
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
147 } #Done
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
148
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
149 #### MAIN ####
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
150
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
151 parameters <- commandArgs(trailingOnly=T)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
152
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
153 ## Error handling
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
154 if (length(parameters) < 6){
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
155 print("Not enough Input files supplied. Specify at least two input files.", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
156 print("", quote=F)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
157 usage()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
158 }
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
159
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
160 Out_Dir <- as.character(parameters[1])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
161 Input <- as.character(parameters[2])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
162 TreatmentName <- as.character(parameters[3])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
163 TreatmentCols <- as.character(parameters[4])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
164 ControlName <- as.character(parameters[5])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
165 ControlCols <- as.character(parameters[6])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
166 outputfilename <- as.character(parameters[7])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
167
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
168
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
169 #Set our columns
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
170 TCols = as.numeric(strsplit(TreatmentCols,",")[[1]])-1 #+1
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
171 CCols = as.numeric(strsplit(ControlCols,",")[[1]])-1 #+1
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
172 cat('## got TCols=')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
173 cat(TCols)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
174 cat('; CCols=')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
175 cat(CCols)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
176 cat('\n')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
177
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
178
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
179 group<-strsplit(TreatmentCols,",")[[1]]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
180
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
181 ## Create output dir if non existent
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
182 if (file.exists(Out_Dir) == F) dir.create(Out_Dir)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
183
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
184 Count_Matrix<-read.table(Input,header=T,row.names=1,sep='\t') #Load tab file assume header
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
185 Count_Matrix<-Count_Matrix[,c(TCols,CCols)] #extract columns we care about
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
186 group<-c(rep(TreatmentName,length(TCols)), rep(ControlName,length(CCols)) ) #Build a group descriptor
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
187 colnames(Count_Matrix) <- paste(group,colnames(Count_Matrix),sep="_") #Relable columns
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
188
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
189
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
190 results <- edgeIt(Input,group,outputfilename) #Run the main function
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
191 # for the log
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
192 sessionInfo()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
193
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
194 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
195
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
196 def timenow():
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
197 """return current time as a string
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
198 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
199 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
200
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
201 class DGE:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
202 """class is a wrapper for DGE - note hard coded script above so it's all in one place for Galaxy"""
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
203
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
204 def __init__(self,myName=None,opts=None):
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
205 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
206 Rscript edgeR.R results HGHN_mRNA_counts.txt HiGly 2,3 Control 1,4
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
207 Out_Dir <- as.character(parameters[1])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
208 Input <- as.character(parameters[2])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
209 TreatmentName <- as.character(parameters[3])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
210 TreatmentCols <- as.character(parameters[4])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
211 ControlName <- as.character(parameters[5])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
212 ControlCols <- as.character(parameters[6])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
213 outputfilename <- as.character(parameters[7])
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
214 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
215 self.thumbformat = 'jpg'
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
216 self.tlog = os.path.join(opts.output_dir,"DGE_runner.log")
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
217 self.opts = opts
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
218 self.myName=myName
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
219 self.cl = []
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
220 a = self.cl.append
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
221 rfname = os.path.join(opts.output_dir,'DGE.R')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
222 rscript = open(rfname,'w')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
223 rscript.write(DGESCRIPT)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
224 rscript.close()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
225 a('Rscript')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
226 a(rfname)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
227 a("%s" % opts.output_dir)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
228 a("%s" % opts.input_matrix)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
229 a("%s" % opts.treatname)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
230 a("%s" % opts.treatcols)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
231 a("%s" % opts.ctrlname)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
232 a("%s" % opts.ctrlcols)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
233 a("%s" % opts.output_tab)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
234
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
235 def compressPDF(self,inpdf=None,thumbformat='png'):
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
236 """need absolute path to pdf
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
237 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
238 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf,self.myName)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
239 sto = open(self.tlog,'a')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
240 outpdf = '%s_compressed' % inpdf
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
241 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dBATCH", "-sOutputFile=%s" % outpdf,inpdf]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
242 x = subprocess.Popen(cl,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
243 retval1 = x.wait()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
244 if retval1 == 0:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
245 os.unlink(inpdf)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
246 shutil.move(outpdf,inpdf)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
247 outpng = '%s.%s' % (os.path.splitext(inpdf)[0],thumbformat)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
248 cl2 = ['convert', inpdf, outpng]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
249 x = subprocess.Popen(cl2,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
250 retval2 = x.wait()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
251 sto.close()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
252 retval = retval1 or retval2
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
253 return retval
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
254
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
255 def runDGE(self):
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
256 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
257 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
258 sto = open(self.tlog,'w')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
259 x = subprocess.Popen(' '.join(self.cl),shell=True,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
260 retval = x.wait()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
261 sto.close()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
262 flist = os.listdir(self.opts.output_dir)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
263 flist.sort()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
264 html = [galhtmlprefix % progname,]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
265 html.append('<h2>Galaxy DGE outputs run at %s<h2></br>Click on the images below to download high quality PDF versions</br>\n' % timenow())
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
266 if len(flist) > 0:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
267 html.append('<table>\n')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
268 for fname in flist:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
269 dname,e = os.path.splitext(fname)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
270 if e.lower() == '.pdf' : # compress and make a thumbnail
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
271 thumb = '%s.%s' % (dname,self.thumbformat)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
272 pdff = os.path.join(self.opts.output_dir,fname)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
273 retval = self.compressPDF(inpdf=pdff,thumbformat=self.thumbformat)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
274 if retval == 0:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
275 s= '<tr><td><a href="%s"><img src="%s" title="Click to download a print quality PDF of %s" hspace="10" width="600"></a></td></tr>' \
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
276 % (fname,thumb,fname)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
277 else:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
278 dname = '%s (thumbnail image not_found)' % fname
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
279 s= '<tr><td><a href="%s">%s</a></td></tr>' % (fname,dname)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
280 html.append(s)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
281 else:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
282 html.append('<tr><td><a href="%s">%s</a></td></tr>' % (fname,fname))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
283 html.append('</table>\n')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
284 else:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
285 html.append('<h2>### Error - R returned no files - please confirm that parameters are sane</h1>')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
286 html.append('<h3>R log follows below</h3><hr><pre>\n')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
287 rlog = open(self.tlog,'r').readlines()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
288 html += rlog
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
289 html.append('%s CL = %s</br>\n' % (self.myName,' '.join(sys.argv)))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
290 html.append('DGE.R CL = %s</br>\n' % (' '.join(self.cl)))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
291 html.append('</pre>\n')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
292 html.append(galhtmlattr % (progname,timenow()))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
293 html.append(galhtmlpostfix)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
294 htmlf = file(self.opts.output_html,'w')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
295 htmlf.write('\n'.join(html))
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
296 htmlf.write('\n')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
297 htmlf.close()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
298
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
299
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
300 def main():
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
301 u = """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
302 This is a Galaxy wrapper. It expects to be called by DGE.xml as:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
303 <command interpreter="python">rgDGE.py --output_html "$html_file.files_path" --input_matrix "$input1" --treatcols "$Treat_cols" --treatname "$treatment_name"
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
304 --ctrlcols "$Control_cols"
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
305 --ctrlname "$control_name" --output_tab "$outtab" --output_html "$html_file" --output_dir "$html_file.files_path" --method "edgeR"
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
306 </command>
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
307 """
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
308 op = optparse.OptionParser()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
309 a = op.add_option
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
310 a('--input_matrix',default=None)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
311 a('--output_tab',default=None)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
312 a('--output_html',default=None)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
313 a('--treatcols',default=None)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
314 a('--treatname',default='Treatment')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
315 a('--ctrlcols',default=None)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
316 a('--ctrlname',default='Control')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
317 a('--output_dir',default=None)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
318 a('--method',default='edgeR')
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
319 opts, args = op.parse_args()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
320 assert opts.input_matrix and opts.output_html,u
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
321 assert os.path.isfile(opts.input_matrix),'## DGE runner unable to open supplied input file %s' % opts.input_matrix
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
322 assert opts.treatcols,'## DGE runner requires a comma separated list of treatment columns eg --treatcols 4,5,6'
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
323 assert opts.treatcols,'## DGE runner requires a comma separated list of control columns eg --ctlcols 2,3,7'
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
324 myName=os.path.split(sys.argv[0])[-1]
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
325 if not os.path.exists(opts.output_dir):
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
326 os.makedirs(opts.output_dir)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
327 m = DGE(myName, opts=opts)
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
328 retcode = m.runDGE()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
329 if retcode:
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
330 sys.exit(retcode) # indicate failure to job runner
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
331
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
332
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
333
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
334 if __name__ == "__main__":
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
335 main()
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
336
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
337
1959becd0592 Initial upload of files for DGE tool
fubar
parents:
diff changeset
338