From 75a04592d7b7154240b2a11f889d3da4c7a22126 Mon Sep 17 00:00:00 2001 From: nahid18 Date: Sun, 16 Jun 2024 23:33:58 +0600 Subject: [PATCH] feat: clonal prop and plot --- NAMESPACE | 1 + R/plot_clonal_prop_per_sample.R | 47 ++++++++++++++++++++++++++++++ R/plot_motif_counts.R | 8 +++-- README.md | 4 +++ man/plot_clonal_prop_per_sample.Rd | 26 +++++++++++++++++ man/plot_motif_counts.Rd | 2 +- 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 R/plot_clonal_prop_per_sample.R create mode 100644 man/plot_clonal_prop_per_sample.Rd diff --git a/NAMESPACE b/NAMESPACE index 4f6ed56..dc5f292 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export("%>%") export(get_study) +export(plot_clonal_prop_per_sample) export(plot_motif_counts) exportClasses(Basic) exportClasses(Clonality) diff --git a/R/plot_clonal_prop_per_sample.R b/R/plot_clonal_prop_per_sample.R new file mode 100644 index 0000000..afd6b94 --- /dev/null +++ b/R/plot_clonal_prop_per_sample.R @@ -0,0 +1,47 @@ +#' Plot Clonal Proportion per Sample +#' +#' This function plots the clonal proportion per sample. +#' +#' @param data A data frame containing the clonal proportion per sample. +#' @param attr_col Character. Attribute column name. +#' +#' @return A ggplot2 object representing the clonal proportion per sample plot. +#' @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) +} diff --git a/R/plot_motif_counts.R b/R/plot_motif_counts.R index d87b54e..773ff46 100644 --- a/R/plot_motif_counts.R +++ b/R/plot_motif_counts.R @@ -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) { @@ -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( diff --git a/README.md b/README.md index a3a338a..3519aea 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,10 @@ print(proj@clonality@pielou) # clonal proportion print(proj@clonality@clonal_prop) +# plot clonal proportion per sample +clonal_prop <- proj@clonality@clonal_prop +reTCR::plot_clonal_prop_per_sample(clonal_prop, "cmv_status") + # relative abundance (in all clonotypes) print(proj@clonality@abundance) diff --git a/man/plot_clonal_prop_per_sample.Rd b/man/plot_clonal_prop_per_sample.Rd new file mode 100644 index 0000000..8d331b8 --- /dev/null +++ b/man/plot_clonal_prop_per_sample.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_clonal_prop_per_sample.R +\name{plot_clonal_prop_per_sample} +\alias{plot_clonal_prop_per_sample} +\title{Plot Clonal Proportion per Sample} +\usage{ +plot_clonal_prop_per_sample(data, attr_col) +} +\arguments{ +\item{data}{A data frame containing the clonal proportion per sample.} + +\item{attr_col}{Character. Attribute column name.} +} +\value{ +A ggplot2 object representing the clonal proportion per sample plot. +} +\description{ +This function plots the clonal proportion per sample. +} +\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") +} +} diff --git a/man/plot_motif_counts.Rd b/man/plot_motif_counts.Rd index a16683b..107c80b 100644 --- a/man/plot_motif_counts.Rd +++ b/man/plot_motif_counts.Rd @@ -19,7 +19,7 @@ This function plots the motif counts. } \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) } }