Skip to main content

pmdshodh

Fast, maintained post-mortem damage (PMD) scoring and filtering for ancient DNA alignments.

pmdshodh scores every read in a SAM, BAM or CRAM file for ancient DNA damage, keeps or removes reads by score, and summarises damage patterns. It uses the scoring model of PMDtools 0.60, fixes PMDtools' known errors by default (--pmdtools-compat reproduces PMDtools exactly), reads and writes alignments directly, and runs on multiple threads.

Status: alpha (v0.2.3). Scores have been validated against PMDtools on real and synthetic data. Performance benchmarks are in docs/benchmarks.md; a Bioconda package is still to come.

Contents

Overview

Ancient DNA molecules carry characteristic damage. Cytosine deamination appears as C→T substitutions near the 5′ end of reads, and as G→A near the 3′ end in double-stranded libraries. The PMD score of Skoglund et al. (2014) is a per-read log-likelihood ratio comparing a damage model with a no-damage model. Reads scoring above a threshold (commonly 3) are likely to be ancient rather than modern contamination.

pmdshodh implements this score as a single program, with no samtools calmd | python | samtools view pipeline. It does not classify taxa, estimate contamination, call genotypes or rescale base qualities.

Features

  • PMDtools-compatible scores. It uses the same likelihood model, defaults and options (--threshold, --ss, --CpG, --UDGhalf, -m, -q, …). Single-stranded (--ss) scores follow the paper's model and are lower than PMDtools'; see Differences that change scores.
  • Direct SAM/BAM/CRAM input, SAM/BAM output. Kept records are written unchanged and in input order, with an @PG header line. CRAM is decoded with the reference given by -r.
  • Multi-threaded scoring and BGZF compression. Results are identical for any thread count.
  • JSON run summary with read counts, a score histogram and damage tables, designed for MultiQC.
  • Robust input handling. It never drops reads silently: every skipped read is counted by reason, and invalid input stops with a clear error.
  • Correct by default, exact on request. Known PMDtools bugs are fixed, and --pmdtools-compat reproduces PMDtools exactly for comparisons.

Installation

pip: pip install pmdshodh installs the pmdshodh command; the wheels contain the compiled binary only. Wheels are built for Linux x86_64 and aarch64 (glibc 2.17 or newer), macOS arm64 and x86_64, and Windows x86_64.

Prebuilt binaries: download the archive for your platform from GitHub Releases, e.g. pmdshodh-0.2.3-x86_64-unknown-linux-gnu.tar.gz, and check it against SHA256SUMS. Linux binaries need glibc 2.17 or newer (CentOS/RHEL 7 and later). Builds are for Linux x86_64 and aarch64, macOS arm64 and x86_64, and Windows x86_64.

From source: this needs Rust 1.91 or newer, the minimum of the noodles dependency. CI tests 1.91 and the latest stable.

git clone https://github.com/dpatel511/pmdshodh.git
cd pmdshodh
cargo build --release
./target/release/pmdshodh --version

Input requirement: pmdshodh needs each read's reference bases. Either pass the reference with -r, or give it reads with an MD tag.

  • With -r ref.fa: the FASTA must be indexed with samtools faidx. Reference bases come from the FASTA, so reads need no MD tag. Any MD tags present are checked against the reference, and stale ones are counted.
  • CRAM input always needs -r, with the reference the CRAM was written against. Each reference sequence is checked against the M5 checksum in the CRAM header when it is loaded, so a different reference is an input error rather than silently wrong reads and scores. (samtools writes M5; without it, only single-reference slices are checked, against their own MD5.) REF_PATH, REF_CACHE and downloads are not used.
  • Without -r: every mapped read needs an MD tag. If your aligner doesn't write one, add it with samtools calmd -b input.bam reference.fa > input.md.bam.

Quick start

# Keep reads with PMD score >= 3 (double-stranded library, no UDG treatment)
pmdshodh filter --threshold 3 sample.bam -o sample.pmd3.bam

