diff --git a/_pkgdown.yml b/_pkgdown.yml index fbad0ae..3438efc 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -63,6 +63,8 @@ navbar: menu: - text: Hematopoiesis href: articles/marchr_hematopoiesis.html + - text: Multiome + href: articles/marchr_multiome.html repo: url: home: https://github.com/GreenleafLab/MarchR @@ -72,6 +74,7 @@ articles: - title: Tutorials contents: - marchr_hematopoiesis + - marchr_multiome reference: - title: All Functions - subtitle: Package Overview @@ -83,6 +86,7 @@ reference: - subtitle: Project & Dataset Management contents: - create_example_project + - get_tutorial_data - insert_dataset - select_dataset - load_project @@ -131,6 +135,7 @@ reference: - IterativeLSI - marchRproject-dimreduction-methods - add_combined_dims + - project_bulk_atac - subtitle: Integration contents: - run_harmony @@ -189,11 +194,22 @@ reference: - add_peak_matrix - add_peak_gc - get_peak_set +- subtitle: Pseudobulk Export + contents: + - pseudobulk_fragments + - write_pseudobulk_fragments + - write_pseudobulk_coverage + - write_pseudobulk_bigwig + - write_pseudobulk_peaks + - write_pseudobulk_chrom_sizes + - write_pseudobulk_manifest - subtitle: Peak & Motif Annotations contents: - add_peak_annotations - get_peak_annotation - add_motif_annotations + - get_motifs + - plot_motif_logo - peak_annotation_enrichment - custom_enrichment - get_matches diff --git a/development/vignettes/marchr_multiome.Rmd b/development/vignettes/marchr_multiome.Rmd deleted file mode 100644 index 959788a..0000000 --- a/development/vignettes/marchr_multiome.Rmd +++ /dev/null @@ -1,437 +0,0 @@ ---- -title: "MarchR Multiome (ATAC + RNA) tutorial" -author: "Eric Liu" -output: - html_document: - toc: true - toc_float: true - toc_depth: 2 -vignette: > - %\VignetteIndexEntry{MarchR Multiome (ATAC + RNA) tutorial} - %\VignetteEngine{knitr::rmarkdown} - %\VignetteEncoding{UTF-8} ---- - - - -```{r setup, include=FALSE} -knitr::opts_chunk$set(collapse = FALSE) - -# Collect each chunk's printed output, messages, and warnings into a single -# collapsible "Output" box rendered after the code, keeping code and logs -# visually separate. -local({ - buf <- new.env(parent = emptyenv()) - buf$txt <- character(0) - collect <- function(x, options) { - buf$txt <- c(buf$txt, x) - "" - } - esc <- function(x) { - x <- gsub("&", "&", x, fixed = TRUE) - x <- gsub("<", "<", x, fixed = TRUE) - gsub(">", ">", x, fixed = TRUE) - } - default_chunk <- knitr::knit_hooks$get("chunk") - knitr::knit_hooks$set( - output = collect, - message = collect, - warning = collect, - chunk = function(x, options) { - out <- default_chunk(x, options) - if (length(buf$txt)) { - body <- esc(paste(buf$txt, collapse = "")) - buf$txt <- character(0) - out <- paste0( - out, "\n\n```{=html}\n", - "
Output", - "
", body, "
\n", - "```\n\n" - ) - } - out - } - ) -}) -``` - -```{css, echo=FALSE} -details.marchr-log { - background-color: #eef4fb; - border: 1px solid #cfe0f3; - border-left: 4px solid #4f88c6; - border-radius: 4px; - margin: 0.5em 0 1em 0; - padding: 0.4em 0.8em; -} -details.marchr-log > summary { - cursor: pointer; - color: #2c5c8f; - font-weight: 600; - font-size: 0.85em; - list-style: none; -} -details.marchr-log > summary::-webkit-details-marker { display: none; } -details.marchr-log > summary::before { content: "\25B6 "; } -details.marchr-log[open] > summary::before { content: "\25BC "; } -details.marchr-log .marchr-log-body { - background: transparent; - border: none; - margin: 0.4em 0 0 0; - padding: 0; - font-size: 0.85em; - white-space: pre-wrap; -} -``` - -## Introduction - -This vignette reproduces the [ArchR "Analyze Multiome" tutorial](https://greenleaflab.github.io/ArchR_2020/Ex-Analyze-Multiome.html) -workflow using MarchR. The dataset is the 10x Genomics **PBMC unsorted 10k -Multiome** sample, which measures scATAC-seq and scRNA-seq (Gene Expression) in -the same nuclei, aligned to hg38. - -The multiome-specific steps are: - -- import the RNA modality from the 10x feature-barcode HDF5 into the same - `MarchRDataset` as the ATAC data (`import_feature_matrix_10x()`), -- reduce each modality independently with `IterativeLSI()`, -- fuse the two reductions into one joint embedding (`add_combined_dims()`), -- and embed / cluster on that joint space. - -**This tutorial assumes that you are running MarchR locally.** It downloads a -~2.9 GB ATAC fragments file and a ~150 MB RNA HDF5. - -## Setup - -```{r message=FALSE} -library(MarchR) -library(ggplot2) -library(patchwork) -``` - -`set_threads()` registers a session-global thread count. - -```{r} -set_threads(1) -``` - -```{r} -sample_name <- "PBMC_10k" - -cache_dir <- file.path(path.expand("~"), ".cache", "marchr_multiome") -data_dir <- file.path(cache_dir, "data") -ref_dir <- file.path(cache_dir, "references") -dir.create(data_dir, showWarnings = FALSE, recursive = TRUE) -dir.create(ref_dir, showWarnings = FALSE, recursive = TRUE) - -out_dir <- file.path(tempdir(), "marchr-multiome") -dir.create(out_dir, showWarnings = FALSE, recursive = TRUE) -project_dir <- file.path(out_dir, "marchr_project") -dir.create(project_dir, showWarnings = FALSE) -``` - -## Download the multiome data - -Both modalities come from the same 10x Multiome sample, and can be downloaded as follows. - -```{r} -atac_url <- paste0( - "https://cf.10xgenomics.com/samples/cell-arc/2.0.0/pbmc_unsorted_10k/", - "pbmc_unsorted_10k_atac_fragments.tsv.gz" -) -rna_url <- paste0( - "https://cf.10xgenomics.com/samples/cell-arc/2.0.0/pbmc_unsorted_10k/", - "pbmc_unsorted_10k_filtered_feature_bc_matrix.h5" -) -atac_gz <- file.path(data_dir, "pbmc_10k_atac_fragments.tsv.gz") -rna_h5 <- file.path(data_dir, "pbmc_10k_filtered_feature_bc_matrix.h5") -``` - -## Build the ATAC project - -Convert the fragments into a BPCells disk-backed directory, then initialize a -single-dataset `MarchRProject`. - -```{r} -frag_dir <- file.path(data_dir, paste0(sample_name, "_frags")) -if (!file.exists(frag_dir)) { - open_fragments_10x(atac_gz) %>% write_fragments_dir(frag_dir) -} -frags <- open_fragments_dir(frag_dir) - -proj <- MarchRProject(dir = project_dir) -proj <- insert_dataset( - proj, - dataset_name = sample_name -) -proj <- add_fragments( - proj, - frags = frags, - frags_name = "ATAC", - dataset_name = sample_name -) -proj -``` - -## Genome annotations - -The sample is aligned to hg38. Gene annotations come from GENCODE; the blacklist -and chromosome sizes are downloaded for hg38. - -```{r} -proj <- set_gene_annotations( - proj, - annotation_type = "transcripts", - name = "transcripts", - dir_name = ref_dir, - release = "44", - transcript_choice = "all", - annotation_set = "comprehensive" -) -proj <- set_gene_annotations( - proj, - annotation_type = "genes", - name = "genes", - dir_name = ref_dir, - release = "44", - annotation_set = "comprehensive" -) -proj <- set_blacklist( - proj, - genome = "hg38", - dir_name = ref_dir -) -proj <- set_chrom_sizes( - proj, - genome = "hg38", - dir_name = ref_dir -) -``` - -## Quality control and tile matrix - -Filter cells on fragment count and TSS enrichment, then build a genome-wide -500bp tile matrix for the ATAC modality. - -```{r} -proj <- qc_fragments( - proj, - dataset_name = sample_name, - frags_name = "ATAC", - n_frags = 1000, - tss_enrichment = 4 -) - -proj <- add_tile_matrix( - proj, - dataset_names = sample_name, - frags_name = "ATAC", - output_assay_name = "TileMatrix", - blacklist = TRUE, - tile_size = 500, - mode = "insertions", - overwrite = TRUE -) - -proj <- align_cells( - proj, - how = "intersect" -) -``` - -## Doublet scoring and filtering - -`add_doublet_scores()` simulates synthetic doublets and scores each real cell, -`filter_doublets()` removes the top-scoring cells. - -```{r} -proj <- add_doublet_scores( - proj, - assay_name = "TileMatrix", - scoring_method = "UMAP", - seed = 1, - verbose = TRUE -) -proj <- filter_doublets( - proj, - filter_ratio = 1, - verbose = TRUE -) -``` - -## Import RNA - -`import_feature_matrix_10x()` reads the Gene Expression features from the 10x -HDF5 and adds them as a `GeneExpressionMatrix` assay **into the existing ATAC -dataset**, intersecting barcodes so both modalities describe the same cells. -Gene identifiers use Ensembl IDs as row names, with gene symbols stored in the -assay's feature metadata under `gene_symbol`. - -```{r} -proj <- import_feature_matrix_10x( - proj, - input = rna_h5, - dataset_name = sample_name, - matrix_name = "GeneExpressionMatrix", - overwrite = TRUE -) - -# Align fragments + both matrices onto one shared cell set. -proj <- align_cells( - proj, - how = "intersect" -) -proj -``` - -The gene symbols are available alongside the Ensembl-ID row names: - -```{r} -head(get_feature_metadata(proj, assay_names = "GeneExpressionMatrix")) -``` - -## Per-modality dimensionality reduction - -Reduce each modality with IterativeLSI. ATAC uses the binarized defaults; RNA -uses expression-appropriate settings (`binarize = FALSE`, 2500 variable genes). - -```{r} -proj <- IterativeLSI( - proj, - assay_name = "TileMatrix", - output_assay_name = "LSI_ATAC", - dim_reduction_name = "LSI_ATAC", - verbose = TRUE -) - -proj <- IterativeLSI( - proj, - assay_name = "GeneExpressionMatrix", - output_assay_name = "LSI_RNA", - dim_reduction_name = "LSI_RNA", - binarize = FALSE, - first_selection = "var", - feature_selection_method = function(mat, ...) { - select_features_variance(mat, num_feats = 2500L, ...) - }, - verbose = TRUE -) -``` - -## Combine the modalities - -`add_combined_dims()` variance-normalizes each modality's reduction and -concatenates them into a single joint embedding. The combined embedding has as -many dimensions as the two inputs together. - -```{r} -proj <- add_combined_dims( - proj, - embedding_names = c("LSI_ATAC", "LSI_RNA"), - output_name = "LSI_Combined" -) - -vapply(c("LSI_ATAC", "LSI_RNA", "LSI_Combined"), - function(e) nrow(proj[[e, sample_name]]), integer(1)) -``` - -## UMAP and clustering - -Compute a UMAP for each embedding and cluster on the joint space. - -```{r} -proj <- add_umap( - proj, - embedding_name = "LSI_ATAC", - output_assay_name = "UMAP_ATAC", - min_dist = 0.8, - seed = 1L -) -proj <- add_umap( - proj, - embedding_name = "LSI_RNA", - output_assay_name = "UMAP_RNA", - min_dist = 0.8, - seed = 1L -) -proj <- add_umap( - proj, - embedding_name = "LSI_Combined", - output_assay_name = "UMAP_Combined", - min_dist = 0.8, - seed = 1L -) - -proj <- add_clusters( - proj, - embedding_name = "LSI_Combined", - col_name = "Clusters_Combined", - cluster_graph_method = cluster_graph_seurat(resolution = 0.4), - seed = 1L -) -table(get_cell_metadata(proj)$Clusters_Combined) -``` - -The three embeddings, colored by the joint clusters. Each modality's UMAP is -fetched by its column prefix; `plot_embedding()` applies the shared MarchR -palette and styling. - -```{r fig.width=15, fig.height=6} -plot_one <- function(emb, title) { - plot_embedding( - proj, - embedding = emb, - col_name = "Clusters_Combined", - title = title, - label_means = FALSE, - verbose = FALSE - ) -} -plot_one("UMAP_ATAC", "ATAC LSI") + - plot_one("UMAP_RNA", "RNA LSI") + - plot_one("UMAP_Combined", "Combined LSI") + - plot_layout(guides = "collect") -``` - -## A note on cluster resolution vs. ArchR - -The ArchR tutorial reports 20 clusters at `resolution = 0.4`, whereas matching parameters -for MarchR yield fewer. Louvain `resolution` is not portable across tools so the -number maps to different granularity because of the underlying nearest-neighbor -graph (LSI normalization, SVD, and feature selection are not identical). -For this particular example, MarchR reaches ArchR's 20 clusters near `resolution = 1.0`: - -```{r fig.width=15, fig.height=6} -proj <- add_clusters( - proj, - embedding_name = "LSI_Combined", - col_name = "Clusters_res1", - cluster_graph_method = cluster_graph_seurat(resolution = 1.0), - seed = 1L, - overwrite = TRUE -) -length(unique(get_cell_metadata(proj)$Clusters_res1)) - -plot_one2 <- function(emb, title) { - plot_embedding( - proj, - embedding = emb, - col_name = "Clusters_res1", - title = title, - label_means = FALSE, - verbose = FALSE - ) -} -plot_one2("UMAP_ATAC", "ATAC LSI") + - plot_one2("UMAP_RNA", "RNA LSI") + - plot_one2("UMAP_Combined", "Combined LSI") + - plot_layout(guides = "collect") -``` - -## Session info - -```{r} -sessionInfo() -``` diff --git a/vignettes/marchr_multiome.Rmd b/vignettes/marchr_multiome.Rmd new file mode 100644 index 0000000..ee23895 --- /dev/null +++ b/vignettes/marchr_multiome.Rmd @@ -0,0 +1,286 @@ +--- +title: "MarchR Multiome (ATAC + RNA) Tutorial (WIP)" +author: "Eric Liu" +output: + html_document: + toc: true + toc_float: true + toc_depth: 2 +vignette: > + %\VignetteIndexEntry{MarchR Multiome (ATAC + RNA) Tutorial (WIP)} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(collapse = FALSE) + +# Collect each chunk's printed output, messages, and warnings into a single +# collapsible "Output" box rendered after the code, keeping code and logs +# visually separate. +local({ + buf <- new.env(parent = emptyenv()) + buf$txt <- character(0) + collect <- function(x, options) { + buf$txt <- c(buf$txt, x) + "" + } + esc <- function(x) { + x <- gsub("&", "&", x, fixed = TRUE) + x <- gsub("<", "<", x, fixed = TRUE) + gsub(">", ">", x, fixed = TRUE) + } + default_chunk <- knitr::knit_hooks$get("chunk") + knitr::knit_hooks$set( + output = collect, + message = collect, + warning = collect, + chunk = function(x, options) { + out <- default_chunk(x, options) + if (length(buf$txt)) { + body <- esc(paste(buf$txt, collapse = "")) + buf$txt <- character(0) + out <- paste0( + out, "\n\n```{=html}\n", + "
Output", + "
", body, "
\n", + "```\n\n" + ) + } + out + } + ) +}) +``` + +```{css, echo=FALSE} +details.marchr-log { + background-color: #eef4fb; + border: 1px solid #cfe0f3; + border-left: 4px solid #4f88c6; + border-radius: 4px; + margin: 0.5em 0 1em 0; + padding: 0.4em 0.8em; +} +details.marchr-log > summary { + cursor: pointer; + color: #2c5c8f; + font-weight: 600; + font-size: 0.85em; + list-style: none; +} +details.marchr-log > summary::-webkit-details-marker { display: none; } +details.marchr-log > summary::before { content: "\25B6 "; } +details.marchr-log[open] > summary::before { content: "\25BC "; } +details.marchr-log .marchr-log-body { + background: transparent; + border: none; + margin: 0.4em 0 0 0; + padding: 0; + font-size: 0.85em; + white-space: pre-wrap; +} +``` + +## Introduction + +This is a brief MarchR multiome tutorial, based upon the +[ArchR "Analyze Multiome" tutorial](https://greenleaflab.github.io/ArchR_2020/Ex-Analyze-Multiome.html). +It uses the 10x Genomics PBMC unsorted 10k Multiome sample (hg38), which measures scATAC-seq and +scRNA-seq in the same nuclei. +**Importantly, this tutorial assumes that you are running MarchR locally.** It downloads a ~2.9 GB +fragments file and a ~150 MB RNA HDF5. + +## Setup + +First, load MarchR. If this fails, we recommend [revisiting the MarchR installation instructions](https://github.com/GreenleafLab/MarchR/blob/main/README.md#installation). + +```{r message=FALSE} +library(MarchR) +``` + +`set_threads()` sets a session-wide thread count for operations that benefit from parallelization +(Certain functions will remain single-threaded). + +```{r} +set_threads(4) +``` + +Downloads and references are cached in `cache_dir` so they are reused +across runs; the project itself is written to a temporary directory. + +```{r} +sample_name <- "PBMC_10k" +cache_dir <- path.expand("~/.cache/marchr_multiome") +data_dir <- file.path(cache_dir, "data") +ref_dir <- file.path(cache_dir, "references") +project_dir <- file.path(tempdir(), "marchr_project") +``` + +## Download Data + +Both modalities come from the same 10x Multiome sample: the ATAC fragments and the RNA +feature-barcode HDF5. Files already in `data_dir` are not downloaded again. + +```{r} +url_base <- "https://cf.10xgenomics.com/samples/cell-arc/2.0.0/pbmc_unsorted_10k/pbmc_unsorted_10k_" +atac_gz <- file.path(data_dir, "pbmc_10k_atac_fragments.tsv.gz") +rna_h5 <- file.path(data_dir, "pbmc_10k_filtered_feature_bc_matrix.h5") + +dir.create(data_dir, recursive = TRUE, showWarnings = FALSE) +options(timeout = 3600) +if (!file.exists(atac_gz)) { + download.file(paste0(url_base, "atac_fragments.tsv.gz"), atac_gz, mode = "wb") +} +if (!file.exists(rna_h5)) { + download.file(paste0(url_base, "filtered_feature_bc_matrix.h5"), rna_h5, mode = "wb") +} +``` + +## Initialize a MarchRProject + +The ATAC fragments become a single `MarchRDataset`; the RNA modality is added to the same +dataset later. + +```{r} +proj <- MarchRProject(dir = project_dir) +frags <- open_fragments_10x(atac_gz) %>% + write_fragments_dir(file.path(project_dir, "fragments", sample_name)) +proj <- add_fragments(proj, frags, frags_name = "ATAC", dataset_name = sample_name) +proj +``` + +## Genome Annotations + +The sample is aligned to hg38. Transcripts are used for TSS enrichment QC. + +```{r} +proj <- set_gene_annotations( + proj, + annotation_type = "transcripts", + name = "transcripts", + dir_name = ref_dir, + release = "44", + transcript_choice = "all", + annotation_set = "comprehensive" +) +proj <- set_blacklist(proj, genome = "hg38", dir_name = ref_dir) +proj <- set_chrom_sizes(proj, genome = "hg38", dir_name = ref_dir) +``` + +## Quality Control + +Filter cells on fragment count and TSS enrichment with `qc_fragments()`. + +```{r} +proj <- qc_fragments(proj, frags_name = "ATAC", n_frags = 1000, tss_enrichment = 4) +``` + +## Tile Matrix + +Build a genome-wide 500bp tile matrix, then `align_cells()` reconciles the fragments and matrix +onto a single, consistent set of cells. + +```{r} +proj <- add_tile_matrix(proj, frags_name = "ATAC") +proj <- align_cells(proj) +``` + +## Doublet Scoring and Filtering + +`add_doublet_scores()` simulates synthetic doublets and scores each real cell by its local +doublet enrichment; `filter_doublets()` removes the highest-enrichment predicted doublets. + +```{r} +proj <- add_doublet_scores(proj, assay_name = "TileMatrix", verbose = TRUE) +proj <- filter_doublets(proj, verbose = TRUE) +``` + +## Import RNA + +`import_feature_matrix_10x()` adds the Gene Expression features as a `GeneExpressionMatrix` +assay **in the existing ATAC dataset**, keeping only barcodes shared by both modalities. +Rows are Ensembl IDs, with gene symbols stored in the feature metadata. + +```{r} +proj <- import_feature_matrix_10x(proj, input = rna_h5) +proj <- align_cells(proj) +head(get_feature_metadata(proj, assay_names = "GeneExpressionMatrix")) +``` + +## Iterative LSI + +Each modality is reduced separately with `IterativeLSI()`. ATAC uses the defaults; RNA uses +non-binarized counts and selects 2,500 variable genes from the first iteration. + +```{r} +proj <- IterativeLSI( + proj, + assay_name = "TileMatrix", + output_assay_name = "LSI_ATAC", + dim_reduction_name = "LSI_ATAC", + verbose = TRUE +) +proj <- IterativeLSI( + proj, + assay_name = "GeneExpressionMatrix", + output_assay_name = "LSI_RNA", + dim_reduction_name = "LSI_RNA", + binarize = FALSE, + first_selection = "var", + feature_selection_method = select_features_variance(num_feats = 2500L), + verbose = TRUE +) +``` + +## Combining Modalities + +`add_combined_dims()` variance-normalizes each modality's reduction and concatenates them into +a single joint embedding. + +```{r} +proj <- add_combined_dims( + proj, + embedding_names = c("LSI_ATAC", "LSI_RNA"), + output_name = "LSI_Combined" +) +``` + +## Clustering + +Call clusters on the joint embedding with `add_clusters()`; raise `resolution` for finer clusters. + +```{r} +proj <- add_clusters( + proj, + embedding_name = "LSI_Combined", + cluster_graph_method = cluster_graph_leiden(resolution = 0.4), + verbose = TRUE +) +table(get_cell_metadata(proj)$clusters) +``` + +## UMAP Embedding + +Compute a UMAP for each modality and for the joint embedding. + +```{r} +for (emb in c("ATAC", "RNA", "Combined")) { + proj <- add_umap( + proj, + embedding_name = paste0("LSI_", emb), + output_assay_name = paste0("UMAP_", emb), + min_dist = 0.8, + verbose = TRUE + ) +} +``` + +All three embeddings, colored by the joint clusters: + +```{r fig.width=15, fig.height=6} +plot_embedding(proj, embedding = "UMAP_ATAC", title = "ATAC") + + plot_embedding(proj, embedding = "UMAP_RNA", title = "RNA") + + plot_embedding(proj, embedding = "UMAP_Combined", title = "Combined") + + patchwork::plot_layout(guides = "collect") +``` diff --git a/vignettes/marchr_multiome.html b/vignettes/marchr_multiome.html new file mode 100644 index 0000000..fc1a115 --- /dev/null +++ b/vignettes/marchr_multiome.html @@ -0,0 +1,1960 @@ + + + + + + + + + + + + + + +MarchR Multiome (ATAC + RNA) Tutorial (WIP) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+
+
+
+
+ +
+ + + + + + + + +
+

