Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
EllaSchwab authored Jun 17, 2024
2 parents 223858f + 8c67f3d commit 0d789ba
Show file tree
Hide file tree
Showing 29 changed files with 895 additions and 53 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Imports:
rlang,
ggplot2,
dplyr,
purrr,
stringr,
methods,
utils,
RColorBrewer,
magrittr
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

export("%>%")
export(get_study)
export(plot_reads_group_abundance)
export(plot_clonal_prop_per_sample)
export(plot_clonal_prop_per_group)
export(plot_clonotype_abundance)
export(get_bottom_n_clonotypes)
export(get_top_n_clonotypes)
export(plot_diversity_index)
export(plot_motif_counts)
exportClasses(Basic)
exportClasses(Clonality)
exportClasses(Diversity)
exportClasses(Motif)
exportClasses(reTCRProj)
Expand Down
13 changes: 9 additions & 4 deletions R/get_study.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source("R/utils-basic.R")
source("R/utils-diversity.R")
source("R/utils-clonality.R")
source("R/utils-basic.R")
source("R/utils-hill.R")

#' Get pyTCR data of a project
#'
Expand All @@ -12,9 +14,8 @@ source("R/utils-diversity.R")
#'
#' @examples
#' \dontrun{
#' proj <- reTCR::get_study(id="PRJNA473147", attr_col="cmv_status")
#' proj <- reTCR::get_study(id = "PRJNA473147", attr_col = "cmv_status")
#' }

