The {ospsuite}
R-package is part of the Open Systems
Pharmacology Software (OSPS), an open-source suite of modeling and
simulation tools for pharmaceutical and other life-sciences
applications. This package provides the functionality of loading,
manipulating, and simulating the simulations created in the software
tools PK-Sim and MoBi.
This guide demonstrates how to load, run, and visualize PBPK simulations using practical examples.
This tutorial covers:
All examples in this tutorial and the rest of the documentation use the Aciclovir example model (unless stated otherwise), which is included in the package and can be accessed with:
This example demonstrates the essential ospsuite workflow. Detailed explanations follow in subsequent sections.
# Load the ospsuite package
library(ospsuite)
options(ospsuite.plots.watermarkEnabled = FALSE)
# Load the built-in example simulation
simFilePath <- system.file("extdata", "Aciclovir.pkml", package = "ospsuite")
sim <- loadSimulation(simFilePath)
# Run the simulation
results <- runSimulations(sim)
# Create a quick visualization
myDataCombined <- DataCombined$new()
myDataCombined$addSimulationResults(results[[1]])
plotTimeProfile(myDataCombined)This example loads, runs, and visualizes a PBPK simulation. The plot shows the concentration-time profile for Aciclovir in plasma after IV administration.
The following sections detail each workflow step and demonstrate ospsuite capabilities.
Simulations require a .pkml format file (exported from PK-Sim or MoBi):
After loading, explore the simulation structure and parameters:
# Get the simulation tree structure
simTree <- getSimulationTree(sim)
# Explore parameter paths
simTree$Organism$Weight$path
# Find parameters using wildcards
getAllParametersMatching("**|Dose*", sim)
# Or search for specific terms
grep("Dose", getAllParameterPathsIn(container = sim), value = TRUE)Note: Parameter paths in ospsuite match those displayed in PK-Sim or MoBi. Starting with MoBi 12.0, parameter paths can be copied by right-clicking the parameter in the simulation tree and selecting “Copy Path”.
Retrieve and modify parameter values:
# Get a specific parameter
dose <- getParameter(
path = "Events|IV 250mg 10min|Application_1|ProtocolSchemaItem|Dose",
sim
)
print(dose)
# Change the dose value
setParameterValues(dose, 0.004) # New dose: 4 mg/kg
print(dose)
# Scale a parameter by a factor
scaleParameterValues(dose, factor = 2) # Double the dose
print(dose)
# Reset to original value
dose$reset()
print(dose)Adjust simulation outputs and solver settings:
# Originally, the simulation outputs plasma concentrations
sim$outputSelections
# Add new outputs to track
addOutputs(c("Organism|Lumen|Stomach|Aciclovir"), simulation = sim)
# Now, the simulation will compute two outputs
sim$outputSelections
# Adjust solver precision if needed
sim$solver$absTol <- 1e-12
sim$solver$relTol <- 1e-8
# Add custom output intervals for higher resolution
addOutputInterval(
simulation = sim,
startTime = 1440, # 1 day
endTime = 3000, # ~2 days
resolution = 10, # Every 10 minutes
intervalName = "highRes"
)
# Check current output schema
print(sim$outputSchema)After setting parameters, run the simulation:
# Run the simulation
results <- runSimulations(sim)
# Examine the results structure
print(results[[1]])
# Extract specific concentration-time data
plasmaConc <- results[[1]]$getValuesByPath(
"Organism|PeripheralVenousBlood|Aciclovir|Plasma (Peripheral Venous Blood)",
individualIds = 0
)
head(plasmaConc)ospsuite provides multiple visualization options:
# Create a DataCombined object for plotting
myDataCombined <- DataCombined$new()
myDataCombined$addSimulationResults(
results[[1]],
quantitiesOrPaths = "Organism|PeripheralVenousBlood|Aciclovir|Plasma (Peripheral Venous Blood)"
)
# Convert to data frame if needed
df_results <- myDataCombined$toDataFrame()
head(df_results)
# Create publication-ready plots
plotTimeProfile(myDataCombined)library(ggplot2)
# Transform simulation results to a dataframe
results_df <- simulationResultsToDataFrame(
results[[1]],
quantitiesOrPaths = "Organism|PeripheralVenousBlood|Aciclovir|Plasma (Peripheral Venous Blood)"
)
results_df$Time <- results_df$Time / (60 * 24) # Convert to days
ggplot(results_df, aes(x = Time, y = simulationValues)) +
geom_line(color = "blue", size = 1) +
labs(
title = "Aciclovir Plasma Concentration over Time",
x = "Time (days)",
y = "Plasma Concentration (µmol/L)"
) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5))This complete example demonstrates the typical ospsuite workflow:
# 1. Load simulation
simFilePath <- system.file("extdata", "Aciclovir.pkml", package = "ospsuite")
sim <- loadSimulation(simFilePath)
# 2. Modify parameters (e.g., change dose)
dose <- getParameter(
path = "Events|IV 250mg 10min|Application_1|ProtocolSchemaItem|Dose",
sim
)
setParameterValues(dose, 0.006) # 6 mg/kg dose
# 3. Run simulation
results <- runSimulations(sim)
# 4. Visualize results
myDataCombined <- DataCombined$new()
myDataCombined$addSimulationResults(results[[1]])
plotTimeProfile(myDataCombined)
# 5. Extract data for further analysis
df_results <- myDataCombined$toDataFrame()
print(paste("Peak concentration:", round(max(df_results$yValues), 2), "µmol/L"))This section explains the key concepts and object structure in ospsuite.
The {ospsuite} R-package utilizes object-oriented (OO)
programming based on the R6
system. The package offers a functional programming workflow
familiar to R users, but understanding basic OO concepts improves
effectiveness.
Most functions return an instance (or object) of a
class. These objects have properties and methods accessible
with the $ sign:
# Creating an object
myData <- DataCombined$new()
# Accessing a property
simulationName <- myData$name
# Calling a method
myData$addSimulationResults(results[[1]])Call print(object) to display detailed information about
any object.
Understanding these main classes improves workflow effectiveness:
Simulation Objects:
Simulation: Your loaded PBPK model
from the .pkml fileSimulationResults: Output from running
simulationsSimulationRunOptions: Controls for how
simulations run (cores, error checking, progress bars)Model Structure:
Entity: Any part of the model
(parameters, molecules, containers)Container: Model compartments that
contain other entities (organs, compartments)Quantity: Entities with values
(Parameters and Molecules)Parameter: Model parameters you can
modifyMolecule: Drug molecules and their
concentrationsResults and Analysis:
DataCombined: Container for combining
simulation results and observed dataSimulationPKAnalyses: PK parameter
calculations (AUC, Cmax, etc.)Population: Virtual population data
for population simulationsConfiguration:
OutputSchema: Defines when simulation
results are savedOutputSelections: Which quantities to
include in resultsSolverSettings: Numerical solver
configurationThe typical ospsuite workflow follows these steps:
After mastering the basics, explore these advanced topics:
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 format. Unless otherwise stated, the examples shown in the vignettes are based on the Aciclovir example model, which can be found in the PK-Sim examples folder of your OSPS installation.
Important Notes:
For detailed exploration of model structure and parameter access, see Loading a simulation and accessing entities.