Introduction

+

This is a brief MarchR multiome tutorial, based upon the ArchR “Analyze Multiome” tutorial. It uses the 10x Genomics PBMC unsorted 10k Multiome sample (hg38), which measures scATAC-seq and scRNA-seq in the same nuclei. Importantly, this tutorial assumes that you are running MarchR locally. It downloads a ~2.9 GB fragments file and a ~150 MB RNA HDF5.

+
+
+

Setup

+

First, load MarchR. If this fails, we recommend revisiting the MarchR installation instructions.

+
library(MarchR)
+

set_threads() sets a session-wide thread count for operations that benefit from parallelization (Certain functions will remain single-threaded).

+
set_threads(4)
+
Output
## Set default MarchR threads to 4.
+
+

Downloads and references are cached in cache_dir so they are reused across runs; the project itself is written to a temporary directory.

+
sample_name <- "PBMC_10k"
+cache_dir <- path.expand("~/.cache/marchr_multiome")
+data_dir <- file.path(cache_dir, "data")
+ref_dir <- file.path(cache_dir, "references")
+project_dir <- file.path(tempdir(), "marchr_project")
+
+
+

Download Data

+

Both modalities come from the same 10x Multiome sample: the ATAC fragments and the RNA feature-barcode HDF5. Files already in data_dir are not downloaded again.

