Skip to content

Commit

Permalink
Added option to specify correlation method in plot_samplecor()
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidasilvaf committed Nov 2, 2023
1 parent be5fc31 commit 5fe1c68
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ importFrom(grDevices,colorRampPalette)
importFrom(patchwork,wrap_plots)
importFrom(rlang,.data)
importFrom(stats,as.formula)
importFrom(stats,cor)
importFrom(stats,prcomp)
importFrom(stats,setNames)
importFrom(stats,var)
9 changes: 6 additions & 3 deletions R/visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ pca_plot <- function(
#' which columns should be extracted from \strong{rowData(se)}.
#' @param ntop Numeric indicating the number of top genes with the
#' highest variances to use for the PCA. Default: 500.
#' @param cor_method Character indicating the correlation method to use.
#' One of "pearson" or "spearman". Default: "pearson".
#' @param palette Character indicating the name of the color palette from
#' the RColorBrewer package to use. Default: "Blues".
#' @param ... Additional arguments to be passed
Expand All @@ -347,6 +349,7 @@ pca_plot <- function(
#' @importFrom ComplexHeatmap pheatmap
#' @importFrom RColorBrewer brewer.pal
#' @importFrom grDevices colorRampPalette
#' @importFrom stats cor
#' @export
#' @rdname plot_samplecor
#' @examples
Expand All @@ -357,12 +360,12 @@ pca_plot <- function(
#' plot_samplecor(se, ntop = 500)
plot_samplecor <- function(
se, coldata_cols = NULL, rowdata_cols = NULL,
ntop = 500, palette = "Blues", ...
ntop = 500, cor_method = "pearson", palette = "Blues", ...
) {

# Get gene expression matrix for genes with highest variances
top <- sort(apply(assay(se), 1, var), decreasing = TRUE)[seq_len(ntop)]
exp <- as.matrix(assay(se)[names(top), ])
exp <- as.matrix(varianceStabilizingTransformation(assay(se)[names(top), ]))

coldata <- se2metadata(se, coldata_cols = coldata_cols)$coldata
rowdata <- se2metadata(se, rowdata_cols = rowdata_cols)$rowdata
Expand All @@ -385,7 +388,7 @@ plot_samplecor <- function(

# Plot heatmap
hm <- ComplexHeatmap::pheatmap(
as.matrix(exp),
cor(as.matrix(exp), method = cor_method),
name = "Correlation",
color = colorRampPalette(brewer.pal(9, palette))(100),
border_color = NA,
Expand Down
4 changes: 4 additions & 0 deletions man/plot_samplecor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5fe1c68

Please sign in to comment.