R - Introduction

R - Introduction

The ospsuite R-package provides the functionality of loading, manipulating, and simulating the simulations created in the software tools PK-Sim and MoBi. This document gives an overview of the general workflow and the most important methods.

Installing the R-Toolbox

Installation instructions are provided here: https://github.com/Open-Systems-Pharmacology/OSPSuite-R/blob/develop/README.md#installation

General information

In order to load a simulation in R, it must be present in the *.pkml file format. Every simulation in PK-Sim or MoBi can be exported to the *.pkml file. The examples shown are based on the Aciclovir example model, until stated otherwise. The model can be found in the PK-Sim examples folder of the OSPS installation.

The general workflow with the ospsuite package can be summarized in following steps:

  1. Loading a simulation from .pkml file.

  2. Accessing entities of the simulation, such as molecules, parameters, or containers, and reading information about them.

  3. Changing simulation settings, parameter values, and molecules start values.

  4. Retrieving the results and processing them.

The workflow steps are described in the following sections:

Some aspects of the ospsuite package may appear uncommon for the users not familiar with the object-oriented approach. It is recommended to read the following section to better understand some semantics and to get the most of the flexibility and efficiency of the package.

Object-oriented approach

The ospsuite R-package utilizes the concept of object oriented (OO) programming based on the R6 system. While the philosophy of the package is to offer a functional programming workflow more common for the R users, it is important to understand some basic concepts of the OO programming. Most of the functions implemented in ospsuite return an instance (or an object) of a class. These objects can be used as inputs for another functions. Additionally, each object offers a set of properties (which can be another objects) and functions, accessible by the $ sign:

Important information about the object can be printed out by calling print(object).

The most important classes are: Simulation Representation of the simulation loaded from the *.pkml file.

SimulationRunOptions An object defining the options of a simulation run. The options are:

  • numberOfCores the maximal number of (logical) cores that can be used by the (population) simulation

  • checkForNegativeValues a boolean defining if an error if thrown if some variables become negative for which the “negativeValuesAllowed”-flag is set to FALSE (can be used to ignore numerical noise)

  • showProgress a boolean if a “progress bar” is shown in the console representing the progress of the simulation.

SolverSettings Object defining the settings of the solver. Stored in SimulationSettings of a Simulation (accessibly by field $settings).

OutputSchema Definition of the output intervals of the simulation. The OutputSchemadefines the total simulation time and the time points at which results are generated. Can be accessed as property of a Simulation-object.

OutputSelections List of quantities (parameters and molecules) for which the outputs will be generated. Can be accessed as property of a Simulation-object.

SimulationResults Results of a simulation, either individual or population. Holds the simulated values for all quantities defined in the OutputSelections. See Running a simulation for more information.

Entity Every accessible distinct part of the model. Most prominent entities are Molecule, Parameter, Container. Every Entity has properties $path representing the path within the model structure and $parentContainer being the container this entity is located in.

Container A Container is an element of model structure that contains other entities (e.g., spatial containers, molecules, parameters). The most prominent containers are organs and compartments. A loaded Simulation is also a container.

Quantity An Entity in the simulation that has a value - namely Molecule and Parameter. Every Quantity has a $value and a dimension. Further important fields are $unit, which is the base unit of the dimension. See Dimensions and Units for more information.

Molecule A molecule located in a Container. The $value-property refers to the initial value in the simulation. Inherits from Quantity.

Parameter A parameter. The $value-property refers to the initial value in the simulation. Inherits from Quantity.

Formula The value of each Quantity is described by a Formula. There are different types of formulae, see Changing parameter and molecule start values for more information.

IndividualCharacteristics An object used for creating of individual parameter sets with the createIndividual-method. See Creating individuals for more information.

Population An object describing a virtual population. Can be either loaded from a *.csv-file created by PK-Sim or created with the createPopulation-method. See Creating populations for more information.

PopulationCharacteristics An object used for creating of population parameter sets with thecreatePopulation-method. See Creating populations for more information.

SimulationPKAnalyses PK analyses for a simulations result.

QuantityPKParameter A certain PK parameter for an output quantity. Has the fields $name which is the name of the PK parameter (e.g. “C_max”), $quantityPath the path of the output the parameter has been calculated for, and $values the value (or list of values for a population simulation).

SensitivityAnalysis A class defining the analysis of which input parameters have most impact on the output curves of a simulation. See Sensitivity analysis for more information.

SensitivityAnalysisResults Results of running the SensitivityAnalysis

PKParameterSensitivity The sensitivity (field $value) of a PK-Parameter ($pkParameterName) for the output $outputPath calculated for the varied parameter $parameterName. See Sensitivity analysis for more information.

Loading a simulation and accessing entities

Loading a simulation

In general, every workflow starts with loading a simulation by calling the loadSimulation() function. The function receives the full path to the PKML file (with extension *.pkml) exported from PK-Sim or MoBi and returns the corresponding simulation object.

Accessing entities of the model and their properties - the path concept

Once the simulation is loaded, it is possible to retrieve various entities of the model. The most important entities are containers, molecules, and parameters. The methods getContainer, getMolecule, and getParameter search for the respective entity with the given path located under a container. path is a string where the elements of the path (i.e., containers in the hierarchy of the simulation) are separated by |. container is an instance of the Container-class within the model structure the path is relative to. In most cases, container is the Simulation object created by calling loadSimulation(pkmlSimulationFile).

The functions getAllContainersMatching, getAllMoleculesMatching, and getAllParametersMatching return a list of objects representing all entities whose paths match the generic paths provided in the list paths located under container. Generic paths are constructed by using the wildcard symbols * (exactly one occurrence of any element) or ** (zero or more occurrences of any element).

Note that the path "Organism|Kidney|*|Intracellular|Volume" will return no parameters in the standard models, as there are no sub-containers between Kidney and Intracellular. In contrast, "Organism|Kidney|**|Intracellular|Volume" is a valid path.

The functions getAllXXXMatching can also be used to retrieve entities from multiple paths:

The entities possess various properties that can be accessed through their objects. The most important properties for a container are:

The most important properties for a molecule are:

The most important properties for a parameter are:

Loading simulation tree

A convenient way to traverse the simulation structure is given by the method . The method generates a tree-like list of all paths within the simulation. Each element of the tree contains all the sub-containers of the element. The final elements are strings representing the path to the entity.

Changing parameter and molecule start values

Changing initial values

It is possible to change the initial values of all parameters and molecules in a simulation. It is important to differentiate between the constant values and the values that are defined by a formula.

Formula types

Every (initial) value is described either by a constant or by a formula. If the value is defined by a simple constant, the field isConstant of the respective parameter or molecule has the value TRUE.

If the value is not constant, it is described by one of the formula types:

Distributed: Distributed parameters describe a variation around a constant value or between two numerical limits. In the simulation, the behavior equals that of constant values. See Working with Constant and Distributed Parameters‌ for more information.

Explicit formula: The value of the parameter is given by an explicit formula. The value of an explicit formula can change during the simulation. The string of the formula can be accessed via parameter$formulaString.

Table formula: The value of the parameter is given by a table with x-y value pairs. X values usually refer to the simulation time. See Table parameters for additional information on how to retrieve or change the table values.

Additionally, some parameters are modeled as state variables. In this case, the parameter has an initial value and a right hand side (RHS) formula, both of which can be any formula type.

Changing parameters and molecules initial values

The user can change initial values of parameters and molecules with the methods setParameterValues and setMoleculeInitialValues, respectively.

Another way to change parameter values is to scale them. The scaling is always performed relative to the current value:

Only constant values can be set. If the parameters value is defined by a formula (explicit or table), the newly assigned value will override the formula. This will be reflected by the field isFixedValue. Changing values of formula parameters should be done with caution, as the potential dependency on another simulation parameters will be destroyed.

The parameter value can be reset to its formula after assigning a constant value:

