edit-sam: edit and analyze SAM/BAM/CRAM files
A high-performance utility for executing arbitrary Python logic on SAM/BAM/CRAM alignment streams. It enables in-flight header modification, tag injection, filtering, and sidecar metadata extraction without breaking unix pipes.
Key Features
- Hot-Injected Logic: Execute multiple Python statements on every pysam.AlignedSegment object.
- Persistent Namespace: Variables created in one alignment iteration persist to the next, allowing for stateful counters or cross-read comparisons.
- Header Provenance: Automatically appends a @PG line to the SAM header containing the exact command-line execution string for reproducibility.
- Zero-Copy Sidecars: Export specific Python variables to a JSON Lines (JSONL) file for downstream analysis without re-parsing the BAM.
- Performance Optimized: Native support for htslib multi-threading and uncompressed BAM streaming (-u) for high-speed piping.
Gemini said BAM Stream Processor (stream.py) A high-performance utility for executing arbitrary Python logic on SAM/BAM/CRAM alignment streams. It enables in-flight header modification, tag injection, filtering, and sidecar metadata extraction without breaking unix pipes.
Key Features Hot-Injected Logic: Execute multiple Python statements on every pysam.AlignedSegment object.
Persistent Namespace: Variables created in one alignment iteration persist to the next, allowing for stateful counters or cross-read comparisons.
Header Provenance: Automatically appends a @PG line to the SAM header containing the exact command-line execution string for reproducibility.
Zero-Copy Sidecars: Export specific Python variables to a JSON Lines (JSONL) file for downstream analysis without re-parsing the BAM.
Performance Optimized: Native support for htslib multi-threading and uncompressed BAM streaming (-u) for high-speed piping.
Installation
pip install pysam click edit-sam
Usage Syntax
edit-sam [OPTIONS] "COMMAND_1" "COMMAND_2" ...
Core Options
- -i, --input: Path to input (defaults to stdin).
- -o, --output: Path to output (defaults to stdout).
- -O, --output-format: Force SAM, BAM, or CRAM.
- -u, --uncompressed: Disable compression for faster piping.
- -l, --locals-out: Path to write exported variables as JSONL.
- -k, --export-key: Variable names to include in the JSONL output.
Examples
1. Composite Tag Injection
Concatenate CB and UB tags into a single XZ tag for multi-factor deduplication.
edit-sam -i in.bam -o out.bam \
"cb = seg.get_tag('CB') if seg.has_tag('CB') else ''" \
"ub = seg.get_tag('UB') if seg.has_tag('UB') else ''" \
"seg.set_tag('XZ', f'{cb}-{ub}') if cb and ub else None"
2. QC Metrics Extraction
Extract mapping quality to a sidecar file.
edit-sam -i in.bam -l mq_report.json -k all_mqs \
"if 'all_mqs' not in locals(): all_mqs = []" \
"all_mqs.append(seg.mapping_quality)" \
3. Regex Read Filtering
Use the built-in re module to modify flags based on read-name patterns.
edit-sam -i in.bam -o - -u \
"if re.search(r'^[A-Z]00', seg.query_name): seg.is_qcfail = True"
Metadata
Release files for edit-sam 0.1.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 | Actions |
|---|---|---|---|---|
| edit_sam-0.1.0.tar.gz | 3.7 kB | Without provenance | Details for edit_sam-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| edit_sam-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for edit_sam-0.1.0-py3-none-any.whl |
Total release size: 8.3 kB
Release files / edit_sam-0.1.0.tar.gz
| Download URL | edit_sam-0.1.0.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7d678af204a93a4208615fb4cb132fe42752f22976b20f04dfddfc4c2b3d11b8
|
|
BLAKE2b-256 checksum How to use checksums |
a818bc97e71903ee7d88487c753d325c217d4c6f0dac64863edd91c97acbfd97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Release files / edit_sam-0.1.0-py3-none-any.whl
| Download URL | edit_sam-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
78ba04fcd488673e4c388297a7632537354c8c3bfc019e8bdee6c2ca90cd4112
|
|
BLAKE2b-256 checksum How to use checksums |
96b543834f48f2c25356d909202b7aa9d274b538f027591dda7c655afc716ac8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Security for edit-sam 0.1.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.