This is an interesting use case - it will not work out of the box, but we can make it happen.
I have created a modified version of the plotMutationSpectrum function that has the extra functionality:
library(SomaticSignatures)
library(ggplot2)
plotMutationSpectrumWithColorMapping <- function(vr, group,
colorby = c("sample", "alteration", "colormapping"),
normalize = TRUE, colormapping = NULL)
{
colorby = match.arg(colorby)
x = motifMatrix(vr, group, normalize = normalize)
w_df = reshape2::melt(x, varnames = c("motif", "sample"))
w_df$colormapping = colormapping[w_df$sample]
w_df$alteration = sub("([ACGTN])([ACGTN]) .+", "\\1>\\2", w_df$motif)
w_df$context = sub("[ACGTN][ACGTN] (.+)", "\\1", w_df$motif)
p = ggplot(w_df)
p = p + geom_bar(aes_string(x = "context", y = "value", fill = colorby),
stat = "identity", position = "identity")
p = p + facet_grid(sample ~ alteration)
p = p + SomaticSignatures:::theme_ss() + SomaticSignatures:::theme_small_axis()
p = p + theme(legend.position = "none")
p = p + scale_fill_brewer(palette = "Set3")
p = p + xlab("Motif") + ylab("Contribution")
return(p)
}
Let's try it with a small example data set:
library(SomaticSignatures)
library(ggplot2)
data(sca_motifs_tiny)
We want to split the data set according to cancer type and additionally distinguish between lung and non-lung cancer via color. For this, we define a mapping between our grouping (cancer studies) and our coloring (lung cancers) variable:
studies = as.character(unique(sca_motifs_tiny$study))
groups = ifelse(grepl("^L", colormapping), "lung", "non-lung")
colormapping = setNames(groups, studies)
KIRC SKCM LUAD LUSC HNSC GBM THCA
"non-lung" "non-lung" "lung" "lung" "non-lung" "non-lung" "non-lung"
OV
"non-lung"
Plotting and coloring the mutational spectrum according to sample/group and alteration type works as before:
plotMutationSpectrumWithColorMapping(sca_motifs_tiny, "study", "sample") + scb
plotMutationSpectrumWithColorMapping(sca_motifs_tiny, "study", "alteration") + scb
Most importantly, we can now use the color mapping defined above:
plotMutationSpectrumWithColorMapping(sca_motifs_tiny, "study", "colormap", colormapping = cm) + scb
This is not perfect, but let me know if this is what you were looking for and if turns out to be helpful.