Skip to main content

sshash

Python bindings for sshash-rs — a compressed dictionary for DNA k-mers based on Sparse and Skew Hashing.

sshash stores a set of k-mers (strings of length k over {A, C, G, T}) compactly using minimal perfect hashing and succinct data structures (Elias-Fano, BitFieldVec), and supports fast individual and streaming lookups. It is the k-mer index underlying the piscem read mapper.

Installation

pip install sshash

Building an index

From a FASTA/FASTQ file

import sshash

config = sshash.BuildConfig(k=31, m=19)
config.canonical = True   # k-mer and its reverse complement map to the same entry
config.threads = 8        # parallel build (0 = all cores)
config.verbose = False

dict = config.build_from_file("sequences.fa.gz")
dict.save("my_index")

From a list of sequences in memory

config = sshash.BuildConfig(k=31, m=19)
sequences = ["ACGTACGTACGTACGTACGTACGTACGTACG",
             "TTGCAACCGTTAGCAACGTACGTACGTACGT"]
dict = config.build(sequences)

From a Cuttlefish .cf_seg file

When sequences come from Cuttlefish, build_from_cf_seg also returns a mapping from sshash string IDs back to the original Cuttlefish node IDs:

config = sshash.BuildConfig(k=31, m=19)
dict, segment_ids = config.build_from_cf_seg("unitigs.cf_seg")
# segment_ids[i] is the Cuttlefish node ID for sshash string_id i

Loading and saving

# Save to disk (writes <prefix>.ssi and <prefix>.ssi.mphf)
dict.save("my_index")

# Load from disk
dict = sshash.Dictionary.load("my_index")

Querying

Single k-mer lookup

# Returns a Hit object, or None if not found
hit = dict.query("ACGTACGTACGTACGTACGTACGTACGTACG")
if hit is not None:
    print(hit.kmer_id)           # global k-mer ID
    print(hit.string_id)         # unitig containing this k-mer
    print(hit.kmer_id_in_string) # position within that unitig
    print(hit.orientation)       # +1 forward, -1 reverse complement

# Just the k-mer ID (faster if location info isn't needed)
kmer_id = dict.lookup("ACGTACGTACGTACGTACGTACGTACGTACG")  # None if absent

# Membership test
present = dict.contains("ACGTACGTACGTACGTACGTACGTACGTACG")

By default, a query on a non-canonical (strand-specific) index falls back to the reverse complement, so the reverse complement of an indexed k-mer is still found (reported with orientation == -1). Pass forward_only=True for a strand-specific query that does not perform this fallback:

# Non-canonical index: only match the k-mer in its given orientation
hit = dict.query("ACGTACGTACGTACGTACGTACGTACGTACG", forward_only=True)
kmer_id = dict.lookup("ACGTACGTACGTACGTACGTACGTACGTACG", forward_only=True)
present = dict.contains("ACGTACGTACGTACGTACGTACGTACGTACG", forward_only=True)

forward_only is ignored for canonical indexes, where both strands are inherently equivalent.

Streaming queries over a sequence

The streaming engine maintains minimizer state across consecutive k-mers, avoiding redundant MPHF lookups for adjacent positions. This is significantly faster than calling query in a loop when processing full reads or contigs.

engine = dict.streaming_query()

# Query all k-mers in a sequence at once (returns list)
hits = engine.query_sequence("ACGTACGTACGTACGTACGTACGTACGTACGTACGT")
for hit in hits:
    if hit is not None:
        print(hit.kmer_id, hit.string_id)

# Lazy iterator (memory-efficient for long sequences)
for hit in engine.iter_sequence(b"ACGTACGT..."):
    if hit is not None:
        print(hit.kmer_id)

# Efficiency statistics
print(engine.num_searches)    # full MPHF lookups performed
print(engine.num_extensions)  # k-mers resolved by sliding-window extension

Index properties

print(dict.k)           # k-mer length
print(dict.m)           # minimizer length
print(dict.canonical)   # canonical mode flag
print(dict.num_strings) # number of unitigs
print(dict.num_bits)    # total index size in bits

BuildConfig options

Property Default Description
canonical False Map each k-mer and its reverse complement to the same entry
threads 0 Worker threads during build (0 = all available cores)
ram_limit_gib 8 RAM budget (GiB) before switching to external sort
seed internal Seed for internal hash functions
verbose True Print progress during building
tmp_dir "sshash_tmp" Directory for temporary files during external sort

k and m are set at construction time and cannot be changed afterwards.

References

License

BSD 3-Clause

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sshash-0.4.0.tar.gz (127.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sshash-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

sshash-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

sshash-0.4.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (2.2 MB view details)

Uploaded CPython 3.8+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file sshash-0.4.0.tar.gz.

File metadata

  • Download URL: sshash-0.4.0.tar.gz
  • Upload date:
  • Size: 127.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sshash-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c5eee74bae73ffd8fafe5f9c2320e205b5b32e0b8d0d01fe2c7151df1f7d35c7
MD5 a357f22e421606635f011464b5401c47
BLAKE2b-256 93bd9eb4bc98ef4cc81c74668f4fb84bdad6f1146b795b0ac606b09f4bc5b201

See more details on using hashes here.

Provenance

The following attestation bundles were made for sshash-0.4.0.tar.gz:

Publisher: publish-sshash-py.yml on COMBINE-lab/sshash-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sshash-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for sshash-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5e7930805d0586e27aec8ff4edb07f26664d00e05b65fff1a65e871b8505a2a1
MD5 82ebca41e70eba0eaa2156fd6e229487
BLAKE2b-256 5d460cfbd742451719ec46be9ee8db7a056bf276c45088bda7a6ed495d67bd7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sshash-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish-sshash-py.yml on COMBINE-lab/sshash-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sshash-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for sshash-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 856c69bb6ed8322759e20959f0ca3ea7bdc050b6c5e58ad5d689710d23900a47
MD5 11325b4ebfb314e1c5516815c03481de
BLAKE2b-256 8fb80a7ecdb1bfad492c76df80a852912f955d689659ad09ea774c3dcafde792

See more details on using hashes here.

Provenance

The following attestation bundles were made for sshash-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: publish-sshash-py.yml on COMBINE-lab/sshash-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sshash-0.4.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for sshash-0.4.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 907a9420bfa3b70296ade32b59211d0f632d488bd165f07c4ce1c1cbe5a4f240
MD5 5ee70a1a926c4825cc6b8c5b9a65fb67
BLAKE2b-256 28c9f0497f8412b6aa430d91d8f16ec9e1acc9241b95e5de861af3d0e7d085c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for sshash-0.4.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: publish-sshash-py.yml on COMBINE-lab/sshash-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.4.0 This release

4 files

0.3.0

4 files

0.2.1

4 files

0.2.0

4 files

0.1.0

4 files

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