CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-dendropy

A Python library for phylogenetics and phylogenetic computing: reading, writing, simulation, processing and manipulation of phylogenetic trees and characters.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

tree-analysis.mddocs/

Tree Analysis & Comparison

Phylogenetic tree analysis including distance calculations, tree comparison metrics, summarization algorithms, and topological analysis. DendroPy provides comprehensive tools for comparing trees, calculating phylogenetic distances, and summarizing tree collections.

Capabilities

Tree Comparison Metrics

Functions for comparing phylogenetic trees using various distance metrics and topological measures.

# Import tree comparison functions
from dendropy.calculate.treecompare import (
    symmetric_difference, 
    unweighted_robinson_foulds_distance,
    weighted_robinson_foulds_distance,
    euclidean_distance
)

# Tree comparison functions
def symmetric_difference(tree1, tree2, is_bipartitions_updated=False):
    """
    Calculate symmetric difference between two trees.
    
    Parameters:
    - tree1, tree2: Tree objects to compare
    - is_bipartitions_updated: Whether bipartitions are already calculated
    
    Returns:
    int: Number of bipartitions in one tree but not the other
    """

def unweighted_robinson_foulds_distance(tree1, tree2, is_bipartitions_updated=False):
    """
    Calculate unweighted Robinson-Foulds distance between trees.
    
    Parameters:
    - tree1, tree2: Tree objects to compare  
    - is_bipartitions_updated: Whether bipartitions are already calculated
    
    Returns:
    int: Robinson-Foulds distance (0 = identical topologies)
    """

def weighted_robinson_foulds_distance(tree1, tree2, edge_weight_attr="length", is_bipartitions_updated=False):
    """
    Calculate weighted Robinson-Foulds distance using branch lengths.
    
    Parameters:
    - tree1, tree2: Tree objects to compare
    - edge_weight_attr: Attribute name for edge weights (default: "length")
    - is_bipartitions_updated: Whether bipartitions are already calculated
    
    Returns:
    float: Weighted Robinson-Foulds distance
    """

def euclidean_distance(tree1, tree2, edge_weight_attr="length", is_bipartitions_updated=False, value_type=float):
    """
    Calculate Euclidean distance between trees based on branch lengths.
    
    Parameters:
    - tree1, tree2: Tree objects to compare
    - edge_weight_attr: Attribute name for edge weights  
    - is_bipartitions_updated: Whether bipartitions are already calculated
    - value_type: Type for calculations (float, Decimal, etc.)
    
    Returns:
    float: Euclidean distance between tree vectors
    """

def false_positives_and_negatives(reference_tree, comparison_tree, is_bipartitions_updated=False):
    """
    Calculate false positives and false negatives when comparing trees.
    
    Parameters:
    - reference_tree: Reference (true) tree
    - comparison_tree: Tree being evaluated
    - is_bipartitions_updated: Whether bipartitions are already calculated
    
    Returns:
    tuple: (false_positives, false_negatives)
    """

def find_missing_bipartitions(reference_tree, comparison_tree, is_bipartitions_updated=False):
    """
    Find bipartitions present in reference but missing in comparison tree.
    
    Parameters:
    - reference_tree: Reference tree with expected bipartitions
    - comparison_tree: Tree to check for missing bipartitions  
    - is_bipartitions_updated: Whether bipartitions are already calculated
    
    Returns:
    set: Set of Bipartition objects missing from comparison tree
    """

def mason_gamer_kellogg_score(tree):
    """
    Calculate Mason-Gamer-Kellogg score for tree shape.
    
    Parameters:
    - tree: Tree object to analyze
    
    Returns:
    float: MGK score measuring tree balance
    """

Phylogenetic Distance Matrices

Classes for calculating and storing various types of phylogenetic distances.

