diff 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
line wrap: on
line diff
--- a/isolib.R	Thu Jun 12 09:17:20 2025 +0000
+++ b/isolib.R	Tue Jul 01 09:28:32 2025 +0000
@@ -4,7 +4,8 @@
 library(MetaboCoreUtils)
 library(readr)
 library(tidyselect)
-
+library(stringr)
+library(dplyr)
 
 isotopes <- data.frame(
     element = character(),
@@ -33,11 +34,23 @@
 parse_args <- function() {
     args <- commandArgs(trailingOnly = TRUE)
 
-    compound_table <- read_tsv(
+    compound_table_full <- read_tsv(
         file = args[1],
-        col_types = "ccd",
-        col_select = all_of(c("name", "formula")) | any_of("rt")
+        col_types = cols(
+            name = col_character(),
+            formula = col_character(),
+            rt = col_double(),
+            .default = col_guess()
+        )
     )
+
+    # Extract selected columns
+    compound_table <- compound_table_full[, intersect(c("name", "formula", "rt"), colnames(compound_table_full)), drop = FALSE]
+
+    # Extract remaining columns
+    remaining_columns <- setdiff(colnames(compound_table_full), colnames(compound_table))
+    remaining_data <- compound_table_full[, c("name", remaining_columns), drop = FALSE]
+
     # Handle missing or empty rel_to argument
     rel_to_value <- if (length(args) >= 8 && args[8] != "") {
         if (args[8] == "none") 0 else as.numeric(args[8])
@@ -60,7 +73,8 @@
         append_isotopes = args[5],
         out_format = args[6],
         outfile = args[7],
-        rel_to = rel_to_value
+        rel_to = rel_to_value,
+        remaining_data = remaining_data
     )
     parsed
 }
@@ -176,12 +190,23 @@
     spectra
 }
 
-write_to_msp <- function(spectra, file) {
+join_remaining_data <- function(df, remaining_data) {
+    if (nrow(remaining_data) > 0) {
+        df <- df %>%
+            dplyr::mutate(base_name = stringr::str_trim(stringr::str_remove(name, "\\s*\\([^)]*\\)$")))
+        df <- dplyr::left_join(df, remaining_data, by = c("base_name" = "name"))
+        df <- df %>% dplyr::select(-base_name)
+    }
+    df
+}
+
+write_to_msp <- function(spectra, file, remaining_data) {
+    spectra <- join_remaining_data(spectra, remaining_data)
     sps <- Spectra::Spectra(dplyr::select(spectra, -isotopes))
     Spectra::export(sps, MsBackendMsp::MsBackendMsp(), file = file)
 }
 
-write_to_table <- function(spectra, file, append_isotopes) {
+write_to_table <- function(spectra, file, append_isotopes, remaining_data) {
     entries <- spectra |>
         dplyr::rowwise() |>
         dplyr::mutate(peaks = paste(unlist(mz), collapse = ";")) |>
@@ -208,6 +233,7 @@
             dplyr::rename(formula = full_formula) |>
             dplyr::relocate(formula, .after = name)
     }
+    result <- join_remaining_data(result, remaining_data)
     readr::write_tsv(result, file = file)
 }
 
@@ -222,9 +248,9 @@
     )
 
     if (args$out_format == "msp") {
-        write_to_msp(spectra, args$outfile)
+        write_to_msp(spectra, args$outfile, args$remaining_data)
     } else if (args$out_format == "tabular") {
-        write_to_table(spectra, args$outfile, args$append_isotopes)
+        write_to_table(spectra, args$outfile, args$append_isotopes, args$remaining_data)
     }
 }