all_times <- list()  # store the time for each chunk
knitr::knit_hooks$set(time_it = local({
  now <- NULL
  function(before, options) {
    if (before) {
      now <<- Sys.time()
    } else {
      res <- difftime(Sys.time(), now, units = "secs")
      all_times[[options$label]] <<- res
    }
  }
}))
knitr::opts_chunk$set(
  tidy = TRUE,
  tidy.opts = list(width.cutoff = 95),
  message = FALSE,
  warning = FALSE,
  time_it = TRUE
)

Converting between scRNA-seq package formats

While many packages are have some object converters they are not always as feature complete as desired. scCustomize provides a few straightforward converter functions.

FYI: Object converters can be fragile and/or not very flexible. I have tried to make these functions to avoid those issues. However, if that proves too hard a task to maintain long-term I may move them to separate package/deprecate them.

# Load Packages
library(Seurat)
library(rliger)
library(scCustomize)
library(qs)

Load Seurat Object & Add QC Data

# read object
pbmc <- pbmc3k.SeuratData::pbmc3k.final
pbmc <- UpdateSeuratObject(pbmc)

pbmc <- Add_Cell_QC_Metrics(object = pbmc, species = "human")

We'll also add some random meta data variables to pbmc data form use in this vignette

pbmc$sample_id <- sample(c("sample1", "sample2", "sample3", "sample4", "sample5", "sample6"), size = ncol(pbmc), replace = TRUE)

Convert Seurat or LIGER objects to Anndata objects

scCustomize also allows for the conversion of Seurat or LIGER objects to python anndata objects for analysis in scanpy or other compatible python packages via the function as.anndata. These functions were inspired/modified/updated from sceasy R package (see as.anndata documentation).

Sys.setenv(RETICULATE_PYTHON = "/Users/marsh_mbp/.virtualenvs/r-reticulate/bin/python")
as.anndata(x = pbmc, file_path = "~/Desktop", file_name = "pbmc_anndata.h5ad")

Convert Seurat assay type within an object

The release of Seurat V5+ has brought about two different types of assay structure that can exist within a Seurat object. However, some community tools that interact with Seurat objects have not been updated to work with both assay formats. Therefore it becomes necessary to change assay format for use with certain tools.

scCustomize provides Convert_Assay() for easy method to convert from Assay>Assay5 (V3/4>5) or Assay5>Assay (V5>V3/4).

Convert V3/4 to V5

# Convert to V5/Assay5 structure
pbmc_V5 <- Convert_Assay(seurat_object = pbmc, convert_to = "V5")

pbmc_V5[["RNA"]]

Convert V5 > V3/4

# Convert to V3/4/Assay structure
pbmc_V3 <- Convert_Assay(seurat_object = pbmc_V5, convert_to = "V3")

pbmc_V3[["RNA"]]

Accepted values for convert_to

Convert_Assay will accept a range of accepted values for the convert_to parameter to specify desired format.

library(magrittr)
accepted_format_names <- data.frame(V3_Assay_Options = c("Assay", "assay", "V3", "v3"),
                                    V5_Assay5_Options = c("Assay5", "assay5", "V5", "v5"))

accepted_format_names %>%
  kableExtra::kbl() %>%
  kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped")) 


samuel-marsh/scCustomize documentation built on Dec. 20, 2024, 7:41 a.m.