multree 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
multree-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ronen Shtein and co-authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,4 @@
1
+ # sdist extras (the wheel holds the package only)
2
+ graft tests
3
+ graft examples
4
+ global-exclude __pycache__ *.py[cod]
multree-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,144 @@
1
+ Metadata-Version: 2.4
2
+ Name: multree
3
+ Version: 1.0.0
4
+ Summary: Reticulate trees (MUL-trees and phylogenetic networks) and measures to compare them
5
+ Author: Ronen Shtein, Tom Ulanovski
6
+ License-Expression: GPL-3.0-or-later
7
+ Project-URL: Homepage, https://github.com/Roshex/multree
8
+ Project-URL: Repository, https://github.com/Roshex/multree
9
+ Project-URL: Issues, https://github.com/Roshex/multree/issues
10
+ Keywords: phylogenetics,phylogenetic network,MUL-tree,reticulation,polyploidy
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Operating System :: OS Independent
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: ete3>=3.1.2
20
+ Requires-Dist: six
21
+ Requires-Dist: legacy-cgi; python_version >= "3.13"
22
+ Requires-Dist: networkx>=2.8
23
+ Requires-Dist: numpy>=1.22
24
+ Requires-Dist: scipy>=1.8
25
+ Requires-Dist: pandas>=1.4
26
+ Requires-Dist: matplotlib>=3.5
27
+ Requires-Dist: seaborn>=0.12
28
+ Requires-Dist: pyvis>=0.3.1
29
+ Provides-Extra: mu
30
+ Requires-Dist: phylonetwork==2.2.2; python_version < "3.12" and extra == "mu"
31
+ Provides-Extra: test
32
+ Requires-Dist: pytest>=7; extra == "test"
33
+ Dynamic: license-file
34
+
35
+ # multree
36
+
37
+ Reticulate trees - multi-labelled trees (MUL-trees) and phylogenetic networks in extended Newick - and
38
+ measures to compare them: reticulation counts, ploidy differences, reticulation leaves and sisters
39
+ (Jaccard distances after Hungarian matching), the mu-distance, the graph edit distance, and pairwise
40
+ comparison tables and heatmaps.
41
+
42
+ Used by [GRANDMA](https://github.com/Roshex/Grandma), PhyloFold (`phylofold.evaluate`) and the benchmark of
43
+ polyploid network inference methods [polyploid-benchmark](https://github.com/tomulanovski/polyploid-benchmark/).
44
+ In GRANDMA this code was the `Reticulate_Tree` folder.
45
+
46
+ ## Installation
47
+
48
+ Python 3.10 or newer:
49
+
50
+ ```bash
51
+ pip install multree # once released on PyPI
52
+ pip install "git+https://github.com/Roshex/multree.git"
53
+ ```
54
+
55
+ The mu-distance needs `phylonetwork==2.2.2` (`pip install "multree[mu]"`). Its setup fails on
56
+ Python >= 3.12 (its `versioneer.py` uses `SafeConfigParser`): there, install it from the sdist with
57
+ that name changed to `ConfigParser`, or use Python 3.10/3.11. Without phylonetwork everything else
58
+ works; `pairwise_compare` then reports `mu_distance` as NaN and `mu_scored` as 0, with one warning.
59
+
60
+ ## Use
61
+
62
+ ```python
63
+ from multree.reticulate_tree import ReticulateTree
64
+ from multree.compare_reticulations import pairwise_compare
65
+
66
+ truth = ReticulateTree("((A,B),(C,(A,D)));", is_multree=True)
67
+ inferred = ReticulateTree("((A,B),(C,D));")
68
+ print(truth.get_reticulation_count(), truth.get_reticulation_leaves(), truth.get_reticulation_sisters())
69
+ print(pairwise_compare(truth, inferred)) # every metric; edit_distance=False skips the slowest
70
+ print(truth - inferred) # graph edit distance
71
+ ```
72
+
73
+ More in [examples/multree_demo.ipynb](https://github.com/Roshex/multree/blob/HEAD/examples/multree_demo.ipynb).
74
+ Tests: `pip install -e ".[test]"`, then `pytest`.
75
+
76
+ ## Contents
77
+
78
+ | file | contents |
79
+ | --- | --- |
80
+ | `reticulate_tree.py` | `ReticulateTree`: Newick / MUL-tree / extended Newick (#H) / ete3 / networkx input; MUL-tree -> network by HOLM folding (`multree_to_dag`) or Polyphest-style folding (`polyphest_multree_to_dag`); network -> MUL-tree; extended Newick out; reticulation count, leaves and sisters; edit distance (`get_edit_distance`, `a - b`, `get_edit_distance_multree`); mu-distance; plots |
81
+ | `compare_reticulations.py` | `pairwise_compare` and the many-network tables (`pairwise_comparison`, `one_stop_compare`), `are_identical`, heatmaps |
82
+ | `holm_python.py` | earlier standalone HOLM implementation, superseded by `multree_to_dag`; kept for reference |
83
+ | `tests/test_multree.py` | the pairwise checks of PhyloFold's `test_evaluate.py` on the package: both Jaccard modes, mu-distance, `num_rets_bias`, the `edit_distance` switch, `one_stop_compare`, and `visualize` / `visualize2` with `launch=False` |
84
+
85
+ ## Provenance
86
+
87
+ My original code with bug fixes and expansion by Tom Ulanovski in his benchmark copy `polyploid-benchmark`
88
+ is built and expanded, e.g. with the mu metric. This package is Tom's copy as the base,
89
+ with everything of my version that his copy had dropped restored, and one
90
+ bug fixed. It gives the same values as the copy that scored the benchmark tables (Polyphest,
91
+ GRAMPA-iter, AlloppNET, ...).
92
+
93
+ **Ronen** (original files): HOLM folding and the Polyphest-style folding,
94
+ `dag_to_multree`, extended Newick in/out, reticulation count / leaves /
95
+ sisters, leaf counts, the graph edit distance, `pairwise_compare` with
96
+ number of reticulations, ploidy difference (TP/FP/FN), reticulation
97
+ descendants and sisters Jaccard with Hungarian matching,
98
+ `pairwise_comparison`, `one_stop_compare`, `are_identical`, heatmaps,
99
+ `visualize`, `visualize2`, `interact`, `holm_python.py`.
100
+
101
+ **Tom** (benchmark additions):
102
+ - mu-distance: `get_mu_distance`, `get_mu_root_vector`, orchard certificate
103
+ (`is_orchard`, `orchard_certificate`), `compare_mu` -> `mu_distance`, `mu_scored`;
104
+ - `partial_match` for the Jaccard metrics (best-match only, no penalty for
105
+ unmatched reticulations); the benchmark uses it only for
106
+ `SINGLE_RETICULATION_METHODS = {'grampa'}`;
107
+ - `_matched` Jaccard variants, `polyploid_species_jaccard`, `num_rets_bias`;
108
+ - semantics: an autopolyploid node counts as a reticulation, with
109
+ `auto_events` / `allo_events` per node (`get_auto_event_count`,
110
+ `get_allo_event_count`); `compare_num_rets` returns `{'signed', 'abs'}`;
111
+ - `refold=True` (unfold a network to its MUL-tree and fold again);
112
+ `#` accepted in extended Newick labels; `get_edit_distance_multree`.
113
+
114
+ **Restored** (missing in Tom's copy):
115
+ - `visualize(filename=None, uid_labeled=False, launch=True)` (barycentric
116
+ layout) and `visualize2(..., launch=True)`; `launch=False` writes the HTML
117
+ without opening a browser (for clusters and tests);
118
+ - the edit distance in `pairwise_compare` (`edit_distance=True` by default;
119
+ `edit_distance=False` skips it, as Tom's scripts did, because it can
120
+ run for very long on large networks);
121
+ - the "Clustering A/B" example in `compare_reticulations.py`.
122
+
123
+ **Fixed:** `get_edit_distance(normalize=True, timeout=None)` took the
124
+ first value of `nx.optimize_graph_edit_distance`, which is only the first,
125
+ loose upper bound of an anytime search (identical networks written in a
126
+ different order scored 0.67). It now returns the exact minimum
127
+ (`nx.graph_edit_distance`), or the best value found within `timeout`
128
+ seconds. The GED inside the Polyphest-style folding is not checked yet.
129
+ `holm_python.py` imported a module by an old name; it now imports `ReticulateTree` from this
130
+ package. Its `__main__` demo still calls `get_number_of_special_nodes`, which does not exist.
131
+
132
+ **Changed as a package:** the modules import each other within the package (they also still work
133
+ as a plain folder on `sys.path`), and `compare_mu` treats a missing phylonetwork like a pair it
134
+ cannot score (NaN, `mu_scored` 0, one warning) instead of raising, since phylonetwork cannot be
135
+ pip-installed on Python >= 3.12.
136
+
137
+ ## Dependencies
138
+
139
+ ete3 (Python >= 3.13 also needs `legacy-cgi`, installed automatically), networkx, scipy, numpy,
140
+ pandas, matplotlib, seaborn, pyvis; phylonetwork 2.2.2 for the mu-distance (see Installation).
141
+
142
+ ## License
143
+
144
+ MIT License (see [LICENSE](https://github.com/Roshex/multree/blob/HEAD/LICENSE)).
@@ -0,0 +1,110 @@
1
+ # multree
2
+
3
+ Reticulate trees - multi-labelled trees (MUL-trees) and phylogenetic networks in extended Newick - and
4
+ measures to compare them: reticulation counts, ploidy differences, reticulation leaves and sisters
5
+ (Jaccard distances after Hungarian matching), the mu-distance, the graph edit distance, and pairwise
6
+ comparison tables and heatmaps.
7
+
8
+ Used by [GRANDMA](https://github.com/Roshex/Grandma), PhyloFold (`phylofold.evaluate`) and the benchmark of
9
+ polyploid network inference methods [polyploid-benchmark](https://github.com/tomulanovski/polyploid-benchmark/).
10
+ In GRANDMA this code was the `Reticulate_Tree` folder.
11
+
12
+ ## Installation
13
+
14
+ Python 3.10 or newer:
15
+
16
+ ```bash
17
+ pip install multree # once released on PyPI
18
+ pip install "git+https://github.com/Roshex/multree.git"
19
+ ```
20
+
21
+ The mu-distance needs `phylonetwork==2.2.2` (`pip install "multree[mu]"`). Its setup fails on
22
+ Python >= 3.12 (its `versioneer.py` uses `SafeConfigParser`): there, install it from the sdist with
23
+ that name changed to `ConfigParser`, or use Python 3.10/3.11. Without phylonetwork everything else
24
+ works; `pairwise_compare` then reports `mu_distance` as NaN and `mu_scored` as 0, with one warning.
25
+
26
+ ## Use
27
+
28
+ ```python
29
+ from multree.reticulate_tree import ReticulateTree
30
+ from multree.compare_reticulations import pairwise_compare
31
+
32
+ truth = ReticulateTree("((A,B),(C,(A,D)));", is_multree=True)
33
+ inferred = ReticulateTree("((A,B),(C,D));")
34
+ print(truth.get_reticulation_count(), truth.get_reticulation_leaves(), truth.get_reticulation_sisters())
35
+ print(pairwise_compare(truth, inferred)) # every metric; edit_distance=False skips the slowest
36
+ print(truth - inferred) # graph edit distance
37
+ ```
38
+
39
+ More in [examples/multree_demo.ipynb](https://github.com/Roshex/multree/blob/HEAD/examples/multree_demo.ipynb).
40
+ Tests: `pip install -e ".[test]"`, then `pytest`.
41
+
42
+ ## Contents
43
+
44
+ | file | contents |
45
+ | --- | --- |
46
+ | `reticulate_tree.py` | `ReticulateTree`: Newick / MUL-tree / extended Newick (#H) / ete3 / networkx input; MUL-tree -> network by HOLM folding (`multree_to_dag`) or Polyphest-style folding (`polyphest_multree_to_dag`); network -> MUL-tree; extended Newick out; reticulation count, leaves and sisters; edit distance (`get_edit_distance`, `a - b`, `get_edit_distance_multree`); mu-distance; plots |
47
+ | `compare_reticulations.py` | `pairwise_compare` and the many-network tables (`pairwise_comparison`, `one_stop_compare`), `are_identical`, heatmaps |
48
+ | `holm_python.py` | earlier standalone HOLM implementation, superseded by `multree_to_dag`; kept for reference |
49
+ | `tests/test_multree.py` | the pairwise checks of PhyloFold's `test_evaluate.py` on the package: both Jaccard modes, mu-distance, `num_rets_bias`, the `edit_distance` switch, `one_stop_compare`, and `visualize` / `visualize2` with `launch=False` |
50
+
51
+ ## Provenance
52
+
53
+ My original code with bug fixes and expansion by Tom Ulanovski in his benchmark copy `polyploid-benchmark`
54
+ is built and expanded, e.g. with the mu metric. This package is Tom's copy as the base,
55
+ with everything of my version that his copy had dropped restored, and one
56
+ bug fixed. It gives the same values as the copy that scored the benchmark tables (Polyphest,
57
+ GRAMPA-iter, AlloppNET, ...).
58
+
59
+ **Ronen** (original files): HOLM folding and the Polyphest-style folding,
60
+ `dag_to_multree`, extended Newick in/out, reticulation count / leaves /
61
+ sisters, leaf counts, the graph edit distance, `pairwise_compare` with
62
+ number of reticulations, ploidy difference (TP/FP/FN), reticulation
63
+ descendants and sisters Jaccard with Hungarian matching,
64
+ `pairwise_comparison`, `one_stop_compare`, `are_identical`, heatmaps,
65
+ `visualize`, `visualize2`, `interact`, `holm_python.py`.
66
+
67
+ **Tom** (benchmark additions):
68
+ - mu-distance: `get_mu_distance`, `get_mu_root_vector`, orchard certificate
69
+ (`is_orchard`, `orchard_certificate`), `compare_mu` -> `mu_distance`, `mu_scored`;
70
+ - `partial_match` for the Jaccard metrics (best-match only, no penalty for
71
+ unmatched reticulations); the benchmark uses it only for
72
+ `SINGLE_RETICULATION_METHODS = {'grampa'}`;
73
+ - `_matched` Jaccard variants, `polyploid_species_jaccard`, `num_rets_bias`;
74
+ - semantics: an autopolyploid node counts as a reticulation, with
75
+ `auto_events` / `allo_events` per node (`get_auto_event_count`,
76
+ `get_allo_event_count`); `compare_num_rets` returns `{'signed', 'abs'}`;
77
+ - `refold=True` (unfold a network to its MUL-tree and fold again);
78
+ `#` accepted in extended Newick labels; `get_edit_distance_multree`.
79
+
80
+ **Restored** (missing in Tom's copy):
81
+ - `visualize(filename=None, uid_labeled=False, launch=True)` (barycentric
82
+ layout) and `visualize2(..., launch=True)`; `launch=False` writes the HTML
83
+ without opening a browser (for clusters and tests);
84
+ - the edit distance in `pairwise_compare` (`edit_distance=True` by default;
85
+ `edit_distance=False` skips it, as Tom's scripts did, because it can
86
+ run for very long on large networks);
87
+ - the "Clustering A/B" example in `compare_reticulations.py`.
88
+
89
+ **Fixed:** `get_edit_distance(normalize=True, timeout=None)` took the
90
+ first value of `nx.optimize_graph_edit_distance`, which is only the first,
91
+ loose upper bound of an anytime search (identical networks written in a
92
+ different order scored 0.67). It now returns the exact minimum
93
+ (`nx.graph_edit_distance`), or the best value found within `timeout`
94
+ seconds. The GED inside the Polyphest-style folding is not checked yet.
95
+ `holm_python.py` imported a module by an old name; it now imports `ReticulateTree` from this
96
+ package. Its `__main__` demo still calls `get_number_of_special_nodes`, which does not exist.
97
+
98
+ **Changed as a package:** the modules import each other within the package (they also still work
99
+ as a plain folder on `sys.path`), and `compare_mu` treats a missing phylonetwork like a pair it
100
+ cannot score (NaN, `mu_scored` 0, one warning) instead of raising, since phylonetwork cannot be
101
+ pip-installed on Python >= 3.12.
102
+
103
+ ## Dependencies
104
+
105
+ ete3 (Python >= 3.13 also needs `legacy-cgi`, installed automatically), networkx, scipy, numpy,
106
+ pandas, matplotlib, seaborn, pyvis; phylonetwork 2.2.2 for the mu-distance (see Installation).
107
+
108
+ ## License
109
+
110
+ MIT License (see [LICENSE](https://github.com/Roshex/multree/blob/HEAD/LICENSE)).
@@ -0,0 +1,315 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "249cca73",
6
+ "metadata": {},
7
+ "source": [
8
+ "# ⚠️ Remaining Issues:\n",
9
+ "1. GED (in RT dist measure)\n",
10
+ "* needs fixing: internal nodes mismatching effects the score in unexpected ways.\n",
11
+ "2. TBD"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "markdown",
16
+ "id": "5b5bd5af",
17
+ "metadata": {},
18
+ "source": [
19
+ "# Imports"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": null,
25
+ "id": "43a2cc38",
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "from multree.reticulate_tree import ReticulateTree\n",
30
+ "from multree import compare_reticulations as cr\n",
31
+ "import importlib\n",
32
+ "import pandas as pd\n"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "markdown",
37
+ "id": "3e332c65",
38
+ "metadata": {},
39
+ "source": [
40
+ "# Example usages of ReticulateTree"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": null,
46
+ "id": "03f3955c",
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "# Test the enewick input case:\n",
51
+ "test_enewick = '((a,(e)#H1:0),(#H1:0,b));'\n",
52
+ "# '((KBNS10,(SRR27702572,(SRR27702568,(((((KPIS19,(KSFS35)#H8),(KMTS31)#H6),(KRBS33)#H2),(KHUS06,(PZ382,(KCOS43,(KGUS05,(KPXS21)#H0))))),(KCUS03,(KVNS23)#H4))))),((((#H6,#H8),#H2),#H4),#H0));'\n",
53
+ "obj = ReticulateTree(test_enewick)\n",
54
+ "print(f'Parsed ETE3 tree from enriched Newick: {obj.tree_str}\\n')\n",
55
+ "obj.visualize()\n",
56
+ "print(obj.to_enewick())\n"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "markdown",
61
+ "id": "189c7bf2",
62
+ "metadata": {},
63
+ "source": [
64
+ "# Debug and usage of some compare funcs"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "code",
69
+ "execution_count": null,
70
+ "id": "146fef93",
71
+ "metadata": {},
72
+ "outputs": [],
73
+ "source": [
74
+ "input_A = {'a':2,'b':2,'c':1,'d':1,'e':1}\n",
75
+ "input_B = {'a':1,'b':2,'c':3,'d':1,'e':1}\n",
76
+ "res = cr.compare_ploidy_diff(input_A, input_B)\n",
77
+ "print(f'Ploidy Jaccard test result: {res}')\n",
78
+ "# We expect: {'dist': 0.75, 'FP': 2, 'FN': 1, 'TP': 1}\n",
79
+ "# out of 4 extra copies (1+1+2+0+0), 2 is correct (from b), 2 are false positive (both from c), 1 is false negative (from a)\n",
80
+ "# therefore the distance is 0.75 i.e., 3/4 differ = 1 - (1/4 match)"
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "execution_count": null,
86
+ "id": "6d162917",
87
+ "metadata": {},
88
+ "outputs": [],
89
+ "source": [
90
+ "input_lA = {'1': ['z', 'y', 'x'], '2': ['w']}\n",
91
+ "input_sA = {'1': [{'a'}, {'b'}], '2': [{'c'}, {'d'}]}\n",
92
+ "\n",
93
+ "input_lB = {'3': ['x', 'y', 'w', 'z'], '4': ['w']}\n",
94
+ "input_sB = {'3': [{'d', 'w', 'c'}, {'a'}], '4': [{'c'}, {'y', 'z'}]}\n",
95
+ "\n",
96
+ "res = cr.match_and_compare(input_lA, input_lB)\n",
97
+ "print(f'Reticulation leaves test result: {res}')\n",
98
+ "\n",
99
+ "res = cr.match_and_compare(input_lA, input_lB, input_sA, input_sB)\n",
100
+ "print(f'Reticulation sisters test result: {res}')"
101
+ ]
102
+ },
103
+ {
104
+ "cell_type": "code",
105
+ "execution_count": null,
106
+ "id": "bf81e6a3",
107
+ "metadata": {},
108
+ "outputs": [],
109
+ "source": [
110
+ "input1, input2 = {1:{'w', 'y'}, 2:{'c'}, 3:{'u', 'a', 'b'}, 4:{'z'}}, {1:{'y', 'z', 'x'}, 2:{'c'}, 3:{'a', 'b'}, 4:{'y', 'z'}, 5:{'u'}}\n",
111
+ "\n",
112
+ "#input1, input2 = [{'c'}, {'u', 'a', 'b'}], [{'c'}, {'a', 'b'}, {'u'}]\n",
113
+ "\n",
114
+ "cr.match_and_compare(input1, input2, debug=True)\n",
115
+ "\n",
116
+ "# should u match to a,b,u too?"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "code",
121
+ "execution_count": null,
122
+ "id": "cfb9ca53",
123
+ "metadata": {},
124
+ "outputs": [],
125
+ "source": [
126
+ "input_lA = {'1': ['z', 'y', 'x'], '2': ['w'], '3': ['u', 'v']}\n",
127
+ "input_sA = {'1': [{'a'}, {'b'}], '2': [{'c'}, {'d', 'y'}], '3': [{'u'}, {'v'}]}\n",
128
+ "\n",
129
+ "input_lB = {'3': ['x', 'y', 'w', 'z'], '4': ['w']}\n",
130
+ "input_sB = {'3': [{'d', 'w', 'c'}, {'a'}, {'u'}], '4': [{'c'}, {'y', 'z'}]}\n",
131
+ "\n",
132
+ "res = cr.match_and_compare(input_lA, input_lB, input_sA, input_sB, debug=True)\n",
133
+ "print(f'Reticulation sisters test result: {res}')"
134
+ ]
135
+ },
136
+ {
137
+ "cell_type": "markdown",
138
+ "id": "76623d07",
139
+ "metadata": {},
140
+ "source": [
141
+ "# Refresh lib"
142
+ ]
143
+ },
144
+ {
145
+ "cell_type": "code",
146
+ "execution_count": null,
147
+ "id": "09ab04b1",
148
+ "metadata": {},
149
+ "outputs": [],
150
+ "source": [
151
+ "importlib.reload(cr)"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "markdown",
156
+ "id": "c14cf1e2",
157
+ "metadata": {},
158
+ "source": [
159
+ "# Run all step by step"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": null,
165
+ "id": "4377e3cf",
166
+ "metadata": {},
167
+ "outputs": [],
168
+ "source": [
169
+ "# Test a family of MUL-trees derived from the same single-labeled tree (all kinds of reticulation placements):\n",
170
+ "phylo_trees = {\n",
171
+ " 'no_reticules': '(((c,w),d)Q,((a,(x,(y,z))),b));',\n",
172
+ " 'no_intercladic': '(((c,w),(d,w)),((a,(x,(y,z))),(b,(x,(y,z)))));',\n",
173
+ " 'no_nested': '((((c,w),(d,w))Q,(x,(y,z))),((a,(x,(y,z))),b));',\n",
174
+ " 'semi_nested': '((((c,w),d),(x,((y,z),w))),((a,(x,((y,z),w))),b));',\n",
175
+ " 'once_nested': '(((c,d),((x,w),((y,z),w))),((a,((x,w),((y,z),w))),b));',\n",
176
+ " 'semi_broken1': '((((c,w),d),(x,(y,z))),((a,(x,((y,z),w))),b));',\n",
177
+ " 'semi_broken2': '((((c,w),d),(x,((y,z),w))),((a,(x,(y,z))),b));',\n",
178
+ " 'semi_shifted1': '((((c,w),d),(x,(y,(z,w)))),((a,(x,((y,z),w))),b));',\n",
179
+ " 'semi_shifted2': '((((c,w),d),(x,((y,z),w))),((a,((x,(y,z)),w)),b));',\n",
180
+ "}\n",
181
+ "\n",
182
+ "phylo_trees_data = []\n",
183
+ "for name, newick_str in phylo_trees.items():\n",
184
+ "\n",
185
+ " # MUL-tree string parsing tests\n",
186
+ " phylo_trees[name] = ReticulateTree(newick_str)\n",
187
+ " curr_obj = phylo_trees[name]\n",
188
+ " print(f'Name: {name} - newick: {curr_obj.tree_str}')\n",
189
+ "\n",
190
+ " # Back-conversion test:\n",
191
+ " dag = curr_obj.dag\n",
192
+ " print(f'Nodes: {dag.number_of_nodes()}, Edges: {dag.number_of_edges()}')\n",
193
+ " back_tree = ReticulateTree.dag_to_multree(dag)\n",
194
+ " #print(f'Converted back to tree: {back_tree.write(format=8)}')\n",
195
+ "\n",
196
+ " # Check if the back-converted tree matches the original\n",
197
+ " print(f'Is back-converted tree identical to original? {cr.are_identical(curr_obj, ReticulateTree(back_tree))}')\n",
198
+ "\n",
199
+ " # Measure the object and check printout\n",
200
+ " object_measurements = curr_obj.measure(printout=True)\n",
201
+ "\n",
202
+ " phylo_trees_data.append({\n",
203
+ " 'name': name,\n",
204
+ " 'object': curr_obj,\n",
205
+ " **object_measurements\n",
206
+ " })\n",
207
+ "\n",
208
+ " # Visualize the object\n",
209
+ " curr_obj.visualize()#main_path / f'{name}_dag.png')\n",
210
+ "\n",
211
+ " # Interactive visualization (if needed)\n",
212
+ " #curr_obj.interact(main_path / f'{name}_interactive.html', launch=True)\n",
213
+ "\n",
214
+ " print('\\n')"
215
+ ]
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "execution_count": null,
220
+ "id": "e91a55c9",
221
+ "metadata": {},
222
+ "outputs": [],
223
+ "source": [
224
+ "# Create a DataFrame for the phylogenetic trees data\n",
225
+ "phylo_df = pd.DataFrame(phylo_trees_data).set_index('name')"
226
+ ]
227
+ },
228
+ {
229
+ "cell_type": "code",
230
+ "execution_count": null,
231
+ "id": "42a8685f",
232
+ "metadata": {},
233
+ "outputs": [],
234
+ "source": [
235
+ "# Generate pairwise comparison matrices\n",
236
+ "comparison_matrices = cr.pairwise_comparison(phylo_df)\n",
237
+ "\n",
238
+ "for metric, matrix in comparison_matrices.items():\n",
239
+ " print(f'\\n{metric.upper()} matrix:')\n",
240
+ " print(matrix.round(3))"
241
+ ]
242
+ },
243
+ {
244
+ "cell_type": "code",
245
+ "execution_count": null,
246
+ "id": "529178d8",
247
+ "metadata": {},
248
+ "outputs": [],
249
+ "source": [
250
+ "cr.plot_comparison_heatmaps(comparison_matrices, phylo_df)"
251
+ ]
252
+ },
253
+ {
254
+ "cell_type": "markdown",
255
+ "id": "a2d9c289",
256
+ "metadata": {},
257
+ "source": [
258
+ "# One stop shop"
259
+ ]
260
+ },
261
+ {
262
+ "cell_type": "code",
263
+ "execution_count": null,
264
+ "id": "055891f6",
265
+ "metadata": {},
266
+ "outputs": [],
267
+ "source": [
268
+ "phylo_trees = {\n",
269
+ " 'no_reticules': '(((c,w),d)Q,((a,(x,(y,z))),b));',\n",
270
+ " 'no_intercladic': '(((c,w),(d,w)),((a,(x,(y,z))),(b,(x,(y,z)))));',\n",
271
+ " 'no_nested': '((((c,w),(d,w))Q,(x,(y,z))),((a,(x,(y,z))),b));',\n",
272
+ " 'semi_nested': '((((c,w),d),(x,((y,z),w))),((a,(x,((y,z),w))),b));',\n",
273
+ " 'once_nested': '(((c,d),((x,w),((y,z),w))),((a,((x,w),((y,z),w))),b));',\n",
274
+ " 'semi_broken1': '((((c,w),d),(x,(y,z))),((a,(x,((y,z),w))),b));',\n",
275
+ " 'semi_broken2': '((((c,w),d),(x,((y,z),w))),((a,(x,(y,z))),b));',\n",
276
+ " 'semi_shifted1': '((((c,w),d),(x,(y,(z,w)))),((a,(x,((y,z),w))),b));',\n",
277
+ " 'semi_shifted2': '((((c,w),d),(x,((y,z),w))),((a,((x,(y,z)),w)),b));',\n",
278
+ "}\n",
279
+ "\n",
280
+ "results = cr.one_stop_compare(phylo_trees, printout=False)"
281
+ ]
282
+ },
283
+ {
284
+ "cell_type": "code",
285
+ "execution_count": null,
286
+ "id": "45689b1d",
287
+ "metadata": {},
288
+ "outputs": [],
289
+ "source": [
290
+ "cr.plot_comparison_heatmaps(results['comparisons'], results['data'])"
291
+ ]
292
+ }
293
+ ],
294
+ "metadata": {
295
+ "kernelspec": {
296
+ "display_name": "learn",
297
+ "language": "python",
298
+ "name": "python3"
299
+ },
300
+ "language_info": {
301
+ "codemirror_mode": {
302
+ "name": "ipython",
303
+ "version": 3
304
+ },
305
+ "file_extension": ".py",
306
+ "mimetype": "text/x-python",
307
+ "name": "python",
308
+ "nbconvert_exporter": "python",
309
+ "pygments_lexer": "ipython3",
310
+ "version": "3.12.7"
311
+ }
312
+ },
313
+ "nbformat": 4,
314
+ "nbformat_minor": 5
315
+ }
@@ -0,0 +1,7 @@
1
+ """multree: reticulate trees (MUL-trees and phylogenetic networks) and measures to compare them.
2
+
3
+ from multree.reticulate_tree import ReticulateTree
4
+ from multree import compare_reticulations
5
+ """
6
+
7
+ __version__ = "1.0.0"