+
url_base <- "https://cf.10xgenomics.com/samples/cell-arc/2.0.0/pbmc_unsorted_10k/pbmc_unsorted_10k_"
+atac_gz <- file.path(data_dir, "pbmc_10k_atac_fragments.tsv.gz")
+rna_h5 <- file.path(data_dir, "pbmc_10k_filtered_feature_bc_matrix.h5")
+
+dir.create(data_dir, recursive = TRUE, showWarnings = FALSE)
+options(timeout = 3600)
+if (!file.exists(atac_gz)) {
+  download.file(paste0(url_base, "atac_fragments.tsv.gz"), atac_gz, mode = "wb")
+}
+if (!file.exists(rna_h5)) {
+  download.file(paste0(url_base, "filtered_feature_bc_matrix.h5"), rna_h5, mode = "wb")
+}
+
+
+

Initialize a MarchRProject

+

The ATAC fragments become a single MarchRDataset; the RNA modality is added to the same dataset later.

+
proj <- MarchRProject(dir = project_dir)
+frags <- open_fragments_10x(atac_gz) %>%
+  write_fragments_dir(file.path(project_dir, "fragments", sample_name))
+proj <- add_fragments(proj, frags, frags_name = "ATAC", dataset_name = sample_name)
+proj
+
Output
## MarchRProject Object
+## Directory: /tmp/RtmpwPUqnD/marchr_project
+## 1 Dataset:
+##   PBMC_10k:
+##     671397 cells: PBMC_10k#GTAGGTGCATAACGGG-1, PBMC_10k#CCAACCCGTGGAAGGC-1, PBMC_10k#ACGGGAAGTTTACGTC-1, PBMC_10k#TAGGTTGGTTTGTGGA-1 ... PBMC_10k#GTTTCCTCACCTACGG-1
+##     Fragments: ATAC
+## All datasets aligned.
+## Empty genome object
+
+
+
+

