<jupyter_start><jupyter_text>Dynamic QSP reporting with R and Jupiter_This is an example of creation of a dynamic QSP report in R and Jupiter based on heta-compiler and mrgsolve. The content of this file and modeling platform is published in GitHub repository _  PreambleThe QSP model which is used as an example of QSP model was published in the article:><NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>. Insulin Signaling in Type 2 Diabetes: Experimental and modeling analyses reveal mechanisms of insulin resistance in human adipocytes. Journal of biological chemistry.. 2013 288(14):9867–9880. DOI: 10.1074/jbc.M112.432062The SBML code was downloaded from BioModels  and used as the part of the [Heta-based](https://hetalang.github.io//) modeling platform.The report includes the steps to reproduce simulations from the original article demonstration facilities of the approach and necessary setups.All necessary files can also be found in [the repository](https://github.com/insysbio/insulin-signaling-t2d).  Preparations 1. Heta compilerFor easier creation of the Heta-based platform install [heta compiler](https://hetalang.github.io//heta-compiler/?id=installation).In command line interface (console) run the code below to create heta platform template```shheta init```The minimal content will be created.Download the SBML model [from the database](https://www.ebi.ac.uk/biomodels/BIOMD0000000448) and copy it into `src/BIOMD0000000448.xml`Update the `src/index.heta` with the following content:```heta// load SBML model as a content of the platforminclude BIOMD0000000448_url.xml type sbml// make the records of a model observableblock {output: true} begin  measuredIRp;  measuredIRint;  measuredIRS1p;  measuredIRS1307;  measuredPKB308p;  measuredPKB473p;  measuredAS160p;  measuredmTORC1a;  measuredS6Kp;  glucoseuptake;  measuredmTORC2a;  measuredS6p;end// make insulin to be an input of the modelinsulin @Const = 10; // nM// make IR (insuline resistance) specific parameters to be input of the modelIR_total @Const = 100;    // reduce to 55%GLUT4_total @Const = 100; // GLUT4 reduce to 50%diabetes @Const = 1;      // reduce to 15%// recalculate initial values for IR and base conditionsIR .= IR_total * 99.87/100; // 99.87IRp .= 0;IRins .= 0;IRip .= IR_total * 0.02/100;      // 0.02IRi .= IR_total * 0.11/100;       // 0.11//GLUT4 .= GLUT4_total * 73.48/100;   // 73.48GLUT4m .= GLUT4_total * 26.52/100;  // 26.52// variable parametersk1a @Const = 0.6331;k1basal @Const = 0.03683;k1c @Const = 0.8768;k1d @Const = 31.01;export { format: Mrgsolve, filepath: _mrgsolve };```  2. Anaconda and JupiterBefore creation of notebook the Jupiter environment must be installed.For installation steps and R support see documentation [part 1](https://docs.anaconda.com/anaconda/navigator/getting-started/) and [part 2](https://docs.anaconda.com/anaconda/navigator/tutorials/r-lang/).This approach installs a separate R environment of version 0.3.6. It will not use your global installation. 3. R-toolsDownload and install the appropriate version of [R-Tools](https://cloud.r-project.org/). If you use R of version 3.6.1 download and install R-tool of version 3.5.See the [compatibility table](https://cran.r-project.org/bin/windows/Rtools/history.html). 4. Notebook runIn windows search and run `anaconda prompt`.Use the name of the created environment and file locations below.```shconda activate snowflakesjupyter notebook --notebook-dir=Y:/PLATFORMS/insulin-signaling-t2d/r-jupiter```Select or create `.ipynb` file.  Loading platformInstall [R](https://www.r-project.org/), [RStudio](https://www.rstudio.com/products/rstudio/) and add all necessary packages:```r must be run once inside R environmentinstall.packages('mrgsolve')install.packages('ggplot2')install.packages('dplyr')```Load the packages<jupyter_code>library('mrgsolve')
library('ggplot2')
library('dplyr')<jupyter_output>Warning message:
"package 'mrgsolve' was built under R version 3.6.3"
Attaching package: 'mrgsolve'

The following object is masked from 'package:stats':

    filter

Warning message:
"package 'ggplot2' was built under R version 3.6.3"Warning message:
"package 'dplyr' was built under R version 3.6.3"
Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union<jupyter_text>The approach uses __mrgsolve__ package as a simulation engine and __Heta compiler__ is a connector to SBML and Heta formats.Heta compiler can be run from the R environment. Mind the current working directory and the location of the Heta-based platform: `..`.<jupyter_code>system('heta build --dist-dir . ..', intern = TRUE)<jupyter_output><empty_output><jupyter_text>After compiling model it can be loaded into mrgsolve.<jupyter_code># This lone set the location of RTools (fir windows).
Sys.setenv(PATH = paste('C:\\Rtools\\mingw_64\\bin', Sys.getenv('PATH'), sep=';'))

m <- mrgsolve::mread(model = 'nameless', file = '../_mrgsolve/nameless.cpp')

# list of observables
observables <- paste(
  'measuredIRp',
  'measuredIRint',
  'measuredIRS1p',
  'measuredIRS1307',
  'measuredPKB308p',
  'measuredPKB473p',
  'measuredAS160p',
  'measuredmTORC1a',
  'measuredS6Kp',
  'glucoseuptake',
  'measuredmTORC2a',
  'measuredS6p',
  sep=', '
)<jupyter_output>Building nameless ... done.<jupyter_text>Default simulation To simulate the observables only one line of code is required. Here we are using “chain” with %>% syntax.<jupyter_code>m %>% mrgsim(end = 30, outvars = observables) %>% plot<jupyter_output><empty_output><jupyter_text>Based on the original publication the insuline resistance (IR) can be set by updating three parameters.<jupyter_code>m %>% param(IR_total=55, GLUT4_total=50, diabetes=0.15) %>% mrgsim(end = 30, outvars = observables) %>% plot<jupyter_output><empty_output><jupyter_text>Simulation scenariosMrgsolve does not operate with "scenario/condition" terms.This functionality can be reproduced by `update` function.<jupyter_code>scn_base <- m %>% update(end = 30, outvars = observables)
scn_ir <- m %>% param(IR_total=55, GLUT4_total=50, diabetes=0.15) %>% update(end = 30, outvars = observables)<jupyter_output><empty_output><jupyter_text>Advanced visualizationIf you need to reshape and visualize different simulation results the easier way is to convert simulation results into `data.frame`.The following simulations reproduce the figures from the original paper.<jupyter_code>results_df_base <- mrgsim(scn_base) %>% as.data.frame
results_df_ir <- mrgsim(scn_ir) %>% as.data.frame


results_df_base$scenario <- 'base'
results_df_ir$scenario <- 'ir'
results_df <- rbind(results_df_base, results_df_ir)
head(results_df)
ggplot(results_df, aes(x = time, y = measuredIRp)) +
  geom_line(aes(col = scenario)) +
  labs(title = "Fig5 1B")
ggplot(results_df, aes(x = time, y = measuredIRint)) +
  geom_line(aes(col = scenario)) +
  labs(title = "Fig5 1C")
ggplot(results_df, aes(x = time, y = measuredIRS1p)) +
  geom_line(aes(col = scenario)) +
  labs(title = "Fig5 2B")
ggplot(results_df, aes(x = time, y = measuredIRS1p)) +
  geom_line(aes(col = scenario)) +
  labs(title = "Fig5 2C")
ggplot(results_df, aes(x = time, y = measuredPKB308p)) +
  geom_line(aes(col = scenario)) +
  labs(title = "Fig5 3B")
ggplot(results_df, aes(x = time, y = measuredPKB473p)) +
  geom_line(aes(col = scenario)) +
  labs(title = "Fig5 3C")<jupyter_output><empty_output><jupyter_text>Titration-like simulationsThe simulations of another type (not time dependence) can be performed and visualized by applying `idata` approach. For example in the original article the titration-like experiment is simulated: intake of insulin and measurements of different observables after 10 minutes.To reproduce the figures the following `idata` table should be created:<jupyter_code>titr_scn_df <- data.frame(ID = 1:22, insulin=c(
  1.00E-03,
  3.16E-03,
  1.00E-02,
  3.16E-02,
  1.00E-01,
  3.16E-01,
  1.00E+00,
  3.16E+00,
  1.00E+01,
  3.16E+01,
  1.00E+02,
  1.00E-03,
  3.16E-03,
  1.00E-02,
  3.16E-02,
  1.00E-01,
  3.16E-01,
  1.00E+00,
  3.16E+00,
  1.00E+01,
  3.16E+01,
  1.00E+02
))
results_titr_base <- scn_base %>%
  idata_set(titr_scn_df) %>%
  mrgsim(end = 10) %>%
  filter(time == 10)
results_titr_ir <- scn_ir %>%
  idata_set(titr_scn_df) %>%
  mrgsim(end = 10) %>%
  filter(time == 10)

results_titr_base$scenario <- 'base'
results_titr_ir$scenario <- 'ir'
results_titr <- rbind(results_titr_base, results_titr_ir)

results_titr$insulin <- titr_scn_df$insulin[match(results_titr$ID, titr_scn_df$ID)]

ggplot(results_titr, aes(x=insulin, y=measuredmTORC2a)) +
  geom_line(aes(col=scenario)) +
  scale_x_log10() +
  labs(title="Fig5 1A")<jupyter_output><empty_output><jupyter_text>Multiple simulations`idata` can also be used to run Monte-Carlo simulations based on parameter variability.For the demonstration purposes we will generate a random set of parameters: k1a, k1basal, k1c, k1d.This simulations mimic the uncertainty in the selected parameters.<jupyter_code>mc_scn_df <- data.frame(
  ID = 1:100,
  k1a = rlnorm(100, mean = 0.6331, sd = 0.5),
  k1basal = rlnorm(100, mean = 0.03683, sd = 0.5),
  k1c = rlnorm(100, mean = 0.8768, sd = 0.5),
  k1d = rlnorm(100, mean = 31.01, sd = 0.5)
)
results_mc_base <- scn_base %>%
  idata_set(mc_scn_df) %>%
  mrgsim(outvars = 'measuredIRp, measuredIRint')
results_mc_ir <- scn_ir %>%
  idata_set(mc_scn_df) %>%
  mrgsim(outvars = 'measuredIRp, measuredIRint')
results_mc_base %>% plot
results_mc_ir %>% plot<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11972491403563613, 'fraction_numerical': 0.046993852245493385, 'mean_word_length': 5.059343434343434, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 66, 'https://': 10, 'lorem ipsum': 0, 'www.': 3, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '515815', 'license': 'MIT', 'n_tokens_mistral': 3688, 'n_tokens_neox': 3260, 'n_words': 922, 'path': 'r-jupiter/notebook.ipynb', 'repo_name': 'insysbio/insulin-signaling-t2d', 'size': '9629'}