Skip to content

Commit

Permalink
feat: reads group abundance
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid18 committed Jun 16, 2024
1 parent 5a31fcc commit face0ab
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 13 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ export("%>%")
export(get_study)
export(plot_clonal_prop_per_group)
export(plot_clonal_prop_per_sample)
export(plot_clonotype_abundance)
export(plot_motif_counts)
export(plot_relative_abundance)
export(plot_reads_group_abundance)
exportClasses(Basic)
exportClasses(Clonality)
exportClasses(Diversity)
Expand Down
11 changes: 6 additions & 5 deletions R/plot_relative_abundance.R → R/plot_clonotype_abundance.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#'
#' @examples
#' \dontrun{
#' proj <- reTCR::get_study(id = "PRJNA473147", attr_col = "cmv_status")
#' reTCR::plot_relative_abundance(proj@clonality@abundance)
#' proj <- reTCR::get_study(id="PRJNA473147", attr_col="cmv_status")
#' reTCR::plot_clonotype_abundance(proj@clonality@abundance)
#' }
plot_relative_abundance <- function(data) {
label_order <- c("Hyperexpanded", "Large", "Medium", "Small", "Rare")
plot_clonotype_abundance <- function(data) {
label_order <- unique(data$clonotype_group)
colors <- c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00")

ggplot2::ggplot(
data,
Expand All @@ -26,7 +27,7 @@ plot_relative_abundance <- function(data) {
color = NA
) +
ggplot2::scale_fill_manual(
values = c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00"),
values = colors,
breaks = label_order
) +
ggplot2::labs(x = "sample", y = "clonotype frequency") +
Expand Down
63 changes: 63 additions & 0 deletions R/plot_reads_group_abundance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' 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.
#' @param attr_col Character. Attribute column name.
#'
#' @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 <- c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00")

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))
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,14 @@ reTCR::plot_clonal_prop_per_group(clonal_data, "cmv_status")
print(proj@clonality@abundance)

# plot relative abundance in all clonotypes
reTCR::plot_relative_abundance(proj@clonality@abundance)
reTCR::plot_clonotype_abundance(proj@clonality@abundance)

# relative abundance (in top 100 clonotypes)
print(proj@clonality@abundance_top)

# plot reads group abbundance in top 100 clonotypes
reTCR::plot_reads_group_abundance(proj@clonality@abundance_top)

# relative abundance (in rare clonotypes)
print(proj@clonality@abundance_rare)
```

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/plot_reads_group_abundance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit face0ab

Please sign in to comment.