Skip to content

Commit

Permalink
Fix plotFeature to shuffle cells correctly
Browse files Browse the repository at this point in the history
Also make sure plots are returned in same order as datasets.
  • Loading branch information
vkozareva committed Jun 5, 2019
1 parent 07a1649 commit abae731
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
19 changes: 10 additions & 9 deletions R/liger.R
Original file line number Diff line number Diff line change
Expand Up @@ -2342,17 +2342,17 @@ plotFeature <- function(object, feature, by.dataset = T, title = NULL, pt.size =
} else {
discrete <- TRUE
}
if (by.dataset) {
dr_df$dataset <- object@cell.data$dataset
} else {
dr_df$dataset <- factor("single")
}
if (do.shuffle) {
set.seed(rand.seed)
idx <- sample(1:nrow(dr_df))
dr_df <- dr_df[idx, ]
}
p_list <- list()
if (by.dataset) {
dr_df$dataset <- object@cell.data$dataset
} else {
dr_df$dataset <- factor("single")
}
for (sub_df in split(dr_df, f = dr_df$dataset)) {
ggp <- ggplot(sub_df, aes(x = dr1, y = dr2, color = feature)) + geom_point(size = pt.size)

Expand Down Expand Up @@ -2384,17 +2384,18 @@ plotFeature <- function(object, feature, by.dataset = T, title = NULL, pt.size =
ggp <- ggp + theme(legend.position = "none")
}
p_list[[as.character(sub_df$dataset[1])]] <- ggp

if (!return.plots) {
print(ggp)
}
}
p_list <- p_list[names(object@raw.data)]
if (return.plots){
if (length(p_list) == 1) {
return(p_list[[1]])
} else {
return(p_list)
}
} else {
for (plot in p_list) {
print(plot)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test_post_factorization.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ plotfeatures_plots <- plotFeature(ligex, feature = 'nUMI', by.dataset = T, retur
test_that("plotFeature returns correct ggplot objects", {
expect_equal(length(plotfeatures_plots), length(ligex@raw.data))
expect_is(plotfeatures_plots[[1]], class = c("ggplot"))
expect_equal(rownames(plotfeatures_plots[[1]]$data)[1:5],
c("AATGCGTGGCTATG", "GAAAGATGATTTCC", "TTCCAAACTCCCAC", "CACTGAGACAGTCA",
"GACGGCACACGGGA"))
})

# Tests for subsetting, object conversion
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test_preprocessing.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test_that("Sparse matrices created", {
expect_is(ligex@raw.data[[1]], "CsparseMatrix")
})

# note that seqwell data is previously normalized, so nUMI is 10000 for all cells
test_that("cell.data created correctly", {
expect_is(ligex@cell.data, "data.frame")
expect_equal(rownames(ligex@cell.data)[1:10], colnames(ligex@raw.data[[1]])[1:10])
Expand Down

0 comments on commit abae731

Please sign in to comment.