R/coverage_plot.R

Defines functions coverage_plot

Documented in coverage_plot

#' Generate a plot of peptide coverage for a specific protein
#'
#' @description A plot which shows where identified peptides map to the literature sequence
#'
#' @param PeptideCoverage A peptide_coverage object generate by get_peptide_coverage. Required.
#' @param ColorByScore A character to indicate whether the coverage plot should
#'     should be colored by QValue, Score, or not at all. Acceptable values are "QValue",
#'     "Score", and NULL. Default is NULL.
#' @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_plot(PeptideCoverage)
#' coverage_plot(PeptideCoverage, ColorByScore = "Score", Interactive = TRUE)
#'
#' }
#'
#' @export
coverage_plot <- function(PeptideCoverage,
                          ColorByScore = NULL,
                          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 ColorByScore is NULL, QValue, or Score
  if (is.null(ColorByScore) == FALSE) {
    if (length(ColorByScore) != 1) {stop("ColorByScore should be a single character or NULL.")}
    if (ColorByScore %in% c("QValue", "Score") == FALSE) {stop('Acceptable values for ColorByScore are "QValue" or "Score."')}
  }

  # 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 position dataframe - QValue and ScoreFilters are applied in get_protein table
  PositionDF <- PeptideCoverage$PeptidesByPosition[PeptideCoverage$PeptidesByPosition$`Scan Number` != 0,]

  # Pull ProteinID
  ProteinID <- attributes(PeptideCoverage)$pspecter$ProteinID

  # Add coloring if indicated
  if (is.null(ColorByScore)) {

    # Generate black and white plot
    PositionPlot <- ggplot2::ggplot(PositionDF, ggplot2::aes(x = `Peptide Start Position`,
      xend = `Peptide End Position`, y = `Scan Number`, yend = `Scan Number`, label = Sequence)) +
      ggplot2::geom_segment() + ggplot2::theme_bw() +
      ggplot2::ylim(c(min(PositionDF$`Scan Number` - 5), max(PositionDF$`Scan Number` + 5))) +
      ggplot2::xlim(c(1, nchar(PeptideCoverage$PeptidesByPosition[PeptideCoverage$PeptidesByPosition$`Scan Number` == 0, "Sequence"] %>% unlist()))) +
      ggplot2::xlab("Amino Acid Position") + ggplot2::ggtitle(ProteinID) +
      ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))

  } else {

    if (ColorByScore == "Score" & is.na(PositionDF$Score[1]) == FALSE) {

      # Color by Score
      PositionPlot <- ggplot2::ggplot(PositionDF, ggplot2::aes(x = `Peptide Start Position`,
        xend = `Peptide End Position`, y = `Scan Number`, yend = `Scan Number`, label = Sequence, color = `Score`)) +
        ggplot2::geom_segment() + ggplot2::theme_bw() +
        ggplot2::ylim(c(min(PositionDF$`Scan Number` - 5), max(PositionDF$`Scan Number` + 5))) +
        ggplot2::xlim(c(1, nchar(PeptideCoverage$PeptidesByPosition[PeptideCoverage$PeptidesByPosition$`Scan Number` == 0, "Sequence"] %>% unlist()))) +
        ggplot2::xlab("Amino Acid Position") + ggplot2::ggtitle(ProteinID) +
        ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) +
        ggplot2::scale_color_gradient2(low = "black", mid = "blue", high = "red", midpoint = median(PositionDF$Score))

    } else if (ColorByScore == "QValue" & is.na(PositionDF$`Q Value`[1]) == FALSE) {

      # Color by QValue
      PositionPlot <- ggplot2::ggplot(PositionDF, ggplot2::aes(x = `Peptide Start Position`,
        xend = `Peptide End Position`, y = `Scan Number`, yend = `Scan Number`, label = Sequence, color = `Q Value`)) +
        ggplot2::geom_segment() + ggplot2::theme_bw() +
        ggplot2::ylim(c(min(PositionDF$`Scan Number` - 5), max(PositionDF$`Scan Number` + 5))) +
        ggplot2::xlim(c(1, nchar(PeptideCoverage$PeptidesByPosition[PeptideCoverage$PeptidesByPosition$`Scan Number` == 0, "Sequence"] %>% unlist()))) +
        ggplot2::xlab("Amino Acid Position") + ggplot2::ggtitle(ProteinID) +
        ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) +
        ggplot2::scale_color_gradient2(low = "black", mid = "blue", high = "red", midpoint = median(PositionDF$`Q Value`))

    } else{

      message("NA values detected in ColorByScore. Setting this parameter to NULL.")

      # Generate black and white plot
      PositionPlot <- ggplot2::ggplot(PositionDF, ggplot2::aes(x = `Peptide Start Position`,
        xend = `Peptide End Position`, y = `Scan Number`, yend = `Scan Number`, label = Sequence)) +
        ggplot2::geom_segment() + ggplot2::theme_bw() +
        ggplot2::ylim(c(min(PositionDF$`Scan Number` - 5), max(PositionDF$`Scan Number` + 5))) +
        ggplot2::xlim(c(1, nchar(PeptideCoverage$PeptidesByPosition[PeptideCoverage$PeptidesByPosition$`Scan Number` == 0, "Sequence"] %>% unlist()))) +
        ggplot2::xlab("Amino Acid Position") + ggplot2::ggtitle(ProteinID) +
        ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))

    }

  }

  # Return a plotly if interactive
  if (Interactive) {
    return(PositionPlot %>% plotly::ggplotly())
  } else {
    PositionPlot
  }

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