annotate deseq/deseq.R @ 3:a49aff09553e draft default tip

Uploaded
author nikhil-joshi
date Wed, 09 Jan 2013 18:39:12 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
1 ## Run DESeq (oversimplified) in Galaxy.
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
2 ## Format: Rscript deseq3.R tab-delimited-counts.txt comma,delimited,col,names,except,first,gene,id,col comparison,here outputfile
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
3 ##
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
4 ## The incoming data must have the first column be the gene names, and
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
5 ## the rest raw counts. The next argument is the list of groups
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
6 ## (i.e. liver,liver,kidney,kidney), and the final argument is the
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
7 ## comparison, i.e. kidney,liver. This produces a top-table called
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
8 ## top-table.txt ordered by p-value.
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
9
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
10 cargs <- commandArgs()
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
11 cargs <- cargs[(which(cargs == "--args")+1):length(cargs)]
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
12
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
13 countstable <- cargs[1]
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
14 conds <- unlist(strsplit(cargs[2], ','))
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
15 comparison <- unlist(strsplit(cargs[3], ','))
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
16 outputfile <- cargs[4]
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
17 diag.html = cargs[5]
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
18 temp.files.path = cargs[6]
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
19 counts.name = cargs[7]
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
20
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
21 # the comparison must only have two values and the conds must
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
22 # be a vector from those values, at least one of each.
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
23 if (length(unique(conds)) != 2) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
24 stop("You can only have two columns types: ", cargs[2])
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
25 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
26
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
27 if (length(comparison) != 2) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
28 stop("Comparison type must be a tuple: ", cargs[3])
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
29 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
30
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
31 if (!identical(sort(comparison), sort(unique(conds)))) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
32 stop("Column types must use the two names from Comparison type, and vice versa. Must have at least one of each in the Column types.\nColumn types: ", cargs[2], "\n", "Comparison type: ", cargs[3])
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
33 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
34
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
35 sink("/dev/null")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
36 dir.create(temp.files.path, recursive=TRUE)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
37 library(DESeq)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
38
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
39 d <- read.table(countstable, sep="\t", header=TRUE, row.names=1)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
40
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
41 if (length(d) != length(conds)) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
42 stop("Number of total sample columns in counts file must correspond to the columns types field. E.g. if column types is 'kidney,kidney,liver,liver' then number of sample columns in counts file must be 4 as well.")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
43 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
44
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
45 cds <- newCountDataSet(d, conds)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
46 cds <- estimateSizeFactors(cds)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
47
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
48 cdsBlind <- estimateDispersions( cds, method="blind" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
49
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
50 if (length(conds) != 2) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
51 cds <- estimateDispersions( cds )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
52 norep = FALSE
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
53 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
54
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
55 if (length(conds) == 2) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
56 cds <- estimateDispersions( cds, method="blind", sharingMode="fit-only" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
57 norep = TRUE
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
58 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
59
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
60 plotDispEsts <- function( cds )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
61 {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
62 plot(
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
63 rowMeans( counts( cds, normalized=TRUE ) ),
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
64 fitInfo(cds)$perGeneDispEsts,
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
65 pch = '.', log="xy" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
66 xg <- 10^seq( -.5, 5, length.out=300 )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
67 lines( xg, fitInfo(cds)$dispFun( xg ), col="red" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
68 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
69
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
70 temp.disp.est.plot = file.path( temp.files.path, "DispersionEstimatePlot.png" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
71 png( temp.disp.est.plot, width=500, height=500 )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
72 plotDispEsts( cds )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
73 dev.off()
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
74
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
75 res <- nbinomTest(cds, comparison[1], comparison[2])
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
76
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
77 write.table(res[order(res$padj), ], file=outputfile, quote=FALSE, row.names=FALSE, sep="\t")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
78
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
79
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
80 plotDE <- function( res )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
81 plot(
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
82 res$baseMean,
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
83 res$log2FoldChange,
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
84 log="x", pch=20, cex=.3,
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
85 col = ifelse( res$padj < .1, "red", "black" ) )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
86
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
87 temp.de.plot = file.path( temp.files.path, "DiffExpPlot.png")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
88 png( temp.de.plot, width=500, height=500 )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
89 plotDE( res )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
90 dev.off()
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
91
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
92
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
93 temp.pval.plot = file.path( temp.files.path, "PvalHist.png")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
94 png( temp.pval.plot, width=500, height=500 )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
95 hist(res$pval, breaks=100, col="skyblue", border="slateblue", main="")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
96 dev.off()
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
97
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
98 vsd <- getVarianceStabilizedData( cdsBlind )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
99
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
100 temp.heatmap.plot = file.path( temp.files.path, "heatmap.png" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
101 png( temp.heatmap.plot, width=700, height=700 )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
102 select <- order(res$pval)[1:100]
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
103 colors <- colorRampPalette(c("white","darkblue"))(100)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
104 heatmap( vsd[select,], col = colors, scale = "none", Rowv=NULL, main="", margins=c(20,20))
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
105 dev.off()
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
106
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
107
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
108 mod_lfc <- (rowMeans( vsd[, conditions(cds)==comparison[2], drop=FALSE] ) - rowMeans( vsd[, conditions(cds)==comparison[1], drop=FALSE] ))
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
109 lfc <- res$log2FoldChange
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
110 finite <- is.finite(lfc)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
111 table(as.character(lfc[!finite]), useNA="always")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
112 largeNumber <- 10
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
113 lfc <- ifelse(finite, lfc, sign(lfc) * largeNumber)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
114
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
115 temp.modlfc.plot = file.path( temp.files.path, "modlfc.png" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
116 png( temp.modlfc.plot, width=500, height=500 )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
117 plot( lfc, mod_lfc, pch=20, cex=.3, col = ifelse( finite, "#80808040", "red" ) )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
118 abline( a=0, b=1, col="#40404040" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
119 dev.off()
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
120
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
121 temp.sampclust.plot = file.path( temp.files.path, "sampclust.png" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
122 png( temp.sampclust.plot, width=700, height=700 )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
123 dists <- dist( t( vsd ) )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
124 heatmap( as.matrix( dists ),
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
125 symm=TRUE, scale="none", margins=c(20,20),
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
126 col = colorRampPalette(c("darkblue","white"))(100),
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
127 labRow = paste( pData(cdsBlind)$condition, pData(cdsBlind)$type ) )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
128 dev.off()
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
129
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
130
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
131 library(aroma.light)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
132 library(lattice)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
133
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
134 mdsPlot <- function(x, conds=NULL, cex=1, ...) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
135 d <- dist(x)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
136
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
137 mds.fit <- cmdscale(d, eig=TRUE, k=2)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
138
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
139 mds.d <- data.frame(x1=mds.fit$points[, 1],
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
140 x2=mds.fit$points[, 2],
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
141 labels=rownames(x))
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
142 if (!is.null(conds))
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
143 mds.d$treatment <- as.factor(conds)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
144
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
145 if (!is.null(conds)) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
146 p <- xyplot(x2 ~ x1, group=treatment, data=mds.d, panel=function(x, y, ..., groups, subscripts) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
147 panel.text(x, y, mds.d$labels[subscripts], cex=cex, col=trellis.par.get()$superpose.line$col[groups])
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
148 }, ...)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
149 } else {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
150 p <- xyplot(x2 ~ x1, data=mds.d, panel=function(x, y, ..., groups, subscripts) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
151 panel.text(x, y, mds.d$labels[subscripts], cex=cex)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
152 }, ...)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
153 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
154 return(p)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
155 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
156
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
157 if (!norep) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
158 dn = normalizeQuantileRank(as.matrix(d))
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
159 p = mdsPlot(t(log10(dn+1)), conds=conds, xlab="dimension 1", ylab="dimension 2", main="")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
160
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
161 temp.mds.plot = file.path( temp.files.path, "mds.png" )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
162 png( temp.mds.plot, width=500, height=500 )
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
163 print(p)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
164 dev.off()
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
165 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
166
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
167 file.conn = file(diag.html, open="w")
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
168 writeLines( c("<html><body bgcolor='lightgray'>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
169 writeLines( c("<h2><u>Diagnostic Plots for ", counts.name, "</u></h2>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
170 writeLines( c("<h3>Dispersion Estimates</h3>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
171 writeLines( c("<img src='DispersionEstimatePlot.png'><br/><br/>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
172 writeLines( c("<h3>Differential Expression - Base Mean vs. Log2 Fold Change</h3>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
173 writeLines( c("<img src='DiffExpPlot.png'><br/><br/>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
174 writeLines( c("<h3>P-value histogram</h3>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
175 writeLines( c("<img src='PvalHist.png'><br/><br/>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
176 writeLines( c("<h3>Top 100 Genes/Transcripts by P-value</h3>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
177 writeLines( c("<img src='heatmap.png'><br/><br/>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
178 writeLines( c("<h3>Moderated LFC vs. LFC</h3>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
179 writeLines( c("<img src='modlfc.png'><br/><br/>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
180 writeLines( c("<h3>VST Sample Clustering</h3>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
181 writeLines( c("<img src='sampclust.png'><br/><br/>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
182 writeLines( c("<h3>MDS Plot</h3>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
183
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
184 if (!norep) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
185 writeLines( c("<img src='mds.png'><br/><br/>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
186 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
187
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
188 if (norep) {
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
189 writeLines( c("<h4>MDS plot not produced for unreplicated data</h4>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
190 }
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
191
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
192 writeLines( c("</body></html>"), file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
193 close(file.conn)
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
194
a49aff09553e Uploaded
nikhil-joshi
parents:
diff changeset
195 sink(NULL)