# Converting 10x BAM Files to FASTQ

bamtofastq is a tool for converting 10x BAMs produced by cellranger, cellranger-atac, cellranger-dna or longranger back to FASTQ files that can be used as inputs to re-run analysis. The FASTQs will be emitted into a directory structure that is compatible with the directories created by the mkfastq tool.

NOTE: only BAMs produced by cellranger, cellranger-atac, cellrange-dna and longranger will work with bamtofastq. Special tags included by 10x pipelines are required to reconstruct the original FASTQ sequences correctly. If your BAM file lacks the appropriate headers, you will get an error message.

## Background

We created bamtofastq to helps users who want to reanalyze 10x data and only have access to 10x BAM files. (e.g. Some customers want to store BAM files only. Others might have downloaded our BAM data from NCBI SRA). 10x pipelines require sequencer FASTQs (with embedded barcodes) as input. The location of the 10x barcode varies depending on product and reagent version. For current version Genome (v2) and Single Cell 3' (v2, v3) products, the 10x barcode is found on the first 16 bases of the R1 read. In earlier product versions, the 10x barcode was attached on the sample indices. bamtofastq determines the appropriate way to construct the original read sequence from the sequences and tags in the BAM file.

bamtofastq is available for Linux and is compatible with RedHat/CentOS 5.2 or later, and Ubuntu 8.04 or later.

We recommend upgrading to bamtofastq 1.2.0—it is now multi-threaded and is compatible with Cell Ranger ATAC BAM files.

bamtofastq is a single executable that can be run directly and requires no compilation or installation. Place the executable file in a directory that is on your PATH, and make sure to chmod 700 to make it executable.

## Running the Tool

10x BAMs produced by Cell Ranger v1.2+, Cell Ranger ATAC v1.0+, Cell Ranger DNA v1.0+, and Long Ranger v2.1+ contain header fields that permit automatic conversion to the correct FASTQ sequences. BAMs produced by older 10x pipelines may require special arguments or have some caveats, see below for details. Run times for full-coverage WGS BAMs may be several hours.

The FASTQ files emitted by bamtofastq contain the same set of sequences that were input to the original pipeline run, although the original order will not be preserved. 10x pipelines are generally insensitive to the order of the input data, so you can expect nearly identical results when re-running with bamtofastq outputs.

## Options

--locus=locus
Optional. Only include read pairs mapping to locus. Use chrom:start-end format.
Number of reads per FASTQ chunk. Default: 50000000
--gemcode
Convert a BAM produced from GemCode data (Longranger 1.0 - 1.3)
--lr20
Convert a BAM produced by Longranger 2.0
--cr11
Convert a BAM produced by Cell Ranger 1.0-1.1
--bx-list=L
Only include BX values listed in text file L. Requires BX-sorted and indexed BAM file (see Long Ranger support for details).
--help
-h
Show the help screen.

## Known Issues

The latest versions of cellranger, cellranger-atac, cellranger-dna and longranger generate BAM files that automatically reconstruct complete FASTQ files representing all input reads. BAMs produced by older versions of cellranger and longranger have some caveats, listed below:

PackageVersionPipelinesExtra ArgumentsComplete FASTQs
Cell Ranger1.3+countnoneYes
Cell Ranger1.2countnoneReads without a valid barcode will be absent from FASTQ. (These reads are ignored by Cell Ranger)
Cell Ranger1.0-1.1count--cr11 Reads without a valid barcode will be absent from FASTQ. (These reads are ignored by Cell Ranger)
Cell Ranger ATAC1.0.0+countnoneYes
Cell Ranger DNA1.0.0+cnvnoneYes
Long Ranger2.1.3+wgs, targeted, align, basicnoneYes
Long Ranger2.1.0 - 2.1.2wgs, targetednoneYes
Long Ranger2.0wgs, targeted--lr20Yes
Long Ranger2.0.0 - 2.1.2align, basicNot SupportedN/A
Long Ranger1.3 (GemCode)wgs, targeted--gemcodeReads without a valid barcode will be absent from FASTQ. This will result in a ~5-10% loss of coverage.