Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace internal functions by Spectra equivalents #168

Merged
merged 6 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tima
Title: Taxonomically Informed Metabolite Annotation
Version: 2.10.0
Version: 2.10.1
Authors@R: c(
person("Adriano", "Rutz", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0443-9902")),
Expand Down Expand Up @@ -93,11 +93,7 @@ Collate:
'parse_adduct.R'
'calculate_mass_of_m.R'
'annotate_masses.R'
'keep_peaks.R'
'sanitize_spectra.R'
'remove_above_precursor.R'
'normalize_peaks.R'
'cleanup_spectra.R'
'import_spectra.R'
'annotate_spectra.R'
'benchmark_taxize_spectra.R'
Expand Down
7 changes: 3 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export(calculate_mass_of_m)
export(clean_bio)
export(clean_chemo)
export(clean_collapse)
export(cleanup_spectra)
export(columns_model)
export(complement_metadata_structures)
export(create_components)
Expand Down Expand Up @@ -46,11 +45,9 @@ export(harmonize_names_sirius)
export(harmonize_spectra)
export(import_spectra)
export(install)
export(keep_peaks)
export(load_yaml_files)
export(log_debug)
export(log_pipe)
export(normalize_peaks)
export(parse_adduct)
export(parse_cli_params)
export(parse_yaml_params)
Expand All @@ -72,7 +69,6 @@ export(prepare_libraries_spectra)
export(prepare_params)
export(prepare_taxa)
export(read_from_sirius_zip)
export(remove_above_precursor)
export(replace_id)
export(round_reals)
export(run_app)
Expand Down Expand Up @@ -101,8 +97,11 @@ importFrom(Spectra,dropNaSpectraVariables)
importFrom(Spectra,filterEmptySpectra)
importFrom(Spectra,filterFourierTransformArtefacts)
importFrom(Spectra,filterIntensity)
importFrom(Spectra,filterMsLevel)
importFrom(Spectra,filterPrecursorCharge)
importFrom(Spectra,filterPrecursorPeaks)
importFrom(Spectra,reduceSpectra)
importFrom(Spectra,scalePeaks)
importFrom(crayon,blue)
importFrom(crayon,cyan)
importFrom(crayon,green)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# tima

# tima 2.10.1

* Replaced internal functions by `Spectra` equivalents (#166)

# tima 2.10.0

* Added alt text to vignettes
Expand Down
47 changes: 15 additions & 32 deletions R/annotate_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import::from(dplyr, join_by, .into = environment())
import::from(msentropy, calculate_entropy_similarity, .into = environment())
import::from(msentropy, calculate_spectral_entropy, .into = environment())
import::from(pbapply, pblapply, .into = environment())
import::from(Spectra, addProcessing, .into = environment())
import::from(Spectra, applyProcessing, .into = environment())
import::from(Spectra, concatenateSpectra, .into = environment())
import::from(Spectra, filterIntensity, .into = environment())
import::from(Spectra, filterPrecursorCharge, .into = environment())
import::from(tidytable, any_of, .into = environment())
import::from(tidytable, arrange, .into = environment())
import::from(tidytable, as_tidytable, .into = environment())
Expand All @@ -31,11 +28,8 @@ import::from(tidytable, tidytable, .into = environment())
#' @importFrom msentropy calculate_entropy_similarity
#' @importFrom msentropy calculate_spectral_entropy
#' @importFrom pbapply pblapply
#' @importFrom Spectra addProcessing
#' @importFrom Spectra applyProcessing
#' @importFrom Spectra concatenateSpectra
#' @importFrom Spectra filterIntensity
#' @importFrom Spectra filterPrecursorCharge
#' @importFrom tidytable any_of
#' @importFrom tidytable arrange
#' @importFrom tidytable as_tidytable
Expand All @@ -52,9 +46,6 @@ import::from(tidytable, tidytable, .into = environment())
#' @include get_params.R
#' @include harmonize_adducts.R
#' @include import_spectra.R
#' @include normalize_peaks.R
#' @include remove_above_precursor.R
#' @include sanitize_spectra.R
#'
#' @param input Query file containing spectra. Currently an '.mgf' file
#' @param library Library containing spectra to match against.
Expand Down Expand Up @@ -94,12 +85,12 @@ annotate_spectra <- function(input = get_params(step = "annotate_spectra")$files

log_debug("Loading spectra...")
spectra <- input |>
import_spectra() |>
filterPrecursorCharge(z = if (polarity == "pos") {
c(1, 2, 3)
} else {
c(-1, -2, -3)
})
import_spectra(
cutoff = qutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)

df_empty <- data.frame(
feature_id = NA,
Expand All @@ -120,24 +111,16 @@ annotate_spectra <- function(input = get_params(step = "annotate_spectra")$files
if (length(spectra) > 0) {
log_debug("Loading spectral library")
spectral_library <- unlist(library) |>
lapply(FUN = import_spectra) |>
concatenateSpectra() |>
sanitize_spectra() |>
addProcessing(remove_above_precursor(),
spectraVariables = c("precursorMz")
) |>
addProcessing(normalize_peaks()) |>
applyProcessing()

log_debug("Applying initial intensity filter to query spectra")
spectra <- spectra |>
sanitize_spectra() |>
filterIntensity(intensity = c(qutoff, Inf)) |>
addProcessing(remove_above_precursor(),
spectraVariables = c("precursorMz")
lapply(
FUN = import_spectra,
cutoff = qutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm,
sanitize = FALSE
) |>
addProcessing(normalize_peaks()) |>
applyProcessing()
lapply(FUN = applyProcessing) |>
concatenateSpectra()

query_precursors <- spectra@backend@spectraData$precursorMz
query_spectra <- spectra@backend@peaksData
Expand Down
37 changes: 0 additions & 37 deletions R/cleanup_spectra.R

This file was deleted.

21 changes: 5 additions & 16 deletions R/create_edges_spectra.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import::from(msentropy, calculate_spectral_entropy, .into = environment())
import::from(pbapply, pblapply, .into = environment())
import::from(Spectra, addProcessing, .into = environment())
import::from(Spectra, applyProcessing, .into = environment())
import::from(tidyfst, rn_col, .into = environment())
import::from(tidytable, any_of, .into = environment())
import::from(tidytable, bind_rows, .into = environment())
Expand All @@ -21,8 +19,6 @@ import::from(tidytable, tidytable, .into = environment())
#'
#' @importFrom msentropy calculate_spectral_entropy
#' @importFrom pbapply pblapply
#' @importFrom Spectra addProcessing
#' @importFrom Spectra applyProcessing
#' @importFrom tidyfst rn_col
#' @importFrom tidytable any_of
#' @importFrom tidytable bind_rows
Expand All @@ -38,9 +34,6 @@ import::from(tidytable, tidytable, .into = environment())
#' @include create_edges.R
#' @include get_params.R
#' @include import_spectra.R
#' @include normalize_peaks.R
#' @include remove_above_precursor.R
#' @include sanitize_spectra.R
#'
#' @param input Query file containing spectra. Currently an '.mgf' file
#' @param output Output file.
Expand Down Expand Up @@ -69,16 +62,12 @@ create_edges_spectra <- function(input = get_params(step = "create_edges_spectra

log_debug("Loading spectra...")
spectra <- input |>
import_spectra()
import_spectra(
cutoff = qutoff,
dalton = dalton,
ppm = ppm
)
if (length(spectra) > 1) {
spectra <- spectra |>
sanitize_spectra(cutoff = qutoff) |>
# addProcessing(remove_above_precursor(),
# spectraVariables = c("precursorMz")
# ) |>
addProcessing(normalize_peaks()) |>
applyProcessing()

log_debug("Performing spectral comparison")
log_debug("As we do not limit the precursors delta,
expect a (relatively) long processing time.")
Expand Down
50 changes: 32 additions & 18 deletions R/get_example_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,42 @@
#' @include go_to_cache.R
#' @include parse_yaml_paths.R
#'
#' @param example The example(s) you want to download
#'
#' @return Example files.
#'
#' @export
#'
#' @examples NULL
get_example_files <- function() {
go_to_cache()
message("Features")
get_file(
url = tima::parse_yaml_paths()$urls$examples$features,
export = tima::parse_yaml_paths()$data$source$features
)
message("Metadata")
get_file(
url = tima::parse_yaml_paths()$urls$examples$metadata,
export = tima::parse_yaml_paths()$data$source$metadata
)
message("Sirius")
get_example_sirius()
message("Spectra")
get_file(
url = tima::parse_yaml_paths()$urls$examples$spectra,
export = tima::parse_yaml_paths()$data$source$spectra
get_example_files <- function(example = c("features", "metadata", "sirius", "spectra")) {
stopifnot(
"Example files available are `features`, `metadata`, `sirius` and `spectra`." =
example %in% c("features", "metadata", "sirius", "spectra")
)
go_to_cache()
if ("features" %in% example) {
message("Features")
get_file(
url = tima::parse_yaml_paths()$urls$examples$features,
export = tima::parse_yaml_paths()$data$source$features
)
}
if ("metadata" %in% example) {
message("Metadata")
get_file(
url = tima::parse_yaml_paths()$urls$examples$metadata,
export = tima::parse_yaml_paths()$data$source$metadata
)
}
if ("sirius" %in% example) {
message("Sirius")
get_example_sirius()
}
if ("spectra" %in% example) {
message("Spectra")
get_file(
url = tima::parse_yaml_paths()$urls$examples$spectra,
export = tima::parse_yaml_paths()$data$source$spectra
)
}
}
35 changes: 26 additions & 9 deletions R/import_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ import::from(stringi, stri_replace_all_regex, .into = environment())
#' @importFrom Spectra Spectra
#' @importFrom stringi stri_replace_all_regex
#'
#' @include cleanup_spectra.R
#' @include sanitize_spectra.R
#'
#' @param file File path of the spectrum file to be imported
#' @param cutoff Absolute minimal intensity
#' @param dalton Dalton tolerance
#' @param polarity Polarity
#' @param ppm PPM tolerance
#' @param sanitize Flag indicating whether to sanitize. Default TRUE
#'
#' @return Spectra object containing the imported spectra
#'
#' @export
#'
#' @examples NULL
import_spectra <- function(file) {
import_spectra <- function(file,
cutoff = 0,
dalton = 0.01,
polarity = NA,
ppm = 10,
sanitize = TRUE) {
file_ext <-
stri_replace_all_regex(
str = file,
Expand All @@ -32,19 +42,17 @@ import_spectra <- function(file) {
vectorize_all = FALSE
)

switch(
spectra <- switch(
EXPR = file_ext,
"mgf" = {
readMgf(f = file) |>
# TODO Change as soon as R 4.4.0 becomes oldrel
# readMgfSplit(f = file) |>
Spectra() |>
cleanup_spectra()
Spectra()
},
"msp" = {
readMsp(f = file) |>
Spectra() |>
cleanup_spectra()
Spectra()
},
# "sqlite" = {
# CompDb(x = file) |>
Expand All @@ -54,8 +62,17 @@ import_spectra <- function(file) {
"rds" = {
readRDS(file = file) |>
data.frame() |>
Spectra() |>
cleanup_spectra()
Spectra()
}
)
if (sanitize) {
spectra <- spectra |>
sanitize_spectra(
cutoff = cutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)
}
return(spectra)
}
15 changes: 0 additions & 15 deletions R/keep_peaks.R

This file was deleted.

Loading
Loading