Open-Systems-Pharmacology / Forum

Discussion forum for the Open Systems Pharmacology Project
65 stars 19 forks source link

Is there a way to change the population script in PK Sim? #689

Closed kohhc closed 3 years ago

kohhc commented 3 years ago

Hi,

I am trying to change and create my own Asian population in PK Sim, and while I understand that I can set up variability within a specified population for most parameters (eg. lymph flow, gastric pH etc) by using the 'User Defined Variability' function under the population building block, there are certain parameters in which their distribution within a population is fixed to the population database in PK Sim (eg. age, weight and height). I would like to change the way that PK Sim generates the virtual individual's age, height and weight but I do not see anywhere that I could change this.

As an example, I would like to change the way an individual's height and weight is simulated based on his/her age and gender using the following equations: If Male, then Height = 175.32 + (0.1113 age) - (0.0025 age ^ 2), & Weight = math.exp (2.643 + 0.0099 Height) If Female, then Height = 161.66 + (0.1319 age) - (0.0027 age ^ 2), & Weight = math.exp (2.7383 + 0.0091 Height)

Is there anywhere which I could overwrite the original population script with this new one? Appreciate anybody who can offer their help with this! Capture

StephanSchaller commented 3 years ago

You should be able to do this in R and then re-import the population to PK-Sim... Will have to get back with a quick how-to later.

msevestre commented 3 years ago

Agreed with @StephanSchaller . That's the only way to do it at the moment

kohhc commented 3 years ago

I see thank you both for the suggestion. I am not familiar with using R and the OSP Suite Manual did not mention anything about it. Is there a guide/tutorial video that I could follow to get a grasp of how it works or would it be possible if you could give a quick crash course on how I could go about changing the population script to my desired equations using R? Thank you.

msevestre commented 3 years ago

There is a section in the manual specifically for the R toolbox;-)

https://docs.open-systems-pharmacology.org/working-with-r/r-introduction

Check it out. Once you have it running and you can create a population from R, get back to us and we'll proceed with the next steps

kohhc commented 3 years ago

I've followed the instructions under the Section for Creating and Running a Population in R. However, I seem to be having an issue with running the simulation. For some reason, the population that I've created via R (myPopulation) is saved as a list type and I get the following error when I try to execute the following command:

simulationResults <- runSimulation(simulation = sim,population = myPopulation) Error in validateIsOfType(population, Population, nullAllowed = TRUE) : runSimulation: argument 'population' is of type 'list', but expected 'Population'!

On the other hand, there was no issue if I load a .csv file that was created directly from PK Sim (which I termed myPopulation1). Seeing that my original goal was to create a custom population using R instead of relying on the Asian_Tanaka databases' age/height/weight distribution, is there any way to transform myPopulation created in R into the correct data type? Capture

kohhc commented 3 years ago

I added a new line (3rd line) such that the population is the one that is being ran instead of the list:

print(PopulationCharacteristics) myPopulation <- createPopulation(populationCharacteristics = PopulationCharacteristics) myPopulation1 <- myPopulation$population print(myPopulation1)

I'm not sure if this is the correct way to rectify the issue but it doesn't seem to give me any more error when I run the simulation command. Below is the script that I've written:

PopulationCharacteristics <- createPopulationCharacteristics(species = Species$Human, population = HumanPopulation$Asian_Tanaka_1996, numberOfIndividuals = 49, proportionOfFemales = 65, weightMin = NULL, weightMax = NULL, weightUnit = "kg", heightMin = NULL, heightMax = NULL, heightUnit = "cm", ageMin = 20, ageMax = 30, ageUnit = "year(s)", BMIMin = 20, BMIMax = 25, BMIUnit = "kg/m²", gestationalAgeMin = NULL, gestationalAgeMax = NULL, gestationalAgeUnit = "week(s)", moleculeOntogenies = NULL) print(PopulationCharacteristics) myPopulation <- createPopulation(populationCharacteristics = PopulationCharacteristics) myPopulation1 <- myPopulation$population print(myPopulation1)

simFilePath <- file.path(getwd(),"..","Documents","4-Hydroxy-Diclofenac.pkml",fsep = .Platform$file.sep) sim <- loadSimulation(simFilePath) simulationResults <- runSimulation(simulation = sim,population = myPopulation1)

print(simulationResults) simulationResults$allQuantityPaths resultsPath <- simulationResults$allQuantityPaths[[1]] print(resultsPath) resultsData <- getOutputValues(simulationResults, quantitiesOrPaths = resultsPath) resultsTime <- resultsData$data$Time resultsValues <- resultsData$data$Organism|PeripheralVenousBlood|4'-Hydroxy-Diclofenac|Blood Cells plot(resultsTime,resultsValues, type = "l",xlab = "Time [min]", ylab = "Concentration [µM]")