Genome Annotations

+

The sample is aligned to hg38. Transcripts are used for TSS enrichment QC.

+
proj <- set_gene_annotations(
+  proj,
+  annotation_type = "transcripts",
+  name = "transcripts",
+  dir_name = ref_dir,
+  release = "44",
+  transcript_choice = "all",
+  annotation_set = "comprehensive"
+)
+proj <- set_blacklist(proj, genome = "hg38", dir_name = ref_dir)
+proj <- set_chrom_sizes(proj, genome = "hg38", dir_name = ref_dir)
+
+
+

Quality Control

+

Filter cells on fragment count and TSS enrichment with qc_fragments().

+
proj <- qc_fragments(proj, frags_name = "ATAC", n_frags = 1000, tss_enrichment = 4)
+
+
+

Tile Matrix

+

Build a genome-wide 500bp tile matrix, then align_cells() reconciles the fragments and matrix onto a single, consistent set of cells.

+
proj <- add_tile_matrix(proj, frags_name = "ATAC")
+proj <- align_cells(proj)
+
+
+

Doublet Scoring and Filtering

+

add_doublet_scores() simulates synthetic doublets and scores each real cell by its local doublet enrichment; filter_doublets() removes the highest-enrichment predicted doublets.

+
proj <- add_doublet_scores(proj, assay_name = "TileMatrix", verbose = TRUE)
+
proj <- filter_doublets(proj, verbose = TRUE)
+
Output
## 2026-10-16 14:21:25 Processing dataset 1 of 1: PBMC_10k
+## 2026-10-16 14:21:39 Simulating 65000 synthetic doublets (1000 doublets x 65 trials)
+## 2026-10-16 14:21:39 Running Iterative LSI on input matrix
+## 2026-10-16 14:21:42 Starting Iterative LSI
+## 2026-10-16 14:21:42 Starting Iterative LSI iteration 1 of 2
+## 2026-10-16 14:21:42 Selecting features
+## 2026-10-16 14:21:46 Running LSI
+## 2026-10-16 14:21:46 Starting LSI
+## 2026-10-16 14:21:48 Normalizing matrix
+## Warning: Matrix compression performs poorly with non-integers.
+## • Consider calling convert_matrix_type if a compressed integer matrix is intended.
+## 2026-10-16 14:21:50 Calculating SVD
+## 
+## 2026-10-16 14:21:57 Clustering LSI results
+## 
+## This message is displayed once every 8 hours.
+## Warning in .fn(threads = 1L, verbose = FALSE, k = 20L, metric = "euclidean", :
+## KNN search didn't find self-neighbor for 371 datapoints. Try higher ef value
+## 2026-10-16 14:21:59 Merging 29 clusters down to 6 via hierarchical clustering
+## 2026-10-16 14:21:59 Pseudobulking matrix
+## 2026-10-16 14:22:00 Starting Iterative LSI iteration 2 of 2
+## 2026-10-16 14:22:00 Selecting features
+## 2026-10-16 14:22:01 Running LSI
+## 2026-10-16 14:22:01 Starting LSI
+## 2026-10-16 14:22:02 Normalizing matrix
+## 2026-10-16 14:22:05 Calculating SVD
+## 2026-10-16 14:22:10 Finished running Iterative LSI
+## 2026-10-16 14:22:10 Projecting simulated doublets on LSI space
+## 2026-10-16 14:23:03 Running UMAP fitting on LSI matrix
+## 14:23:03 Using HNSW for nearest neighbor search
+## 14:23:03 UMAP embedding parameters a = 0.7669 b = 1.223
+## 14:23:03 Setting random seed 1
+## 14:23:03 Read 13164 rows and found 30 numeric columns
+## 14:23:03 Building HNSW index with metric 'l2' ef = 200 M = 16 using 1 threads
+## 14:23:04 Finished building index
+## 14:23:04 Searching HNSW index with ef = 40 and 4 threads
+## 14:23:05 Finished searching
+## 14:23:06 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 40
+## 14:23:07 Initializing from normalized Laplacian + noise (using RSpectra)
+## 14:23:07 Range-scaling initial input columns to 0-10
+## 14:23:07 Commencing optimization for 500 epochs, with 946616 positive edges using 4 threads
+## 14:23:07 Using rng type: pcg
+## 14:23:10 Optimization finished
+## 2026-10-16 14:23:10 Validating re-projection Pearson correlation
+## 14:23:10 Setting model random seed 1
+## 14:23:10 Read 13164 rows and found 30 numeric columns
+## 14:23:10 Processing block 1 of 1
+## 14:23:10 Searching HNSW index with ef = 40 and 4 threads
+## 14:23:10 Finished searching
+## 14:23:10 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 40
+## 14:23:10 Initializing by weighted average of neighbor coordinates using 4 threads
+## 14:23:10 Commencing optimization for 167 epochs, with 526560 positive edges using 4 threads
+## 14:23:11 Finished
+## 2026-10-16 14:23:11 UMAP Re-projection Correlation: R^2 = 0.902
+## 2026-10-16 14:23:11 Projecting simulated doublets on UMAP space
+## 14:23:11 Setting model random seed 1
+## 14:23:11 Read 65000 rows and found 30 numeric columns
+## 14:23:11 Processing block 1 of 1
+## 14:23:11 Searching HNSW index with ef = 40 and 4 threads
+## 14:23:11 Finished searching
+## 14:23:11 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 40
+## 14:23:11 Initializing by weighted average of neighbor coordinates using 4 threads
+## 14:23:12 Commencing optimization for 167 epochs, with 2600000 positive edges using 4 threads
+## 14:23:13 Finished
+## 2026-10-16 14:23:13 Compute doublet scores and enrichments from UMAP
+## 14:23:13 Building HNSW index with metric 'euclidean' ef = 200 M = 16 using 1 threads
+## 14:23:14 Finished building index
+## 14:23:14 Searching HNSW index with ef = 200 and 1 threads
+## 14:23:15 Finished searching
+## Filtering doublets in 1 dataset(s): PBMC_10k
+## 2026-10-16 14:23:16 filtering dataset: PBMC_10k.
+## 2026-10-16 14:23:16 Filtering 1732 doublets from 13164 cells (13.2%)
+## 2026-10-16 14:23:16 Dataset successfully filtered.
+## 2026-10-16 14:23:16 Removing stale doublet data
+## 2026-10-16 14:23:16 doublet filtering complete.
+
+
+
+

