# BAM to gene expression matrix (UMI counts per gene per cell),10X

I am trying to reproduce some results of a scRNASeq experiment. However I am new to the server-side aspect of such analyses and am very confused at the moment.

The data provided by the authors of the paper is in .BAM format and from there I wish to derive a gene expression matrix (UMI counts per gene per cell). The authors stated that they did this using the 10X genomics pipeline, however it seems that .BAM outputs are sort of the "final version" of the pipeline and, the pipeline only really deals with truly raw sequencing files (bcl/fastq).

I have considered converting it back to fastq format and following the pipeline from the beginning, but it just seems like I would be moving backwards by doing such.

I have also found some R packages that can integrate BAM files in the environment like Rsamtools, but I would much rather do this on the server and then upload a csv into R for the downstream analysis as the files are +60GB each.

Essentially I am just asking for some advice, or a vignette/article I can read that would clear up how to proceed with the BAM outputs to derive the gene expression data. I have already read the cellranger pages on the 10X genomics website but they were of no help in answering this quesion.

• You might just email the authors. They just uploaded the BAM files because that's the easiest thing to do. CellRanger produces 3 files that describe the counts (they're in "matrix market format") and presumably they can just email them to you. Feb 22 '19 at 7:44
• Thanks Devon for your reply. Thing is though, I am trying to learn how to do all this processing myself as I will need to eventually. Unfortunately, most of the documents I have read end up making me more confused. I know BAM files are technically already processed and aligned but, how would I get the counts from the file? Feb 25 '19 at 15:11
• If you're going to use cell ranger anyway then this isn't something you have to bother learning how to do, it's done by cell ranger internally. Feb 25 '19 at 15:14
• If you accept using other tools than those that the authors mention, one that can get you from a bam file to counts is featureCounts (bioinf.wehi.edu.au/featureCounts). You would also need an annotation file in gtf format.
– bli
Feb 25 '19 at 15:25
• @DevonRyan thanks for clearing that up, I get what you are saying now as well as your earlier comment. I will probably just contact the authors directly. Feb 25 '19 at 15:43

You should be able to parse out what you need using the tags in the .bam. 10xGenomics' website says what tags they add.

https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/output/bam

Going backwards would also involve parsing the tags, because you have to make two fastq files, and a simple bam -> fastq pipeline won't do that correctly.

• Could you be more specific please? I'm very new to this procedure and bash scripting in general. Feb 21 '19 at 22:13
• Yes, 10xGenomics single cell bams have the genes called in the bam tags. support.10xgenomics.com/single-cell-gene-expression/software/… And no, samtools fastq will not make paired end fastqs suitable to rerun through cellranger, because the read data from the read containing the cell barcode and UMI will not have its own line in the bams. Feb 25 '19 at 16:45
• @swbarnes2 There are indeed many tags, but isn't the UMI tag different than UMI counts? So how would I derive the expression matrix from these said tags? Thank you btw for your answer! Feb 25 '19 at 18:50
• Each read has its UMI as a bam tag. You can go through the bam line by line and figure out which reads were assigned to what cell barcode, UMI and gene. Feb 25 '19 at 20:18
• This reverse process is also complicated by the fact that error-correction is performed on the barcodes and UMIs as well. For scRNA-seq, you typically start with 4 FASTQs: UMIs, barcodes, R1, and R2. There will likely be errors in those sequences, so barcodes/UMIs are corrected for by comparing to a whitelist of known input barcodes. You can take a look at the "Feature Barcoding Tags" section of the 10x support page @swbarnes2 listed to see what tags to look for. As mentioned, it's not a simple BAM -> FASTQ operation, but you should be able to do it Sep 27 '19 at 14:00

### How to re-analyze 10X BAM files?

This is a great question and honestly, I don't think there was an easy way to do this at the time the question was asked. The reason is that if you want to re-do the authors' analysis to get the gene expression (gene-cell / feature-barcode) matrix and they used cellranger (the official 10X pipeline software) to do that, you will need FASTQ files as input - because "10x pipelines require sequencer FASTQs (with embedded barcodes) as input."

Therefore, to get FASTQ files from 10X BAM files, there's a relatively new tool provided by 10X Genomics: bamtofastq. Then you can simply use those FASTQ files as input to cellranger.

The only caveat is that you need a BAM file generated directly by 10X's cellranger (or the respective 10X pipeline, if not dealing with gene expression) - that means that a BAM file obtained by downloading an SRA from NCBI and converting to BAM won't work; you need to get the original BAM file directly (often found among the originally submitted files, under "Original format").

To give you some additional help here (because figuring this out is also not trivial): an original 10X BAM file (if originally submitted in this format) can be downloaded from NCBI using prefetch from sra-tools, specifying the --type option - for example:

prefetch --type TenX --max-size 100000000 SRR5167880


Because the file was larger than the default limit of 20GB, I also had to increase it by specifying the --max-size option.

Alternatively, you can follow the official 10X Instructions to Download and Process BAM files of 1.3 Million Brain Cells.

I know this is an old thread but I'm hoping my answer will be helpful to anyone who has the same question (like me).

• This is the simplest approach (if the process’s data is not available on GEO). Computing the UMI count matrix for 10x Genomics data is already implemented in Cell Ranger and if the same version is used it should reproduce the results of the study. Mar 12 '21 at 15:14