Browse code

Fixes warnings and NOTES

Ashastry2 authored on 21/10/2024 17:30:29
Showing 1 changed files

... ...
@@ -14,8 +14,8 @@
14 14
 #' @param cellIndex A vector that can subset the input SCE object by columns
15 15
 #' (cells). Alternatively, it can be a vector identifying cells in another
16 16
 #' 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}
19 19
 #' @param trim A 2-element numeric vector. Values outside of this range will be
20 20
 #' trimmed to their nearst bound. Default \code{c(-2, 2)}
21 21
 #' @param featureIndexBy A single character specifying a column name of
... ...
@@ -103,8 +103,11 @@
103 103
 #' @importFrom stringr str_replace_all str_c
104 104
 #' @importFrom stats prcomp quantile
105 105
 #' @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
107 107
 #' @importFrom grid gpar
108
+#' @importFrom ComplexHeatmap anno_barplot
109
+#' @importFrom rlang .data
110
+#' 
108 111
 plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
109 112
                            doLog = FALSE, featureIndex = NULL, cellIndex = NULL,
110 113
                            scale = TRUE, trim = c(-2,2),
... ...
@@ -238,14 +241,26 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
238 241
     }
239 242
     featureIndex <- which(featureIndex)
240 243
   }
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
+  
243 258
   SCE <- SCE[featureIndex, cellIndex]
244 259
   ### Scaling should be done before aggregating
245 260
   if (isTRUE(doLog)) assay(SCE) <- log1p(assay(SCE))
246 261
   if(isTRUE(scale)) scale <- "zscore"
247 262
   if ((scale == "zscore")) {
248
-    assay(SCE) <- as.matrix(scale(assay(SCE)))
263
+    assay(SCE) <- as.matrix(base::scale(assay(SCE)))
249 264
   } else if (scale ==  "min_max") {
250 265
     assay(SCE) <- as.matrix(.minmax(assay(SCE)))
251 266
   }    
... ...
@@ -263,7 +278,14 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
263 278
     # TODO: `aggregateAcrossCells` produce duplicated variables in colData
264 279
     # and unwanted "ncell" variable even if I set `store.number = NULL`.
265 280
    #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
+
267 289
     colData(SCE)<-DataFrame(temp_df)
268 290
     rowData(SCE) <- origRowData
269 291
   }
... ...
@@ -278,14 +300,14 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
278 300
     colData(SCE) <- origColData
279 301
   }
280 302
   # STAGE 4: Other minor preparation for plotting ####
281
-
303
+ 
282 304
   # Create a function that sorts the matrix by PC1
283 305
   .orderMatrix<-function(mat){
284 306
     # Adding extra character to rownames because presence of some char gets a "." if I don't
285 307
     mat2<-data.frame(t(mat))
286 308
     rownames(mat2)<-stringr::str_c("K_",rownames(mat2))
287 309
     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"]))
289 311
     mat<-data.frame(t(mat2)) %>% dplyr::select(rownames(kl))
290 312
     colnames(mat)<-stringr::str_replace_all(colnames(mat),"K_","")
291 313
     return(as.matrix(mat))
... ...
@@ -298,10 +320,15 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
298 320
     mat <- .orderMatrix(mat)
299 321
     
300 322
   } 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
+    }
302 330
   }
303 331
    
304
- 
305 332
   
306 333
   if (!is.null(trim) & scale == "zscore") {
307 334
     assay(SCE) <- trimCounts(assay(SCE), trim)  
... ...
@@ -344,6 +371,8 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
344 371
     if (breaks[1] != min(trim) || breaks[length(breaks)] != max(trim))
345 372
       stop('Breaks of `colorScheme` do not match with `trim`.')
346 373
   }
374
+  
375
+  
347 376
   ### Generate HeatmapAnnotation object
348 377
   ca <- NULL
349 378
   cellAnnotationColor <- .heatmapAnnColor(SCE, slot = "colData",
... ...
@@ -351,7 +380,7 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
351 380
                                           palette = palette)
352 381
   if(dim(cellAnnotations)[2] > 0)
353 382
     if(is.null(addCellSummary)){
354
-      ca <- ComplexHeatmap::HeatmapAnnotation(df = colData(SCE),
383
+      ca <- ComplexHeatmap::HeatmapAnnotation(df = as.data.frame(colData(SCE)),
355 384
                                               col = cellAnnotationColor)
356 385
     }
357 386
   else if (!addCellSummary %in% colnames(oldColData)){
... ...
@@ -415,6 +444,7 @@ plotSCEHeatmap <- function(inSCE, useAssay = 'logcounts', useReducedDim = NULL,
415 444
                                 show_row_names = rowLabel,
416 445
                                 row_names_gp = grid::gpar(fontsize = rowLabelSize),
417 446
                                 show_row_dend = rowDend,
447
+                                show_column_dend = colDend,
418 448
                                 row_dend_reorder = TRUE,
419 449
                                 cluster_columns = FALSE,
420 450
                                 show_column_names = colLabel,