Import RNA

+

import_feature_matrix_10x() adds the Gene Expression features as a GeneExpressionMatrix assay in the existing ATAC dataset, keeping only barcodes shared by both modalities. Rows are Ensembl IDs, with gene symbols stored in the feature metadata.

+
proj <- import_feature_matrix_10x(proj, input = rna_h5)
+
proj <- align_cells(proj)
+head(get_feature_metadata(proj, assay_names = "GeneExpressionMatrix"))
+
Output
## 2026-10-16 14:23:16 Barcode intersection: 10010 shared, 1422 ATAC-only dropped, 2002 RNA-only dropped.
+##                 gene_symbol           assay_name
+## ENSG00000243485 MIR1302-2HG GeneExpressionMatrix
+## ENSG00000237613     FAM138A GeneExpressionMatrix
+## ENSG00000186092       OR4F5 GeneExpressionMatrix
+## ENSG00000238009  AL627309.1 GeneExpressionMatrix
+## ENSG00000239945  AL627309.3 GeneExpressionMatrix
+## ENSG00000239906  AL627309.2 GeneExpressionMatrix
+
+
+
+

Iterative LSI

+

Each modality is reduced separately with IterativeLSI(). ATAC uses the defaults; RNA uses non-binarized counts and selects 2,500 variable genes from the first iteration.