# Add the score to each kept read, write a run summary, use 8 threads
pmdshodh filter --threshold 3 --tag --summary sample.json --threads 8 sample.bam -o sample.pmd3.bam

# Single-stranded, UDG-half treated library
pmdshodh filter --threshold 3 --library ss --treatment half sample.bam -o sample.pmd3.bam

# Per-read scores as a table
pmdshodh score sample.bam -o sample.scores.tsv

# Damage summary only
pmdshodh damage sample.bam -o sample.damage.json

# CRAM input (needs its reference); writes BAM
pmdshodh filter --threshold 3 -r ref.fa sample.cram -o sample.pmd3.bam

Commands

Command Purpose Output
filter Keep reads by PMD score SAM or BAM, chosen by the -o extension (-o - writes BAM to stdout)
score Score every read Tab-separated table: read_name, flag, pmds, n_sites
damage Summarise damage without filtering JSON run summary

filter keeps reads with score ≥ --threshold and/or < --upper-threshold; at least one is required.

Run pmdshodh <command> --help for every option, grouped by purpose, with examples.

Options

PMDtools option names are accepted as aliases (shown in the second column).

Library and treatment

Option PMDtools Default Description
--library ds|ss --ss ds Double- or single-stranded library. Never inferred from the data.
--treatment none|half|full --UDGminus, --UDGhalf, --UDGplus / --CpG none half: CpG sites plus the terminal base; full: CpG sites only.

Read and base selection

Option PMDtools Default Description
-m, --min-mapq N -m, --requiremapq 0 Skip reads below this mapping quality.
-q, --min-baseq N -q, --requirebaseq 0 Ignore bases below this base quality.
--no-clips --noclips off Skip reads with soft or hard clips.
--no-indels --noindels off Skip reads with insertions or deletions.

Model parameters

Option PMDtools Default
--pmd-p --PMDpparam 0.3
--pmd-c --PMDconstant 0.01
--pi-ancient --polymorphism_ancient 0.001
--pi-contamination --polymorphism_contamination 0.001

Each is between 0 and 1, and --pmd-p + --pmd-c (the damage rate at the read end) must be at most 1; larger values would make scores undefined (NaN).

Output and performance

Option Applies to Description
-t, --threshold X filter Keep reads with score ≥ X.
--upper-threshold X filter Keep reads with score < X.
--tag, --tag-name pm|DS filter Add the score to kept reads as pm:f (32-bit float, default) or DS:Z (3 decimals, as PMDtools --writesamfield).
--summary FILE filter, score Also write the JSON run summary.
--range N all Positions from each read end in damage tables (default 30, from 1 to 1000).
-n, --max-reads N damage Stop after N reads.
-@, --threads N all Threads (default 1, at most 1024): at most N do work at once, so you can pass your CPU allocation.
-r, --reference FILE all Indexed reference FASTA (samtools faidx; bgzip also needs .gzi). Reference bases come from it instead of MD tags, and MD tags are checked against it. Every header sequence must be in it with the same length.
--pmdtools-compat all Reproduce PMDtools 0.60 exactly, including its bugs.
--quiet all Suppress the summary line on stderr.

Output

Alignments (filter)

  • Kept records are unchanged (identical when decoded) apart from the optional score tag. From CRAM input, records are written as stored in the CRAM: MD and NM tags that htslib left out because it can regenerate them are not added back (see Limitations).
  • Input order is preserved.
  • The input header is copied, and one @PG line records the command.
  • Output files are written under a temporary name and renamed only on success, so a failed run never leaves a partial file. A symlink is followed and the file it points to is replaced. FIFOs and devices (/dev/null, NUL on Windows, /dev/stdout, >(...)) are written in place.
  • The output format comes from a .bam or .sam extension (any case). For stdout and devices, which have none, --output-format decides, BAM by default: -o /dev/null --summary run.json keeps only the summary.
  • An output may not be the input, the reference or another output (exit 2).

