rm(list=()) # remove all variables library(Simcyp) library(ggplot2) library(dplyr)

##------## LOCATIONS ##------simcyp_system_folder <- "C:/Program Files/Simcyp/Simcyp Simulator V15/Screens/SystemFiles/Rat" workspace_folder <- "//certara/data/sites/SHF/Consult/sgt- 1a/Simcyp/Workspaces_Compounds/Phase_4/Rat/Workspaces/" output_folder <- "//certara/data/sites/SHF/Consult/sgt- 1a/Simcyp/RSimcyp/29_100_mpk_PO_final/" code_folder <- "//certara/data/sites/SHF/Consult/sgt- 1a/Simcyp/RSimcyp/29_100_mpk_PO_final/" obs_data_folder <- "//certara/data/sites/SHF/Consult/sgt- 1a/Simcyp/RSimcyp/29_100_mpk_PO_final/Obs_data"

##------## INITIALISE SIMCYP ##------

Simcyp::Initialise (simcyp_system_folder) Version()

##------## LOAD OBSERVED DATA ##------setwd(obs_data_folder) obs_met <- read.csv("ASM_met_PO_100mpk_excluded.csv") colnames(obs_met) <- c("ID", "TIME", "CONC") dose_par <- 100 # dose of parent (mg/kg) mwt_met <- 180.19 # molecular weight of metabolite mwt_par <- 210.28 # molecular weight of parent fa_par <- 0.93 # fraction of parent absorbed dose_met <- dose_par*fa_par*mwt_met/mwt_par dose <- dose_met # dose (mg/kg)

##------## LOAD WORKSPACE ##------setwd(workspace_folder) Simcyp::SetWorkspace('080_ASM_rat_PO_100mpk.wksz')

##------## GENERATE VARIABLE PARAMETER VALUES ##------setwd(workspace_folder) n_trials <- 10 trial_size <- 6 pop_size <- trial_size*n_trials gen_var <- function (mn, cv, n) {

sigma <- sqrt(log(1+cv^2)) #analogous to sd but on log scale (taken from Simcyp induction guides) mu <- log(mn)-(0.5*sigma^2) #analogous to mean but on log scale (taken from Simcyp induction guides) set.seed(1) pop <- rlnorm(n, meanlog=mu, sdlog=sigma) return(pop) }

ASM_ES_liv_scalar <- gen_var(100, 0.3, pop_size) ASM_ES_int_scalar <- gen_var(100, 0.3, pop_size) ASM_ES_plasma_half_life <- gen_var(13.4, 0.3, pop_size) ASM_met_hep_CLint <- gen_var(0.596, 0.3, pop_size) ASM_met_CLR <- gen_var(2.1, 0.3, pop_size) pop_params <- data.frame(ASM_ES_liver_scalar = ASM_ES_liv_scalar, ASM_ES_int_scalar = ASM_ES_int_scalar, ASM_ES_plasma_half_life = ASM_ES_plasma_half_life, ASM_met_hep_CLint = ASM_met_hep_CLint, ASM_met_CLR = ASM_met_CLR)

Simcyp::GetTag("idEsteraseRoutesLiverScalar2", 0, 0) Simcyp::GetTag("idEsteraseRoutesGutScalar3", 0, 0) Simcyp::GetTag("idPlasmaEsteraseRoutesPlasmaHL1", 0, 0) Simcyp::GetTag("idTotalHepCLintValueHep", 0, 4) Simcyp::GetTag("idCLRbase", 0, 4)

##------## PRE-ALLOCATE RESULTS TABLES ##------n_tp <- 2000 sim_end <- 54 # hours

CSub_i <- data.frame(matrix(NA, n_tp*pop_size, 3)) CMet_i <- data.frame(matrix(NA, n_tp*pop_size, 3)) colnames(CSub_i) <- c("ID", "TIME", "CONC") colnames(CMet_i) <- c("ID", "TIME", "CONC")

##------## SETUP AND START SIMULATION ##------for (i in 1:pop_size) {

Simcyp::SetTag("idEsteraseRoutesLiverScalar2", 0, 0, as.numeric(pop_params[i,"ASM_ES_liver_scalar"])) Simcyp::SetTag("idEsteraseRoutesGutScalar3", 0, 0, as.numeric(pop_params[i,"ASM_ES_int_scalar"])) Simcyp::SetTag("idPlasmaEsteraseRoutesPlasmaHL1", 0, 0, as.numeric(pop_params[i,"ASM_ES_plasma_half_life"])) Simcyp::SetTag("idTotalHepCLintValueHep", 0, 4, as.numeric(pop_params[i,"ASM_met_hep_CLint"])) Simcyp::SetTag("idCLRbase", 0, 4, as.numeric(pop_params[i,"ASM_met_CLR"]))