Changing the value of a state variable parameter will only change its initial value, but not the right hand side (i.e., the value in the simulation will still change according to the rhs formula). To switch to RHS formula off and, by that, make the parameter be only dependent on its initial value, the field isStateVariable can be set to FALSE. This function is one-way only! It is not possible to re-activate the RHS formula after switching it off.

An example how to set the initial values of molecules in all containers to a certain value:

Running a simulation

Running individual simulation and retrieving the results

Once the simulation is loaded (see Loading a simulation and accessing entities), it can be run to produce an object of the class SimulationResults.

The advantage of storing the results in a object is the option to keep different results of the same simulation produced with different settings (e.g., model parameters).

Simulated time-value pairs for a specific output from the SimulationResults-object can be accessed with the method getOutputValues. The user can provide either the path(s) of the output (which can be a molecule, a parameter, or an observer), or the object(s) of the type Molecule, Parameter, or Quantity (for observers) with the argument quantitiesOrPaths. If no output is specified, all outputs available in the simulation results are returned.

The paths of all available outputs can be accessed via

getOutputValues returns a list with two entries: data and metadata:

  • data is a dataframe with two predefined columns (IndividualId and Time) as well as one column for each requested output

  • IndividualId (not relevant for an individual simulation)

    • Time a vector with simulated time values (in minutes, equal

      for all outputs)

    • a vector with simulated entries for each output requested.

  • metaData is a list containing one entry for each requested output. Each entry contains information pertinent to the output such as its dimension or the unit.

Get simulated results by path

resultsData <- getOutputValues(simulationResults, quantitiesOrPaths = resultsPath)

resultsTime <- resultsData$data$Time resultsValues <- resultsData$data$Organism|PeripheralVenousBlood|Aciclovir|Plasma (Peripheral Venous Blood)

plot(resultsTime, resultsValues, type = "l")

The results can be stored in and imported from a *.csv file with the methods exportResultsToCSV and importResultsFromCSV.

Adding new outputs

