Skip to content

Commit

Permalink
Merge pull request #83 from taxonomicallyinformedannotation/refactor-rt
Browse files Browse the repository at this point in the history
Refactoring RT matching (#76)
  • Loading branch information
Adafede authored Aug 15, 2023
2 parents 091400b + d51c27f commit bc455c3
Show file tree
Hide file tree
Showing 75 changed files with 1,627 additions and 991 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: timaR
Title: Taxonomically Informed Metabolite Annotation
Version: 2.8.2
Version: 2.9.0
Authors@R: c(
person("Adriano", "Rutz", , "rutz@imsb.biol.ethz.ch", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0443-9902")),
Expand All @@ -18,7 +18,7 @@ Imports:
crayon (>= 1.5.2),
docopt (>= 0.7.1),
dplyr (>= 1.1.2),
httr (>= 1.4.6),
httr (>= 1.4.7),
igraph (>= 1.5.1),
jsonlite (>= 1.8.7),
MsBackendMgf (>= 1.8.0),
Expand Down Expand Up @@ -93,6 +93,7 @@ Collate:
'extract_spectra.R'
'fake_annotations_columns.R'
'fake_sop_columns.R'
'filter_annotations.R'
'get_file.R'
'get_example_sirius.R'
'get_gnps_tables.R'
Expand All @@ -117,6 +118,7 @@ Collate:
'prepare_features_edges.R'
'prepare_features_tables.R'
'prepare_libraries_adducts.R'
'prepare_libraries_rt.R'
'prepare_libraries_sop_closed.R'
'prepare_libraries_sop_ecmdb.R'
'prepare_libraries_sop_lotus.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(export_spectra_2)
export(extract_spectra)
export(fake_annotations_columns)
export(fake_sop_columns)
export(filter_annotations)
export(get_example_sirius)
export(get_file)
export(get_gnps_tables)
Expand All @@ -50,6 +51,7 @@ export(prepare_features_components)
export(prepare_features_edges)
export(prepare_features_tables)
export(prepare_libraries_adducts)
export(prepare_libraries_rt)
export(prepare_libraries_sop_closed)
export(prepare_libraries_sop_ecmdb)
export(prepare_libraries_sop_lotus)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# timaR

# timaR 2.9.0

* Refactored RT matching (#76)

# timaR 2.8.2

* Change from pbmclapply to pblapply
Expand Down
113 changes: 32 additions & 81 deletions R/annotate_masses.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ utils::globalVariables(
"delta_min",
"Distance",
"error_mz",
"error_rt",
"exact_mass",
"feature_id",
"feature_id_dest",
Expand Down Expand Up @@ -99,67 +98,29 @@ annotate_masses <-
name_source = params$names$source,
name_target = params$names$target,
library = paths$data$interim$libraries$sop$merged$keys,
str_2d_3d = params$
files$
libraries$
sop$
merged$
structures$
dd_ddd,
str_met = params$
files$
libraries$
sop$
merged$
structures$
metadata,
str_nam = params$
files$
libraries$
sop$
merged$
structures$
names,
str_tax_cla = params$
files$
libraries$
sop$
merged$
structures$
taxonomies$
cla,
str_tax_npc = params$
files$
libraries$
sop$
merged$
structures$
taxonomies$
npc,
str_2d_3d = params$files$libraries$sop$merged$structures$dd_ddd,
str_met = params$files$libraries$sop$merged$structures$metadata,
str_nam = params$files$libraries$sop$merged$structures$names,
str_tax_cla = params$files$libraries$sop$merged$structures$taxonomies$cla,

Check warning on line 104 in R/annotate_masses.R

View workflow job for this annotation

GitHub Actions / MegaLinter

file=/github/workspace/R/annotate_masses.R,line=104,col=81,[line_length_linter] Lines should not be more than 80 characters.
str_tax_npc = params$files$libraries$sop$merged$structures$taxonomies$npc,

Check warning on line 105 in R/annotate_masses.R

View workflow job for this annotation

GitHub Actions / MegaLinter

file=/github/workspace/R/annotate_masses.R,line=105,col=81,[line_length_linter] Lines should not be more than 80 characters.
name = params$files$libraries$adducts$prepared,
adducts_list = params$ms$adducts,
adducts_masses_list = system.file(
"extdata",
adducts_masses_list = system.file("extdata",
"adducts.tsv",
package = "timaR"
),
neutral_losses_list = system.file(
"extdata",
neutral_losses_list = system.file("extdata",
"neutral_losses.tsv",
package = "timaR"
),
ms_mode = params$ms$polarity,
tolerance_ppm = params$ms$tolerances$mass$ppm$ms1,
tolerance_rt = params$ms$tolerances$rt$minutes,
parameters = params) {
stopifnot(
"Your ppm tolerance must be lower or equal to 20" = tolerance_ppm <=
20
)
stopifnot(
"Your rt tolerance must be lower or equal to 0.1" = tolerance_rt <=
0.1
)
stopifnot("Your ppm tolerance must be lower or equal to 20" = tolerance_ppm <=

Check warning on line 120 in R/annotate_masses.R

View workflow job for this annotation

GitHub Actions / MegaLinter

file=/github/workspace/R/annotate_masses.R,line=120,col=81,[line_length_linter] Lines should not be more than 80 characters.
20)

Check warning on line 121 in R/annotate_masses.R

View workflow job for this annotation

GitHub Actions / MegaLinter

file=/github/workspace/R/annotate_masses.R,line=121,col=6,[indentation_linter] Indentation should be 16 spaces but is 6 spaces.
stopifnot("Your rt tolerance must be lower or equal to 0.05" = tolerance_rt <=

Check warning on line 122 in R/annotate_masses.R

View workflow job for this annotation

GitHub Actions / MegaLinter

file=/github/workspace/R/annotate_masses.R,line=122,col=81,[line_length_linter] Lines should not be more than 80 characters.
0.05)

Check warning on line 123 in R/annotate_masses.R

View workflow job for this annotation

GitHub Actions / MegaLinter

file=/github/workspace/R/annotate_masses.R,line=123,col=6,[indentation_linter] Indentation should be 16 spaces but is 6 spaces.

paths <<- parse_yaml_paths()
params <<- parameters
Expand Down Expand Up @@ -411,13 +372,10 @@ annotate_masses <-
label_dest
) |>
tidytable::select(
feature_id := !!as.name(
paste(
"feature_id",
"dest",
sep = "_"
)
),
feature_id := !!as.name(paste("feature_id",
"dest",
sep = "_"
)),
label = label_dest
)

Expand Down Expand Up @@ -464,17 +422,13 @@ annotate_masses <-
mz_1 <= value_max
)
) |>
dplyr::mutate(
error_mz = adduct_mass - mz_1,
error_rt = NA_real_
) |>
dplyr::mutate(error_mz = adduct_mass - mz_1) |>
tidytable::select(
feature_id,
rt,
mz,
score_input,
error_mz,
error_rt,
exact_mass,
adduct,
adduct_mass,
Expand All @@ -497,7 +451,6 @@ annotate_masses <-
score_input,
library,
error_mz,
error_rt,
exact_mass
) |>
tidyft::filter(!is.na(exact_mass))
Expand Down Expand Up @@ -556,15 +509,15 @@ annotate_masses <-
tidytable::select(
structure_molecular_formula,
library,
tidytable::everything(), -exact_mass
tidytable::everything(),
-exact_mass
) |>
dplyr::filter(library %ni% forbidden_adducts) |>
tidytable::distinct()

log_debug("adding adduct mass to get back to [1M] \n")
df15 <-
tidytable::left_join(
df14,
tidytable::left_join(df14,
adducts_table,
by = stats::setNames("adduct", "library")
) |>
Expand Down Expand Up @@ -734,9 +687,7 @@ annotate_masses <-
mz_max
)

log_debug(
"joining within given mz tol and filtering possible adducts \n"
)
log_debug("joining within given mz tol and filtering possible adducts \n")
df21 <- df20_a |>
dplyr::inner_join(df2,
by = dplyr::join_by(
Expand Down Expand Up @@ -764,13 +715,13 @@ annotate_masses <-
df13,
by = stats::setNames("structure_exact_mass", "exact_mass")
) |>
dplyr::mutate(
score_input = 0
) |>
dplyr::mutate(score_input = 0) |>
tidytable::select(
structure_molecular_formula,
library = library_name,
tidytable::everything(), -exact_mass, -adduct_value
tidytable::everything(),
-exact_mass,
-adduct_value
) |>
dplyr::filter(library %ni% forbidden_adducts) |>
dplyr::mutate(library = as.character(library)) |>
Expand All @@ -786,7 +737,6 @@ annotate_masses <-
tidytable::distinct(
feature_id,
error_mz,
error_rt,
structure_name,
structure_inchikey_2D,
structure_smiles_2D,
Expand Down Expand Up @@ -815,9 +765,12 @@ annotate_masses <-
structure_taxonomy_classyfire_04directparent
)
) |>
tidyft::mutate_vars(is.character, .func = function(x) {
tidytable::na_if(x, "")
})
tidyft::mutate_vars(
is.character,
.func = function(x) {
tidytable::na_if(x, "")
}
)

df25 |>
decorate_masses()
Expand All @@ -826,16 +779,14 @@ annotate_masses <-
df9 |>
dplyr::mutate(label = paste0(label, " _ ", label_dest)) |>
tidytable::select(
!!as.name(name_source) := feature_id,
!!as.name(name_target) := feature_id_dest,
!!as.name(name_source) := feature_id, !!as.name(name_target) := feature_id_dest,
label
) |>
tidytable::distinct(),
df9_d |>
dplyr::mutate(label = paste0(loss, " loss")) |>
tidytable::select(
!!as.name(name_source) := feature_id,
!!as.name(name_target) := feature_id_dest,
!!as.name(name_source) := feature_id, !!as.name(name_target) := feature_id_dest,
label
) |>
tidytable::distinct()
Expand Down
Loading

0 comments on commit bc455c3

Please # to comment.