Skip to content

PyO3 Bridge — fastdpplot._rs

Source: crates/fastdpplot-py/src/lib.rs

The fastdpplot._rs module is a compiled PyO3 extension that exposes the Rust core to Python. Import it directly for maximum performance or use the higher-level Python wrappers in fastdpplot.io and fastdpplot.plot.

Module name

The shared library is registered as fastdpplot._rs (note the leading underscore). It is treated as an implementation detail; the stable public API is the Python package.


Function reference

load_parquet(path) → list[tuple[float, float, float]]

Load a Parquet file and return a list of (x, y, value) tuples.

from fastdpplot._rs import load_parquet
points = load_parquet("hits.parquet")
# [(x0, y0, v0), (x1, y1, v1), …]

load_txt(path, sep, x_col, y_col, val_col) → list[tuple[float, float, float]]

Parse a delimited text file and return (x, y, value) tuples.

Parameter Type Default Description
path str File path.
sep str "\t" Single-character separator.
x_col int 0 Zero-based X column index.
y_col int 1 Zero-based Y column index.
val_col int \| None None Zero-based value column index.
from fastdpplot._rs import load_txt
points = load_txt("blast.txt", sep="\t", x_col=6, y_col=8, val_col=2)

Warning

sep must be exactly one character. Passing an empty string or a multi-character string raises ValueError.


bin_points_py(xs, ys, values, x_min, x_max, y_min, y_max, width, height) → (counts, values)

Bin a set of points into a 2-D histogram grid.

Parameter Type Description
xs list[float] X coordinates.
ys list[float] Y coordinates.
values list[float] Scalar values.
x_min/x_max float Viewport X bounds.
y_min/y_max float Viewport Y bounds.
width int Grid width in pixels.
height int Grid height in pixels.

Returns (counts_flat, values_flat) — row-major flat arrays of length width × height.

from fastdpplot._rs import bin_points_py
counts, values = bin_points_py(xs, ys, vs, 0.0, 1000.0, 0.0, 1000.0, 800, 600)

Re-binning on pan/zoom

This is the hot function called by serve_interactive on every RangeXY stream event. The Python lists are pre-computed once to avoid O(N) numpy → list conversion on every zoom.


to_coo(counts, values, width, height) → (rows, cols, data)

Convert flat binning output to COO sparse triplets. Bins where count == 0 are excluded.

from fastdpplot._rs import to_coo
rows, cols, data = to_coo(counts, values, width, height)

parse_fasta_first(path) → (id, description, sequence, length)

Return (id, description, sequence, length) for the first FASTA record.

from fastdpplot._rs import parse_fasta_first
id_, desc, seq, length = parse_fasta_first("data/query.fasta")

parse_fasta_all(path) → list[tuple[str, str, str, int]]

Return (id, description, sequence, length) for all FASTA records.

from fastdpplot._rs import parse_fasta_all
records = parse_fasta_all("data/multi.fasta")
for id_, desc, seq, length in records:
    print(f"{id_}: {length} bp")

fasta_axis_label(path, max_len) → str

Return the axis label string for the first FASTA record, truncated to max_len characters.

from fastdpplot._rs import fasta_axis_label
label = fasta_axis_label("data/query.fasta", 80)

load_dp_input(matrix_path, fasta_a_path, fasta_b_path) → (xs, ys, values, query_label, subject_label, query_len, subject_len)

One-shot loader: parse two FASTA files + .bin matrix, validate dimensions, and return everything needed to render a labelled dotplot.

Values are raw i32 scores cast to f32; sign is fully preserved (negative scores remain negative, zero stays zero, positive scores remain positive).

Return position Type Description
0 list[float] X coordinates.
1 list[float] Y coordinates.
2 list[float] Raw scores (sign preserved).
3 str Query axis label (Y axis).
4 str Subject axis label (X axis).
5 int Query sequence length.
6 int Subject sequence length.
from fastdpplot._rs import load_dp_input

xs, ys, vs, q_label, s_label, q_len, s_len = load_dp_input(
    "data/dp_matrix.bin",
    "data/query.fasta",
    "data/subject.fasta",
)

Prefer fastdpplot.io.load_bin

For most use cases fastdpplot.io.load_bin is more convenient — it wraps this function and returns a named dict including a pd.DataFrame.