By default, only outputs that were selected in PK-Sim or MoBi prior to the export of the simulation to pkml are generated. The user can add new outputs to the simulation with the method addOutputs. The outputs can be provided either as objects of the type(s) Molecule, Parameter, orQuantity, or as path strings. The output list is a property of thesimulation`. After adding or removing outputs, the corresponding simulation needs to be re-run in order to generate updated results.

Changing simulation intervals

The simulation interval (i.e., the simulation times at which results are stored) can are stored as the property outputSchema of a Simulation object.

To change the simulation interval, the user can use one of the functions clearOutputIntervals, addOutputInterval, and setOutputInterval.

Working with individuals

The ospsuite-R package provides an interface to the PK-Sim physiology database to create parameter sets describing a certain individual. By applying these parameter values to a simulation, it is possible to simulate different individuals based on one exported *.pkml simulation. This functionality is only available when PK-Sim is installed on the system.

The easier way to get started is to install the OSPSuite Setup. This will link PK-Sim to your ospsuite-R installation automatically. If the portable version of PK-Sim is used installed, one must specify the path to the PK-Sim folder by calling the function initPKSim.

Creating individuals

The physiology of an individual is defined by the values of the simulation parameters. To simulate a specific individual, one has to generate a set of parameter values and apply these values to the model. NOTE: Currently, only individuals of the same species as in the original *.pkml simulation can be applied. I.e., if the simulation exported from PK-Sim represents a human individual, it is possible to simulate another human individual of different race, gender, etc., but it is not possible to simulate a rat. Though it is technically possible, and the simulation will produce some results, these will not valid.

The first step is creating an object describing individual characteristics. To see the list of available values for the arguments species, population (only for human), and gender (only for human), use the enums Species, HumanPopulation, and Gender, respectively. This object is then passed to the function createIndividual to generate a set of parameter values. The algorithm behind is the same used in PK-Sim when creating an Individual-Building Block.

The output contains two lists of parameters - distributedParameters and derivedParameters. The first list contains parameters that differ between the individuals of the selected species, while the second list mostly consists of values of parameters defined by formulas in the simulation. When applying the generated individual parameter set to a simulation in R, only parameters from the distributedParameters should be overwritten, otherwise formula dependencies may be destroyed. Generated parameter values can be conveniently applied using the setParameterValuesByPath method:

Adding enzyme ontogenies

The PK-Sim database includes ontogeny information for some proteins (see PK-Sim Ontogeny Database). For a protein molecule present in the simulation, it is possible to add the ontogeny information on one of the pre-defined proteins. For example, it is possible to set the ontogeny of the protein MyProtein to the value of the ontogeny of a CYP3A4 enzyme for the specified individual. The list of supported ontogenies is stored in the StandardOntogeny-list.

Working with population simulations

Population simulations

Population simulations can be easily performed in R by combining the simulation loaded from a *.pkml file with the population information created in PK-Sim and exported to CSV format (for details, please refer to Creating Populations) or created directly in R (see Creating populations).

Loading population file

The method loadPopulation creates an object of the Population class that can be passed to the runSimulation method (see Running simulations and retrieving the results).

Creating populations

Similar to creating individual parameter sets (see Creating individuals), a population is created from population characteristics created by calling the method createPopulationCharacteristics. To see the list of available values for the arguments species and population (only for human), use the enums Species and HumanPopulation, respectively. The returned object of type PopulationCharacteristics is then passed to the function createPopulation to generate a set of parameter values. The algorithm behind is the same used in PK-Sim when creating an population. Molecule ontogenies can be added as described in Creating individuals.

Running population simulation

To run a population simulation, the Population object created by the createPopulation method must be passed to the runSimulation method:

Population simulations are run in parallel on multi-core machines - one core simulates a subset of all individuals defined in the population. By default, the number of cores used equals the maximal number of logical cores available minus one. The user can change the default behavior by providing custom SimulationRunOptions.

Simulated time-value pairs for a specific output from the SimulationResults-object returned by the runSimulation method can be accessed with the method getOutputValues. The user can provide either the path(s) of the output (which can be a molecule, a parameter, or an observer), or the object(s) of the type Molecule, Parameter, or Quantity (for observers) with the argument quantitiesOrPaths. If no output is specified, all outputs available in the simulation results are returned. The paths of all available outputs can be accessed via

getOutputValues returns a list with two entries: data and metadata:

  • data is a dataframe with two predefined columns (IndividualId and

    Time) as well as one column for each requested output

  • IndividualId

    • Time a vector with simulated time values (in minutes, equal

      for all outputs)

    • a vector with simulated entries for each output requested.

The values of IndividualId, Time, and the simulated outputs, are appended for each simulated individual. Note that this results in non-monotonously increasing column Time.

To get the results for a specific individual or a set of individuals, the argument individualIds of the method getOutputValues can be specified:

For more information about running simulations, please refer to Running simulations and retrieving the results.

Calculating PK parameters of simulation outputs

PK parameters such as AUC, C_max, etc., can be calculated for all outputs of a simulation. First, simulation results must be calculated, and the SimulationResults object is then passed to the method calculatePKAnalyses. For the list of calculated PK parameters and their description, please refer to PK-Analysis view‌.

The function calculatePKAnalyses returns an object of the class SimulationPKAnalyses, which allows to retrieve either a certain PK parameter for an output path, or all calculated PK parameters for an output path.

The methods $allPKParametersFor and $pKParameterFor return a object (or a list of objects) of the class QuantityPKParameter, with the fields $name which is the name of the pk-parameter (e.g. “C_max”), $quantityPath the path of the output the parameter has been calculated for, and $values the value (or list of values for a population simulation).

In case of a population simulation, $values return a list of values calculated for each individual.

Import and export of PK-analyses

Population analysis calculated in R can be exported to a *.csv file and loaded in PK-Sim, and vice versa.

Sensitivity analysis

The models built with OSPS depend on a lot of input parameters which are based on literature values, measurements, databases, assumptions. For a given set of input parameters a number of output curves is computed in a simulation. To assess which input parameters have most impact on the output curves, a sensitivity analysis of the simulation can be performed. For more information about theoretical background, see Sensitivity Analysis.

In brief, the values of the chosen parameters are changed by a certain percentage and the impact of these changes on PK parameters of model outputs is assessed.

Performing a sensitivity analysis

The first step of performing a sensitivity analysis is creating an object of the class SensitivityAnalysis. At this step, the user can define which parameters should be considered for the sensitivity analysis, in which range the values are varied, and how may steps are performed in one direction (plus and minus). If no parameters are specified, all constant and suitable for sensitivity calculations parameters of the simulation will be variated. The list of such parameters paths for a simulation can be accessed with the method potentialVariableParameterPathsFor(simulation).

New parameters can be added to an existing SensitivityAnalysis by calling the method addParameterPaths. WARNING: If no parameters were specified during the creation of a SensitivityAnalysis (all constant and suitable for sensitivity calculation parameters are considered), calling addParameterPaths will only vary the newly added parameters.

To run the specified SensitivityAnalysis, call the method runSensitivityAnalysis. The method returns an object of the class SensitivityAnalysisResults. The impact of the defined parameters is calculated for PK-Parameters (see PK Analysis for more information) of all model outputs.

The method allPKParameterSensitivitiesFor returns a list of PKParameterSensitivity objects. PKParameterSensitivity describes the sensitivity (field $value) of a PK-Parameter ($pkParameterName) for the output $outputPath calculated for the varied parameter $parameterPath. The argument totalSensitivityThreshold of the method allPKParameterSensitivitiesFor is used to filter out the most impactful parameters. A threshold of 0.9 means that only parameters participating to a total of 90 percent of the sensitivity would be returned. A value of 1 would return the sensitivity for all parameters. If no value is provided, a default value is used.

The value -1 for the sensitivity of “AUC_inf” of the output Organism|PeripheralVenousBlood|Aciclovir|Plasma (Peripheral Venous Blood) for the parameter Organism-Kidney-Volume means that increasing Organism-Kidney-Volume by 10% will decrease “AUC_inf” by 10%. Note that the list of sensitivities is ordered from largest to smallest with respect to magnitude.

Import and export of sensitivity analysis results

Sensitivity analysis calculated in R can be exported to a *.csv file, which can be loaded in another instance.

Table parameters

Parameters defined by a table formula have special syntax of retrieving and changing the values. The value of a table parameter is given by a list of ValuePoint objects, each ValuePoint being an x-y value pair. X values usually refer to the simulation time.

Direct access to the value points is possible through the TableFormula of the table parameter. All x- or y-values stored in the table can be conveniently retrieved using the lapply method:

The method valueAt of the TableFormula returns the value of y for the given x. If no entry exists for the x, the value y is linearly interpolated between the two closest x values.

Changing table parameter values

Simply setting the value of a table-defined parameter using setParameterValues will override the formula and make the parameter constant.

To change the values of the table, a set of methods of the TableFormula is available. The method addPoints adds a set of x-y values to the existing table. If trying to add a point with the x-value already present in the table, an error is thrown:

To remove a point from the table, use the method removePoint. It remove a point if the x value is present in the table and has the provided y.

The method clearPoints removes all points from the table. setPointsis a combination of clearing the table and adding new points:

Dimensions and Units

Unit conversion

Every entity - a molecule, a parameter, or an observer - has a certain dimension, like Amount, Concentration, or Volume. The dimension is a property of an entity:

The values of a certain dimension can be presented in different units - for example, l or ml for the dimension Volume, or mol and µmol for the dimension Amount. The list of all available units is a method of an entity:

Internally, OSPS works with the base units, and all the values that are shown or passed to functions are in base units by default. These base units are often different from the units that are displayed by default in PK-Sim (and MoBi). The list of base and default display units can be found in the documentation.

As an example, the parameter BMI is given in the default unit kg/dm², while the default display unit is the more convenient kg/m². The ospuite-R package provides a set of methods for conversion between different units. The methods toUnit, toBaseUnit, and toDisplayUnit require the quantity to get the correct dimension and units; however, it does not change the value of the quantity!

Last updated