You are on page 1of 4

rm(list=ls()) # remove all variables

library(Simcyp)
library(ggplot2)
library(dplyr)
library(tidyr)

##-----------------------------------------------------------------------
-------------------
## LOCATIONS
##-----------------------------------------------------------------------
-------------------

output_folder <-
"C:/Users/hburt/Documents/Temp/Syngenta/Human_final_sims/RSimcyp/35_Sim_p
aediatric_CA_acute/Results"
code_folder <-
"C:/Users/hburt/Documents/Temp/Syngenta/Human_final_sims/RSimcyp/35_Sim_p
aediatric_CA_acute/"

##-----------------------------------------------------------------------
-------------------
## LOAD DATA
##-----------------------------------------------------------------------
-------------------

setwd(code_folder)

data <- data.frame(t(read.csv("data.csv", header = FALSE))) # block


of simcyp individual blood concentrations (top row = times), should be in
units of ng/mL

dose_par <- 0.005303 # 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)

n_ind <- dim(data)[2]-1


n_tp <- dim(data)[1]

data <- gather(data, X1) # stack individuals on top


of each other
colnames(data) <- c("TIME", "ID", "CONC")
data$ID <- rep(seq(1,n_ind), each = n_tp) # rename IDs
data <- data[c("ID", "TIME", "CONC")] # reorder columns

pk_tstart <- 0 # time to start PK analysis


pk_tstop <- 72 # time to stop PK analysis
tabove_tstart <- 0 # time to start time above
threshold analysis
tabove_tstop <- 72 # time to stop time above
threshold analysis

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

out_profile <- data

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, n_tp*n_ind))


# 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 ln 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

##-----------------------------------------------------------------------
-------------------
## PLOT
##-----------------------------------------------------------------------
-------------------

setwd(output_folder)

# create information for x_axis labels


# max_time_plot <- max(out_profile_summary$TIME)
max_time_plot <- 24
x_interval <- 4 # 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 at 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

plot <- ggplot(data = out_profile_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 = "Systemic Concentration in Blood (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(plot)

# Output graph as .PNG file


ggsave(paste("Plot", ".png", sep = ""), width = 14, height = 9, units =
"cm", dpi = 600)

##-----------------------------------------------------------------------
-------------------
## OUTPUT
##-----------------------------------------------------------------------
-------------------

write.csv(out_profile, paste("out_profile", ".csv", sep = ""))


write.csv(out_profile_summary, paste("out_profile_summary", ".csv", sep =
""))
write.csv(out_ind_params, paste("out_ind_params", ".csv", sep = ""))
write.csv(out_pop_params, paste("out_pop_params", ".csv", sep = ""))

You might also like