Skip to content

gseek API reference

English | 简体中文

Version: 0.10.5
Python: >=3.12
Source: src/gseek/
New users: read Quickstart first.

0. Status

M0 foundation and M1–M4 adapters are implemented.

Backend Input Index Operations
blast FASTA target alignment (DNA/protein tasks)
minimap2 FASTA / FASTQ / paired target alignment
kma FASTA / FASTQ / paired target alignment
mummer FASTA none alignment (MUMmer 4)
bwa FASTA / FASTQ / paired target alignment
mash FASTA / FASTQ sketch distance / screen
sourmash FASTA / FASTQ sketch search / gather
skani FASTA sketch ANI distance

All symbols below are importable from the top-level package:

import gseek

gseek.__version__        # "0.10.5"
gseek.ALIGNMENT_SCHEMA   # "gseek.alignment/1"
gseek.DISTANCE_SCHEMA    # "gseek.distance/1"
gseek.RUN_SCHEMA         # "gseek.run/1"
gseek.INDEX_SCHEMA       # "gseek.index/1"

1. Core concepts

1.1 One run

gseek run pipeline

Adapters plan argv and parse output. gseek.execution is the only layer that starts subprocesses. The runner owns orchestration and provenance.

1.2 Coordinates

All coordinates are 0-based half-open on the forward sequence. Orientation is stored separately in strand (+ / -). Adapters must not emit 1-based-inclusive coordinates.

1.3 Identity and coverage

identity is a percentage in [0, 100]. Coverage is a fraction in [0, 1]. Keep the source in tags (for example identity_method), especially when a tool reports identity by a non-obvious formula.

1.4 Lossless records

Merging is an explicit view (best_per_query_target, primary_only, best_per_query), not a mutation of records. Consumers can always inspect the original rows.

1.5 Stable order

AlignmentResult and DistanceResult sort records deterministically by their stable key. Do not rely on tool output order.

2. Public API

2.1 gseek.align

gseek.align(
    *,
    query: Path | str | Sequence[Path | str],
    target: Path | str | Sequence[Path | str],
    backend: str | None = None,
    adapter: AlignmentAdapter | None = None,
    query_kind: InputKind | str | None = None,
    paired: bool = False,
    params: Mapping[str, JsonValue] | None = None,
    preset: str | None = None,
    threads: int = 1,
    timeout_s: float | None = None,
    index: IndexPolicy | str = IndexPolicy.AUTO,
    target_filter: Iterable[str] | None = None,
    work_dir: Path | str | None = None,
    keep_work: bool = False,
    manifest: Path | str | None = None,
    record_manifest: bool = True,
    resolver: ToolResolver | None = None,
    executor: Executor | None = None,
    store: IndexStore | None = None,
    query_refs: Sequence[InputRef] | None = None,
    target_refs: Sequence[InputRef] | None = None,
    sort_records: bool = True,
    verify_inputs: bool = False,
) -> AlignmentResult
Argument Meaning
query one file, or two files when paired=True
target one or more target FASTA files
backend registered adapter name; mutually exclusive with adapter
adapter explicit adapter instance (plugins/tests)
query_kind "fasta" / "fastq" override; auto-detected by default
paired treat two query paths as mates
params adapter-specific parameters
preset shorthand for params["preset"]
threads tool threads; recorded, not part of recipe hash
timeout_s per-command timeout
index auto / reuse / rebuild / none
target_filter optional target IDs passed to the adapter
work_dir parent for temporary workspaces
keep_work keep workspaces for debugging
manifest write a gseek.run/1 manifest
record_manifest False skips manifest/result hashing for low-latency consumers
resolver / executor / store inject custom infrastructure
query_refs / target_refs precomputed InputRef tuples (from gseek.provenance.input_refs) that skip re-hashing; lengths must match the path arguments. Useful when running many queries against one target.
sort_records False keeps parser order for consumers that re-sort/aggregate immediately; default canonical ordering is unchanged.
verify_inputs re-hash query/target refs after the tool and fail if an input changed mid-run; off by default (doubles input I/O).

If backend and adapter are both omitted, gseek filters registered adapters by input/output/paired capabilities. Exactly one candidate must remain; zero or multiple candidates raise ConfigError instead of guessing.

Common errors: ConfigError, UnsupportedFeatureError, ToolNotFoundError, ToolVersionError, ExecutionError, ParseError, IndexBuildError.