JSON run summary

A single file per run, versioned with format_version (currently 2). It contains:

Section Contents
tool, command, mode, model, filters Version, command line, library and treatment, model parameters, thresholds
input_format sam, bam or cram, detected from the file contents
reference The -r FASTA (if any); whether reference bases came from MD or the FASTA; with -r, how many scored reads had MD tags agreeing, disagreeing or absent (for CRAM, only MD tags stored in the file count)
reads Input, scored, kept and removed counts; skipped reads by reason; flag counts (duplicate, secondary, supplementary, …)
score_histogram, length_histogram Score distribution (0.5-wide bins from −20 to 40), read lengths
damage Counts of read bases A/C/G/T at reference C and G, by position from each molecule end; first-base C→T rate with standard error

Exit status and streams

Exit status Meaning
0 Success, including input with only a header
1 Runtime failure, e.g. the output cannot be written
2 Invalid options or invalid input, e.g. a mapped read without an MD tag (the read is named in the message), or an input with no data at all
141 The reader of stdout closed it early (e.g. | head): pmdshodh stops without a message, as samtools does when SIGPIPE ends it

Results go to stdout only when requested (-o -, or score/damage without -o). The run summary line and errors go to stderr.

Compatibility with PMDtools

Validation

pmdshodh was built from the published model and black-box testing of PMDtools 0.60; PMDtools' source code was not used. Every read in each test set was compared:

Test set Reads Largest score difference
Single-site score terms (ds and ss, base quality 0–93, 6 parameter sets) 5,412 1.1 × 10⁻¹³
Synthetic reads with clips, indels and =/X CIGARs, both strands, 8 modes 1,500 per mode 8.3 × 10⁻¹⁴ ¹
nf-core/eager test BAMs (mammoth mtDNA, human 1240k capture), ds, --CpG, --UDGhalf, --ss 13,670 per mode 3.0 × 10⁻¹⁴ ¹
Real single-stranded library, not UDG-treated (ENA PRJEB37007, UKY001.B0102: mtDNA and 1240k capture), --ss 281,906 and 14,461,790 8.2 × 10⁻¹⁴ ¹
Damage tables vs pmdtools --deamination --range 30 (no UDG and full UDG) 1,500 identical at all 30 positions

¹ With --pmdtools-compat. Without it, scores differ only where PMDtools has a bug (below).

Methods and full results: docs/blackbox.md.

Differences that change scores

pmdshodh corrects these PMDtools behaviours by default. Use --pmdtools-compat to reproduce them.

