# HG changeset patch # User recetox # Date 1717080722 0 # Node ID 6b0fef8a77c02111d8679ea9397bbdfcb221f864 # Parent b3251a7dae25adeea6a9c4b32ba7dc29c300bec0 planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/isolib commit bc3445f7c41271b0062c7674108f57708d08dd28 diff -r b3251a7dae25 -r 6b0fef8a77c0 isolib.R --- a/isolib.R Wed Apr 24 14:51:32 2024 +0000 +++ b/isolib.R Thu May 30 14:52:02 2024 +0000 @@ -6,77 +6,77 @@ #' @param args A list of command line arguments. main <- function() { - data(isotopes) - data(adducts) + data(isotopes) + data(adducts) + + args <- commandArgs(trailingOnly = TRUE) + compound_table <- read_tsv( + file = args[1], + col_types = "ccd", + col_select = tidyselect::all_of(c("name", "formula")) | tidyselect::any_of("rt") + ) + adducts_to_use <- c(unlist(strsplit(args[2], ",", fixed = TRUE))) + + chemforms <- compound_table$formula + chemforms <- check_chemform(isotopes, chemforms)[, 2] - args <- commandArgs(trailingOnly = TRUE) - compound_table <- read_tsv( - file = args[1], - col_types = "ccd", - col_select = tidyselect::all_of(c("name", "formula")) | tidyselect::any_of("rt") - ) - adducts_to_use <- c(unlist(strsplit(args[2], ",", fixed = TRUE))) + spectra <- data.frame() + + for (current in adducts_to_use) { + adduct <- adducts[adducts$Name == current, ] + multiplied_chemforms <- multiform(chemforms, adduct$Mult) - chemforms <- compound_table$formula - chemforms <- check_chemform(isotopes, chemforms)[, 2] + if (adduct$Ion_mode == "negative") { + merged_chemforms <- subform(multiplied_chemforms, adduct$Formula_ded) + } else { + merged_chemforms <- mergeform(multiplied_chemforms, adduct$Formula_add) + } - spectra <- data.frame() + 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 - for (current in adducts_to_use) { - adduct <- adducts[adducts$Name == current, ] - multiplied_chemforms <- multiform(chemforms, adduct$Mult) + if (args[4] == TRUE) { + names <- paste(compound_table$name, paste0("(", adduct$Name, ")"), sep = " ") + } else { + names <- compound_table$name + } + + spectra_df <- data.frame( + name = names, + adduct = adduct_string, + formula = chemforms, + charge = adduct$Charge, + ionization_mode = adduct$Ion_mode, + precursor_mz = precursor_mz, + msLevel = as.integer(1) + ) - if (adduct$Ion_mode == "negative") { - merged_chemforms <- subform(multiplied_chemforms, adduct$Formula_ded) - } else { - merged_chemforms <- mergeform(multiplied_chemforms, adduct$Formula_add) - } + if ("rt" %in% colnames(compound_table)) { + spectra_df$retention_time <- compound_table$rt + } + + patterns <- enviPat::isopattern( + isotopes = isotopes, + chemforms = merged_chemforms, + charge = adduct$Charge, + threshold = as.numeric(args[3]), + ) - 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 + mzs <- list() + intensities <- list() + for (i in seq_along(patterns)) { + mzs <- append(mzs, list(patterns[[i]][, 1])) + intensities <- append(intensities, list(patterns[[i]][, 2])) + } - if (args[4] == TRUE) { - names <- paste(compound_table$name, paste0("(", adduct$Name, ")"), sep = " ") - } else { - names <- compound_table$name + spectra_df$mz <- mzs + spectra_df$intensity <- intensities + spectra <- rbind(spectra, spectra_df) } - spectra_df <- data.frame( - name = names, - adduct = adduct_string, - formula = chemforms, - charge = adduct$Charge, - ionization_mode = adduct$Ion_mode, - precursor_mz = precursor_mz, - msLevel = as.integer(1) - ) - - if ("rt" %in% colnames(compound_table)) { - spectra_df$retention_time <- compound_table$rt - } - - patterns <- enviPat::isopattern( - isotopes = isotopes, - chemforms = merged_chemforms, - charge = adduct$Charge, - threshold = as.numeric(args[3]), - ) - - mzs <- list() - intensities <- list() - for (i in seq_along(patterns)) { - mzs <- append(mzs, list(patterns[[i]][, 1])) - intensities <- append(intensities, list(patterns[[i]][, 2])) - } - - spectra_df$mz <- mzs - spectra_df$intensity <- intensities - spectra <- rbind(spectra, spectra_df) - } - - sps <- Spectra(spectra) - export(sps, MsBackendMsp(), file = args[5]) + sps <- Spectra(spectra) + export(sps, MsBackendMsp(), file = args[5]) } # Get the command line arguments