Difference between revisions of "CXE1graphics.R"
m (dummy edit) |
m |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | library(lattice) | + | # {{R}} |
+ | library(lattice) | ||
library(grid) | library(grid) | ||
− | # | + | # 1) ---------------------------------- Setup ---------------------------------- # |
− | # | + | # Read in esterase realtime RT-PCR data |
+ | |||
+ | showDiagnostics <- F | ||
+ | draft <- F | ||
folder <- "/Volumes/HD2/Clinton/Data" | folder <- "/Volumes/HD2/Clinton/Data" | ||
filename <- "esteraseRTcompare4runs2.txt" | filename <- "esteraseRTcompare4runs2.txt" | ||
+ | |||
dset <- read.table(file.path(folder,filename), header=T, sep="\t", as.is=T) | dset <- read.table(file.path(folder,filename), header=T, sep="\t", as.is=T) | ||
dset$Time <- dset$Time / 24 | dset$Time <- dset$Time / 24 | ||
− | # | + | # Creating factors |
dset$Treatment <- factor(dset$Treatment, levels=c("Sucrose","Esterase"), | dset$Treatment <- factor(dset$Treatment, levels=c("Sucrose","Esterase"), | ||
labels=c("Sucrose","CXE1 dsRNA")) | labels=c("Sucrose","CXE1 dsRNA")) | ||
+ | |||
dset$Run <- factor(dset$Run, levels=unique(dset$Run)[c(3,2,4,1)], | dset$Run <- factor(dset$Run, levels=unique(dset$Run)[c(3,2,4,1)], | ||
labels=c("Expt 4","Expt 3","Expt 2","Expt 1")) | labels=c("Expt 4","Expt 3","Expt 2","Expt 1")) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
# Normalization of realtime RT-PCR | # Normalization of realtime RT-PCR | ||
dset$RE <- dset$Quantity/dset$NormFactor | dset$RE <- dset$Quantity/dset$NormFactor | ||
− | # | + | # Diagnostics |
+ | if( showDiagnostics) { | ||
+ | dim(dset) | ||
+ | summary(dset) | ||
+ | table(dset$Time, dset$Treatment) | ||
+ | table(dset$Run, dset$Time, dset$Treatment) | ||
− | + | par(mfrow=c(1,3)) | |
− | + | hist(dset$RE) | |
− | + | plot(density(dset$RE)) | |
− | + | plot(sort(dset$RE), pch=".") | |
− | + | dev.off() | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | par(mfrow=c(1,3)) | ||
− | hist(dset$RE) | ||
− | plot(density(dset$RE)) | ||
− | plot(sort(dset$RE), pch=".") | ||
− | dev.off( | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | # Draft plots | ||
if( draft ) { | if( draft ) { | ||
histogram(~ Quantity | Treatment*Time, data=dset) | histogram(~ Quantity | Treatment*Time, data=dset) | ||
Line 57: | Line 45: | ||
} | } | ||
− | + | # RE versus Time in the Run:Time:Treatment stratum of variation | |
− | # | ||
if( draft ) { | if( draft ) { | ||
− | + | xyplot(Quantity ~ Time | Treatment * Run, data=dset, aspect=1, | |
− | xyplot(Quantity ~ Time | Treatment * Run, data=dset, aspect=1, | + | panel=function(x,y){ |
− | + | panel.loess(x,y, span=1) | |
− | + | panel.xyplot(x,y) | |
− | + | }) | |
− | |||
− | |||
xyplot(RE ~ Time | Treatment * Run, data=dset, aspect=1, | xyplot(RE ~ Time | Treatment * Run, data=dset, aspect=1, | ||
panel=function(x,y){ | panel=function(x,y){ | ||
Line 77: | Line 62: | ||
} | } | ||
+ | # 2) -------------------------------- Figures --------------------------------- # | ||
xlab <- "Time post feeding (days)" | xlab <- "Time post feeding (days)" | ||
figx <- 0.2 | figx <- 0.2 | ||
figy <- 0.97 | figy <- 0.97 | ||
+ | myformula <- RE ~ Time | Treatment*Run | ||
− | plot1 <- xyplot( | + | plot1 <- xyplot(myformula, data=dset, aspect=1, col=1, groups = dset$Run, between=list(x=0.2,y=0.2), |
− | ylab="Relative Expression", xlab = | + | ylab="CXE1 Relative Expression", xlab = xlab, |
page = function(n) | page = function(n) | ||
grid.text(paste("A"), | grid.text(paste("A"), | ||
Line 88: | Line 75: | ||
default.units = "npc", | default.units = "npc", | ||
just = c("right", "bottom")), | just = c("right", "bottom")), | ||
− | panel=function(x,y,...){ | + | panel=function(x,y,subscripts,groups,...){ |
panel.loess(x,y, span=1.4) | panel.loess(x,y, span=1.4) | ||
panel.xyplot(x,y,...) | panel.xyplot(x,y,...) | ||
− | panel.grid( | + | panel.grid(col=1, lty=3, lwd=0.3) |
}) | }) | ||
Line 98: | Line 85: | ||
} | } | ||
− | # | + | # 3) ------------------------- Creating ratio data ---------------------------- # |
− | + | # Taking means over Time:Run:Treatment | |
− | |||
− | |||
− | |||
− | # | ||
− | |||
− | # | ||
colsToRemove <- c("Run","Time","Treatment","Sample.Name", "Detector.Name", | colsToRemove <- c("Run","Time","Treatment","Sample.Name", "Detector.Name", | ||
"Reporter","Task","Well") | "Reporter","Task","Well") | ||
− | dsetMeans <- aggregate(dset[,-match(colsToRemove, names(dset))], list(Time=dset$Time, | + | dsetMeans <- aggregate(dset[,-match(colsToRemove, names(dset))], |
− | + | list(Time=dset$Time,Run=dset$Run,Treatment=dset$Treatment), | |
− | |||
mean) | mean) | ||
Line 116: | Line 96: | ||
# Bug in here factor reversed after aggregate... | # Bug in here factor reversed after aggregate... | ||
tmp$Run <- factor(tmp$Run, levels = unique(tmp$Run)) | tmp$Run <- factor(tmp$Run, levels = unique(tmp$Run)) | ||
− | |||
tmp$ratio <- dsetMeans$RE[21:40] / dsetMeans$RE[1:20] | tmp$ratio <- dsetMeans$RE[21:40] / dsetMeans$RE[1:20] | ||
+ | tmp$Time <- as.numeric(as.vector(tmp$Time)) | ||
− | # | + | # Check... |
− | run <- " | + | run <- "Expt 1" |
times <- 7 | times <- 7 | ||
mean(dset$RE[dset$Time==times & dset$Run==run & dset$Treatment=="CXE1 dsRNA"]) / | mean(dset$RE[dset$Time==times & dset$Run==run & dset$Treatment=="CXE1 dsRNA"]) / | ||
Line 127: | Line 107: | ||
# =============================================================================== | # =============================================================================== | ||
− | + | myformula <- ratio ~ Time |Run | |
+ | plot2 <- xyplot(myformula , data=tmp, layout=c(1,4), pch=4, | ||
aspect=1, groups = 1:3, col=1, | aspect=1, groups = 1:3, col=1, | ||
between=list(x=0.2,y=0.2), | between=list(x=0.2,y=0.2), | ||
Line 134: | Line 115: | ||
page = function(n) grid.text(paste("B"), x = figx+0.15, y = figy+0.01, gp=gpar(fontsize=20), | page = function(n) grid.text(paste("B"), x = figx+0.15, y = figy+0.01, gp=gpar(fontsize=20), | ||
default.units = "npc", just = c("right", "bottom")), | default.units = "npc", just = c("right", "bottom")), | ||
− | panel=function(x,y, ...){ panel.loess(x,y, span=1. | + | panel=function(x,y, ...){ panel.loess(x,y, span=1.39) |
panel.xyplot(x,y,...) | panel.xyplot(x,y,...) | ||
panel.abline(h=1, lty=2) | panel.abline(h=1, lty=2) | ||
panel.grid(..., lty=3, lwd=0.3) }) | panel.grid(..., lty=3, lwd=0.3) }) | ||
− | + | print(plot2) | |
if( draft ) { | if( draft ) { | ||
print(plot2) | print(plot2) | ||
} | } | ||
− | |||
dev.off() | dev.off() | ||
− | |||
− | # | + | # ------------------------- Final figure for paper ---------------------------- # |
draft <- F | draft <- F | ||
Line 153: | Line 132: | ||
printType <- "postscript" | printType <- "postscript" | ||
dpi <- 1024 / ((4/5)*17) | dpi <- 1024 / ((4/5)*17) | ||
− | if( | + | |
+ | if( draft ) { | ||
trellis.par.set("background", list(col=0)) | trellis.par.set("background", list(col=0)) | ||
trellis.par.set("strip.background", list(alpha=0, col="gray90")) | trellis.par.set("strip.background", list(alpha=0, col="gray90")) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} else { | } else { | ||
if(printType=="postscript") { | if(printType=="postscript") { | ||
Line 168: | Line 142: | ||
trellis.device ("postscript", color=T, file=file.path(directory, "CXE1.eps"),width=sizemm, height=sizemm, horizontal=F) #, font="serif") | trellis.device ("postscript", color=T, file=file.path(directory, "CXE1.eps"),width=sizemm, height=sizemm, horizontal=F) #, font="serif") | ||
trellis.par.set("strip.background", list(alpha=0, col=c("gray80"))) | trellis.par.set("strip.background", list(alpha=0, col=c("gray80"))) | ||
+ | |||
+ | tweaky <- 0.85 # for postscript | ||
+ | print(plot1, split=c(1,1,2,1), position=c(0,0,1,1), more=T) | ||
+ | print(plot2, split=c(2,1,2,1), position=c(-0.15,0+(1-tweaky),1,1*tweaky), more=F) | ||
+ | dev.off() | ||
} else { | } else { | ||
− | png(file.path(directory, "CXE1. | + | png(file.path(directory, "CXE1.png"), width=8*dpi,height=8*dpi) |
+ | tweaky <- 0.985 # for png | ||
+ | print(plot1, split=c(1,1,2,1), position=c(0,0,1,1), more=T) | ||
+ | print(plot2, split=c(2,1,2,1), position=c(-0.15,0+(1-tweaky),1,1*tweaky), more=F) | ||
+ | dev.off() | ||
} | } | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
# Put graphs on one page for RE and ratio information | # Put graphs on one page for RE and ratio information | ||
Line 210: | Line 186: | ||
trellis.device ("postscript", color=T, file = file.path(directory, "Esteraseinteraction.eps"), width=169, height=150, horizontal=F) | trellis.device ("postscript", color=T, file = file.path(directory, "Esteraseinteraction.eps"), width=169, height=150, horizontal=F) | ||
− | plot3 <- xyplot(RE ~ Time, aspect=1, data=tmp, pch=as.numeric(tmp$Treatment), | + | # CXElsd is from the ANOVA function! |
− | + | plot3 <- xyplot(RE ~ Time, aspect=1, data=tmp, pch=as.numeric(tmp$Treatment), ylab="Mean Relative Expression", xlab=xlab, | |
− | + | scales=list(x=list(at=0:7, labels=0:7)), key = mykey, | |
− | scales=list(x=list(at=0:7, labels=0:7)), | ||
panel=function(x,y, subscripts) { | panel=function(x,y, subscripts) { | ||
panel.superpose(x,y, subscripts, groups=as.numeric(tmp$Treatment), pch=as.numeric(tmp$Treatment), col=1) | panel.superpose(x,y, subscripts, groups=as.numeric(tmp$Treatment), pch=as.numeric(tmp$Treatment), col=1) | ||
Line 221: | Line 196: | ||
panel.lines(x[tmp$Treatment==i],y[tmp$Treatment==i], col="black") | panel.lines(x[tmp$Treatment==i],y[tmp$Treatment==i], col="black") | ||
} | } | ||
− | } | + | }) |
− | |||
Latest revision as of 00:35, 6 June 2007
Code snipits and programs written in R, S or S-PLUS library(lattice) library(grid)
- 1) ---------------------------------- Setup ---------------------------------- #
- Read in esterase realtime RT-PCR data
showDiagnostics <- F draft <- F
folder <- "/Volumes/HD2/Clinton/Data" filename <- "esteraseRTcompare4runs2.txt"
dset <- read.table(file.path(folder,filename), header=T, sep="\t", as.is=T) dset$Time <- dset$Time / 24
- Creating factors
dset$Treatment <- factor(dset$Treatment, levels=c("Sucrose","Esterase"),
labels=c("Sucrose","CXE1 dsRNA"))
dset$Run <- factor(dset$Run, levels=unique(dset$Run)[c(3,2,4,1)],
labels=c("Expt 4","Expt 3","Expt 2","Expt 1"))
- Normalization of realtime RT-PCR
dset$RE <- dset$Quantity/dset$NormFactor
- Diagnostics
if( showDiagnostics) {
dim(dset) summary(dset) table(dset$Time, dset$Treatment) table(dset$Run, dset$Time, dset$Treatment)
par(mfrow=c(1,3)) hist(dset$RE) plot(density(dset$RE)) plot(sort(dset$RE), pch=".") dev.off()
}
- Draft plots
if( draft ) { histogram(~ Quantity | Treatment*Time, data=dset) histogram(~ RE | Treatment*Time, data=dset) }
- RE versus Time in the Run:Time:Treatment stratum of variation
if( draft ) {
xyplot(Quantity ~ Time | Treatment * Run, data=dset, aspect=1, panel=function(x,y){ panel.loess(x,y, span=1) panel.xyplot(x,y) })
xyplot(RE ~ Time | Treatment * Run, data=dset, aspect=1,
panel=function(x,y){ panel.loess(x,y, span=1) panel.xyplot(x,y) })
dev.off() dev.off() }
- 2) -------------------------------- Figures --------------------------------- #
xlab <- "Time post feeding (days)" figx <- 0.2 figy <- 0.97 myformula <- RE ~ Time | Treatment*Run
plot1 <- xyplot(myformula, data=dset, aspect=1, col=1, groups = dset$Run, between=list(x=0.2,y=0.2),
ylab="CXE1 Relative Expression", xlab = xlab, page = function(n) grid.text(paste("A"), x = figx, y = figy, gp=gpar(fontsize=20), default.units = "npc", just = c("right", "bottom")), panel=function(x,y,subscripts,groups,...){ panel.loess(x,y, span=1.4) panel.xyplot(x,y,...) panel.grid(col=1, lty=3, lwd=0.3) })
if( draft ) { print(plot1) }
- 3) ------------------------- Creating ratio data ---------------------------- #
- Taking means over Time:Run:Treatment
colsToRemove <- c("Run","Time","Treatment","Sample.Name", "Detector.Name",
"Reporter","Task","Well")
dsetMeans <- aggregate(dset[,-match(colsToRemove, names(dset))],
list(Time=dset$Time,Run=dset$Run,Treatment=dset$Treatment), mean)
tmp <- dsetMeans[1:20, 1:2]
- Bug in here factor reversed after aggregate...
tmp$Run <- factor(tmp$Run, levels = unique(tmp$Run)) tmp$ratio <- dsetMeans$RE[21:40] / dsetMeans$RE[1:20] tmp$Time <- as.numeric(as.vector(tmp$Time))
- Check...
run <- "Expt 1" times <- 7 mean(dset$RE[dset$Time==times & dset$Run==run & dset$Treatment=="CXE1 dsRNA"]) /
mean(dset$RE[dset$Time==times & dset$Run==run & dset$Treatment=="Sucrose"])
- ===============================================================================
myformula <- ratio ~ Time |Run plot2 <- xyplot(myformula , data=tmp, layout=c(1,4), pch=4,
aspect=1, groups = 1:3, col=1, between=list(x=0.2,y=0.2), ylab=expression(over("CXE1 dsRNA","Sucrose")), xlab=xlab, page = function(n) grid.text(paste("B"), x = figx+0.15, y = figy+0.01, gp=gpar(fontsize=20), default.units = "npc", just = c("right", "bottom")), panel=function(x,y, ...){ panel.loess(x,y, span=1.39) panel.xyplot(x,y,...) panel.abline(h=1, lty=2) panel.grid(..., lty=3, lwd=0.3) })
print(plot2) if( draft ) { print(plot2) } dev.off()
- ------------------------- Final figure for paper ---------------------------- #
draft <- F directory <- "/Volumes/HD2/Clinton/Graphs" printType <- "postscript" dpi <- 1024 / ((4/5)*17)
if( draft ) {
trellis.par.set("background", list(col=0)) trellis.par.set("strip.background", list(alpha=0, col="gray90"))
} else {
if(printType=="postscript") { sizemm <- 80 names(postscriptFonts()) # Available fonts trellis.device ("postscript", color=T, file=file.path(directory, "CXE1.eps"),width=sizemm, height=sizemm, horizontal=F) #, font="serif") trellis.par.set("strip.background", list(alpha=0, col=c("gray80")))
tweaky <- 0.85 # for postscript print(plot1, split=c(1,1,2,1), position=c(0,0,1,1), more=T) print(plot2, split=c(2,1,2,1), position=c(-0.15,0+(1-tweaky),1,1*tweaky), more=F) dev.off() } else { png(file.path(directory, "CXE1.png"), width=8*dpi,height=8*dpi) tweaky <- 0.985 # for png print(plot1, split=c(1,1,2,1), position=c(0,0,1,1), more=T) print(plot2, split=c(2,1,2,1), position=c(-0.15,0+(1-tweaky),1,1*tweaky), more=F) dev.off() }
}
- Put graphs on one page for RE and ratio information
- ==============================================================================
REmeans <- tapply(dset$RE, paste(dset$Time, dset$Treatment, sep=":"), mean, na.rm=T) times <- as.numeric(substr(names(REmeans), 1,1)) treatment <- substr(names(REmeans), 3, nchar(REmeans))
tmp <- data.frame(Time=times, Treatment=treatment, RE = REmeans)
lerrorbars <- function(x, y, se, yl = y - se, yu = y + se, eps, ...) {
if(any(is.na(yl)) | any(is.na(yu))) ltext(x[is.na(yl) | is.na(yu)], y[is.na(yl) | is.na(yu)], "NA", adj = 1) lsegments(x, yl, x, yu, ...) lsegments(x - eps, yl, x + eps, yl, ...) lsegments(x - eps, yu, x + eps, yu, ...)
}
trellis.device("quartz") source("CXE1Anovas.R")
mykey <- simpleKey(text=levels(tmp$Treatment), space="top", pch=1:2, columns=2) mykey$points <- list(alpha=1, cex=0.8, col="black", font=1, pch=1:2)
xyplot(RE ~ Time, data=dset, col=as.numeric(dset$Treatment), key = simpleKey(c("foo", "fodda"), points = F))
trellis.device ("postscript", color=T, file = file.path(directory, "Esteraseinteraction.eps"), width=169, height=150, horizontal=F)
- CXElsd is from the ANOVA function!
plot3 <- xyplot(RE ~ Time, aspect=1, data=tmp, pch=as.numeric(tmp$Treatment), ylab="Mean Relative Expression", xlab=xlab,
scales=list(x=list(at=0:7, labels=0:7)), key = mykey, panel=function(x,y, subscripts) { panel.superpose(x,y, subscripts, groups=as.numeric(tmp$Treatment), pch=as.numeric(tmp$Treatment), col=1) ltext(5, 0.45, "99% lsd") lerrorbars(4, 0.45, CXElsd/2, eps = 0.1) # lsd comes from CXE1Anovas.R for( i in levels(tmp$Treatment)) { panel.lines(x[tmp$Treatment==i],y[tmp$Treatment==i], col="black") } })
print(plot3) dev.off()
- LOOKS LIKE REDUNDANT CODE HERE...
- simpleKey(text=levels(tmp$Treatment), space="top", pch=1:2, columns=2
trellis.device("postscript") a <- xyplot(log2(RE) ~ Time, aspect=1, data=dset, col=as.numeric(dset$Treatment), ylim=log2(c(0.01,1)), xlim=-1:8) print(a, more=T) b <- xyplot(log2(RE) ~ Time, aspect=1, data=tmp, pch=as.numeric(tmp$Treatment), col="orange", cex=4, ylim=log2(c(0.01,1)), xlim=-1:8,
ylab="Relative expression", xlab=xlab, scales=list(x=list(at=0:7, labels=0:7)))
print(b, more=T) dev.off()