Skip to content

Commit

Permalink
fix spectral libraries names (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adafede committed Sep 6, 2023
1 parent 254cc41 commit 2c71f31
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
14 changes: 10 additions & 4 deletions R/annotate_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ annotate_spectra <- function(input = params$files$spectral$raw,
if (is.null(lib_smiles2D)) {
lib_smiles2D <- rep(NA_character_, length(spectral_library))
}
lib_name <- spectral_library@backend@spectraData$name
if (is.null(lib_name)) {
lib_name <- rep(NA_character_, length(spectral_library))
lib_library <- spectral_library@backend@spectraData$library
if (is.null(lib_library)) {
lib_library <- rep(NA_character_, length(spectral_library))
}
lib_mf <- spectral_library@backend@spectraData$formula
if (is.null(lib_mf)) {
Expand All @@ -283,6 +283,10 @@ annotate_spectra <- function(input = params$files$spectral$raw,
if (is.null(lib_mass)) {
lib_mass <- rep(NA_real_, length(spectral_library))
}
lib_name <- spectral_library@backend@spectraData$name
if (is.null(lib_name)) {
lib_name <- rep(NA_character_, length(spectral_library))
}
lib_xlogp <- spectral_library@backend@spectraData$xlogp
if (is.null(lib_xlogp)) {
lib_xlogp <- rep(NA_real_, length(spectral_library))
Expand All @@ -294,9 +298,10 @@ annotate_spectra <- function(input = params$files$spectral$raw,
"target_inchikey_no_stereo" = lib_inchikey2D,
"target_smiles" = lib_smiles,
"target_smiles_no_stereo" = lib_smiles2D,
"target_name" = lib_name,
"target_library" = lib_library,
"target_formula" = lib_mf,
"target_exactmass" = lib_mass,
"target_name" = lib_name,
"target_xlogp" = lib_xlogp,
"target_precursorMz" = lib_precursors
)
Expand Down Expand Up @@ -325,6 +330,7 @@ annotate_spectra <- function(input = params$files$spectral$raw,
tidytable::select(tidytable::any_of(
c(
"feature_id",
"candidate_library" = "target_library",
"candidate_structure_error_mz",
"candidate_structure_name" = "target_name",
"candidate_structure_inchikey_no_stereo",
Expand Down
26 changes: 21 additions & 5 deletions R/harmonize_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' @description This function harmonizes spectra headers
#'
#' @param spectra Spectra object to be harmonized
#' @param metad Metadata to identify the library
#' @param mode MS ionization mode. Must contain 'pos' or 'neg'
#' @param col_ce Name of the collision energy in mgf
#' @param col_ci Name of the compound id in mgf
Expand All @@ -27,6 +28,10 @@
#'
#' @examples NULL
harmonize_spectra <- function(spectra,
metad = get(
"metad",
envir = parent.frame()
),
mode,
col_ce = get(
"col_ce",
Expand Down Expand Up @@ -142,11 +147,21 @@ harmonize_spectra <- function(spectra,
spectra_filtered <- spectra |>
data.frame() |>
tidytable::as_tidytable() |>
tidytable::filter(grepl(
pattern = mode,
x = !!as.name(col_po),
ignore.case = TRUE
)) |>
tidytable::filter(
grepl(
pattern = mode,
x = !!as.name(col_po),
ignore.case = TRUE
) | grepl(
pattern = if (mode == "pos") {
1
} else {
0
},
x = !!as.name(col_po),
ignore.case = TRUE
)
) |>
tidytable::select(
tidytable::any_of(c(columns_full)),
precursorCharge,
Expand Down Expand Up @@ -182,6 +197,7 @@ harmonize_spectra <- function(spectra,
intensity
) |>
tidytable::mutate(
library = metad,
exactmass = as.numeric(exactmass),
spectrum_id = ifelse(
test = is.na(spectrum_id),
Expand Down
3 changes: 1 addition & 2 deletions R/prepare_annotations_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ prepare_annotations_spectra <-
tidytable::filter(!is.na(feature_id)) |>
tidytable::distinct(
feature_id,
candidate_library,
candidate_spectrum_entropy,
candidate_structure_error_mz,
candidate_structure_name,
Expand All @@ -63,8 +64,6 @@ prepare_annotations_spectra <-
) |>
## Add new columns
tidytable::mutate(
candidate_library = "ISDB",
## TODO candidate_library_type = "TODO",
candidate_structure_exact_mass =
as.numeric(candidate_structure_exact_mass),
candidate_structure_tax_npc_01pat = NA_character_,
Expand Down
4 changes: 2 additions & 2 deletions R/prepare_libraries_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param input File containing spectra
#' @param output Output file
#' @param polarity MS polarity
#' @param metad Optional. Metadata as in 'CompoundDb' package
#' @param metad Metadata to identify the library
#' @param col_ce Name of the collision energy in mgf
#' @param col_ci Name of the compound id in mgf
#' @param col_em Name of the exact mass in mgf
Expand Down Expand Up @@ -40,7 +40,7 @@ prepare_libraries_spectra <-
function(input = params$files$libraries$spectral$exp$raw,
output = params$files$libraries$spectral$exp,
polarity = params$ms$polarity,
metad = NULL,
metad = "myLib",
col_ce = params$names$mgf$collision_energy,
col_ci = params$names$mgf$compound_id,
col_em = params$names$mgf$exact_mass,
Expand Down

0 comments on commit 2c71f31

Please sign in to comment.