---
title: "Red blood cell steady-state distribution"
output:
  bookdown::html_document2:
    base_format: rmarkdown::html_vignette
    code_folding: show
    toc: true
    number_sections: false
pkgdown:
  as_is: true
vignette: >
  %\VignetteIndexEntry{Red blood cell steady-state distribution}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(spleenrbc)
```

Steven Kho circulated a submitted manuscript (*"Retention of uninfected red blood cells causing congestive splenomegaly is the major mechanism of anemia in malaria"*) in which they report:

> The median estimated proportion of total-body RBCs retained in Pf-infected spleens was 8.2% (range:1.0-33.6%), significantly higher than in Pv (2.6% [range:0.6-23.8%]; p=0.015) and PCR-negative subjects (2.5% [range:1.0-3.3%]; p=0.006).

We don't need to run the model in order to calculate the fraction of uninfected red blood cells that will be retained in the spleen at homeostasis.
Instead, we only need to define the model parameters and call `retic_steady_state()`:

```{r ss_spleen}
steady_state_rbc_spleen_pcnt <- function(p) {
  steady_state <- spleenrbc::retic_steady_state(p)
  rbc_spleen <- sum(steady_state$Ur_a)
  rbc_circ <- sum(c(p$R_a_ss, p$N_a_ss))
  100 * rbc_spleen / (rbc_spleen + rbc_circ)
}
```

Here is how we can obtain the baseline parameters, including the extra parameters that are derived from the core parameters.
Note that only a subset of the model parameters are required to calculate the RBC distribution, so we can focus only on this subset:

```{r subset}
only_params <- c(
  "T_urbc", "T_R", "T_M", "T_n",
  "T_R_min", "kappa", "rho0", "rho_slope", "rho_inflection",
  "U_ss", "R_a_ss", "N_a_ss",
  "lambdaU.sel",
  "M_t_1",
  "deltaU_c50", "deltaU_g", "deltaU_kmin", "deltaU_kmax",
  "deltaU_A",
  "deltaUra",
  "p_to_spleen",
  "nu"
)
p <- baseline_parameters(species = "Pf")[only_params]
for (name in names(p)) {
  if (length(p[[name]]) == 1) {
    cat(name, "=", p[[name]], "\n")
  } else {
    cat(name, ": length", length(p[[name]]), "\n")
  }
}
```

So given the baseline parameter values, what fraction of (uninfected) red blood cells will be retained in the spleen at homeostasis?

```{r rbc_pcnt}
pcnt_in_spleen <- steady_state_rbc_spleen_pcnt(p)
cat("RBCs in spleen:", sprintf("%0.2f%%\n", pcnt_in_spleen))
```

We can increase this fraction by adjusting certain parameter values.
For example:

```{r rbc_pcnt_adj}
# Increase the release rate of reticulocytes from the bone marrow.
p$rho0 <- 10 * p$rho0
# Increase the minimum rate of RBC removal into the spleen.
p$deltaU_kmin <- 3 * p$deltaU_kmin
pcnt_in_spleen <- steady_state_rbc_spleen_pcnt(p)
cat("RBCs in spleen:", sprintf("%0.2f%%\n", pcnt_in_spleen))
```