+
proj <- IterativeLSI(
+  proj,
+  assay_name = "TileMatrix",
+  output_assay_name = "LSI_ATAC",
+  dim_reduction_name = "LSI_ATAC",
+  verbose = TRUE
+)
+
proj <- IterativeLSI(
+  proj,
+  assay_name = "GeneExpressionMatrix",
+  output_assay_name = "LSI_RNA",
+  dim_reduction_name = "LSI_RNA",
+  binarize = FALSE,
+  first_selection = "var",
+  feature_selection_method = select_features_variance(num_feats = 2500L),
+  verbose = TRUE
+)
+
Output
## 2026-10-16 14:23:25 Starting Iterative LSI
+## 2026-10-16 14:23:25 Starting Iterative LSI iteration 1 of 2
+## 2026-10-16 14:23:25 Selecting features
+## 2026-10-16 14:23:27 Running LSI
+## 2026-10-16 14:23:27 Starting LSI
+## 2026-10-16 14:23:31 Filtering 401 outlier cells (colSum quantiles 0.02-0.98) before SVD
+## 2026-10-16 14:23:35 Normalizing matrix
+## 2026-10-16 14:23:43 Calculating SVD
+## 2026-10-16 14:23:55 Dropping PCs 1 due to high correlation with sequencing depth
+## 2026-10-16 14:23:55 Re-projecting outlier cells into LSI space
+## 2026-10-16 14:24:05 Clustering LSI results
+## Warning in .fn(threads = 1L, verbose = FALSE, k = 20L, metric = "euclidean", :
+## KNN search didn't find self-neighbor for 1 datapoints. Try higher ef value
+## 2026-10-16 14:24:07 Merging 22 clusters down to 6 via hierarchical clustering
+## 2026-10-16 14:24:07 Pseudobulking matrix
+## 2026-10-16 14:24:13 Starting Iterative LSI iteration 2 of 2
+## 2026-10-16 14:24:13 Selecting features
+## 2026-10-16 14:24:13 Running LSI
+## 2026-10-16 14:24:14 Starting LSI
+## 2026-10-16 14:24:16 Filtering 402 outlier cells (colSum quantiles 0.02-0.98) before SVD
+## 2026-10-16 14:24:20 Normalizing matrix
+## 2026-10-16 14:24:28 Calculating SVD
+## 2026-10-16 14:24:35 Dropping PCs 1 due to high correlation with sequencing depth
+## 2026-10-16 14:24:35 Re-projecting outlier cells into LSI space
+## 2026-10-16 14:24:44 Finished running Iterative LSI
+## 2026-10-16 14:25:07 Starting Iterative LSI
+## 2026-10-16 14:25:07 Starting Iterative LSI iteration 1 of 2
+## 2026-10-16 14:25:07 Selecting features
+## 2026-10-16 14:25:30 Running LSI
+## 2026-10-16 14:25:30 Starting LSI
+## 2026-10-16 14:25:33 Filtering 402 outlier cells (colSum quantiles 0.02-0.98) before SVD
+## 2026-10-16 14:25:36 Normalizing matrix
+## 2026-10-16 14:25:42 Calculating SVD
+## 2026-10-16 14:25:44 Re-projecting outlier cells into LSI space
+## 2026-10-16 14:25:46 Clustering LSI results
+## 2026-10-16 14:25:48 Merging 27 clusters down to 6 via hierarchical clustering
+## 2026-10-16 14:25:48 Pseudobulking matrix
+## 2026-10-16 14:25:50 Starting Iterative LSI iteration 2 of 2
+## 2026-10-16 14:25:50 Selecting features
+## 2026-10-16 14:25:50 Running LSI
+## 2026-10-16 14:25:50 Starting LSI
+## 2026-10-16 14:26:13 Filtering 402 outlier cells (colSum quantiles 0.02-0.98) before SVD
+## 2026-10-16 14:26:35 Normalizing matrix
+## 2026-10-16 14:27:19 Calculating SVD
+## 2026-10-16 14:27:20 Re-projecting outlier cells into LSI space
+## 2026-10-16 14:27:22 Finished running Iterative LSI
+
+
+
+

