Inputs and Outputs

Alexander Dietrich

library(Seurat)
#> Attaching SeuratObject
library(curl)
#> Using libcurl 7.68.0 with OpenSSL/1.1.1f
library(SimBu)

This chapter covers the different input and output options of the package in detail.

Input

The input for your simulations is always a SummarizedExperiment object. You can create this object with different constructing functions, which will explained below. It is also possible to merge multiple datasets objects into one.
Sfaira is not covered in this vignette, but in “Public Data Integration”.\

Custom data

Using existing count matrices and annotations is already covered in the “Getting started” vignette; this section will explain some minor details.\

When generating a dataset with your own data, you need to provide the count_matrix parameter of dataset(); additionally you can provide a TPM matrix with the tpm_matrix. This will then lead to two simulations, one based on counts and one based on TPMs. For either of them, genes are located in the rows, cells in the columns.
Additionally, an annotation table is needed, with the cell-type annotations. It needs to consist of at least out of 2 columns: ID and cell_type, where ID has to be identical to the column names of the provides matrix/matrices. If not all cells appear in the annotation or matrix, the intersection of both is used to generate the dataset. \

Here is some example data:

counts <- Matrix::Matrix(matrix(rpois(3e5, 5), ncol = 300), sparse = TRUE)
tpm <- Matrix::Matrix(matrix(rpois(3e5, 5), ncol = 300), sparse = TRUE)
tpm <- Matrix::t(1e6 * Matrix::t(tpm) / Matrix::colSums(tpm))
colnames(counts) <- paste0("cell-", rep(1:300))
colnames(tpm) <- paste0("cell-", rep(1:300))
rownames(counts) <- paste0("gene-", rep(1:1000))
rownames(tpm) <- paste0("gene-", rep(1:1000))
annotation <- data.frame(
  "ID" = paste0("cell-", rep(1:300)),
  "cell_type" = c(
    rep("T cells CD4", 50),
    rep("T cells CD8", 50),
    rep("Macrophages", 100),
    rep("NK cells", 10),
    rep("B cells", 70),
    rep("Monocytes", 20)
  ),
  row.names = paste0("cell-", rep(1:300))
)
seurat_obj <- Seurat::CreateSeuratObject(counts = counts, assay = "counts", meta.data = annotation)
tpm_assay <- Seurat::CreateAssayObject(counts = tpm)
seurat_obj[["tpm"]] <- tpm_assay
seurat_obj
#> An object of class Seurat 
#> 2000 features across 300 samples within 2 assays 
#> Active assay: counts (1000 features, 0 variable features)
#>  1 other assay present: tpm

Seurat

It is possible to use a Seurat object to build a dataset; give the name of the assay containing count data in the counts slot, the name of the column in the meta table with the unique cell IDs and the name of the column in the meta table with the cell type identifier. Additionally you may give the name of the assay containing TPM data in the counts slot.

ds_seurat <- SimBu::dataset_seurat(
  seurat_obj = seurat_obj,
  count_assay = "counts",
  cell_id_col = "ID",
  cell_type_col = "cell_type",
  tpm_assay = "tpm",
  name = "seurat_dataset"
)
#> Filtering genes...
#> Created dataset.

h5ad files

It is possible to use an h5ad file directly, a file format which stores AnnData objects. As h5ad files can store cell specific information in the obs layer, no additional annotation input for SimBu is needed.
Note: if you want both counts and tpm data as input, you will have to provide two files; the cell annotation has to match between these two files. As SimBu expects the cells to be in the columns and genes/features in the rows of the input matrix, but this is not necessarily the case for anndata objects https://falexwolf.de/img/scanpy/anndata.svg, SimBu can handle h5ad files with cells in the obs or var layer. If your cells are in obs, use cells_in_obs=TRUE and FALSE otherwise. This will also automatically transpose the matrix. To know, which columns in the cell annotation layer correspond to the cell identifiers and cell type labels, use the cell_id_col and cell_type_col parameters, respectively.\

As this function uses the SimBu python environment to read the h5ad files and extract the data, it may take some more time to initialize the conda environment at the first usage only.

# example h5ad file, where cell type info is stored in `obs` layer
#h5 <- system.file("extdata", "anndata.h5ad", package = "SimBu")
#ds_h5ad <- SimBu::dataset_h5ad(
#  h5ad_file_counts = h5,
#  name = "h5ad_dataset",
#  cell_id_col = 0, # this will use the rownames of the metadata as cell identifiers
#  cell_type_col = "group", # this will use the 'group' column of the metadata as cell type info
#  cells_in_obs = TRUE # in case your cell information is stored in the var layer, switch to FALSE
#) 

Merging datasets

You are able to merge multiple datasets by using the dataset_merge function:

ds <- SimBu::dataset(
  annotation = annotation,
  count_matrix = counts,
  tpm_matrix = tpm,
  name = "test_dataset"
)
#> Filtering genes...
#> Created dataset.
ds_multiple <- SimBu::dataset_merge(
  dataset_list = list(ds_seurat, ds),
  name = "ds_multiple"
)
#> Filtering genes...
#> Created dataset.

Output

The simulation object contains three named entries: \

simulation <- SimBu::simulate_bulk(
  data = ds_multiple,
  scenario = "random",
  scaling_factor = "NONE",
  nsamples = 10, ncells = 100
)
#> Finished simulation.
dim(SummarizedExperiment::assays(simulation$bulk)[["bulk_counts"]])
#> [1] 1000   10
dim(SummarizedExperiment::assays(simulation$bulk)[["bulk_tpm"]])
#> [1] 1000   10

