From 3cbce66c354cdc6e259665524e830a00fb535a37 Mon Sep 17 00:00:00 2001 From: koolgax99 Date: Fri, 7 Oct 2022 20:11:23 +0530 Subject: [PATCH] run meta analysis for a settings file --- apps/api/R/entrypoint.R | 3 ++ apps/api/R/ma.R | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 apps/api/R/ma.R diff --git a/apps/api/R/entrypoint.R b/apps/api/R/entrypoint.R index 5f1d8a3fb94..1aec6dcf02b 100755 --- a/apps/api/R/entrypoint.R +++ b/apps/api/R/entrypoint.R @@ -69,6 +69,9 @@ root$mount("/api/runs", runs_pr) runs_pr <- plumber::Plumber$new("available-models.R") root$mount("/api/availableModels", runs_pr) +ma_pr <- plumber::Plumber$new("ma.R") +root$mount("/api/ma", ma_pr) + # set swagger documentation root$setApiSpec("../pecanapi-spec.yml") diff --git a/apps/api/R/ma.R b/apps/api/R/ma.R new file mode 100644 index 00000000000..26330c402b8 --- /dev/null +++ b/apps/api/R/ma.R @@ -0,0 +1,67 @@ +library(dplyr) +library("PEcAn.all") +library("RCurl") + +#' Post a settings file for running a Meta-Analysis +#' @param req Send pecan.xml in bodyas xml filetype +#' @return A list of post.distns.MA.R +#' @author Nihar Sanda +#* @post /run +submitWorkflow <- function(req, res){ + if(req$HTTP_CONTENT_TYPE == "application/xml") { + # read req$bosy as xml + settingsXml <- XML::xmlParseString(stringr::str_replace(req$body, "\n", "")) + + ## convert the xml to a list + settings <- XML::xmlToList(settingsXml) + settings <- as.Settings(settings) + settings <- expandMultiSettings(settings) + + # Update/fix/check settings. + # Will only run the first time it's called, unless force=TRUE + settings <- + PEcAn.settings::prepare.settings(settings, force = FALSE) + + # Changing update to TRUE + settings$meta.analysis$update <- TRUE + + # Write pecan.CHECKED.xml + PEcAn.settings::write.settings(settings, outputfile = "pecan.CHECKED.xml") + + # Do conversions + settings <- PEcAn.workflow::do_conversions(settings) + settings <- PEcAn.workflow::runModule.get.trait.data(settings) + + # initiating variables needed for running meta analysis + pfts <- settings$pfts + iterations <- settings$meta.analysis$iter + random <- settings$meta.analysis$random.effects$on + use_ghs <- settings$meta.analysis$random.effects$use_ghs + threshold <- settings$meta.analysis$threshold + dbfiles <- settings$database$dbfiles + database <- settings$database$bety + + # running meta analysis + run.meta.analysis(pfts, iterations, random, threshold, + dbfiles, database, use_ghs) + + #PEcAn.MA::runModule.run.meta.analysis(settings = ma_settings) + + if(dir.exists(settings$pfts$pft$outdir)){ + filepath <- paste0(settings$pfts$pft$outdir, "/post.distns.Rdata") + e <- new.env(parent = emptyenv()) + load(filepath, envir = e) + objs <- ls(envir = e, all.names = TRUE) + for(obj in objs) { + data <- get(obj, envir =e) + } + #csv_file <- paste0(settings$pfts$pft$outdir, '/post.distns.csv') + #plumber::include_file(csv_file, res) + return(list(status = "Meta Analysis ran successfully", data=data)) + } + } + else{ + res$status <- 415 + return(paste("Unsupported request content type:", req$HTTP_CONTENT_TYPE)) + } +} \ No newline at end of file