Skip to content

Commit

Permalink
Add coercion method from ExpressionSets; DataLayer docs
Browse files Browse the repository at this point in the history
  • Loading branch information
amcdavid committed Apr 25, 2014
1 parent 03c3c30 commit 8410a63
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SingleCellAssay
Type: Package
Title: Core data structures for single-cell assays
Version: 0.79
Version: 0.80
Date: 2014-04-08
Author: Andrew McDavid <[email protected]>, Greg Finak <[email protected]>
Maintainer: Greg Finak <[email protected]>
Expand Down
40 changes: 39 additions & 1 deletion R/AllClasses.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,46 @@

NULL

#setOldClass("ncdf")

##' DataLayer class
##'
##' DataLayer is a 3-D array, wrapped to make it look like a matrix.
##' It is used to hold matrix-like expression data, for which we might want to keep several representations (transformations) around.
##' The number of matrix "layers" is given by the trailing dimension.
##' Dimensions 1 and 2 correspond to the "rows" and "columns" of the matrix.
##' The layer that is active can be set, and additional layers created (concatenated).
##' }
##' \section{Slots}{
##' DataLayer extends array, and has the following additional slots
##' \describe{
##' \item{.Data}{the underlying array}
##' \item{valid}{a \code{logical} that may optionally indicate the freshness of derived layers (if the underlying data changes). Not currently used.}
##' \item{layer}{which 'slice' of the array is being used}
##' }}
##' \section{Methods}{
##' \describe{
##' \item{addlayer}{Concatentate another slice onto the object}
##' \item{layername}{Return the name of the current slice}
##' \item{layer}{Return the active layer}
##' \item{layer<-}{Set the active layer}
##' \item{exprs}{Return the matrix representation of the active layer}
##' \item{exprs<-}{Replace the matrix on the current layer.}
##' }
##' @examples
##' ar <- array(1:10, dim=c(2, 5, 1))
##' dl <- new('DataLayer', .Data=ar)
##' nrow(dl) #2
##' ncol(dl) #5
##' layer(dl)
##' dl <- addlayer(dl, 'negative')
##' ex <- exprs(dl)
##' layer(dl) <- 'negative' #or could use 2
##' exprs(dl)<- -ex
##' exprs(dl)
##' @name DataLayer-class
##' @docType class
##' @aliases DataLayer
##' @seealso \code{\link{SingleCellAssay}}, \code{\link{SingleCellAssay-class}}
setClass('DataLayer', contains='array', representation=representation(layer='numeric', valid='logical'), prototype=prototype(array(NA, dim=c(0, 0, 1)), layer=1L, valid=TRUE), validity=function(object){
#cat('DL dim ', dim([email protected]), '\n')
length(dim(object@.Data))==3
Expand Down
4 changes: 4 additions & 0 deletions R/DataLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ setMethod("exprs",signature(object="DataLayer"),function(object){
setMethod('initialize', 'DataLayer',
function(.Object, ...){
## message('init DataLayer') #DEBUG
## This is (was?) necessary initialize since we inherit from 'array'
## But is rather mysterious, nonetheless.
.Object <- getMethod('initialize', 'ANY')(.Object, ...)
dn <- dimnames(.Object@.Data)
dimnames(.Object@.Data) <-if(is.null(dn)) list(wells=NULL, features=NULL, layers=NULL) else dn
.Object
})

Expand Down
2 changes: 1 addition & 1 deletion R/Fluidigm-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ filter <- function(sc, groups=NULL, filt_control=NULL, apply_filter=TRUE){
if (!is.null(groups)) {
checkGroups(sc, groups)
scL <- split(sc, groups)
lapp <- lapply(scL, filter, groups=NULL, filt_control, apply_filter)
lapp <- lapply(scL, filter, groups=NULL, filt_control=filt_control, apply_filter=apply_filter)
## Do various things with lapp:
if(apply_filter && filt_control$filter){
## list of SingleCellAssays
Expand Down
22 changes: 21 additions & 1 deletion R/SingleCellAssay-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fixdf <- function(df, idvars, primerid, measurement, cmap, fmap, keep.names){
## unnamed arguments get passed along to callNextMethod
## which eventually just sets the slots
setMethod('initialize', 'SingleCellAssay',
function(.Object, dataframe, idvars, primerid, measurement, cellvars=NULL, featurevars=NULL, phenovars=NULL, sort=TRUE, ...){
function(.Object, dataframe, idvars, primerid, measurement, exprsMatrix, cellvars=NULL, featurevars=NULL, phenovars=NULL, sort=TRUE, ...){
##message(class(.Object), ' calling SingleCellAssay Initialize') #DEBUG
.Object <- callNextMethod()
if(sort) .Object <- sort(.Object)
Expand Down Expand Up @@ -639,3 +639,23 @@ setMethod('combine', signature=c(x='SingleCellAssay', y='AnnotatedDataFrame'), f
slot(x, along) <- newdata
x
})

setAs('ExpressionSet', 'SingleCellAssay', function(from){
## just a transposed version
ex <- t(exprs(from))
dn <- dimnames(ex)
names(dn) <- c('wellKey', 'primerid')
dim(ex) <- c(dim(ex), 1)
pd <- phenoData(from)
pData(pd)[,'wellKey'] <- sampleNames(pd)
fd <- featureData(from)
fd$primerid <- sampleNames(fd)
dimnames(ex) <- c(dn, layer='ExpressionSet')
DL <- new('DataLayer', .Data=ex)
new('SingleCellAssay', .Data=DL, featureData=fd, cellData=pd, sort=FALSE)
})

## setAs('SingleCellAssay', 'data.table', function(from){
## dt <- data.table(as.vector(exprs(from)))

## })
8 changes: 4 additions & 4 deletions inst/tests/test-DataLayer.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ test_that('doubleton dl works', {

context('subset and replace')
test_that('subset works', {
expect_equal(dl[[,1]], mat[,1,drop=FALSE])
expect_equal(dl[[1,]], mat[1,,drop=FALSE])
expect_equal(dl[[,1]], mat[,1,drop=FALSE], check.names=FALSE)
expect_equal(dl[[1,]], mat[1,,drop=FALSE], check.names=FALSE)
matidx <- cbind(c(1, 1, 2), 1:3)
expect_equal(dl[[matidx]], mat[matidx])
mat[matidx] <- -999
dl[[matidx]] <- -999
expect_equal(exprs(dl), mat)
expect_equal(exprs(dl), mat, check.attributes=FALSE)
})

test_that('[ subscripting works', {
expect_is(dl[1,], 'DataLayer')
expect_is(dl[,2:3], 'DataLayer')
expect_equal(exprs(dl[1,]), dl[[1,,drop=FALSE]])
expect_equal(exprs(dl[1,]), dl[[1,,drop=FALSE]], check.attributes=FALSE)
})

spl <- split(dl, 1:2)
Expand Down
44 changes: 44 additions & 0 deletions man/DataLayer-class.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
\docType{class}
\name{DataLayer-class}
\alias{DataLayer}
\alias{DataLayer-class}
\title{DataLayer class}
\description{
DataLayer is a 3-D array, wrapped to make it look like a
matrix. It is used to hold matrix-like expression data, for
which we might want to keep several representations
(transformations) around. The number of matrix "layers" is
given by the trailing dimension. Dimensions 1 and 2
correspond to the "rows" and "columns" of the matrix. The
layer that is active can be set, and additional layers
created (concatenated). } \section{Slots}{ DataLayer
extends array, and has the following additional slots
\describe{ \item{.Data}{the underlying array}
\item{valid}{a \code{logical} that may optionally indicate
the freshness of derived layers (if the underlying data
changes). Not currently used.} \item{layer}{which 'slice'
of the array is being used} }} \section{Methods}{
\describe{ \item{addlayer}{Concatentate another slice onto
the object} \item{layername}{Return the name of the current
slice} \item{layer}{Return the active layer}
\item{layer<-}{Set the active layer} \item{exprs}{Return
the matrix representation of the active layer}
\item{exprs<-}{Replace the matrix on the current layer.} }
}
\examples{
ar <- array(1:10, dim=c(2, 5, 1))
dl <- new('DataLayer', .Data=ar)
nrow(dl) #2
ncol(dl) #5
layer(dl)
dl <- addlayer(dl, 'negative')
ex <- exprs(dl)
layer(dl) <- 'negative' #or could use 2
exprs(dl)<- -ex
exprs(dl)
}
\seealso{
\code{\link{SingleCellAssay}},
\code{\link{SingleCellAssay-class}}
}

0 comments on commit 8410a63

Please sign in to comment.