1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
import pathlib
import pytest
from cogent3 import make_tree
from cogent3.phylo.tree_distance import (
lin_rajan_moret,
matching_cluster_distance,
rooted_robinson_foulds,
unrooted_robinson_foulds,
)
ROOTED_DISTANCE_MEASURES = matching_cluster_distance, rooted_robinson_foulds
UNROOTED_DISTANCE_MEASURES = lin_rajan_moret, unrooted_robinson_foulds
# unrooted rf distance
def test_unrooted_rf_different_trees():
a = make_tree(treestring="(a, b, (c, (d, e)));")
# There are two splits not in common for the two trees
# abc-de in tree a, and ad-dce in tree b.
b = make_tree(treestring="(a, b, (d, (c, e)));")
distance = unrooted_robinson_foulds(a, b)
assert distance == 2
distance = unrooted_robinson_foulds(b, a)
assert distance == 2
# Identical unrooted tree as before, but a different orientation.
b = make_tree(treestring="((a, b), d, (c, e));")
distance = unrooted_robinson_foulds(a, b)
assert distance == 2
distance = unrooted_robinson_foulds(b, a)
assert distance == 2
# Identical unrooted tree as before, but a different orientation.
b = make_tree(treestring="(((a, b), d), c, e);")
distance = unrooted_robinson_foulds(a, b)
assert distance == 2
distance = unrooted_robinson_foulds(b, a)
assert distance == 2
a = make_tree(treestring="(a, (b, (c, d)), (e, (f, (g, h))));")
b = make_tree(treestring="(a, (c, (b, d)), (h, (f, (g, e))));")
# There are six splits not in common.
# For tree a they are cd-abefgh, abcdef-gh, and abcde-fgh
# For tree b they are bd-acefgh, abcdhf-ge, and abcdh-fge
distance = unrooted_robinson_foulds(a, b)
assert distance == 6
distance = unrooted_robinson_foulds(b, a)
assert distance == 6
def test_unrooted_rf_same_tree():
# Topology of the unrooted trees are identical
# though they are stored in different orientations.
a = make_tree(treestring="(a, b, (c, d));")
b = make_tree(treestring="(c, d, (a, b));")
distance = unrooted_robinson_foulds(a, b)
assert distance == 0
distance = unrooted_robinson_foulds(b, a)
assert distance == 0
def test_unrooted_rf_distance_multifurcation():
a = make_tree(treestring="(a, b, (c, (d, e)));")
b = make_tree(treestring="(a, b, (c, d, e));")
# There is one split not in common.
# Tree a: abc-de
distance = unrooted_robinson_foulds(a, b)
assert distance == 1
distance = unrooted_robinson_foulds(b, a)
assert distance == 1
a = make_tree(treestring="(a, b, (((c1, c2), (d1, (d2, d3))), (e1, (e2, e3))));")
b = make_tree(treestring="(a, b, ((c1, c2), (d2, (d1, d3)), (e1, e2, e3)));")
# There are four splits not in common
# Tree a: e1-others, d2,d3-others, c's and d's - others
# Tree b: d1,d3-others
distance = unrooted_robinson_foulds(a, b)
assert distance == 4
distance = unrooted_robinson_foulds(b, a)
assert distance == 4
# lin_rajan_moret distance
def test_lrm_different_trees():
a = make_tree(treestring="(1,(((2,3),4),(5,((6,(7,(8,9))),(10,11)))),12);")
b = make_tree(treestring="(1,((((2,3),4),5),((6,7),((8,9),(10,11)))),12);")
distance = lin_rajan_moret(a, b)
assert distance == 8
def test_lrm_matches_original_implementation_small_tree(DATA_DIR):
path, expect = DATA_DIR / "match_dist_100.tree", 1402
data = path.read_text().splitlines()
t1 = make_tree(data[0])
t2 = make_tree(data[1])
distance = lin_rajan_moret(t1, t2)
# Assert that this is same value as original C implementation
assert distance == expect
@pytest.mark.slow
def test_lrm_matches_original_implementation_big_tree(DATA_DIR):
path, expect = DATA_DIR / "match_dist_2000.tree", 17037
data = pathlib.Path(path).read_text().splitlines()
t1 = make_tree(data[0])
t2 = make_tree(data[1])
distance = lin_rajan_moret(t1, t2)
# Assert that this is same value as original C implementation
assert distance == expect
def test_lrm_same_tree():
# Topology of the unrooted trees are identical
# though they are stored in different orientations.
a = make_tree(treestring="(a, b, (c, d));")
b = make_tree(treestring="(c, d, (a, b));")
distance = lin_rajan_moret(a, b)
assert distance == 0
distance = lin_rajan_moret(b, a)
assert distance == 0
# rooted rf distance
def test_rooted_rf_multifurcation():
a = make_tree(treestring="((a, b), (c, d));")
b = make_tree(treestring="((a, c, b), d);")
# There are three clades not in common
# Tree a: ab, cd
# Tree b: acb
distance = rooted_robinson_foulds(a, b)
assert distance == 3
distance = rooted_robinson_foulds(b, a)
assert distance == 3
def test_rooted_rf_different_trees():
a = make_tree(
treestring="((((a1,a2),(b1,b2,b3),x),(c1,c2,c3),(d1,d2)),(e1,(e2,e3)));"
)
b = make_tree(
treestring="((((a1,a2),(b1,b2,b3)),x,(c1,c2,c3),(d1,d2)),(e1,(e2,e3)));"
)
# Moving the single x up one level creates two clades that are not in common.
# Tree a: ABx
# Tree b: AB
distance = rooted_robinson_foulds(a, b)
assert distance == 2
distance = rooted_robinson_foulds(b, a)
assert distance == 2
a = make_tree(treestring="((a,b),c);")
b = make_tree(treestring="((a,c),b);")
# There are two clades not in common
# Tree a: ab
# Tree b: ac
distance = rooted_robinson_foulds(a, b)
assert distance == 2
distance = rooted_robinson_foulds(b, a)
assert distance == 2
a = make_tree(treestring="((a,(b1,(b2,b3))),((c1,c2),(c3,c4)));")
b = make_tree(treestring="((a,((c1,c2),(c3,c4))),(b1,(b2,b3)));")
# There are two clades not in common
# Tree a: aB
# Tree b: aC
distance = rooted_robinson_foulds(a, b)
assert distance == 2
distance = rooted_robinson_foulds(b, a)
assert distance == 2
a = make_tree(treestring="(a,(b,(c,(d,(e,f)))));")
b = make_tree(treestring="(a,(f,(c,(d,(b,e)))));")
# There are six clades not in common
# Tree a: ef, def, cdef
# Tree b: be, bde, bcde
distance = rooted_robinson_foulds(a, b)
assert distance == 6
distance = rooted_robinson_foulds(b, a)
assert distance == 6
def test_rooted_rf_same_tree():
a = make_tree(treestring="((a, b), (c, d));")
b = make_tree(treestring="((c, d), (a, b));")
# The distance between trees of the same topology is 0
distance = rooted_robinson_foulds(a, b)
assert distance == 0
# matching cluster distance
def test_matching_cluster_distance_multifurcation():
# The basic example from the matching cluster distance paper
# see reference in matching_cluster_distance
a = make_tree(treestring="((a, b), (c, d));")
b = make_tree(treestring="((a, c, b), d);")
distance = matching_cluster_distance(a, b)
assert distance == 3
distance = matching_cluster_distance(b, a)
assert distance == 3
def test_matching_cluster_distance_properties():
# Test properties of the matching cluster distance paper shown as exampels
# see reference in matching_cluster_distance
a = make_tree(
treestring="((((a1,a2),(b1,b2,b3),x),(c1,c2,c3),(d1,d2)),(e1,(e2,e3)));"
)
b = make_tree(
treestring="((((a1,a2),(b1,b2,b3)),x,(c1,c2,c3),(d1,d2)),(e1,(e2,e3)));"
)
distance = matching_cluster_distance(a, b)
assert distance == 1
distance = matching_cluster_distance(b, a)
assert distance == 1
a = make_tree(treestring="((a,b),c);")
b = make_tree(treestring="((a,c),b);")
distance = matching_cluster_distance(a, b)
assert distance == len(a.tips()) - 1
distance = matching_cluster_distance(b, a)
assert distance == len(a.tips()) - 1
a = make_tree(treestring="((a,(b1,(b2,b3))),((c1,c2),(c3,c4)));")
b = make_tree(treestring="((a,((c1,c2),(c3,c4))),(b1,(b2,b3)));")
distance = matching_cluster_distance(a, b)
assert distance == len(a.tips()) - 1
distance = matching_cluster_distance(b, a)
assert distance == len(a.tips()) - 1
def test_matching_cluster_distance_same_tree():
a = make_tree(treestring="((a, b), (c, d));")
b = make_tree(treestring="((c, d), (a, b));")
# The distance between trees of the same topology is 0
distance = matching_cluster_distance(a, b)
assert distance == 0
# different tips
@pytest.mark.parametrize(
"b",
(
"(e, d, (a, b));",
"(a, d, (a, b));",
"(a, b, (c, (d, e)));",
),
)
def test_unrooted_rf_fails_different_tips(b):
t1 = make_tree(treestring="(a, b, (c, d));")
t2 = make_tree(treestring=b)
with pytest.raises(ValueError):
unrooted_robinson_foulds(t1, t2)
with pytest.raises(ValueError):
unrooted_robinson_foulds(t2, t1)
@pytest.mark.parametrize(
"b",
(
"(e, d, (a, b));",
"(a, d, (a, b));",
"(a, b, (c, (d, e)));",
),
)
def test_lrm_fails_different_tips(b):
t1 = make_tree(treestring="(a, b, (c, d));")
t2 = make_tree(treestring=b)
with pytest.raises(ValueError):
lin_rajan_moret(t1, t2)
with pytest.raises(ValueError):
lin_rajan_moret(t2, t1)
@pytest.mark.parametrize(
"b",
(
"((e, d), (a, b));",
"((a, d), (a, b));",
"(a, (b, (c, (d, e))));",
),
)
def test_rooted_rf_fails_different_tips(b):
t1 = make_tree(treestring="((a, b), (c, d));")
t2 = make_tree(treestring=b)
with pytest.raises(ValueError):
rooted_robinson_foulds(t1, t2)
with pytest.raises(ValueError):
rooted_robinson_foulds(t2, t1)
@pytest.mark.parametrize(
"b",
(
"((e, d), (a, b));",
"((a, d), (a, b));",
"(a, (b, (c, (d, e))));",
),
)
def test_matching_cluster_fails_different_tips(b):
t1 = make_tree(treestring="((a, b), (c, d));")
t2 = make_tree(treestring=b)
with pytest.raises(ValueError):
matching_cluster_distance(t1, t2)
with pytest.raises(ValueError):
matching_cluster_distance(t2, t1)
# unrooted distance measures applied to examples with rooted trees
@pytest.mark.parametrize(
"a,b",
(
("((a, b), (c, d));", "(d, c, (a, b));"),
("(a, b, (c, d));", "((d, c), (a, b));"),
("((a, b), (c, d));", "((a, c), (b, d));"),
),
)
def test_unrooted_dist_fails_rooted_trees(a, b):
t1 = make_tree(treestring=a)
t2 = make_tree(treestring=b)
for unrooted_distance_measure in UNROOTED_DISTANCE_MEASURES:
with pytest.raises(ValueError):
unrooted_distance_measure(t1, t2)
# rooted distance measures applied to examples with unrooted trees
@pytest.mark.parametrize(
"a,b",
(
("((a, b), (c, d));", "(d, c, (a, b));"),
("(a, b, (c, d));", "((d, c), (a, b));"),
("(a, b, (c, d));", "(d, c, (a, b));"),
),
)
def test_rooted_dist_fails_unrooted_trees(a, b):
t1 = make_tree(treestring=a)
t2 = make_tree(treestring=b)
for rooted_distance_measure in ROOTED_DISTANCE_MEASURES:
with pytest.raises(ValueError):
rooted_distance_measure(t1, t2)
|