Backend notes:

Backend Notes
blast DNA/protein tasks; FASTA; no preset; index=none uses -subject
minimap2 requires a preset; FASTA/FASTQ; paired; primary-only by default
kma requires a target index; no coordinates; depth only for FASTQ
mummer FASTA only; MUMmer 4.x; no persistent index
bwa requires a target index; SAM/CIGAR; secondary skipped by default
mash default dist; params={"mode":"screen"} for read screening
sourmash one query file per call; containment toggles semantics
skani FASTA only; sketch database; returns ANI + aligned fraction

2.2 gseek.iter_align

for record in gseek.iter_align(
    query=query,
    target=target,
    backend="blast",
    params={"db": "/path/to/owned/sequences"},
    index="none",
):
    process(record)

iter_align is the low-memory sibling of align: it yields AlignmentRecord objects as the adapter parses them and does not build an AlignmentResult, stats, or a manifest. It accepts the same shared inputs (query_refs / target_refs included) but has no manifest / record_manifest arguments. Close the iterator early to release the child process and workspace.

By default iter_align does not hash the query at all and hashes the target only when a content-addressed index needs its key. Pass prepare_refs=True when a custom adapter consumes PlanContext.query_refs / target_refs. verify_inputs=True prepares refs and re-hashes them after iteration.

2.3 gseek.align_many

result = gseek.align_many(
    queries=["sample1.fa", "sample2.fa", "sample3.fa"],
    target=target,
    backend="blast",
    params={"db": "/path/to/owned/sequences"},
    index="none",
)

align_many merges several single-end FASTA queries into one temporary query, runs the backend once, and restores each record's original query id. This amortizes target hashing, index loading, workspace setup, and process startup across many small queries. It returns a normal AlignmentResult, but without a manifest because the merged query file is temporary. Pass annotate_batch=True to add a batch_index tag to each record, so a consumer can split the merged result back into its input files.

Per-file consumers can pass compute_inputs=False to skip hashing the original query files (the result then has empty inputs), and sort_records=False to keep parser order when they immediately re-sort or aggregate rows. query_refs can supply precomputed input refs instead. FASTQ/paired batching is not covered; use per-file align/iter_align there.

2.4 gseek.distance

gseek.distance(
    *,
    query: Path | str | Sequence[Path | str],
    reference: Path | str | Sequence[Path | str],
    backend: str | None = None,
    adapter: DistanceAdapter | None = None,
    query_kind: InputKind | str | None = None,
    params: Mapping[str, JsonValue] | None = None,
    threads: int = 1,
    timeout_s: float | None = None,
    index: IndexPolicy | str = IndexPolicy.AUTO,
    external_reference: bool = False,
    external_sha256: str | None = None,
    external_size: int | None = None,
    work_dir: Path | str | None = None,
    keep_work: bool = False,
    manifest: Path | str | None = None,
    record_manifest: bool = True,
    verify_inputs: bool = False,
    resolver: ToolResolver | None = None,
    executor: Executor | None = None,
    store: IndexStore | None = None,
) -> DistanceResult

Used by mash, sourmash, and skani. There are no alignment coordinates.

external_reference=True means reference is already a built artifact rather than FASTA: .msh, .sig/.zip, a sourmash rocksdb directory, or a skani database directory. In that case gseek skips reference indexing and hashes the artifact (directories use deterministic tree hashing; symlinks are rejected). verify_inputs=True optionally re-hashes query/reference refs after the tool to detect inputs that changed mid-run.

2.5 Registry queries

gseek.backends() -> tuple[BackendSpec, ...]
gseek.find(
    *,
    input: InputKind | str | None = None,
    output: OutputKind | str | None = None,
    paired: bool | None = None,
    molecule: Molecule | str | None = None,
) -> tuple[BackendSpec, ...]
gseek.find_backends(...)  # alias

BackendSpec.capabilities describes inputs, outputs, molecule, paired, index_scope, streaming, threads, presets, and deterministic.

2.6 Get/register an adapter

adapter = gseek.get_backend("minimap2")
result = gseek.align(
    adapter=adapter,
    query="sample.fna",
    target="alleles.fna",
    preset="asm5",
)

gseek.register_adapter(MyAdapter)
gseek.register_adapter(MyAdapter, replace=True)