get_study <- function(id, attr_col) {
stopifnot(is.character(id) && nchar(id) > 0)
filename <- paste0(id, "_mixcr_metadata_file.csv")
Expand All @@ -23,11 +24,15 @@ get_study <- function(id, attr_col) {

basic <- .get_basic(data = data, attr_col = attr_col)
diversity <- .get_diversity(data = data, attr_col = attr_col)
clonality <- .get_clonality(data = data, attr_col = attr_col)
hill <- .get_hill_numbers(df = data)

return(methods::new(
"reTCRProj",
data = data,
basic = basic,
diversity = diversity
diversity = diversity,
clonality = clonality,
hill = hill
))
}
47 changes: 47 additions & 0 deletions R/plot_clonal_prop_per_group.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Plot clonal proportion per group
#'
#' This function plots the clonal proportion per group.
#'
#' @param data A data frame of clonal proportion per group.
#' @param attr_col Character. Attribute column name.
#'
#' @return A ggplot2 plot of clonal proportion per group.
#' @export
#'
#' @examples
#' \dontrun{
#' proj <- reTCR::get_study(id = "PRJNA473147", attr_col = "cmv_status")
#' clonal_prop <- proj@clonality@clonal_prop
#' reTCR::plot_clonal_prop_per_group(clonal_prop, "cmv_status")
#' }
plot_clonal_prop_per_group <- function(data, attr_col) {
data[[attr_col]] <- as.factor(data[[attr_col]])
ggplot2::ggplot(
data,
ggplot2::aes_string(
x = attr_col,
y = "clonality_portion"
)
) +
ggplot2::geom_violin(trim = FALSE, fill = "skyblue", alpha = 0.5) +
ggplot2::geom_boxplot(outlier.shape = NA, width = 0.1, alpha = 0.7) +
ggplot2::geom_jitter(width = 0, height = 0.1, size = 1.5, alpha = 0.6) +
ggplot2::theme_classic() +
ggplot2::labs(x = attr_col, y = "number of clonotypes") +
ggplot2::theme(
axis.text.x = ggplot2::element_text(
angle = 90, vjust = 0.5, hjust = 1, size = 10
),
axis.text.y = ggplot2::element_text(
size = 12,
margin = ggplot2::margin(l = 10)
),
axis.title.x = ggplot2::element_text(
size = 14,
margin = ggplot2::margin(t = 10)
),
axis.title.y = ggplot2::element_text(size = 14),
legend.title = ggplot2::element_text(size = 14),
legend.text = ggplot2::element_text(size = 12)
)
}
47 changes: 47 additions & 0 deletions R/plot_clonal_prop_per_sample.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Plot clonal proportion per sample
#'
#' This function plots the clonal proportion per sample.
#'
#' @param data A data frame of clonal proportion per sample.
#' @param attr_col Character. Attribute column name.
#'
#' @return A ggplot2 plot of clonal proportion per sample.
#' @export
#'
#' @examples
#' \dontrun{
#' proj <- reTCR::get_study(id="PRJNA473147", attr_col="cmv_status")
#' clonal_prop <- proj@clonality@clonal_prop
#' reTCR::plot_clonal_prop_per_sample(clonal_prop, "cmv_status")
#' }
plot_clonal_prop_per_sample <- function(data, attr_col) {
ggplot2::ggplot(
data,
ggplot2::aes(
x = sample,
y = clonality_portion,
fill = !!rlang::sym(attr_col)
)
) +
ggplot2::geom_bar(stat = "identity", position = ggplot2::position_dodge()) +
ggplot2::labs(x = "sample", y = "number of clonotypes") +
ggplot2::theme_classic() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(
angle = 90, vjust = 0.5, hjust = 1, size = 10
),
axis.text.y = ggplot2::element_text(
size = 12,
margin = ggplot2::margin(l = 10)
),
axis.title.x = ggplot2::element_text(
size = 14,
margin = ggplot2::margin(t = 10)
),
axis.title.y = ggplot2::element_text(size = 14),
legend.title = ggplot2::element_text(size = 14),
legend.text = ggplot2::element_text(size = 12)
) +
ggplot2::scale_y_continuous(expand = c(0, 0)) +
ggplot2::scale_fill_brewer(palette = "Set1", name = attr_col)
}
60 changes: 60 additions & 0 deletions R/plot_clonotype_abundance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' Plot relative abundance for all clonotypes
#'
#' This function plots the relative abundance for all clonotypes.
#'
#' @param data A data frame of relative abundance for all clonotypes.
#'
#' @return A ggplot2 plot of relative abundance for all clonotypes.
#' @export
#'
#' @examples
#' \dontrun{
#' proj <- reTCR::get_study(id="PRJNA473147", attr_col="cmv_status")
#' reTCR::plot_clonotype_abundance(proj@clonality@abundance)
#' }
plot_clonotype_abundance <- function(data) {
label_order <- unique(data$clonotype_group)
colors <- RColorBrewer::brewer.pal(n = length(label_order), name = "Set1")

ggplot2::ggplot(
data,
ggplot2::aes(x = sample, y = relative_abundance, fill = clonotype_group)
) +
ggplot2::geom_bar(
stat = "identity",
width = 0.5,
color = NA
) +
ggplot2::scale_fill_manual(
values = colors,
breaks = label_order
) +
ggplot2::labs(x = "sample", y = "clonotype frequency") +
ggplot2::theme_minimal() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(
angle = 90,
vjust = 0.5,
hjust = 1,
size = 10
),
axis.text.y = ggplot2::element_text(size = 10),
axis.title = ggplot2::element_text(size = 14),
legend.title = ggplot2::element_blank(),
legend.text = ggplot2::element_text(size = 12),
legend.position = "right",
panel.grid.major = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank(),
axis.line = ggplot2::element_line(color = "black"),
axis.text = ggplot2::element_text(margin = ggplot2::margin(t = 10)),
axis.ticks = ggplot2::element_line(color = "black", linewidth = 0.3),
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(size = 12)
) +
ggplot2::scale_x_discrete(
labels = function(x) {
stringr::str_wrap(x, width = 10)
}
) +
ggplot2::scale_y_continuous(expand = c(0, 0))
}
76 changes: 76 additions & 0 deletions R/plot_diversity_index.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#' Plot diversity index metrics
#'
#' This function plots the diversity index metrics.
#'
#' @param data A data frame of diversity index metrics.
#' @param index Character. Diversity index metric.
#' @param attr_col Character. Attribute column name.
#'
#' @return A ggplot2 plot of diversity index metrics.
#' @export
#'
#' @examples
#' \dontrun{
#' proj <- reTCR::get_study(id = "PRJNA473147", attr_col = "cmv_status")
#' reTCR::plot_diversity_index(
#' proj@diversity@shannon,
#' "shannon_wiener_index",
#' "cmv_status"
#' )
#' }
plot_diversity_index <- function(data, index, attr_col) {
label_order <- unique(data[[attr_col]])
colors <- RColorBrewer::brewer.pal(n = 3, name = "Set1")[c(2, 1)]

df_summary <- data %>%
dplyr::group_by(.data[[attr_col]]) %>%
dplyr::summarize(
mean_index = mean(.data[[index]], na.rm = TRUE),
sem_index = stats::sd(.data[[index]], na.rm = TRUE) / sqrt(dplyr::n())
)

ggplot2::ggplot(
df_summary,
ggplot2::aes(
x = .data[[attr_col]],
y = .data$mean_index,
fill = .data[[attr_col]]
)
) +
ggplot2::geom_bar(position = "dodge", stat = "identity") +
ggplot2::geom_errorbar(
ggplot2::aes(
ymin = .data$mean_index - .data$sem_index,
ymax = .data$mean_index + .data$sem_index
),
position = ggplot2::position_dodge(0.9),
width = 0.2
) +
ggplot2::scale_fill_manual(
values = colors,
breaks = label_order,
name = attr_col
) +
ggplot2::labs(x = attr_col, y = index) +
ggplot2::theme_minimal() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(
angle = 90, vjust = 0.5, hjust = 1, size = 10
),
axis.text.y = ggplot2::element_text(size = 10),
axis.title = ggplot2::element_text(size = 14),
legend.title = ggplot2::element_blank(),
legend.text = ggplot2::element_text(size = 12),
legend.position = "right",
panel.grid.major = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank(),
axis.line = ggplot2::element_line(color = "black"),
axis.text = ggplot2::element_text(margin = ggplot2::margin(t = 10)),
axis.ticks = ggplot2::element_line(color = "black", size = 0.3),
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(size = 12)
) +
ggplot2::scale_x_discrete(
labels = function(x) stringr::str_wrap(x, width = 10)
)
}
8 changes: 6 additions & 2 deletions R/plot_motif_counts.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#'
#' @examples
#' \dontrun{
#' proj <- reTCR::get_study(id = "PRJNA473147")
#' proj <- reTCR::get_study(id="PRJNA473147", attr_col="cmv_status")
#' reTCR::plot_motif_counts(proj@motif@aa_motif_count, 20)
#' }
plot_motif_counts <- function(data, threshold = 60) {
Expand All @@ -29,7 +29,11 @@ plot_motif_counts <- function(data, threshold = 60) {
)
) +
ggplot2::geom_jitter(width = 0.2, size = 3) +
ggplot2::labs(x = "Amino acid motif", y = "Count", color = "CMV Status") +
ggplot2::labs(
x = "Amino acid motif",
y = "Count",
color = "CMV Status"
) +
ggplot2::theme_bw() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(
Expand Down
62 changes: 62 additions & 0 deletions R/plot_reads_group_abundance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' Plot reads group abundance for top 100 clonotypes
#'
#' This function plots reads group abundance for top 100 clonotypes.
#'
#' @param data A data frame of relative abundance for top 100 clonotypes.
#'
#' @return A ggplot2 plot of reads group abundance for top 100 clonotypes.
#' @export
#'
#' @examples
#' \dontrun{
#' proj <- reTCR::get_study(id = "PRJNA473147", attr_col = "cmv_status")
#' reTCR::plot_reads_group_abundance(proj@clonality@abundance_top)
#' }
plot_reads_group_abundance <- function(data) {
label_order <- unique(data$reads_group)
colors <- RColorBrewer::brewer.pal(n = length(label_order), name = "Set1")

df_counts <- data %>%
dplyr::filter(reads_group != "None") %>%
dplyr::group_by(sample, reads_group) %>%
dplyr::summarize(count = dplyr::n()) %>%
dplyr::ungroup()

ggplot2::ggplot(
df_counts,
ggplot2::aes(
x = sample,
y = count,
fill = reads_group
)
) +
ggplot2::geom_bar(position = "stack", stat = "identity", width = 0.7) +
ggplot2::scale_fill_manual(
values = colors,
breaks = label_order,
name = "reads_group"
) +
ggplot2::labs(x = "sample", y = "number of clonotypes") +
ggplot2::theme_minimal() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(
angle = 90, vjust = 0.5, hjust = 1, size = 10
),
axis.text.y = ggplot2::element_text(size = 10),
axis.title = ggplot2::element_text(size = 14),
legend.title = ggplot2::element_blank(),
legend.text = ggplot2::element_text(size = 12),
legend.position = "right",
panel.grid.major = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank(),
axis.line = ggplot2::element_line(color = "black"),
axis.text = ggplot2::element_text(margin = ggplot2::margin(t = 10)),
axis.ticks = ggplot2::element_line(color = "black", size = 0.3),
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(size = 12)
) +
ggplot2::scale_x_discrete(
labels = function(x) stringr::str_wrap(x, width = 10)
) +
ggplot2::scale_y_continuous(expand = c(0, 0))
}
Loading

0 comments on commit 0d789ba

Please sign in to comment.