Skip to content

Commit

Permalink
wrap_dataset(): Repurpose function to be able to choose the output …
Browse files Browse the repository at this point in the history
…format using a parameter (#28).
  • Loading branch information
rcannood committed Feb 20, 2021
1 parent af4181f commit 6c4ff01
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 56 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export("%>%")
export(as_anndata)
export(as_dyno)
export(as_list)
export(as_sce)
export(as_seurat)
export(backbone)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# dyngen 1.0.1

## NEW FEATURES

* `wrap_dataset()`: Repurpose function to be able to choose the output format using a parameter (#28).

* `as_list()`: Added a function for converting the dyngen output to a simple list object.

# dyngen 1.0.0

This version mostly upgrades dyngen's ease-of-use, such as better vignettes, conversion functions for working with dyngen datasets in other packages, and more useful ways of specifying platform-specific parameters (i.e. number of cores and cache location). Perhaps more excitingly, the dyngen documentation is more readable online at [https://dyngen.dynverse.org](https://dyngen.dynverse.org)!
Expand Down
122 changes: 104 additions & 18 deletions R/8_convert.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Convert simulation output to different formats.
#'
#' For use with other packages compatible with dyno / anndata.
#' For use with other packages compatible with dyno, anndata, SingleCellExperiment, or Seurat.
#'
#' @param model A dyngen output model for which the experiment has been emulated with [generate_experiment()].
#' @param store_cellwise_grn Whether or not to also store cellwise GRN information.
Expand Down Expand Up @@ -131,23 +131,6 @@ as_dyno <- function(
dataset
}

#' @rdname convert
#' @export
wrap_dataset <- function(
model,
store_dimred = !is.null(model$simulations$dimred),
store_cellwise_grn = !is.null(model$experiment$cellwise_grn),
store_rna_velocity = !is.null(model$experiment$rna_velocity)
) {
.Deprecated("as_dyno")
as_dyno(
model = model,
store_dimred = store_dimred,
store_cellwise_grn = store_cellwise_grn,
store_rna_velocity = store_rna_velocity
)
}


#' @rdname convert
#' @importFrom tibble column_to_rownames
Expand Down Expand Up @@ -386,4 +369,107 @@ as_seurat <- function(
}

seurat_obj
}

#' @rdname convert
#' @export
as_list <- function(
model,
store_dimred = !is.null(model$simulations$dimred),
store_cellwise_grn = !is.null(model$experiment$cellwise_grn),
store_rna_velocity = !is.null(model$experiment$rna_velocity)
) {
assert_that(
!is.null(model$experiment),
msg = "model should be an object that was initialised with `initialise_model()`."
)

counts <- model$experiment$counts_mrna + model$experiment$counts_premrna
dataset <- list(
id = model$id,
cell_ids = rownames(counts),
feature_ids = colnames(counts),
counts = counts,
counts_spliced = model$experiment$counts_mrna,
counts_unspliced = model$experiment$counts_premrna,
counts_protein = model$experiment$counts_protein,
expression = as(log2(counts + 1), "dgCMatrix"),
cell_info = model$experiment$cell_info %>% select(-.data$from, -.data$to, -.data$time),
feature_info = model$experiment$feature_info,
milestone_ids = unique(c(model$gold_standard$network$from, model$gold_standard$network$to)),
milestone_network = model$gold_standard$network,
progressions = model$experiment$cell_info %>%
select(.data$cell_id, .data$from, .data$to, percentage = .data$time)
)

if (store_dimred) {
dimred <- model$simulations$dimred[model$experiment$cell_info$step_ix, , drop = FALSE]
rownames(dimred) <- model$experiment$cell_info$cell_id

dataset$dimred <- dimred
dataset$dimred_segment_points <- model$gold_standard$dimred[!model$gold_standard$meta$burn, , drop = FALSE]
dataset$dimred_segment_progressions <- model$gold_standard$meta[!model$gold_standard$meta$burn, , drop = FALSE] %>%
select(.data$from, .data$to, percentage = .data$time)
}

# add a few more values
if (store_cellwise_grn) {
regulatory_network <- model$feature_network %>%
select(regulator = .data$from, target = .data$to, .data$strength, .data$effect)
regulation_sc <- model$experiment$cellwise_grn

regulators <- unique(regulatory_network$regulator)
targets <- colnames(dataset$counts)
regulatory_network_sc <-
Matrix::summary(regulation_sc) %>%
transmute(
cell_id = factor(dataset$cell_ids[.data$i], levels = dataset$cell_ids),
regulator = factor(regulatory_network$regulator[.data$j], levels = regulators),
target = factor(regulatory_network$target[.data$j], levels = targets),
strength = .data$x
) %>%
as_tibble()


dataset$regulatory_network <- regulatory_network
dataset$regulatory_network_sc <- regulatory_network_sc
dataset$regulators <- regulators
dataset$targets <- targets
}

if (store_rna_velocity) {
dataset$rna_velocity <- model$experiment$rna_velocity
}

dataset
}

conversion_funs <- list(
dyno = as_dyno,
sce = as_sce,
seurat = as_seurat,
anndata = as_anndata,
list = as_list,
none = function(...) NULL
)

#' @rdname convert
#' @param format Which output format to use, must be one of 'dyno', 'sce', 'seurat', 'anndata', 'list' or 'none'.
#' @export
wrap_dataset <- function(
model,
format = c("dyno", "sce", "seurat", "anndata", "list", "none"),
store_dimred = !is.null(model$simulations$dimred),
store_cellwise_grn = !is.null(model$experiment$cellwise_grn),
store_rna_velocity = !is.null(model$experiment$rna_velocity)
) {
format <- match.arg(format, choices = c("dyno", "sce", "seurat", "anndata", "list", "none"))
fun <- conversion_funs[[format]]

fun(
model = model,
store_dimred = store_dimred,
store_cellwise_grn = store_cellwise_grn,
store_rna_velocity = store_rna_velocity
)
}
40 changes: 13 additions & 27 deletions R/generate_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
#' @param output_dir If not `NULL`, then the generated model and dynwrap
#' dataset will be written to files in this directory.
#' @param make_plots Whether or not to generate an overview of the dataset.
#' @param dataset_format Which format to store the output dataset in.
#' Must be one of "dyno", "sce", "seurat", "anndata" or "none".
#'
#' @inheritParams as_dyno
#' @inheritParams wrap_dataset
#'
#' @importFrom patchwork wrap_plots plot_annotation plot_layout
#' @importFrom ggplot2 ggsave
Expand Down Expand Up @@ -54,16 +52,16 @@
#' # dynplot::plot_dimred(dataset)
#' }
generate_dataset <- function(
model,
model,
format = c("dyno", "sce", "seurat", "anndata", "list", "none"),
output_dir = NULL,
make_plots = FALSE,
store_dimred = model$simulation_params$compute_dimred,
store_cellwise_grn = model$simulation_params$compute_cellwise_grn,
store_rna_velocity = model$simulation_params$compute_rna_velocity,
dataset_format = c("dyno", "sce", "seurat", "anndata", "none")
store_rna_velocity = model$simulation_params$compute_rna_velocity
) {
assert_that(is(model, "dyngen::init"))
dataset_format <- match.arg(dataset_format)
format <- match.arg(format, choices = c("dyno", "sce", "seurat", "anndata", "list", "none"))

model <- model %>%
generate_tf_network() %>%
Expand All @@ -73,23 +71,14 @@ generate_dataset <- function(
generate_cells() %>%
generate_experiment()

dataset_function <-
case_when(
dataset_format == "dyno" ~ as_dyno,
dataset_format == "sce" ~ as_sce,
dataset_format == "seurat" ~ as_seurat,
dataset_format == "anndata" ~ as_anndata,
dataset_format == "none" ~ NULL,
TRUE ~ stop("invalid dataset format, must be one of 'dyno', 'sce', 'seurat', 'anndata' or 'none'.")
)

if (dataset_format != "none") {
if (model$verbose) cat("Wrapping dataset as ", dataset_format, "\n", sep = "")
dataset <- dataset_function(
if (format != "none") {
if (model$verbose) cat("Wrapping dataset as ", format, "\n", sep = "")
dataset <- wrap_dataset(
model,
store_dimred = store_dimred,
store_cellwise_grn = store_cellwise_grn,
store_rna_velocity = store_rna_velocity
store_rna_velocity = store_rna_velocity,
format = format
)
}

Expand All @@ -98,9 +87,9 @@ generate_dataset <- function(
if (model$verbose) cat("Writing model to file\n")
dir.create(dirname(output_dir), showWarnings = FALSE, recursive = FALSE)

if (dataset_format == "anndata") {
if (format == "anndata") {
dataset$write_h5ad(paste0(output_dir, "dataset.h5ad"))
} else if (dataset_format != "none") {
} else if (format != "none") {
saveRDS(dataset, paste0(output_dir, "dataset.rds"))
}

Expand Down Expand Up @@ -139,10 +128,7 @@ generate_dataset <- function(
}

if (is.null(output_dir)) {
out <- list(model = model)
if (dataset_format != "none") {
out$dataset <- dataset
}
out <- list(model = model, dataset = dataset)
if (make_plots) {
out$plot <- g
}
Expand Down
23 changes: 17 additions & 6 deletions man/convert.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions man/generate_dataset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6c4ff01

Please sign in to comment.