From c03e821d1cff40b1afa252c86b4bbf38ebdd43c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Todor=20Kondi=C4=87?= <kontrapunkt@uclmail.net>
Date: Tue, 10 Jan 2023 12:48:23 +0100
Subject: [PATCH] api, resources, tests, ... : Add MetFrag related arguments to
 `app'.

* DESCRIPTION: New version and doc engine.

* NAMESPACE: Adapted.

* man: Updated.
---
 DESCRIPTION               |  4 +-
 NAMESPACE                 |  1 -
 R/api.R                   | 81 +++++++++++++++++++++++++++------------
 R/resources.R             |  9 ++++-
 man/app.Rd                | 25 ++++++++----
 man/serve.Rd              |  8 ++--
 tests/testthat/test-api.R | 40 +++++++++++++++++++
 7 files changed, 127 insertions(+), 41 deletions(-)
 create mode 100644 tests/testthat/test-api.R

diff --git a/DESCRIPTION b/DESCRIPTION
index f33b90a..57676cd 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,6 +1,6 @@
 Package: shinyscreen
 Title: Pre-screening of Mass Spectrometry Data 
-Version: 1.2.9000
+Version: 1.2.9001
 Author: Todor Kondić
 Maintainer: Todor Kondić <todor.kondic@uni.lu>
 Authors@R: 
@@ -41,7 +41,7 @@ Description: Pre-screening of Mass Spectrometry Data.
 License: Apache License (>= 2.0)
 Encoding: UTF-8
 LazyData: true
-RoxygenNote: 7.1.2
+RoxygenNote: 7.2.3
 Roxygen: list(markdown = TRUE)
 Collate:
     'base.R'
diff --git a/NAMESPACE b/NAMESPACE
index 9937375..ec49d63 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -52,4 +52,3 @@ importFrom(MSnbase,readMSData)
 importFrom(promises,"%...>%")
 importFrom(promises,future_promise)
 importFrom(shiny,validate)
-importFrom(ggplot2,aes)
diff --git a/R/api.R b/R/api.R
index 03fb257..1b2db95 100644
--- a/R/api.R
+++ b/R/api.R
@@ -673,41 +673,72 @@ create_plots <- function(m) {
     m
 }
 
+prepare_app <- function(dir_before,
+                        projects,
+                        top_data_dir,
+                        metfrag_db_dir,
+                        metfrag_runtime) {
+    init <- list()
+    init$dir_before <- dir_before
+    init$top_data_dir <- norm_path(top_data_dir)
+    init$projects <- norm_path(projects)
+    init$metfrag_db_dir <- if (nchar(norm_path(metfrag_db_dir))>0L) norm_path(metfrag_db_dir) else ""
+    init$metfrag_runtime <- if (nchar(norm_path(metfrag_runtime))>0L) norm_path(metfrag_runtime) else ""
+    if (!dir.exists(init$top_data_dir)) stop(errorCondition(paste0("Data directory (top_data_dir), currently `",
+                                                              init$top_data_dir,
+                                                              "` does not exist. Abort."),
+                                                       class = "top-data-dir-absent"))
+    if (!dir.exists(init$projects)) stop(errorCondition(paste0("User root directory (projects), currently `",
+                                                          init$projects,"` does not exist.. Abort."),
+                                                   class= "projects-absent"))
+
+    if (nchar(init$metfrag_db_dir)>0L && !dir.exists(init$metfrag_db_dir)) stop(errorCondition("MetFrag DB directory specified, but cannot be found.", class = "mf-db-dir-absent"))
+    if (nchar(init$metfrag_runtime)>0L && !file.exists(init$metfrag_runtime)) stop(errorCondition("MetFrag jar file specified, but cannot be found.", class = "mf-jar-absent"))
+    
+    dir_start <- tempfile("shinyscreen")
+    dir.create(dir_start, recursive = T)
+
+    dir.create(file.path(dir_start,'www'), showWarnings=F)
+    saveRDS(object = init,file=file.path(dir_start,"init.rds"))
+    file.copy(system.file(file.path("rmd","app.Rmd"),package = "shinyscreen"),file.path(dir_start,"app_run.Rmd"))
+    file.copy(system.file(file.path("www","custom.css"),package = "shinyscreen"),file.path(dir_start,"www","custom.css"))
+    dir_start
+}
+
 #' @export
 #' @title app
