r paste('#', basename(fileList[[i]]), '{data-navmenu="Results File Name"}')

r htmltools::tags$h3(basename(fileList[[i]]), style="text-align:center;color:#ffffff")

# knitr::opts_knit$set(base.dir = normalizePath(tempdir(), winslash = '/'))

library(flexdashboard)
library(shiny)
library(shinydashboard)
library(magrittr)
library(stringr)
library(forcats)
library(purrr)
library(UpSetR)
library(ggplot2)
library(DT)
library(plotly)
library(ggthemes)
library(viridis)
library(waffle)
library(dplyr)
library(magick)

fraction_number_protein <- 
  results_protein[[i]] %>% 
  dplyr::pull(
    fraction
  ) %>% 
  unique() %>% 
  length()

filename_number_protein <- 
  results_protein[[i]] %>% 
  dplyr::pull(
    filename
  ) %>% 
  unique() %>% 
  length()

# Plotly parameters

download_button_params <- 
  list(
    format = "svg",
    width = 800,
    height = 500
  )

# ggplot themes

col_plot_theme <- 
  list(
    theme_minimal(),
    theme(
      axis.text.x = element_text(angle = 90),
      panel.grid.major.x = element_blank()
    )
  )

heatmap_theme <- 
  list(
    theme_minimal(),
    theme(
      text = element_text(size=16)
    ) 
  )
