Skip to content

Commit

Permalink
Merge pull request #157 from uclahs-cds/hwinata-clone-genome-snv-count
Browse files Browse the repository at this point in the history
Clone genome snv count
  • Loading branch information
whelena authored Nov 13, 2024
2 parents 97210b4 + 7a9cfa0 commit 030bb7c
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 26 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Add parameters to specify polygon shape and width.
* Add option to use scale bars instead of y-axes.
* Wrapper function for `SRCgrob` to automatically save plots to file
* * Add option to annotate the CCF summary heatmap with the cell values.

## Update
* Fixed angle calculation bug where child angles do not follow
Expand All @@ -23,6 +24,7 @@
* Set default parameters for heatmaps, defaulting to BPG defaults unless necessary
* Improved default node style
* Define color scheme with a single value, automatically generating other shades.
* Report the number of SNVs per clone in the legend for the clone-genome distribution plot.

## Bug
* Resolved issue where the spread parameter was not applied in dendrogram mode.
Expand Down
2 changes: 1 addition & 1 deletion R/SRCGrob.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ SRCGrob <- function(

node.col <- 'white';
if (length(colour.scheme) == 1) {
colour.scheme <- BoutrosLab.plotting.general::colour.gradient(colour.scheme, nrow(tree));
colour.scheme <- c(NA, BoutrosLab.plotting.general::colour.gradient(colour.scheme, nrow(tree)));
}

inputs <- prep.tree(
Expand Down
2 changes: 1 addition & 1 deletion R/create.ccf.heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ create.ccf.heatmap <- function(
clustering.method = 'complete',
distance.method = 'euclidean',
xaxis.lab = '',
xlab.label = 'Mutations',
xlab.label = 'SNVs',
print.colour.key = FALSE,
colour.scheme = c('white', 'blue'),
...
Expand Down
21 changes: 16 additions & 5 deletions R/create.ccf.summary.heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ create.ccf.summary.heatmap <- function(
legend.x = 0.9,
legend.y = 0.8,
plot.objects.heights = c(0.3, 1),
add.median.text = FALSE,
...
) {

Expand Down Expand Up @@ -91,16 +92,16 @@ create.ccf.summary.heatmap <- function(
xat = sample.xaxis$at
);

hm <- BoutrosLab.plotting.general::create.heatmap(
hm.args <- list(
x = arr,
cluster.dimensions = 'none',
xlab.label = 'Clone ID',
xlab.label = 'Clone',
xlab.cex = ifelse(is.null(clone.colours), subplot.xlab.cex, 0),
xaxis.lab = rownames(arr),
xaxis.cex = ifelse(is.null(clone.colours), subplot.xaxis.cex, 0),
xaxis.fontface = subplot.xaxis.fontface,
xaxis.rot = hm.xaxis.rot,
ylab.label = 'Sample ID',
ylab.label = 'Sample',
ylab.cex = subplot.ylab.cex,
yaxis.lab = colnames(arr),
yaxis.cex = subplot.yaxis.cex,
Expand All @@ -109,11 +110,21 @@ create.ccf.summary.heatmap <- function(
colour.scheme = hm.col.scheme
);

if (add.median.text) {
contrast.thres <- (diff(range(arr)) * 0.8) + min(arr);
hm.args$cell.text <- round(arr[arr > 0], 2);
hm.args$row.pos <- which(arr > 0, arr.ind = TRUE)[,2];
hm.args$col.pos <- which(arr > 0, arr.ind = TRUE)[,1];
hm.args$text.col <- ifelse(arr > contrast.thres, 'white', 'black');
}

hm <- do.call(BoutrosLab.plotting.general::create.heatmap, hm.args);

legend.ccf <- BoutrosLab.plotting.general::legend.grob(
list(
legend = list(
title = 'CCF',
labels = c(min(arr), rep('', legend.size), max(arr)),
labels = c(signif(min(arr), 2), rep('', legend.size), signif(max(arr), 2)),
colours = c('white', 'blue'),
border = 'black',
continuous = TRUE,
Expand All @@ -128,7 +139,7 @@ create.ccf.summary.heatmap <- function(
if (!is.null(clone.colours)) {
clone.cov <- BoutrosLab.plotting.general::create.heatmap(
x = t(clone.colours[rownames(arr)]),
xlab.label = 'Clone ID',
xlab.label = 'Clone',
xlab.cex = subplot.xlab.cex,
xaxis.lab = rownames(arr),
xaxis.cex = subplot.xaxis.cex,
Expand Down
23 changes: 15 additions & 8 deletions R/create.clone.genome.distribution.plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ create.clone.genome.distribution.plot <- function(
stop('ID column must contain sample ID if multi.sample is TRUE');
}
# filename must be a directory
if (!dir.exists(filename)) {
if (!is.null(filename) && !dir.exists(filename)) {
stop('filename must be a directory if multi.sample is TRUE');
}
} else {
if (dir.exists(filename)) {
if (!is.null(filename) && dir.exists(filename)) {
stop('filename must be a path (not a directory) if multi.sample is FALSE');
}
snv.df$ID <- 'all';
Expand All @@ -46,29 +46,31 @@ create.clone.genome.distribution.plot <- function(
chr.info <- genome.pos.df$chr.info;
chr.info$xat <- (chr.info$length / 2) + chr.info$start;

plt.list <- list();
for (s in unique(snv.df$ID)) {
# Iterate through each sample -------------------------------------------------------------
print(paste('Plotting clone distribution across the genome for sample:', s));

sample.df <- droplevels(snv.df[snv.df$ID == s, ]);
sample.df <- unique(sample.df[, c('clone.id', 'genome.pos', 'SNV.id', 'ID')]);
if (multi.sample & !is.null(filename)) {
if (!is.null(filename) && multi.sample) {
save.plt <- file.path(filename, paste0(s, '.png'));
} else {
save.plt <- filename;
}

plt <- create.clone.genome.distribution.plot.per.sample(
plt.list[[s]] <- create.clone.genome.distribution.plot.per.sample(
sample.df,
clone.colours[levels(sample.df$clone.id)],
chr.info,
save.plt = ifelse(is.null(filename), NULL, save.plt),
save.plt = save.plt,
alpha = alpha,
legend.x = legend.x,
legend.y = legend.y,
...
);
}
return(plt.list);
}

create.clone.genome.distribution.plot.per.sample <- function(
Expand Down Expand Up @@ -98,7 +100,7 @@ create.clone.genome.distribution.plot.per.sample <- function(
# calculate densities for each cluster --------------------------------------------------------
density.list <- list();
for (k in unique(sample.df$clone.id)) {
if (sum(sample.df$clone.id == k) <= 1) {
if (sum(sample.df$clone.id == k, na.rm = TRUE) <= 1) {
warning(paste('Skipping clone', k, 'in sample', unique(sample.df$ID), 'since there is only one SNV'));
next;
}
Expand All @@ -109,17 +111,22 @@ create.clone.genome.distribution.plot.per.sample <- function(
density.df <- do.call(rbind, density.list);

# get plot legend -----------------------------------------------------------------------------
legend.label <- sapply(names(clone.colours), function(k) {
nsnv <- length(unique(sample.df[sample.df$clone.id == k, ]$SNV.id));
return(paste0(k, ' (', nsnv, ')'));
});
clone.colours <- clone.colours[levels(sample.df$clone.id)];
cluster.legend <- BoutrosLab.plotting.general::legend.grob(
list(
legend = list(
title = 'Clone ID',
labels = names(clone.colours),
title = 'Clone (SNVs)',
labels = legend.label[names(clone.colours)],
colours = c(clone.colours),
border = 'black'
)
),
size = legend.size,
title.just = 'left',
title.cex = legend.title.cex,
label.cex = legend.label.cex
);
Expand Down
4 changes: 1 addition & 3 deletions R/create.clone.genome.distribution.scatterplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ create.clone.genome.distribution.scatterplot <- function(
filename = save.plt,
formula = clone.id ~ genome.pos,
data = scatter.df,
xlab.top.lab = paste0('Total SNVs: ', nsnv),
xlab.top.y = 0.5,
ylab.label = 'Clone ID',
ylab.label = 'Clone',
xaxis.lab = chr.info$chr,
yaxis.lab = levels(scatter.df$clone.id),
xat = chr.info$xat,
Expand Down
7 changes: 4 additions & 3 deletions R/create.cluster.heatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ create.cluster.heatmap <- function(
legend.title.cex = 1.2,
legend.label.cex = 1,
filename = NULL,
xlab.label = 'SNVs',
xlab.cex = 1.2,
xaxis.cex = 1,
xaxis.fontface = 'bold',
Expand Down Expand Up @@ -54,7 +55,7 @@ create.cluster.heatmap <- function(

cov <- BoutrosLab.plotting.general::create.heatmap(
x = t(clone.colours[snv.order$clone.id]),
xlab.label = 'Mutations',
xlab.label = xlab.label,
xlab.cex = xlab.cex,
xaxis.cex = xaxis.cex,
xaxis.fontface = xaxis.fontface,
Expand All @@ -69,14 +70,14 @@ create.cluster.heatmap <- function(
list(
legend = list(
title = 'CCF',
labels = c(min(arr), rep('', legend.size), max(arr)),
labels = c(signif(min(arr), 2), rep('', legend.size), signif(max(arr), 2)),
colours = colour.scheme,
border = 'black',
continuous = TRUE,
cex = legend.label.cex
),
legend = list(
title = 'Clone ID',
title = 'Clone',
labels = names(clone.colours),
colours = clone.colours,
border = 'black',
Expand Down
6 changes: 3 additions & 3 deletions man/create.ccf.heatmap.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ create.ccf.heatmap(
clustering.method = 'complete',
distance.method = 'euclidean',
xaxis.lab = '',
xlab.label = 'Mutations',
xlab.label = 'SNVs',
print.colour.key = FALSE,
colour.scheme = c('white', 'blue'),
...
Expand All @@ -23,11 +23,11 @@ create.ccf.heatmap(
\item{clustering.method}{Defaults to \dQuote{complete}.}
\item{distance.method}{Defaults to \dQuote{euclidean}.}
\item{xaxis.lab}{Defaults to an empty string.}
\item{xlab.label}{Defaults to \dQuote{Mutations}.}
\item{xlab.label}{Defaults to \dQuote{SNVs}.}
\item{print.colour.key}{Defaults to \code{FALSE}.}
\item{colour.scheme}{Defaults to \code{c('white', 'blue')}.}
\item{...}{Pass through argument. See BoutrosLab.plotting.general::create.heatmap() for further details.}
}
\value{A `grob` object of the heatmap.}
\author{Helena Winata}
\seealso{\code{\link[BoutrosLab.plotting.general]{create.heatmap}}}
\seealso{\code{\link[BoutrosLab.plotting.general]{create.heatmap}}}
4 changes: 3 additions & 1 deletion man/create.ccf.summary.heatmap.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ create.ccf.summary.heatmap(
legend.x = 0.9,
legend.y = 0.8,
plot.objects.heights = c(0.3, 1),
add.median.text = FALSE,
...
);
}
Expand All @@ -53,9 +54,10 @@ create.ccf.summary.heatmap(
\item{legend.x}{Legend parameter. Defaults to 0.9}
\item{legend.y}{Legend parameter. Defaults to 0.8}
\item{plot.objects.heights}{If \code{clone.colours} is specified, this must be an array of length 3. Defaults to \code{c(0.3, 1)}. See BoutrosLab.plotting.general::create.multipanelplot()}
\item{add.median.text}{If \code{TRUE}, median CCF values will be added to the heatmap. Defaults to \code{FALSE}}
\item{...}{Pass through argument. See BoutrosLab.plotting.general::create.multipanelplot() for further details.}

}
\value{A `grob` object of the summary plot.}
\author{Helena Winata}
\seealso{\code{\link[BoutrosLab.plotting.general]{legend.grob}}, \code{\link[BoutrosLab.plotting.general]{create.multipanelplot}}, \code{\link[BoutrosLab.plotting.general]{create.heatmap}}}
\seealso{\code{\link[BoutrosLab.plotting.general]{legend.grob}}, \code{\link[BoutrosLab.plotting.general]{create.multipanelplot}}, \code{\link[BoutrosLab.plotting.general]{create.heatmap}}}
4 changes: 3 additions & 1 deletion man/create.cluster.heatmap.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ create.cluster.heatmap(
legend.title.cex = 1.2,
legend.label.cex = 1,
filename = NULL,
xlab.label = 'SNVs',
xlab.cex = 1.2,
xaxis.cex = 1,
xaxis.fontface = 'bold',
Expand All @@ -35,6 +36,7 @@ create.cluster.heatmap(
\item{legend.title.cex}{Size of titles in the legends. Defaults to 1.2}
\item{legend.label.cex}{Size of text labels in the legends. Defaults to 1}
\item{filename}{Filename for tiff output, or if NULL returns the trellis object itself. Defaults to \code{NULL}.}
\item{xlab.label}{Defaults to \dQuote{SNVs}.}
\item{xlab.cex}{Defaults to 1.2}
\item{xaxis.cex}{Defaults to 1}
\item{xaxis.fontface}{Defaults to \dQuote{bold}.}
Expand All @@ -44,4 +46,4 @@ create.cluster.heatmap(
}
\value{A `grob` object of the heatmap.}
\author{Helena Winata}
\seealso{\code{\link{create.ccf.heatmap}}, \code{\link[BoutrosLab.plotting.general]{create.heatmap}}}
\seealso{\code{\link{create.ccf.heatmap}}, \code{\link[BoutrosLab.plotting.general]{create.heatmap}}}

0 comments on commit 030bb7c

Please sign in to comment.