R/coverage_bar_plot.R

Defines functions coverage_bar_plot

Documented in coverage_bar_plot

#' Generates a bar plot of identified residues for a specific protein
#'
#' @description Generates a bar plot of counts of identified residues across fragments
#'     for a specific protein
#'
#' @param PeptideCoverage A peptide_coverage object generate by get_peptide_coverage. Required.
#' @param Interactive A TRUE/FALSE to indicate whether the plot should be interactive or not. Default is FALSE.
#'
#' @examples
#' \dontrun{
#'
#' # Make Peptide Coverage Object
#' PeptideCoverage <- get_peptide_coverage(ScanMetadata = BU_ScanMetadata, ProteinTable = ProteinTable, ProteinID = "SO_0225")
#'
#' # Generate peptide coverage plot
#' coverage_bar_plot(PeptideCoverage)
#' coverage_bar_plot(PeptideCoverage, Interactive = TRUE)
#'
#' }
#' @export
coverage_bar_plot <- function(PeptideCoverage,
                              Interactive = FALSE) {


  ##################
  ## CHECK INPUTS ##
  ##################

  # Assert that PeptideCoverage is a peptide coverage object
  if ("peptide_coverage" %in% class(PeptideCoverage) == FALSE) {
    stop("PeptideCoverage must be an object of the peptide_coverage class generated by get_peptide_coverage.")
  }

  # Assert that Interactive is a single logical
  if (is.logical(Interactive) == FALSE || length(Interactive) > 1) {
    stop("Interactive must be a single logical value TRUE or FALSE.")
  }
  if (is.na(Interactive)) {Interactive <- FALSE}

  ###############
  ## MAKE PLOT ##
  ###############

  # Pull data table needed
  Residues <- PeptideCoverage$ResidueCount

  # Generate plot
  BarPlot <- ggplot2::ggplot(Residues, ggplot2::aes(x = Position, y = Count, label = Residue)) +
    ggplot2::geom_bar(stat = "identity", fill = "black") + ggplot2::theme_bw() +
    ggplot2::xlim(c(1, nchar(PeptideCoverage$PeptidesByPosition[PeptideCoverage$PeptidesByPosition$`Scan Number` == 0, "Sequence"] %>% unlist()))) +
    ggplot2::xlab("Amino Acid Position") + ggplot2::ggtitle(attr(PeptideCoverage, "Protein")) +
    ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))

  # Return interactive or not
  if (Interactive) {
    return(BarPlot %>% plotly::ggplotly())
  } else {
    return(BarPlot)
  }

}
EMSL-Computing/pspecterlib documentation built on Jan. 28, 2024, 8:13 p.m.