Enables easy loading of sparse data matrices provided by alevin-fry USA mode.

loadFry(
  fryDir,
  outputFormat = "scRNA",
  aux_columns = c("X", "Y"),
  gene_id_to_name = NULL,
  nonzero = FALSE,
  quiet = FALSE,
  featureLevel = c("gene", "probe"),
  probeRef = NULL
)

Arguments

fryDir

path to the output directory returned by alevin-fry quant command. This directory should contain a metainfo.json, and an alevin folder which contains quants_mat.mtx, quants_mat_cols.txt and quants_mat_rows.txt

outputFormat

can be either be a list that defines the desired format of the output SingleCellExperiment object or a string that represents one of the pre-defined output formats, which are "scRNA" (or "S+A"), "snRNA" (or "all", "U+S+A"), "scVelo", and "velocity". See details for the explanations of the pre-defined formats and how to define custom format.

aux_columns

vector of characters containing the column names of the auxiliary information in the barcodes file starting from the second column. The first column is assumed to be the barcodes and is named as "barcodes". Extra auxiliary columns in the barcodes file without a specified name will be ignored.

gene_id_to_name

path to a file that contains the mapping from gene names to gene ids. It is only needed if
1. you are not using the simpleaf pipeline (`simpleaf index` + `simpleaf quant`),
2. you have such a file, and,
3. you want to add this information to the colData of your output object.
If you do, please ensure it is a tab-separated, two-column file without a header, and the first column is the gene ids and the second column is the gene names.

nonzero

whether to filter cells with non-zero expression value across all genes (default FALSE). If TRUE, this will filter based on all assays. If a string vector of assay names, it will filter based on the matching assays in the vector. If not in USA mode, it must be TRUE/FALSE/counts.

quiet

logical whether to display no messages

featureLevel

"gene" (the default) reads the cell by gene matrix quants_mat.mtx. "probe" reads the cell by probe matrix probe_quants_mat.mtx, which alevin-fry quant-probe --probe-mtx (or simpleaf multiplex-quant --probe-mtx) writes. The function stops with an error if quant.json does not have "probe_mtx": true.

probeRef

only for featureLevel = "probe": the path to the 10x probe-set CSV of the run, which gives the gene of each probe. If it is NULL (the default), the function uses probe_set.csv in fryDir. The function stops with an error if the file does not exist. See the Flex section.

Value

A SingleCellExperiment object that contains one or more assays. Each assay consists of a gene by cell count matrix. The row names are feature names, and the column names are cell barcodes (for a multiplex run, <sample_name>_<cell_barcode>)

Details about loadFry

This function consumes the result folder returned by running alevin-fry quant in unspliced, spliced, ambiguous (USA) quantification mode, and returns a SingleCellExperiment object that contains a final count for each gene within each cell. In USA mode, alevin-fry quant returns a count matrix contains three types of count for each feature (gene) within each sample (cell or nucleus), which represent the spliced mRNA count of the gene (S), the unspliced mRNA count of the gene (U), and the count of UMIs whose splicing status is ambiguous for the gene (A). For each assay defined by outputFormat, these three counts of a gene within a cell will be summed to get the final count of the gene according to the rule defined in the outputFormat. The returned object will contains the desired assays defined by outputFormat, with rownames as the barcode of samples and colnames as the feature names.

Details about the output format

The outputFormat argument takes either be a list that defines the desired format of the output SingleCellExperiment object or a string that represents one of the pre-defined output format.

Currently the pre-defined formats of the output SingleCellExperiment object are:

"scRNA":

This format is recommended for single cell experiments. It returns a counts assay that contains the S+A count of each gene in each cell, and a unspliced assay that contains the U count of each gene in each cell.

"snRNA", "all" and "U+S+A":

These three formats are the same. They return a counts assay that contains the U+S+A count of each gene in each cell without any extra layers. "snRNA" is recommended for single-nucleus RNA-sequencing experiments. "raw" is recommended for mimicing CellRanger 7's behavior, which returns this format for both single-cell and single-nucleus experiments.

