Skip to content

Commit

Permalink
Fix #166
Browse files Browse the repository at this point in the history
  • Loading branch information
Adafede committed Aug 6, 2024
1 parent 882ecd4 commit a87375a
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 265 deletions.
4 changes: 0 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
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
46 changes: 14 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,15 @@ 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
) |>
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
33 changes: 28 additions & 5 deletions R/import_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ 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
#'
#' @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) {
file_ext <-
stri_replace_all_regex(
str = file,
Expand All @@ -39,12 +47,22 @@ import_spectra <- function(file) {
# TODO Change as soon as R 4.4.0 becomes oldrel
# readMgfSplit(f = file) |>
Spectra() |>
cleanup_spectra()
sanitize_spectra(
cutoff = cutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)
},
"msp" = {
readMsp(f = file) |>
Spectra() |>
cleanup_spectra()
sanitize_spectra(
cutoff = cutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)
},
# "sqlite" = {
# CompDb(x = file) |>
Expand All @@ -55,7 +73,12 @@ import_spectra <- function(file) {
readRDS(file = file) |>
data.frame() |>
Spectra() |>
cleanup_spectra()
sanitize_spectra(
cutoff = cutoff,
dalton = dalton,
polarity = polarity,
ppm = ppm
)
}
)
}
15 changes: 0 additions & 15 deletions R/keep_peaks.R

This file was deleted.

15 changes: 0 additions & 15 deletions R/normalize_peaks.R

This file was deleted.

19 changes: 0 additions & 19 deletions R/remove_above_precursor.R

This file was deleted.

Loading

0 comments on commit a87375a

Please sign in to comment.