From be522f798ffa09b0b07d1e3c2faa23fbf74c446d Mon Sep 17 00:00:00 2001 From: Lambda Moses Date: Wed, 20 Nov 2024 16:28:22 -0500 Subject: [PATCH] Drop graph when only 1 cell is left in sample as the graph is meaningless, #58 --- R/subset.R | 6 ++++++ tests/testthat/test-subset.R | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/R/subset.R b/R/subset.R index e874bff..6d4570b 100644 --- a/R/subset.R +++ b/R/subset.R @@ -145,6 +145,12 @@ setMethod( ) for (s in which(samples_subset)) { j_sample <- old_sample_compare[[s]] %in% new_sample_colnames[[s]] + if (sum(j_sample) == 1) { + message("Only 1 column left in sample ", sample_ids[[s]], + ", graphs are meaningless, dropping graphs.") + graphs_sub[[s]] <- list(row = NULL, col = NULL, annot = NULL) + next + } for (m in seq_len(2)) { # Not subsetting annotGraphs # Not sure what to do differently with rowGraphs yet for (g in seq_along(graphs_sub[[s]][[m]])) { diff --git a/tests/testthat/test-subset.R b/tests/testthat/test-subset.R index 3b1e998..791ab4a 100644 --- a/tests/testthat/test-subset.R +++ b/tests/testthat/test-subset.R @@ -81,6 +81,17 @@ test_that("Subset the graph when distance-based edge weights are used", { ) }) +test_that("When only one cell/spot is left in a sample after subsetting", { + expect_message(sfe1 <- sfe_visium[,1:6], "graphs are meaningless, dropping graphs") + expect_null(colGraphNames(sfe1, sample_id = "sample02")) +}) + +test_that("When only one cell is left, option reconstruct", { + withr::local_options(SFE_graph_subset = FALSE) + expect_message(sfe1 <- sfe_visium[,1:6], "graphs are meaningless, dropping graphs") + expect_null(colGraphNames(sfe1, sample_id = "sample02")) +}) + test_that("Warning message and dropping graphs when reconstruction info is unavailable", { withr::local_options(SFE_graph_subset = FALSE) # Remove one item from sample02