Skip to content

Commit

Permalink
Merge pull request #156 from MacoskoLab/fix-subset-object
Browse files Browse the repository at this point in the history
Fix cell order bug in subsetLiger
  • Loading branch information
MacoskoLab authored Apr 10, 2020
2 parents 8d32067 + 922abcc commit 1f1a7b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
9 changes: 5 additions & 4 deletions R/liger.R
Original file line number Diff line number Diff line change
Expand Up @@ -4776,10 +4776,6 @@ subsetLiger <- function(object, clusters.use = NULL, cells.use = NULL, remove.mi
nms <- names(object@raw.data)[!missing]
names(raw.data) <- nms
a <- createLiger(raw.data, remove.missing = remove.missing)
# Add back additional cell.data
if (ncol(a@cell.data) < ncol(object@cell.data)) {
a@cell.data <- droplevels(data.frame(object@cell.data[cells.use, ]))
}
a@norm.data <- lapply(1:length(a@raw.data), function(i) {
object@norm.data[[nms[i]]][, colnames(a@raw.data[[i]])]
})
Expand All @@ -4793,6 +4789,11 @@ subsetLiger <- function(object, clusters.use = NULL, cells.use = NULL, remove.mi
a@clusters <- droplevels(a@clusters)
a@tsne.coords <- object@tsne.coords[names(a@clusters), ]
a@H.norm <- object@H.norm[names(a@clusters), ]
# Add back additional cell.data
if (ncol(a@cell.data) < ncol(object@cell.data)) {
a@cell.data <- droplevels(data.frame(object@cell.data[names(a@clusters), ]))
}

a@W <- object@W
a@V <- object@V
a@var.genes <- object@var.genes
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test_post_factorization.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ test_that("Returns correct subsetted object", {
expect_equal(rownames(ligex_subset@cell.data), rownames(ligex_subset@tsne.coords))
})

# create "pseudorandom" set of cells to downsample
cells.use = c('CACTGAGACAGTCA', 'CD8_124', 'Bcell_103', 'Bcell_17', 'Bcell_236', 'GGGCCAACCTTGGA',
'ACCTCCGATATGCG', 'Bcell_242', 'CD4_407', 'CD8_265', 'GACAGTACGAGCTT', 'GACCCTACTAAAGG',
'DC_37', 'CD4_35', 'ATACTCTGGTATGC', 'CAAAGCTGAAAGTG', 'AGCACTGATGCTTT', 'Bcell_280',
'CD4_503', 'DC_97', 'NK_192', 'GGCACGTGGCTTAG', 'CGTTTAACTGGTCA', 'TATGCGGATAACCG',
'TGTGATCTGACACT', 'CD4_500', 'GGCGGACTTGAACC', 'ATGTAAACACCTCC', 'CD4_539', 'DC_12')
ligex_subset <- subsetLiger(ligex, cells.use = cells.use)

test_that("Returns correct subsetted object", {
expect_equal(names(ligex_subset@raw.data), c('tenx', 'seqwell'))
expect_equal(dim(ligex_subset@raw.data[[1]]), c(5233, 14))
expect_equal(colnames(ligex_subset@raw.data[[1]])[1:3], c("CACTGAGACAGTCA",
"GGGCCAACCTTGGA", "ACCTCCGATATGCG"))
expect_equal(dim(ligex_subset@raw.data[[2]]), c(4670, 16))
expect_equal(colnames(ligex_subset@raw.data[[2]])[1:3], c("CD8_124", "Bcell_103",
"Bcell_17"))
expect_equal(levels(ligex_subset@clusters), c("0", "1", "2", "3", "4", "5", "6", "7"))
expect_equal(nrow(ligex_subset@cell.data), 30)
expect_equal(rownames(ligex_subset@cell.data), rownames(ligex_subset@tsne.coords))
})

# TODO: Add tests for ligerToSeurat and seuratToLiger functions
# after including functionality related to new cell.data slot

Expand Down

0 comments on commit 1f1a7b9

Please sign in to comment.