tests/testthat/test_scan_metadata_plot.R

context("test: scan_metadata_plot")

test_that("Testing metadata plot function", {

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

  source(system.file("tests/testthat/file_check.R", package = "pspecterlib"))
  BU_ScanMetadata <- file_check("BU")
  RAW_ScanMetadata <- file_check("RAW")

  # Test scan_metadata_plot input checks----------------------------------------

  # Input must be a scan_metadata object
  expect_error(
    scan_metadata_plot(data.frame("test" = 1)),
    "ScanMetadata must be a scan_metadata object generated by get_scan_metadata."
  )

  # XVar must be an acceptable string
  expect_error(
    scan_metadata_plot(
      ScanMetadata = BU_ScanMetadata,
      XVar = 1
    ),
    "X Variable must be one of the following options: Scan Number, MS Level, Retention Time, Precursor M/Z, Precursor Scan, Calculated Mass, Experimental Mass"
  )

  # YVar must be an acceptable string
  expect_error(
    scan_metadata_plot(
      ScanMetadata = BU_ScanMetadata,
      XVar = "Calculated Mass",
      YVar = 1
    ),
    "Y Variable must be one of the following options: Scan Number, MS Level, Retention Time, Precursor M/Z, Precursor Scan, Calculated Mass, Experimental Mass"
  )

  # XVar cannot be a mass variable when no ID data is provided
  expect_error(
    scan_metadata_plot(
      ScanMetadata = RAW_ScanMetadata,
      XVar = "Calculated Mass"
    ),
    "XVar and YVar cannot be Calculated or Experimental Mass if no ID data is provided."
  )

  # Lab Var must be an acceptable string
  expect_error(
    scan_metadata_plot(
      ScanMetadata = BU_ScanMetadata,
      XVar = "Calculated Mass",
      YVar = "Experimental Mass",
      LabVar = "Precursor Scan"
    ),
    "Label Variable must be one of the following options: MS Level, Precursor Charge, Score, Q Value"
  )

  # Lab Var cannot be anything but MS Level when there is no ID data
  expect_error(
    scan_metadata_plot(
      ScanMetadata = RAW_ScanMetadata,
      LabVar = "Score"
    ),
    "If no ID data is provided, LabVar must be MS Level."
  )

  # Interactive must be true or false
  expect_error(
    scan_metadata_plot(
      ScanMetadata = BU_ScanMetadata,
      Interactive = NA
    ),
    "Interactive must be True or False."
  )

  # MS Filter must be an acceptable choice
  expect_error(
    scan_metadata_plot(
      ScanMetadata = BU_ScanMetadata,
      MSFilter = c(3)
    ),
    "MS Filter must contain 1, 2, or both in a vector."
  )

  # Scan Number filter must have an acceptable range
  expect_error(
    scan_metadata_plot(
      ScanMetadata = BU_ScanMetadata,
      ScanNumFilter = c(5000, 200)
    ),
    "Scan Number Filter must contain 2 values: a min and a max scan number in order from smallest to largest."
  )

  # Generate an example plot----------------------------------------------------

  # Create plots
  interPlot <- scan_metadata_plot(
    ScanMetadata = BU_ScanMetadata,
    LabVar = "Score",
    MSFilter = 2,
    ScanNumFilter = c(30000, 32000)
  )
  interPlot2 <- scan_metadata_plot(
    ScanMetadata = RAW_ScanMetadata,
    LabVar = "MS Level"
  )

  # Result should be a plotly object
  expect_true(inherits(interPlot, "plotly"))
  expect_true(inherits(interPlot2, "plotly"))

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