register_adapter accepts a class or instance; duplicate names raise ConfigError unless replace=True. Plugins may also use the gseek.adapters entry-point group.

2.7 Verify a manifest

issues = gseek.verify("runs/sample.manifest.json")
for issue in issues:
    print(issue.severity, issue.code, issue.message)

verify checks recorded file sizes/SHA-256, recomputes recipe_hash, and rejects empty input/target manifests. strict_paths=True confines every resolved path to base_dir. It does not re-probe tools or rerun.

2.8 Replay

from pathlib import Path
import gseek

result = gseek.replay(Path("runs/sample.manifest.json"), check=True)
print(result.matches, result.issues)

Replay re-plans through the adapter from the manifest; it never executes stored argv. check=True requires a recorded result_hash, fails on any verification issue, and compares the new result hash. strict_paths=True confines resolved paths to base_dir, but replay is still not a sandbox.

2.9 Consumer switch: gseek.compat

from gseek.compat import Engine

engine = Engine.from_env("GSPEC_ENGINE")  # legacy (default) | gseek | shadow

result = engine.run(
    legacy=run_legacy,
    gseek=run_gseek,
    compare=compare_results,  # optional; default ==
)
  • legacy: only legacy; no gseek adapter import.
  • gseek: only gseek; exceptions propagate.
  • shadow: run both, compare, return legacy; gseek/comparator failures log and fall back to legacy.

Consumers materialize streaming iterators before passing them to run.

3. Result models

3.1 AlignmentRecord

Key fields:

query_id, target_id
query_length, target_length
query_start, query_end, target_start, target_end
strand
identity, aligned_length, matches, mismatches,
gap_count, gap_openings
evalue, bit_score, mapq, depth, score
query_coverage, target_coverage
query_aligned, target_aligned, target_description
is_primary, is_supplementary
tags

AlignmentRecord.stable_key defines deterministic ordering.

3.2 AlignmentResult

backend, records, inputs, targets, params, stats
index (IndexRef | None), manifest (RunManifest | None)
warnings, errors, schema, sort_records

sort_records=True is the default canonical ordering. sort_records=False keeps parser order for consumers that re-sort or aggregate immediately; it does not change result_hash, which canonicalizes independently.

Views:

  • primary_only() — primary, non-supplementary records.
  • best_per_query_target() — best row per query/target by (identity, aligned_length, query_coverage, target_coverage); ties keep the first row.
  • summarize_by_target() — aggregate counts.
  • Iteration/len() operate on normalized, sorted records.

3.3 DistanceRecord / DistanceResult

Fields: query_id, reference_id, distance, pvalue, shared_hashes, total_hashes, containment, jaccard, ani, aligned_fraction, tags.

DistanceResult contains backend, records, inputs, references, params, stats, index, manifest, warnings, errors, schema; it offers best_per_query().

3.4 Provenance references

  • InputRef: path, kind (fasta/fastq/sketch), SHA-256, size.
  • ToolRef: binary, path, version.
  • IndexRef: key, adapter/version, entry, source hash, artifacts, params, cache-hit flag, tool versions.
  • CommandRecord: argv, cwd, exit code, duration, stdout/stderr hashes.
  • RunManifest: schema, run ID, timestamps, inputs, tools, index, commands, params, result hash, environment allowlist, issues.

4. Adapter API

4.1 Protocol

An adapter implements:

class MyAdapter:
    spec: BackendSpec

    def probe(self, resolver, *, strict=True): ...
    def plan_index(self, request: IndexRequest, ctx: PlanContext) -> IndexPlan: ...
    def plan_align(self, request: AlignRequest, index: IndexRef | None,
                   ctx: PlanContext) -> AlignPlan: ...
    def parse_align(self, ctx: ParseContext, lines: Iterable[str]) -> Iterable[AlignmentRecord]: ...

Distance adapters use DistanceRequest / DistancePlan / DistanceRecord. Adapters must not call subprocess directly.

4.2 Requests and contexts

AlignRequest carries query/target paths, detected kinds, paired flag, params, threads, timeout, index policy, target filter, workspace policy. PlanContext carries workspace, index directory, and environment.

4.3 Capabilities

Declare inputs, outputs, molecule, paired support, index scope, streaming, threads, presets, and determinism in BackendSpec / Capabilities. The runner uses these for automatic adapter selection and validation.

4.4 Minimal adapter

