-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutilities-individual.R
157 lines (138 loc) · 5.96 KB
/
utilities-individual.R
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
#' Create a parameter set describing an individual and write it to the Excel file
#'
#' @param individualCharacteristics An `IndividualCharacteristics` object
#' describing the individual. See `createIndividualCharacterstics` for more
#' information.
#' @param outputXLSPath Path to the Excel file the parameter set will be written to
#'
#' @seealso createIndividualCharacteristics crateIndividual
#'
#' @examples
#' \dontrun{
#' simulation <- loadSimulation(pathToPKML)
#' humanIndividualCharacteristics <- createIndividualCharacteristics(
#' species = Species$Human, population = HumanPopulation$European_ICRP_2002,
#' gender = Gender$Male, weight = 70
#' )
#' writeIndividualToXLS(humanIndividualCharacteristics, pathToExcelFile)
#' }
#'
#' @export
writeIndividualToXLS <- function(individualCharacteristics, outputXLSPath) {
validateIsString(outputXLSPath)
individual <- createIndividual(individualCharacteristics)
columnNames <- c("Container Path", "Parameter Name", "Value", "Units")
containerPaths <- vector("character", length(individual$distributedParameters$paths))
paramNames <- vector("character", length(individual$distributedParameters$paths))
values <- vector("numeric", length(individual$distributedParameters$paths))
units <- vector("character", length(individual$distributedParameters$paths))
for (i in seq_along(individual$distributedParameters$paths)) {
splittedPath <- .splitParameterPathIntoContainerAndName(individual$distributedParameters$paths[[i]])
containerPaths[i] <- splittedPath$containerPath
paramNames[i] <- splittedPath$parameterName
values[i] <- individual$distributedParameters$values[[i]]
units[i] <- individual$distributedParameters$units[[i]]
}
output <- data.frame(
unlist(containerPaths, use.names = FALSE),
unlist(paramNames, use.names = FALSE),
unlist(as.numeric(values), use.names = FALSE),
unlist(units, use.names = FALSE)
)
colnames(output) <- columnNames
.writeExcel(data = output, path = outputXLSPath)
}
#' Read individual characteristics from file
#'
#' @details Read individual characteristics from an excel sheet
#' and create an `IndividualCharacteristics`-object. The excel sheet must have
#' the columns `IndividualId`, `Species`, `Population`, `Gender`, `Weight [kg]`,
#' `Height [cm]`, `Age [year(s)]`, `Protein`, and `Ontogeny`.
#' @param XLSpath Full path to the excel file
#'
#' @param individualId (String) Id of the individual as stored in the
#' `IndividualId` column.
#' @param sheet Name of the sheet. If `NULL` (default), the first sheet of the
#' file is used.
#' @param nullIfNotFound Boolean. If `TRUE` (default), `NULL` is returned if
#' no entry with the give `individualId` exists. Otherwise, an error is thrown.
#'
#' @return An `IndividualCharacteristics` object
#' @import ospsuite
#' @export
readIndividualCharacteristicsFromXLS <- function(XLSpath, # nolint: object_length_linter.
individualId,
sheet = "IndividualBiometrics",
nullIfNotFound = TRUE) {
validateIsString(c(XLSpath, individualId))
# If no sheet has been specified, read from the first sheet
if (is.null(sheet)) {
sheet <- c(1)
}
columnNames <- c(
"IndividualId", "Species", "Population", "Gender", "Weight [kg]",
"Height [cm]", "Age [year(s)]", "Protein", "Ontogeny"
)
data <- readExcel(path = XLSpath, sheet = sheet)
if (!all(columnNames %in% names(data))) {
stop(messages$errorWrongXLSStructure(XLSpath, columnNames))
}
# Find the row with the given individual id
rowIdx <- which(data$IndividualId == individualId)
if (length(rowIdx) == 0) {
if (nullIfNotFound) {
return(NULL)
}
stop(messages$errorWrongIndividualId(individualId))
}
# Create ontogenies for the proteins
moleculeOntogenies <- .readOntongeniesFromXLS(data[rowIdx, ])
# Create the IndividualCharacteristics object
individualCharacteristics <- ospsuite::createIndividualCharacteristics(
species = data$Species[[rowIdx]], population = data$Population[[rowIdx]],
gender = data$Gender[[rowIdx]],
weight = data$`Weight [kg]`[[rowIdx]],
height = data$`Height [cm]`[[rowIdx]],
age = data$`Age [year(s)]`[[rowIdx]],
moleculeOntogenies = moleculeOntogenies
)
return(individualCharacteristics)
}
#' Apply an individual to the simulation.
#' For human species, only parameters that do not override formulas are applied.
#' For other species, all parameters returned by `createIndividual` are applied.
#'
#' @param individualCharacteristics `IndividualCharacteristics` describing an individual. Optional
#' @param simulation `Simulation` loaded from the PKML file
#' @import ospsuite
#' @export
#'
#' @examples
#' \dontrun{
#' simulation <- loadSimulation(filePath = modelPath)
#' humanIndividualCharacteristics <- createIndividualCharacteristics(
#' species = Species$Human, population = HumanPopulation$European_ICRP_2002,
#' gender = Gender$Male, weight = 70
#' )
#' applyIndividualParameters(humanIndividualCharacteristics, simulation)
#' }
applyIndividualParameters <- function(individualCharacteristics, simulation) {
individual <- ospsuite::createIndividual(individualCharacteristics)
# For human species, only set distributed parameters
allParamPaths <- individual$distributedParameters$paths
allParamValues <- individual$distributedParameters$values
allParamUnits <- individual$distributedParameters$units
# For other species, also add derived parameters
if (individualCharacteristics$species != ospsuite::Species$Human) {
allParamPaths <- c(allParamPaths, individual$derivedParameters$paths)
allParamValues <- c(allParamValues, individual$derivedParameters$values)
allParamUnits <- c(allParamUnits, individual$derivedParameters$units)
}
ospsuite::setParameterValuesByPath(
parameterPaths = allParamPaths,
values = allParamValues,
simulation = simulation,
units = allParamUnits,
stopIfNotFound = FALSE
)
}