class PhylogeneticDistanceMatrix:
    """
    Matrix of phylogenetic distances between taxa.
    
    Parameters:
    - tree: Tree object for distance calculations
    - taxon_namespace: TaxonNamespace for matrix indexing
    """
    
    def __init__(self, tree=None, taxon_namespace=None): ...
    
    def patristic_distances(self, tree):
        """
        Calculate patristic (tree path) distances between all taxa.
        
        Parameters:
        - tree: Tree for distance calculation
        
        Returns:
        None (updates internal distance matrix)
        """
    
    def path_distance(self, taxon1, taxon2):
        """
        Get path distance between two specific taxa.
        
        Parameters:
        - taxon1, taxon2: Taxon objects
        
        Returns:
        float: Path distance between taxa
        """
    
    def max_dist(self):
        """Get maximum distance in matrix."""
    
    def mean_dist(self):
        """Get mean distance in matrix."""
    
    def distances(self):
        """Iterator over all pairwise distances."""
    
    def taxon_pairs(self):
        """Iterator over all taxon pairs."""

class PatristicDistanceMatrix(PhylogeneticDistanceMatrix):
    """Specialized matrix for patristic distances."""
    
    def __init__(self, tree): ...

class NodeDistanceMatrix:
    """
    Matrix of distances between tree nodes.
    
    Parameters:
    - tree: Tree object for node distance calculations
    """
    
    def __init__(self, tree=None): ...
    
    def distances(self, node1, node2):
        """Get distance between two nodes."""

Tree Summarization

Tools for summarizing collections of trees and extracting consensus information.

class TreeSummarizer:
    """
    Summarizes collections of trees into consensus trees and statistics.
    
    Parameters:
    - taxon_namespace: TaxonNamespace for summary trees
    """
    
    def __init__(self, taxon_namespace=None): ...
    
    def summarize(self, trees, min_freq=0.5):
        """
        Create consensus tree from tree collection.
        
        Parameters:
        - trees: Iterable of Tree objects
        - min_freq: Minimum frequency for bipartition inclusion
        
        Returns:
        Tree: Consensus tree with support values
        """
    
    def map_support_as_node_ages(self, tree, trees):
        """Map bipartition support values as node ages on tree."""
    
    def map_support_as_node_labels(self, tree, trees, label_format="%.2f"):
        """Map bipartition support values as node labels."""

class TopologyCounter:
    """
    Counts and analyzes tree topologies in collections.
    
    Parameters:
    - taxon_namespace: TaxonNamespace for topology comparison
    """
    
    def __init__(self, taxon_namespace=None): ...
    
    def count(self, trees, topology_counter=None):
        """
        Count unique topologies in tree collection.
        
        Parameters:
        - trees: Iterable of Tree objects  
        - topology_counter: Optional existing counter to update
        
        Returns:
        dict: Mapping from topology to count
        """
    
    def topology_frequencies(self, trees):
        """Get frequencies of different topologies."""
    
    def unique_topologies(self, trees):
        """Get set of unique topologies."""

Bipartition Analysis

Classes and functions for working with bipartitions (splits) in phylogenetic trees.

class Bipartition:
    """
    Represents a bipartition (split) in a phylogenetic tree.
    
    Parameters:
    - taxon_namespace: TaxonNamespace defining taxa
    - bitmask: Bitmask representing the split
    """
    
    def __init__(self, taxon_namespace=None, **kwargs): ...
    
    def split_as_newick_string(self, taxon_namespace=None):
        """Return bipartition as Newick string."""
    
    def leafset_as_newick_string(self, taxon_namespace=None):
        """Return leaf set as Newick string."""
    
    def is_compatible_with(self, other_bipartition):
        """Check if bipartition is compatible with another."""
    
    def is_nested_within(self, other_bipartition):
        """Check if bipartition is nested within another."""

def encode_bipartitions(tree):
    """
    Encode all bipartitions in tree.
    
    Parameters:
    - tree: Tree object to encode
    
    Returns:
    None (adds bipartition encoding to tree nodes)
    """

def update_bipartitions(tree, suppress_unifurcations=True, collapse_unrooted_basal_bifurcation=True):
    """Update bipartition encoding on tree."""

