Skip to content

Commit

Permalink
lint and updated test data
Browse files Browse the repository at this point in the history
  • Loading branch information
hechth committed Oct 25, 2024
1 parent 5c6f057 commit 862b380
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
49 changes: 38 additions & 11 deletions tools/isolib/isolib.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ library(Spectra)
library(MsBackendMsp)
library(MetaboCoreUtils)
library(readr)
library(tidyselect)


parse_args <- function() {
Expand All @@ -11,7 +12,7 @@ parse_args <- function() {
compound_table <- read_tsv(
file = args[1],
col_types = "ccd",
col_select = tidyselect::all_of(c("name", "formula")) | tidyselect::any_of("rt")
col_select = all_of(c("name", "formula")) | any_of("rt")
)

parsed <- list(
Expand All @@ -26,7 +27,10 @@ parse_args <- function() {
return(parsed)
}

generate_isotope_spectra <- function(compound_table, adducts_to_use, append_adducts, threshold) {
generate_isotope_spectra <- function(compound_table,
adducts_to_use,
append_adducts,
threshold) {
data(isotopes)
data(adducts)

Expand All @@ -48,12 +52,18 @@ generate_isotope_spectra <- function(compound_table, adducts_to_use, append_addu
merged_chemforms <- mergeform(multiplied_chemforms, adduct$Formula_add)
}

charge_string <- paste0(if (adduct$Charge > 0) "+" else "-", if (abs(adduct$Charge) > 1) abs(adduct$Charge) else "")
charge_string <- paste0(
if (adduct$Charge > 0) "+" else "-",
if (abs(adduct$Charge) > 1) abs(adduct$Charge) else ""
)
adduct_string <- paste0("[", adduct$Name, "]", charge_string)
precursor_mz <- calculateMass(multiplied_chemforms) + adduct$Mass

if (append_adducts == TRUE) {
names <- paste(compound_table$name, paste0("(", adduct$Name, ")"), sep = " ")
names <- paste(
compound_table$name,
paste0("(", adduct$Name, ")"), sep = " "
)
} else {
names <- compound_table$name
}
Expand Down Expand Up @@ -86,13 +96,23 @@ generate_isotope_spectra <- function(compound_table, adducts_to_use, append_addu
for (i in seq_along(patterns)) {
mzs <- append(mzs, list(patterns[[i]][, 1]))
intensities <- append(intensities, list(patterns[[i]][, 2]))
compositions <- as.data.frame(patterns[[i]][, -c(1, 2)]) |> # select all columns which describe the elemental composition
dplyr::select(-tidyselect::any_of(monoisotopic$isotope)) |> # remove all 12C, 35Cl etc.
dplyr::select_if(~ !all(. == 0)) # remove isotopes which don't occur

# select all columns which describe the elemental composition
# remove all 12C, 35Cl etc.
# remove isotopes which don't occur
compositions <- as.data.frame(patterns[[i]][, -c(1, 2)]) |>
dplyr::select(-tidyselect::any_of(monoisotopic$isotope)) |>
dplyr::select_if(~ !all(. == 0))

# combine elemental composition into single string
compositions <- compositions |>
dplyr::rowwise() |>
dplyr::mutate(isotopes = paste( # combine elemental composition into single string
purrr::map2_chr(names(compositions), dplyr::c_across(everything()), ~ paste(.x, .y, sep = ":")),
dplyr::mutate(isotopes = paste(
purrr::map2_chr(
names(compositions),
dplyr::c_across(everything()),
~ paste(.x, .y, sep = ":")
),
collapse = ", "
)) |>
dplyr::ungroup() |>
Expand All @@ -118,12 +138,19 @@ write_to_table <- function(spectra, file, append_isotopes) {
dplyr::rowwise() |>
dplyr::mutate(peaks = paste(unlist(mz), collapse = ";")) |>
dplyr::mutate(isos = paste(unlist(isotopes), collapse = ";"))
result <- tidyr::separate_longer_delim(entries, tidyselect::all_of(c("peaks", "isos")), ";") |>
result <- tidyr::separate_longer_delim(
entries,
all_of(c("peaks", "isos")),
";"
)
result <- result |>
dplyr::select(-c("mz", "intensity", "isotopes")) |>
dplyr::rename(mz = peaks, isotopes = isos, rt = retention_time)

if (append_isotopes) {
result <- dplyr::mutate(result, full_formula = paste0(formula, " (", isotopes, ")")) |>
result <- result |>
dplyr::mutate(result,
full_formula = paste0(formula, " (", isotopes, ")")) |>
dplyr::select(-all_of(c("formula", "isotopes"))) |>
dplyr::rename(formula = full_formula) |>
dplyr::relocate(formula, .after = name)
Expand Down
2 changes: 1 addition & 1 deletion tools/isolib/test-data/test2.tabular
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name adduct formula charge ionization_mode precursor_mz msLevel retention_time mz isotopes
name adduct formula charge ionization_mode precursor_mz msLevel rt mz isotopes
2,4-Dichlorophenoxyacetic acid ou 2,4-D (M-H) [M-H]- C8H6Cl2O3 -1 negative 218.962123412 1 484.2 218.962123019909 13C:0, 37Cl:0
2,4-Dichlorophenoxyacetic acid ou 2,4-D (M-H) [M-H]- C8H6Cl2O3 -1 negative 218.962123412 1 484.2 219.965477859909 13C:1, 37Cl:0
2,4-Dichlorophenoxyacetic acid ou 2,4-D (M-H) [M-H]- C8H6Cl2O3 -1 negative 218.962123412 1 484.2 220.959172909909 13C:0, 37Cl:1
Expand Down
2 changes: 1 addition & 1 deletion tools/isolib/test-data/test3.tabular
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name adduct formula charge ionization_mode precursor_mz msLevel retention_time mz isotopes
name adduct formula charge ionization_mode precursor_mz msLevel rt mz isotopes
2,4-Dichlorophenoxyacetic acid ou 2,4-D (M-H) [M-H]- C8H6Cl2O3 -1 negative 218.962123412 1 484.2 218.962123019909 13C:0, 37Cl:0
2,4-Dichlorophenoxyacetic acid ou 2,4-D (M-H) [M-H]- C8H6Cl2O3 -1 negative 218.962123412 1 484.2 219.965477859909 13C:1, 37Cl:0
2,4-Dichlorophenoxyacetic acid ou 2,4-D (M-H) [M-H]- C8H6Cl2O3 -1 negative 218.962123412 1 484.2 220.959172909909 13C:0, 37Cl:1
Expand Down

0 comments on commit 862b380

Please sign in to comment.