Calculate similarity between spectra

Description

Calculates similarity scores between query and target spectra using either entropy, cosine, or GNPS methods.

Usage

calculate_similarity(
  method,
  query_spectrum,
  target_spectrum,
  query_precursor,
  target_precursor,
  dalton,
  ppm,
  return_matched_peaks = FALSE,
  ...
)

Arguments

method Character string specifying method: "entropy", "gnps", or "cosine"
query_spectrum Numeric matrix with columns for mz and intensity
target_spectrum Numeric matrix with columns for mz and intensity
query_precursor Numeric precursor m/z value for query
target_precursor Numeric precursor m/z value for target
dalton Numeric Dalton tolerance for peak matching
ppm Numeric PPM tolerance for peak matching
return_matched_peaks Logical; return matched peaks count? Not compatible with ‘entropy’ method. Default: FALSE
Additional arguments passed to MsCoreUtils::join

Value

Numeric similarity score (0-1), or list with score and matches if return_matched_peaks = TRUE. Returns 0.0 if calculation fails.

Examples

library("tima")

sp_1 <- cbind(
  mz = c(10, 36, 63, 91, 93),
  intensity = c(14, 15, 999, 650, 1)
)
precursor_1 <- 123.4567
precursor_2 <- precursor_1 + 14
sp_2 <- cbind(
  mz = c(10, 12, 50, 63, 105),
  intensity = c(35, 5, 16, 999, 450)
)
calculate_similarity(
  method = "entropy",
  query_spectrum = sp_1,
  target_spectrum = sp_2,
  query_precursor = precursor_1,
  target_precursor = precursor_2,
  dalton = 0.005,
  ppm = 10.0
)
[1] 0.5427377
calculate_similarity(
  method = "gnps",
  query_spectrum = sp_1,
  target_spectrum = sp_2,
  query_precursor = precursor_1,
  target_precursor = precursor_2,
  dalton = 0.005,
  ppm = 10.0,
  return_matched_peaks = TRUE
)
$score
[1] 0.9923501

$matches
[1] 4