bwamem
Python bindings for the BWA-MEM aligner.
Installation
pip install bwamem
Usage
Build Index
from bwamem import BwaIndexer
indexer = BwaIndexer()
index_path = indexer.build_index('reference.fa')
Single-End Alignment
from bwamem import BwaAligner
aligner = BwaAligner('path/to/index')
alignments = aligner.align('ACGATCGCGATCGA')
for aln in alignments:
print(f'{aln.ctg}:{aln.r_st} strand={aln.strand} mapq={aln.mapq}')
Paired-End Alignment
read1 = 'ACGATCGCGATCGA'
read2 = 'TTCGATCGATCGAT'
paired_alignments = aligner.align(read1, read2)
for pe_aln in paired_alignments:
print(f'Insert size: {pe_aln.insert_size}, Proper pair: {pe_aln.is_proper_pair}')
Retrieve Sequences from Index
# Get full sequence
seq = aligner.seq('chr1')
# Get subsequence
subseq = aligner.seq('chr1', start=100, end=200)
Monitor Index Building Progress
from bwamem import BwaIndexer
# Progress messages are captured by default (no console spam)
indexer = BwaIndexer(capture_progress=True)
index_path = indexer.build_index('genome.fasta')
# Check progress info
progress = indexer.get_progress()
print(f"Status: {progress['status']}")
print(f"Iterations: {progress['iterations']}")
print(f"Characters processed: {progress['characters_processed']}")
# Get progress percentage (if available)
if indexer.progress_percent:
print(f"Progress: {indexer.progress_percent:.1f}%")
# Access all captured messages
for msg in progress['messages']:
print(msg)
Control Index Building Verbosity
from bwamem import BwaIndexer
# Verbosity levels:
# 0 = silent (no output)
# 1 = quiet (only warnings/errors) - default
# 2 = normal (standard BWA messages)
# 3+ = debug (verbose output)
# Silent mode
indexer = BwaIndexer(verbose=0)
indexer.build_index('genome.fasta')
# Normal mode with progress messages shown in console
indexer = BwaIndexer(verbose=2, capture_progress=False)
indexer.build_index('genome.fasta')
# Debug mode with captured progress
indexer = BwaIndexer(verbose=3, capture_progress=True)
indexer.build_index('genome.fasta')
# Custom algorithm and block size
indexer = BwaIndexer(algorithm='bwtsw', block_size=50000000)
indexer.build_index('genome.fasta')
Read FASTA/FASTQ Files
from bwamem import fastx_read, read_paired_fastx
# Single-end (supports both FASTA and FASTQ)
for read in fastx_read('sequences.fasta.gz'):
print(f'{read.name}: {read.sequence}')
# Paired-end
for read1, read2 in read_paired_fastx('R1.fastq', 'R2.fastq'):
print(f'{read1.name}, {read2.name}')
Custom Options
# Specify alignment parameters
aligner = BwaAligner('path/to/index', options='-x ont2d -A 1 -B 0')
# Set custom insert size for paired-end reads
aligner = BwaAligner('path/to/index', insert_model=(500, 50))
paired_alignments = aligner.align(read1, read2)
Command-line tool
bwamem map runs BWA-MEM against one or more reference layers and emits a
SAM stream annotated with a HI:Z:<layer> tag used for hierarchical /
contamination-aware assignment:
# Single-end, hierarchical (contamination -> genes -> transcripts)
bwamem map -i contamination -i genes -i tx_transcript \
-k 18,14,10 -n 0.05,0.15,0.30 -T 30,30,30 reads.fq
# Paired-end against a single reference
bwamem map -i ref -1 r1.fq.gz -2 r2.fq.gz -o out.sam
HI:Z:<i> records which layer claimed each read (0-based); reads that map
to no layer get HI:Z:-1 (these are the candidates forwarded to the genome
stage). Each layer's parameters (-k, -n, -T) may be given as a single
value applied to all layers or a comma-separated list, one per layer.
Parallelism (-t / --threads)
Alignment is parallelized across processes (fork, COW-shared index), not threads, so the C alignment runs concurrently without holding the GIL:
bwamem map -t 8 -i ref -1 r1.fq.gz -2 r2.fq.gz
Single-threaded output is byte-identical to the parallel output. Because input parsing is streamed lazily through a bounded buffer, memory stays bounded regardless of input size.
-t 1 (the default) keeps the historical single-process code path unchanged.
SE primary-hit determinism
For single-end reads that have perfectly tied, equal-scoring alignments
(identical score / mapq / CIGAR / layer), BWA's mem_align1 does not mark a
deterministic primary (the SE path — unlike the paired-end path — never calls
mem_mark_primary_se). As a result, the exact repeat-scaffold coordinate
reported for such reads can depend on process-internal state, and may differ
between runs or between the process path and fork workers.
This affects only the reported repeat position of a tiny fraction of
multi-mapper reads: the HI:Z:<layer> assignment, mapped/unmapped status,
score, mapq and CIGAR are all unaffected. Pipelines that consume the SAM by
HI:Z layer (e.g. the small_RNA genome-vs-not decision) are therefore
unaffected. This is pre-existing BWA behavior, not introduced by the
parallel path.
Alignment Attributes
Each Alignment object contains the following attributes:
| Attribute | Description |
|---|---|
ctg |
Contig/reference name |
r_st |
Reference start position (0-based) |
r_en |
Reference end position (property) |
strand |
Strand: +1 for forward, -1 for reverse |
q_st, q_en |
Query start/end positions |
mapq |
Mapping quality score |
cigar |
CIGAR as list of [length, op] pairs |
cigar_str |
CIGAR string (property) |
NM |
Edit distance |
score |
Alignment score |
is_primary |
Primary alignment flag |
Calculated properties (computed on demand): r_en, cigar_str, blen, mlen
CIGAR operations: 0=M (match), 1=I (insertion), 2=D (deletion), 3=N (skip), 4=S (soft-clip), 5=H (hard-clip)
PairedAlignment contains: read1, read2 (Alignment objects), is_proper_pair (bool), insert_size (int or None)
License
- Python bindings: Mozilla Public License 2.0
- BWA: GNU General Public License v3.0
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bwamem-0.0.56.tar.gz.
File metadata
- Download URL: bwamem-0.0.56.tar.gz
- Upload date:
- Size: 1.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e73498615f5a0745758e102dda8e8de72fc415bc19d5c7b5c00b9f3f12f70c22
|
|
| MD5 |
6e9c184bd783363596c78e5d4815543f
|
|
| BLAKE2b-256 |
c0a326adf28f4a193d233357940c1709f06beb477b0efd6431bc5e2574f8e583
|
Provenance
The following attestation bundles were made for bwamem-0.0.56.tar.gz:
Publisher:
publish.yml on y9c/bwamem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bwamem-0.0.56.tar.gz -
Subject digest:
e73498615f5a0745758e102dda8e8de72fc415bc19d5c7b5c00b9f3f12f70c22 - Sigstore transparency entry: 2568145595
- Sigstore integration time:
-
Permalink:
y9c/bwamem@81994fd9858b190fe24965f61ab8db882b858147 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/y9c
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@81994fd9858b190fe24965f61ab8db882b858147 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bwamem-0.0.56-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: bwamem-0.0.56-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 386.1 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac6e777cc40ae9a3581e8e53e5a483c3af01d92ead66352996fd0027a71f64ec
|
|
| MD5 |
892d843984910b6d90fa67fe8f8969ee
|
|
| BLAKE2b-256 |
f5b60ab28931781cd49e1c770a730a2def0b2f80e9380e87962407965ed3dc06
|
Provenance
The following attestation bundles were made for bwamem-0.0.56-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
publish.yml on y9c/bwamem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bwamem-0.0.56-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
ac6e777cc40ae9a3581e8e53e5a483c3af01d92ead66352996fd0027a71f64ec - Sigstore transparency entry: 2568145641
- Sigstore integration time:
-
Permalink:
y9c/bwamem@81994fd9858b190fe24965f61ab8db882b858147 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/y9c
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@81994fd9858b190fe24965f61ab8db882b858147 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bwamem-0.0.56-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: bwamem-0.0.56-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 386.1 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a6a017c2ff95877d339d9a51d4b6f97d58074fd13a2ddee8483e516a07629ab
|
|
| MD5 |
d0ade2c494f224b511946ad9e9774319
|
|
| BLAKE2b-256 |
95d98647f3c7a37fe8f34f3068ad800470022805521a5fa7a9c47cf653010d9f
|
Provenance
The following attestation bundles were made for bwamem-0.0.56-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
publish.yml on y9c/bwamem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bwamem-0.0.56-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
2a6a017c2ff95877d339d9a51d4b6f97d58074fd13a2ddee8483e516a07629ab - Sigstore transparency entry: 2568145634
- Sigstore integration time:
-
Permalink:
y9c/bwamem@81994fd9858b190fe24965f61ab8db882b858147 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/y9c
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@81994fd9858b190fe24965f61ab8db882b858147 -
Trigger Event:
push
-
Statement type: