comparison isolib.R @ 0:8a1893635ac0 draft

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/isolib commit 019a1087adb6bd570eada9ce1d7d6fcd6d55bff8
author recetox
date Tue, 23 Apr 2024 06:39:20 +0000
parents
children b3251a7dae25
comparison
equal deleted inserted replaced
-1:000000000000 0:8a1893635ac0
1 library(enviPat)
2 library(Spectra)
3 library(MsBackendMsp)
4 library(MetaboCoreUtils)
5
6 #' @param args A list of command line arguments.
7 main <- function() {
8 data(isotopes)
9 data(adducts)
10
11 args <- commandArgs(trailingOnly = TRUE)
12 compound_table <- read.delim(args[1], stringsAsFactors = FALSE)
13 adducts_to_use <- c(unlist(strsplit(args[2], ",", fixed = TRUE)))
14
15 chemforms <- compound_table$formula
16 chemforms <- check_chemform(isotopes, chemforms)[, 2]
17
18 spectra <- data.frame()
19
20 for (current in adducts_to_use) {
21 adduct <- adducts[adducts$Name == current, ]
22 multiplied_chemforms <- multiform(chemforms, adduct$Mult)
23
24 if (adduct$Ion_mode == "negative") {
25 merged_chemforms <- subform(multiplied_chemforms, adduct$Formula_ded)
26 } else {
27 merged_chemforms <- mergeform(multiplied_chemforms, adduct$Formula_add)
28 }
29
30 charge_string <- paste0(if (adduct$Charge > 0) "+" else "-", if (abs(adduct$Charge) > 1) abs(adduct$Charge) else "")
31 adduct_string <- paste0("[", adduct$Name, "]", charge_string)
32 precursor_mz <- calculateMass(multiplied_chemforms) + adduct$Mass
33
34 if (args[4] == TRUE) {
35 names <- paste(compound_table$name, paste0("(", adduct$Name, ")"), sep = " ")
36 } else {
37 names <- compound_table$name
38 }
39
40 spectra_df <- data.frame(
41 name = names,
42 adduct = adduct_string,
43 formula = chemforms,
44 charge = adduct$Charge,
45 ionization_mode = adduct$Ion_mode,
46 precursor_mz = precursor_mz,
47 msLevel = as.integer(1)
48 )
49
50 if ("rt" %in% colnames(compound_table)) {
51 spectra_df$retention_time <- compound_table$rt
52 }
53
54 patterns <- enviPat::isopattern(
55 isotopes = isotopes,
56 chemforms = merged_chemforms,
57 charge = adduct$Charge,
58 threshold = as.numeric(args[3]),
59 )
60
61 mzs <- list()
62 intensities <- list()
63 for (i in seq_along(patterns)) {
64 mzs <- append(mzs, list(patterns[[i]][, 1]))
65 intensities <- append(intensities, list(patterns[[i]][, 2]))
66 }
67
68 spectra_df$mz <- mzs
69 spectra_df$intensity <- intensities
70 spectra <- rbind(spectra, spectra_df)
71 }
72
73 sps <- Spectra(spectra)
74 export(sps, MsBackendMsp(), file = args[5])
75 }
76
77 # Get the command line arguments
78 args <- commandArgs(trailingOnly = TRUE)
79 # Call the main function
80 main()