---
title: "Uninfected red blood cell loss due to malaria"
output:
  bookdown::html_document2:
    base_format: rmarkdown::html_vignette
    code_folding: show
    toc: true
    number_sections: false
pkgdown:
  as_is: true
vignette: >
  %\VignetteIndexEntry{Uninfected red blood cell loss due to malaria}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center",
  fig.width = 7,
  fig.height = 5,
  fig.asp = NULL,
  out.width = "95%"
)
```

```{r setup}
library(spleenrbc, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(tidyr)
library(ggplot2)
```

A malaria infection can cause uninfected red blood cells to be "lost" from circulation.
This can manifest as a direct effect — becoming infected — but also as an indirect effect where the infection leads to an increased retention of uninfected red blood cells in the spleen.

We can use this model to examine the relative contributions of these different processes to the overall loss.
Here, we will use the baseline parameter values for a Pf infection:

```{r load-baseline, results = 'hide'}
scenario <- baseline_parameters(species = "Pf")
df_base <- run_spleenrbc(scenario, days = 150) |>
  infection_flows_and_loss() |>
  pivot_longer(! c("time", "scenario")) |>
  mutate(
    sim_no = factor(scenario),
    days = time / 24
  )
```

# Decrease in circulating RBCs

We can plot the circulating (uninfected) RBC population over time, to see how it changes over the course of the infection:

```{r uc-popn, class.source = 'fold-hide', fig.cap = 'Circulating uRBC population. The horizontal dashed line show the baseline value (no infection).'}
ggplot(df_base |> filter(name == "U_t"),
       aes(days, value)) +
  geom_line() +
  geom_hline(
    yintercept = df_base |>
      filter(name == "U_t", time == 0) |>
      pull(value),
    linetype = "dashed"
  ) +
  xlab("Time (days)") +
  scale_y_log10("Circulating uRBC population")
```

We can also convert from RBC counts to haemoglobin levels:

```{r uc-hg, class.source = 'fold-hide', fig.cap = 'Circulating haemoglobin. The horizontal dashed line show the baseline value (no infection).'}
df_hg <- df_base |>
  filter(name == "U_t") |>
  mutate(value = rbc_counts_to_haemoglobin(value))
initial_hg <- df_hg$value[1]

ggplot(df_hg, aes(days, value)) +
  geom_line() +
  geom_hline(
    yintercept = initial_hg,
    linetype = "dashed"
  ) +
  xlab("Time (days)") +
  scale_y_log10("Haemoglobin (g/dL)")
```

# RBC retention in the spleen

We can plot the rate at which red blood cells are moved from the circulation into the spleen, and how much of this is due to the malaria infection:

```{r urbc-retention, class.source = 'fold-hide', fig.cap = 'Rate of uRBC retention in the spleen. Baseline retention, in the absence of infection, is indicated by the horizontal dashed line.'}
urbc_flows <- c("flow_uc_to_r", "flow_uc_to_r_inf", "flow_uc_to_r_no_inf")
urbc_baseline_retention <- df_base |>
  filter(name == "flow_uc_to_r", time == 1) |>
  pull(value)
ggplot(df_base |>
         filter(name %in% urbc_flows, time > 0),
       aes(days, value, colour = name)) +
  geom_line() +
  geom_hline(
    yintercept = urbc_baseline_retention,
    linetype = "dashed"
  ) +
  xlab("Time (days)") +
  scale_y_log10("RBC movement (per hour)") +
  scale_colour_hue(
    name = NULL,
    labels = c("Total retention", "Due to infection", "Baseline")
  ) +
  theme(legend.position = "top") +
  theme(legend.text = element_text(margin = margin(r = 12)))
```

**Important:** note that the baseline RBC retention rate is sensitive to the age distribution of RBCs in the circulation, and this age distribution changes over the course of an infection.

# RBC infection in the circulation and spleen

We can plot the rate at which red blood cells are being infected in the circulation and in the spleen:

```{r urbc-inf, class.source = 'fold-hide', fig.cap = 'Rates of uRBC infection in the circulation and spleen.'}
urbc_infs <- c("flow_uc_to_ic", "flow_ur_to_ir")
ggplot(df_base |>
         filter(name %in% urbc_infs, time > 0),
       aes(days, value, colour = name)) +
  geom_line() +
  xlab("Time (days)") +
  scale_y_log10("RBC infections (per hour)") +
  scale_colour_hue(
    name = NULL,
    labels = c("uRBCs in circulation", "uRBCs in spleen")
  ) +
  theme(legend.position = "top") +
  theme(legend.text = element_text(margin = margin(r = 12)))
```

# RBC infection by source of merozoites

We can plot the rate at which red blood cells (in the circulation and spleen) are being infected by merozoites released from infected cells in the circulation, in the microvasculature, and in the spleen:

```{r urbc-inf-src, class.source = 'fold-hide', fig.cap = 'Rates of uRBC infection, categorised by the source of infection.'}
urbc_inf_src <- c("inf_from_ic", "inf_from_iq", "inf_from_ir")
ggplot(df_base |>
         filter(name %in% urbc_inf_src, time > 0),
       aes(days, value, colour = name)) +
  geom_line() +
  xlab("Time (days)") +
  scale_y_log10("RBC infections (per hour)") +
  scale_colour_hue(
    name = NULL,
    labels = c("Circulation", "Microvasculature", "Spleen")
  ) +
  theme(legend.position = "top") +
  theme(legend.text = element_text(margin = margin(r = 12)))
```

# RBC destruction in the spleen

We can plot the rate at which uninfected and infected red blood cells are destroyed in the spleen:

```{r rbc-destr, class.source = 'fold-hide', fig.cap = 'Rates of RBC destruction in the spleen.'}
rbc_destr <- c("flow_ir_out", "flow_ur_out")
ggplot(df_base |>
         filter(name %in% rbc_destr, time > 0),
       aes(days, value, colour = name)) +
  geom_line() +
  xlab("Time (days)") +
  scale_y_log10("RBC destruction (per hour)") +
  scale_colour_hue(
    name = NULL,
    labels = c("Infected RBCs", "Uninfected RBCs")
  ) +
  theme(legend.position = "top") +
  theme(legend.text = element_text(margin = margin(r = 12)))
```

# Ratio of RBC retention to infection

We can plot the ratio of the rate at which RBCs are retained in the spleen to the rate at which RBCs are infected:

```{r urbc-ratios, class.source = 'fold-hide', fig.cap = 'Ratios of RBC retention to infection.'}
urbc_ratios <- c("Uc_loss_ratio_c", "Uc_loss_ratio_cr")
ggplot(df_base |>
         filter(name %in% urbc_ratios, time > 0),
       aes(days, value, colour = name)) +
  geom_line() +
  xlab("Time (days)") +
  scale_y_log10("Ratio of retention : infection") +
  scale_colour_hue(
    name = "Infections",
    labels = c("Circulation", "Circulation and spleen")
  ) +
  theme(legend.position = "top") +
  theme(legend.text = element_text(margin = margin(r = 12)))
```

# Comparison of retention and infection

We can compare the rates at which RBCs are being retained and infected (similar to Figure 6b of the draft manuscript):

```{r urbc-cmp, class.source = 'fold-hide', fig.cap = 'Comparison of RBC retention and RBC infection rates.'}
urbc_cmp <- c("flow_uc_to_r_inf", "inf_from_ic", "inf_from_iq", "inf_from_ir", "inf_total")
ggplot(df_base |>
         filter(name %in% urbc_cmp, time > 0),
       aes(days, value, colour = name)) +
  geom_line() +
  xlab("Time (days)") +
  scale_y_log10("Rate of RBC retention or infection") +
  scale_colour_hue(
    name = NULL,
    labels = c(
      "Retention",
      "Inf: circulation",
      "Inf: microvasculature",
      "Inf: spleen",
      "Inf: total"
    )
  ) +
  theme(legend.position = "top") +
  theme(legend.text = element_text(margin = margin(r = 12)))
```