Tree Shape and Balance Analysis

Functions for analyzing tree shape, balance, and other topological properties.

def colless_tree_imbalance(tree, normalize="max"):
    """
    Calculate Colless index of tree imbalance.
    
    Parameters:
    - tree: Tree object to analyze
    - normalize: Normalization method ("max", "yule", or None)
    
    Returns:
    float: Colless imbalance index
    """

def sackin_index(tree, normalize=True):
    """
    Calculate Sackin index of tree balance.
    
    Parameters:
    - tree: Tree object to analyze  
    - normalize: Whether to normalize by number of leaves
    
    Returns:
    float: Sackin balance index
    """

def b1_index(tree):
    """
    Calculate B1 balance index.
    
    Parameters:
    - tree: Tree object to analyze
    
    Returns:
    float: B1 balance index
    """

def treeness(tree):
    """
    Calculate treeness (proportion of internal edge length).
    
    Parameters:
    - tree: Tree object with branch lengths
    
    Returns:
    float: Treeness value (0-1)
    """

def resolution(tree):
    """
    Calculate tree resolution (proportion of internal nodes that are bifurcating).
    
    Parameters:
    - tree: Tree object to analyze
    
    Returns:
    float: Resolution value (0-1)
    """

Population Genetics Statistics

Statistical calculations relevant to population genetics and phylogeography.

class PopulationPairSummaryStatistics:
    """
    Summary statistics for pairs of populations.
    
    Parameters:
    - pop1_nodes: Nodes representing population 1
    - pop2_nodes: Nodes representing population 2
    """
    
    def __init__(self, pop1_nodes, pop2_nodes): ...
    
    def fst(self):
        """Calculate Fst between populations."""
    
    def average_number_of_pairwise_differences(self):
        """Calculate average pairwise differences."""
    
    def average_number_of_pairwise_differences_between(self):
        """Calculate average differences between populations."""
    
    def average_number_of_pairwise_differences_within(self):
        """Calculate average differences within populations."""
    
    def num_segregating_sites(self):
        """Count segregating sites."""
    
    def wattersons_theta(self):
        """Calculate Watterson's theta."""
    
    def tajimas_d(self):
        """Calculate Tajima's D."""

Statistical Functions

General statistical and probability functions used in phylogenetic analysis.

def mean_and_sample_variance(values):
    """
    Calculate mean and sample variance.
    
    Parameters:
    - values: Iterable of numeric values
    
    Returns:
    tuple: (mean, sample_variance)
    """

def mean_and_population_variance(values):
    """
    Calculate mean and population variance.
    
    Parameters:
    - values: Iterable of numeric values
    
    Returns:  
    tuple: (mean, population_variance)
    """

def mode(values):
    """
    Find mode (most frequent value).
    
    Parameters:
    - values: Iterable of values
    
    Returns:
    Value that appears most frequently
    """

def median(values):
    """
    Calculate median value.
    
    Parameters:
    - values: Iterable of numeric values
    
    Returns:
    float: Median value
    """

def quantile(sorted_values, q):
    """
    Calculate quantile of sorted data.
    
    Parameters:
    - sorted_values: Sorted list of values
    - q: Quantile to calculate (0.0-1.0)
    
    Returns:
    float: Quantile value
    """

def empirical_hpd(samples, level=0.95):
    """
    Calculate empirical highest posterior density interval.
    
    Parameters:
    - samples: List of sample values
    - level: Credibility level (default 0.95)
    
    Returns:
    tuple: (lower_bound, upper_bound)
    """

class FishersExactTest:
    """Fisher's exact test for contingency tables."""
    
    def __init__(self, a, b, c, d): ...
    def left_tail_p(self): ...
    def right_tail_p(self): ...
    def two_tail_p(self): ...

Install with Tessl CLI

npx tessl i tessl/pypi-dendropy

docs

character-data.md

core-data-models.md

data-io.md

index.md

simulation.md

tree-analysis.md

visualization-interop.md

tile.json