|
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | from gnomad.assessment.validity_checks import ( |
| 10 | + check_global_and_row_annot_lengths, |
10 | 11 | check_missingness_of_struct, |
11 | 12 | check_raw_and_adj_callstats, |
12 | 13 | check_sex_chr_metrics, |
@@ -502,6 +503,58 @@ def test_sum_group_callstats(ht_for_group_sums, caplog) -> None: |
502 | 503 | ), f"Expected phrase missing: {log_phrase}" |
503 | 504 |
|
504 | 505 |
|
| 506 | +@pytest.fixture |
| 507 | +def ht_for_check_global_and_row_annot_lengths() -> hl.Table: |
| 508 | + """Fixture to set up a Hail Table with the desired structure and data for check_global_and_row_annot_lengths.""" |
| 509 | + ht = hl.Table.parallelize( |
| 510 | + [ |
| 511 | + {"freq": [0.1, 0.2, 0.3], "faf": [0.01, 0.02]}, |
| 512 | + {"freq": [0.8, 0.4, 0.5], "faf": [0.03, 0.04, 0.05]}, |
| 513 | + ], |
| 514 | + hl.tstruct(freq=hl.tarray(hl.tfloat64), faf=hl.tarray(hl.tfloat64)), |
| 515 | + ) |
| 516 | + |
| 517 | + return ht.annotate_globals( |
| 518 | + freq_meta=["A", "B", "C"], |
| 519 | + freq_index_dict={"A": 0, "B": 1, "C": 2}, |
| 520 | + freq_meta_sample_count=[100, 200, 300], |
| 521 | + faf_meta=["D", "E"], |
| 522 | + faf_index_dict={"D": 0, "E": 1}, |
| 523 | + ) |
| 524 | + |
| 525 | + |
| 526 | +def test_check_global_and_row_annot_lengths( |
| 527 | + ht_for_check_global_and_row_annot_lengths, caplog |
| 528 | +) -> None: |
| 529 | + """Test that check_global_and_row_annot_lengths produces the expected log messages.""" |
| 530 | + ht = ht_for_check_global_and_row_annot_lengths |
| 531 | + |
| 532 | + # Define the row_to_globals_check dictionary. |
| 533 | + row_to_globals_check = { |
| 534 | + "freq": ["freq_meta", "freq_index_dict", "freq_meta_sample_count"], |
| 535 | + "faf": ["faf_meta", "faf_index_dict"], |
| 536 | + } |
| 537 | + |
| 538 | + with caplog.at_level(logging.INFO, logger="gnomad.assessment.validity_checks"): |
| 539 | + check_global_and_row_annot_lengths( |
| 540 | + ht, row_to_globals_check, check_all_rows=True |
| 541 | + ) |
| 542 | + |
| 543 | + log_messages = [record.message for record in caplog.records] |
| 544 | + |
| 545 | + # Verify log messages. |
| 546 | + expected_logs = [ |
| 547 | + "Passed global and row lengths comparison: Length of freq_meta in globals (3) does match length of freq in 2 out of 2 rows (row length counter: {3: 2})", |
| 548 | + "Passed global and row lengths comparison: Length of freq_index_dict in globals (3) does match length of freq in 2 out of 2 rows (row length counter: {3: 2})", |
| 549 | + "Passed global and row lengths comparison: Length of freq_meta_sample_count in globals (3) does match length of freq in 2 out of 2 rows (row length counter: {3: 2})", |
| 550 | + "Failed global and row lengths comparison: Length of faf_meta in globals (2) does NOT match length of faf in 1 out of 2 rows (row length counter: {2: 1, 3: 1})", |
| 551 | + "Failed global and row lengths comparison: Length of faf_index_dict in globals (2) does NOT match length of faf in 1 out of 2 rows (row length counter: {2: 1, 3: 1})", |
| 552 | + ] |
| 553 | + |
| 554 | + for msg in expected_logs: |
| 555 | + assert msg in log_messages, f"Expected log message is missing: {msg}" |
| 556 | + |
| 557 | + |
505 | 558 | @pytest.fixture |
506 | 559 | def ht_for_check_raw_and_adj_callstats() -> hl.Table: |
507 | 560 | """Fixture to create a Hail Table with the expected structure and test values for check_raw_and_adj_callstats, using underscore as the delimiter.""" |
|
0 commit comments