from gseek.adapter import BaseAdapter
from gseek.capabilities import BackendSpec, Capabilities, ToolRequirement
from gseek.execution import CommandPlan

class MyAdapter(BaseAdapter):
    spec = BackendSpec(
        name="myalign",
        family="myalign",
        summary="example aligner",
        capabilities=Capabilities(...),
        tools=(ToolRequirement(binary="myalign", role="aligner"),),
    )

    def plan_align(self, request, index, ctx):
        return AlignPlan(commands=(CommandPlan(argv=("myalign", ...)),))

Use @pytest.mark.integration and skip when the binary is absent. Adapter tests must cover command planning, golden parsing (forward/reverse/gapped/empty), coordinate normalization, identity/coverage sources, index keys, and errors.

4.5 Plugin entry points

Register third-party adapters under the gseek.adapters entry-point group; the registry imports them lazily.

5. Execution layer

5.1 CommandPlan

A command plan is an argv tuple plus label, cwd, environment overrides, stdin, output capture mode, and timeout. No shell strings.

5.2 Executor

Responsibilities:

  • resolve binaries and run with an argv list;
  • set LC_ALL=C, deterministic environment, absolute paths;
  • enforce timeouts and bounded stdout/stderr capture, support streaming;
  • return exit code/stdout/stderr without interpreting tool semantics.

5.3 ToolResolver

Discovers binaries from PATH and explicit overrides, probes versions, caches results per process, and raises typed errors for missing/old tools. Tool SHA-256 is opt-in (GSEEK_TOOL_HASH=1 or ToolResolver(hash_tools=True)) and persisted in GSEEK_CACHE_DIR/tool-hashes.json, keyed by path/size/mtime/inode.

5.4 Environment variables

Variable Meaning
GSEEK_CACHE_DIR index cache root
GSEEK_INDEX_VERIFY 1/full re-hashes cached index artifacts on lookup
GSEEK_MAX_DECOMPRESSED_BYTES override the compressed-input budget
GSEEK_TOOL_HASH 1/full records tool binary SHA-256 in manifests/keys
GSEEK_<BINARY> explicit tool path, e.g. GSEEK_BLASTN
GSEEK_*_TIMEOUT per-tool timeout overrides where supported
LC_ALL forced to C for tool execution

Only an allowlisted environment subset is recorded automatically. Manifest params, commands.argv, and stderr_tail are recorded verbatim and may contain sensitive values; use record_manifest=False for sensitive jobs or redact values before writing.

5.5 Workspace

RunWorkspace allocates a temporary directory per run, supports cleanup, and can be retained with keep_work=True for debugging. All output paths must stay inside the workspace or index staging directory.

6. Index layer

6.1 Policies

IndexPolicy: auto, reuse, rebuild, none.

  • auto: reuse a matching cache entry or build one.
  • reuse: require a cache hit.
  • rebuild: invalidate and rebuild.
  • none: no persistent index (where the adapter supports subject mode).

6.2 Content-addressed key

The key includes adapter name/version, semantic parameters, target content SHA-256, and tool version (plus tool SHA-256 when GSEEK_TOOL_HASH is enabled). threads, timeout, and other non-semantic values are excluded. There are no mtime decisions.

6.3 Cache layout and GC

Indexes are stored under the cache root by key with a lock file, staging directory, and atomic publish. Index/adapter directories are created 0700 and index files 0600; a shared cache is not a security boundary. index.json records artifact sizes/SHA-256; lookup checks sizes by default and only re-hashes when GSEEK_INDEX_VERIFY=1/full (or IndexStore(verify_artifacts=True)) is used. Long runs hold a refreshed in-use lease; GC/rebuild skip leased entries and re-check the directory identity before deletion. IndexStore supports lookup, build, purge, and garbage collection. gseek index gc is a dry run unless --apply is passed.

7. Reproducibility and manifests

7.1 Hashing

  • sha256_file, sha256_tree (deterministic tree hash; symlinks rejected; bounded by a max entry count)
  • canonical_json / sha256_json
  • result_hash over sorted normalized records

7.2 recipe_hash

The recipe hash captures the semantic operation: adapter, semantic params, inputs, index key, and tool versions. It is stable across hosts where the same inputs/tools are used.

7.3 Manifest

gseek.run/1 records:

