Contents

0.1 Introduction

The Barcode, UMI, Set (BUS) format is a new way to represent pseudoalignments of reads from RNA-seq. Files of this format can be efficiently generated by the command line tool kallisto bus. With kallisto bus and this package, we go from the fastq files to the sparse matrix used for downstream analysis such as with Seurat within half an hour, while Cell Ranger would take hours.

In this vignette, we convert an 10x 1:1 mouse and human cell mixture dataset from the BUS format to a sparse matrix. To see how the BUS format can be generated from fastq file, as well as more in depth vignettes, see the website of this package.

Note that this vignette is deprecated and is kept for historical reasons as it was implemented when kallisto | bustools was experimental. The functionality of make_sparse_matrix has been implemented more efficiently in the command line tool bustools. Please use the updated version of bustools and if you wish, the wrapper kb instead.

0.2 Download the dataset

# The dataset package
library(TENxBUSData)
library(BUSpaRse)
library(Matrix)
library(zeallot)
library(ggplot2)
TENxBUSData(".", dataset = "hgmm100")
#> see ?TENxBUSData and browseVignettes('TENxBUSData') for documentation
#> loading from cache
#> The downloaded files are in /tmp/RtmpCBJbBk/Rbuild3c46b2f26b786/BUSpaRse/vignettes/out_hgmm100
#> [1] "/tmp/RtmpCBJbBk/Rbuild3c46b2f26b786/BUSpaRse/vignettes/out_hgmm100"

0.3 Convert to sparse matrix

First, we map transcripts, as in the kallisto index, to the corresponding genes.

tr2g <- transcript2gene(species = c("Homo sapiens", "Mus musculus"), type = "vertebrate",
                        kallisto_out_path = "./out_hgmm100", ensembl_version = 99,
                        write_tr2g = FALSE)
#> Querying biomart for transcript and gene IDs of Homo sapiens
#> Querying biomart for transcript and gene IDs of Mus musculus
head(tr2g)
#>          transcript              gene gene_name chromosome_name
#> 1 ENST00000434970.2 ENSG00000237235.2     TRDD2              14
#> 2 ENST00000415118.1 ENSG00000223997.1     TRDD1              14
#> 3 ENST00000448914.1 ENSG00000228985.1     TRDD3              14
#> 4 ENST00000632684.1 ENSG00000282431.1     TRBD1               7
#> 5 ENST00000390583.1 ENSG00000211923.1  IGHD3-10              14
#> 6 ENST00000431440.2 ENSG00000232543.2  IGHD4-11              14

Here we make both the gene count matrix and the TCC matrix.

c(gene_count, tcc) %<-% make_sparse_matrix("./out_hgmm100/output.sorted.txt",
                               tr2g = tr2g, est_ncells = 1e5,
                               est_ngenes = nrow(tr2g))
#> Reading matrix.ec
#> Processing ECs
#> Matching genes to ECs
#> Reading data
#> Constructing gene count matrix
#> Constructing TCC matrix

0.4 Remove empty droplets

Here we have a sparse matrix with genes in rows and cells in columns.

dim(gene_count)
#> [1]  26937 151449

This dataset should only have about 100 cells, but here we get over 100,000. In fact, most of the barcodes correspond to empty droplets; they can be removed by filtering out barcodes with too few UMI.

tot_counts <- Matrix::colSums(gene_count)
summary(tot_counts)
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
#>     1.00     1.00     2.00    24.77     5.00 64041.00
df1 <- get_knee_df(gene_count)
infl1 <- get_inflection(df1)
knee_plot(df1, infl1)

gene_count <- gene_count[, tot_counts > infl1]
dim(gene_count)
#> [1] 26937   122

Then this sparse matrix can be used in Seurat for downstream analysis.

Likewise, we can remove empty droplets from the TCC matrix.

dim(tcc)
#> [1] 129371 157659

This dataset should only have about 100 cells, but here we get over 100,000. In fact, most of the barcodes correspond to empty droplets; they can be removed by filtering out barcodes with too few UMI.

tot_counts <- Matrix::colSums(tcc)
summary(tot_counts)
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
#>     1.00     1.00     2.00    25.84     5.00 69235.00
df2 <- get_knee_df(tcc)
infl2 <- get_inflection(df2)
knee_plot(df2, infl2)

