Skip to content

simulate_alignment

piqtree.simulate_alignment(tree, model, length=1000, rand_seed=None, insertion_rate=0.0, deletion_rate=0.0, insertion_size_distribution='POW{1.7/100}', deletion_size_distribution='POW{1.7/100}', root_seq=None, num_threads=None)

Uses AliSim to simulate an Alignment through IQ-TREE.

PARAMETER DESCRIPTION
tree

The tree to simulate an alignment over.

TYPE: PhyloNode

model

The substitution model with base frequencies and rate heterogeneity.

TYPE: Model | str

length

The length of the alignment, by default 1000. The alignment may be longer when an indel model is used, due to insertion events.

TYPE: int DEFAULT: 1000

rand_seed

The random seed - None means no seed is used, by default None.

TYPE: int | None DEFAULT: None

insertion_rate

The insertion rate relative to the substitution rate, by default 0.0.

TYPE: float DEFAULT: 0.0

deletion_rate

The deletion rate relative to the substitution rate, by default 0.0.

TYPE: float DEFAULT: 0.0

insertion_size_distribution

The insertion size distribution, by default the Zipfian distribution with a=1.7 and maximum size 100.

TYPE: IndelDistribution | str DEFAULT: 'POW{1.7/100}'

deletion_size_distribution

The deletion size distribution, by default the Zipfian distribution with a=1.7 and maximum size 100.

TYPE: IndelDistribution | str DEFAULT: 'POW{1.7/100}'

root_seq

The root sequence, by default None (a random root sequence).

TYPE: str | None DEFAULT: None

num_threads

Number of threads for IQ-TREE to use, by default None (single-threaded).

TYPE: int | None DEFAULT: None

RETURNS DESCRIPTION
Alignment

The simulated alignment.

Source code in src/piqtree/iqtree/_alignment.py
def simulate_alignment(
    tree: PhyloNode,
    model: Model | str,
    length: int = 1000,
    rand_seed: int | None = None,
    insertion_rate: float = 0.0,
    deletion_rate: float = 0.0,
    insertion_size_distribution: IndelDistribution | str = "POW{1.7/100}",
    deletion_size_distribution: IndelDistribution | str = "POW{1.7/100}",
    root_seq: str | None = None,
    num_threads: int | None = None,
) -> Alignment:
    """Uses AliSim to simulate an Alignment through IQ-TREE.

    Parameters
    ----------
    tree : PhyloNode
        The tree to simulate an alignment over.
    model : Model | str
        The substitution model with base frequencies and rate heterogeneity.
    length : int, optional
        The length of the alignment, by default 1000.
        The alignment may be longer when an indel model is used, due to
        insertion events.
    rand_seed : int | None, optional
        The random seed - None means no seed is used, by default None.
    insertion_rate : float, optional
        The insertion rate relative to the substitution rate, by default 0.0.
    deletion_rate : float, optional
        The deletion rate relative to the substitution rate, by default 0.0.
    insertion_size_distribution : IndelDistribution | str, optional
        The insertion size distribution, by default the Zipfian
        distribution with a=1.7 and maximum size 100.
    deletion_size_distribution : IndelDistribution | str, optional
        The deletion size distribution, by default the Zipfian
        distribution with a=1.7 and maximum size 100.
    root_seq : str | None, optional
        The root sequence, by default None (a random root sequence).
    num_threads : int | None, optional
        Number of threads for IQ-TREE to use, by default None (single-threaded).

    Returns
    -------
    Alignment
        The simulated alignment.

    """
    if rand_seed is None:
        rand_seed = make_rand_seed()

    if root_seq is None:
        root_seq = ""

    if num_threads is None:
        num_threads = 1

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

    if model.submod_type.base_model in UNSUPPORTED_MODELS:
        msg = f"Lie Model {cast('LieModel', model.submod_type.base_model).value} is unsupported."
        raise ValueError(msg)

    newick_tree = get_newick(tree)

    yaml_result = load_yaml(
        iq_simulate_alignment(
            tree=newick_tree,
            subst_model=str(model),
            rand_seed=rand_seed,
            partition_info="",
            partition_type="",
            seq_length=length,
            insertion_rate=insertion_rate,
            deletion_rate=deletion_rate,
            root_seq=root_seq,
            num_threads=num_threads,
            insertion_size_distribution=str(insertion_size_distribution),
            deletion_size_distribution=str(deletion_size_distribution),
            population_size=-1,
        ),
    )

    seqs = _parse_yaml_alignment(yaml_result["alignment"])

    return make_aligned_seqs(seqs, moltype=model.submod_type.get_moltype())

Usage

For usage, see "Simulate alignments with AliSim".