Combining Modalities

+

add_combined_dims() variance-normalizes each modality’s reduction and concatenates them into a single joint embedding.

+
proj <- add_combined_dims(
+  proj,
+  embedding_names = c("LSI_ATAC", "LSI_RNA"),
+  output_name = "LSI_Combined"
+)
+
Output
## Warning: Converting to a dense matrix may use excessive memory
+## This message is displayed once every 8 hours.
+
+
+
+

Clustering

+

Call clusters on the joint embedding with add_clusters(); raise resolution for finer clusters.

+
proj <- add_clusters(
+  proj,
+  embedding_name = "LSI_Combined",
+  cluster_graph_method = cluster_graph_leiden(resolution = 0.4),
+  verbose = TRUE
+)
+
table(get_cell_metadata(proj)$clusters)
+
Output
## 2026-10-16 14:27:23 Extracting embedding 'LSI_Combined'
+## 2026-10-16 14:27:23 Clustering 10010 cells with 59 dimensions
+## Warning in .fn(threads = 1L, verbose = FALSE, k = 20L, metric = "euclidean", :
+## KNN search didn't find self-neighbor for 2 datapoints. Try higher ef value
+## Found 16 clusters
+## 2026-10-16 14:27:24 Added 16 clusters as 'clusters' in cell metadata
+## 
+##   C1  C10  C11  C12  C13  C14  C15  C16   C2   C3   C4   C5   C6   C7   C8   C9 
+##  228 2852  477   98   34  338  364  401 1092 1143 1128  343  657  124  567  164
+
+
+
+

