Skip to content

fit_tree

piqtree.fit_tree(aln, tree, model, num_threads=None, other_options='', *, bl_fixed=False)

Fit branch lengths and likelihood for a tree.

Given a sequence alignment and a fixed topology, uses IQ-TREE to fit branch lengths to the tree.

PARAMETER DESCRIPTION
aln

The sequence alignment.

TYPE: Alignment

tree

The topology to fit branch lengths to.

TYPE: PhyloNode

model

The substitution model with base frequencies and rate heterogeneity.

TYPE: Model | str

num_threads

Number of threads for IQ-TREE to use, by default None (single-threaded). If 0 is specified, IQ-TREE attempts to find the optimal number of threads.

TYPE: int | None DEFAULT: None

bl_fixed

If True, evaluates likelihood using the provided branch lengths on the tree. Branch lengths will be treated as constant in this case, with any unspecified branch lengths still being optimised. Otherwise if False, branch lengths are fitted to the tree whether provided or not. By default False.

TYPE: bool DEFAULT: False

other_options

Additional command line options for IQ-TREE.

TYPE: str DEFAULT: ''

RETURNS DESCRIPTION
PhyloNode

A phylogenetic tree with the same given topology fitted with branch lengths.

Source code in src/piqtree/iqtree/_tree.py
def fit_tree(
    aln: Alignment,
    tree: PhyloNode,
    model: Model | str,
    num_threads: int | None = None,
    other_options: str = "",
    *,
    bl_fixed: bool = False,
) -> PhyloNode:
    """Fit branch lengths and likelihood for a tree.

    Given a sequence alignment and a fixed topology,
    uses IQ-TREE to fit branch lengths to the tree.

    Parameters
    ----------
    aln : Alignment
        The sequence alignment.
    tree : PhyloNode
        The topology to fit branch lengths to.
    model : Model | str
        The substitution model with base frequencies and rate heterogeneity.
    num_threads: int | None, optional
        Number of threads for IQ-TREE to use, by default None (single-threaded).
        If 0 is specified, IQ-TREE attempts to find the optimal number of threads.
    bl_fixed: bool, optional
        If True, evaluates likelihood using the provided branch lengths on the tree.
        Branch lengths will be treated as constant in this case, with any unspecified
        branch lengths still being optimised. Otherwise if False, branch lengths are
        fitted to the tree whether provided or not. By default False.
    other_options: str, optional
        Additional command line options for IQ-TREE.

    Returns
    -------
    PhyloNode
        A phylogenetic tree with the same given topology fitted with branch lengths.

    """
    validate_other_options(other_options, INVALID_FIT_TREE_PARAMS)

    if isinstance(model, str):
        model = make_model(model)

    if num_threads is None:
        num_threads = 1

    names = aln.names
    seqs = [str(seq) for seq in aln.iter_seqs(names)]
    newick = get_newick(tree)

    yaml_result = yaml.safe_load(
        iq_fit_tree(
            names,
            seqs,
            str(model),
            newick,
            bl_fixed,
            0,
            num_threads,
            other_options,
        ),
    )
    return _process_tree_yaml(yaml_result, names, model)

Usage

For usage, see "Fit branch lengths to a tree topology from an alignment".