"S+A":

This format returns a counts assay that contains the S+A count of each gene in each cell.

"raw":

This format puts the three kinds of counts into three separate assays, which are unspliced, spliced and ambiguous.

"velocity":

This format contains two assays. The spliced assay contains the S+A count of each gene in each cell. The unspliced assay contains the U counts of each gene in each cell.

"scVelo":

This format is for direct entry into velociraptor R package or other scVelo downstream analysis pipeline for velocity analysis in R with Bioconductor. It adds the expected "S"-pliced assay and removes errors for size factors being non-positive.

A custom output format can be defined using a list. Each element in the list defines an assay in the output SingleCellExperiment object. The name of an element in the list will be the name of the corresponding assay in the output object. Each element in the list should be defined as a vector that takes at least one of the three kinds of count, which are U, S and A. See the provided toy example for defining a custom output format.

Details about Flex (multiplex) output

loadFry also reads the output of simpleaf multiplex-quant and alevin-fry quant-probe (10x Flex). In this output, the row label of a cell in quants_mat_rows.txt is <sample_name>_<cell_barcode>, because the same barcode can occur in two samples. The function adds the two parts to the colData as sample_name and cell_barcode. They come from the sample_name and CB columns of featureDump.txt, whose line i describes cell i; the function stops with an error if a line does not match its cell. If there is no featureDump.txt file, but the directory has a collation_manifest.bin or a sample_info.json file (a multiplex run), the function gives a warning and adds no columns. Other outputs get no new columns.

The gene_id_to_name.tsv file of a Flex run can have more gene ids than the matrix. The function adds the gene name of each gene in the matrix (a left join); a gene that is not in the file gets NA.

With featureLevel = "probe", the rows are the probes in probe_quants_mat_cols.txt, and the columns are the same cells as at gene level. The rowData is the probe-set CSV as it is: all its columns, with their names (for example gene_id, probe_seq, probe_id, included, region and gene_name), read with read.csv (for example, included is logical, and an empty value is NA). The function skips the lines at the top of the CSV that start with #, and uses the CSV rows of the matrix probes, in the order of the matrix (by probe_id, because the probe order of the matrix is not the order of the CSV). It stops with an error if a probe of the matrix is not in the CSV, or if the CSV has a probe_id more than one time. A 10x "unspliced" probe does not span a splice junction; it does not mean pre-mRNA. The sum of the probe counts of a gene (gene_id) is the gene count of the gene matrix.

References

alevin-fry publication:

He, D., Zakeri, M., Sarkar, H. et al. "Alevin-fry unlocks rapid, accurate and memory-frugal quantification of single-cell RNA-seq data." Nature Methods 19, 316–322 (2022). https://doi.org/10.1038/s41592-022-01408-3

Author

Dongze He, with contributions from Steve Lianoglou, Wes Wilson

Examples


# Get path for minimal example avelin-fry output dir
testdat <- fishpond:::readExampleFryData("fry-usa-basic")

# This is exactly how the velocity format defined internally.
custom_velocity_format <- list("spliced"=c("S","A"), "unspliced"=c("U"))

# Load alevin-fry gene quantification in velocity format
sce <- loadFry(fryDir=testdat$parent_dir, outputFormat=custom_velocity_format)
#> locating quant file
#> Reading meta data
#> USA mode: TRUE
#> Processing 4 genes and 4 barcodes
#> Using user-defined output assays
#> Building the 'spliced' assay, which contains S A
#> Building the 'unspliced' assay, which contains U
#> Constructing output SingleCellExperiment object
#> Done
SummarizedExperiment::assayNames(sce)
#> [1] "spliced"   "unspliced"

# Load the same data but use pre-defined, velociraptor R pckage desired format
scvelo_format <- "scVelo"

