PyEvoMotion package

PyEvoMotion is a single compiled extension module (written in Rust, built with PyO3). Every public name lives directly under PyEvoMotion. The analysis classes form one inheritance chain, so an instance of PyEvoMotion has every method listed on this page. FASTA input is handled by SequenceRecord and FastaReader; the dataset itself lives in a Table and is exposed to Python as a pandas DataFrame through the data attribute.

Analysis

class PyEvoMotion.PyEvoMotion(input_fasta, input_meta, dt='7D', filters=None, positions=None, date_range=None, refseq=None, verbose=0, load_mutation_instructions=None, recount_mutation_types=False)

Bases: _PyEvoMotionCore

class PyEvoMotion._PyEvoMotionCore(*args, **kwargs)

Bases: PyEvoMotionParser

analysis(length, show=False, mutation_kind='all', export_plots_filename=None, confidence_level=0.95)
compute_stats(DT, origin, mutation_kind='all')

Per-window statistics: rows are binned into windows of DT anchored at origin (pandas Grouper semantics), windows with fewer than two rows are discarded, and for each remaining window the mean and the sample variance of every selected count column are reported together with the window size. Tick frequencies (7D, 12h, …) run in Rust with deterministic arithmetic; anchored frequencies (W, MS, …) take the pandas path.

compute_stats_pandas(DT, origin, mutation_kind)
count_mutation_types()

Add the per-sequence counts (number of substitutions, insertions, deletions, indels, mutations) computed from the mutation instructions. Row-wise, so it is correct whatever the index labels.

classmethod export_plot_results(stats, regs, data_xlabel_units, dt_ratio, output_ptr=None)
get_lengths()
length_filter(length, how='gt')
n_filter(threshold=0.01, how='lt')
classmethod plot_results(stats, regs, data_xlabel_units, dt_ratio)

Parsing and alignment

class PyEvoMotion.PyEvoMotionParser(input_fasta, input_meta, filters, positions, date_range=None, refseq=None, verbose=0, load_mutation_instructions=None)

Bases: PyEvoMotionBase

classmethod create_modifs(alignment)
data

The parsed dataset as a pandas.DataFrame. Reading it materialises the internal table (once, until the next pipeline stage); assigning a DataFrame or a Table replaces the dataset.

filter_by_daterange(start=None, end=None)

Keep rows with start <= date <= end, each bound clamped to the data’s own range (a bound outside the data is ignored). None leaves a bound open. Raises if the effective start is after the effective end.

filter_by_position(start, end)

Restrict mutations to a genome window. Window membership on 1-based positions: substitutions start <= P < end, insertions/deletions start < P <= end (the indel rule reproduces the counting behaviour of every release before 0.2.0, when indel positions were written 0-based; a deletion starting at the first base stays excluded and an insertion after the last base stays included). Rows whose mutations all fall outside the window are dropped; rows that had no mutations are kept. end <= 0 means “to the end of the reference”.

filter_columns(filters)

Keep rows whose value in each filter key matches the given value(s), interpreted as regular expressions with * meaning “anything” (pandas str.contains(regex=True) semantics). Keys that are not columns are ignored.

classmethod generate_alignment(seq1, seq2)

Align seq1 (reference) against seq2 and return the two aligned records as a list [reference, target] of SequenceRecord (residues lower-case, gaps as -).

get_differing_mutations(input_fasta, selection)
load_reference(refseq, input_fasta)

Set self.reference. With refseq (path to a FASTA file) the first record of that file is the reference; otherwise the reference is the sequence in input_fasta whose id is the first row of self.data (the earliest-dated entry, since the metadata is sorted by date).

parse_data(input_fasta, selection)
static parse_metadata(input_meta)

Read a metadata CSV/TSV, parse its date column and sort by date (stable; rows sharing a date keep their file order).

static parse_mutation_data(input_tsv)

Read a data TSV written by a previous run (<out>.tsv: the metadata columns plus mutation instructions, N count and the mutation counts). The mutation instructions cells are Python list literals and are parsed back into lists; empty cells become None (dropped by the caller with a warning). Dates are parsed and rows sorted by date (stable), like parse_metadata.

static parse_sequence_by_id(input_fasta, _id)

The record with id _id, or None if the file has none.

static read_fasta(input_fasta)

Lazily iterate over the records of a FASTA file.

FASTA records

class PyEvoMotion.SequenceRecord(id, seq, description=None)

A named sequence: what PyEvoMotion.reference holds, what PyEvoMotionParser.read_fasta yields and what generate_alignment returns (two aligned records). seq is a plain str, so len(), slicing, .count() and .upper() all behave as on any string.

description

Full FASTA header (without the leading >).

format()

The record as FASTA text (>description\\nsequence\\n).

id

First token of the FASTA header.

lower()

Copy with the sequence lower-cased.

name

Alias of id (Biopython compatibility).

seq

The sequence, as a string.

upper()

Copy with the sequence upper-cased.

class PyEvoMotion.FastaReader(path)

Lazy iterator over the records of a FASTA file (for record in PyEvoMotionParser.read_fasta(path)).

Internal table

class PyEvoMotion.Table

Python-visible wrapper: PyEvoMotion.Table.

column(name)

Cell values of one column as a Python list (NaN/None for missing).

columns

Column names, in order.

dtypes

Mapping of column name to dtype name, using pandas’ dtype names.

empty

True when there are no rows or no columns (pandas DataFrame.empty).

static from_pandas(df)

Build a Table from a pandas DataFrame (exact dtype mapping).

to_pandas()

Materialise as a pandas DataFrame (values, dtypes and index as pandas itself would have produced them).

to_tsv(path, sep='\t')

Write the table as delimited text exactly as to_pandas().to_csv(path, sep=sep, index=False) would. Falls back to pandas for content the Rust writer does not render (opaque objects, foreign dtypes, sub-second timestamps).

Mathematical utilities

class PyEvoMotion.PyEvoMotionBase

Bases: object

classmethod AIC(model1, model2, data, weights=None)
classmethod F_test(model1, model2, data, weights=None)
classmethod adjust_model(x, y, name=None, weights=None, confidence_level=0.95)
static count_prefixes(prefix, mutations)
static date_grouper(df, DT, origin)
classmethod linear_regression(x, y, weights=None, fit_intercept=True, confidence_level=0.95)
static mutation_length_modification(mutation)
static plot_single_data_and_model(data_x, data_y, data_ylabel, model, model_label, data_xlabel_units, ax, dt_ratio, **kwargs)
classmethod power_law_fit(x, y, weights=None, confidence_level=0.95)

Command-line entry point

PyEvoMotion._main()

Console-script entry point (pyproject: PyEvoMotion = “PyEvoMotion:_main”).