PMDtools 0.60 behaviour Effect pmdshodh default
The reference is rebuilt wrongly for reads that combine an insertion or soft clip with a deletion or = CIGAR operations Deleted bases are scored against later read bases, and positions shift Reference rebuilt correctly from CIGAR and MD
CpG context wraps around the read ends in --CpG and --UDGhalf modes Changed 6% of read scores (up to 1.62) on the human UDG test BAM No wrap-around
--ss counts every C site twice (the double-stranded term plus the paper's single-stranded term, SI Eq. S1) ss scores are about twice as large as the model gives, and lopsided: a terminal C→T scores 9.68 at the 5′ end and 6.34 at the 3′ end (Q40), so a threshold of 3 is far looser than for ds libraries One term per C site (Eq. S1): 4.65 at either end. ss scores are lower than PMDtools'; re-check thresholds tuned on PMDtools ss scores
--upperthreshold keeps reads with no informative sites (score 0) Reads with no C/G evidence at all are output as "non-damaged" Not kept by --upper-threshold (still counted as no_informative_sites)
Lowercase reference bases in MD (outside the SAM spec) are never counted as C or G 339 of 14.0 M reads scored differently on a public horse BAM (ENA ERR1892229) Read as uppercase
On reverse-strand reads, MD characters other than ACGTNRY/acgt (e.g. lowercase n) are dropped when the reference is reverse-complemented The reference shifts against the read; scores up to 15.7 too high No shift
Read bases written as = (same as the reference; samtools calmd -e) never count as C or G Matches at those sites are missed: a read with nine = at reference C scored 5.03 from 1 site instead of 4.22 from 10 Taken from -r; without -r, an input error (MD does not give their reference bases)

In CpG modes, expect pmdshodh's default results to differ from PMDtools on some reads.

Differences that prevent crashes or data loss

Situation PMDtools 0.60 pmdshodh
Mapped read without an MD tag Dropped silently Error (exit 2); with -r, scored from the reference
MD string without a 0 after a deletion (as written by MALT) Misparsed silently Parsed correctly
Hard-clipped reads Dropped Scored
Existing DS:Z tag, or DS:Z: text in qualities Crash Handled
Input with only a header Crash Empty output, exit 0
BAM without its end-of-file marker (cut at a block boundary) samtools view warns and passes on the reads before the cut Error (exit 2), no output file
Damage site ≥ 1,000 bp from a read end, or hundreds of mismatches in one read Crash Scored
--first (first-base damage rate with standard error) Crashes in the 0.60 release Reported in the JSON summary

Behaviour kept for compatibility

These are part of PMDtools' score as computed, and pmdshodh keeps them so that scores mean the same:

  • The no-damage model assumes a background damage rate of 0.001.
  • Reads are scored regardless of SAM flags (duplicates, secondary and supplementary alignments, paired reads). Remove unwanted reads beforehand; the summary counts them.

The complete list, with reasoning: docs/design.md §5.

Limitations

  • CRAM is input only; filter writes SAM or BAM.
  • From CRAM input, kept records have no MD or NM tag unless the CRAM stored one. htslib usually leaves these tags out and regenerates them on decoding; pmdshodh does not. Run samtools calmd on the output if a later step needs MD.
  • CRAM input must be scored against the reference it was written with, so a masked FASTA (as nf-core/eager uses to mask positions) fails the checksum check. Mask BAM input instead, or decode the CRAM to BAM first.
  • CRAM input loads whole reference sequences, because noodles decodes against them. With filter -r -@ 8, CRAM took 38.7 s (horse) and 58.5 s (human) with peak memory of 1,190 MB and 1,248 MB, against 12.6 s and 14.8 s with 112 MB for the same reads as BAM (median of 5 runs, docs/benchmarks.md). CRAM input also checks every reference sequence against its checksum (docs/design.md §11).
  • Scores can differ in the last digits between operating systems, because each platform's math library rounds ln and powf differently. On the test fixture, Windows differed from Linux and macOS by at most 1.8e-15. Only a score this close to a threshold could be filtered differently.
  • Benchmarks cover one laptop and two double-stranded libraries: docs/benchmarks.md. On one thread, pmdshodh filter was 32–39× faster than the PMDtools pipeline, and pmdshodh score was faster than metaDMG pmd in all 10 alternating pairs: 48.8 s against 61.7 s and 60.6 s against 71.9 s (medians).
  • --pmdtools-compat does not reproduce PMDtools on = read bases (samtools calmd -e output) for reverse-strand reads, which PMDtools scores as if their explicit bases were at the read end, or for reads made only of =, which PMDtools drops. Default mode resolves = from -r (docs/design.md §5).
  • One real single-stranded library has been tested (ENA PRJEB37007, not UDG-treated; CHANGELOG.md). Single-stranded UDG-half and full-UDG scoring rests on synthetic reads.
  • UDG-half damage tables do not yet match PMDtools (UDG-half scores do).

Development

cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test
  • Reference data: the tests compare against values produced by PMDtools 0.60, stored in tests/data/. The scripts in tests/oracle/ regenerate them; they need PMDtools 0.60 on Linux or WSL.
  • Readable model: tests/oracle/pmd_reference.py is a short Python version of the scoring model.
  • Background: research, design decisions and black-box findings are in docs/.

License

MIT.

Metadata

Release files for pmdshodh 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pmdshodh 0.2.3
File Size Uploaded Provenance Actions
pmdshodh-0.2.3.tar.gz 428.9 kB With provenance Details for pmdshodh-0.2.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for pmdshodh 0.2.3
File Actions
pmdshodh-0.2.3-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for pmdshodh-0.2.3-py3-none-win_amd64.whl
pmdshodh-0.2.3-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for pmdshodh-0.2.3-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
pmdshodh-0.2.3-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for pmdshodh-0.2.3-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
pmdshodh-0.2.3-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for pmdshodh-0.2.3-py3-none-macosx_11_0_arm64.whl
pmdshodh-0.2.3-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details for pmdshodh-0.2.3-py3-none-macosx_10_12_x86_64.whl

Total release size: 7.7 MB

Release files / pmdshodh-0.2.3.tar.gz

Download URL pmdshodh-0.2.3.tar.gz
Size 428.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4c05056b0f74cdfec1dbc56af4bd5ecb7ad07d3f5a3e1c35c4c8d8b6d89855d2
BLAKE2b-256 checksum
How to use checksums
78235cee30e753d30df04771af47cc040df197590f057f48032dace635ee15e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 18, 2026.

Transparency log

Release files / pmdshodh-0.2.3-py3-none-win_amd64.whl

Download URL pmdshodh-0.2.3-py3-none-win_amd64.whl
Size 1.3 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
b449198f4ac1a19a7d2c9716905286e9166b0d0a4f84f37590593438be8dec79
BLAKE2b-256 checksum
How to use checksums
e59bb34e036ee9b3bc344feda3ae1f077d19ff96ae0e54f661aa676910d05212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 18, 2026.

Transparency log

Release files / pmdshodh-0.2.3-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL pmdshodh-0.2.3-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 1.6 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
c8f51065839dd263f1312eb42adb5304d9b4e215386e8a19093945cc6859f0f1
BLAKE2b-256 checksum
How to use checksums
39c847c088090342c718c454b60f462d47bbd6dd47ddca01d6d4e3d1dd90f51d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 18, 2026.

Transparency log

Release files / pmdshodh-0.2.3-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL pmdshodh-0.2.3-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 1.5 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
9574f704d4e1caa9263de0de4c2e89d09af6435d2c9c193e3026b1d00271ff1e
BLAKE2b-256 checksum
How to use checksums
5a2fe9d290abf5f646629d3ccacf5836fa768ac2f70ad50c98742d318a2175f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 18, 2026.

Transparency log

Release files / pmdshodh-0.2.3-py3-none-macosx_11_0_arm64.whl

Download URL pmdshodh-0.2.3-py3-none-macosx_11_0_arm64.whl
Size 1.4 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1ad53d1e9261b991dff8515c601edf0e6b160054456949ba57f026ecb80c311c
BLAKE2b-256 checksum
How to use checksums
fb5004831bab992d52b3d9c99b5308fbae2140101676506c89bf4644fa1ae837
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 18, 2026.

Transparency log

Release files / pmdshodh-0.2.3-py3-none-macosx_10_12_x86_64.whl

Download URL pmdshodh-0.2.3-py3-none-macosx_10_12_x86_64.whl
Size 1.5 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
213bd47a8a4f4f59417d57b91eb785fb15649bfb65a485cea5361ea3731a9175
BLAKE2b-256 checksum
How to use checksums
04cb75bbad61a1c94cf3ededb0ddb2b5ff88fd59b94a8257b9c58335f3e0fee3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 18, 2026.

Transparency log

Security for pmdshodh 0.2.3

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 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for pmdshodh-0.2.3.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 Nov 18, 2026.

Transparency log

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page