Skip to content

Commit 0b66085

Browse files
authored
Merge pull request #36 from NIDAP-Community/dev
Dev
2 parents 31a4d13 + 49f0486 commit 0b66085

3 files changed

Lines changed: 86 additions & 16 deletions

File tree

R/Heatmap.R

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#' @importFrom stringr str_replace_all str_wrap
4141
#' @importFrom colorspace RGB diverge_hcl heat_hcl hex
4242
#' @importFrom grDevices colorRampPalette
43+
#' @importFrom RColorBrewer brewer.pal
4344
#'
4445
#' @export
4546
#'
@@ -76,7 +77,7 @@ heatmapSC <- function(object,
7677
color.space <- colorspace::RGB(runif(n), runif(n), runif(n))
7778
color.space <- as(color.space, "LAB")
7879

79-
80+
8081
#function to create large palette of colors for annotation tracks
8182
.distinctColorPalette <- function(k = 1, seed) {
8283
current.color.space <- color.space@coords
@@ -89,14 +90,14 @@ heatmapSC <- function(object,
8990

9091
## Function to create cyan to mustard palette
9192
.pal <- function (n,
92-
h = c(237, 43),
93-
c = 100,
94-
l = c(70, 90),
95-
power = 1,
96-
fixup = TRUE,
97-
gamma = NULL,
98-
alpha = 1,
99-
...) {
93+
h = c(237, 43),
94+
c = 100,
95+
l = c(70, 90),
96+
power = 1,
97+
fixup = TRUE,
98+
gamma = NULL,
99+
alpha = 1,
100+
...) {
100101
if (n < 1L)
101102
return(character(0L))
102103
h <- rep(h, length.out = 2L)
@@ -230,15 +231,33 @@ heatmapSC <- function(object,
230231
samples.to.include <- samples.to.include[samples.to.include != ""]
231232
samples.to.include <- gsub("-", "_", samples.to.include)
232233

234+
#Error messaging for metadata
235+
236+
if(is.null(metadata)){
237+
stop("Error: You should choose at least one annotation track under metadata_to_plot")
238+
}
239+
240+
if(sum(grepl("Barcode",metadata,ignore.case=TRUE)) > 0){
241+
sprintf("Annotation Track cannot include Barcode")
242+
metadata <- metadata[!grepl('Barcode', metadata, ignore.case=TRUE)]
243+
}
244+
233245
#Clean up transcript names and print missing genes:
234246
transcripts = gsub(" ", "", transcripts)
235247

236248
l1 <- length(transcripts)
249+
p1 <- length(proteins)
250+
251+
if(l1 + p1 == 0){
252+
stop(sprintf("At least 1 transcript and/or protein is needed for plotting"))
253+
}
254+
237255
dups <- transcripts[duplicated(transcripts)]
238256
transcripts <- transcripts[!duplicated(transcripts)]
239257

258+
240259
l2 <- length(transcripts)
241-
print(sprintf("There are %s total unique genes/proteins in the dataset", l2))
260+
sprintf("There are %s total unique genes/proteins in the dataset", l2)
242261
if (l1 > l2) {
243262
warning(sprintf("\n\nThe following duplicate genes were removed: %s",
244263
dups))
@@ -269,21 +288,28 @@ heatmapSC <- function(object,
269288
)
270289
)
271290
}
272-
transcripts <- transcripts[transcripts %in% rownames(object)]
273291

292+
transcripts <- transcripts[transcripts %in% rownames(object)]
274293

275294
#Clean up protein names and print missing proteins:
276295
if (!is.null(object@assays$Protein)) {
277296
proteins = gsub(" ", "", proteins)
278297
if (proteins[1] != "") {
279298
protmiss = setdiff(proteins, rownames(object$Protein@scale.data))
280299
if (length(protmiss) > 0) {
281-
print(sprintf("missing proteins: %s", protmiss))
300+
sprintf("missing proteins: %s", protmiss)
282301
}
283302
}
284303
proteins = proteins[proteins %in% rownames(object$Protein@scale.data)]
285304
}
286305

306+
#Error messaging for protein annotation tracks:
307+
308+
if(add.gene.or.protein == FALSE & (!is.null(protein.annotations) | !is.null(rna.annotations))) {
309+
stop("Error: You should choose to add gene or protein annotation tracks if you add protein or rna annotations")
310+
}
311+
312+
287313
#collect transcript expression data from SCT slot
288314
df.mat1 = NULL
289315
if (length(transcripts) > 0) {
@@ -380,7 +406,7 @@ heatmapSC <- function(object,
380406
annot <- cbind(annot, annot2)
381407
colnames(annot)[colnames(annot) == "annot2"] <- rna.annotations
382408
}
383-
409+
384410
#Arrange columns by metadata tracks:
385411
if (arrange.by.metadata == TRUE) {
386412
annot <- annot %>% arrange(across(all_of(colnames(annot))))
@@ -400,7 +426,7 @@ heatmapSC <- function(object,
400426
annotation.col <- annotation.col %>%
401427
mutate_if(is.logical, as.factor)
402428
rownames(annotation.col) <- rownames(annot)
403-
if (dim(annot)[2] == 2) {
429+
if (dim(annot)[2] == 1) {
404430
annottitle = colnames(annot)[1]
405431
colnames(annotation.col) = annottitle
406432
}

tests/testthat/helper-Heatmap.R

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ getParamHM <- function(data) {
1515
} else if (data == "Chariou") {
1616
object <- selectCRObject("Chariou")
1717
sample.names <- c("PBS", "CD8dep", "ENT", "NHSIL12", "Combo")
18-
metadata <- "orig.ident"
18+
metadata <- c("orig.ident")
1919
set.seed(15)
2020
add.gene.or.protein <- TRUE
2121
transcripts <- sample(rownames(object), 10, replace = FALSE)
@@ -24,6 +24,30 @@ getParamHM <- function(data) {
2424
protein.annotations <- NULL
2525
plot.title <- "Heatmap_Chariou_test"
2626

27+
} else if (data == "Chariou2") {
28+
object <- selectCRObject("Chariou")
29+
sample.names <- c("PBS", "CD8dep", "ENT", "NHSIL12", "Combo")
30+
metadata <- c("orig.ident","Phase")
31+
set.seed(15)
32+
add.gene.or.protein <- FALSE
33+
transcripts <- sample(rownames(object), 10, replace = FALSE)
34+
proteins <- NULL
35+
rna.annotations <- NULL
36+
protein.annotations <- NULL
37+
plot.title <- "Heatmap_Chariou_test"
38+
39+
} else if (data == "Chariou3") {
40+
object <- selectCRObject("Chariou")
41+
sample.names <- c("PBS", "CD8dep", "ENT", "NHSIL12", "Combo")
42+
metadata <- c("orig.ident","Phase")
43+
set.seed(15)
44+
add.gene.or.protein <- FALSE
45+
transcripts <- NULL
46+
proteins <- NULL
47+
rna.annotations <- NULL
48+
protein.annotations <- NULL
49+
plot.title <- "Heatmap_Chariou_test"
50+
2751
} else if (data == "pbmc-single") {
2852
object <- selectSRObject("pbmc-single")
2953
sample.names <- c("PBMC_Single")

tests/testthat/test-Heatmap.R

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ test_that("Produce heatmap and return plot and filtered dataframe: TEC data",
22
{
33
cr.object <- getParamHM("TEC")
44
output <- do.call(heatmapSC, cr.object)
5-
5+
66
expect_type(output, "list")
77
expected.elements = c("plot", "data")
88
expect_setequal(names(output), expected.elements)
@@ -77,6 +77,26 @@ test_that("Produce heatmap - Chariou data", {
7777
"Chariou_heatmap.png")
7878
})
7979

80+
test_that("Chariou with no additional protein/transcript annotations", {
81+
cr.object <- getParamHM("Chariou2")
82+
output <- do.call(heatmapSC, cr.object)
83+
84+
expect_type(output, "list")
85+
expected.elements = c("plot", "data")
86+
expect_setequal(names(output), expected.elements)
87+
88+
skip_on_ci()
89+
expect_snapshot_file(.drawHeatPng(output$plot),
90+
"Chariou_heatmap2.png")
91+
})
92+
93+
test_that("Produce heatmap - Chariou with no transcripts/proteins", {
94+
cr.object <- getParamHM("Chariou3")
95+
96+
expect_error(do.call(heatmapSC, cr.object),
97+
"At least 1 transcript and/or protein is needed for plotting")
98+
})
99+
80100
test_that("Produce heatmap - PBMC single data", {
81101
cr.object <- getParamHM("pbmc-single")
82102
output <- do.call(heatmapSC, cr.object)

0 commit comments

Comments
 (0)