|
26 | 26 |
|
27 | 27 | import functools |
28 | 28 | import io |
| 29 | +import random |
29 | 30 | import textwrap |
30 | 31 |
|
31 | 32 | import dendropy |
@@ -334,6 +335,85 @@ def test_nexus_no_trees_or_alignments(self): |
334 | 335 | ) |
335 | 336 |
|
336 | 337 |
|
| 338 | +class TestNexusNodeLabels: |
| 339 | + @tests.cached_example |
| 340 | + def balanced_tree(self): |
| 341 | + # 4 |
| 342 | + # ┏━┻┓ |
| 343 | + # ┃ 3 |
| 344 | + # ┃ ┏┻┓ |
| 345 | + # 0 1 2 |
| 346 | + return tskit.Tree.generate_balanced(3) |
| 347 | + |
| 348 | + def test_as_nexus_labels_basic(self): |
| 349 | + ts = self.balanced_tree().tree_sequence |
| 350 | + labels = {0: "human", 1: "chimp", 2: "bonobo"} |
| 351 | + expected = textwrap.dedent( |
| 352 | + """\ |
| 353 | + #NEXUS |
| 354 | + BEGIN TAXA; |
| 355 | + DIMENSIONS NTAX=3; |
| 356 | + TAXLABELS human chimp bonobo; |
| 357 | + END; |
| 358 | + BEGIN TREES; |
| 359 | + TRANSLATE n0 human, n1 chimp, n2 bonobo; |
| 360 | + TREE t0^1 = [&R] (n0:2,(n1:1,n2:1):1); |
| 361 | + END; |
| 362 | + """ |
| 363 | + ) |
| 364 | + assert expected == ts.as_nexus(include_alignments=False, node_labels=labels) |
| 365 | + |
| 366 | + def test_as_nexus_labels_partial(self): |
| 367 | + ts = self.balanced_tree().tree_sequence |
| 368 | + labels = {0: "human", 2: "bonobo"} |
| 369 | + expected = textwrap.dedent( |
| 370 | + """\ |
| 371 | + #NEXUS |
| 372 | + BEGIN TAXA; |
| 373 | + DIMENSIONS NTAX=3; |
| 374 | + TAXLABELS human n1 bonobo; |
| 375 | + END; |
| 376 | + BEGIN TREES; |
| 377 | + TRANSLATE n0 human, n2 bonobo; |
| 378 | + TREE t0^1 = [&R] (n0:2,(n1:1,n2:1):1); |
| 379 | + END; |
| 380 | + """ |
| 381 | + ) |
| 382 | + assert expected == ts.as_nexus(include_alignments=False, node_labels=labels) |
| 383 | + |
| 384 | + def test_as_nexus_labels_none(self): |
| 385 | + ts = self.balanced_tree().tree_sequence |
| 386 | + expected = textwrap.dedent( |
| 387 | + """\ |
| 388 | + #NEXUS |
| 389 | + BEGIN TAXA; |
| 390 | + DIMENSIONS NTAX=3; |
| 391 | + TAXLABELS n0 n1 n2; |
| 392 | + END; |
| 393 | + BEGIN TREES; |
| 394 | + TREE t0^1 = [&R] (n0:2,(n1:1,n2:1):1); |
| 395 | + END; |
| 396 | + """ |
| 397 | + ) |
| 398 | + assert expected == ts.as_nexus(include_alignments=False, node_labels=None) |
| 399 | + |
| 400 | + @pytest.mark.parametrize("ts", get_example_tree_sequences()) |
| 401 | + def test_parseable(self, ts): |
| 402 | + for tree in ts.trees(): |
| 403 | + if not tree.has_single_root: |
| 404 | + return |
| 405 | + |
| 406 | + labels = {} |
| 407 | + samples = ts.samples() |
| 408 | + k = random.randint(1, len(samples)) |
| 409 | + for node in random.sample(list(samples), k): |
| 410 | + labels[node] = f"new_node_which_was_{node}" |
| 411 | + |
| 412 | + nexus = ts.as_nexus(include_alignments=False, node_labels=labels) |
| 413 | + print(nexus) |
| 414 | + dendropy.DataSet.get(data=nexus, schema="nexus") |
| 415 | + |
| 416 | + |
337 | 417 | class TestNewickCodePaths: |
338 | 418 | """ |
339 | 419 | Test that the different code paths we use under the hood lead to |
|
0 commit comments