Skip to content

Commit 21e2eb4

Browse files
Merge pull request #769 from Ashastry2/master
Fixes Notes and other warnings
2 parents d392716 + fb16476 commit 21e2eb4

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

R/plotSCEHeatmap.R

+42-12
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#' @param cellIndex A vector that can subset the input SCE object by columns
1515
#' (cells). Alternatively, it can be a vector identifying cells in another
1616
#' cell list indicated by \code{featureIndexBy}. Default \code{NULL}.
17-
#' @param scale Whether to perform z-score scaling on each row. Default
18-
#' \code{TRUE}.
17+
#' @param scale Whether to perform z-score or min-max scaling on each row.Choose from \code{"zscore"}, \code{"min-max"} or default
18+
#' \code{TRUE} or \code{FALSE}
1919
#' @param trim A 2-element numeric vector. Values outside of this range will be
2020
#' trimmed to their nearst bound. Default \code{c(-2, 2)}
2121
#' @param featureIndexBy A single character specifying a column name of
@@ -103,8 +103,11 @@
103103
#' @importFrom stringr str_replace_all str_c
104104
#' @importFrom stats prcomp quantile
105105
#' @importFrom dplyr select arrange group_by count ungroup mutate one_of desc
106-
#' @importFrom tidyr spread
106+
#' @importFrom tidyr spread unite column_to_rownames remove_rownames
107107
#' @importFrom grid gpar
108+
#' @importFrom ComplexHeatmap anno_barplot
109+
#' @importFrom rlang .data
110+
#'
108111
plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
109112
doLog = FALSE, featureIndex = NULL, cellIndex = NULL,
110113
scale = TRUE, trim = c(-2,2),
@@ -238,14 +241,26 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
238241
}
239242
featureIndex <- which(featureIndex)
240243
}
241-
colnames(SCE) <- colLabelName
242-
rownames(SCE) <- rowLabelName
244+
if(is.null(colLabelName)){
245+
colnames(SCE) <- NULL
246+
}
247+
else{
248+
colnames(SCE) <- colLabelName
249+
}
250+
251+
if(is.null(rowLabelName)){
252+
rownames(SCE) <- NULL
253+
}
254+
else{
255+
rownames(SCE) <- rowLabelName
256+
}
257+
243258
SCE <- SCE[featureIndex, cellIndex]
244259
### Scaling should be done before aggregating
245260
if (isTRUE(doLog)) assay(SCE) <- log1p(assay(SCE))
246261
if(isTRUE(scale)) scale <- "zscore"
247262
if ((scale == "zscore")) {
248-
assay(SCE) <- as.matrix(scale(assay(SCE)))
263+
assay(SCE) <- as.matrix(base::scale(assay(SCE)))
249264
} else if (scale == "min_max") {
250265
assay(SCE) <- as.matrix(.minmax(assay(SCE)))
251266
}
@@ -263,7 +278,14 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
263278
# TODO: `aggregateAcrossCells` produce duplicated variables in colData
264279
# and unwanted "ncell" variable even if I set `store.number = NULL`.
265280
#colData(SCE) <- colData(SCE)[,c(aggregateCol),drop=FALSE] ##change
266-
temp_df<-as.data.frame(colData(SCE)[,c(aggregateCol),drop=FALSE]) %>% unite("new_colnames",1:ncol(.),sep = "_") %>% remove_rownames() %>% column_to_rownames("new_colnames")
281+
282+
temp_df<-as.data.frame(colData(SCE)[,c(aggregateCol),drop=FALSE]) %>%
283+
unite("new_colnames",1:ncol(.),sep = "_",remove = FALSE) %>%
284+
remove_rownames() %>%
285+
mutate(aggregated_column = new_colnames) %>%
286+
dplyr::select(new_colnames, aggregated_column) %>%
287+
column_to_rownames("new_colnames")
288+
267289
colData(SCE)<-DataFrame(temp_df)
268290
rowData(SCE) <- origRowData
269291
}
@@ -278,14 +300,14 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
278300
colData(SCE) <- origColData
279301
}
280302
# STAGE 4: Other minor preparation for plotting ####
281-
303+
282304
# Create a function that sorts the matrix by PC1
283305
.orderMatrix<-function(mat){
284306
# Adding extra character to rownames because presence of some char gets a "." if I don't
285307
mat2<-data.frame(t(mat))
286308
rownames(mat2)<-stringr::str_c("K_",rownames(mat2))
287309
pca_mat<-stats::prcomp(mat2,center = TRUE, scale. = FALSE)
288-
kl<-dplyr::arrange(data.frame(pca_mat$x)["PC1"],desc(PC1))
310+
kl<-dplyr::arrange(data.frame(pca_mat$x)["PC1"],desc(data.frame(pca_mat$x)["PC1"]))
289311
mat<-data.frame(t(mat2)) %>% dplyr::select(rownames(kl))
290312
colnames(mat)<-stringr::str_replace_all(colnames(mat),"K_","")
291313
return(as.matrix(mat))
@@ -298,10 +320,15 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
298320
mat <- .orderMatrix(mat)
299321

300322
} else{
301-
mat<- assay(SCE)
323+
324+
if(class(assay(SCE))[1] == "dgCMatrix"){
325+
mat<- as.matrix(assay(SCE))
326+
}
327+
else{
328+
mat <- assay(SCE)
329+
}
302330
}
303331

304-
305332

306333
if (!is.null(trim) & scale == "zscore") {
307334
assay(SCE) <- trimCounts(assay(SCE), trim)
@@ -344,14 +371,16 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
344371
if (breaks[1] != min(trim) || breaks[length(breaks)] != max(trim))
345372
stop('Breaks of `colorScheme` do not match with `trim`.')
346373
}
374+
375+
347376
### Generate HeatmapAnnotation object
348377
ca <- NULL
349378
cellAnnotationColor <- .heatmapAnnColor(SCE, slot = "colData",
350379
custom = cellAnnotationColor,
351380
palette = palette)
352381
if(dim(cellAnnotations)[2] > 0)
353382
if(is.null(addCellSummary)){
354-
ca <- ComplexHeatmap::HeatmapAnnotation(df = colData(SCE),
383+
ca <- ComplexHeatmap::HeatmapAnnotation(df = as.data.frame(colData(SCE)),
355384
col = cellAnnotationColor)
356385
}
357386
else if (!addCellSummary %in% colnames(oldColData)){
@@ -415,6 +444,7 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
415444
show_row_names = rowLabel,
416445
row_names_gp = grid::gpar(fontsize = rowLabelSize),
417446
show_row_dend = rowDend,
447+
show_column_dend = colDend,
418448
row_dend_reorder = TRUE,
419449
cluster_columns = FALSE,
420450
show_column_names = colLabel,

0 commit comments

Comments
 (0)