Simcyp::Simulate()

##------## EXTRACT RESULTS ##------

# time <- Simcyp::GetProfile(0, -1, 0) # Simulation time (from Simcyp). Causes problems as each individual can be slightly different

CSub_i[((i-1)*n_tp+1):(i*n_tp),] <- data.frame(ID = rep(i,n_tp), TIME = seq(0, sim_end, length.out = n_tp), CONC = Simcyp::GetProfile(6, 0, 0))

CMet_i[((i-1)*n_tp+1):(i*n_tp),] <- data.frame(ID = rep(i,n_tp), TIME = seq(0, sim_end, length.out = n_tp), CONC = Simcyp::GetProfile(6, 4, 0))

}

# Convert to blood profiles

SubBP <- GetTag("idBP", 0, 0) MetBP <- GetTag("idBP", 0, 4)

CSub_i$CONC <- CSub_i$CONC*SubBP CMet_i$CONC <- CMet_i$CONC*MetBP

# Concentration unit conversion mg/L to ng/mL CSub_i$CONC <- CSub_i$CONC*1000 CMet_i$CONC <- CMet_i$CONC*1000

##------## ANALYSE RESULTS ##------

# Generate summary parameters for each timepoint CSub_summary <- summarise(group_by(CSub_i, TIME), N = n(), Mean = mean(CONC, na.rm = TRUE), Percentile_5 = quantile(CONC, 0.05, na.rm = TRUE), Percentile_95 = quantile(CONC, 0.95, na.rm = TRUE))

CMet_summary <- summarise(group_by(CMet_i, TIME), N = n(), Mean = mean(CONC, na.rm = TRUE), Percentile_5 = quantile(CONC, 0.05, na.rm = TRUE), Percentile_95 = quantile(CONC, 0.95, na.rm = TRUE))

##------## ANALYSE RESULTS - ASM-metabolite ##------setwd(output_folder) out_profile <- CMet_i trapezoid <- function(x,y) { sum(diff(x)*(y[-1]+y[-length(y)]))/2 } # Trapezoidal AUC calculation out_profile <- data.frame(out_profile, DOSE = rep(dose, pop_size)) # Add dose to profiles

# Generate summary parameters for each timepoint out_profile_summary <- summarise(group_by(out_profile, TIME), N = n(), Mean = mean(CONC, na.rm = TRUE), Percentile_5 = quantile(CONC, 0.05, na.rm = TRUE), Percentile_95 = quantile(CONC, 0.95, na.rm = TRUE))

# out_profile <- out_profile[out_profile$TIME >= pk_tstart & out_profile$TIME <= pk_tstop,] # Select data from last dosing interval for PK calculations

# Generate summary parameters for each individual out_ind_params <- summarise(group_by(out_profile, ID), N = n(), # Number of timepoints Cmax = max(CONC), # Cmax (ng/mL) Tmax = TIME[match(max(CONC),CONC)], # Tmax (h) AUC = trapezoid(TIME, CONC), # AUC (ng.h/mL) AUC_period = max(TIME), # AUC period (h) CL = (DOSE[1]*10^6)/AUC) # CL (mL/h/kg) }

# out_ind_params_units <- c("ID", "Timepoints", "ng/mL", "h", "ng.h/mL", "h", "L/h") out_pop_params <- data.frame(Min=sapply(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")], min), # Calculate minimum value for each parameter Max=sapply(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")], max), # Calculate maximum value for each parameter Median=sapply(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")], median), # Calculate median for each parameter Mean=sapply(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")], mean), # Calculate mean for each parameter SD=sapply(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")], sd), # Calculate standard deviation for each parameter

