neurosnap.sequence.pssm module#

Protein position-specific scoring matrices and affine-gap profile alignment.

Scores use base-2 log odds (bits), not PSI-BLAST’s integer score scale. Only NumPy and the SDK’s existing alignment workflows are used.

class neurosnap.sequence.pssm.PSSM(probabilities, background, counts, sequence_weights, msa_columns, query_positions, query=None)[source]#

Bases: object

A protein profile with rows in query/MSA order and columns in AA_ORDER.

Stored profile data:

  • probabilities: Smoothed residue probabilities, shape (length, 20).

  • background: Positive background frequencies, shape (20,).

  • counts: Weighted residue counts before smoothing, shape (length, 20).

  • sequence_weights: Weights used for the input MSA, normalized to sum to its row count.

  • msa_columns: Zero-based original match-column indices (A3M insertions excluded).

  • query_positions: Zero-based ungapped query indices; -1 denotes a query gap.

  • query: Aligned query characters for retained rows, or None without a query row.

Arrays are copied and made read-only. Direct construction requires all metadata; use pssm_from_msa() for routine construction.

background: ndarray#
property consensus#

Most probable standard residue per position, ties resolved by AA_ORDER.

counts: ndarray#
msa_columns: ndarray#
probabilities: ndarray#
query: Optional[str] = None#
query_positions: ndarray#
property scores#

Return log2(probability/background); unobserved residues may score -inf.

sequence_weights: ndarray#
to_dataframe(kind='scores')[source]#

Return a labeled pandas DataFrame for scores, probabilities, or counts.

The index contains original MSA match-column indices. Pandas is an existing SDK dependency and is imported only when this method is called.

class neurosnap.sequence.pssm.ProfileAlignment(score, aligned_a, aligned_b, pairs, start_a, end_a, start_b, end_b, mode)[source]#

Bases: object

Alignment score in bits, display strings, and zero-based coordinates.

pairs contains profile/sequence indices, with None for a gap. Profile indices address PSSM rows; use msa_columns/query_positions to map them back. start/end are half-open spans in each input. Display strings use consensus residues for profiles. Local alignments with no positive score have empty strings/pairs and zero spans. Scores are not calibrated E-values.

aligned_a: str#
aligned_b: str#
end_a: int#
end_b: int#
mode: str#
pairs: Tuple[Tuple[Optional[int], Optional[int]], ...]#
score: float#
start_a: int#
start_b: int#
neurosnap.sequence.pssm.align_pssms(a, b, *, mode='local', gap_open=5.0, gap_extend=1.0, background=None)[source]#

Align two profiles using log-odds probability overlap and affine gaps.

Position score: log2(sum_r p_a(r) * p_b(r) / background(r)). This symmetric score compares residue overlap with independent background draws. It is a simple profile similarity model, not an HHsearch/HMM alignment or a calibrated significance score. Uniform/background-only columns score zero when profiles share that background. Different backgrounds require an explicit common background argument. Gap semantics and O(L_a * L_b) costs match sequence alignment. Output strings display profile consensus residues.

Return type:

ProfileAlignment

neurosnap.sequence.pssm.align_sequence_to_pssm(profile, sequence, *, mode='local', gap_open=5.0, gap_extend=1.0)[source]#

Align an ungapped protein sequence to a PSSM using affine gap penalties.

Penalties are positive costs in bits. A k-residue gap costs gap_open + (k-1)*gap_extend. Time and memory are O(profile length * sequence length); this implementation is intended for individual proteins, not database searches. B/Z/J/X use marginal log odds as in score_sequence.

Return type:

ProfileAlignment

neurosnap.sequence.pssm.generate_pssm(sequences, *, method='mafft', alignment_kwargs=None, pssm_kwargs=None)[source]#

Generate an MSA through an existing workflow and build its profile.

Parameters:
  • sequences – MAFFT sequences/path, or a single query string for MMseqs2 or phmmer_mafft. A single sequence alone supplies no evolutionary evidence.

  • method – "mafft", "mmseqs2", or "phmmer_mafft".

  • alignment_kwargs – Keyword arguments forwarded to the selected workflow. MMseqs2 requires output and network access; phmmer_mafft requires ref_db_path. MAFFT/phmmer retain their existing executable requirements.

  • pssm_kwargs – Options forwarded to pssm_from_msa. MMseqs2 input is always A3M.

Return type:

PSSM

No new dependencies are installed. Only MSA-to-PSSM calculation and profile alignment run entirely in Python/NumPy; workflow prerequisites still apply.

neurosnap.sequence.pssm.pssm_from_msa(msa, *, background=None, pseudocount=1.0, weighting='henikoff', weights=None, ambiguous='distribute', query_index=0, drop_query_gaps=True, a3m=False)[source]#

Build a PSSM from aligned sequences, FASTA text/path, or a text handle.

Parameters:
  • msa (Union[str, TextIOBase, Sequence[str], Mapping[str, str]]) – Equal-length aligned rows, a name-to-sequence mapping, or FASTA input.

  • background – Mapping or vector in AA_ORDER; defaults to uniform frequencies.

  • pseudocount (float) – Total prior mass per column, distributed using the background. Zero is allowed and may produce -inf scores. Empty columns use background.

  • weighting (str) – "henikoff" (position-based) or "uniform". Henikoff weighting counts fractional ambiguous residues and excludes gaps. All-gap rows get zero weight unless every row is all gaps, when uniform weights are used.

  • weights – Optional explicit nonnegative row weights; overrides weighting. Weights are normalized to sum to the number of input sequences.

  • ambiguous (str) – "distribute" assigns B/Z/J/X across their possible standard residues in background proportions; "ignore" skips nonstandard letters; "error" rejects them. U/O are rejected unless ignored.

  • query_index (Optional[int]) – Query row index, default first row. None retains all columns and records -1 for every query position.

  • drop_query_gaps (bool) – Remove columns that are gaps in the query row.

  • a3m (bool) – Strip lowercase insertion residues and dots BEFORE uppercasing. False treats lowercase FASTA residues as ordinary amino acids.

Return type:

PSSM

Query coordinates are zero-based in the original ungapped query, counting lowercase A3M insertion residues even though those residues have no profile row. This is a transparent log-odds model, not PSI-BLAST emulation.

neurosnap.sequence.pssm.scan_sequence(profile, sequence)[source]#

Return ungapped window scores in bits, indexed by zero-based start position.

A sequence shorter than the profile yields an empty array.

Return type:

ndarray

neurosnap.sequence.pssm.score_sequence(profile, sequence)[source]#

Score a same-length ungapped sequence in bits, marginalizing B/Z/J/X.

Runtime is linear in profile length and no pairwise score matrix is allocated.

Return type:

float