scev <- loadFry(fryDir=testdat$parent_dir, outputFormat=scvelo_format, nonzero=TRUE)
#> locating quant file
#> Reading meta data
#> USA mode: TRUE
#> Processing 4 genes and 4 barcodes
#> Using pre-defined output format: scvelo
#> Building the 'counts' assay, which contains S A
#> Building the 'spliced' assay, which contains S A
#> Building the 'unspliced' assay, which contains U
#> Constructing output SingleCellExperiment object
#> Done
SummarizedExperiment::assayNames(scev)
#> [1] "counts"    "spliced"   "unspliced"

# Load a minimal Flex (multiplex) output, at gene level and at probe level
flexdir <- system.file("extdata", "alevin", "example-quants", "flex",
                       "flex_probe", package = "fishpond")
sce <- loadFry(flexdir)
#> locating quant file
#> Reading meta data
#> USA mode: FALSE
#> Reading the sample of each cell from /__w/_temp/Library/fishpond/extdata/alevin/example-quants/flex/flex_probe/featureDump.txt
#> Using simpleaf default gene id to name file gene_id_to_name.tsv in the given fryDir
#> Processing 6 genes and 10 barcodes
#> Not in USA mode, set assay name as "counts"
#> Constructing output SingleCellExperiment object
#> Done
table(sce$sample_name)
#> 
#>   1e5   S_1 S_1_A 
#>     3     4     3 
scep <- loadFry(flexdir, featureLevel = "probe")
#> locating quant file
#> Reading meta data
#> USA mode: FALSE
#> Using the probe-set CSV /__w/_temp/Library/fishpond/extdata/alevin/example-quants/flex/flex_probe/probe_set.csv
#> Reading the sample of each cell from /__w/_temp/Library/fishpond/extdata/alevin/example-quants/flex/flex_probe/featureDump.txt
#> Using simpleaf default gene id to name file gene_id_to_name.tsv in the given fryDir
#> Processing 7 probes and 10 barcodes
#> Not in USA mode, set assay name as "counts"
#> Constructing output SingleCellExperiment object
#> Done
SummarizedExperiment::rowData(scep)
#> DataFrame with 7 rows and 6 columns
#>                                gene_id              probe_seq
#>                            <character>            <character>
#> GENE000050|Epsilon|b4ed544  GENE000050 CATAGGTAAGAGGGAGAGTG..
#> GENE000020|Beta|957d909     GENE000020 GAAGTATCCACGAGTTCGAA..
#> GENE000010|Alpha|3ea2dde    GENE000010 TACACCCGGAGCATAAAAGA..
#> GENE000060|Zeta|59475df     GENE000060 CTAAGAGACCGGTTATGAAA..
#> GENE000020|Beta|5a8364d     GENE000020 TGCGCCCATCCTATTTTCAG..
#> GENE000040|Delta|8c35c89    GENE000040 CCACAAGGGGCGGCTCTAAT..
#> GENE000030|Gamma|cf56783    GENE000030 TTACTCCTGGGTGCTATGTG..
#>                                          probe_id  included      region
#>                                       <character> <logical> <character>
#> GENE000050|Epsilon|b4ed544 GENE000050|Epsilon|b..      TRUE     spliced
#> GENE000020|Beta|957d909    GENE000020|Beta|957d..      TRUE   unspliced
#> GENE000010|Alpha|3ea2dde   GENE000010|Alpha|3ea..      TRUE     spliced
#> GENE000060|Zeta|59475df    GENE000060|Zeta|5947..      TRUE     spliced
#> GENE000020|Beta|5a8364d    GENE000020|Beta|5a83..      TRUE     spliced
#> GENE000040|Delta|8c35c89   GENE000040|Delta|8c3..      TRUE   unspliced
#> GENE000030|Gamma|cf56783   GENE000030|Gamma|cf5..      TRUE     spliced
#>                              gene_name
#>                            <character>
#> GENE000050|Epsilon|b4ed544     Epsilon
#> GENE000020|Beta|957d909           Beta
#> GENE000010|Alpha|3ea2dde         Alpha
#> GENE000060|Zeta|59475df           Zeta
#> GENE000020|Beta|5a8364d           Beta
#> GENE000040|Delta|8c35c89         Delta
#> GENE000030|Gamma|cf56783         Gamma