Skip to content

Commit

Permalink
Show fixes
Browse files Browse the repository at this point in the history
- fixed show function
- vignettes sampling naming conventions imposed
  • Loading branch information
caravagn committed Nov 29, 2023
1 parent 83b53a2 commit 5db3883
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 64 deletions.
1 change: 1 addition & 0 deletions R/plot_forest.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ plot_forest <- function(forest) {

if (nrow(nodes) == 0) {
warning("The forest does not contain any node")
return(ggplot())
} else {
forest_data <- forest$get_nodes() %>%
dplyr::as_tibble() %>%
Expand Down
39 changes: 21 additions & 18 deletions R/simulation_printer.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' @importFrom rlang .data
setMethod("show", "Rcpp_Simulation", function(object) {
# If it can be simulated
sim_status <- (length(object$get_species())
sim_status <- (nrow(object$get_species())
& nrow(object$get_cells()))

# If it has samples assigned
Expand Down Expand Up @@ -93,29 +93,32 @@ setMethod("show", "Rcpp_Simulation", function(object) {

cli::cli_h3(text = paste("Firings:", sum(f_table$fired), 'total'))

nch = f_table$fired %>% nchar %>% max

for(s in my_tab$species)
if(nrow(f_table) > 0)
{
s_ftab = f_table %>% dplyr::filter(species == s) %>% dplyr::arrange(event)
nch = f_table$fired %>% nchar %>% max

if(with_epigenetics)
sprintf(
paste0('\n\tSpecies [%s]: %', nch, 's (death), %', nch, 's (growth) and %', nch, 's (switches)'),
s,
s_ftab$fired[1],
s_ftab$fired[2],
s_ftab$fired[3]
) %>% cat()
else
for(s in my_tab$species)
{
s_ftab = f_table %>% dplyr::filter(species == s) %>% dplyr::arrange(event)

if(with_epigenetics)
sprintf(
paste0('\n\tSpecies [%s]: %', nch, 's (death) and %', nch, 's (growth)'),
paste0('\n\tSpecies [%s]: %', nch, 's (death), %', nch, 's (growth) and %', nch, 's (switches)'),
s,
s_ftab$fired[1],
s_ftab$fired[2]
s_ftab$fired[2],
s_ftab$fired[3]
) %>% cat()

}
else
sprintf(
paste0('\n\tSpecies [%s]: %', nch, 's (death) and %', nch, 's (growth)'),
s,
s_ftab$fired[1],
s_ftab$fired[2]
) %>% cat()

}
}

# cat(" ",
# knitr::kable(object$get_firings(), format = "rst", align = "rcrccc"),
Expand Down
81 changes: 44 additions & 37 deletions vignettes/a02_sampling_simulation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ sim$get_cells(c(400, 400), c(400 + bbox_width, 400 + bbox_width)) %>% head

### Cell division tree for sampled cells

Every sampled cell is linked, at the evolutionary level,
to the other cells that originate from
the same initial cell. It helps to visualise the evolutionary information on the cells that we have sampled as a forest of trees (if one seeded
multiple initial cells). The forest is an object of the S4 class
`SamplesForest`.
Every sampled cell is linked, at the evolutionary level, to the other
cells that originate from the same initial cell. It helps to visualise
the evolutionary information on the cells that we have sampled as a
forest of trees (if one seeded multiple initial cells). The forest is an
object of the S4 class `SamplesForest`.

```{r}
forest <- sim$get_samples_forest()
Expand All @@ -129,10 +129,9 @@ The forest has methods to obtain the nodes of the sampled cells.
forest$get_nodes() %>% head
```

The leaves of the forest are sampled cells, while the
internal nodes are their ancestors. The field `sample`
is not available for internal nodes, and reports the
sample name otherwise.
The leaves of the forest are sampled cells, while the internal nodes are
their ancestors. The field `sample` is not available for internal nodes,
and reports the sample name otherwise.

```{r}
# The leaves in the forest represent sampled cells
Expand All @@ -155,12 +154,17 @@ We can also query the forest about the samples used to build it.
forest$get_samples_info()
```

We can visualise the forest.
This plot reports the cells and, on the y-axis, their time of birth.
We can visualise the forest. This plot reports the cells and, on the
y-axis, their time of birth.

```{r, fig.height=6, fig.width=11}
plot_forest(forest)
```
The plot shows also samples annotations and species but, for a large number of cells, it might be complicated to view the full tree, unless a very large canvas is used. For this reaason, it is possible to subset the tree.

The plot shows also samples annotations and species but, for a large
number of cells, it might be complicated to view the full tree, unless a
very large canvas is used. For this reaason, it is possible to subset
the tree.

```{r, fig.height=6, fig.width=11}
# Extract the subforest linked to sample
Expand All @@ -169,7 +173,8 @@ S_1_1_forest <- forest$get_subforest_for("S_1_1")
plot_forest(S_1_1_forest)
```

In general, these plots can be annotated with extra information, such as the sampling times, and the MRCAs of each sample in the tree.
In general, these plots can be annotated with extra information, such as
the sampling times, and the MRCAs of each sample in the tree.

```{r, fig.height=6, fig.width=11}
# Full plot
Expand All @@ -182,7 +187,6 @@ plot_forest(S_1_1_forest) %>%

## Randomised multi-region samples


```{r}
sim <- new(Simulation, "Randomised")
Expand All @@ -192,7 +196,8 @@ sim$place_cell("A", 500, 500)
sim$run_up_to_size("A", 60000)
```

We include a new clone and let it grow. This new clone has much higher growth rates than its ancestor.
We include a new clone and let it grow. This new clone has much higher
growth rates than its ancestor.

```{r, fig.height=4, fig.width=3}
# Add a new clone
Expand All @@ -205,17 +210,23 @@ current <- plot_tissue(sim)
current
```

Since clone start has been randomised by `choose_cell_in`, we have no exact idea of where to sample to obtain for example, $100$ of its cells. We can look visually at the simulation, but this is slow.
Since clone start has been randomised by `choose_cell_in`, we have no
exact idea of where to sample to obtain for example, $100$ of its cells.
We can look visually at the simulation, but this is slow.

rRACES provides a `bbox_sampler` function to sample bounding boxes that
contain a desired number of cells. The function takes in input:

- a bounding box size;
- the number $n$ of cells to sample for a species of interest.
- a bounding box size;
- the number $n$ of cells to sample for a species of interest.

`bbox_sampler` will attempt a fixed number of times to sample the box, starting from positions occupied by the species of interest. If a box that contains at least $n$ cells is not found within a number of attempts, then the one with the largest number of samples is returned.
`bbox_sampler` will attempt a fixed number of times to sample the box,
starting from positions occupied by the species of interest. If a box
that contains at least $n$ cells is not found within a number of
attempts, then the one with the largest number of samples is returned.

This allows to program sampling without having a clear idea of the tissue conformation.
This allows to program sampling without having a clear idea of the
tissue conformation.

```{r, fig.height=4, fig.width=3}
# A bounding box 50x50 with at least 100 cells of species B
Expand Down Expand Up @@ -267,8 +278,8 @@ plot_forest(forest) %>%

## Two populations with epigenetic state

We are now ready to simulate a model with epigenetic
switches and subclonal expansions.
We are now ready to simulate a model with epigenetic switches and
subclonal expansions.

```{r, fig.height=4, fig.width=3}
sim <- new(Simulation, "Two Populations")
Expand All @@ -291,11 +302,11 @@ We sample before introducing a new clone.
```{r, fig.height=4, fig.width=3}
bbox_width <- 10
sim$sample_cells("A1",
sim$sample_cells("S_1_1",
bottom_left = c(480, 480),
top_right = c(480 + bbox_width, 480 + bbox_width))
sim$sample_cells("B1",
sim$sample_cells("S_1_2",
bottom_left = c(500, 500),
top_right = c(500 + bbox_width, 500 + bbox_width))
Expand Down Expand Up @@ -334,10 +345,10 @@ n_w <- n_h <- 50
ncells <- 0.8*n_w*n_h
bbox <- sim$search_sample("A", ncells, n_w, n_h)
sim$sample_cells("A2", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_2_1", bbox$lower_corner, bbox$upper_corner)
bbox <- sim$search_sample("B", ncells, n_w, n_h)
sim$sample_cells("B2", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_2_2", bbox$lower_corner, bbox$upper_corner)
plot_tissue(sim, num_of_bins = 500)
plot_muller(sim)
Expand Down Expand Up @@ -374,19 +385,18 @@ sim$run_up_to_size("A", 300)
plot_tissue(sim)
```

We use an automatic function `bbox_sampler` to determine bounding boxes
so to collect a dedired number of cells of interest.
We sample

```{r, fig.height=4, fig.width=3}
n_w <- n_h <- 7
ncells <- 0.3*n_w*n_h
# Sampling ncells with random box sampling of boxes of size n_w x n_h
bbox <- sim$search_sample("A", ncells, n_w, n_h)
sim$sample_cells("A1", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_1_1", bbox$lower_corner, bbox$upper_corner)
bbox <- sim$search_sample("A", ncells, n_w, n_h)
sim$sample_cells("B1", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_1_2", bbox$lower_corner, bbox$upper_corner)
plot_tissue(sim)
```
Expand All @@ -412,10 +422,10 @@ Random sampling of cells from both clone `A` and `B`.
```{r, fig.height=4, fig.width=3}
# Sampling with random box sampling
bbox <- sim$search_sample("A", ncells, n_w, n_h)
sim$sample_cells("A2", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_2_1", bbox$lower_corner, bbox$upper_corner)
bbox <- sim$search_sample("B", ncells, n_w, n_h)
sim$sample_cells("B2", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_2_2", bbox$lower_corner, bbox$upper_corner)
plot_tissue(sim)
```
Expand All @@ -437,10 +447,10 @@ With sampling of clones `B` and `C`.
```{r, fig.height=4, fig.width=3}
# Sampling with random box sampling
bbox <- sim$search_sample("B", ncells, n_w, n_h)
sim$sample_cells("A3", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_3_1", bbox$lower_corner, bbox$upper_corner)
bbox <- sim$search_sample("C", ncells, n_w, n_h)
sim$sample_cells("B3", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_3_2", bbox$lower_corner, bbox$upper_corner)
plot_tissue(sim, num_of_bins = 100)
plot_muller(sim)
Expand All @@ -456,6 +466,3 @@ forest <- sim$get_samples_forest()
plot_forest(forest) %>%
annotate_forest(forest)
```

The horizontal line denotes sampling times, and circles the MRCAs of interest.

18 changes: 9 additions & 9 deletions vignettes/a03_growth_models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ knitr::opts_chunk$set(
library(rRACES)
```

rRACES supports two growth models: the "border"-growth model and the homogeneous-growth
rRACES/RACS supports two growth models: the "border"-growth model and the homogeneous-growth
model.

The former exclusively admits duplication of cells that have space to duplicate
Expand Down Expand Up @@ -72,11 +72,11 @@ sim$run_up_to_size("A+", 1000)
bbox_width <- 15
# Takes two samples
sim$sample_cells("A1",
sim$sample_cells("S_1_1",
bottom_left = c(480, 480),
top_right = c(480 + bbox_width, 480 + bbox_width))
sim$sample_cells("B1",
sim$sample_cells("S_1_2",
bottom_left = c(500, 500),
top_right = c(500 + bbox_width, 500 + bbox_width))
Expand All @@ -101,10 +101,10 @@ sim$run_up_to_size("B+", 5000)
ncells <- 0.8*bbox_width*bbox_width
bbox <- sim$search_sample("B", ncells, bbox_width, bbox_width)
sim$sample_cells("A2", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_2_1", bbox$lower_corner, bbox$upper_corner)
bbox <- sim$search_sample("A", ncells, bbox_width, bbox_width)
sim$sample_cells("B2", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_2_2", bbox$lower_corner, bbox$upper_corner)
```

Let us have a look at the simulated tissue and plot the simulation Muller plot.
Expand Down Expand Up @@ -157,11 +157,11 @@ sim$run_up_to_size("A+", 1000)
bbox_width <- 15
# Takes two samples
sim$sample_cells("A1",
sim$sample_cells("S_1_1",
bottom_left = c(480, 480),
top_right = c(480 + bbox_width, 480 + bbox_width))
sim$sample_cells("B1",
sim$sample_cells("S_1_2",
bottom_left = c(500, 500),
top_right = c(500 + bbox_width, 500 + bbox_width))
Expand All @@ -184,10 +184,10 @@ sim$run_up_to_size("B+", 5000)
ncells <- 0.8*bbox_width*bbox_width
bbox <- sim$search_sample("B", ncells, bbox_width, bbox_width)
sim$sample_cells("A2", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_2_1", bbox$lower_corner, bbox$upper_corner)
bbox <- sim$search_sample("A", ncells, bbox_width, bbox_width)
sim$sample_cells("B2", bbox$lower_corner, bbox$upper_corner)
sim$sample_cells("S_2_2", bbox$lower_corner, bbox$upper_corner)
```

Let us have a look at the simulated tissue and plot the simulation Muller plot.
Expand Down

0 comments on commit 5db3883

Please sign in to comment.