tests/testthat/test_get_matched_peaks.R

context("test: get_matched_peaks")

test_that("Testing get matched peaks function", {

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

  source(system.file("tests/testthat/file_check.R", package = "pspecterlib"))
  downfolder <- file_check("downfolder")
  BU_ScanMetadata <- file_check("BU")
  BU_Peaks <- get_peak_data(ScanMetadata = BU_ScanMetadata, ScanNumber = 31728)

  # Test get_peak_data input checks---------------------------------------------

  # Scan Metadata must be of the appropriate class
  expect_error(
    get_matched_peaks(data.frame(test = 1)),
    "ScanMetadata must be a scan_metadata object generated by get_scan_metadata."
  )

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

  # PPM Threshold must be an appropriate number
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      PPMThreshold = "12"
    ),
    "PPMThreshold must be a single number."
  )

  # Ion Groups cannot be larger than 6
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      IonGroups = c("a", "b", "c", "x", "y", "z", "aa")
    ),
    "IonGroups cannot be longer than length 6."
  )

  # Ion Groups must contain any combination of a, b, c, x, y, and z
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      IonGroups = c("aa")
    ),
    "IonGroups must only contain the letters a, b, c, x, y, and z."
  )

  # Calculate Isotopes must be a true or false
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      CalculateIsotopes = NA
    ),
    "CalculateIsotopes must be a single logical value TRUE or FALSE."
  )

  # Minimum Abundance must be a numeric
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      MinimumAbundance = "2"
    ),
    "MinimumAbundance must be a single numeric value. For example, 0.1."
  )

  # Correlation Score must be a numeric
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      CorrelationScore = "2"
    ),
    "CorrelationScore must be a single numeric value. For example, 0.1."
  )

  # Correlation Score must be between 0 and 1
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      CorrelationScore = 2
    ),
    "CorrelationScore must be between 0 and 1."
  )

  # Alternative Sequence must be a valid protein sequence
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      AlternativeSequence = "INVALIDSEQUENCE"
    ),
    "The detected sequence: INVALIDSEQUENCE is not acceptable."
  )

  # Alternative Spectrum must be a valid spectrum
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      AlternativeSpectrum = data.frame(test = 1)
    ),
    "AlternativeSpectrum must be made with make_peak_data."
  )

  # Alternative Charge must be a single numeric
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      AlternativeCharge = c(2, 3)
    ),
    "AlternativeCharge must be a single number."
  )

  # Alternative Charge must be a single numeric
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      AlternativeCharge = c(2, 3)
    ),
    "AlternativeCharge must be a single number."
  )

  # AlternativeIonGroups must be from the make_modified_ions function
  expect_error(
    get_matched_peaks(
      ScanMetadata = BU_ScanMetadata,
      PeakData = BU_Peaks,
      AlternativeIonGroups = data.frame(test = 1)
    ),
    "AlernativeIonGroups must be of the class 'modified_ion' from make_mass_modified_ion."
  )

  # Create a matched_peaks object-----------------------------------------------

  # First, run the defaults
  BU_MatchedPeaks <- get_matched_peaks(BU_ScanMetadata, BU_Peaks, CorrelationScore_FilterNA = TRUE)
  expect_true(inherits(BU_MatchedPeaks, "matched_peaks"))

  # Second, run an example with alternatives
  BU_MatchedPeaks2 <- get_matched_peaks(
    AlternativeSpectrum = make_peak_data(MZ = BU_Peaks$`M/Z`, Intensity = BU_Peaks$Intensity),
    AlternativeCharge = 2,
    AlternativeSequence = "TESTTEST[Acetyl]TESTTEST",
    AlternativeIonGroups = make_mass_modified_ion(Ion = "a", Symbol = "+", AMU_Change = 1),
  )
  expect_null(BU_MatchedPeaks2)

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