<%@meta title="RMA reproducibility: doRMA() versus affyPLM::fitPLM()"%>
<%@meta author="Henrik Bengtsson, Mark Robinson"%>

# <%@meta name="title"%>

by <%@meta name="author"%>

This test verifies that `aroma.affymetrix::doRMA()` of aroma.affymetrix can reproduce the RMA chip-effect estimates as estimated by `affyPLM::fitPLM()`.

<%
library("R.utils")
use("R.devices")
verbose <- Arguments$getVerbose(-8, timestamp=TRUE)
options(digits=4L)

# Avoid being masked by oligo::fitPLM() and affy::plotDensity()
fitPLM <- affyPLM::fitPLM
plotDensity <- aroma.light::plotDensity
%>


## Setup
```r
<%=withCapture({
use("aroma.affymetrix")
use("affyPLM")
use("hgu133plus2cdf")
})%>
```

## Data
```r
<%=withCapture({
dataset <- "GSE9890"
chiptype <- "HG-U133_Plus_2"
cdf <- AffymetrixCdfFile$byChipType(chiptype)
cdf

csR <- AffymetrixCelSet$byName(dataset, cdf=cdf)
csR
})%>
```

## RMA by aroma.affymetrix
```r
<%=withCapture({
ces <- doRMA(csR, verbose=verbose)
ces
})%>
```

Retrieving chip-effect estimates (on the log2 scale)
```r
<%=withCapture({
theta <- log2(extractTheta(ces, drop=TRUE))
rownames(theta) <- getUnitNames(cdf)
head(theta)
})%>
```

## RMA by affyPLM
```r
<%=withCapture({
ab <- ReadAffy(filenames=getPathnames(csR))
ab
fit <- fitPLM(ab, verbos=9)
theta0 <- coefs(fit)
colnames(theta0) <- gsub("[.]cel", "", colnames(theta0), ignore.case=TRUE)

# Reordering genes
o <- match(rownames(theta), rownames(theta0))
theta0 <- theta0[o,]

head(theta0)
})%>
```

## Comparisons

Overall correlation of chip effects per each sample:
```r
<%=withCapture({
rho <- diag(cor(theta, theta0))
rho
range(rho)
})%>
```

Chip-effect differences:
```r
<%=withCapture({
d <- (theta - theta0)
summary(d)
})%>
```

![](<%=toPNG(dataset, tags=c(chiptype, "doRMA_vs_affyPLM", "pairwise"), aspectRatio=5/2, {
  par(mar=c(5,5,4,2)+0.1, cex.main=2, cex.lab=2, cex.axis=1.5)
  layout(matrix(seq_along(csR), ncol=2L, byrow=TRUE))

  xlab <- expression(log[2](theta[affyPLM]))
  ylab <- expression(log[2](theta[aroma.affymetrix]))
  for (kk in seq_len(ncol(theta))) {
    main <- colnames(theta)[kk]
    plot(theta0[,kk], theta[,kk], pch=".", xlab=xlab, ylab=ylab, main=main)
    abline(0,1, col="blue")
    stext(side=3, pos=0, line=-1.1, cex=1.2, substitute(rho==x, list(x=rho[kk])))
  }
})%>)

_Figure: Pairwise chip-effect estimates (theta) from `aroma.affymetrix::doRMA()` and `affyPLM::fitPlm()` across <%=length(csR)%> <%=chiptype%> arrays._



![](<%=toPNG(dataset, tags=c(chiptype, "doRMA_vs_affyPLM", "density_of_differences"), aspectRatio=2/3, {
  par(mar=c(5,5,4,2)+0.1, cex.main=2, cex.lab=2, cex.axis=1.5)
  xlab <- expression(log[2](theta[aroma.affymetrix]/theta[affyPLM]))
  plotDensity(d, xlab=xlab)
})%>)

_Figure: Empirical density of differences in chip-effect estimates (theta) between `aroma.affymetrix::doRMA()` and `affyPLM::fitPlm()` across <%=length(csR)%> <%=chiptype%> arrays._


<%----
<%
# (b) Assert correlations
print(rho)
print(range(rho))
stopifnot(all(rho > 0.99995))

# (c) Assert differences
stopifnot(mean(as.vector(d^2)) < 1e-3)
stopifnot(sd(as.vector(d^2)) < 1e-3)
stopifnot(quantile(abs(d), 0.99) < 0.05)
stopifnot(max(abs(d)) < 0.100)

verbose && print(verbose, sessionInfo())
%>

----%>

## Appendix
```r
<%=withCapture({
sessionInfo()
})%>
```

--------------------------------------------------------------------
Last generated: <%=format(Sys.time(), "%Y-%m-%d %H:%M:%S UTC%z")%>.
Powered by [RSP](http://cran.r-project.org/package=R.rsp).