That being said, this population that I've created is still using the age/height/weight distribution from the Asian_Tanaka database in PK Sim. @StephanSchaller @msevestre you mentioned that it is possible to create a custom population with my own age/height/weight distribution in R and re-import the population back to PK Sim. May I know how I could go about doing that? Greatly appreciate the help! :)

msevestre commented 3 years ago

@kohhc Well done in getting the R tool box to run. Now, there is no DIRECT way to change the algorithm as this is not something that was ever requested (e.g. we use body weight and height based on population data instead of some fitted formula)

In order to accomplish what you want to do, you will have to trick the system a bit and create single individuals. You can do this by setting min== max

So for instance let say your formula returns BW = 70kg and height = 175cm then you could try creating a population with one individual (weightMin=70, weightMax=70 etc..) and iterate over the number of individuals you want to create.

Then once you have created your N populations each with one individual, I would advise you to create a new population with N individual and simply overwrite each individual values with the values created above. That way you should have one unique population matching exactly what you need.

This should not be too complicated to get going

kohhc commented 3 years ago

@msevestre I see. Manually defining the population sounds rather tedious but it does seem like the only solution if there's no way to change the internal algorithm. In this case I realized that there's actually no need for me to use R since the solution you proposed can be done using PK Sim.

Nonetheless thank you to both for your help! I should be able to get my desired population with your proposed method. :)

msevestre commented 3 years ago

The big difference is that you can script the creation of the population => if you need to recreate it, add new individuals, change the formula etc ..you simply need to run the script by hand.

Good luck

kohhc commented 3 years ago

@msevestre @StephanSchaller Hi apologies for re-opening the issue again. While it is possible to manually create my desired population using the method you proposed, it may not be a very feasible solution in the long term if I have many different custom populations that I have to manually create each time. Referring to the picture below, new populations have been consistently added into PK Sim over the years, so I am thinking that there must be some way to be able to override the original algorithm and add a custom population with its own database.

Screenshot 2021-03-22 141631

Looking through some of the past issues in the forum, it seems that someone has previously tried to ask the same question as well here: https://github.com/Open-Systems-Pharmacology/Forum/issues/307

It seems like it may be possible to change the exiting values contained in the PKSimDB.sqlite file by using a software like DB Browser for SQLite as shown below though I have yet to try it. Do you think this may be a possible workaround to creating my own custom population?

Screenshot 2021-03-22 142711

msevestre commented 3 years ago

While it is possible to manually create my desired population using the method you proposed, it may not be a very feasible solution in the long term

@kohhc That's why I suggested scripting so that you can always adjust and recreate on the fly. Doing it manually in PK-Sim is definitely not sustainable

You can of course change the DB directly...but this is a dangerous path. You would OVERRIDE the default behavior of the tool... It was never intended of the user to mess around with the DB and if you do, I cannot predict what may go wrong. Also, any update you do will be lost when installing a new version of PKSim (e..g v10 coming in the next few weeks)

Assuming you are brave enough, this should be fairly simple: Since you only want to change Weight and Height as a function of Age, you would only need to update parameters MeanBW and MeanHeight parameters (140 entries for the Tanaka population)

image

You would set the STD to 0 and for each age of interest, set the value of your method above e.g age = 1, mean_value = xx age = 2, mean_value=yy etc

kohhc commented 3 years ago

@msevestre Thank you for your advice noted that that there are certain risks involved with this method. For now, I have tried adjusting the values within the database file and it seems to be working for the most part. However, I do encounter 2 parameters where I have issues trying to change the distribution:

1) Age: Even before making any changes to the database file, I've noticed that the age distribution in my simulated population seems to be rather random (as seen in picture below). However, I was expecting the age distribution in my population to resembles more of a normal distribution. Can I ask how PK Sim generates individual age in a population (is it randomly selecting a number based on your defined range or does it try to follow a normal distribution)? Is there also any way that one might be able to change this if it is not normally distributed?

Capture

2) Liver volume: I understand that liver volume is calculated based on the generated age, height and weight of the individual in PK Sim. May I know what is the exact equation that PK Sim uses to calculate this liver volume? Asking this because I am thinking of incorporating my own equation (as shown below), but I am unsure where to find and how to change the default liver volume equation in PK Sim.

Liver Volume = 0.722 * BSA^1.176

msevestre commented 3 years ago

@kohhc

However, I was expecting the age distribution in my population to resembles more of a normal distribution.

I don't know anything about physiology etc.. but I would not expect age to be normally distributed. In PK-Sim, it is uniformly distributed (e.g. you have has many 20-25 years old as 45-50 years old as 70-75 years old).

This cannot be changed as it is part of the actual algorithm.

2/ There are no equations. It is all based on population data. By setting an equation like above as a function of BSA, you are losing variability etc...

What you would have to do in that case is generate the population (in PKSim or R). Then load the population in R and adjusted the liver volume (and of course compensate for the difference with for instance fat volume). This can only be done in R.

However, such a population is by no means qualified. Is there any reason why you do not want to use the Japanese population that comes out of the box?