Skip to content

make_model

piqtree.make_model(iqtree_str)

Convert an IQ-TREE model specification into a Model class.

PARAMETER DESCRIPTION
iqtree_str

The IQ-TREE model string.

TYPE: str

RETURNS DESCRIPTION
Model

The equivalent Model class.

Source code in src/piqtree/model/_model.py
def make_model(iqtree_str: str) -> Model:
    """Convert an IQ-TREE model specification into a Model class.

    Parameters
    ----------
    iqtree_str : str
        The IQ-TREE model string.

    Returns
    -------
    Model
        The equivalent Model class.

    """
    if "+" not in iqtree_str:
        return Model(iqtree_str)

    sub_mod_str, components = iqtree_str.split("+", maxsplit=1)

    freq_type = None
    invariable_sites: float | bool | None = None
    rate_model = None

    for component in components.split("+"):
        if component.startswith("F"):
            if freq_type is not None:
                msg = f"Model {iqtree_str!r} contains multiple base frequency specifications."
                raise ValueError(msg)
            freq_type = component
        elif component.startswith("I"):
            if invariable_sites is not None:
                msg = f"Model {iqtree_str!r} contains multiple specifications for invariable sites."
                raise ValueError(msg)
            invariable_sites = _parse_invariable_sites(component)

        elif component.startswith(("G", "R")):
            if rate_model is not None:
                msg = f"Model {iqtree_str!r} contains multiple rate heterogeneity specifications."
                raise ValueError(msg)
            rate_model = component
        else:
            msg = f"Model {iqtree_str!r} contains unexpected component."
            raise ValueError(msg)

    if invariable_sites is None:
        invariable_sites = False

    return Model(sub_mod_str, freq_type, rate_model, invariable_sites=invariable_sites)

Usage

For usage, see "Use different kinds of substitution models".