tcc <- tcc[, tot_counts > infl2]
dim(tcc)
#> [1] 129371    121
sessionInfo()
#> R version 4.3.0 RC (2023-04-13 r84269)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.2 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.17-bioc/R/lib/libRblas.so 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB              LC_COLLATE=C              
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: America/New_York
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.4.2      zeallot_0.1.0      Matrix_1.5-4       BUSpaRse_1.14.1   
#> [5] TENxBUSData_1.14.0 BiocStyle_2.28.0  
#> 
#> loaded via a namespace (and not attached):
#>   [1] DBI_1.1.3                     bitops_1.0-7                 
#>   [3] biomaRt_2.56.0                rlang_1.1.1                  
#>   [5] magrittr_2.0.3                matrixStats_0.63.0           
#>   [7] compiler_4.3.0                RSQLite_2.3.1                
#>   [9] GenomicFeatures_1.52.0        png_0.1-8                    
#>  [11] vctrs_0.6.2                   stringr_1.5.0                
#>  [13] ProtGenerics_1.32.0           pkgconfig_2.0.3              
#>  [15] crayon_1.5.2                  fastmap_1.1.1                
#>  [17] magick_2.7.4                  dbplyr_2.3.2                 
#>  [19] XVector_0.40.0                ellipsis_0.3.2               
#>  [21] utf8_1.2.3                    Rsamtools_2.16.0             
#>  [23] promises_1.2.0.1              rmarkdown_2.21               
#>  [25] purrr_1.0.1                   bit_4.0.5                    
#>  [27] xfun_0.39                     zlibbioc_1.46.0              
#>  [29] cachem_1.0.8                  GenomeInfoDb_1.36.0          
#>  [31] jsonlite_1.8.4                progress_1.2.2               
#>  [33] blob_1.2.4                    highr_0.10                   
#>  [35] later_1.3.1                   DelayedArray_0.26.1          
#>  [37] BiocParallel_1.34.0           interactiveDisplayBase_1.38.0
#>  [39] parallel_4.3.0                prettyunits_1.1.1            
#>  [41] plyranges_1.20.0              R6_2.5.1                     
#>  [43] bslib_0.4.2                   stringi_1.7.12               
#>  [45] rtracklayer_1.60.0            GenomicRanges_1.52.0         
#>  [47] jquerylib_0.1.4               Rcpp_1.0.10                  
#>  [49] bookdown_0.33                 SummarizedExperiment_1.30.1  
#>  [51] knitr_1.42                    IRanges_2.34.0               
#>  [53] httpuv_1.6.9                  tidyselect_1.2.0             
#>  [55] yaml_2.3.7                    codetools_0.2-19             
#>  [57] curl_5.0.0                    lattice_0.21-8               
#>  [59] tibble_3.2.1                  withr_2.5.0                  
#>  [61] Biobase_2.60.0                shiny_1.7.4                  
#>  [63] KEGGREST_1.40.0               evaluate_0.20                
#>  [65] BiocFileCache_2.8.0           xml2_1.3.4                   
#>  [67] ExperimentHub_2.8.0           Biostrings_2.68.0            
#>  [69] pillar_1.9.0                  BiocManager_1.30.20          
#>  [71] filelock_1.0.2                MatrixGenerics_1.12.0        
#>  [73] stats4_4.3.0                  generics_0.1.3               
#>  [75] RCurl_1.98-1.12               BiocVersion_3.17.1           
#>  [77] ensembldb_2.24.0              S4Vectors_0.38.1             
#>  [79] hms_1.1.3                     munsell_0.5.0                
#>  [81] scales_1.2.1                  xtable_1.8-4                 
#>  [83] glue_1.6.2                    lazyeval_0.2.2               
#>  [85] tools_4.3.0                   AnnotationHub_3.8.0          
#>  [87] BiocIO_1.10.0                 BSgenome_1.68.0              
#>  [89] GenomicAlignments_1.36.0      XML_3.99-0.14                
#>  [91] grid_4.3.0                    tidyr_1.3.0                  
#>  [93] colorspace_2.1-0              AnnotationDbi_1.62.1         
#>  [95] GenomeInfoDbData_1.2.10       restfulr_0.0.15              
#>  [97] cli_3.6.1                     rappdirs_0.3.3               
#>  [99] fansi_1.0.4                   S4Arrays_1.0.1               
#> [101] dplyr_1.1.2                   AnnotationFilter_1.24.0      
#> [103] gtable_0.3.3                  sass_0.4.5                   
#> [105] digest_0.6.31                 BiocGenerics_0.46.0          
#> [107] farver_2.1.1                  rjson_0.2.21                 
#> [109] memoise_2.0.1                 htmltools_0.5.5              
#> [111] lifecycle_1.0.3               httr_1.4.5                   
#> [113] mime_0.12                     bit64_4.0.5