Commit 76da474c authored by smorabit's avatar smorabit
Browse files

SetDatExpr unit tests

parent 11349904
Loading
Loading
Loading
Loading
+40 −11
Original line number Diff line number Diff line
@@ -12,11 +12,11 @@ test_that("Metacell output is Seurat format", {

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('Sample'),
        group.by = c('cell_type'),
        k = 5, 
        max_shared = 5,
        ident.group = 'Sample',
        target_metacells=10,
        ident.group = 'cell_type',
        target_metacells=50,
        min_cells=10
    )

@@ -39,11 +39,11 @@ test_that("Metacell features are same as Seurat object",{

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('Sample'),
        group.by = c('cell_type'),
        k = 5, 
        max_shared = 5,
        ident.group = 'Sample',
        target_metacells=10,
        ident.group = 'cell_type',
        target_metacells=50,
        min_cells=10
    )

@@ -67,17 +67,17 @@ test_that("Meta-data gets passed to the metacell object",{

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('Sample', 'cell_type', 'annotation'),
        group.by = c('cell_type', 'annotation'),
        k = 5, 
        max_shared = 5,
        ident.group = 'Sample',
        target_metacells=10,
        ident.group = 'cell_type',
        target_metacells=50,
        min_cells=10
    )

    m_obj <- GetMetacellObject(test_seurat)

    expect_equal(all(c('Sample', 'cell_type', 'annotation') %in% colnames(m_obj@meta.data)), TRUE)
    expect_equal(all(c('cell_type', 'annotation') %in% colnames(m_obj@meta.data)), TRUE)

})

@@ -107,3 +107,32 @@ test_that("Metacell object Idents are expected", {
    expect_equal(check, TRUE)

})

# test that normalizing the metacells works as intended
test_that("Normalizing the metacell object works",{
    data(test_seurat)

    test_seurat <- SetupForWGCNA(
        test_seurat,
        wgcna_name = 'test',
        features = rownames(test_seurat)
    )

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('cell_type'),
        k = 5, 
        max_shared = 5,
        ident.group = 'cell_type',
        target_metacells=50,
        min_cells=10
    )

    test_seurat <- NormalizeMetacells(test_seurat, verbose=FALSE)
    m_obj <- GetMetacellObject(test_seurat)
    expr <- Seurat::GetAssayData(test_seurat, slot='data')

    check <- all.equal(ceiling(expr), expr) == TRUE
    expect_equal(check, FALSE)

})
+153 −0
Original line number Diff line number Diff line
# test that the output format is expected
test_that("SetDatExpr output is a data.frame", {

    data(test_seurat)

    test_seurat <- SetupForWGCNA(
        test_seurat,
        wgcna_name = 'test',
        features = rownames(test_seurat)
    )

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('cell_type'),
        k = 5, 
        max_shared = 5,
        ident.group = 'cell_type',
        target_metacells=50,
        min_cells=10
    )
    test_seurat <- NormalizeMetacells(test_seurat, verbose=FALSE)
    test_seurat <- SetDatExpr(test_seurat, assay = 'RNA')
    datExpr <- GetDatExpr(test_seurat)
    expect_equal(class(datExpr), "data.frame")

})

# check that the datExpr has features for columns
test_that("features are columns", {

    data(test_seurat)

    test_seurat <- SetupForWGCNA(
        test_seurat,
        wgcna_name = 'test',
        features = rownames(test_seurat)
    )

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('cell_type'),
        k = 5, 
        max_shared = 5,
        ident.group = 'cell_type',
        target_metacells=50,
        min_cells=10
    )
    test_seurat <- NormalizeMetacells(test_seurat, verbose=FALSE)
    test_seurat <- SetDatExpr(test_seurat, assay = 'RNA')
    datExpr <- GetDatExpr(test_seurat)

    check <- all(colnames(datExpr) %in% rownames(test_seurat))

    expect_equal(check, TRUE)

})

# check that the return_seurat flag works
test_that("return_seurat flag works", {

    data(test_seurat)

    test_seurat <- SetupForWGCNA(
        test_seurat,
        wgcna_name = 'test',
        features = rownames(test_seurat)
    )

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('cell_type'),
        k = 5, 
        max_shared = 5,
        ident.group = 'cell_type',
        target_metacells=50,
        min_cells=10
    )
    test_seurat <- NormalizeMetacells(test_seurat, verbose=FALSE)
    datExpr <- SetDatExpr(test_seurat, assay = 'RNA', return_seurat=FALSE)
    
    expect_equal(class(datExpr), "data.frame")
})

# check that the use_metacells flag works
test_that("use_metacells flag works", {

    data(test_seurat)

    test_seurat <- SetupForWGCNA(
        test_seurat,
        wgcna_name = 'test',
        features = rownames(test_seurat)
    )

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('cell_type'),
        k = 5, 
        max_shared = 5,
        ident.group = 'cell_type',
        target_metacells=50,
        min_cells=10
    )
    test_seurat <- NormalizeMetacells(test_seurat, verbose=FALSE)
    test_seurat <- SetDatExpr(test_seurat, assay = 'RNA', use_metacells=FALSE)
    datExpr <- GetDatExpr(test_seurat)

    check <- all(rownames(datExpr) %in% colnames(test_seurat))

    expect_equal(check, TRUE)
})



# test that group.by subsetting works as intended
test_that("group.by subsetting works", {

    data(test_seurat)

    test_seurat <- SetupForWGCNA(
        test_seurat,
        wgcna_name = 'test',
        features = rownames(test_seurat)
    )

    test_seurat <- MetacellsByGroups(
        test_seurat,
        group.by = c('Sample'),
        k = 5, 
        max_shared = 5,
        ident.group = 'Sample',
        target_metacells=10,
        min_cells=10
    )
    test_seurat <- NormalizeMetacells(test_seurat, verbose=FALSE)

    # selected groups to subset
    selected_groups <- c("C1", 'C2', 'C3')
    test_seurat <- SetDatExpr(
        test_seurat,
        group.by='Sample', 
        group_name = selected_groups,
        assay = 'RNA'
    )
    datExpr <- GetDatExpr(test_seurat)

    # check that the groups are in the rownames of the datExpr
    samples <- do.call(rbind, strsplit(rownames(datExpr), '_'))[,1]
    check <- all(selected_groups %in% samples)

    expect_equal(check, TRUE)

})
 No newline at end of file