Bamurai
A Python toolkit for manipulating BAM and FASTQ files, designed to split reads into smaller fragments, extract statistics, validate files, and manage multi-sample data.
For BAM/SAM/CRAM files, Bamurai only processes primary alignments. Secondary and supplementary alignments are ignored in all commands. This approach ensures that each original read from sequencing is counted only once. Secondary and supplementary alignments represent alternative mappings or split alignments of the same read, not additional unique reads. Including them would artificially inflate read counts and statistics, leading to misleading results.
Description
Bamurai is a command-line tool for splitting reads in BAM/FASTQ files into smaller fragments. It is designed to be fast and efficient, and can be used to split reads into a target length or a target number of pieces per read.
These are the current features of Bamurai:
- Splitting reads in a file to a target length (
split) - Splitting reads in a file to a target number of pieces per read (
divide) - Getting statistics from a BAM or FASTQ(.gz) file (
stats) - Basic validation of BAM and FASTQ(.gz) files (
validate) - Splitting a file into size-based chunks (
chunk) - Splitting or extracting reads by donor ID using a barcode mapping
(
split_samples,extract_sample,assign_samples) - Extracting hashtag oligo (HTO) information from 10x FASTQ pairs (
get_hto) - Tagging BAM reads with arbitrary values from a TSV keyed by read ID (
add_tags)
The split command splits reads into a target length, each read will be split into fragments as close to the target length as possible. Reads shorter than the target length will not be split.
The divide command splits reads into a target number of pieces (default 2), each read will be split into the number of pieces specified. A further minimum length can be specified to ensure that reads are not split if the resultant fragments are less than the minimum length (default 100).
The stats command will output the following information by default:
Statistics for input.bam:
Total reads: 8160
Average read length: 30638
Throughput (Gb): 0.25
N50: 82547
It can be used with the --tsv argument to output the statistics in a tab-separated format for computational analysis. Note that throughput is reported in raw bases here, rather than the gigabases of the default output.
file_name total_reads avg_read_len throughput n50
input.bam 8160 30638 250006998 82547
The validate command will check the integrity of a BAM or FASTQ(.gz) file and output the following information if the file is valid.:
input.bam is a valid BAM file with 8160 records.
Unlike the read-processing commands, validate inspects every record in the file, so the count it reports includes any secondary and supplementary alignments.
Installation
To install the released version of Bamurai from PyPI
pip install bamurai
To install the latest version of Bamurai from GitHub
pip install git+https://github.com/Shians/Bamurai.git
Usage
To get help on the command-line interface and list available commands
bamurai --help
To get help on a specific command
bamurai <command> --help
Splitting reads to target size
To split a file into 10,000 bp reads
bamurai split input.bam --len-target 10000 --output output.fastq
To create a gzipped output file
bamurai split input.bam --len-target 10000 | gzip > output.fastq.gz
Dividing reads into a target number of pieces
To divide reads into 2 pieces
bamurai divide input.bam --num-fragments 2 --output output.fastq
To divide reads into 2 pieces unless resultant fragments are less than 1000 bp
bamurai divide input.bam --num-fragments 2 --min-length 1000 --output output.fastq
Getting statistics from a BAM or FASTQ file
To get stats from a BAM file
bamurai stats input.bam
To get stats from a FASTQ file or Gzipped FASTQ file
bamurai stats input.fastq
bamurai stats input.fastq.gz
Validating BAM or FASTQ files
To validate a BAM file
bamurai validate input.bam
Splitting a file into chunks
To split a file into chunks of at least 1 GB each
bamurai chunk input.bam --size 1G
Sizes are given in human-readable notation (1G, 100M, 1000K). Each chunk is at least the requested size, and may be slightly larger because reads are never split across chunks.
Output files are written as chunk_1.fastq, chunk_2.fastq, and so on. Use --prefix to change the name
bamurai chunk input.bam --size 100M --prefix sample_a
Working with multi-sample BAM files
Bamurai provides commands for processing BAM files with multiple samples based on barcode information.
Splitting BAM or FASTQ files by donor ID
To split a BAM or FASTQ file into multiple files, one for each donor ID:
bamurai split_samples --input input.bam --tsv barcode_to_donor.tsv --output-dir donor_bams
bamurai split_samples --input input.fastq.gz --tsv barcode_to_donor.tsv --output-dir donor_fastqs
The TSV file should contain a barcode column and a donor ID column, one row per barcode; see Barcode and donor columns below for how those columns are found.
You can process multiple BAM or FASTQ files at once:
bamurai split_samples --input input1.bam input2.bam --tsv barcode_to_donor.tsv --output-dir donor_bams
bamurai split_samples --input input1.fastq.gz input2.fastq.gz --tsv barcode_to_donor.tsv --output-dir donor_fastqs
Extracting reads for a specific donor
To extract all reads belonging to a specific donor from a BAM file:
bamurai extract_sample --bam input.bam --tsv barcode_to_donor.tsv --donor-id donor1 --output donor1.bam
You can also process multiple BAM files at once, combining all donor-specific reads into a single output file:
bamurai extract_sample --bam input1.bam input2.bam input3.bam --tsv barcode_to_donor.tsv --donor-id donor1 --output donor1.bam
This command will extract all reads with barcodes belonging to the specified donor ID and write them to a new BAM file.
Tagging reads with their donor ID
The assign_samples command labels each read in a BAM with the donor it came from, using the RG (read group) tag. Unlike split_samples, which writes one file per donor, this keeps every read in a single BAM so that downstream tools can group by read group.
bamurai assign_samples --bam input.bam --tsv barcode_to_donor.tsv --output assigned.bam
This writes assigned.bam containing every read from the input, where:
- reads whose cell barcode maps to a donor carry an
RGtag naming that donor - reads with an unknown or absent barcode are written through untagged
- the header gains an
@RGrecord per donor, withIDandSMboth set to the donor ID
A summary of how many reads were assigned to each donor is printed on completion. Note that --bam takes a single BAM, unlike split_samples and extract_sample, which accept several.
Barcode and donor columns
split_samples, extract_sample and assign_samples all read the same barcode-to-donor TSV, and all detect its columns the same way. The barcode column is taken from a column named barcode or cell, and the donor column from one named donor_id. If both barcode and cell are present the file is ambiguous and the command fails, asking you to choose.
Use --barcode-column and --donor-id-column when your columns are named something else, or to resolve that ambiguity:
bamurai extract_sample --bam input.bam --tsv mapping.tsv --donor-id donor1 \
--barcode-column cell_barcode --donor-id-column sample --output donor1.bam
Which barcode tag is read
For BAM input, a read's cell barcode is read from the first of these tags that is present:
| Tag | Meaning |
|---|---|
CB |
Cell barcode, error-corrected (preferred) |
CR |
Cell barcode, raw/uncorrected |
XC |
Cell barcode, Drop-seq and early 10x convention (not in the SAM spec) |
BC and RX are deliberately not consulted: per the SAM specification BC identifies a sample and RX holds the UMI, so neither names the cell that a donor is assigned to. A read carrying only those tags is treated as having no barcode.
Extracting HTO information
The get_hto command pulls hashtag oligo (HTO) information out of a pair of 10x FASTQ files. It assumes R1 holds the cell barcode followed by the UMI, and R2 holds the hashtag sequence.
bamurai get_hto --r1 sample_R1.fastq.gz --r2 sample_R2.fastq.gz \
--bc-len 16 --umi-len 12 --output htos.tsv
--bc-len and --umi-len give the barcode and UMI lengths in R1, and are required. The output is a tab-separated file with the columns read_name, cell_barcode, umi, hto, bc_qual, umi_qual and hto_qual, where the three _qual columns carry the FASTQ quality strings for the preceding sequences.
Where in R2 the hashtag is read from can be adjusted with --hashtag-len (how many bases to take, default 15) and --hashtag-left-buffer (how many bases to skip first, default 10).
Tagging reads from a TSV
To tag reads in a BAM file with values from a TSV, keyed by read_id:
bamurai add_tags --bam input.bam --tsv tags.tsv --output tagged.bam
The TSV must have a header row with a read_id column plus one column per
tag, where each tag column name is the 2-letter BAM tag to apply (e.g., XX,
RG) — every column besides read_id is applied as a tag. Tag values are
auto-typed as int, float, or string. Reads not found in the TSV, or with a
blank cell for a given tag column, are left untagged for that tag. Only
primary alignments are tagged.
Metadata
Release files for bamurai 0.13.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| bamurai-0.13.0.tar.gz | 40.3 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bamurai-0.13.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 71.5 kB
Release files / bamurai-0.13.0.tar.gz
| Download URL | bamurai-0.13.0.tar.gz |
|---|---|
| Size | 40.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fc3b764550a7e0eed4e7910d4cbeecb5af3ab45473ace60a5f7a6ee0169035c0
|
|
BLAKE2b-256 checksum How to use checksums |
95907a88429a7217c4049df1a3e44cbaa0c0d976bbef2f493f6e4e4ef93a324f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 21, 2026.
Transparency logRelease files / bamurai-0.13.0-py3-none-any.whl
| Download URL | bamurai-0.13.0-py3-none-any.whl |
|---|---|
| Size | 31.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
917873ad2eddf0c8d282d73c635a386f63c493bf477a02eb82963abeae4e77b6
|
|
BLAKE2b-256 checksum How to use checksums |
9711f8ed845c57b5d1f44012481122795c581a80390852c0fa6a5c7df7017c4e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 21, 2026.
Transparency logSecurity for bamurai 0.13.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for bamurai-0.13.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 21, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.