diff --git a/NEWS.md b/NEWS.md index 920ad9f..b91816a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,3 +9,4 @@ * Add `sample.name.column` to select the metadata column used for sample filtering, defaulting to `orig.ident` (#100). * Log requested sample filtering and displayed filtered/total cell counts (#101). * Use all samples when sample selection is blank (#102). +* Report found Heatmap features before missing requested features without duplicate warnings (#103). diff --git a/R/Heatmap.R b/R/Heatmap.R index 72f3f15..d327836 100755 --- a/R/Heatmap.R +++ b/R/Heatmap.R @@ -292,20 +292,19 @@ heatmapSC <- function(object, } transcript.data <- object@assays[[use.assay]]@scale.data missing.genes <- setdiff(transcripts, rownames(transcript.data)) + transcripts <- transcripts[transcripts %in% rownames(transcript.data)] + if (length(transcripts) > 0) { + message("Transcripts found in ", use.assay, ": ", + paste(transcripts, collapse = ", ")) + } if (length(missing.genes) > 0) { missing.transcript.message <- paste0( "There are ", length(missing.genes), " gene(s) absent from dataset: ", paste(shQuote(missing.genes), collapse = ", "), - ". Possible reasons are that gene is not official gene symbol or gene is ", - "not highly expressed and has been filtered." + ".\n Possible reasons are that gene is not official gene symbol ", + "or gene is not highly expressed and has been filtered." ) message(missing.transcript.message) - warning(missing.transcript.message) - } - transcripts <- transcripts[transcripts %in% rownames(transcript.data)] - if (length(transcripts) > 0) { - message("Transcripts found in ", use.assay, ": ", - paste(transcripts, collapse = ", ")) } if (length(proteins) > 0 && !"Protein" %in% names(object@assays)) { @@ -314,18 +313,17 @@ heatmapSC <- function(object, if (length(proteins) > 0) { missing.proteins <- setdiff(proteins, rownames(object$Protein@scale.data)) + proteins <- proteins[proteins %in% rownames(object$Protein@scale.data)] + if (length(proteins) > 0) { + message("Proteins found in Protein assay: ", + paste(proteins, collapse = ", ")) + } if (length(missing.proteins) > 0) { missing.protein.message <- paste0( "Proteins not found in Protein assay: ", paste(missing.proteins, collapse = ", "), ". They will be excluded." ) message(missing.protein.message) - warning(missing.protein.message) - } - proteins <- proteins[proteins %in% rownames(object$Protein@scale.data)] - if (length(proteins) > 0) { - message("Proteins found in Protein assay: ", - paste(proteins, collapse = ", ")) } } diff --git a/tests/testthat/test-Heatmap.R b/tests/testthat/test-Heatmap.R index a0a6b16..4bb19a7 100755 --- a/tests/testthat/test-Heatmap.R +++ b/tests/testthat/test-Heatmap.R @@ -192,19 +192,22 @@ test_that("Heatmap run with bad gene name", { #Spike in with bad gene name cr.object$transcripts <- c("badgene", cr.object$transcripts) missing.transcript.message <- paste0( - "There are 1 gene(s) absent from dataset: 'badgene'. Possible reasons are ", - "that gene is not official gene symbol or gene is not highly expressed and ", - "has been filtered." + "There are 1 gene(s) absent from dataset: 'badgene'.\n", + " Possible reasons are that gene is not official gene symbol or gene ", + "is not highly expressed and has been filtered." ) - expect_warning( - expect_message( + expect_no_warning( + logs <- capture.output( do.call(heatmapSC, cr.object), - missing.transcript.message, - fixed = TRUE - ), - missing.transcript.message, - fixed = TRUE + type = "message" + ) + ) + log.text <- paste(logs, collapse = "\n") + expect_true(grepl(missing.transcript.message, log.text, fixed = TRUE)) + expect_lt( + grep("Transcripts found in", logs, fixed = TRUE)[[1]], + grep("There are 1 gene(s) absent from dataset", logs, fixed = TRUE)[[1]] ) }) @@ -213,12 +216,18 @@ test_that("Heatmap reports missing proteins and retains available proteins", { cr.object$proteins <- c("CD3", "not_a_protein") cr.object$protein.annotations <- NULL - expect_warning( - expect_message( + expect_no_warning( + logs <- capture.output( output <- do.call(heatmapSC, cr.object), - "Proteins not found in Protein assay: not_a_protein. They will be excluded." - ), + type = "message" + ) + ) + missing.protein.message <- "Proteins not found in Protein assay: not_a_protein. They will be excluded." + expect_true(any(grepl(missing.protein.message, logs, fixed = TRUE))) + expect_lt( + grep("Proteins found in", logs, fixed = TRUE)[[1]], + grep(missing.protein.message, logs, fixed = TRUE)[[1]] ) expect_true("CD3_Prot" %in% output$data$gene) @@ -264,10 +273,10 @@ test_that("Heatmap run with error for missing all genes", { c("APOE", "ARG1", "CD38", "CD3D", "CD3E", "CD3G") missing.transcript.message <- paste0( "There are 6 gene(s) absent from dataset: 'APOE', 'ARG1', 'CD38', 'CD3D', ", - "'CD3E', 'CD3G'. Possible reasons are that gene is not official gene symbol ", - "or gene is not highly expressed and has been filtered." + "'CD3E', 'CD3G'.\n Possible reasons are that gene is not official ", + "gene symbol or gene is not highly expressed and has been filtered." ) - expect_warning( + expect_no_warning( expect_message( expect_error( do.call(heatmapSC, cr.object), @@ -275,9 +284,7 @@ test_that("Heatmap run with error for missing all genes", { ), missing.transcript.message, fixed = TRUE - ), - missing.transcript.message, - fixed = TRUE + ) ) })