In this vignette, we process fastq data from scRNA-seq (10x v2 chemistry) with to make a sparse matrix that can be used in downstream analysis. Then we will start that standard downstream analysis with Seurat
.
Setup
First, if you want to run this notebook, please make sure you have git
installed, go to the terminal and
git clone https://github.com/BUStools/BUS_notebooks_R.git
This notebook is in the analysis
directory. Also, in RStudio, go to Tools -> Global Options -> R Markdown, and set “Evaluate chunks in directory” to “Project”. The reason why is that this website was built with workflowr
, which renders the notebooks in the project directory rather than the document directory.
Alternatively, you can directly download this notebook here and open it in RStudio, but make sure to adjust the paths where files are downloaded and where output files are stored to match those in your environment.
Install packages
The primary analysis section of this notebook demonstrates the use of command line tools kallisto
and bustools
. Please use kallisto
0.45, whose binary can be downloaded here. The binary of bustools
can be found here.
After you download the binary, you should decompress the file (if it is tar.gz
) with tar -xzvf file.tar.gz
in the bash
terminal, and add the directory containing the binary to PATH
by export PATH=$PATH:/foo/bar
, where /foo/bar
here is the directory of interest. Then you can directly invoke the binary on the command line as we will do in this notebook.
Note for Windows users: bustools
does not have a Windows binary, but you can use a Linux subsystem in Windows 10. If you use earlier version of Windows, then unfortunately you need to either use Linux dual boot or virtual machine or find a Linux or Mac computer such as a server.
We will be using the R packages below. BUSpaRse
is not yet on CRAN or Bioconductor. For Mac users, see the installation note for BUSpaRse
. We will also use Seurat
3.0, which is not yet on CRAN (the CRAN version is 2.3.4), for the standard downstream analysis. Seurat
3.0 has some cool new features such as getting batch corrected gene count matrix and cell label transfer across datasets. See this paper for more detail. Though we will not get into those new features here, it’s worthwhile to check them out.
# Install devtools if it's not already installed
if (!require(devtools)) {
install.packages("devtools")
}
# Install from GitHub
devtools::install_github("BUStools/BUSpaRse")
devtools::install_github("satijalab/seurat", ref = "release/3.0")
The package DropletUtils
will be used to estimate the number of real cells as opposed to empty droplets. It’s on Bioconductor, and here is how it should be installed:
if (!require(BiocManager)) {
install.packages("BiocManager")
}
BiocManager::install("DropletUtils")
The other R packages below are on CRAN, and can be installed with install.packages
.
library(BUSpaRse)
library(data.table)
library(Seurat)
library(tidyverse)
library(DropletUtils)
library(Matrix)
Download data
The data set we are using here is 1k 1:1 Mixture of Fresh Frozen Human (HEK293T) and Mouse (NIH3T3) Cells from the 10x website. First, we download the fastq files (6.34 GB).
download.file("http://cf.10xgenomics.com/samples/cell-exp/2.1.0/hgmm_1k/hgmm_1k_fastqs.tar", destfile = "./data/hgmm_1k_fastqs.tar", quiet = TRUE)
Then untar this file
cd ./data
tar -xvf ./hgmm_1k_fastqs.tar
#> fastqs/
#> fastqs/hgmm_1k_S1_L001_I1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L001_R1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L001_R2_001.fastq.gz
#> fastqs/hgmm_1k_S1_L002_I1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L002_R1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L002_R2_001.fastq.gz
#> fastqs/hgmm_1k_S1_L003_I1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L003_R1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L003_R2_001.fastq.gz
#> fastqs/hgmm_1k_S1_L004_I1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L004_R1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L004_R2_001.fastq.gz
#> fastqs/hgmm_1k_S1_L005_I1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L005_R1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L005_R2_001.fastq.gz
#> fastqs/hgmm_1k_S1_L006_I1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L006_R1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L006_R2_001.fastq.gz
#> fastqs/hgmm_1k_S1_L007_I1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L007_R1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L007_R2_001.fastq.gz
#> fastqs/hgmm_1k_S1_L008_I1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L008_R1_001.fastq.gz
#> fastqs/hgmm_1k_S1_L008_R2_001.fastq.gz
Primary analysis
Build the kallisto
index
Here we use kallisto to pseudoalign the reads to the transcriptome and then to create the bus
file to be converted to a sparse matrix. The first step is to build an index of the transcriptome. This data set has both human and mouse cells, so we need both human and mouse transcriptomes. The transcriptomes downloaded here are from Ensembl version 95, released in January 2019. As of the writing of this notebook, this is the most recent Ensembl release.
# Human transcriptome
download.file("ftp://ftp.ensembl.org/pub/release-95/fasta/homo_sapiens/cdna/Homo_sapiens.GRCh38.cdna.all.fa.gz", "./data/hs_cdna.fa.gz", quiet = TRUE)
# Mouse transcriptome
download.file("ftp://ftp.ensembl.org/pub/release-95/fasta/mus_musculus/cdna/Mus_musculus.GRCm38.cdna.all.fa.gz", "./data/mm_cdna.fa.gz", quiet = TRUE)
kallisto version
#> kallisto, version 0.45.0
Actually, we don’t need to unzip the fasta files
kallisto index -i ./output/hs_mm_tr_index.idx ./data/hs_cdna.fa.gz ./data/mm_cdna.fa.gz
#>
#> [build] loading fasta file ./data/hs_cdna.fa.gz
#> [build] loading fasta file ./data/mm_cdna.fa.gz
#> [build] k-mer length: 31
#> [build] warning: clipped off poly-A tail (longer than 10)
#> from 2083 target sequences
#> [build] warning: replaced 8 non-ACGUT characters in the input sequence
#> with pseudorandom nucleotides
#> [build] counting k-mers ... done.
#> [build] building target de Bruijn graph ... done
#> [build] creating equivalence classes ... done
#> [build] target de Bruijn graph has 2145397 contigs and contains 206575877 k-mers
Run kallisto bus
Here we will generate the bus
file. Here bus
stands for Barbode, UMI, Set (i.e. equivalent class). In text form, it is a table whose first column is the barcode. The second column is the UMI that are associated with the barcode. The third column is the index of the equivalence class reads with the UMI maps to (equivalence class will be explained later). The fourth column is count of reads with this barcode, UMI, and equivalence class combination, which is ignored as one UMI should stand for one molecule. See this paper for more detail.
These are the technologies supported by kallisto bus
:
system("kallisto bus --list", intern = TRUE)
#> Warning in system("kallisto bus --list", intern = TRUE): running command
#> 'kallisto bus --list' had status 1
#> [1] "List of supported single cell technologies"
#> [2] ""
#> [3] "short name description"
#> [4] "---------- -----------"
#> [5] "10Xv1 10X chemistry version 1"
#> [6] "10Xv2 10X chemistry verison 2"
#> [7] "DropSeq DropSeq"
#> [8] "inDrop inDrop"
#> [9] "CELSeq CEL-Seq"
#> [10] "CELSeq2 CEL-Seq version 2"
#> [11] "SCRBSeq SCRB-Seq"
#> [12] ""
#> attr(,"status")
#> [1] 1
Here we have 8 samples. Each sample has 3 files: I1
means sample index, R1
means barcode and UMI, and R2
means the piece of cDNA. The -i
argument specifies the index file we just built. The -o
argument specifies the output directory. The -x
argument specifies the sequencing technology used to generate this data set. The -t
argument specifies the number of threads used. I ran this on a server and used 8 threads.
cd ./data
kallisto bus -i ../output/hs_mm_tr_index.idx -o ../output/out_hgmm1k -x 10xv2 -t8 \
./fastqs/hgmm_1k_S1_L001_R1_001.fastq.gz ./fastqs/hgmm_1k_S1_L001_R2_001.fastq.gz \
./fastqs/hgmm_1k_S1_L002_R1_001.fastq.gz ./fastqs/hgmm_1k_S1_L002_R2_001.fastq.gz \
./fastqs/hgmm_1k_S1_L003_R1_001.fastq.gz ./fastqs/hgmm_1k_S1_L003_R2_001.fastq.gz \
./fastqs/hgmm_1k_S1_L004_R1_001.fastq.gz ./fastqs/hgmm_1k_S1_L004_R2_001.fastq.gz \
./fastqs/hgmm_1k_S1_L005_R1_001.fastq.gz ./fastqs/hgmm_1k_S1_L005_R2_001.fastq.gz \
./fastqs/hgmm_1k_S1_L006_R1_001.fastq.gz ./fastqs/hgmm_1k_S1_L006_R2_001.fastq.gz \
./fastqs/hgmm_1k_S1_L007_R1_001.fastq.gz ./fastqs/hgmm_1k_S1_L007_R2_001.fastq.gz \
./fastqs/hgmm_1k_S1_L008_R1_001.fastq.gz ./fastqs/hgmm_1k_S1_L008_R2_001.fastq.gz
#>
#> [index] k-mer length: 31
#> [index] number of targets: 303,693
#> [index] number of k-mers: 206,575,877
#> [index] number of equivalence classes: 1,256,267
#> [quant] will process sample 1: ./fastqs/hgmm_1k_S1_L001_R1_001.fastq.gz
#> ./fastqs/hgmm_1k_S1_L001_R2_001.fastq.gz
#> [quant] will process sample 2: ./fastqs/hgmm_1k_S1_L002_R1_001.fastq.gz
#> ./fastqs/hgmm_1k_S1_L002_R2_001.fastq.gz
#> [quant] will process sample 3: ./fastqs/hgmm_1k_S1_L003_R1_001.fastq.gz
#> ./fastqs/hgmm_1k_S1_L003_R2_001.fastq.gz
#> [quant] will process sample 4: ./fastqs/hgmm_1k_S1_L004_R1_001.fastq.gz
#> ./fastqs/hgmm_1k_S1_L004_R2_001.fastq.gz
#> [quant] will process sample 5: ./fastqs/hgmm_1k_S1_L005_R1_001.fastq.gz
#> ./fastqs/hgmm_1k_S1_L005_R2_001.fastq.gz
#> [quant] will process sample 6: ./fastqs/hgmm_1k_S1_L006_R1_001.fastq.gz
#> ./fastqs/hgmm_1k_S1_L006_R2_001.fastq.gz
#> [quant] will process sample 7: ./fastqs/hgmm_1k_S1_L007_R1_001.fastq.gz
#> ./fastqs/hgmm_1k_S1_L007_R2_001.fastq.gz
#> [quant] will process sample 8: ./fastqs/hgmm_1k_S1_L008_R1_001.fastq.gz
#> ./fastqs/hgmm_1k_S1_L008_R2_001.fastq.gz
#> [quant] finding pseudoalignments for the reads ... done
#> [quant] processed 63,252,296 reads, 52,231,469 reads pseudoaligned
See what the outputs are
list.files("./output/out_hgmm1k/")
#> [1] "matrix.ec" "output.bus" "output.sorted"
#> [4] "output.sorted.txt" "run_info.json" "transcripts.txt"
This is what the bus
file looks like:
First column is barcode, second column is UMI, 3rd column is equivalence class index, and 4th column is count, which is ignored. This has already been sorted according to barcode.
head ./output/out_hgmm1k/output.sorted.txt
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 6575 1
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 9414 1
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 10354 1
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 38532 1
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 112639 1
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 123866 1
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 136025 1
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 204499 2
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 209310 1
#> AAAAAAAAAAAAAAAA AAAAAAAAAA 221957 1
Secondary analysis
Map transcripts to genes
For the sparse matrix, most people are interested in how many UMIs per gene per cell, we here we will quantify this from the bus
output, and to do so, we need to find which gene corresponds to each transcript. Remember in the output of kallisto bus
, there’s the file transcripts.txt
. Those are the transcripts in the transcriptome index. Now we’ll only keep the transcripts present there and make sure that the transcripts in tr2g
are in the same order as those in the index. You will soon see why the order is important.
Remember that we downloaded transcriptome FASTA files from Ensembl just now. In FASTA files, each entry is a sequence with a name. In Ensembl FASTA files, the sequence name has genome annotation of the corresponding sequence, so we can extract transcript IDs and corresponding gene IDs and gene names from there.
tr2g <- transcript2gene(fasta_file = c("./data/hs_cdna.fa.gz", "./data/mm_cdna.fa.gz"),
kallisto_out_path = "./output/out_hgmm1k")
#> Reading FASTA file.
#> Reading FASTA file.
#> Sorting transcripts
head(tr2g) %>% knitr::kable()
ENST00000632684.1 |
ENSG00000282431.1 |
TRBD1 |
ENST00000434970.2 |
ENSG00000237235.2 |
TRDD2 |
ENST00000448914.1 |
ENSG00000228985.1 |
TRDD3 |
ENST00000415118.1 |
ENSG00000223997.1 |
TRDD1 |
ENST00000631435.1 |
ENSG00000282253.1 |
TRBD1 |
ENST00000390583.1 |
ENSG00000211923.1 |
IGHD3-10 |
Alternative ways of getting tr2g
have been implemented in the BUSpaRse
package. You may use tr2g_ensembl
to query Ensembl with biomart to get transcript and gene IDs. If you use this method, then please make sure that the Ensembl version used in the query matches that of the transcriptome. This method is convenient for the user since you only need to input species names, but it can be slow since biomart database query can be slow. You may also use tr2g_gtf
for GTF files and tr2g_gff3
for GFF3 files, which are more useful for non-model organisms absent from Ensemble. After calling the tr2g_*
family of functions, you should sort the transcripts from those functions with sort_tr2g
so the transcripts are in the same order as those in the kallisto index. The FASTA way is the fastest, as reading FASTA files is faster than reading GTF and GFF files. Whichever way you choose, this shouldn’t take much more than half a minute per species.
The function transcript2gene
not only gets the transcript and gene IDs but also sorts the transcripts. This function only supports Ensembl biomart query and Ensembl fasta files as the source of transcript and gene IDs, as the attribute field of GTF and GFF3 files can differ between sources and further cleanup may be needed.
Map ECs to genes
The 3rd column in the output.sorted.txt
is the equivalence class (EC) index of each UMI for each cell barcode. Equivalence class (EC) means the set of transcripts in the transcriptome that the read is compatible to. While in most cases, an EC only has transcripts for the same gene, there are some ECs that have transcripts for different genes. The file in the kallisto bus
output, matrix.ec
, maps the EC index in output.sorted.txt
to sets of line numbers in the transcriptome assembly. That’s why we ensured that the tr2g
data frame has the same order as the transcripts in the index.
Also note that while this function supports multithreading, unless the matrix.ec
file is really large, setting ncores > 1
will not improve performance since only 1 core will be used anyway. It may even compromise performance since creating multiple processes has overhead. For this dataset, where matrix.ec
is 68.8MB, it is not large enough to warrant the use of more than 1 core.
genes <- EC2gene(tr2g, "./output/out_hgmm1k", ncores = 1)
#> Reading matrix.ec
#> Processing genes
Now for each EC, we have a set of genes the EC is compatible to.
head(genes)
#> [[1]]
#> [1] "ENSG00000282431.1"
#>
#> [[2]]
#> [1] "ENSG00000237235.2"
#>
#> [[3]]
#> [1] "ENSG00000228985.1"
#>
#> [[4]]
#> [1] "ENSG00000223997.1"
#>
#> [[5]]
#> [1] "ENSG00000282253.1"
#>
#> [[6]]
#> [1] "ENSG00000211923.1"
tail(genes)
#> [[1]]
#> [1] "ENSG00000256683.6" "ENSG00000112276.13" "ENSG00000274671.4"
#> [4] "ENSG00000278767.4" "ENSG00000100625.8" "ENSG00000140093.9"
#> [7] "ENSG00000152939.15" "ENSG00000135956.8" "ENSG00000215271.7"
#> [10] "ENSG00000141965.4" "ENSG00000255561.6" "ENSG00000196376.10"
#> [13] "ENSG00000185960.14" "ENSG00000172000.7" "ENSG00000227454.2"
#> [16] "ENSG00000280700.1"
#>
#> [[2]]
#> [1] "ENSMUSG00000062647.16" "ENSMUSG00000115584.1"
#> [3] "ENSMUSG00000115188.1" "ENSMUSG00000117228.1"
#> [5] "ENSMUSG00000083553.1" "ENSMUSG00000107920.1"
#> [7] "ENSMUSG00000114756.1" "ENSMUSG00000067147.7"
#> [9] "ENSMUSG00000104775.1" "ENSMUSG00000081238.3"
#> [11] "ENSMUSG00000110109.1" "ENSMUSG00000062758.6"
#> [13] "ENSMUSG00000116481.1" "ENSMUSG00000081675.1"
#> [15] "ENSMUSG00000090305.2" "ENSMUSG00000106258.1"
#> [17] "ENSMUSG00000106243.1" "ENSMUSG00000117585.1"
#> [19] "ENSMUSG00000071052.4" "ENSMUSG00000086925.1"
#> [21] "ENSMUSG00000099927.1" "ENSMUSG00000112574.1"
#> [23] "ENSMUSG00000087321.1" "ENSMUSG00000099980.1"
#> [25] "ENSMUSG00000098893.1" "ENSMUSG00000082920.1"
#> [27] "ENSMUSG00000117002.1" "ENSMUSG00000083826.1"
#> [29] "ENSMUSG00000116757.1"
#>
#> [[3]]
#> [1] "ENSG00000165792.17" "ENSG00000171346.15" "ENSG00000180336.17"
#> [4] "ENSG00000105443.14" "ENSG00000148225.15" "ENSG00000101940.17"
#> [7] "ENSG00000157540.20" "ENSG00000197563.10" "ENSG00000003402.19"
#> [10] "ENSG00000136731.12" "ENSG00000160049.11"
#>
#> [[4]]
#> [1] "ENSG00000100626.16" "ENSG00000153283.12" "ENSG00000196312.13"
#> [4] "ENSG00000132680.10" "ENSG00000196418.12" "ENSG00000174197.16"
#> [7] "ENSG00000186185.13" "ENSG00000130164.13" "ENSG00000198089.15"
#> [10] "ENSG00000081320.10" "ENSG00000122779.17" "ENSG00000089053.12"
#> [13] "ENSG00000010244.18" "ENSG00000177034.15" "ENSG00000039068.18"
#> [16] "ENSG00000068079.7" "ENSG00000149554.12" "ENSG00000104408.9"
#> [19] "ENSG00000165525.17" "ENSG00000167523.13" "ENSG00000172339.9"
#> [22] "ENSG00000167721.10" "ENSG00000149182.14" "ENSG00000134759.13"
#> [25] "ENSG00000100554.11" "ENSG00000116871.15" "ENSG00000198252.11"
#> [28] "ENSG00000090382.6" "ENSG00000067533.5" "ENSG00000185869.14"
#> [31] "ENSG00000117505.12" "ENSG00000152229.18" "ENSG00000135966.12"
#> [34] "ENSG00000148655.14" "ENSG00000100243.20" "ENSG00000115756.12"
#> [37] "ENSG00000225973.3" "ENSG00000140451.12" "ENSG00000105793.15"
#> [40] "ENSG00000162144.9" "ENSG00000196417.12" "ENSG00000069020.18"
#> [43] "ENSG00000175575.12" "ENSG00000153066.12" "ENSG00000160062.14"
#> [46] "ENSG00000265491.4" "ENSG00000125965.8" "ENSG00000078674.17"
#> [49] "ENSG00000162241.12" "ENSG00000167703.14" "ENSG00000149483.11"
#> [52] "ENSG00000158987.20" "ENSG00000110031.12" "ENSG00000144730.17"
#> [55] "ENSG00000163705.12" "ENSG00000278550.4" "ENSG00000007545.15"
#> [58] "ENSG00000178927.17" "ENSG00000115947.13" "ENSG00000186687.15"
#> [61] "ENSG00000152348.15" "ENSG00000184898.6" "ENSG00000138380.17"
#> [64] "ENSG00000108587.15" "ENSG00000172000.7" "ENSG00000119906.12"
#> [67] "ENSG00000169371.13" "ENSG00000100034.13" "ENSG00000108448.21"
#> [70] "ENSG00000169895.5" "ENSG00000088888.17" "ENSG00000178950.16"
#> [73] "ENSG00000214176.9" "ENSG00000226789.1" "ENSG00000204118.2"
#>
#> [[5]]
#> [1] "ENSG00000127311.9" "ENSG00000135722.8"
#>
#> [[6]]
#> [1] "ENSG00000111832.12" "ENSG00000167637.16" "ENSG00000066422.4"
#> [4] "ENSG00000131269.16" "ENSG00000110871.14" "ENSG00000140416.20"
#> [7] "ENSG00000151131.10" "ENSG00000151338.18" "ENSG00000085382.11"
#> [10] "ENSG00000143315.7" "ENSG00000144460.12" "ENSG00000128973.12"
#> [13] "ENSG00000011021.22" "ENSG00000135643.4" "ENSG00000177565.16"
#> [16] "ENSG00000185324.21" "ENSG00000105726.16" "ENSG00000184304.14"
#> [19] "ENSG00000135747.11" "ENSG00000248905.8" "ENSG00000143322.19"
#> [22] "ENSG00000102144.14" "ENSG00000166822.12" "ENSG00000215883.10"
#> [25] "ENSG00000163728.10" "ENSG00000127603.25" "ENSG00000161328.10"
#> [28] "ENSG00000141551.14" "ENSG00000166938.12" "ENSG00000103657.13"
#> [31] "ENSG00000114982.17" "ENSG00000148225.15" "ENSG00000114902.13"
#> [34] "ENSG00000114904.12" "ENSG00000134539.16" "ENSG00000265491.4"
#> [37] "ENSG00000162971.10" "ENSG00000134245.17" "ENSG00000273831.2"
#> [40] "ENSG00000224383.7" "ENSG00000101417.11" "ENSG00000148688.13"
#> [43] "ENSG00000073605.18" "ENSG00000115641.18" "ENSG00000102290.22"
#> [46] "ENSG00000275111.4" "ENSG00000124357.12" "ENSG00000106069.22"
#> [49] "ENSG00000163964.14" "ENSG00000138592.13" "ENSG00000090238.11"
#> [52] "ENSG00000113532.12" "ENSG00000213949.8" "ENSG00000175387.15"
#> [55] "ENSG00000282513.1" "ENSG00000189136.9" "ENSG00000268949.1"
#> [58] "ENSG00000267253.1" "ENSG00000249616.1" "ENSG00000229816.1"
#> [61] "ENSG00000265158.1"
Apparently some ECs map to many genes.
Make the sparse matrix
For 10x, we do have a file with all valid cell barcodes that comes with CellRanger.
# Copy v2 chemistry whitelist to working directory
cp ~/cellranger-3.0.1/cellranger-cs/3.0.1/lib/python/cellranger/barcodes/737K-august-2016.txt \
./data/whitelist_v2.txt
# Read in the whitelist
whitelist_v2 <- fread("./data/whitelist_v2.txt", header = FALSE)$V1
length(whitelist_v2)
#> [1] 737280
There about 737K valid cell barcodes.
Now we have everything we need to make the sparse matrix. This function reads in output.sorted.txt
line by line and processes them. It does not do barcode correction for now, so the barcode must exactly match those in the whitelist if one is provided. It took 5 to 6 minutes to construct the sparse matrix in the hgmm6k dataset, which has over 280 million lines in output.sorted.txt
, which is over 9GB. Here the data set is smaller, and it takes less than a minute. A future version of BUSpaRse
will parallelize this process to make it more scalable for huge datasets. For now, it’s single threaded.
Note that the arguments est_ncells
(estimated number of cells) and est_ngenes
(estimated number of genes) are important. With the estimate, this function reserves memory for the data to be added into, reducing the need of reallocation, which will slow the function down. Since the vast majority of “cells” you get in this sparse matrix are empty droplets rather than cells, please put at least 200 times more “cells” than you actually expect in est_ncells
.
If you do not have a whitelist of barcodes, then it’s fine; the whitelist
argument is optional.
res_mat <- make_sparse_matrix("./output/out_hgmm1k/output.sorted.txt",
genes = genes, est_ncells = 3.5e5,
est_ngenes = nrow(tr2g),
whitelist = whitelist_v2)
#> Reading data
#> Read 5 million lines
#> Read 10 million lines
#> Read 15 million lines
#> Read 20 million lines
#> Read 25 million lines
#> Read 30 million lines
#> Read 35 million lines
#> Read 40 million lines
#> Constructing sparse matrix
The matrix we get has genes in rows and barcode in columns. The row names are the gene IDs (not using human readable gene names since they’re not guaranteed to be unique), and the column names are cell barcodes.
For Ensembl transcriptomes, all the 3 steps in BUSpaRse
we have just done can be condensed into one step, with the busparse_gene_count
function.
Explore the data
Remove empty droplets
Cool, so now we have the sparse matrix. What does it look like?
dim(res_mat)
#> [1] 47367 353961
That’s way more cells than we expect, which is about 1000. So what’s going on?
How many UMIs per barcode?
tot_counts <- Matrix::colSums(res_mat)
summary(tot_counts)
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 1.0 1.0 2.0 101.8 11.0 92917.0
The vast majority of “cells” have only a few UMI detected. Those are empty droplets. 10x claims to have cell capture rate of up to 65%, but in practice, depending on how many cells are in fact loaded, the rate can be much lower. A commonly used method to estimate the number of empty droplets is barcode ranking knee and inflection points, as those are often assumed to represent transition between two components of a distribution. While more sophisticated method exist (e.g. see emptyDrops
in DropletUtils
), for simplicity, we will use the barcode ranking method here. However, whichever way we go, we don’t have the ground truth.
# Compute barcode rank
bc_rank <- barcodeRanks(res_mat)
qplot(bc_rank$rank, bc_rank$total, geom = "line") +
geom_hline(yintercept = bc_rank$knee, color = "blue", linetype = 2) +
geom_hline(yintercept = bc_rank$inflection, color = "green", linetype = 2) +
annotate("text", x = 1000, y = 1.5 * c(bc_rank$knee, bc_rank$inflection),
label = c("knee", "inflection"), color = c("blue", "green")) +
scale_x_log10() +
scale_y_log10() +
labs(x = "Rank", y = "Total reads") +
theme_bw()
Expand here to see past versions of rank-1.png:
Version
|
Author
|
Date
|
8cd81e0
|
Lambda Moses
|
2019-02-14
|
The inflection point looks like a reasonable number of cells.
# Filter the matrix
res_mat <- res_mat[, tot_counts > bc_rank$inflection]
dim(res_mat)
#> [1] 47367 1123
Cell species
How many cells are from humans and how many from mice? The number of cells with mixed species indicates doublet rate.
gene_species <- ifelse(str_detect(rownames(res_mat), "^ENSMUSG"), "mouse", "human")
mouse_inds <- gene_species == "mouse"
human_inds <- gene_species == "human"
# mark cells as mouse or human
cell_species <- tibble(n_mouse_umi = Matrix::colSums(res_mat[mouse_inds,]),
n_human_umi = Matrix::colSums(res_mat[human_inds,]),
tot_umi = Matrix::colSums(res_mat),
prop_mouse = n_mouse_umi / tot_umi,
prop_human = n_human_umi / tot_umi)
# Classify species based on proportion of UMI, with cutoff of 90%
cell_species <- cell_species %>%
mutate(species = case_when(
prop_mouse > 0.9 ~ "mouse",
prop_human > 0.9 ~ "human",
TRUE ~ "mixed"
))
ggplot(cell_species, aes(n_human_umi, n_mouse_umi, color = species)) +
geom_point(size = 0.5) +
theme_bw()
Expand here to see past versions of barn-1.png:
Version
|
Author
|
Date
|
8cd81e0
|
Lambda Moses
|
2019-02-14
|
Great, looks like the vast majority of cells are not mixed.
cell_species %>%
dplyr::count(species) %>%
mutate(proportion = n / ncol(res_mat))
#> # A tibble: 3 x 3
#> species n proportion
#> <chr> <int> <dbl>
#> 1 human 579 0.516
#> 2 mixed 3 0.00267
#> 3 mouse 541 0.482
Great, only about 0.3% of cells here are doublets, which is lower than the ~1% 10x lists. Doublet rate tends to be lower when cell concentration is lower. However, doublets can still be formed with cells from the same species, so the number of mixed species “cells” is only a lower bound of doublet rate.
Dimension reduction
seu <- CreateSeuratObject(res_mat, min.cells = 3) %>%
NormalizeData(verbose = FALSE) %>%
ScaleData(verbose = FALSE) %>%
FindVariableFeatures(verbose = FALSE)
# Add species to meta data
seu <- AddMetaData(seu, metadata = cell_species$species, col.name = "species")
See how number of total counts and number of genes expressed are distributed.
VlnPlot(seu, c("nCount_RNA", "nFeature_RNA"), group.by = "species")
Expand here to see past versions of vln-1.png:
Version
|
Author
|
Date
|
8cd81e0
|
Lambda Moses
|
2019-02-14
|
seu <- RunPCA(seu, verbose = FALSE, npcs = 30)
ElbowPlot(seu, ndims = 30)
Expand here to see past versions of elbow-1.png:
Version
|
Author
|
Date
|
8cd81e0
|
Lambda Moses
|
2019-02-14
|
DimPlot(seu, reduction = "pca", pt.size = 0.5, group.by = "species")
Expand here to see past versions of pca-1.png:
Version
|
Author
|
Date
|
8cd81e0
|
Lambda Moses
|
2019-02-14
|
The first PC separates species, as expected. Also as expected, the doublets are in between human and mouse cells in this plot.
seu <- RunTSNE(seu, dims = 1:20, check_duplicates = FALSE)
DimPlot(seu, reduction = "tsne", pt.size = 0.5, group.by = "species")
Expand here to see past versions of tsne-1.png:
Version
|
Author
|
Date
|
8cd81e0
|
Lambda Moses
|
2019-02-14
|
The species separate, and the few doublets form its own cluster, as expected.