Convert SAM/BAM alignment files into a uniform tabular format — CSV, TSV, PSV, Parquet, or any custom delimiter — with optional SAM tags promoted to their own sorted columns.
- Reads SAM (text) and BAM (binary/BGZF) from a file or standard input.
- Auto-detects format from magic bytes (
--input-formatto override). - Outputs CSV (default), TSV, PSV, Parquet, or a custom delimiter.
- Optional tags (
NM,RG,MD, …) become their own columns, sorted alphabetically. - Two-phase discover-then-stream; tag discovery is capped at
min(--detect-limit, --limit)so memory stays bounded. - Parquet output is typed:
FLAG/POS/MAPQ/PNEXT/TLENasint32, text and tags asutf8, missing tags asNULL. - Downsample:
--downsample 0.1keeps ~10% of records (Bernoulli);--downsample 1000keeps exactly 1000 uniformly random records (reservoir sampling).--seedmakes a sample reproducible.
conda install -c conda-forge -c bioconda sam-formatter
mamba install -c conda-forge -c bioconda sam-formatter # faster solver
pixi global install sam-formatter # pixicargo install sam-formatterRequires Rust ≥ 1.89 (noodles).
git clone https://github.com/jiangyun-fun/sam-formatter.git
cd sam-formatter
cargo install --path .
# or: cargo build --release # binary at target/release/sam-formatterPrebuilt images auto-built from the Bioconda package (no Rust toolchain needed):
# Docker
docker run --rm -v "$PWD:$PWD" -w "$PWD" \
quay.io/biocontainers/sam-formatter:1.2.0--hab7d0fd_0 sam-formatter --help
# Podman (drop-in for Docker)
podman run --rm -v "$PWD:$PWD" -w "$PWD" \
quay.io/biocontainers/sam-formatter:1.2.0--hab7d0fd_0 sam-formatter --help
# Singularity / Apptainer
singularity exec docker://quay.io/biocontainers/sam-formatter:1.2.0--hab7d0fd_0 sam-formatter --helpSee all tags at quay.io/biocontainers/sam-formatter.
sam-formatter in.sam # SAM file -> CSV on stdout
sam-formatter in.bam # BAM file -> CSV (auto-detected)
cat in.sam | sam-formatter - # SAM from stdin
samtools view -b in.bam | sam-formatter - # BAM from stdinOutput format:
sam-formatter in.bam -o out.tsv # extension -> TSV
sam-formatter in.bam -o out.parquet # Parquet
sam-formatter in.bam -f psv # pipe-separated
sam-formatter in.bam -f custom -d ';' # any custom delimiterTags:
sam-formatter in.sam # NM:i:1 -> "1" (value only)
sam-formatter in.sam --keep-tag-prefix # NM:i:1 -> "NM:i:1" (full tag)Downsampling:
sam-formatter in.bam --downsample 0.1 # keep ~10% of records (Bernoulli)
sam-formatter in.bam --downsample 1000 # keep exactly 1000 random records (reservoir)
sam-formatter in.bam --downsample 1000 --seed 7 # reproducible sampleA fraction in (0, 1) keeps each record independently (≈ that share of the input); an
integer ≥ 1 keeps exactly that many records via reservoir sampling (held in memory; the
whole stream is read before any output). --limit caps the input before sampling.
| Flag | Default | Purpose |
|---|---|---|
[INPUT] |
- |
SAM/BAM file, or - for standard input |
-o, --output |
stdout | output file (extension picks format) |
-f, --format |
auto | csv|tsv|psv|parquet|custom |
--input-format |
auto |
auto|sam|bam |
-d, --delimiter |
, |
delimiter for custom |
-n, --limit |
1000000 |
max records read from input (capped before sampling) |
--detect-limit |
100000 |
records scanned to discover tags |
--keep-tag-prefix |
off | keep TAG:TYPE:VALUE instead of value only |
--no-quotes |
off | disable CSV quoting |
--downsample |
off | fraction (0,1) or integer ≥1; downsample output |
--seed |
off | u64 seed for a reproducible --downsample |
- When piping SAM text, include the header (
samtools view -h): records whoseRNAMEis not declared in an@SQline are rejected. BAM carries its header internally, so no special handling is needed. - A gzip-compressed SAM is not read directly (auto-detect misreads it as BAM, and the
tool does not decompress). Decompress first:
zcat in.sam.gz | sam-formatter. - A mate reference equal to
RNAMEis written as=(matchessamtools view).
Parsing uses noodles for both SAM and BAM via the
shared sam::alignment::Record trait. Each record is serialized to a canonical SAM line
with noodles' own writer and then split into columns — so CIGAR, SEQ/QUAL, and typed tag
formatting are always spec-correct, identically for SAM and BAM.
Licensed under the MIT License.