Skip to content

Bump iqtree2 from 18fa5a3 to ea7a080 #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion iqtree2
13 changes: 12 additions & 1 deletion src/piqtree2/_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(
invariant_sites: bool = False,
rand_seed: int | None = None,
bootstrap_reps: int | None = None,
num_threads: int | None = None,
) -> None:
self._model = Model(
submod_type=submod_type,
Expand All @@ -39,6 +40,7 @@ def __init__(
)
self._rand_seed = rand_seed
self._bootstrap_reps = bootstrap_reps
self._num_threads = num_threads

def main(
self,
Expand All @@ -49,6 +51,7 @@ def main(
self._model,
self._rand_seed,
bootstrap_replicates=self._bootstrap_reps,
num_threads=self._num_threads,
)


Expand All @@ -63,6 +66,7 @@ def __init__(
rate_model: str | None = None,
*,
rand_seed: int | None = None,
num_threads: int | None = None,
invariant_sites: bool = False,
) -> None:
self._tree = tree
Expand All @@ -73,12 +77,19 @@ def __init__(
freq_type=freq_type,
)
self._rand_seed = rand_seed
self._num_threads = num_threads

def main(
self,
aln: c3_types.AlignedSeqsType,
) -> cogent3.PhyloNode | cogent3.app.typing.SerialisableType:
return fit_tree(aln, self._tree, self._model, self._rand_seed)
return fit_tree(
aln,
self._tree,
self._model,
self._rand_seed,
self._num_threads,
)


@composable.define_app
Expand Down
9 changes: 6 additions & 3 deletions src/piqtree2/_libiqtree/_piqtree2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ extern string build_tree(vector<string>& names,
vector<string>& seqs,
string model,
int rand_seed = 0,
int bootstrap_rep = 0);
int bootstrap_rep = 0,
int num_thres = 1);

/*
* Perform phylogenetic analysis on the input alignment
Expand All @@ -43,7 +44,8 @@ extern string fit_tree(vector<string>& names,
vector<string>& seqs,
string model,
string intree,
int rand_seed = 0);
int rand_seed = 0,
int num_thres = 1);

/*
* Perform phylogenetic analysis with ModelFinder
Expand All @@ -59,7 +61,8 @@ extern string modelfinder(vector<string>& names,
int rand_seed = 0,
string model_set = "",
string freq_set = "",
string rate_set = "");
string rate_set = "",
int num_thres = 1);

/*
* Build pairwise JC distance matrix
Expand Down
5 changes: 5 additions & 0 deletions src/piqtree2/iqtree/_model_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ def model_finder(
freq_set: Iterable[str] | None = None,
rate_set: Iterable[str] | None = None,
rand_seed: int | None = None,
num_threads: int | None = None,
) -> ModelFinderResult | c3_types.SerialisableType:
source = aln.info.source
if rand_seed is None:
rand_seed = 0 # The default rand_seed in IQ-TREE

if num_threads is None:
num_threads = 1

if model_set is None:
model_set = set()
if freq_set is None:
Expand All @@ -111,6 +115,7 @@ def model_finder(
",".join(model_set),
",".join(freq_set),
",".join(rate_set),
num_threads,
),
)
return ModelFinderResult(raw_data=raw, source=source)
23 changes: 21 additions & 2 deletions src/piqtree2/iqtree/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def build_tree(
model: Model,
rand_seed: int | None = None,
bootstrap_replicates: int | None = None,
num_threads: int | None = None,
) -> cogent3.PhyloNode:
"""Reconstruct a phylogenetic tree.

Expand All @@ -216,6 +217,8 @@ def build_tree(
The number of bootstrap replicates to perform, by default None.
If 0 is provided, then no bootstrapping is performed.
At least 1000 is required to perform bootstrapping.
num_threads: int | None, optional
Number of threads for IQ-TREE 2 to use, by default None (single-threaded).

Returns
-------
Expand All @@ -229,11 +232,21 @@ def build_tree(
if bootstrap_replicates is None:
bootstrap_replicates = 0

if num_threads is None:
num_threads = 1

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

yaml_result = yaml.safe_load(
iq_build_tree(names, seqs, str(model), rand_seed, bootstrap_replicates),
iq_build_tree(
names,
seqs,
str(model),
rand_seed,
bootstrap_replicates,
num_threads,
),
)
tree = _process_tree_yaml(yaml_result, names)

Expand All @@ -249,6 +262,7 @@ def fit_tree(
tree: cogent3.PhyloNode,
model: Model,
rand_seed: int | None = None,
num_threads: int | None = None,
) -> cogent3.PhyloNode:
"""Fit branch lengths to a tree.

Expand All @@ -265,6 +279,8 @@ def fit_tree(
The substitution model with base frequencies and rate heterogeneity.
rand_seed : int | None, optional
The random seed - 0 or None means no seed, by default None.
num_threads: int | None, optional
Number of threads for IQ-TREE 2 to use, by default None (single-threaded).

Returns
-------
Expand All @@ -275,12 +291,15 @@ def fit_tree(
if rand_seed is None:
rand_seed = 0 # The default rand_seed in IQ-TREE

if num_threads is None:
num_threads = 1

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

yaml_result = yaml.safe_load(
iq_fit_tree(names, seqs, str(model), newick, rand_seed),
iq_fit_tree(names, seqs, str(model), newick, rand_seed, num_threads),
)
tree = _process_tree_yaml(yaml_result, names)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_iqtree/test_build_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ def test_build_tree_inadequate_bootstrapping(four_otu: ArrayAlignment) -> None:

def test_build_tree_bootstrapping(four_otu: ArrayAlignment) -> None:
tree = piqtree2.build_tree(four_otu, Model(DnaModel.GTR), bootstrap_replicates=1000)
# internal nodes are now named by default
supported_node = tree.get_node_matching_name("node1")

supported_node = max(tree.children, key=lambda x: len(x.children))
assert "support" in supported_node.params