-#' @param shiny_args `list`, optional list of arguments conveyed to
-#'     `rmarkdown::run` `shiny_args` argument.
-#' @param render_args `list`, optional list of arguments conveyed to
-#'     `rmarkdown::run` `render_args` argument.
+#' @param projects `character(1)`, a location on the server side
+#'     containing project directories.
 #' @param top_data_dir `character(1)`, a location on the server side
 #'     containing data directories.
 #' @param metfrag_db_dir `character(1)`, a location on the server side
 #'     containing MetFrag databases.
-#' @param projects `character(1)`, a location on the server side
-#'     containing project directories.
+#' @param metfrag_runtime `character(1)`, a location on the server side
+#'     of the MetFrag jar file.
+#' @param shiny_args `list`, optional list of arguments conveyed to
+#'     `rmarkdown::run` `shiny_args` argument.
+#' @param render_args `list`, optional list of arguments conveyed to
+#'     `rmarkdown::run` `render_args` argument.
 #' @return Nada.
 #' @author Todor Kondić
-app <- function(shiny_args=list(launch.browser=F),render_args=NULL,top_data_dir=getwd(),projects=getwd(),metfrag_db_dir="") {
-    dir_before <- getwd()
-    init <- list()
-    init$dir_before <- dir_before
-    init$top_data_dir <- norm_path(top_data_dir)
-    init$metfrag_db_dir <- metfrag_db_dir
-    init$projects <- norm_path(projects)
-    if (!dir.exists(init$top_data_dir)) stop("Data directory (top_data_dir), currently `",
-                                        init$top_data_dir,
-                                        "` does not exist. Abort.")
-    if (!dir.exists(init$projects)) stop("User root directory (projects), currently `",
-                                          init$projects,"` does not exist.. Abort.")
+app <- function(projects=getwd(),
+                top_data_dir=getwd(),
+                metfrag_db_dir="",
+                metfrag_runtime="",
+                shiny_args=list(launch.browser=F),
+                render_args=NULL) {
+    dir_before = getwd()
+    message("dir_before: ", dir_before)
+    message("top_data_dir: ", top_data_dir)
+    message("projects: ", projects)
+    dir_start = prepare_app(dir_before=dir_before,
+                            projects=projects,
+                            top_data_dir=top_data_dir,
+                            metfrag_db_dir=metfrag_db_dir,
+                            metfrag_runtime=metfrag_runtime)
+
     on.exit(expr=setwd(dir_before))
-    
-    dir_start <- tempfile("shinyscreen")
-    dir.create(dir_start, recursive = T)
     setwd(dir_start)
-    dir.create('www', showWarnings=F)
-    saveRDS(object = init,file="init.rds")
-    file.copy(system.file(file.path("rmd","app.Rmd"),package = "shinyscreen"),"app_run.Rmd")
-    file.copy(system.file(file.path("www","custom.css"),package = "shinyscreen"),file.path("www","custom.css"))
     rmarkdown::run(file = "app_run.Rmd", shiny_args = shiny_args, render_args = render_args)
 }
 
diff --git a/R/resources.R b/R/resources.R
index 98b115a..041499a 100644
--- a/R/resources.R
+++ b/R/resources.R
@@ -323,4 +323,11 @@ METFRAG_PREPFLT_CHOICES = c("UnconnectedCompoundFilter","IsotopeFilter")
 METFRAG_PREPFLT_DEFAULT = c("UnconnectedCompoundFilter","IsotopeFilter")
 METFRAG_POSTPFLT_CHOICES = c("InChIKeyFilter")
 METFRAG_POSTPFLT_DEFAULT = c("InChIKeyFilter")
-
+METFRAG_STANDARD_SCORES = "FragmenterScore,OfflineIndividualMoNAScore" # AutomatedPeakFingerprintAnnotationScore,
+                                                                       # AutomatedLossFingerprintAnnotationScore
+                                                                       # supposedly
+                                                                       # do
+                                                                       # not
+                                                                       # work
+                                                                       # well.
+METFRAG_STANDARD_WEIGHTS = "1,1"
diff --git a/man/app.Rd b/man/app.Rd
index cf673be..d4ba1d3 100644
--- a/man/app.Rd
+++ b/man/app.Rd
@@ -5,23 +5,32 @@
 \title{app}
 \usage{
 app(
+  projects = getwd(),
+  top_data_dir = getwd(),
+  metfrag_db_dir = "",
+  metfrag_runtime = "",
   shiny_args = list(launch.browser = F),
-  render_args = NULL,
-  indir = getwd(),
-  userdir = getwd()
+  render_args = NULL
 )
 }
 \arguments{
+\item{projects}{\code{character(1)}, a location on the server side
+containing project directories.}
+
+\item{top_data_dir}{\code{character(1)}, a location on the server side
+containing data directories.}
+
+\item{metfrag_db_dir}{\code{character(1)}, a location on the server side
+containing MetFrag databases.}
+
+\item{metfrag_runtime}{\code{character(1)}, a location on the server side
+of the MetFrag jar file.}
+
 \item{shiny_args}{\code{list}, optional list of arguments conveyed to
 \code{rmarkdown::run} \code{shiny_args} argument.}
 
 \item{render_args}{\code{list}, optional list of arguments conveyed to
 \code{rmarkdown::run} \code{render_args} argument.}
-
-\item{indir}{\code{character(1)}, a location on the server side
-containing data directories.}
-
-\item{userdir}{\code{character(1)}, a location on the server side containing project directories.}
 }
 \value{
 Nada.
diff --git a/man/serve.Rd b/man/serve.Rd
index e653a05..531f87e 100644
--- a/man/serve.Rd
+++ b/man/serve.Rd
@@ -4,16 +4,16 @@
 \alias{serve}
 \title{serve}
 \usage{
-serve(indir, topuserdir, user, host = "0.0.0.0", port = 7777)
+serve(top_data_dir, usersdir, user, host = "0.0.0.0", port = 7777)
 }
 \arguments{
-\item{indir}{\code{character(1)}, a location on the server side
+\item{top_data_dir}{\code{character(1)}, a location on the server side
 containing data directories.}
 
-\item{topuserdir}{\code{character(1)}, a location on the server side
+\item{usersdir}{\code{character(1)}, a location on the server side
 containing individual user directories.}
 
-\item{user}{\code{character(1)}, subdir of topuserdir.}
+\item{user}{\code{character(1)}, subdir of usersdir.}
 
 \item{host}{\code{character(1)}, optional, address where the page is
 served.}
diff --git a/tests/testthat/test-api.R b/tests/testthat/test-api.R
new file mode 100644
index 0000000..6c7ca2e
--- /dev/null
+++ b/tests/testthat/test-api.R
@@ -0,0 +1,40 @@
+test_that("Preparation for app start",{
+    dbef = "bef"
+    dir.create("bef")
+    ddata = "topdata"
+    dir.create(ddata)
+    dproj = "proj"
+    dir.create(dproj)
+    ddbdir = "dbdir"
+    dir.create(ddbdir)
+    mrt = "mrt.fake.jar"
+    saveRDS("",file=mrt)
+    expect_error(prepare_app(dir_before=dbef,
+                             projects=dproj,
+                             top_data_dir=ddata,
+                             metfrag_db_dir=ddbdir,
+                             metfrag_runtime="absent.jar"),
+                 class="mf-jar-absent")
+
+    expect_error(prepare_app(dir_before=dbef,
+                             projects="absent",
+                             top_data_dir=ddata,
+                             metfrag_db_dir=ddbdir,
+                             metfrag_runtime=mrt),
+                 class="projects-absent")
+    expect_error(prepare_app(dir_before=dbef,
+                             projects=dproj,
+                             top_data_dir="absent",
+                             metfrag_db_dir=ddbdir,
+                             metfrag_runtime=mrt),
+                 class="top-data-dir-absent")
+    expect_error(prepare_app(dir_before=dbef,
+                             projects=dproj,
+                             top_data_dir=ddata,
+                             metfrag_db_dir="absent",
+                             metfrag_runtime=mrt),
+                 class="mf-db-dir-absent")
+
+
+                             
+})
-- 
GitLab