knitr::asis_output("\n")
knitr::asis_output("Row {data-height:400}\n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("\n### Protein Identifications\n")

results_protein[[i]] %>%
  mutate(filename = sub('(^[^_]+_[^_]+_[^_]+)_(.*)$', '\\1_ \\2', filename)) %>%
  mutate(
    `PROTEIN-NAMES` = 
      stringr::str_trunc(`PROTEIN-NAMES`, 60, "right", ellipsis = "..."
      )
  ) %>% 
  # mutate(GRAVY = formatC(GRAVY, format = "f", digits = 3)) %>% 
  # mutate(MonoisotopicMass = formatC(MonoisotopicMass, format = "f", digits = 4)) %>% 
  # mutate(AverageMass = formatC(AverageMass, format = "f", digits = 4)) %>%
  dplyr::select(
    "UNIPROTKB",
    "PROTEIN-NAMES",
    "fraction",
    "filename",
    everything()
  ) %>%
  dplyr::rename(
    "UniProt Accession" = UNIPROTKB,
    "Protein Name" = `PROTEIN-NAMES`,
    "Fraction" = fraction,
    "Filename" = filename
  ) %>% 
  DT::datatable(
    extensions = c("FixedColumns", "Buttons", "Scroller"),
    class = "hover",
    options = list(
      deferRender = TRUE,
      scrollY = 400,
      scrollX = TRUE,
      # scroller = TRUE,
      columnDefs = list(
        list(
          visible = FALSE,
          targets = c(8:length(results_protein[[i]]))
        ),
        list(
          className = 'dt-center'
        )
      ),
      dom = "Bfrtip",
      fixedColumns = list(leftColumns = 2),
      buttons = c("copy", "csv", "excel", "colvis")
    )
  ) %>% 
  DT::formatStyle(
    1:length(results_protein[[i]]),
    fontSize = "9",
    digits = 3
  )
knitr::asis_output("\n")
knitr::asis_output("Row {data-height:400}\n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("\n### Proteoform Identifications\n")

results_proteoform[[i]] %>%
  mutate(filename = sub('(^[^_]+_[^_]+_[^_]+)_(.*)$', '\\1_ \\2', filename)) %>%
  mutate(
    `PROTEIN-NAMES` = 
      stringr::str_trunc(`PROTEIN-NAMES`, 60, "right", ellipsis = "..."
      )
  ) %>% 
  dplyr::select(
    "UNIPROTKB",
    "PROTEIN-NAMES",
    "HitCount",
    dplyr::any_of(
      c("ProteoformSequence_GRAVY", "IntactProteinSequence_GRAVY")
    ),
    "fraction",
    "filename",
    everything()
  ) %>%
  dplyr::rename(
    "UniProt Accession" = UNIPROTKB,
    "Protein Name" = `PROTEIN-NAMES`,
    "Hit Count" = HitCount,
    "Fraction" = fraction,
    "Filename" = filename
  ) %>% 
  DT::datatable(
    extensions = c("FixedColumns", "Buttons", "Scroller"),
    class = "hover",
    options = list(
      deferRender = TRUE,
      scrollY = 400,
      scrollX = TRUE,
      # scroller = TRUE,
      columnDefs = list(
        list(
          visible = FALSE,
          targets = c(9:length(results_proteoform[[i]]))
        ),
        list(
          className = 'dt-center'
        )
      ),
      dom = "Bfrtip",
      fixedColumns = list(leftColumns = 2),
      buttons = c("copy", "csv", "excel", "colvis")
    )
  ) %>% 
  DT::formatStyle(
    1:length(results_proteoform[[i]]),
    fontSize = "9",
    digits = 3
  )
knitr::asis_output("\n")
knitr::asis_output("Row \n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("\n### Protein Counts by Fraction\n")

plot <- 
  results_protein[[i]] %>% 
  group_by(fraction) %>% 
  summarise("protein count" = n()) %>%
  ggplot() +
  geom_col(
    aes(fraction, `protein count`),
    color = "black",
    fill = "#4C4184",
    size = 0.25
  ) +
  labs(
    x = "Fraction",
    y = "Protein Count"
  ) +
  col_plot_theme

plotly::ggplotly(plot) %>% 
  plotly::config(
    displaylogo = FALSE,
    toImageButtonOptions = download_button_params
  ) %>% 
  plotly::layout(
    xaxis = list(
      tickfont = list(
        size = 12
      )
    )
  )

``` {r eval=!all(is.na(results_protein[[i]])) & filename_number_protein > 1}

knitr::asis_output("\n") knitr::asis_output("Row \n") knitr::asis_output("-------------------------------------\n") knitr::asis_output("\n### Protein Counts by Filename")

plot <- results_protein[[i]] %>% group_by(filename) %>% summarise("protein count" = n()) %>% mutate(filename = sub('(^[^]+[^]+[^]+)(.*)$', '\1_ \2', filename)) %>% mutate(filename = stringr::str_trunc(filename, 50, "center")) %>% # mutate(filename = stringr::str_trunc(filename, 40, side = "left")) %>% ggplot() + geom_col( aes(filename, protein count), color = "black", fill = "#4C4184", size = 0.25 ) + labs( x = "Filename", y = "Protein Count" ) + guides( color = "none", fill = "none" ) + scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 8)) + col_plot_theme

plotly::ggplotly(plot) %>% plotly::config( displaylogo = FALSE, toImageButtonOptions = download_button_params ) %>% plotly::layout( xaxis = list( tickfont = list( size = 8 ) ) )

```r

knitr::asis_output("\n")
knitr::asis_output("Row \n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("\n### Protein Identifications, UpSet Plot\n")

fraction_number_allhits <- 
  results_protein_allhits[[i]] %>% 
  dplyr::pull(
    fraction
  ) %>% 
  unique() %>% 
  length()

if (fraction_number_allhits > 1) {

  results_protein_allhits[[i]] %>% 
    dplyr::select(
      ProteoformRecordNum,
      fraction
    ) %>%
    dplyr::arrange(fraction) %>%
    tidyr::pivot_wider(
      names_from = fraction,
      values_from =
        "ProteoformRecordNum",
      values_fn = list,
      names_prefix = "Frac_"
    ) %>%
    purrr::flatten() %>%
    list() %>%
    rlang::set_names("1") %>% 
    viztools::make_UpSet_plot()

} else {

  pander::pander("Only one fraction, no UpSet plot made") 

}
knitr::asis_output("\n")
knitr::asis_output("Row \n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("\n### Proteoform Identifications, UpSet Plot\n")

fraction_number <- 
  results_protein_allhits[[i]] %>% 
  dplyr::pull(
    fraction
  ) %>% 
  unique() %>% 
  length()

if (fraction_number > 1) {
  results_protein_allhits[[i]] %>% 
    dplyr::select(
      ProteoformRecordNum,
      fraction
    ) %>%
    dplyr::arrange(fraction) %>%
    tidyr::pivot_wider(
      names_from = fraction,
      values_from =
        "ProteoformRecordNum",
      values_fn = list,
      names_prefix = "Frac_"
    ) %>%
    purrr::flatten() %>%
    list() %>%
    rlang::set_names("1") %>% 
    viztools::make_UpSet_plot()

} else {

  pander::pander("Only one fraction, no UpSet plot made") 

}
knitr::asis_output("\n")
knitr::asis_output("Row \n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("\n### Protein Identifications, Intersection Degree Plot\n")

if (fraction_number_protein > 1) {

  results_protein_allhits[[i]] %>% 
    dplyr::select(
      AccessionNumber,
      fraction
    ) %>%
    dplyr::arrange(fraction) %>%
    tidyr::pivot_wider(
      names_from = fraction,
      values_from =
        AccessionNumber,
      values_fn = list,
      names_prefix = "Frac_"
    ) %>%
    purrr::flatten() %>%
    purrr::map(unique) %>% 
    viztools::make_intersection_degree_plot(
      Yrange = c(0, 100),
      plotType = "Protein"
    ) %>% 
    plotly::ggplotly() %>% 
    plotly::config(
      displaylogo = FALSE,
      toImageButtonOptions = download_button_params
    )

} else {

  pander::pander("Only one fraction, no UpSet plot made") 

}
knitr::asis_output("\n")
knitr::asis_output("Row\n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("\n### Proteoform Identifications, Intersection Degree Plot\n")

if (fraction_number_allhits > 1) {

  results_protein_allhits[[i]] %>% 
    dplyr::select(
      ProteoformRecordNum,
      fraction
    ) %>%
    dplyr::arrange(fraction) %>%
    tidyr::pivot_wider(
      names_from = fraction,
      values_from =
        ProteoformRecordNum,
      values_fn = list,
      names_prefix = "Frac_"
    ) %>%
    purrr::flatten() %>%
    purrr::map(unique) %>% 
    viztools::make_intersection_degree_plot(
      Yrange = c(0, 100),
      plotType = "Proteoform"
    ) %>% 
    plotly::ggplotly() %>% 
    plotly::config(
      displaylogo = FALSE,
      toImageButtonOptions = download_button_params
    )

} else {

  pander::pander("Only one fraction, no Intersection Degree plot made") 

}
knitr::asis_output("\n")
knitr::asis_output("Row \n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("\n### Proteoform Identifications, Heatmap (Bin size = 1 kDa)")

results_protein_allhits[[i]] %>%
  dplyr::group_by(
    ProteoformRecordNum,
    fraction
  ) %>%
  viztools::make_heatmap(
    plotType = "Proteoform",
    orientation = "v",
    binSize = 1000,
    massColname = "ObservedPrecursorMass",
    fractionColname = "fraction"
  ) %>%
  plotly::ggplotly() %>%
  plotly::config(
    displaylogo = FALSE,
    toImageButtonOptions = download_button_params
  )
knitr::asis_output("\n### Proteoform Identifications Heatmap, Result Sets (Bin size = 1 kDa)")

proteoform_resultset_heatmap <- 
  results_proteoform[[i]] %>% 
  dplyr::mutate(ResultSet = stringr::str_wrap(ResultSet,10)) %>%
  ggplot(aes(ObservedPrecursorMass/1000, ResultSet)) +
  geom_bin2d(aes(group = ResultSet), position = "identity", stat = "bin2d") +
  scale_fill_viridis_c(option = "C", direction = -1) +
  labs(
    x = "Mass Bin (kDa)",
    y = "Result Set"
  ) +
  theme_minimal() +
  theme(text = ggplot2::element_text(size=18))

plotly::ggplotly(proteoform_resultset_heatmap) %>% 
  plotly::config(
    displaylogo = FALSE,
    toImageButtonOptions = download_button_params
  )

Row

Protein Identifications, Waffle Plot

is_waffle_data_valid <- 
  results_protein_countsbyfraction %>%
  dplyr::filter(results_file_name == basename(fileList[[i]])) %>%
  dplyr::ungroup() %>% 
  dplyr::select(-fraction, -results_file_name) %>% 
  unlist() %>% 
  {is.na(.) | . == 0} %>% 
  all() %>% 
  {!.}

if (is_waffle_data_valid == TRUE) {

  waffle1 <- 
    results_protein_countsbyfraction %>%
    dplyr::filter(results_file_name == basename(fileList[[i]])) %>%
    dplyr::ungroup() %>% 
    dplyr::select(-results_file_name) %>% 
    viztools::waffle_iron(
      fraction_colname = "fraction"
    )

  # fig1 <- image_graph(height = 1200, width = 1600, res = 150)

  waffle1

  # dev.off()

  # magick::image_trim(fig1)

} else {

  cat("No valid data to use for Waffle plot")

}
knitr::asis_output("\n")
knitr::asis_output("Row\n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("### Hit Count vs. Intact Monoisotopic Mass")

allhits_sum <- 
  results_protein_allhits[[i]] %>% 
  mutate(MonoisotopicMass = MonoisotopicMass/1000) %>% 
  dplyr::rename(`MonoisotopicMass (kDa)` = MonoisotopicMass) %>% 
  group_by(
    AccessionNumber,
    `MonoisotopicMass (kDa)`
  ) %>% 
  summarize(`Hit Count` = n()) %>% 
  ungroup()

allhits_linear_plot <- 
  allhits_sum %>% 
  ggplot(aes(`MonoisotopicMass (kDa)`, `Hit Count`)) +
  geom_point(size = 2) +
  labs(
    x = "Intact Monoisotopic Mass (kDa)",
    y = "Hit Count"
  ) +
  scale_x_continuous(
    breaks = scales::pretty_breaks()
  ) +
  theme_minimal() +
  theme(
    panel.background = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    axis.line = element_line(color = "black"),
    axis.ticks = element_line(color = "black"),
    text = element_text(size = 12)
  )

allhits_log10_plot <- 
  allhits_sum %>% 
  ggplot(aes(`MonoisotopicMass (kDa)`, log10(`Hit Count`))) +
  geom_point(size = 2) +
  labs(
    x = "Intact Monoisotopic Mass (kDa)",
    y = "Log10(Hit Count)"
  ) +
  scale_x_continuous(
    breaks = scales::pretty_breaks()
  ) +
  theme_minimal() +
  theme(
    panel.background = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    axis.line = element_line(color = "black"),
    axis.ticks = element_line(color = "black"),
    text = element_text(size = 12)
  )

p1 <- 
  plotly::ggplotly(allhits_linear_plot) %>% 
  plotly::config(
    displaylogo = FALSE,
    toImageButtonOptions = download_button_params
  ) %>% 
  plotly::style(
    marker = list(
      size = 3,
      color = "black"
    )
  )

p2 <- 
  plotly::ggplotly(allhits_log10_plot) %>% 
  plotly::config(
    displaylogo = FALSE,
    toImageButtonOptions = download_button_params
  ) %>% 
  plotly::style(
    marker = list(
      size = 3,
      color = "black"
    )
  )

plotly::subplot(p1, p2, shareX = T, shareY = F, titleY = T, titleX = T)
knitr::asis_output("\n")
knitr::asis_output("Row\n")
knitr::asis_output("-------------------------------------\n")
knitr::asis_output("### -log10(Q-value) vs. Intact Monoisotopic Mass")

allhits_qvals <- 
  results_proteoform[[i]] %>%
  mutate(MonoisotopicMass = MonoisotopicMass/1000) %>% 
  dplyr::rename(`MonoisotopicMass (kDa)` = MonoisotopicMass)

allhits_qvals_plot <- 
  allhits_qvals %>% 
  ggplot(aes(`MonoisotopicMass (kDa)`, -log10(GlobalQvalue))) +
  geom_point(size = 2) +
  labs(
    x = "Intact Monoisotopic Mass (kDa)",
    y = "-Log10(Global Q-value)"
  ) +
  scale_x_continuous(
    breaks = scales::pretty_breaks()
  ) +
  theme_minimal() +
  theme(
    panel.background = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    axis.line = element_line(color = "black"),
    axis.ticks = element_line(color = "black"),
    text = element_text(size = 12)
  )

plotly::ggplotly(allhits_qvals_plot) %>% 
  plotly::config(
    displaylogo = FALSE,
    toImageButtonOptions = download_button_params
  ) %>% 
  plotly::style(
    marker = list(
      size = 3,
      color = "black"
    )
  )


davidsbutcher/GUPPI documentation built on Feb. 26, 2021, 5:41 a.m.