Mercurial > repos > recetox > isolib
comparison isolib.R @ 7:06f2d0d6d107 draft default tip
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/isolib commit aec90552d259748822d040248a9e1265759cc198
| author | recetox |
|---|---|
| date | Tue, 01 Jul 2025 09:28:32 +0000 |
| parents | f0fe957df1cc |
| children |
comparison
equal
deleted
inserted
replaced
| 6:f0fe957df1cc | 7:06f2d0d6d107 |
|---|---|
| 2 library(Spectra) | 2 library(Spectra) |
| 3 library(MsBackendMsp) | 3 library(MsBackendMsp) |
| 4 library(MetaboCoreUtils) | 4 library(MetaboCoreUtils) |
| 5 library(readr) | 5 library(readr) |
| 6 library(tidyselect) | 6 library(tidyselect) |
| 7 | 7 library(stringr) |
| 8 library(dplyr) | |
| 8 | 9 |
| 9 isotopes <- data.frame( | 10 isotopes <- data.frame( |
| 10 element = character(), | 11 element = character(), |
| 11 abundance = numeric(), | 12 abundance = numeric(), |
| 12 isotope = character() | 13 isotope = character() |
| 31 | 32 |
| 32 | 33 |
| 33 parse_args <- function() { | 34 parse_args <- function() { |
| 34 args <- commandArgs(trailingOnly = TRUE) | 35 args <- commandArgs(trailingOnly = TRUE) |
| 35 | 36 |
| 36 compound_table <- read_tsv( | 37 compound_table_full <- read_tsv( |
| 37 file = args[1], | 38 file = args[1], |
| 38 col_types = "ccd", | 39 col_types = cols( |
| 39 col_select = all_of(c("name", "formula")) | any_of("rt") | 40 name = col_character(), |
| 40 ) | 41 formula = col_character(), |
| 42 rt = col_double(), | |
| 43 .default = col_guess() | |
| 44 ) | |
| 45 ) | |
| 46 | |
| 47 # Extract selected columns | |
| 48 compound_table <- compound_table_full[, intersect(c("name", "formula", "rt"), colnames(compound_table_full)), drop = FALSE] | |
| 49 | |
| 50 # Extract remaining columns | |
| 51 remaining_columns <- setdiff(colnames(compound_table_full), colnames(compound_table)) | |
| 52 remaining_data <- compound_table_full[, c("name", remaining_columns), drop = FALSE] | |
| 53 | |
| 41 # Handle missing or empty rel_to argument | 54 # Handle missing or empty rel_to argument |
| 42 rel_to_value <- if (length(args) >= 8 && args[8] != "") { | 55 rel_to_value <- if (length(args) >= 8 && args[8] != "") { |
| 43 if (args[8] == "none") 0 else as.numeric(args[8]) | 56 if (args[8] == "none") 0 else as.numeric(args[8]) |
| 44 } else { | 57 } else { |
| 45 0 # Default value is 0 | 58 0 # Default value is 0 |
| 58 threshold = as.numeric(args[3]), | 71 threshold = as.numeric(args[3]), |
| 59 append_adducts = args[4], | 72 append_adducts = args[4], |
| 60 append_isotopes = args[5], | 73 append_isotopes = args[5], |
| 61 out_format = args[6], | 74 out_format = args[6], |
| 62 outfile = args[7], | 75 outfile = args[7], |
| 63 rel_to = rel_to_value | 76 rel_to = rel_to_value, |
| 77 remaining_data = remaining_data | |
| 64 ) | 78 ) |
| 65 parsed | 79 parsed |
| 66 } | 80 } |
| 67 | 81 |
| 68 generate_isotope_spectra <- function(compound_table, | 82 generate_isotope_spectra <- function(compound_table, |
| 174 spectra <- rbind(spectra, spectra_df) | 188 spectra <- rbind(spectra, spectra_df) |
| 175 } | 189 } |
| 176 spectra | 190 spectra |
| 177 } | 191 } |
| 178 | 192 |
| 179 write_to_msp <- function(spectra, file) { | 193 join_remaining_data <- function(df, remaining_data) { |
| 194 if (nrow(remaining_data) > 0) { | |
| 195 df <- df %>% | |
| 196 dplyr::mutate(base_name = stringr::str_trim(stringr::str_remove(name, "\\s*\\([^)]*\\)$"))) | |
| 197 df <- dplyr::left_join(df, remaining_data, by = c("base_name" = "name")) | |
| 198 df <- df %>% dplyr::select(-base_name) | |
| 199 } | |
| 200 df | |
| 201 } | |
| 202 | |
| 203 write_to_msp <- function(spectra, file, remaining_data) { | |
| 204 spectra <- join_remaining_data(spectra, remaining_data) | |
| 180 sps <- Spectra::Spectra(dplyr::select(spectra, -isotopes)) | 205 sps <- Spectra::Spectra(dplyr::select(spectra, -isotopes)) |
| 181 Spectra::export(sps, MsBackendMsp::MsBackendMsp(), file = file) | 206 Spectra::export(sps, MsBackendMsp::MsBackendMsp(), file = file) |
| 182 } | 207 } |
| 183 | 208 |
| 184 write_to_table <- function(spectra, file, append_isotopes) { | 209 write_to_table <- function(spectra, file, append_isotopes, remaining_data) { |
| 185 entries <- spectra |> | 210 entries <- spectra |> |
| 186 dplyr::rowwise() |> | 211 dplyr::rowwise() |> |
| 187 dplyr::mutate(peaks = paste(unlist(mz), collapse = ";")) |> | 212 dplyr::mutate(peaks = paste(unlist(mz), collapse = ";")) |> |
| 188 dplyr::mutate(isos = paste(unlist(isotopes), collapse = ";")) | 213 dplyr::mutate(isos = paste(unlist(isotopes), collapse = ";")) |
| 189 result <- tidyr::separate_longer_delim( | 214 result <- tidyr::separate_longer_delim( |
| 206 ) |> | 231 ) |> |
| 207 dplyr::select(-all_of(c("formula", "isotopes"))) |> | 232 dplyr::select(-all_of(c("formula", "isotopes"))) |> |
| 208 dplyr::rename(formula = full_formula) |> | 233 dplyr::rename(formula = full_formula) |> |
| 209 dplyr::relocate(formula, .after = name) | 234 dplyr::relocate(formula, .after = name) |
| 210 } | 235 } |
| 236 result <- join_remaining_data(result, remaining_data) | |
| 211 readr::write_tsv(result, file = file) | 237 readr::write_tsv(result, file = file) |
| 212 } | 238 } |
| 213 | 239 |
| 214 main <- function() { | 240 main <- function() { |
| 215 args <- parse_args() | 241 args <- parse_args() |
| 220 args$threshold, | 246 args$threshold, |
| 221 args$rel_to | 247 args$rel_to |
| 222 ) | 248 ) |
| 223 | 249 |
| 224 if (args$out_format == "msp") { | 250 if (args$out_format == "msp") { |
| 225 write_to_msp(spectra, args$outfile) | 251 write_to_msp(spectra, args$outfile, args$remaining_data) |
| 226 } else if (args$out_format == "tabular") { | 252 } else if (args$out_format == "tabular") { |
| 227 write_to_table(spectra, args$outfile, args$append_isotopes) | 253 write_to_table(spectra, args$outfile, args$append_isotopes, args$remaining_data) |
| 228 } | 254 } |
| 229 } | 255 } |
| 230 | 256 |
| 231 # Call the main function | 257 # Call the main function |
| 232 main() | 258 main() |
