← Back to PhyNetPy

PhyNetPy Documentation

Library for the Development and Use of Phylogenetic Network Methods

MSA Module v1.0.0

The MSA module provides classes for handling Multiple Sequence Alignments, including file I/O from Nexus files, sequence grouping, and distance matrix computation.

Author:
Mark Kessler
Last Edit:
3/11/25
Source:
MSA.py

DataSequence Class

class DataSequence

An individual sequence record defined by the data sequence, a name/identifier, and optionally a group ID.

Constructor

__init__(self, sequence: list, name: str, gid: int = -1)
Parameter Type Description
sequence list Sequence of biological data
name str Sequence label/name
gid int Group ID (-1 means no group)

Methods

get_name(self) -> str

Get the sequence name/label.

get_seq(self) -> list[object]

Get the sequence data (typically list of characters).

get_numerical_seq(self) -> list[int]

Get the sequence as hexadecimal integers (for SNP data).

get_gid(self) -> int

Get the group ID.

set_ploidy(self, ploidyness: int) -> None

Set the ploidy level (for bimarker data).

ploidy(self) -> int

Get the ploidy level.

distance(self, seq2: DataSequence) -> float

Calculate pairwise distance to another sequence (count of differences).

MSA Class

class MSA(Iterable[DataSequence])

Class for managing Multiple Sequence Alignments. Handles file I/O from Nexus files, sequence grouping, and provides iteration over sequences.

Constructor

__init__(self, filename: str = None, data: list[DataSequence] = None, grouping: dict = None, grouping_auto_detect: bool = False)
Parameter Type Description
filename str Path to Nexus file with matrix block
data list[DataSequence] Alternative: provide sequences directly
grouping dict Map from group names to sequence names
grouping_auto_detect bool Auto-group by name similarity

Sequence Access

get_records(self) -> list[DataSequence]

Retrieve all sequences in the alignment.

seq_by_name(self, name: str) -> DataSequence

Get a sequence by its exact name.

add_data(self, data_seq: DataSequence) -> None

Add a sequence to the MSA.

Grouping Methods

num_groups(self) -> int

Get the number of groups in the MSA.

group_given_id(self, gid: int) -> list[DataSequence]

Get all sequences with a given group ID.

get_category(self, name: str) -> int

Get the group ID for a sequence name.

group_auto_detect(self) -> dict

Group sequences by name similarity.

retroactive_group(self) -> None

Assign group IDs to ungrouped sequences using auto-detection.

SNP/Ploidy Methods

set_sequence_ploidy(self, sequence_ploidy: list[int] = None) -> None

Set ploidy for each group. If not provided, ploidy is auto-detected from max SNP value.

total_samples(self) -> int

Total number of allele samples across all sequences.

samples_given_group(self, gid: int) -> int

Total samples within a specific group.

Info Methods

dim(self) -> tuple[int, int]

Return (num_sequences, sequence_length) dimensions.

distance_matrix(self) -> dict[tuple, float]

Compute pairwise distances between all sequence pairs.

Usage Examples

from PhyNetPy.MSA import MSA, DataSequence

# Load MSA from Nexus file
msa = MSA(filename="alignment.nex")

# Get dimensions
rows, cols = msa.dim()
print(f"Alignment: {rows} taxa, {cols} sites")

# Iterate over sequences
for seq in msa:
    print(f"{seq.get_name()}: {len(seq)} bp")

# Get specific sequence
human = msa.seq_by_name("Human")
print(human.get_seq()[:10])

# Get all records
all_seqs = msa.get_records()

# Create MSA from DataSequence objects
seq1 = DataSequence(['A','C','G','T'], "Species1", gid=0)
seq2 = DataSequence(['A','C','T','T'], "Species2", gid=0)
seq3 = DataSequence(['A','T','G','T'], "Species3", gid=1)

custom_msa = MSA(data=[seq1, seq2, seq3])

# Compute pairwise distances
D = msa.distance_matrix()
for (s1, s2), dist in D.items():
    print(f"{s1.get_name()} - {s2.get_name()}: {dist}")

# Auto-detect grouping by name similarity
msa_grouped = MSA(filename="data.nex", grouping_auto_detect=True)
print(f"Found {msa_grouped.num_groups()} groups")

# Set ploidy for SNP data
msa.set_sequence_ploidy([2, 2, 2])  # All diploid
# Or auto-detect from max SNP values
msa.set_sequence_ploidy()

See Also

Navigation

Modules

This Page