{
  "schema": "gseek.run/1",
  "run_id": "...",
  "created_at": "...",
  "inputs": [],
  "tools": [],
  "index": null,
  "commands": [],
  "params": {},
  "result_hash": "...",
  "environment": {},
  "issues": []
}

write_manifest/read_manifest round-trip the model; read_manifest rejects files larger than 64 MiB by default. verify_manifest checks recorded file hashes and recomputes recipe_hash. replay re-plans and can compare result_hash; --check fails on missing hashes or verification issues.

8. I/O and views

  • gseek.io: FASTA/FASTQ readers, line normalization, merge_fasta.
  • gseek.views: explicit record views used by consumers.
  • Compressed inputs have a decompression budget of max(16 MiB, 64 × disk size); open_text(max_chars=...) can override it.
  • Input paths, sequence IDs, and BWA read groups reject control characters; to_tsv escapes tabs/newlines instead of creating extra columns/rows.
  • No adapter writes outside its workspace.

9. Errors

See Error model for the full table. All typed failures derive from GseekError with code, message, context, hint, and exit_code.

10. CLI

See CLI reference. The CLI is a thin API wrapper; its JSON output and exit codes are part of the automation contract.

11. Schemas

The stable schema strings are:

gseek.alignment/1
gseek.distance/1
gseek.run/1
gseek.index/1
gseek.error/1
gseek.ispcr/1
gseek.primers/1

Example alignment record:

{
  "query_id": "contig1",
  "target_id": "allele_1",
  "query_start": 100,
  "query_end": 580,
  "target_start": 0,
  "target_end": 480,
  "strand": "+",
  "identity": 99.79,
  "aligned_length": 480,
  "target_coverage": 1.0,
  "query_coverage": 1.0,
  "tags": {"identity_method": "blast_pident"}
}

Schemas are versioned; breaking changes require a new schema string.

12. Stability and limits

  • 0.10.5 is the current release; pin it in production. Public API may still change before 1.0.
  • Pin gseek==0.10.5 in production and keep a legacy path via gseek.compat while migrating.
  • Backend coverage differs by tool; see the matrix at the top.
  • Manifest replay requires the same adapter/tool versions; environment changes can legitimately change results and are reported as replay mismatches.
  • Return to the documentation index or Chinese API reference.

13. In-silico PCR

gseek.ispcr answers the question an alignment cannot: does this primer pair amplify, and what is the product size? It is pure stdlib arithmetic on sequences — no external tool, no index, no subprocess — so any consumer can put PCR confirmation next to its alignments in the same result vocabulary.

import gseek

pairs = gseek.read_primers("primers.tsv")            # or gseek.primers/1 JSON
result = gseek.ispcr("assembly.fna", pairs, max_mismatches=2)
result.pairs                                          # frozenset of positive pair names
result.amplicons[0].size                              # product length in bp
result.to_document()                                  # gseek.ispcr/1
$ gseek ispcr -p primers.tsv -q assembly.fna -f tsv
FILE          PAIR    CONTIG  START  END  SIZE  FWD_MM  REV_MM
assembly.fna  HS55_1  ctg3    1250   1583 334   0       0
Concept Rule
Amplification the forward primer matches one strand and the reverse primer's reverse complement matches the same strand downstream, inside the pair's size window
Orientation both strands are searched; coordinates are always on the forward strand, 0-based half-open
Mismatches counted per primer; max_mismatches is a run default a pair may override
Indels max_edits enables a gapped pass (edits = substitutions + insertions + deletions per primer site); it runs only when the substitution scan finds nothing, and 0 (default) keeps substitution-only matching
Performance pure stdlib; the optional gseek-ispcr-fast Rust wheel (rust/ispcr, q-gram index + banded edit distance) replaces the per-seed scans with one pass per contig: 7 pairs 263 → 20 ms per 1.7 Mb genome
IUPAC ambiguity codes in a primer match their expansion; an ambiguity code in the target is a mismatch, never a match
Contigs one contig per amplicon — products never span contigs
Limits seed-and-verify search; at most 4096 seed hits verified per seed, so a low-complexity target cannot blow up the runtime

Primer tables are TSV (name, forward, reverse, optional min_size, max_size, max_mismatches) or gseek.primers/1 JSON. The result document is gseek.ispcr/1; every amplicon carries its product size, both primer sites and each primer's mismatch count, so a call can be audited without re-running.