GeoMean=exp(sapply(log(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")]), mean)), # Calculate geometric mean for each parameter

GeoSD=exp(sapply(log(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")]), sd)), # Calculate geometric standard deviation for each parameter # SDln=sapply(log(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")]), sd), # Calculate standard deviation for data Percentile_5 = sapply(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")], quantile, probs = 0.05, na.rm = TRUE), # Calculate the 5th percentile for each parameter Percentile_95 = sapply(out_ind_params[c("Cmax", "Tmax", "AUC", "CL")], quantile, probs = 0.95, na.rm = TRUE)) # Calculate the 5th percentile for each parameter out_pop_params_units <- c("ng/mL", "h", "ng.h/mL", "mL/h/kg") out_pop_params <- data.frame(out_pop_params,

CV=(out_pop_params$SD/out_pop_params$Mean*100), # Calculate % CV for each parameter # GeoCV=sqrt(exp(out_pop_params$SDln^2)- 1)*100, # Calculate % GeoCV for each parameter GeoCV=sqrt(exp(log(out_pop_params$GeoSD)^2)- 1)*100, # Calculate % GeoCV for each parameter Units=out_pop_params_units) out_pop_params <- out_pop_params[c("Units", "Min", "Max", "Median", "Mean", "SD", "CV", "GeoMean", "GeoSD", "GeoCV", "Percentile_5", "Percentile_95")] # Reorder columns .csv(CMet_summary, "CMet_summary.csv") write.csv(out_profile_summary, "out_profile_summary.csv") write.csv(out_ind_params, "out_ind_params.csv") write.csv(out_pop_params, "out_pop_params.csv")

##------## PLOT ##------setwd(output_folder)

# create information for x_axis labels max_time_plot <- 12 x_interval <- 2 # time interval between major axis labels (one minor will be added between major) x_labels <- as.character(seq(0, max_time_plot, x_interval/2)) # create labels major and minor points x_labels[seq(2,length(x_labels),2)] <- "" # add blank labels at every other point i.e. for just minor tick marks at every other point

Sub_plot <- ggplot(data = CSub_summary) + geom_ribbon(aes(x = TIME, ymin = Percentile_5, ymax = Percentile_95), fill = "grey90") + # Shaded ribbon for percentile region geom_line(aes(x = TIME, y = Mean), colour = "black", size = 0.6) + # Solid line for mean geom_line(aes(x = TIME, y = Percentile_5), colour = "grey36", size = 0.15, linetype = 2) + # Solid line for lower percentile geom_line(aes(x = TIME, y = Percentile_95), colour = "grey36", size = 0.15, linetype = 2) + # Solid line for upper percentile labs(x = "\nTime (h)", y = "ASM concentration in blood\n(ng/mL)\n") + scale_x_continuous(limits = c(0, max_time_plot), # set x axis limits breaks = seq(0, max_time_plot, x_interval/2), # define x axis points for all labels (minor and major) labels = x_labels) + # add x axis labels (minor ticks are blank) theme(panel.background = element_rect(fill = "white"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.line.x = element_line(colour = "black"), axis.line.y = element_line(colour = "black"), axis.text = element_text(family = "sans", size = 12, colour = "black"), axis.ticks = element_line(colour = "black")) print(Sub_plot)

# Output as .PNG ggsave(("Sub_Plot.png", sep = ""), width = 14, height = 9, units = "cm", dpi = 600)

Met_plot <- ggplot(data = CMet_summary) + geom_ribbon(aes(x = TIME, ymin = Percentile_5, ymax = Percentile_95), fill = "grey90") + # Shaded ribbon for percentile region geom_line(aes(x = TIME, y = Mean), colour = "black", size = 0.8) + # Solid line for mean geom_line(aes(x = TIME, y = Percentile_5), colour = "black", size = 0.6, linetype = 2) + # Solid line for lower percentile geom_line(aes(x = TIME, y = Percentile_95), colour = "black", size = 0.6, linetype = 2) + # Solid line for upper percentile geom_point(data = obs_met, aes(x = TIME, y = CONC), colour = "black", shape = 1, fill = "white", size = 1.8) + labs(x = "\nTime (h)", y = "ASM-metabolite concentration in blood\n(ng/mL)") + scale_x_continuous(limits = c(0, max_time_plot), # set x axis limits breaks = seq(0, max_time_plot, x_interval/2), # define x axis points for all labels (minor and major) labels = x_labels) + # add x axis labels (minor ticks are blank) theme(panel.background = element_rect(fill = "white"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.line.x = element_line(colour = "black"), axis.line.y = element_line(colour = "black"), axis.text = element_text(family = "sans", size = 12, colour = "black"), axis.ticks = element_line(colour = "black")) print(Met_plot)

# Output graph as .PNG file ggsave(paste("Met_Plot.png", sep = ""), width = 14, height = 9, units = "cm", dpi = 600)