13.7.12

Creating custom CDF for Affy chips in R / Bioconductor






What?

For those who don't know, CDF files are chip definition format files that define which probes belong to which probesets, and are necessary to use any of the standard summarization methods such as RMA, and others.

Why?

Because we can, and because custom definitions have been shown to be quite useful. See the information over at Brainarray.

Why not somewhere else?

A lot of times other people create custom CDF files based on their own criteria, and make it subsequently available for others to use (see the Brainarray for an example of what some are doing, as well as [PlandbAffy][linkplandb])

You have a really nifty idea for a way to reorganize the probesets on an Affymetrix chip to perform a custom analysis, but you don't want to go to the trouble of actually creating the CDF files and Bioconductor packages normally required to do the analysis, and yet you want to test and develop your analysis method.

How?

It turns out you are in luck. At least for AffyBatch objects in Bioconductor (created by calling ReadAffy), the CDF information is stored as an attached environment that can be easily hacked and modified to your hearts content. Environments in R are quite important and useful, and I wouldn't have come up with this if I hadn't been working in R for the past couple of years, but figured someone else might benefit from this knowledge.

The environment

In R, one can access an environment like so:

get("objName", envName)  # get the value of object in the environment
ls(envName)

What is also very cool, is that one can extract the objects in an environment to a list, and also create their own environment from a list using list2env. Using this methodology, we can create our own definition of probesets that can be used by standard Bioconductor routines to summarize the probes into probesets.

A couple of disclaimers:

  • I have only tried this on 3' expression arrays
  • There might be a better way to do this, but I couldn't find it (let me know in the comments)

Example

require(affy)
require(estrogen)
require(hgu95av2cdf)

datadir = system.file("extdata", package = "estrogen")

pd = read.AnnotatedDataFrame(file.path(datadir, "estrogen.txt"), 
    header = TRUE, sep = "", row.names = 1)
pData(pd)
##              estrogen time.h
## low10-1.cel    absent     10
## low10-2.cel    absent     10
## high10-1.cel  present     10
## high10-2.cel  present     10
## low48-1.cel    absent     48
## low48-2.cel    absent     48
## high48-1.cel  present     48
## high48-2.cel  present     48

celDat = ReadAffy(filenames = rownames(pData(pd)), phenoData = pd, 
    verbose = TRUE, celfile.path = datadir)
## 1 reading J:/R150_libraries/estrogen/extdata/low10-1.cel ...instantiating an AffyBatch (intensity a 409600x8 matrix)...done.
## Reading in : J:/R150_libraries/estrogen/extdata/low10-1.cel
## Reading in : J:/R150_libraries/estrogen/extdata/low10-2.cel
## Reading in : J:/R150_libraries/estrogen/extdata/high10-1.cel
## Reading in : J:/R150_libraries/estrogen/extdata/high10-2.cel
## Reading in : J:/R150_libraries/estrogen/extdata/low48-1.cel
## Reading in : J:/R150_libraries/estrogen/extdata/low48-2.cel
## Reading in : J:/R150_libraries/estrogen/extdata/high48-1.cel
## Reading in : J:/R150_libraries/estrogen/extdata/high48-2.cel

This loads up the data, reads in the raw data, and gets it ready for us to use. Now, lets see what is in the actual CDF environment.

topProbes <- head(ls(hgu95av2cdf))  # get a list of probesets
topProbes
## [1] "100_g_at"  "1000_at"   "1001_at"   "1002_f_at" "1003_s_at" "1004_at"  

exSet <- get(topProbes[1], hgu95av2cdf)
exSet
##           pm     mm
##  [1,] 175218 175858
##  [2,] 356689 357329
##  [3,] 227696 228336
##  [4,] 237919 238559
##  [5,] 275173 275813
##  [6,] 203444 204084
##  [7,] 357984 358624
##  [8,] 368524 369164
##  [9,] 285352 285992
## [10,] 304510 305150
## [11,] 159937 160577
## [12,] 223929 224569
## [13,] 282764 283404
## [14,] 270003 270643
## [15,] 303343 303983
## [16,] 389048 389688

We can see here that the first probe set 100_g_at has 16 perfect-match and mis-match probes in associated with it.

Lets summarize the original data using RMA.

