Repository navigation
Expand file tree
/
Copy pathsimulation.R
More file actions
272 lines (266 loc) · 10.7 KB
/
Copy pathsimulation.R
File metadata and controls
272 lines (266 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
MODEL_CORE_SIMULATION_EXTENSIONS <- "OSPSuite.Core.Domain.ModelCoreSimulationExtensions, OSPSuite.Core"
#' @title Simulation
#' @docType class
#' @description An OSPSuite simulation
#' @format NULL
Simulation <- R6::R6Class(
"Simulation",
cloneable = FALSE,
inherit = ObjectBase,
active = list(
#' @field root Root container of the simulation (read-only)
root = function(value) {
if (missing(value)) {
model <- self$get("Model")
root <- model$get("Root")
Container$new(root)
} else {
private$.throwPropertyIsReadonly("root")
}
},
#' @field path Path of the root container of the simulation (read-only)
path = function(value) {
private$.readOnlyProperty("path", value, self$root$path)
},
#' @field solver SimulationSolver object for the simulation (read-only)
solver = function(value) {
if (missing(value)) {
private$.settings$solver
}
},
#' @field outputSchema outputSchema object for the simulation (read-only)
outputSchema = function(value) {
private$.readOnlyProperty(
"outputSchema",
value,
private$.settings$outputSchema
)
},
#' @field outputSelections outputSelections object for the simulation (read-only)
outputSelections = function(value) {
private$.readOnlyProperty(
"outputSelections",
value,
private$.settings$outputSelections
)
},
#' @field sourceFile Path to the file the simulation was loaded from (read-only)
sourceFile = function(value) {
private$.readOnlyProperty("sourceFile", value, private$.sourceFile)
},
#' @field name Name of the simulation
name = function(value) {
if (missing(value)) {
return(self$get("Name"))
} else {
validateIsString(value)
# Trim the name of ws
value <- trimws(value, which = "both")
# Check if the name actually changes
if (self$name == value) {
return(invisible(self))
}
# Check for forbidden names
forbiddenNames <- .getIllegalSimulationNames(self)
if (value %in% forbiddenNames) {
stop(messages$forbiddenSimulationName(value, self))
}
# Check for illegal characters
illegalChars <- .getIllegalCharacters()
if (any(stringr::str_detect(value, paste0("[", illegalChars, "]")))) {
stop(messages$illegalCharactersInName(value))
}
self$set("Name", value)
}
},
#' @field configuration An object of the type `SimulationConfiguration`,
#' describing the modules used for the simulation, selected Parameter Values (PV) and Initial Conditions (IC).
configuration = function(value) {
# OSP Version number that is required for this feature
supportedVersion <- 12
if (missing(value)) {
# Convert to numeric as the returned value is a string
simVersion <- as.numeric(self$get("Creation")$get("Version"))
if (simVersion < supportedVersion) {
stop(messages$errorFeatureNotSupportedBySimulation(
"SimulationConfiguration",
simVersion,
supportedVersion
))
}
netObj <- self$get("Configuration")
config <- .createSimulationConfigurationFromNetObject(netObj)
# Get calculation method overrides for molecules and apply them to the config.
moleculeNames <- unique(c(
self$allFloatingMoleculeNames(),
self$allStationaryMoleculeNames()
))
for (moleculeName in moleculeNames) {
pcMethod <- self$calculationMethodFor(
moleculeName,
CalculationMethodCategories$PartitionCoefficient
)
if (!is.null(pcMethod)) {
config$setPartitionCoefficientMethods(moleculeName, pcMethod)
}
cpMethod <- self$calculationMethodFor(
moleculeName,
CalculationMethodCategories$CellularPermeability
)
if (!is.null(cpMethod)) {
config$setCellularPermeabilityMethods(moleculeName, cpMethod)
}
}
return(config)
} else {
private$.throwPropertyIsReadonly("configuration")
}
},
#' @field isPopulation `TRUE` if a population is currently assigned to the
#' simulation, meaning it will be run as a population simulation; `FALSE`
#' if it will be run for a single individual. To change this, assign a
#' population (or `NULL`) to the `population` field. (read-only)
isPopulation = function(value) {
private$.readOnlyProperty("isPopulation", value, self$get("IsPopulation"))
},
#' @field population The `Population` assigned to the simulation, or `NULL`
#' if the simulation is run for a single individual.
#'
#' Assigning a `Population` turns the simulation into a population
#' simulation, so that a subsequent `runSimulations(simulation)` runs it for
#' the whole population. Assigning `NULL` removes the population and switches
#' the simulation back to an individual simulation.
#'
#' Any aging data previously set on the simulation is cleared whenever the
#' population is (re)assigned or removed, since aging data only applies to
#' the population it was generated for.
population = function(value) {
if (missing(value)) {
netPopulation <- self$get("IndividualValuesCache")
if (is.null(netPopulation)) {
return(NULL)
}
return(Population$new(netPopulation))
}
# Not using validateIsOfType() here: it inspects the calling frame to build
# its message, which fails when called directly from an R6 active binding.
if (!is.null(value) && !isOfType(value, "Population")) {
stop(messages$errorWrongType(
"population",
class(value)[1],
"Population"
))
}
self$set("IndividualValuesCache", value)
# Aging data is only meaningful together with the population it belongs to.
# Drop it on any (re)assignment so stale aging data cannot be silently
# applied to a different population or to an individual simulation.
self$set("AgingData", NULL)
invisible(self)
}
),
public = list(
#' @description
#' Initialize a new instance of the class
#' @param netObject Reference to `NetObject` .NET simulation object
#' @param sourceFile (Optional) File used to load the simulation
#' @return A new `Simulation` object.
initialize = function(netObject, sourceFile = NULL) {
super$initialize(netObject)
private$.sourceFile <- sourceFile
private$.settings <- SimulationSettings$new(self$get("Settings"))
netTask <- .getCoreTaskFromCache("SimulationTask")
private$.buildConfiguration <- netTask$call(
"CreateSimulationBuilderFor",
netObject
)
},
#' @description
#' Returns the name of all endogenous stationary molecules defined in the simulation. (e.g. with the flag IsStationary = TRUE)
#' This is a typically a molecule that is individual specific such as en Enzyme, Protein, Transporter, FcRn etc.
allEndogenousStationaryMoleculeNames = function() {
private$.buildConfiguration$call(
"AllPresentEndogenousStationaryMoleculeNames"
)
},
#' @description
#' Returns the name of all xenobiotic floating molecules defined in the simulation. (e.g. with the flag IsStationary = FALSE)
#' This is typically a molecule that is being explicitly simulated such as Compound, Inhibitor, DrugComplex.
allXenobioticFloatingMoleculeNames = function() {
private$.buildConfiguration$call(
"AllPresentXenobioticFloatingMoleculeNames"
)
},
#' @description
#' Returns the name of all stationary molecules defined in the simulation. (e.g. with the flag IsStationary = TRUE)
allStationaryMoleculeNames = function() {
private$.buildConfiguration$call("AllPresentStationaryMoleculeNames")
},
#' @description
#' Returns the name of all floating molecules defined in the simulation. (e.g. with the flag IsStationary = FALSE)
allFloatingMoleculeNames = function() {
private$.buildConfiguration$call("AllPresentFloatingMoleculeNames")
},
#' @description
#' Returns the mol weight value (in core unit) associated to the quantity with given path or NA if not found
#' @param quantityPath Path of quantity used to retrieve the molecular weight
molWeightFor = function(quantityPath) {
validateIsString(quantityPath)
mw <- self$call("MolWeightFor", quantityPath)
mw %||% NA_real_
},
#' @description
#' Returns the calculation method name used for the given molecule and
#' category, or `NULL` if no override is set.
#' @param moleculeName Name of the molecule.
#' @param category One of the `CalculationMethodCategories` enum values
#' (e.g. `CalculationMethodCategories$PartitionCoefficient`).
calculationMethodFor = function(moleculeName, category) {
validateIsString(moleculeName)
validateEnumValue(category, CalculationMethodCategories)
supportedVersion <- 12
simVersion <- as.numeric(self$get("Creation")$get("Version"))
if (simVersion < supportedVersion) {
stop(messages$errorFeatureNotSupportedBySimulation(
"calculationMethodFor",
simVersion,
supportedVersion
))
}
self$call("CalculationMethodFor", moleculeName, category)
},
#' @description
#' Returns the applications ordered by start time associated to the quantity with path `quantityPath` or an empty list if not found
#' @param quantityPath Path of quantity used to retrieve the applications (e.g. applications resulting in this quantity being applied)
allApplicationsFor = function(quantityPath) {
validateIsString(quantityPath)
netApplicationParameters <- rSharp::callStatic(
MODEL_CORE_SIMULATION_EXTENSIONS,
"AllApplicationParametersOrderedByStartTimeForQuantityPath",
self,
quantityPath
)
.toObjectType(netApplicationParameters, Application)
},
#' @description
#' Print the object to the console
#' @param printClassProperties Logical, whether to print class properties (default: `FALSE`). If `TRUE`, calls first the `print` method of the parent class.
#' Useful for debugging.
#' @param ... Rest arguments.
print = function(printClassProperties = FALSE, ...) {
if (printClassProperties) {
super$print(...)
}
ospsuite.utils::ospPrintClass(self)
ospsuite.utils::ospPrintItems(list(
"Name" = self$name,
"Source file" = self$sourceFile
))
}
),
private = list(
.sourceFile = NULL,
.buildConfiguration = NULL,
.settings = NULL
)
)