R/plotTrackSingular.R

Defines functions highlightPlot plotTrackSingular

Documented in highlightPlot plotTrackSingular

#' Highlight clones 
#' 
#' Create alluvial plots highlighting each sequence in amino acid list
#' 
#' @param ctable A tibble of productive amino acid sequences to highlight
#' generated by LymphoSeq function cloneTrack
#' @examples 
#' file_path <- system.file("extdata", "TCRB_sequencing", package = "LymphoSeq2")
#' stable <- LymphoSeq2::readImmunoSeq(path = file_path)
#' atable <- LymphoSeq2::productiveSeq(stable, aggregate = "junction_aa")
#' ctable <- LymphoSeq2::cloneTrack(study_table = atable,
#'                                 sample_list = c("TRB_CD8_949", "TRB_CD8_CMV_369"))
#'
#' @return A list of alluvial plots highlighting a single sequence
#' @details The plot is made using the package ggplot2 and can be reformatted
#' using ggplot2 functions.  
#' @export
plotTrackSingular <- function(ctable) {
  alist <- ctable %>% dplyr::pull(junction_aa) %>% unique()
  plots <- alist %>% purrr::map(~highlightPlot(.x, ctable))
  return(plots)
}

#' Highlight specific sequences
#' 
#' @describeIn plotTrackSingular Highligh a specific amino acid sequence
#' 
#' @param aseq CDR3 amino acid sequence to highlight 
#' @param ctable  tibble of productive amino acid sequences to highlight
#' generated by LymphoSeq function cloneTrack
highlightPlot <- function(aseq, ctable) {
  pal_table <- ctable %>%
            select(junction_aa) %>%
            distinct() %>%
            mutate(color = if_else(junction_aa == aseq, "#7570b3", "grey"))
  cpal <- pal_table %>% dplyr::pull(color)
  names(cpal) <- pal_table %>% dplyr::pull(junction_aa)
  tplot <- plotTrack(ctable) +
           scale_fill_manual(values = cpal, breaks = aseq, name = "CDR3 Sequence") +
           theme(legend.position = "bottom")
  return(tplot)
}
elulu3/LymphoSeqTest documentation built on Aug. 27, 2022, 5:47 a.m.