From 416d2ec39c51e2d0ee6c9be70b3b9e2271bce706 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Todor=20Kondi=C4=87?= <kontrapunkt@uclmail.net>
Date: Mon, 16 Jan 2023 09:27:38 +0100
Subject: [PATCH] mefrag, errors, test: Complete the system for running MetFrag
 on `summ' entries.

* metfrag(metrag_run): Update.
---
 R/errors.R                       |  6 ++++++
 R/metfrag.R                      | 21 +++++++++++++++++----
 tests/testthat/_snaps/metfrag.md | 24 ++++++++++++++++++++++++
 tests/testthat/test-metfrag.R    | 23 ++++++++++++++++-------
 4 files changed, 63 insertions(+), 11 deletions(-)

diff --git a/R/errors.R b/R/errors.R
index 7b9493f..53f048e 100644
--- a/R/errors.R
+++ b/R/errors.R
@@ -31,3 +31,9 @@ check_file_absent <- function(file,what) {
 check_not_one <- function(value,what) {
     if (length(value)!=1L) stop(errorCondition(paste0("Size of", what, " is not one."), class=paste0(what,'-not-one')))
 }
+
+check_extension <- function(extfileval,what) {
+    check_notastring(extfileval[[1]],what = what)
+    if (extfileval[[1]]==extfileval[[2]]) stop(errorCondition(paste0("We could not find the extension for ",what, ". The returned value was: ", extfileval[[2]]),
+                                                              class = paste0(what,'-no-ext-found')))
+}
diff --git a/R/metfrag.R b/R/metfrag.R
index 93630f6..da543d0 100644
--- a/R/metfrag.R
+++ b/R/metfrag.R
@@ -41,7 +41,7 @@ metfrag_get_ms2_spec <- function(ms2,stag_entry) {
     x
 }
 
-metfrag_run <- function(param,path,subpaths,db_path,stag_tab,ms2,runtime,java_bin) {
+metfrag_run <- function(param,path,subpaths,db_path,stag_tab,ms2,runtime,java_bin,nproc = 1L) {
     keys = intersect(colnames(stag_tab),colnames(ms2))
     message("Generating MetFrag configs.")
     file_tab = ms2[stag_tab,{
@@ -60,8 +60,21 @@ metfrag_run <- function(param,path,subpaths,db_path,stag_tab,ms2,runtime,java_bi
     withr::with_dir(path,{
         metfrag_run_many(fn_jar = runtime,
                          file_tab = file_tab,
-                         java_bin = java_bin)
+                         java_bin = java_bin,
+                         nproc = nproc)
     })
+
+    ## We don't know (so well) in advance what are the endings of the
+    ## results files. Lets find this out.
+    pth = file.path(path,subpaths[["results"]])
+    a_res_f = list.files(path = pth,
+                         pattern = param$SampleName)[[1]]
+    ext = sub(pattern = r"(^.*\.([[:alnum:]]+)$)",r"(\1)", a_res_f)
+    check_extension(c(ext=ext,file=a_res_f),what="mf-res-file")
+    file_tab[,f_res:=paste0(param$SampleName,"_",stag,".",(ext))]
+
+    
+    
 }
 
     
@@ -203,7 +216,8 @@ metfrag_run_many_w_futures <- function(fn_jar,fn_conf,fn_log, mem = NA_character
 metfrag_run_many <- function(fn_jar,file_tab, mem = NA_character_, java_bin = "java",nproc=1L) {
     ntasks = NROW(file_tab)
 
-    k = ntasks %/% nproc
+    todo = min(nproc,ntasks)
+    k = ntasks %/% todo
     ndone = 0L
     lc = 1L
     while (ndone < ntasks) {
@@ -212,7 +226,6 @@ metfrag_run_many <- function(fn_jar,file_tab, mem = NA_character_, java_bin = "j
         for (i in (ndone + 1):ncurr_last) {
             fn_conf = file_tab[i,f_conf]
             fn_log = file_tab[i,f_log]
-            message("fn_conf:", fn_conf)
             procs[[i-ndone]] = metfrag_run_one(fn_jar,
                                                fn_conf= fn_conf,
                                                fn_log = fn_log,
diff --git a/tests/testthat/_snaps/metfrag.md b/tests/testthat/_snaps/metfrag.md
index 2f4c222..5c7749d 100644
--- a/tests/testthat/_snaps/metfrag.md
+++ b/tests/testthat/_snaps/metfrag.md
@@ -18,3 +18,27 @@
       [13] "MetFragDatabaseType = \nFragmentPeakMatchAbsoluteMassDeviation = 0.001\nFragmentPeakMatchRelativeMassDeviation = 5\nDatabaseSearchRelativeMassDeviation = 5\nMetFragScoreTypes = FragmenterScore,OfflineIndividualMoNAScore\nMetFragScoreWeights = 1,1\nMetFragCandidateWriter = CSV\nSampleName = sample_a_[M+]+_a\nMaximumTreeDepth = 2\nMetFragPreProcessingCandidateFilter = UnconnectedCompoundFilter,IsotopeFilter\nMetFragPostProcessingCandidateFilter = InChIKeyFilter\nIonizedPrecursorMass = 777.7789\nIsPositiveIonMode = True\nPrecursorIonMode = 0\nResultsPath = results\nPeakListPath = spec/sample_a_[M+]+_a.csv\n\n"               
       [14] "MetFragDatabaseType = \nFragmentPeakMatchAbsoluteMassDeviation = 0.001\nFragmentPeakMatchRelativeMassDeviation = 5\nDatabaseSearchRelativeMassDeviation = 5\nMetFragScoreTypes = FragmenterScore,OfflineIndividualMoNAScore\nMetFragScoreWeights = 1,1\nMetFragCandidateWriter = CSV\nSampleName = sample_a_[M-]-_a\nMaximumTreeDepth = 2\nMetFragPreProcessingCandidateFilter = UnconnectedCompoundFilter,IsotopeFilter\nMetFragPostProcessingCandidateFilter = InChIKeyFilter\nIonizedPrecursorMass = 777.7789\nIsPositiveIonMode = False\nPrecursorIonMode = 0\nResultsPath = results\nPeakListPath = spec/sample_a_[M-]-_a.csv\n\n"              
 
+# function metfrag_run returns correct result.
+
+    Code
+      ftab
+    Output
+          ID adduct tag   an CE
+      1: 100 [M+H]+  AA 5413 10
+      2: 100 [M+H]+  KO 5434 10
+                                                                f_conf
+      1: config/testsample_setNTs_ID100_adduct[M+H]+_tagAA_an5413.conf
+      2: config/testsample_setNTs_ID100_adduct[M+H]+_tagKO_an5434.conf
+                                                             f_log
+      1: log/testsample_setNTs_ID100_adduct[M+H]+_tagAA_an5413.log
+      2: log/testsample_setNTs_ID100_adduct[M+H]+_tagKO_an5434.log
+                                                             f_spec
+      1: spec/testsample_setNTs_ID100_adduct[M+H]+_tagAA_an5413.csv
+      2: spec/testsample_setNTs_ID100_adduct[M+H]+_tagKO_an5434.csv
+                                           stag
+      1: setNTs_ID100_adduct[M+H]+_tagAA_an5413
+      2: setNTs_ID100_adduct[M+H]+_tagKO_an5434
+                                                         f_res
+      1: testsample_setNTs_ID100_adduct[M+H]+_tagAA_an5413.csv
+      2: testsample_setNTs_ID100_adduct[M+H]+_tagKO_an5434.csv
+
diff --git a/tests/testthat/test-metfrag.R b/tests/testthat/test-metfrag.R
index 45abcaf..d05ef02 100644
--- a/tests/testthat/test-metfrag.R
+++ b/tests/testthat/test-metfrag.R
@@ -54,13 +54,22 @@ ok_return_val("metfrag_run",{
     withr::with_dir(m$run$metfrag$path,{
             stagtab = metfrag_get_stag_tab(m$out$tab$summ[ms2_sel == T])
 
-            metfrag_run(param = m$conf$metfrag$param,
-                        path = m$run$metfrag$path,
-                        subpaths = m$run$metfrag$subpaths,
-                        db_path = m$run$metfrag$db_path,
-                        stag_tab = stab, ms2 = m$extr$ms2,
-                        runtime=m$run$metfrag$runtime,
-                        java_bin=m$run$metfrag$java_bin)
+            ftab = metfrag_run(param = m$conf$metfrag$param,
+                               path = m$run$metfrag$path,
+                               subpaths = m$run$metfrag$subpaths,
+                               db_path = m$run$metfrag$db_path,
+                               stag_tab = stagtab, ms2 = m$extr$ms2,
+                               runtime=m$run$metfrag$runtime,
+                               java_bin=m$run$metfrag$java_bin,
+                               nproc = 2)
+
+            expect_snapshot(ftab)
+
+            for (f in ftab[,f_res]) {
+                expect_true(file.exists(file.path(m$run$metfrag$path,
+                                                  m$run$metfrag$subpaths['results'],
+                                                  f)))
+            }
 
                                  
     })
-- 
GitLab