a2_HeatmapExample.RmdWe will begin by creating a scCNVis object using the
provided example data.
library(scCNVis)
set.seed(42)
input.matrix <- example.matrix
cell.names <- example.cells
granges <- example.granges
meta.df <- example.meta
#Create a scCNVis object
obj <- scCNVis::createPlotObject(input.matrix, cell.names, granges, meta.df)The scCNVis object is a simple object. It contains the input matrix
(accessible as obj@Matrix) and the associated
GenomicRanges::GRanges object (accessible as obj@GRanges).
It also contains metadata associated with each cell, if provided.
For example, a simple heatmap can be created using the following:
#Set custom color scale
heatmap.colors <- c(0,1,2)
names(heatmap.colors) <- c("Blue","White","Red")
heatmap.result <- scCNVis::makeSCHeatmap(obj,
heatmap.colors = heatmap.colors)
print(names(heatmap.result))
#> [1] "Plot" "PlotData"
print(names(heatmap.result$PlotData))
#> [1] "PlotData" "Clustering" "Legend" "Title"As you can see, the result is a list object that contains the
Plot, which is a ComplexHeatmap object, and
the PlotData. In this case, the PlotData is a
list containing the actual PlotData (a matrix for the heatmap) and a
clustering result, which relates to the row ordering in the heatmap. In
this case, it also includes the information for drawing the legend of
the plot. These values can be used to create other version of the plot
using other packages, or be saved for downstream analysis.
The resulting file can be saved however you would normally export a
ComplexHeatmap. For convenience, it can be saved using the
scCNVis wrapper function for exporting plots. The expected output for
the heatmap is also shown below.
#Example manual method
#png("output/file.png", width=2000, height=500)
#ComplexHeatmap::draw(heatmap.result$Plot,
# show_heatmap_legend = FALSE,
# annotation_legend_list = heatmap.result$PlotData$Legend)
#dev.off()
#Simple wrapper for convenience
scCNVis::saveCustomPlot(heatmap.result, "/path/to/heatmap.png")
Example 1a. Heatmap
The heatmap can be annotated if metadata has been provided. If
needed, metadata groupings can be added using the
addMetaData() function. At the same time, we may choose to
limit our plot to specific regions of interest. For this example, we may
choose to exclude chr22, as seen below:
plot.regions <- example.granges[example.granges@seqnames != "chr22",]
#If using a basic annotation as shown here and you want to specify colors,
#it should be provided as a named list
#The data here should be consistent with whatever metadata was added to the
#scCNVis object. In this case, our example meta data had 4 subclones.
#This is optional.
colors <- c("red","blue","green","yellow")
names(colors) <- unique(obj@Meta$Subclone)
annotation.colors <- list(Subclone = colors)
heatmap.result <- scCNVis::makeSCHeatmap(obj,
gr = plot.regions,
annotations = "Subclone",
show.annotation.name = TRUE,
annotation.colors = annotation.colors,
heatmap.colors = heatmap.colors)
#Save the result
#scCNVis::saveCustomPlot(heatmap.result, "/path/to/heatmap.png")
Example 1b. Heatmap of chr7 and chrX, with sample annotation
To simplify our heatmap, we may wish to present the data as a simple average of our subclones, rather than plotting every individual cell in those subclones. Whenever the heatmap is the result of grouped data, the right side of the plot will show the number of cells described by each group.
#If using a grouping method, the way to supply annotation colors changes.
#In this case, they can simply be supplied as a character vector
annotation.colors <- c("red","blue","green","yellow")
heatmap.result <- scCNVis::makeSCHeatmap(obj,
group = "Subclone",
annotation.colors = annotation.colors,
heatmap.colors = heatmap.colors)
Example 1c. Heatmap with pre-specified clusters
makeSCHeatmap allows us to identify secondary grouping.
For example, if our data included cells from two samples, we can display
what percentage of our subclones were derived from each sample. Since
our example data is all from a single sample, let us add fake sample
labels as metadata and perform secondary grouping on those fake sample
names.
Furthermore, makeSCHeatmap() accepts custom functions
that can be applied to the plot data. The custom function should accept
an input data.frame and return a data.frame
with the same dimensions and the same row/column order. Column names and
row names should not be altered. For visual clarity, let’s add a
function that changes all values within 1 standard deviation of the mean
to be the mean, so we can focus on whatever strong signals may be
present in our data.
#Generate fake sample names and add it to our scCNVisObject
ncells <- nrow(obj@Matrix) #number of cells
fake.sample.names <- c("FakeSampleA", "FakeSampleB")
set.seed(42)
fake.samples <- sample(fake.sample.names, size = ncells, replace = TRUE)
obj <- scCNVis::addMetaData(obj, fake.samples, colname = "FakeSampleNames")
#Custom function that takes all values within 1 standard deviation of the mean
#and changes them to the mean, to minimize noise in the heatmap
custom.func <- function(data) {
sd.value <- sd(data, na.rm = TRUE)
mean.value <- mean(data, na.rm = TRUE)
data[data > mean.value - sd.value & data < mean.value + sd.value] <- mean.value
return(data)
}
heatmap.result <- scCNVis::makeSCHeatmap(obj,
group = "Subclone",
secondary.group = "FakeSampleNames",
post.plot.mod.fun = custom.func,
heatmap.colors = heatmap.colors,
annotation.colors = annotation.colors,
border = TRUE)
Example 1d. Grouped heatmap with secondary group info