If only the count matrix was given to the dataset initially, only the bulk_counts assay is filled.

sessionInfo()
#> R version 4.2.2 (2022-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.5 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB              LC_COLLATE=C              
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] curl_5.0.0         SeuratObject_4.1.3 Seurat_4.3.0       SimBu_1.0.2       
#> 
#> loaded via a namespace (and not attached):
#>   [1] Rtsne_0.16                  colorspace_2.1-0           
#>   [3] deldir_1.0-6                ellipsis_0.3.2             
#>   [5] ggridges_0.5.4              XVector_0.38.0             
#>   [7] GenomicRanges_1.50.2        spatstat.data_3.0-1        
#>   [9] leiden_0.4.3                listenv_0.9.0              
#>  [11] farver_2.1.1                ggrepel_0.9.3              
#>  [13] fansi_1.0.4                 codetools_0.2-19           
#>  [15] splines_4.2.2               sparseMatrixStats_1.10.0   
#>  [17] cachem_1.0.7                knitr_1.42                 
#>  [19] polyclip_1.10-4             jsonlite_1.8.4             
#>  [21] ica_1.0-3                   cluster_2.1.4              
#>  [23] png_0.1-8                   uwot_0.1.14                
#>  [25] spatstat.sparse_3.0-1       sctransform_0.3.5          
#>  [27] shiny_1.7.4                 compiler_4.2.2             
#>  [29] httr_1.4.5                  Matrix_1.5-3               
#>  [31] fastmap_1.1.1               lazyeval_0.2.2             
#>  [33] cli_3.6.0                   later_1.3.0                
#>  [35] htmltools_0.5.4             tools_4.2.2                
#>  [37] igraph_1.4.1                gtable_0.3.1               
#>  [39] glue_1.6.2                  GenomeInfoDbData_1.2.9     
#>  [41] reshape2_1.4.4              RANN_2.6.1                 
#>  [43] dplyr_1.1.0                 Rcpp_1.0.10                
#>  [45] scattermore_0.8             Biobase_2.58.0             
#>  [47] jquerylib_0.1.4             vctrs_0.5.2                
#>  [49] nlme_3.1-162                spatstat.explore_3.0-6     
#>  [51] progressr_0.13.0            lmtest_0.9-40              
#>  [53] spatstat.random_3.1-4       stringr_1.5.0              
#>  [55] xfun_0.37                   globals_0.16.2             
#>  [57] mime_0.12                   miniUI_0.1.1.1             
#>  [59] lifecycle_1.0.3             irlba_2.3.5.1              
#>  [61] goftest_1.2-3               future_1.32.0              
#>  [63] zlibbioc_1.44.0             MASS_7.3-58.3              
#>  [65] zoo_1.8-11                  scales_1.2.1               
#>  [67] spatstat.utils_3.0-2        promises_1.2.0.1           
#>  [69] MatrixGenerics_1.10.0       parallel_4.2.2             
#>  [71] SummarizedExperiment_1.28.0 RColorBrewer_1.1-3         
#>  [73] yaml_2.3.7                  gridExtra_2.3              
#>  [75] reticulate_1.28             pbapply_1.7-0              
#>  [77] ggplot2_3.4.1               sass_0.4.5                 
#>  [79] stringi_1.7.12              highr_0.10                 
#>  [81] S4Vectors_0.36.2            BiocGenerics_0.44.0        
#>  [83] BiocParallel_1.32.5         GenomeInfoDb_1.34.9        
#>  [85] rlang_1.0.6                 pkgconfig_2.0.3            
#>  [87] matrixStats_0.63.0          bitops_1.0-7               
#>  [89] evaluate_0.20               lattice_0.20-45            
#>  [91] tensor_1.5                  ROCR_1.0-11                
#>  [93] purrr_1.0.1                 patchwork_1.1.2            
#>  [95] htmlwidgets_1.6.1           labeling_0.4.2             
#>  [97] cowplot_1.1.1               tidyselect_1.2.0           
#>  [99] parallelly_1.34.0           RcppAnnoy_0.0.20           
#> [101] plyr_1.8.8                  magrittr_2.0.3             
#> [103] R6_2.5.1                    IRanges_2.32.0             
#> [105] generics_0.1.3              DBI_1.1.3                  
#> [107] DelayedArray_0.24.0         pillar_1.8.1               
#> [109] withr_2.5.0                 fitdistrplus_1.1-8         
#> [111] proxyC_0.3.3                abind_1.4-5                
#> [113] survival_3.5-5              RCurl_1.98-1.10            
#> [115] sp_1.6-0                    tibble_3.2.0               
#> [117] future.apply_1.10.0         KernSmooth_2.23-20         
#> [119] utf8_1.2.3                  spatstat.geom_3.1-0        
#> [121] plotly_4.10.1               rmarkdown_2.20             
#> [123] grid_4.2.2                  data.table_1.14.8          
#> [125] digest_0.6.31               xtable_1.8-4               
#> [127] tidyr_1.3.0                 httpuv_1.6.9               
#> [129] RcppParallel_5.1.7          stats4_4.2.2               
#> [131] munsell_0.5.0               viridisLite_0.4.1          
#> [133] bslib_0.4.2