Hi Tim,

On 02/26/2013 02:01 PM, Tim Triche, Jr. wrote:
It seems like the best way to write an email around these parts is using R
code, so here goes.  I'm just wondering why a coercion that I define for
DataFrame -> data.frame doesn't automatically get used in practice.


library(Biobase)
library(GEOquery)
library(GenomicRanges)

## download a dataset which will become a SummarizedExperiment
##
gset <- getGEO("GSE41826") ## GSEMatrix seems to hose it
if (length(gset) > 1) idx <- grep("GPL13534", attr(gset, "names"))
   else idx <- 1  ## a kludge, for dealing with SuperSeries entries
gset <- gset[[idx]]
sampleNames(gset) <- gset$title

## turn it into a SummarizedExperiment so I can call DMRs
if(require(regulatoR))
   sortedBrainCells <- as(gset, 'SummarizedExperiment')
   ## the above could maybe become a generic for GEOquery... !?

## massage some covariates
sortedBrainCells$sex <-
     as.factor(sub('Sex: ', '', sortedBrainCells$characteristics_ch1.2))
sortedBrainCells$race <-
     as.factor(sub('race: ', '', sortedBrainCells$characteristics_ch1.3))
sortedBrainCells$age <-
     as.numeric(sub('age: ', '', sortedBrainCells$characteristics_ch1.4))

## now the part that is bugging me: why won't R coerce automatically?
##
## define a coercion for DataFrame to data.frame:
setAs("DataFrame", "data.frame", function(from) as.data.frame(from))
##
## try to assemble a model.matrix
model.matrix(~ age + race + sex, data=colData(sortedBrainCells))
##
## D'OH!
##
## Error in as.data.frame.default(data, optional = TRUE) :
##  cannot coerce class "structure("DataFrame", package = "IRanges")" to a
data.frame

What seems to be called internally is as.data.frame.default, and
of course as.data.frame.default as no idea how to convert a DataFrame
into a data.frame:

  > as.data.frame.default(DataFrame())
  Error in as.data.frame.default(DataFrame()) :
cannot coerce class "structure("DataFrame", package = "IRanges")" to a data.frame

I think this would work if you defined an S3 as.data.frame method
for DataFrame, instead of an S4 "coerce" method from DataFrame to
data.frame. Try to define the following:

  as.data.frame.DataFrame <- selectMethod("as.data.frame", "DataFrame")

A couple of months ago I've started to add some S3 methods to the
IRanges/GenomicRanges infrastructure. So far I only did it for
duplicated, unique, sort, levels, as.list, window, window<-. but
many are still missing (30-40 or more). as.data.frame.DataFrame is
one of them.

Cheers,
H.


##  ok fine whatever, I'll do the coercion myself
mat <- model.matrix(~ age + race + sex,
data=as(colData(sortedBrainCells),'data.frame'))

head(mat)
##        (Intercept) age raceAsian raceCaucasian sexMale
## 5175-G           1  47         0             1       1
## 5175-N           1  47         0             1       1
## 813-N            1  30         0             1       0
## 1740-N           1  13         0             0       0
## 1546-N           1  14         0             0       0
## 1546-G           1  14         0             0       0



--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpa...@fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319

_______________________________________________
Bioc-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/bioc-devel

Reply via email to