UMAP Embedding

+

Compute a UMAP for each modality and for the joint embedding.

+
for (emb in c("ATAC", "RNA", "Combined")) {
+  proj <- add_umap(
+    proj,
+    embedding_name = paste0("LSI_", emb),
+    output_assay_name = paste0("UMAP_", emb),
+    min_dist = 0.8,
+    verbose = TRUE
+  )
+}
+
Output
## 2026-10-16 14:27:24 Extracting embedding 'LSI_ATAC'
+## 2026-10-16 14:27:24 Computing UMAP on 10010 cells with 29 dimensions
+## 14:27:24 Using HNSW for nearest neighbor search
+## 14:27:24 UMAP embedding parameters a = 0.2321 b = 1.681
+## 14:27:24 Setting random seed 1
+## 14:27:24 Read 10010 rows and found 29 numeric columns
+## 14:27:24 Building HNSW index with metric 'cosine' ef = 200 M = 16 using 1 threads
+## 14:27:25 Finished building index
+## 14:27:25 Searching HNSW index with ef = 40 and 4 threads
+## 14:27:25 Finished searching
+## 14:27:26 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 40
+## 14:27:27 Initializing from normalized Laplacian + noise (using RSpectra)
+## 14:27:27 Range-scaling initial input columns to 0-10
+## 14:27:27 Commencing optimization for 500 epochs, with 586026 positive edges using 4 threads
+## 14:27:27 Using rng type: pcg
+## 14:27:30 Optimization finished
+## 2026-10-16 14:27:30 Added UMAP embedding as 'UMAP_ATAC' (2 components)
+## 2026-10-16 14:27:30 Extracting embedding 'LSI_RNA'
+## 2026-10-16 14:27:30 Computing UMAP on 10010 cells with 30 dimensions
+## 14:27:30 Using HNSW for nearest neighbor search
+## 14:27:30 UMAP embedding parameters a = 0.2321 b = 1.681
+## 14:27:30 Setting random seed 1
+## 14:27:30 Read 10010 rows and found 30 numeric columns
+## 14:27:30 Building HNSW index with metric 'cosine' ef = 200 M = 16 using 1 threads
+## 14:27:31 Finished building index
+## 14:27:31 Searching HNSW index with ef = 40 and 4 threads
+## 14:27:31 Finished searching
+## 14:27:32 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 40
+## 14:27:33 Initializing from normalized Laplacian + noise (using RSpectra)
+## 14:27:33 Range-scaling initial input columns to 0-10
+## 14:27:33 Commencing optimization for 500 epochs, with 573228 positive edges using 4 threads
+## 14:27:33 Using rng type: pcg
+## 14:27:36 Optimization finished
+## 2026-10-16 14:27:36 Added UMAP embedding as 'UMAP_RNA' (2 components)
+## 2026-10-16 14:27:36 Extracting embedding 'LSI_Combined'
+## 2026-10-16 14:27:36 Computing UMAP on 10010 cells with 59 dimensions
+## 14:27:36 Using HNSW for nearest neighbor search
+## 14:27:36 UMAP embedding parameters a = 0.2321 b = 1.681
+## 14:27:36 Setting random seed 1
+## 14:27:36 Read 10010 rows and found 59 numeric columns
+## 14:27:36 Building HNSW index with metric 'cosine' ef = 200 M = 16 using 1 threads
+## 14:27:37 Finished building index
+## 14:27:37 Searching HNSW index with ef = 40 and 4 threads
+## 14:27:37 Finished searching
+## 14:27:38 Commencing smooth kNN distance calibration using 4 threads with target n_neighbors = 40
+## 14:27:39 Initializing from normalized Laplacian + noise (using RSpectra)
+## 14:27:39 Range-scaling initial input columns to 0-10
+## 14:27:39 Commencing optimization for 500 epochs, with 587204 positive edges using 4 threads
+## 14:27:39 Using rng type: pcg
+## 14:27:42 Optimization finished
+## 2026-10-16 14:27:42 Added UMAP embedding as 'UMAP_Combined' (2 components)
+
+

All three embeddings, colored by the joint clusters:

+
plot_embedding(proj, embedding = "UMAP_ATAC", title = "ATAC") +
+  plot_embedding(proj, embedding = "UMAP_RNA", title = "RNA") +
+  plot_embedding(proj, embedding = "UMAP_Combined", title = "Combined") +
+  patchwork::plot_layout(guides = "collect")
+

+
+ + + +
+
+ +
+ + + + + + + + + + + + + + + +