Skip to content

ENH: add piqtree_nj as a cogent3 quick_tree hook #170

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 2 commits into from
Mar 17, 2025
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
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ piqtree_jc_dists = "piqtree._app:piqtree_jc_dists"
piqtree_nj = "piqtree._app:piqtree_nj"
piqtree_mfinder = "piqtree._app:piqtree_mfinder"

[project.entry-points."cogent3.hook"]
quick_tree = "piqtree._app:piqtree_nj"

[tool.setuptools.dynamic]
version = {attr = "piqtree.__version__"}

Expand Down
4 changes: 3 additions & 1 deletion src/piqtree/_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def main(
@composable.define_app
@extend_docstring_from(nj_tree)
def piqtree_nj(dists: c3_types.PairwiseDistanceType) -> cogent3.PhyloNode:
return nj_tree(dists)
tree = nj_tree(dists)
tree.params |= {"provenance": "piqtree"}
return tree


@composable.define_app
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ def DATA_DIR() -> pathlib.Path:

@pytest.fixture
def three_otu(DATA_DIR: pathlib.Path) -> ArrayAlignment:
aln = load_aligned_seqs(DATA_DIR / "example.fasta", moltype="dna")
aln = load_aligned_seqs(DATA_DIR / "example.fasta", moltype="dna", new_type=True)
aln = aln.take_seqs(["Human", "Rhesus", "Mouse"])
return aln.omit_gap_pos(allowed_gap_frac=0)


@pytest.fixture
def four_otu(DATA_DIR: pathlib.Path) -> ArrayAlignment:
aln = load_aligned_seqs(DATA_DIR / "example.fasta", moltype="dna")
aln = load_aligned_seqs(DATA_DIR / "example.fasta", moltype="dna", new_type=True)
aln = aln.take_seqs(["Human", "Chimpanzee", "Rhesus", "Mouse"])
return aln.omit_gap_pos(allowed_gap_frac=0)


@pytest.fixture
def five_otu(DATA_DIR: pathlib.Path) -> ArrayAlignment:
aln = load_aligned_seqs(DATA_DIR / "example.fasta", moltype="dna")
aln = load_aligned_seqs(DATA_DIR / "example.fasta", moltype="dna", new_type=True)
aln = aln.take_seqs(["Human", "Chimpanzee", "Rhesus", "Manatee", "Dugong"])
return aln.omit_gap_pos(allowed_gap_frac=0)
24 changes: 16 additions & 8 deletions tests/test_app/test_app.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import pytest
from cogent3 import ArrayAlignment, get_app, make_tree
from cogent3 import __version__ as cogent3_vers
from cogent3 import get_app, make_tree
from cogent3.core.new_alignment import Alignment

import piqtree
from piqtree import jc_distances


def test_piqtree_phylo(four_otu: ArrayAlignment) -> None:
def test_piqtree_phylo(four_otu: Alignment) -> None:
expected = make_tree("(Human,Chimpanzee,(Rhesus,Mouse));")
app = get_app("piqtree_phylo", submod_type="JC")
got = app(four_otu)
assert expected.same_topology(got)


def test_piqtree_phylo_support(four_otu: ArrayAlignment) -> None:
def test_piqtree_phylo_support(four_otu: Alignment) -> None:
app = get_app("piqtree_phylo", submod_type="JC", bootstrap_reps=1000)
got = app(four_otu)
supports = [
Expand All @@ -23,7 +25,7 @@ def test_piqtree_phylo_support(four_otu: ArrayAlignment) -> None:
assert all(supports)


def test_piqtree_fit(three_otu: ArrayAlignment) -> None:
def test_piqtree_fit(three_otu: Alignment) -> None:
tree = make_tree(tip_names=three_otu.names)
app = get_app("model", "JC69", tree=tree)
expected = app(three_otu)
Expand Down Expand Up @@ -53,7 +55,7 @@ def test_piqtree_random_trees(
assert len(tree.tips()) == num_taxa


def test_piqtree_jc_distances(five_otu: ArrayAlignment) -> None:
def test_piqtree_jc_distances(five_otu: Alignment) -> None:
app = get_app("piqtree_jc_dists")
dists = app(five_otu)

Expand All @@ -79,7 +81,7 @@ def test_piqtree_jc_distances(five_otu: ArrayAlignment) -> None:
) # dugong closer than rhesus


def test_piqtree_nj(five_otu: ArrayAlignment) -> None:
def test_piqtree_nj(five_otu: Alignment) -> None:
dists = jc_distances(five_otu)

expected = make_tree("(((Human, Chimpanzee), Rhesus), Manatee, Dugong);")
Expand All @@ -91,15 +93,15 @@ def test_piqtree_nj(five_otu: ArrayAlignment) -> None:
assert expected.same_topology(actual)


def test_mfinder(five_otu: ArrayAlignment) -> None:
def test_mfinder(five_otu: Alignment) -> None:
from piqtree.iqtree import ModelFinderResult

app = get_app("piqtree_mfinder")
got = app(five_otu)
assert isinstance(got, ModelFinderResult)


def test_mfinder_result_roundtrip(five_otu: ArrayAlignment) -> None:
def test_mfinder_result_roundtrip(five_otu: Alignment) -> None:
from piqtree.iqtree import ModelFinderResult

app = get_app("piqtree_mfinder")
Expand All @@ -108,3 +110,9 @@ def test_mfinder_result_roundtrip(five_otu: ArrayAlignment) -> None:
inflated = ModelFinderResult.from_rich_dict(rd)
assert isinstance(inflated, ModelFinderResult)
assert str(got.best_aicc) == str(inflated.best_aicc)

@pytest.mark.skipif(cogent3_vers < "2025.3.1", reason="requires cogent3 >= 2025.3.1")
@pytest.mark.parametrize("use_hook", [None, "piqtree"])
def test_quick_tree_hook(four_otu: Alignment, use_hook: str | None) -> None:
tree = four_otu.quick_tree(use_hook=use_hook)
assert tree.params["provenance"] == "piqtree"