tests/testthat/test-sce-with-lem.R

# Checks that LEM can be used inside a SCE object.
# library(SingleCellExperiment); library(testthat); source("setup.R"); source("test-sce-with-lem.R")

factors <- matrix(rnorm(1000), ncol=10)
loadings <- matrix(runif(10000), ncol=10)
lem <- LinearEmbeddingMatrix(factors, loadings)

sce <- loaded
reducedDim(sce) <- lem

test_that("SCE construction works correctly with LEM", {

    sce1 <- SingleCellExperiment(assay=SimpleList(counts=u, exprs=v),
                                 reducedDims = SimpleList(rd1 = lem))
    sce2 <- SingleCellExperiment(assay=SimpleList(counts=u, exprs=v),
                                 reducedDims = SimpleList(rd1 = lem, rd2 = factors))


    expect_identical(reducedDim(sce1, "rd1"), reducedDim(sce2, "rd1"))

    # Weird hack required as a round-trip through a DataFrame seems to strip attributes.
    class(lem) <- as.character(class(lem))
    expect_identical(reducedDim(sce1, "rd1"), lem)
})

test_that("reduced dimension getters/setters are functioning", {

    sce <- SingleCellExperiment(assay=SimpleList(counts=u, exprs=v))
    class(lem) <- as.character(class(lem))
    reducedDim(sce, "PCA") <- lem
    expect_identical(reducedDim(sce, "PCA"), lem)
    expect_identical(reducedDims(sce), SimpleList(PCA=lem))
    expect_identical(reducedDimNames(sce), "PCA")

})


test_that("subsetting by row works correctly", {

    colData(sce)$indicator <- seq_len(ncells)

    for (j in 1:3) {
        if (j==1L) {
            by.col <- sample(ncells, 20)
            sub.sce <- sce[,by.col]
            expect_identical(colData(sub.sce)$indicator, by.col)
        } else if (j==2L) {
            by.col <- rbinom(ncells, 1, 0.2)==1
            sub.sce <- sce[,by.col]
            expect_identical(colData(sub.sce)$indicator, which(by.col))
        } else if (j==3L) {
            by.col <- colnames(sce)[sample(ncells, 50)]
            sub.sce <- sce[,by.col]
            expect_identical(colData(sub.sce)$indicator, match(by.col, colnames(sce)))
        }
        ind <- colData(sub.sce)$indicator

        expect_identical(reducedDim(sub.sce, "PCA"), lem[ind,,drop=FALSE])

    }

})


test_that("cbind works correctly", {
    shuffled <- sample(ncells)
    sce.alt <- sce[,shuffled]

    sce2 <- cbind(sce, sce.alt)
    expect_identical(reducedDim(sce2, "PCA"), rbind(reducedDim(sce, "PCA"), reducedDim(sce.alt, "PCA")))

    sce.err <- sce
    reducedDim(sce.err, "PCA") <- NULL
    expect_error(cbind(sce.err, sce), "'int_colData'")

    sce.err <- sce
    reducedDim(sce.err, "PCA") <- sampleFactors(reducedDim(sce.err, "PCA"))
    expect_error(cbind(sce.err, sce), "no method for coercing this S4 class to a vector")
    expect_error(cbind(sce, sce.err), "no method for coercing this S4 class to a vector")

})

Try the SingleCellExperiment package in your browser

Any scripts or data that you put into this service are public.

SingleCellExperiment documentation built on Nov. 8, 2020, 7:51 p.m.