library(data.table)
library(ggplot2)
library(CellBarcode)

1 Preprocess of CellRanger output bam file

The the information in the bam file. There are RNA sequence, Cell barcode and UMI in the bam file. We need to get the barcode in the RNA sequence together with the Cell barcode and UMI.

Where to find the bam file. Usually, the bam file in in following location of the CellRanger output.

CellRanger Output fold/outs/possorted_genome_bam.bam

Why preprocess. We need get the sam file as input. And in some cases we can do some filtering to make the input file smaller, for reducing the running time.

Example 1 get the sam file

samtools view possorted_genome_bam.bam > scRNASeq_10X.sam

Example 2 get the sam file only contain un mapped reads

In most of the time, the barcodes are designed not overlap with the genome sequence, so those barcodes sequences are not mapped to the reference genome. Add a simple parameter, we can get the un-mapped reads to significantly reduce the running time of the barcode extraction procedure. In the following example, the scRNASeq_10X.sam file only contains the un-mapped reads.

samtools view -f 4 possorted_genome_bam.bam > scRNASeq_10X.sam

2 Extract lineage barcode

Extract lineage barcode with cell-barcode, UMI information.

The parameters:

  • sam: The location of sam format file derived from CellRanger output bam file.

  • pattern: A regular expression, that matchs the lineage barcode. The first capture will be extract as lineage barcode. Please check next section to know more about how to define a pattern.

  • cell_barcode_tag: The cell barcode field tag in the sam file, the default is “CR” in the Cell Ranger output. You do not need to modify it, unless you know what you are doing.

  • umi_tag: The UMI field tag in the sam file, the default is “UR” in the Cell Ranger output. You do not need to modify it, unless you know what you are doing.

sam_file <- system.file("extdata", "scRNASeq_10X.sam", package = "CellBarcode")

d = bc_extract_10XscSeq(
   sam = sam_file,
   pattern = "AGATCAG(.*)TGTGGTA",
   cell_barcode_tag = "CR",
   umi_tag = "UR"
)
head(d)
#>       cell_barcode          umi           barcode_seq count
#> 1 AATAGAGGTTCTAACG AAGACAGGCTTA AGCCATTATGTCGTGTCATGA     1
#> 2 AAGTGAAAGTCCCGGT AGCCATTGTCCT TTTAATAGTTTCTATTTTGGA     1
#> 3 AAGTCGTAGCACGGAT AAAGCCTGCAAG CTATATATTCGTTGTATTATA     1
#> 4 AAGACAAAGGCGCTTC CGTCTTGGTAGC TCATTTGATACTAAGAATGAA     2
#> 5 AAGACAAAGCTAAATG GTATTCTCTGAG         ATGGTTGTAGATG     1
#> 6 AACTTCTGTGTTCCTC GACAACCTTGGT GGGTGGGATTCAGTGATCATA     1

The output is a data.frame. It contains 3 columns:

  • cell_barcode: It is unique identificaiton for each cell. One cell_barcode corresponding to a cell.

  • umi: It is used to label the molecular before PCR, a unique UMI means one molecular.

  • barcode_seq: Depends on the experiment design, it usually labels the lineage.

  • count: The reads number of a specifc cell-barcode, UMI, lineage barcode combination.

3 More about the pattern

The pattern is a regular expression, it tells the function where to find the barcode. In the pattern, we define the barcode backbone, and label the barcode sequence by bracket ().

For example, the pattern ATCG(.{21})TCGG tells the barcode is surrounded by constant sequence of ATCG, and TCGG. Following are some examples to define the constant region and barcode sequence.

Example 1

ATCG(.{21})

21 bases barcode after a constant sequence of “ATCG”.

Example 2

(.{15})TCGA

15 bases barcode before a constant sequence of “TCGA”.

Example 3

ATCG(.*)TCGA

A flexible length barcode between constant regions of “ATCG” and “TCGA”.

Need more helps: About more complex barcode pattern, please ask the package author, then the exmaple will be apear here.

4 Session Info

sessionInfo()
#> R version 4.2.1 (2022-06-23)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.5 LTS
#> 
#> Matrix products: default
#> BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
#> LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
#> 
#> 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       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] CellBarcode_1.4.0 ggplot2_3.3.6     data.table_1.14.4 BiocStyle_2.26.0 
#> 
#> loaded via a namespace (and not attached):
#>  [1] MatrixGenerics_1.10.0       Biobase_2.58.0             
#>  [3] sass_0.4.2                  jsonlite_1.8.3             
#>  [5] bslib_0.4.0                 Rdpack_2.4                 
#>  [7] assertthat_0.2.1            BiocManager_1.30.19        
#>  [9] stats4_4.2.1                latticeExtra_0.6-30        
#> [11] GenomeInfoDbData_1.2.9      Rsamtools_2.14.0           
#> [13] yaml_2.3.6                  Ckmeans.1d.dp_4.3.4        
#> [15] pillar_1.8.1                lattice_0.20-45            
#> [17] glue_1.6.2                  digest_0.6.30              
#> [19] GenomicRanges_1.50.0        RColorBrewer_1.1-3         
#> [21] XVector_0.38.0              rbibutils_2.2.9            
#> [23] colorspace_2.0-3            plyr_1.8.7                 
#> [25] htmltools_0.5.3             Matrix_1.5-1               
#> [27] pkgconfig_2.0.3             ShortRead_1.56.0           
#> [29] bookdown_0.29               zlibbioc_1.44.0            
#> [31] scales_1.2.1                jpeg_0.1-9                 
#> [33] BiocParallel_1.32.0         tibble_3.1.8               
#> [35] generics_0.1.3              IRanges_2.32.0             
#> [37] cachem_1.0.6                withr_2.5.0                
#> [39] SummarizedExperiment_1.28.0 BiocGenerics_0.44.0        
#> [41] cli_3.4.1                   magrittr_2.0.3             
#> [43] crayon_1.5.2                deldir_1.0-6               
#> [45] evaluate_0.17               fansi_1.0.3                
#> [47] hwriter_1.3.2.1             tools_4.2.1                
#> [49] lifecycle_1.0.3             matrixStats_0.62.0         
#> [51] stringr_1.4.1               interp_1.1-3               
#> [53] S4Vectors_0.36.0            munsell_0.5.0              
#> [55] DelayedArray_0.24.0         Biostrings_2.66.0          
#> [57] compiler_4.2.1              jquerylib_0.1.4            
#> [59] GenomeInfoDb_1.34.0         rlang_1.0.6                
#> [61] grid_4.2.1                  RCurl_1.98-1.9             
#> [63] bitops_1.0-7                rmarkdown_2.17             
#> [65] egg_0.4.5                   gtable_0.3.1               
#> [67] codetools_0.2-18            DBI_1.1.3                  
#> [69] R6_2.5.1                    gridExtra_2.3              
#> [71] GenomicAlignments_1.34.0    knitr_1.40                 
#> [73] dplyr_1.0.10                fastmap_1.1.0              
#> [75] utf8_1.2.2                  stringi_1.7.8              
#> [77] parallel_4.2.1              Rcpp_1.0.9                 
#> [79] vctrs_0.5.0                 png_0.1-7                  
#> [81] tidyselect_1.2.0            xfun_0.34