|
15 | 15 | #' compatible output. To obtain an output appropriate for d2d [2] "M" must be |
16 | 16 | #' selected. |
17 | 17 | #' @param testSteady Boolean, if "T" the correctness of the obtained steady states is numerically checked (this can be very time intensive). If "F" this is skipped. |
18 | | -#' |
| 18 | +#' @param resolve Boolean. If TRUE, recursive dependencies are resolved, meaning that independent equations are substituted into each expression and then simplified. |
| 19 | +#' |
19 | 20 | #' @return Character vector of steady-state equations. |
20 | 21 | #' |
21 | 22 | #' @references [1] |
|
29 | 30 | #' |
30 | 31 | #' @export |
31 | 32 | #' @importFrom utils write.table |
| 33 | +#' @importFrom reticulate py_require source_python |
32 | 34 | #' @example inst/examples/steadystates.R |
33 | | -steadyStates <- function(model, file=NULL, rates = NULL, forcings = NULL, givenCQs = NULL, neglect=NULL, sparsifyLevel = 2, outputFormat = "R", testSteady = "T") { |
34 | | - |
35 | | - require(reticulate) |
| 35 | +steadyStates <- function(model, file=NULL, rates = NULL, forcings = NULL, givenCQs = NULL, neglect=NULL, sparsifyLevel = 2, outputFormat = "R", testSteady = "T", resolve = TRUE) { |
36 | 36 |
|
37 | 37 | # Check if model is an equation list |
38 | 38 | if (inherits(model, "eqnlist")) { |
39 | 39 | if(is.null(file)) file <- "reactions_for_Alyssa" |
40 | 40 | write.eqnlist(model, file = paste0(file, "_model.csv")) |
41 | 41 | model <- paste0(file, "_model.csv") |
42 | 42 | } |
43 | | - |
| 43 | + |
44 | 44 | if (!is.null(givenCQs) && length(names(givenCQs)) > 0) |
45 | 45 | stop("givenCQs must not have names. Please unname() them.") |
46 | 46 |
|
47 | 47 |
|
48 | 48 | # Calculate steady states. |
49 | | - source_python(system.file("code/AlyssaPetit_ver1.1.py", package = "dMod")) |
| 49 | + reticulate::py_require("sympy") |
| 50 | + reticulate::source_python(system.file("code/AlyssaPetit_ver1.1.py", package = "dMod")) |
50 | 51 | m_ss <- Alyssa(model, as.list(forcings), as.list(givenCQs), as.list(neglect), sparsifyLevel, outputFormat, testSteady) |
51 | 52 |
|
52 | 53 | # Write steady states to disk. |
53 | | - if(length(m_ss)>1){ |
| 54 | + if(length(m_ss)>0){ |
54 | 55 | m_ssChar <- do.call(c, lapply(strsplit(m_ss, "="), function(eq) { |
55 | 56 | out <- eq[2] |
56 | 57 | names(out) <- eq[1] |
| 58 | + |
57 | 59 | return(out) |
58 | 60 | })) |
59 | 61 | if(!is.null(file) & is.character(file)) |
60 | 62 | saveRDS(object = m_ssChar, file = file) |
61 | 63 |
|
| 64 | + if (resolve) { |
| 65 | + simplify <- import("sympy")$simplify |
| 66 | + m_ssChar <- lapply(resolveRecurrence(m_ssChar), function(expr) { |
| 67 | + simplify(expr) %>% as.character() |
| 68 | + }) %>% unlist(use.names = TRUE) |
| 69 | + } |
62 | 70 | return(m_ssChar) |
63 | 71 | } else return(0) |
64 | 72 | } |
|
0 commit comments