Skip to content

Commit 6d7a913

Browse files
authored
Merge pull request #3053 from koolgax99/run-ma
[WIP] Run meta analysis for a settings file API
2 parents 90ba165 + ec365a2 commit 6d7a913

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

apps/api/R/entrypoint.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ root$mount("/api/availableModels", runs_pr)
7373
runs_pr <- plumber::Plumber$new("posteriors.R")
7474
root$mount("/api/posteriors", runs_pr)
7575

76+
# Run a meta-analysis from an uploaded XML file
77+
# Caution: Minimally tested.
78+
ma_pr <- plumber::Plumber$new("ma.R")
79+
root$mount("/api/ma", ma_pr)
80+
7681
# set swagger documentation
7782
root$setApiSpec("../pecanapi-spec.yml")
7883

apps/api/R/ma.R

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
library(dplyr)
2+
library("PEcAn.all")
3+
library("RCurl")
4+
5+
#' Post a settings file for running a Meta-Analysis
6+
#' @param req Send pecan.xml in body as xml filetype
7+
#' @return A list of post.distns.MA.R
8+
#' @author Nihar Sanda
9+
#* @post /run
10+
submitWorkflow <- function(req, res){
11+
if(req$HTTP_CONTENT_TYPE == "application/xml") {
12+
# read req$bosy as xml
13+
settingsXml <- XML::xmlParseString(stringr::str_replace(req$body, "<?.*?>\n", ""))
14+
15+
## convert the xml to a list
16+
settings <- XML::xmlToList(settingsXml)
17+
settings <- as.Settings(settings)
18+
settings <- expandMultiSettings(settings)
19+
20+
# Update/fix/check settings.
21+
# Will only run the first time it's called, unless force=TRUE
22+
settings <-
23+
PEcAn.settings::prepare.settings(settings, force = FALSE)
24+
25+
# Changing update to TRUE
26+
settings$meta.analysis$update <- TRUE
27+
28+
# Write pecan.CHECKED.xml
29+
PEcAn.settings::write.settings(settings, outputfile = "pecan.CHECKED.xml")
30+
31+
# Do conversions
32+
settings <- PEcAn.workflow::do_conversions(settings)
33+
settings <- PEcAn.workflow::runModule.get.trait.data(settings)
34+
35+
# initiating variables needed for running meta analysis
36+
pfts <- settings$pfts
37+
iterations <- settings$meta.analysis$iter
38+
random <- settings$meta.analysis$random.effects$on
39+
use_ghs <- settings$meta.analysis$random.effects$use_ghs
40+
threshold <- settings$meta.analysis$threshold
41+
dbfiles <- settings$database$dbfiles
42+
database <- settings$database$bety
43+
44+
# running meta analysis
45+
run.meta.analysis(pfts, iterations, random, threshold,
46+
dbfiles, database, use_ghs)
47+
48+
#PEcAn.MA::runModule.run.meta.analysis(settings = ma_settings)
49+
50+
if(dir.exists(settings$pfts$pft$outdir)){
51+
filepath <- paste0(settings$pfts$pft$outdir, "/post.distns.Rdata")
52+
e <- new.env(parent = emptyenv())
53+
load(filepath, envir = e)
54+
return(list(status = "Meta Analysis ran successfully", data = as.list(e))
55+
}
56+
}
57+
else{
58+
res$status <- 415
59+
return(paste("Unsupported request content type:", req$HTTP_CONTENT_TYPE))
60+
}
61+
}

0 commit comments

Comments
 (0)