-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgetting_started.Rmd
217 lines (180 loc) · 6.16 KB
/
getting_started.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
---
title: "Getting started"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Getting started}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
<!-- github markdown built using
rmarkdown::render("vignettes/getting_started.Rmd", output_format = rmarkdown::github_document())
-->
This vignette demonstrates the basics of running a dyngen simulation.
If you haven't done so already, first check out the installation instructions
in the README.
## Step 1: Define backbone and other parameters
A dyngen simulation can be started by providing a backbone to the `initialise_model()` function.
The backbone of a `dyngen` model is what determines the overall dynamic process
that a cell will undergo during a simulation. It consists of a set of gene modules, which regulate
eachother in such a way that expression of certain genes change over time in a specific manner.
```{r init, message=FALSE, fig.width = 10, fig.height = 6}
library(tidyverse)
library(dyngen)
set.seed(1)
backbone <- backbone_bifurcating()
config <-
initialise_model(
backbone = backbone,
num_tfs = nrow(backbone$module_info),
num_targets = 500,
num_hks = 500,
verbose = FALSE
)
```
```{r quick_config, include=TRUE}
# the simulation is being sped up because rendering all vignettes with one core
# for pkgdown can otherwise take a very long time
set.seed(1)
config <-
initialise_model(
backbone = backbone,
num_cells = 1000,
num_tfs = nrow(backbone$module_info),
num_targets = 50,
num_hks = 50,
verbose = FALSE,
download_cache_dir = tools::R_user_dir("dyngen", "data"),
simulation_params = simulation_default(
total_time = 1000,
census_interval = 2,
ssa_algorithm = ssa_etl(tau = 300/3600),
experiment_params = simulation_type_wild_type(num_simulations = 10)
)
)
```
```{r plotinit, message=FALSE, fig.width = 10, fig.height = 6}
plot_backbone_statenet(config)
plot_backbone_modulenet(config)
```
For backbones with all different sorts of topologies, check `list_backbones()`:
```{r list_backbones}
names(list_backbones())
```
## Step 2: Generate transcription factors (TFs)
Each gene module consists of a set of transcription factors.
These can be generated and visualised as follows.
```{r tf_network}
model <- generate_tf_network(config)
plot_feature_network(model, show_targets = FALSE)
```
## Step 3: Sample target genes and housekeeping genes (HKs)
Next, target genes and housekeeping genes are added to the network by
sampling a gold standard gene regulatory network using the Page Rank algorithm.
Target genes are regulated by TFs or other target genes, while HKs are only regulated
by themselves.
```{r target_network}
model <- generate_feature_network(model)
plot_feature_network(model)
plot_feature_network(model, show_hks = TRUE)
```
## Step 4: Generate kinetics
Note that the target network does not show the effect of some interactions,
because these are generated along with other kinetics parameters of the
SSA simulation.
```{r ssa}
model <- generate_kinetics(model)
plot_feature_network(model)
plot_feature_network(model, show_hks = TRUE)
```
## Step 5: Simulate gold standard
The gold standard is simulated by enabling certain parts of
the module network and performing ODE simulations. The gold standard
are visualised by performing a dimensionality reduction on the
mRNA expression values.
```{r gold_standard}
model <- generate_gold_standard(model)
plot_gold_simulations(model) + scale_colour_brewer(palette = "Dark2")
```
The expression of the modules (average of TFs) can be visualised as follows.
```{r gold_pt, fig.width=10, fig.height=10}
plot_gold_expression(model, what = "mol_mrna") # mrna
plot_gold_expression(model, label_changing = FALSE) # premrna, mrna, and protein
```
## Step 6: Simulate cells.
Cells are simulated by running SSA simulations. The simulations are again
using dimensionality reduction.
```{r simulations}
model <- generate_cells(model)
plot_simulations(model)
```
The gold standard can be overlayed on top of the simulations.
```{r overlay}
plot_gold_simulations(model) + scale_colour_brewer(palette = "Dark2")
```
We can check how each segment of a simulation is mapped to the gold standard.
```{r compare}
plot_gold_mappings(model, do_facet = FALSE) + scale_colour_brewer(palette = "Dark2")
```
The expression of the modules (average of TFs) of a single simulation can be visualised as follows.
```{r expression_sim}
plot_simulation_expression(model, 1:4, what = "mol_mrna")
```
## Step 7: Experiment emulation
Effects from performing a single-cell RNA-seq experiment can be emulated as follows.
```{r experiment}
model <- generate_experiment(model)
```
## Step 8: Convert to a dyno object
Converting the dyngen to a dyno object allows you to visualise the dataset
using the `dynplot` functions, or infer trajectories using `dynmethods`.
```{r wrap}
dataset <- as_dyno(model)
```
### Visualise with `dynplot`
```{r dynplot}
library(dynplot)
plot_dimred(dataset)
plot_graph(dataset)
```
### Save output to file
You can save the output of the model and/or dataset to a file as follows.
```r
write_rds(model, "model.rds", compress = "gz")
write_rds(dataset, "dataset.rds", compress = "gz")
```
## Step 8 alternative: Convert to an anndata/SCE/Seurat object
dyngen 1.0.0 allows converting the output to an `anndata`, `SCE` or `Seurat` object as well.
Check out the [anndata documentation](https://cran.r-project.org/package=anndata)
on how to install anndata for R.
```r
library(anndata)
ad <- as_anndata(model)
ad$write_h5ad("dataset.h5ad")
library(SingleCellExperiment)
sce <- as_sce(model)
write_rds(sce, "dataset_sce.rds")
library(Seurat)
seurat <- as_seurat(model)
write_rds(seurat, "dataset_seurat.rds")
```
# One-shot function
`dyngen` also provides a one-shot function for running
all of the steps all at once and producing plots.
```{r oneshot_run, message=FALSE, fig.width=20, fig.height=20}
out <- generate_dataset(
config,
format = "dyno",
make_plots = TRUE
)
dataset <- out$dataset
model <- out$model
print(out$plot)
```
`dataset` and `model` can be used in much the same way as before.
```{r}
library(dyno)
plot_dimred(dataset)
plot_graph(dataset)
```