tests/testthat/test_annotated_spectrum_plot.R

context("test: annotated_spectrum_plot")

test_that("Testing annotated spectrum plotting function", {

  # Download required test files------------------------------------------------

  BU_Match <- readRDS(system.file("testdata", "BU_Match.RDS", package = "pspecterlib"))
  BU_Peak <- readRDS(system.file("testdata", "BU_Peak.RDS", package = "pspecterlib"))

  # Test annotated_spectrum_plot input checks-----------------------------------

  # Peak data must be a peak data object
  expect_error(
    annotated_spectrum_plot(PeakData = data.frame(test = 1)),
    "PeakData must be of the class peak_data generated by get_peak_data."
  )

  # Matched peaks must be a matched peaks object
  expect_error(
    annotated_spectrum_plot(
      PeakData = BU_Peak,
      MatchedPeaks = data.frame(test = 1)
    ),
    "MatchedPeaks must be of the class matched_peaks generated by get_matched_peaks."
  )

  # Include Isotopes must be a true or false
  expect_error(
    annotated_spectrum_plot(
      PeakData = BU_Peak,
      MatchedPeaks = BU_Match,
      IncludeIsotopes = NA
    ),
    "IncludeIsotopes must be a single logical value TRUE or FALSE."
  )

  # Include Labels must be a true or false
  expect_error(
    annotated_spectrum_plot(
      PeakData = BU_Peak,
      MatchedPeaks = BU_Match,
      IncludeLabels = NA
    ),
    "IncludeLabels must be a single logical value TRUE or FALSE."
  )

  # Label Size must be a numeric
  expect_error(
    annotated_spectrum_plot(
      PeakData = BU_Peak,
      MatchedPeaks = BU_Match,
      LabelSize = "5"
    ),
    "LabelSize must be a number."
  )

  # Label Distance must be a numeric
  expect_error(
    annotated_spectrum_plot(
      PeakData = BU_Peak,
      MatchedPeaks = BU_Match,
      LabelDistance = "5"
    ),
    "LabelDistance must be a number."
  )

  # Interactive must be a TRUE or FALSe
  expect_error(
    annotated_spectrum_plot(
      PeakData = BU_Peak,
      MatchedPeaks = BU_Match,
      Interactive = "TRUE"
    ),
    "Interactive must be a single logical value TRUE or FALSE."
  )

  # Create an annotated spectrum plot-------------------------------------------

  # Non interactive
  plot1 <- annotated_spectrum_plot(BU_Peak, BU_Match, IncludeIsotopes = FALSE)
  expect_true(inherits(plot1, "ggplot"))

  # Interactive
  plot2 <- annotated_spectrum_plot(BU_Peak, BU_Match, Interactive = TRUE)
  expect_true(inherits(plot2, "plotly"))

  # Make one without any annotations and not interactive --> returning NULL
  plot3 <- annotated_spectrum_plot(
    PeakData = make_peak_data(MZ = BU_Peak$`M/Z`, Intensity = BU_Peak$Intensity),
    Interactive = FALSE
  )
  expect_true(inherits(plot3, "ggplot"))

  # Make one with any annotation, special peak data, and interactive
  plot4 <- annotated_spectrum_plot(
    MatchedPeaks = BU_Match,
    PeakData = make_peak_data(MZ = BU_Peak$`M/Z`, Intensity = BU_Peak$Intensity),
    Interactive = TRUE,
    IncludeLabels = FALSE
  )
  expect_true(inherits(plot4, "plotly"))

  # Make one interactive without annotations
  plot5 <- annotated_spectrum_plot(
    PeakData = BU_Peak,
    Interactive = TRUE
  )
  expect_true(inherits(plot5, "plotly"))

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