-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
#' Calculate the clonal diversities. | ||
#' | ||
#' @keywords internal | ||
#' @importFrom utils getFromNamespace | ||
#' @importFrom dplyr slice_sample | ||
ClonalDiversity <- function( | ||
input.data, cloneCall = "gene", chain = "both", | ||
method = c("shannon", "gini.coeff", "inv.simpson", "norm.entropy", "gini.simpson", "chao1", "ACE"), | ||
group_by = NULL, n_boots = 100) { | ||
method <- match.arg(method) | ||
if (method == "gini.coeff") { | ||
div_fn <- function(d) { | ||
n <- length(d) | ||
1 / n * (n + 1 - 2 * sum((n + 1 - 1:n) * d) / sum(d)) | ||
} | ||
} else { | ||
div_fn <- getFromNamespace(paste0(".", method), "scRepertoire") | ||
} | ||
|
||
is_seurat_object <- getFromNamespace("is_seurat_object", "scRepertoire") | ||
is_se_object <- getFromNamespace("is_se_object", "scRepertoire") | ||
.data.wrangle <- getFromNamespace(".data.wrangle", "scRepertoire") | ||
.theCall <- getFromNamespace(".theCall", "scRepertoire") | ||
.groupList <- getFromNamespace(".groupList", "scRepertoire") | ||
.short.check <- getFromNamespace(".short.check", "scRepertoire") | ||
|
||
sco <- is_seurat_object(input.data) | is_se_object(input.data) | ||
input.data <- .data.wrangle(input.data, group_by, .theCall(input.data, | ||
cloneCall, | ||
check.df = FALSE | ||
), chain) | ||
cloneCall <- .theCall(input.data, cloneCall) | ||
mat <- NULL | ||
sample <- c() | ||
if (!is.null(group_by) & !sco) { | ||
input.data <- .groupList(input.data, group_by) | ||
} | ||
min <- .short.check(input.data, cloneCall) | ||
for (i in seq_along(input.data)) { | ||
data <- as.data.frame(table(input.data[[i]][, cloneCall])) | ||
mat_a <- NULL | ||
sample <- c() | ||
if (n_boots > 0) { | ||
sample <- div_fn(data$Freq) | ||
mat_a <- rbind(mat_a, sample) | ||
mat_a[is.na(mat_a)] <- 0 | ||
mat <- rbind(mat, mat_a) | ||
mat <- as.data.frame(mat) | ||
} else { | ||
for (j in seq(seq_len(n_boots))) { | ||
x <- slice_sample(data, n = min) | ||
sample <- div_fn(x$Freq) | ||
mat_a <- rbind(mat_a, sample) | ||
} | ||
mat_a[is.na(mat_a)] <- 0 | ||
mat_b <- colMeans(mat_a) | ||
mat_b <- as.data.frame(t(mat_b)) | ||
mat <- rbind(mat, mat_b) | ||
} | ||
} | ||
if (is.null(group_by)) { | ||
group_by <- "Group" | ||
} | ||
colnames(mat) <- method | ||
mat[, group_by] <- names(input.data) | ||
mat | ||
} | ||
|
||
#' ClonalDiversityPlot | ||
#' | ||
#' Plot the clonal diversities of the samples/groups. | ||
#' | ||
#' @param data The product of [scRepertoire::combineTCR], [scRepertoire::combineTCR], or | ||
#' [scRepertoire::combineExpression]. | ||
#' @param clone_call How to call the clone - VDJC gene (gene), CDR3 nucleotide (nt), | ||
#' CDR3 amino acid (aa), VDJC gene + CDR3 nucleotide (strict) or a custom variable | ||
#' in the data | ||
#' @param chain indicate if both or a specific chain should be used - e.g. "both", | ||
#' "TRA", "TRG", "IGH", "IGL" | ||
#' @param method The method to calculate the diversity. Options are "shannon" (default), | ||
#' "inv.simpson", "norm.entropy", "gini.simpson", "chao1" and "ACE". | ||
#' See [scRepertoire::clonalDiversity] for details. | ||
#' @param plot_type The type of plot. Options are "bar", "box" and "violin". | ||
#' @param position The position adjustment for the bars. Default is "dodge". | ||
#' @param group_by A character vector of column names to group the samples. Default is NULL. | ||
#' @param facet_by A character vector of column names to facet the plots. Default is NULL. | ||
#' @param split_by A character vector of column names to split the plots. Default is NULL. | ||
#' @param xlab The x-axis label. Default is NULL. | ||
#' @param ylab The y-axis label. Default is NULL. | ||
#' @param ... Other arguments passed to the specific plot function. | ||
#' * For "bar", [plotthis::BarPlot] | ||
#' * For "box", [plotthis::BoxPlot] | ||
#' * For "violin", [plotthis::ViolinPlot] | ||
#' @return A ggplot object or a list if `combine` is FALSE | ||
#' @export | ||
#' @importFrom tidyr separate | ||
#' @importFrom scRepertoire clonalDiversity | ||
#' @importFrom plotthis BarPlot BoxPlot ViolinPlot | ||
#' @examples | ||
#' set.seed(8525) | ||
#' data(contig_list, package = "scRepertoire") | ||
#' data <- scRepertoire::combineTCR(contig_list, | ||
#' samples = c("P17B", "P17L", "P18B", "P18L", "P19B","P19L", "P20B", "P20L")) | ||
#' data <- scRepertoire::addVariable(data, | ||
#' variable.name = "Type", | ||
#' variables = rep(c("B", "L"), 4) | ||
#' ) | ||
#' data <- scRepertoire::addVariable(data, | ||
#' variable.name = "Subject", | ||
#' variables = rep(c("P17", "P18", "P19", "P20"), each = 2) | ||
#' ) | ||
#' | ||
#' ClonalDiversityPlot(data) | ||
#' ClonalDiversityPlot(data, group_by = "Type") | ||
#' ClonalDiversityPlot(data, group_by = "Type", plot_type = "box") | ||
#' ClonalDiversityPlot(data, group_by = "Type", plot_type = "violin") | ||
#' ClonalDiversityPlot(data, group_by = "Type", plot_type = "violin", | ||
#' method = "gini.coeff", add_box = TRUE) | ||
ClonalDiversityPlot <- function( | ||
data, clone_call = "gene", chain = "both", | ||
method = c("shannon", "gini.coeff", "inv.simpson", "norm.entropy", "gini.simpson", "chao1", "ACE"), | ||
plot_type = c("bar", "box", "violin"), position = "dodge", | ||
group_by = NULL, facet_by = NULL, split_by = NULL, | ||
xlab = NULL, ylab = NULL, | ||
...) { | ||
method <- match.arg(method) | ||
plot_type <- match.arg(plot_type) | ||
|
||
if (plot_type %in% c("box", "violin") && is.null(group_by)) { | ||
stop("'group_by' must be provided for box/violin ClonalDiversityPlot") | ||
} | ||
all_groupings <- unique(c("Sample", group_by, facet_by, split_by)) | ||
method_name <- switch(method, | ||
shannon = "Shannon Index", | ||
gini.coeff = "Gini Coefficient", | ||
inv.simpson = "Inverse Simpson Index", | ||
norm.entropy = "Normalized Entropy", | ||
gini.simpson = "Gini-Simpson Index", | ||
chao1 = "Chao1 Index", | ||
ACE = "ACE Index" | ||
) | ||
|
||
data <- MergeClonalGroupings(data, all_groupings) | ||
data <- ClonalDiversity(data, | ||
cloneCall = clone_call, chain = chain, method = method, | ||
group_by = ".group" | ||
) | ||
data <- separate(data, ".group", into = all_groupings, sep = " // ") | ||
|
||
if (plot_type == "bar") { | ||
x <- group_by %||% "Sample" | ||
group_by <- if(is.null(group_by)) NULL else "Sample" | ||
BarPlot(data, | ||
x = x, y = method, group_by = group_by, position = position, | ||
xlab = xlab %||% group_by, ylab = ylab %||% method_name, | ||
split_by = split_by, facet_by = facet_by, ... | ||
) | ||
} else if (plot_type == "box") { | ||
BoxPlot(data, | ||
x = group_by, y = method, | ||
xlab = xlab %||% group_by, ylab = ylab %||% method_name, | ||
split_by = split_by, facet_by = facet_by, ... | ||
) | ||
} else if (plot_type == "violin") { | ||
ViolinPlot(data, | ||
x = group_by, y = method, | ||
xlab = xlab %||% group_by, ylab = ylab %||% method_name, | ||
split_by = split_by, facet_by = facet_by, ... | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.