|
4 | 4 | import pytest
|
5 | 5 | import xarray as xr
|
6 | 6 |
|
7 |
| -from sgkit import display_genotypes |
| 7 | +from sgkit import display_genotypes, display_pedigree |
8 | 8 | from sgkit.display import genotype_as_bytes
|
9 | 9 | from sgkit.testing import simulate_genotype_call_dataset
|
10 | 10 |
|
@@ -417,3 +417,192 @@ def test_genotype_as_bytes(genotype, phased, max_allele_chars, expect):
|
417 | 417 | expect,
|
418 | 418 | genotype_as_bytes(genotype, phased, max_allele_chars),
|
419 | 419 | )
|
| 420 | + |
| 421 | + |
| 422 | +def pedigree_Hamilton_Kerr(): |
| 423 | + ds = xr.Dataset() |
| 424 | + ds["sample_id"] = "samples", ["S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8"] |
| 425 | + ds["parent_id"] = ["samples", "parents"], [ |
| 426 | + [".", "."], |
| 427 | + [".", "."], |
| 428 | + [".", "S2"], |
| 429 | + ["S1", "."], |
| 430 | + ["S1", "S3"], |
| 431 | + ["S1", "S3"], |
| 432 | + ["S6", "S2"], |
| 433 | + ["S6", "S2"], |
| 434 | + ] |
| 435 | + ds["stat_Hamilton_Kerr_tau"] = ["samples", "parents"], [ |
| 436 | + [1, 1], |
| 437 | + [2, 2], |
| 438 | + [0, 2], |
| 439 | + [2, 0], |
| 440 | + [1, 1], |
| 441 | + [2, 2], |
| 442 | + [2, 2], |
| 443 | + [2, 2], |
| 444 | + ] |
| 445 | + ds["stat_Hamilton_Kerr_lambda"] = ["samples", "parents"], [ |
| 446 | + [0.0, 0.0], |
| 447 | + [0.167, 0.167], |
| 448 | + [0.0, 0.167], |
| 449 | + [0.041, 0.0], |
| 450 | + [0.0, 0.0], |
| 451 | + [0.918, 0.041], |
| 452 | + [0.167, 0.167], |
| 453 | + [0.167, 0.167], |
| 454 | + ] |
| 455 | + return ds |
| 456 | + |
| 457 | + |
| 458 | +def test_display_pedigree__no_coords(): |
| 459 | + ds = pedigree_Hamilton_Kerr() |
| 460 | + graph = display_pedigree(ds) |
| 461 | + expect = """ digraph { |
| 462 | + \t0 |
| 463 | + \t1 |
| 464 | + \t2 |
| 465 | + \t3 |
| 466 | + \t4 |
| 467 | + \t5 |
| 468 | + \t6 |
| 469 | + \t7 |
| 470 | + \t1 -> 2 |
| 471 | + \t0 -> 3 |
| 472 | + \t0 -> 4 |
| 473 | + \t2 -> 4 |
| 474 | + \t0 -> 5 |
| 475 | + \t2 -> 5 |
| 476 | + \t5 -> 6 |
| 477 | + \t1 -> 6 |
| 478 | + \t5 -> 7 |
| 479 | + \t1 -> 7 |
| 480 | + } |
| 481 | + """ |
| 482 | + assert str(graph) == dedent(expect) |
| 483 | + |
| 484 | + |
| 485 | +def test_display_pedigree__samples_coords(): |
| 486 | + ds = pedigree_Hamilton_Kerr() |
| 487 | + ds = ds.assign_coords(samples=ds.sample_id) |
| 488 | + graph = display_pedigree(ds) |
| 489 | + expect = """ digraph { |
| 490 | + \t0 [label=S1] |
| 491 | + \t1 [label=S2] |
| 492 | + \t2 [label=S3] |
| 493 | + \t3 [label=S4] |
| 494 | + \t4 [label=S5] |
| 495 | + \t5 [label=S6] |
| 496 | + \t6 [label=S7] |
| 497 | + \t7 [label=S8] |
| 498 | + \t1 -> 2 |
| 499 | + \t0 -> 3 |
| 500 | + \t0 -> 4 |
| 501 | + \t2 -> 4 |
| 502 | + \t0 -> 5 |
| 503 | + \t2 -> 5 |
| 504 | + \t5 -> 6 |
| 505 | + \t1 -> 6 |
| 506 | + \t5 -> 7 |
| 507 | + \t1 -> 7 |
| 508 | + } |
| 509 | + """ |
| 510 | + assert str(graph) == dedent(expect) |
| 511 | + |
| 512 | + |
| 513 | +def test_display_pedigree__samples_coords_reorder(): |
| 514 | + ds = pedigree_Hamilton_Kerr() |
| 515 | + ds = ds.sel(samples=[7, 3, 5, 0, 4, 1, 2, 6]) |
| 516 | + ds = ds.assign_coords(samples=ds.sample_id) |
| 517 | + graph = display_pedigree(ds) |
| 518 | + expect = """ digraph { |
| 519 | + \t0 [label=S8] |
| 520 | + \t1 [label=S4] |
| 521 | + \t2 [label=S6] |
| 522 | + \t3 [label=S1] |
| 523 | + \t4 [label=S5] |
| 524 | + \t5 [label=S2] |
| 525 | + \t6 [label=S3] |
| 526 | + \t7 [label=S7] |
| 527 | + \t2 -> 0 |
| 528 | + \t5 -> 0 |
| 529 | + \t3 -> 1 |
| 530 | + \t3 -> 2 |
| 531 | + \t6 -> 2 |
| 532 | + \t3 -> 4 |
| 533 | + \t6 -> 4 |
| 534 | + \t5 -> 6 |
| 535 | + \t2 -> 7 |
| 536 | + \t5 -> 7 |
| 537 | + } |
| 538 | + """ |
| 539 | + assert str(graph) == dedent(expect) |
| 540 | + |
| 541 | + |
| 542 | +def test_display_pedigree__samples_labels(): |
| 543 | + ds = pedigree_Hamilton_Kerr() |
| 544 | + graph = display_pedigree(ds, node_attrs=dict(label=ds.sample_id)) |
| 545 | + expect = """ digraph { |
| 546 | + \t0 [label=S1] |
| 547 | + \t1 [label=S2] |
| 548 | + \t2 [label=S3] |
| 549 | + \t3 [label=S4] |
| 550 | + \t4 [label=S5] |
| 551 | + \t5 [label=S6] |
| 552 | + \t6 [label=S7] |
| 553 | + \t7 [label=S8] |
| 554 | + \t1 -> 2 |
| 555 | + \t0 -> 3 |
| 556 | + \t0 -> 4 |
| 557 | + \t2 -> 4 |
| 558 | + \t0 -> 5 |
| 559 | + \t2 -> 5 |
| 560 | + \t5 -> 6 |
| 561 | + \t1 -> 6 |
| 562 | + \t5 -> 7 |
| 563 | + \t1 -> 7 |
| 564 | + } |
| 565 | + """ |
| 566 | + assert str(graph) == dedent(expect) |
| 567 | + |
| 568 | + |
| 569 | +def test_display_pedigree__broadcast(): |
| 570 | + ds = pedigree_Hamilton_Kerr() |
| 571 | + inbreeding = np.array([0.0, 0.077, 0.231, 0.041, 0.0, 0.197, 0.196, 0.196]) |
| 572 | + label = (ds.sample_id.str + "\n").str + inbreeding.astype("U") |
| 573 | + edges = xr.where( |
| 574 | + ds.stat_Hamilton_Kerr_tau == 2, |
| 575 | + "black:black", |
| 576 | + "black", |
| 577 | + ) |
| 578 | + graph = display_pedigree( |
| 579 | + ds, |
| 580 | + graph_attrs=dict(splines="false", outputorder="edgesfirst"), |
| 581 | + node_attrs=dict( |
| 582 | + style="filled", fillcolor="black", fontcolor="white", label=label |
| 583 | + ), |
| 584 | + edge_attrs=dict(arrowhead="crow", color=edges), |
| 585 | + ) |
| 586 | + expect = """ digraph { |
| 587 | + \toutputorder=edgesfirst splines=false |
| 588 | + \t0 [label="S1\n 0.0" fillcolor=black fontcolor=white style=filled] |
| 589 | + \t1 [label="S2\n 0.077" fillcolor=black fontcolor=white style=filled] |
| 590 | + \t2 [label="S3\n 0.231" fillcolor=black fontcolor=white style=filled] |
| 591 | + \t3 [label="S4\n 0.041" fillcolor=black fontcolor=white style=filled] |
| 592 | + \t4 [label="S5\n 0.0" fillcolor=black fontcolor=white style=filled] |
| 593 | + \t5 [label="S6\n 0.197" fillcolor=black fontcolor=white style=filled] |
| 594 | + \t6 [label="S7\n 0.196" fillcolor=black fontcolor=white style=filled] |
| 595 | + \t7 [label="S8\n 0.196" fillcolor=black fontcolor=white style=filled] |
| 596 | + \t1 -> 2 [arrowhead=crow color="black:black"] |
| 597 | + \t0 -> 3 [arrowhead=crow color="black:black"] |
| 598 | + \t0 -> 4 [arrowhead=crow color=black] |
| 599 | + \t2 -> 4 [arrowhead=crow color=black] |
| 600 | + \t0 -> 5 [arrowhead=crow color="black:black"] |
| 601 | + \t2 -> 5 [arrowhead=crow color="black:black"] |
| 602 | + \t5 -> 6 [arrowhead=crow color="black:black"] |
| 603 | + \t1 -> 6 [arrowhead=crow color="black:black"] |
| 604 | + \t5 -> 7 [arrowhead=crow color="black:black"] |
| 605 | + \t1 -> 7 [arrowhead=crow color="black:black"] |
| 606 | + } |
| 607 | + """ |
| 608 | + assert str(graph) == dedent(expect) |
0 commit comments