-
Notifications
You must be signed in to change notification settings - Fork 977
feature: updates for cellranger 9.0 #9541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stephenwilliams22
wants to merge
6
commits into
satijalab:main
Choose a base branch
from
stephenwilliams22:stephen/cellranger-9.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
069d8f3
fix little formatting
stephenwilliams22 7e84ebd
use base r and small changes
stephenwilliams22 f9b0e9e
Update R/preprocessing.R
stephenwilliams22 8f3035e
Update R/preprocessing.R
stephenwilliams22 f7c219f
Merge branch 'main' into stephen/cellranger-9.0
stephenwilliams22 7dbd15c
use base R
stephenwilliams22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -642,6 +642,139 @@ Load10X_Spatial <- function ( | |
|
|
||
| return(object) | ||
| } | ||
| #' Add 10X Cell Types to a Seurat Object | ||
| #' | ||
| #' This function reads cell type annotations from a CSV file and adds them to the metadata of a Seurat object. | ||
| #' If the cell type file does not exist, the original Seurat object is returned unchanged. | ||
| #' | ||
| #' @param data.dir A string specifying the directory containing the "cell_types" folder with the "cell_types.csv" file. | ||
| #' @param object A Seurat object to which the cell type annotations will be added. | ||
| #' | ||
| #' @return A Seurat object with updated metadata including cell type annotations if the file is found. | ||
| #' | ||
| #' @details | ||
| #' The function searches for a CSV file named "cell_types.csv" in the "cell_types" subdirectory within `data.dir`. | ||
| #' The CSV file should contain at least a "barcode" column that matches the cell barcodes in the Seurat object. | ||
| #' Additional columns in the CSV file will be merged into the Seurat object's metadata. | ||
| #' | ||
| #' @importFrom utils read.csv | ||
| #' @importFrom tibble rownames_to_column column_to_rownames | ||
| #' | ||
| #' @examples | ||
| #' \dontrun{ | ||
| #' # Specify the data directory containing the "cell_types" folder | ||
| #' data.dir <- "/path/to/data" | ||
| #' | ||
| #' # Create a Seurat object (example) | ||
| #' seurat_obj <- CreateSeuratObject(counts = some_counts_matrix) | ||
| #' | ||
| #' # Add cell type annotations to the Seurat object | ||
| #' seurat_obj <- Add_10X_CellTypes(data.dir, seurat_obj) | ||
| #' } | ||
|
|
||
| Add_10X_CellTypes <- function(data.dir, object) { | ||
| cell_types_path <- file.path(data.dir, "cell_types", "cell_types.csv") | ||
| if (file.exists(cell_types_path)) { | ||
| cell.types <- read.csv(cell_types_path) | ||
| meta_data_with_barcodes <- tibble::rownames_to_column([email protected], "barcode") | ||
| merged_meta_data <- merge( | ||
| x = meta_data_with_barcodes, | ||
| y = cell.types, | ||
| by = "barcode", | ||
| all.x = TRUE | ||
| ) | ||
| [email protected] <- tibble::column_to_rownames(merged_meta_data, "barcode") | ||
| return(object) | ||
| } else { | ||
| return(object) | ||
| } | ||
| } | ||
|
|
||
| #' Load a 10x Genomics Single Cell Experiment into a \code{Seurat} object | ||
| #' | ||
| #' @inheritParams Read10X | ||
| #' @inheritParams SeuratObject::CreateSeuratObject | ||
| #' @note If multiome 10x data the assay param will not be used. The names of each assay contained in the matrix are used. | ||
| #' @param data.dir Directory containing the H5 file specified by \code{filename} | ||
| #' @param filename Name of H5 file containing the feature barcode matrix | ||
| #' @param to.upper Converts all feature names to upper case. This can provide an | ||
anashen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #' approximate conversion of mouse to human gene names which can be useful in an | ||
| #' explorative analysis. For cross-species comparisons, orthologous genes should | ||
| #' be identified across species and used instead. | ||
| #' @param ... Arguments passed to \code{\link{Read10X_h5}} | ||
| #' | ||
| #' @return A \code{Seurat} object | ||
| #' | ||
| #' | ||
| #' @export | ||
| #' @concept preprocessing | ||
| #' | ||
| #' @examples | ||
| #' \dontrun{ | ||
| #' data_dir <- 'path/to/data/directory' | ||
| #' list.files(data_dir) # Should show filtered_feature_bc_matrix.h5 | ||
| #' Load10X(data.dir = data_dir) | ||
stephenwilliams22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #' } | ||
| #' | ||
| Load10X <- function(data.dir, filename = "filtered_feature_bc_matrix.h5", | ||
| assay = "RNA", to.upper = FALSE, ...) { | ||
|
|
||
| if (length(data.dir) > 1) { | ||
| stop("`data.dir` expects a single directory path but received multiple values.") | ||
| } | ||
| if (!file.exists(data.dir)) { | ||
| stop("No such file or directory: '", data.dir, "'") | ||
| } | ||
|
|
||
|
|
||
| filename <- list.files(data.dir, filename, full.names = FALSE, recursive = FALSE) | ||
| counts.path <- file.path(data.dir, filename) | ||
| if (!file.exists(counts.path)) { | ||
| stop("File not found: '", counts.path, "'") | ||
| } | ||
|
|
||
| counts <- Read10X_h5(counts.path, ...) | ||
|
|
||
| if (is.list(counts)) { | ||
| counts <- lapply(counts, function(mat) { | ||
| rownames(mat) <- toupper(rownames(mat)) | ||
| mat | ||
| }) | ||
| } else { | ||
| rownames(counts) <- toupper(rownames(counts)) | ||
| } | ||
|
|
||
| if (is.list(counts)) { | ||
| seurat.list <- lapply(names(counts), function(name) { | ||
| CreateSeuratObject( | ||
| counts = counts[[name]], | ||
| assay = name, | ||
| project = name | ||
| ) | ||
| }) | ||
|
|
||
| for (i in seq_along(seurat.list)) { | ||
| if (Assays(seurat.list[[i]]) %in% c("Gene Expression", "RNA")) { | ||
| seurat.list[[i]] <- Add_10X_CellTypes(data.dir, seurat.list[[i]]) | ||
| } | ||
| } | ||
|
|
||
| merged.object <- merge( | ||
| x = seurat.list[[1]], | ||
| y = seurat.list[-1], | ||
| add.cell.ids = names(counts), | ||
| merge.data = FALSE | ||
| ) | ||
| return(merged.object) | ||
|
|
||
| } else { | ||
| object <- CreateSeuratObject(counts, assay = assay) | ||
| if (Assays(object) %in% c("Gene Expression", "RNA")) { | ||
| object <- Add_10X_CellTypes(data.dir, object) | ||
| } | ||
anashen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return(object) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| #' Read10x Probe Metadata | ||
|
|
@@ -1263,8 +1396,8 @@ Read10X_Image <- function( | |
| image = image | ||
| ) | ||
|
|
||
| # As of v5.1.0 `Radius.VisiumV1` no longer returns the value of the | ||
| # `spot.radius` slot and instead calculates the value on the fly, but we | ||
| # As of v5.1.0 `Radius.VisiumV1` no longer returns the value of the | ||
| # `spot.radius` slot and instead calculates the value on the fly, but we | ||
| # can populate the static slot in case it's depended on. | ||
| [email protected] <- Radius(visium.v1) | ||
|
|
||
|
|
@@ -3520,7 +3653,7 @@ SampleUMI <- function( | |
| #' replaces the \code{NormalizeData} → \code{FindVariableFeatures} → | ||
| #' \code{ScaleData} workflow by fitting a regularized negative binomial model | ||
| #' per gene and returning: | ||
| #' | ||
| #' | ||
| #' - A new assay (default name “SCT”), in which: | ||
| #' - \code{counts}: depth‐corrected UMI counts (as if each cell had uniform | ||
| #' sequencing depth; controlled by \code{do.correct.umi}). | ||
|
|
@@ -3531,13 +3664,13 @@ SampleUMI <- function( | |
| #' | ||
| #' When multiple \code{counts} layers exist (e.g. after \code{split()}), | ||
| #' each layer is modeled independently. A consensus variable‐feature set is | ||
| #' then defined by ranking features by how often they’re called “variable” | ||
| #' then defined by ranking features by how often they’re called “variable” | ||
| #' across different layers (ties broken by median rank). | ||
| #' | ||
| #' | ||
| #' By default, \code{sctransform::vst} will drop features expressed in fewer | ||
| #' than five cells. In the multi-layer case, this can lead to consenus | ||
| #' variable-features being excluded from the output's \code{scale.data} when | ||
| #' a feature is "variable" across many layers but sparsely expressed in at | ||
| #' a feature is "variable" across many layers but sparsely expressed in at | ||
| #' least one. | ||
| #' | ||
| #' @param object A Seurat object or UMI count matrix. | ||
|
|
@@ -3593,11 +3726,11 @@ SampleUMI <- function( | |
| #' @seealso \code{\link[sctransform]{vst}}, | ||
| #' \code{\link[sctransform]{get_residuals}}, | ||
| #' \code{\link[sctransform]{correct_counts}} | ||
| #' | ||
| #' | ||
| #' @rdname SCTransform | ||
| #' @concept preprocessing | ||
| #' @export | ||
| #' | ||
| #' | ||
| SCTransform.default <- function( | ||
| object, | ||
| cell.attr, | ||
|
|
@@ -4557,7 +4690,7 @@ FindSpatiallyVariableFeatures.Seurat <- function( | |
| verbose = verbose, | ||
| ... | ||
| ) | ||
|
|
||
| object <- LogSeuratCommand(object) | ||
|
|
||
| return(object) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.