rma1 <- exprs(rma(celDat))
## Background correcting
## Normalizing
## Calculating Expression

head(rma1)
##           low10-1.cel low10-2.cel high10-1.cel high10-2.cel low48-1.cel
## 100_g_at        9.643       9.741        9.537        9.354       9.592
## 1000_at        10.398      10.254       10.004        9.904      10.375
## 1001_at         5.718       5.881        5.860        5.954       5.961
## 1002_f_at       5.513       5.802        5.571        5.608       5.390
## 1003_s_at       7.784       8.008        8.038        7.835       7.926
## 1004_at         7.289       7.604        7.489        7.772       7.522
##           low48-2.cel high48-1.cel high48-2.cel
## 100_g_at        9.571        9.476        9.531
## 1000_at        10.034       10.345        9.863
## 1001_at         6.021        5.981        6.285
## 1002_f_at       5.495        5.508        5.630
## 1003_s_at       8.139        7.995        8.233
## 1004_at         7.600        7.456        7.675

Now lets get the data as a list, and then create a new environment to be used for summarization.

allSets <- ls(hgu95av2cdf)
allSetDat <- mget(allSets, hgu95av2cdf)

allSetDat[1]
## $`100_g_at`
##           pm     mm
##  [1,] 175218 175858
##  [2,] 356689 357329
##  [3,] 227696 228336
##  [4,] 237919 238559
##  [5,] 275173 275813
##  [6,] 203444 204084
##  [7,] 357984 358624
##  [8,] 368524 369164
##  [9,] 285352 285992
## [10,] 304510 305150
## [11,] 159937 160577
## [12,] 223929 224569
## [13,] 282764 283404
## [14,] 270003 270643
## [15,] 303343 303983
## [16,] 389048 389688
## 

hgu2 <- list2env(allSetDat)
celDat@cdfName <- "hgu2"

rma2 <- exprs(rma(celDat))
## Background correcting
## Normalizing
## Calculating Expression
head(rma2)
##           low10-1.cel low10-2.cel high10-1.cel high10-2.cel low48-1.cel
## 100_g_at        9.643       9.741        9.537        9.354       9.592
## 1000_at        10.398      10.254       10.004        9.904      10.375
## 1001_at         5.718       5.881        5.860        5.954       5.961
## 1002_f_at       5.513       5.802        5.571        5.608       5.390
## 1003_s_at       7.784       8.008        8.038        7.835       7.926
## 1004_at         7.289       7.604        7.489        7.772       7.522
##           low48-2.cel high48-1.cel high48-2.cel
## 100_g_at        9.571        9.476        9.531
## 1000_at        10.034       10.345        9.863
## 1001_at         6.021        5.981        6.285
## 1002_f_at       5.495        5.508        5.630
## 1003_s_at       8.139        7.995        8.233
## 1004_at         7.600        7.456        7.675

What about removing the MM columns? RMA only uses the PM, so it should still work.

allSetDat <- lapply(allSetDat, function(x) {
    x[, 1, drop = F]
})

allSetDat[1]
## $`100_g_at`
##           pm
##  [1,] 175218
##  [2,] 356689
##  [3,] 227696
##  [4,] 237919
##  [5,] 275173
##  [6,] 203444
##  [7,] 357984
##  [8,] 368524
##  [9,] 285352
## [10,] 304510
## [11,] 159937
## [12,] 223929
## [13,] 282764
## [14,] 270003
## [15,] 303343
## [16,] 389048
## 

hgu3 <- list2env(allSetDat)
celDat@cdfName <- "hgu3"
rma3 <- exprs(rma(celDat))
## Background correcting
## Normalizing
## Calculating Expression
head(rma3)
##           low10-1.cel low10-2.cel high10-1.cel high10-2.cel low48-1.cel
## 100_g_at        9.643       9.741        9.537        9.354       9.592
## 1000_at        10.398      10.254       10.004        9.904      10.375
## 1001_at         5.718       5.881        5.860        5.954       5.961
## 1002_f_at       5.513       5.802        5.571        5.608       5.390
## 1003_s_at       7.784       8.008        8.038        7.835       7.926
## 1004_at         7.289       7.604        7.489        7.772       7.522
##           low48-2.cel high48-1.cel high48-2.cel
## 100_g_at        9.571        9.476        9.531
## 1000_at        10.034       10.345        9.863
## 1001_at         6.021        5.981        6.285
## 1002_f_at       5.495        5.508        5.630
## 1003_s_at       8.139        7.995        8.233
## 1004_at         7.600        7.456        7.675

