<%@meta title="CRLMM reproducibility: CRLMM versus oligo::justCRLMMv2()"%>
<%@meta author="Henrik Bengtsson"%>

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

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

This test verifies that CRLMM in aroma.affymetrix can reproduce the GCRMA chip-effect estimates as estimated by `oligo::justCRLMMv2()`.

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

## Setup
```r
<%=withCapture({
use("aroma.affymetrix")
use("oligo")
use("pd.mapping50k.hind240")
})%>
```


## Data
```r
<%=withCapture({
dataset <- "HapMap,CEU,testset"
chiptype <- "Mapping50K_Hind240"
cdf <- AffymetrixCdfFile$byChipType(chiptype)
cdf

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

## CRLMM by aroma.affymetrix
```r
<%=withCapture({
ces <- justSNPRMA(csR, normalizeToHapmap=TRUE, returnESet=FALSE, verbose=verbose)
ces

crlmm <- CrlmmModel(ces, tags="*,oligo", recalibrate=TRUE)
crlmm

units <- fit(crlmm, ram="oligo", verbose=verbose)
units

callset <- getCallSet(crlmm)
callset

confset <- getConfidenceScoreSet(crlmm)
confset
})%>
```


## CRLMM by oligo

```r
<%=withCapture({
# Create temporary output directory for oligo
path <- file.path("oligoData", dataset, chiptype)
mkdirs(dirname(path))
if (!isDirectory(path)) {
  oligo:::justCRLMMv2(getPathnames(csR), tmpdir=path, recalibrate=TRUE, balance=1.5, verbose=as.logical(verbose))
}
# Files created by oligo
print(list.files(path))
})%>
```


## Comparisons

Compare genotype calls:
```r
<%=withCapture({
calls0 <- readSummaries("calls", path)

unitNames <- rownames(calls0)
units <- indexOf(cdf, names=unitNames)
calls <- extractGenotypes(callset, units=units, encoding="oligo")

count <- 0;
for (cc in 1:ncol(calls)) {
  idxs <- whichVector(calls[,cc] != calls0[,cc])
  count <- count + length(idxs)
  cat(sprintf("%s: ", getNames(callset)[cc]))
  if (length(idxs) > 0) {
    map <- c("AA", "AB", "BB")
    cat(paste(map[calls[idxs,cc]], map[calls0[idxs,cc]], sep="!="), sep=", ")
  }
  cat("\n")
}
cat(sprintf("Averages number of discrepances per array: %.1f\n", count/ncol(calls)))
errorRate <- count/length(calls)
cat(sprintf("Concordance rate: %.5f%%\n", 100*(1-errorRate)))
stopifnot(errorRate < 5e-4)
})%>
```

Compare confidence scores:
```r
<%=withCapture({
conf <- extractMatrix(confset, units=units)
conf0 <- readSummaries("conf", path)

res <- all.equal(conf, conf0, check.attributes=FALSE)
res

# Sanity check
stopifnot(all.equal(conf, conf0, check.attributes=FALSE, tolerance=1e-4))
})%>
```

Visual comparison of confidence scores:
 
![](<%=toPNG(getFullName(ces), tags=c(chiptype, "CRLMM_vs_oligo", "pairwise"), aspectRatio=3/2, { 
  layout(matrix(seq_along(ces), ncol=2L, byrow=TRUE))
  par(mar=c(3,2,1,1)+0.1)
  lim <- c(0,1)
  for (cc in 1:ncol(conf)) {
    plot(NA, xlim=lim, ylim=lim)
    abline(a=0, b=1, col="#999999")
    points(conf[,cc], conf0[,cc], pch=".", cex=3)
    rho <- cor(conf[,cc], conf0[,cc])
    stext(side=3, pos=0, line=-1, sprintf("rho=%.4f", rho))
    stext(side=3, pos=0, getNames(confset)[cc])
    mcat(sprintf("Array #%d: Correlation: %.4f\n", cc, rho))
  }
})%>)

_Figure: Pairwise confidence scores between CRLMM and `oligo::justCRLMMv2()` across <%=length(csR)%> <%=chiptype%> arrays._
 

## 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).