What if we only want to use the first 5 probesets?

allSetDat <- allSetDat[1:5]
hgu4 <- list2env(allSetDat)
celDat@cdfName <- "hgu4"
celDat
## AffyBatch object
## size of arrays=640x640 features (22 kb)
## cdf=hgu4 (5 affyids)
## number of samples=8
## number of genes=5
## annotation=hgu95av2
## notes=
rma4 <- exprs(rma(celDat))
## Background correcting
## Normalizing
## Calculating Expression
rma4
##           low10-1.cel low10-2.cel high10-1.cel high10-2.cel low48-1.cel
## 100_g_at        9.463       9.555        9.449        9.402       9.448
## 1000_at        10.183      10.010       10.010        9.970      10.102
## 1001_at         5.944       6.005        5.944        6.090       6.237
## 1002_f_at       5.787       5.846        5.817        5.815       5.763
## 1003_s_at       7.751       7.769        7.913        7.864       7.861
##           low48-2.cel high48-1.cel high48-2.cel
## 100_g_at        9.458        9.401        9.431
## 1000_at        10.010       10.197        9.890
## 1001_at         6.148        6.189        6.207
## 1002_f_at       5.763        5.741        5.755
## 1003_s_at       7.918        7.863        7.929
dim(rma4)
## [1] 5 8

Custom CDF

To generate our custom CDF, we are going to set our own names, and take random probes from all of the probes on the chip. The actual criteria of which probes should be together can be made using any method the author chooses.

maxIndx <- 640 * 640

customCDF <- lapply(seq(1, 100), function(x) {
    tmp <- matrix(sample(maxIndx, 20), nrow = 20, ncol = 1)
    colnames(tmp) <- "pm"
    return(tmp)
})

names(customCDF) <- seq(1, 100)

hgu5 <- list2env(customCDF)
celDat@cdfName <- "hgu5"
rma5 <- exprs(rma(celDat))
## Background correcting
## Normalizing
## Calculating Expression

head(rma5)
##     low10-1.cel low10-2.cel high10-1.cel high10-2.cel low48-1.cel
## 1         6.938       6.851        6.831        6.742       6.894
## 10        6.663       6.610        6.615        6.612       6.610
## 100       5.256       5.499        5.420        5.205       5.352
## 11        7.529       7.807        7.552        7.539       7.728
## 12        6.163       6.216        6.230        6.091       6.071
## 13        6.311       6.404        6.264        6.290       6.257
##     low48-2.cel high48-1.cel high48-2.cel
## 1         6.636        6.862        6.576
## 10        6.572        6.685        6.481
## 100       5.256        5.281        5.258
## 11        7.792        7.682        7.590
## 12        6.058        6.204        6.159
## 13        6.249        6.275        6.313

I hope this information is useful to someone else. I know it made my life a lot easier.

sessionInfo()
## R version 2.15.0 (2012-03-30)
## Platform: x86_64-pc-mingw32/x64 (64-bit)
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] knitr_0.6.3          hgu95av2cdf_2.10.0   estrogen_1.8.8      
## [4] BiocInstaller_1.4.7  rat2302cdf_2.10.0    AnnotationDbi_1.18.1
## [7] affy_1.34.0          Biobase_2.16.0       BiocGenerics_0.2.0  
## 
## loaded via a namespace (and not attached):
##  [1] affyio_1.24.0         DBI_0.2-5             digest_0.5.2         
##  [4] evaluate_0.4.2        formatR_0.5           IRanges_1.14.4       
##  [7] markdown_0.5.2        parser_0.0-16         plyr_1.7.1           
## [10] preprocessCore_1.18.0 Rcpp_0.9.10           RSQLite_0.11.1       
## [13] stats4_2.15.0         stringr_0.6           tools_2.15.0         
## [16] zlibbioc_1.2.0       

Edit: added session information.

Source of the code that generated the output is on Github