Whole-genome per-variant calculations

This notebook computes per-variant and filter-derived statistics used directly in manuscript text and tables. Each section prints plug-and-play f-string sentences that use the exact manuscript sentence structure with computed values inserted. These sentences are the primary deliverable – copy-paste them into the paper and use the surrounding context to ctrl-F to the correct section.

Paper source of truth

The manuscript PDFs in papers/ are the source of truth for organization and published figure numbering. See notebooks/notebooks_whole_genome/PAPER_CROSS_REFERENCE.md for the full figure/data-point map.

Execution order

  1. Run the whole-genome summary dataframe creation pipeline.
  2. Run this notebook first (heavy per-variant calculations).
  3. Run notebooks/notebooks_whole_genome/make_plots.ipynb.
  4. Run notebooks/notebooks_whole_genome/calc_figures_for_paper.ipynb (summary fast-path and consolidated exports).

Data paths

Set RUN_PROFILE in the first code cell. test uses plain folders (intermediate_data, tables); whole_genome uses _whole_genome folders for publication-scale inputs and outputs.

Organization

Production manuscript statistics are organized first. Exploratory checks are at the end under a dedicated heading. The central statistics reference TSV is exported to tables/paper_per_variant_inline_statistics_reference.tsv.

Code
import os
from pathlib import Path

import numpy as np
import pandas as pd
from pandas.api.types import CategoricalDtype
import polars as pl

from utilities import find_repo_root, require_files

# Choose "test" for Docker test data or "whole_genome" for publication-scale data.
# The environment variable is useful for automated runs; manually edit RUN_PROFILE for interactive use.
RUN_PROFILE = "whole_genome"
RUN_PROFILE = os.environ.get("PHASING_T2T_RUN_PROFILE", RUN_PROFILE)

PROFILE_PATHS = {
    "test": {
        "intermediate_data": "intermediate_data",
        "imputation_statistics": "imputation_statistics",
        "figures": "figures",
        "tables": "tables",
    },
    "whole_genome": {
        "intermediate_data": "intermediate_data_whole_genome",
        "imputation_statistics": "imputation_statistics_whole_genome",
        "figures": "figures_whole_genome",
        "tables": "tables_whole_genome",
    },
}

if RUN_PROFILE not in PROFILE_PATHS:
    raise ValueError(f"RUN_PROFILE must be one of {sorted(PROFILE_PATHS)}, got {RUN_PROFILE!r}")

profile_paths = PROFILE_PATHS[RUN_PROFILE]

repo_root = find_repo_root()
os.chdir(repo_root)

summary_statistics_folder = profile_paths["intermediate_data"]
tables_folder = profile_paths["tables"]

required_summary_files = (
    "MAF_performance_variants.parquet",
    "samples.parquet",
    "variants.parquet",
)

summary_statistics_folder = require_files(
    profile_paths["intermediate_data"],
    required_summary_files,
    "per-variant summary data",
    RUN_PROFILE,
)

Path(tables_folder).mkdir(parents=True, exist_ok=True)

print(f"RUN_PROFILE: {RUN_PROFILE}")
print(f"Working directory: {repo_root}")
print(f"Summary statistics folder: {summary_statistics_folder}")
print(f"Tables folder: {tables_folder}")
Checking for required files in intermediate_data_whole_genome
intermediate_data_whole_genome/MAF_performance_variants.parquet
RUN_PROFILE: whole_genome
Working directory: /mnt/ssd/lalli/test_dir2/phasing_T2T
Summary statistics folder: intermediate_data_whole_genome
Tables folder: tables_whole_genome

1) Load data

Code
MAF_performance_variant_df = pl.read_parquet(f'{summary_statistics_folder}/MAF_performance_variants.parquet')
all_samples = pd.read_parquet(f'{summary_statistics_folder}/samples.parquet')
all_variants = pl.read_parquet(f'{summary_statistics_folder}/variants.parquet')

2) Variant filtering statistics

Reconstructs CHM13 variant filter flags from bcftools query data to compute the proportion of variants removed by each filter criterion in T2T-CHM13-unique regions.

Code
# Build CHM13 variant ID set for filter reconstruction
chm13_ids = pl.scan_parquet(f"{summary_statistics_folder}/MAF_performance_variants.parquet") \
    .select(['variant_id','genome']) \
    .filter(pl.col('genome')=='CHM13v2.0') \
    .select('variant_id') \
    .unique() \
    .collect() \
    .to_series()
chm13_ids = set(chm13_ids)

# Load bcftools query data and compute filter flags
variant_filter_stats = (
    pl.scan_parquet(f"{summary_statistics_folder}/bcftools_query_variant_data.parquet")
    .with_columns(
        MAC_original=pl.min_horizontal(
            pl.col('AN_original_panel') - pl.col('AC_original_panel'),
            pl.col('AC_original_panel'),
        )
    )
    .filter(pl.col('genome') == 'CHM13v2.0')
    .with_columns(
        VQSLOD_filter=(pl.col('VQSLOD') < 0),
        MERR_filter=~(pl.col('MERR') > (602 * 0.05)),
        in_panel=pl.col('ID').is_in(chm13_ids),
        HWE_pop_filter=(
            (pl.col('HWE_EUR') < 1e-10) & (pl.col('HWE_AFR') < 1e-10) &
            (pl.col('HWE_EAS') < 1e-10) & (pl.col('HWE_AMR') < 1e-10) &
            (pl.col('HWE_SAS') < 1e-10)
        ).cast(pl.Boolean),
        MAC_filter=(pl.col('MAC_original') == 0).cast(pl.Boolean),
        AC_filter=(pl.col('AC_original_panel') <= 1),
        f_missing_filter=(pl.col('F_MISSING') > 0.05).cast(pl.Boolean),
        var_len_filter=(
            (pl.col('ID').str.split('_').list[2].str.len_chars().cast(pl.Int64)
             - pl.col('ID').str.split('_').list[3].str.len_chars().cast(pl.Int64)
            ).abs() >= 50 - 1  # minus 1 because bcftools ILEN does not count the anchor base
        ).cast(pl.Boolean),
        alt_star_filter=(pl.col('ALT') == '*').cast(pl.Boolean),
        singleton=(pl.col('MAC_original') == 1).cast(pl.Boolean),
        pass_filter=(pl.col('FILTER') != 'PASS').cast(pl.Boolean),
    )
    .collect()
)

variant_filter_stats = variant_filter_stats.with_columns(
    CHM13_criteria_fail=(
        pl.col('MAC_filter') | pl.col('VQSLOD_filter') |
        pl.col('f_missing_filter') | pl.col('pass_filter') |
        pl.col('HWE_pop_filter') | pl.col('MERR_filter') |
        pl.col('var_len_filter') | pl.col('alt_star_filter')
    ).cast(pl.Boolean)
)

3) Table 1: Genotype concordance

Computes genotype discordance/concordance and switch error summaries by variant class (SNP, Indel, combined), genome (GRCh38 vs CHM13v2.0), syntenic/non-syntenic regions, and ground truth source (HPRC/HGSVC/combined).

Code
best_phased_variants = MAF_performance_variant_df.filter(
    (pl.col('ground_truth_data_source') != 'trios') &
    (pl.col('method_of_phasing') == 'phased_with_parents_and_pedigree')
)
catVarType = pl.Enum(['SNP', 'Indel', 'SNPs + Indels'])
best_phased_variants = best_phased_variants.with_columns(type=pl.col('type').cast(catVarType))
best_phased_variants = pl.concat([
    best_phased_variants,
    best_phased_variants.with_columns(type=pl.lit('SNPs + Indels').cast(catVarType)),
])

best_phased_variants_by_syntenic = (
    best_phased_variants
    .group_by(['type', 'Syntenic', 'method_of_phasing', 'ground_truth_data_source', 'genome'])
    .sum()
    .with_columns(
        gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked') * 100,
        switch_error_rate=pl.col('n_switch_errors') / pl.col('n_checked') * 100,
    )
    .to_pandas()
)

best_phased_variants_overall = (
    best_phased_variants
    .group_by(['type', 'method_of_phasing', 'ground_truth_data_source', 'genome'])
    .sum()
    .with_columns(
        gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked') * 100,
        switch_error_rate=pl.col('n_switch_errors') / pl.col('n_checked') * 100,
    )
    .to_pandas()
)

best_phased_variants_overall.sort_values(['ground_truth_data_source', 'genome', 'type'])
type method_of_phasing ground_truth_data_source genome variant_id n_switch_errors n_checked n_gt_errors n_gt_checked MAC AN Syntenic multiallelic in_STRs in_platinum_STRs in_segdups MAF rounded_MAF gt_error_rate switch_error_rate
8 SNP phased_with_parents_and_pedigree HPRC_samples GRCh38 None 203833 96830151 3025975 711358610 10944096358 116867178764 18221880 1265576 766061 1743056 729897 163978912.0 NaN 0.425380 0.210506
15 Indel phased_with_parents_and_pedigree HPRC_samples GRCh38 None 270831 19555688 6481975 145991067 2456453580 23980917912 3740768 2691149 755934 2640091 125781 38157432.0 NaN 4.439981 1.384922
5 SNPs + Indels phased_with_parents_and_pedigree HPRC_samples GRCh38 None 474664 116385839 9507950 857349677 13400549938 140848096676 21962648 3956725 1521995 4383147 855678 199150896.0 NaN 1.108993 0.407837
2 SNP phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 125429 83970616 1581451 615986668 9469281970 101191249828 15792611 987434 517335 1313540 278708 142845632.0 NaN 0.256735 0.149372
26 Indel phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 236515 18027890 5285347 131789145 2235467907 21649362400 3376142 2364140 779837 2303815 113211 34772904.0 NaN 4.010457 1.311939
1 SNPs + Indels phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 361944 101998506 6866798 747775813 11704749877 122840612228 19168753 3351574 1297172 3617355 391919 174973280.0 NaN 0.918296 0.354852
14 SNP phased_with_parents_and_pedigree HGSVC_samples GRCh38 None 754134 151010447 8002821 1262828279 10955497233 128888389728 20094909 1367442 838425 1895313 803320 163417504.0 NaN 0.633722 0.499392
34 Indel phased_with_parents_and_pedigree HGSVC_samples GRCh38 None 645574 29972914 13099186 257525124 2469918872 26283412072 4099769 2940460 813114 2887930 138324 38311468.0 NaN 5.086566 2.153858
37 SNPs + Indels phased_with_parents_and_pedigree HGSVC_samples GRCh38 None 1399708 180983361 21102007 1520353403 13425416105 155171801800 24194678 4307902 1651539 4783243 941644 198561648.0 NaN 1.387967 0.773390
35 SNP phased_with_parents_and_pedigree HGSVC_samples CHM13v2.0 None 485678 131554603 5512226 1118869739 9491939443 114194366900 17820975 1097102 587425 1471419 302956 142577184.0 NaN 0.492660 0.369184
38 Indel phased_with_parents_and_pedigree HGSVC_samples CHM13v2.0 None 533760 27594727 10825486 232867189 2243090467 23766358296 3702815 2583571 833259 2518845 123142 34848972.0 NaN 4.648781 1.934283
36 SNPs + Indels phased_with_parents_and_pedigree HGSVC_samples CHM13v2.0 None 1019438 159149330 16337712 1351736928 11735029910 137960725196 21523790 3680673 1420684 3990264 426098 174604576.0 NaN 1.208646 0.640554
9 SNP phased_with_parents_and_pedigree HPRC_HGSVC_all_samples GRCh38 None 957339 241013562 14721907 2350794845 10982317171 152594204608 23793129 1568185 958358 2213064 920122 162859712.0 NaN 0.626252 0.397214
40 Indel phased_with_parents_and_pedigree HPRC_HGSVC_all_samples GRCh38 None 903281 47466990 21396841 445915360 2476326087 29056677080 4532480 3185320 886608 3129710 150987 38340108.0 NaN 4.798409 1.902967
17 SNPs + Indels phased_with_parents_and_pedigree HPRC_HGSVC_all_samples GRCh38 None 1860620 288480552 36118748 2796710205 13458643258 181650881688 28325609 4753505 1844966 5342774 1071109 197901824.0 NaN 1.291473 0.644972
41 SNP phased_with_parents_and_pedigree HPRC_HGSVC_all_samples CHM13v2.0 None 619740 209878593 10533796 2082299645 9521752792 135196547464 21100865 1266753 672813 1726315 341533 142198736.0 NaN 0.505873 0.295285
16 Indel phased_with_parents_and_pedigree HPRC_HGSVC_all_samples CHM13v2.0 None 766099 43608428 17831326 402399835 2245832839 26229554432 4089910 2798030 893192 2729404 131564 34835480.0 NaN 4.431246 1.756768
33 SNPs + Indels phased_with_parents_and_pedigree HPRC_HGSVC_all_samples CHM13v2.0 None 1385839 253487021 28365122 2484699480 11767585631 161426101896 25190775 4064783 1566005 4455719 473097 174114928.0 NaN 1.141592 0.546710
11 SNP phased_with_parents_and_pedigree HPRC_HGSVC_probands GRCh38 None 392141 164491634 7523433 1396660154 10907986574 133179742148 20766556 1386360 839847 1939416 805160 162603296.0 NaN 0.538673 0.238396
27 Indel phased_with_parents_and_pedigree HPRC_HGSVC_probands GRCh38 None 530431 32349730 13448642 275069141 2433600414 26313991172 4105018 2919010 811535 2866535 135286 37747404.0 NaN 4.889186 1.639677
13 SNPs + Indels phased_with_parents_and_pedigree HPRC_HGSVC_probands GRCh38 None 922572 196841364 20972075 1671729295 13341586988 159493733320 24871574 4305370 1651382 4805951 940446 197185088.0 NaN 1.254514 0.468688
0 SNP phased_with_parents_and_pedigree HPRC_HGSVC_probands CHM13v2.0 None 239782 142966973 4885380 1206225547 9459032010 115063447336 17958339 1097397 576521 1475703 292575 141984288.0 NaN 0.405014 0.167718
29 Indel phased_with_parents_and_pedigree HPRC_HGSVC_probands CHM13v2.0 None 468515 29686314 11141390 247071200 2206659927 23644887224 3687219 2553224 824407 2491677 117071 34286444.0 NaN 4.509384 1.578219
7 SNPs + Indels phased_with_parents_and_pedigree HPRC_HGSVC_probands CHM13v2.0 None 708297 172653287 16026770 1453296747 11665691937 138708334560 21645558 3650621 1400928 3967380 409646 173475584.0 NaN 1.102787 0.410242
4 SNP phased_with_parents_and_pedigree HGSVC_probands GRCh38 None 189404 73719637 3536488 478222804 10701095005 99849848776 15569973 1075135 639116 1470196 607418 161630560.0 NaN 0.739506 0.256925
32 Indel phased_with_parents_and_pedigree HGSVC_probands GRCh38 None 246392 14535103 5879840 103388337 2360505655 21645936260 3377159 2470508 678159 2422863 109993 36720588.0 NaN 5.687141 1.695151
20 SNPs + Indels phased_with_parents_and_pedigree HGSVC_probands GRCh38 None 435796 88254740 9416328 581611141 13061600660 121495785036 18947132 3545643 1317275 3893059 717411 195674480.0 NaN 1.619008 0.493793
22 SNP phased_with_parents_and_pedigree HGSVC_probands CHM13v2.0 None 111439 64083157 2443173 413454077 9283079788 86358522764 13478025 850471 438828 1117010 221586 141086784.0 NaN 0.590918 0.173897
25 Indel phased_with_parents_and_pedigree HGSVC_probands CHM13v2.0 None 214074 13331480 4837731 92803375 2140405049 19439463676 3031931 2155489 699440 2102793 94671 33334620.0 NaN 5.212883 1.605778
21 SNPs + Indels phased_with_parents_and_pedigree HGSVC_probands CHM13v2.0 None 325513 77414637 7280904 506257452 11423484837 105797986440 16509956 3005960 1138268 3219803 316257 172083104.0 NaN 1.438182 0.420480
6 SNP phased_with_parents_and_pedigree HGSVC_parents GRCh38 None 142964 33168366 1101386 155285503 10306271778 77507554364 12085944 856704 500251 1148979 472025 157311664.0 NaN 0.709265 0.431025
3 Indel phased_with_parents_and_pedigree HGSVC_parents GRCh38 None 132952 6628759 2189651 34800663 2238640700 17421422388 2718117 2005144 562272 1962357 87418 34890936.0 NaN 6.291981 2.005685
12 SNPs + Indels phased_with_parents_and_pedigree HGSVC_parents GRCh38 None 275916 39797125 3291037 190086166 12544912478 94928976752 14804061 2861848 1062523 3111336 559443 190270688.0 NaN 1.731340 0.693306
30 SNP phased_with_parents_and_pedigree HGSVC_parents CHM13v2.0 None 60029 28907987 687896 135397717 8941645479 67615026596 10552603 682129 346061 878947 174249 137145680.0 NaN 0.508056 0.207655
28 Indel phased_with_parents_and_pedigree HGSVC_parents CHM13v2.0 None 110811 6100767 1824830 31395703 2031937895 15729447164 2453315 1755681 586063 1709678 75536 31681220.0 NaN 5.812356 1.816345
23 SNPs + Indels phased_with_parents_and_pedigree HGSVC_parents CHM13v2.0 None 170840 35008754 2512726 166793420 10973583374 83344473760 13005918 2437810 932124 2588625 249785 167135872.0 NaN 1.506490 0.487992
39 SNP phased_with_parents_and_pedigree HGSVC_samples_nontrios_only GRCh38 None 423045 43341629 2699076 233348273 10442767236 79816247596 12446981 883777 514418 1184948 483147 159613792.0 NaN 1.156673 0.976071
19 Indel phased_with_parents_and_pedigree HGSVC_samples_nontrios_only GRCh38 None 241792 8580217 3510984 53180074 2283546245 18263529176 2849598 2120649 583367 2076316 91449 35577040.0 NaN 6.602067 2.818017
18 SNPs + Indels phased_with_parents_and_pedigree HGSVC_samples_nontrios_only GRCh38 None 664837 51921846 6210060 286528347 12726313481 98079776772 15296579 3004426 1097785 3261264 574596 193158208.0 NaN 2.167346 1.280457
24 SNP phased_with_parents_and_pedigree HGSVC_samples_nontrios_only CHM13v2.0 None 320395 37994100 2097605 207776731 9060713235 71102619380 11097182 713470 361885 923329 180852 139174608.0 NaN 1.009548 0.843276
10 Indel phased_with_parents_and_pedigree HGSVC_samples_nontrios_only CHM13v2.0 None 188371 7907309 2866376 47918554 2070335651 16469973704 2569134 1853408 607640 1805361 78522 32271328.0 NaN 5.981766 2.382239
31 SNPs + Indels phased_with_parents_and_pedigree HGSVC_samples_nontrios_only CHM13v2.0 None 508766 45901409 4963981 255695285 11131048886 87572593084 13666316 2566878 969525 2728690 259374 169718640.0 NaN 1.941366 1.108389
Code
best_phased_variants_by_syntenic.loc[
    best_phased_variants_by_syntenic.ground_truth_data_source == 'HPRC_samples'
].sort_values(['ground_truth_data_source', 'Syntenic', 'genome', 'type'])
type Syntenic method_of_phasing ground_truth_data_source genome variant_id n_switch_errors n_checked n_gt_errors n_gt_checked MAC AN multiallelic in_STRs in_platinum_STRs in_segdups MAF rounded_MAF gt_error_rate switch_error_rate
41 SNP False phased_with_parents_and_pedigree HPRC_samples GRCh38 None 450 122930 25015 1054999 14013151 174259244 3736 16442 2220 983 2.188301e+05 NaN 2.371092 0.366062
67 Indel False phased_with_parents_and_pedigree HPRC_samples GRCh38 None 174 15490 10527 152017 2255971 25039640 2623 1673 1852 191 3.522783e+04 NaN 6.924883 1.123305
0 SNPs + Indels False phased_with_parents_and_pedigree HPRC_samples GRCh38 None 624 138420 35542 1207016 16269122 199298884 6359 18115 4072 1174 2.540636e+05 NaN 2.944617 0.450802
5 SNP False phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 133 26737 15469 336505 3625205 55368984 1475 3542 849 2255 5.660954e+04 NaN 4.596960 0.497438
17 Indel False phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 410 15019 16586 173023 2662187 28549032 3520 3259 1150 1868 4.157111e+04 NaN 9.586009 2.729875
34 SNPs + Indels False phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 543 41756 32055 509528 6287392 83918016 4995 6801 1999 4123 9.818216e+04 NaN 6.291116 1.300412
69 SNP True phased_with_parents_and_pedigree HPRC_samples GRCh38 None 203383 96707221 3000960 710303611 10930083207 116692919520 1261840 749619 1740836 728914 1.637886e+08 NaN 0.422490 0.210308
11 Indel True phased_with_parents_and_pedigree HPRC_samples GRCh38 None 270657 19540198 6471448 145839050 2454197609 23955878272 2688526 754261 2638239 125590 3.812274e+07 NaN 4.437390 1.385129
39 SNPs + Indels True phased_with_parents_and_pedigree HPRC_samples GRCh38 None 474040 116247419 9472408 856142661 13384280816 140648797792 3950366 1503880 4379075 854504 1.989228e+08 NaN 1.106405 0.407785
20 SNP True phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 125296 83943879 1565982 615650163 9465656765 101135880844 985959 513793 1312691 276453 1.427933e+08 NaN 0.254362 0.149262
54 Indel True phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 236105 18012871 5268761 131616122 2232805720 21620813368 2360620 776578 2302665 111343 3.473215e+07 NaN 4.003127 1.310757
29 SNPs + Indels True phased_with_parents_and_pedigree HPRC_samples CHM13v2.0 None 361401 101956750 6834743 747266285 11698462485 122756694212 3346579 1290371 3615356 387796 1.748844e+08 NaN 0.914633 0.354465

4) Percent concordance summaries

Code
overall_subsetted = (
    MAF_performance_variant_df
    .filter(
        (pl.col('ground_truth_data_source') != 'trios') &
        (pl.col('method_of_phasing') == 'phased_with_parents_and_pedigree')
    )
    .group_by(['type', 'Syntenic', 'method_of_phasing', 'ground_truth_data_source', 'genome'])
    .sum()
    .with_columns(gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked'))
)

overall = (
    MAF_performance_variant_df
    .filter(
        (pl.col('ground_truth_data_source') != 'trios') &
        (pl.col('method_of_phasing') == 'phased_with_parents_and_pedigree')
    )
    .group_by(['method_of_phasing', 'ground_truth_data_source', 'genome'])
    .sum()
    .with_columns(gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked'))
)

overall_syntenic = (
    MAF_performance_variant_df
    .filter(
        (pl.col('ground_truth_data_source') != 'trios') &
        (pl.col('Syntenic')) &
        (pl.col('method_of_phasing') == 'phased_with_parents_and_pedigree')
    )
    .group_by(['method_of_phasing', 'ground_truth_data_source', 'genome'])
    .sum()
    .with_columns(gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked'))
)
Code
overall_syntenic = overall_syntenic.to_pandas()[['Syntenic', 'genome', 'ground_truth_data_source', 'gt_error_rate']].sort_values(['genome', 'Syntenic', 'gt_error_rate'])
overall_syntenic['Non-reference true positive rate'] = (1 - overall_syntenic.gt_error_rate) * 100
overall_syntenic['gt_error_rate'] *= 100
overall_syntenic.loc[overall_syntenic.ground_truth_data_source.isin(('HPRC_samples', 'HGSVC_samples', 'HPRC_HGSVC_all_samples'))]
Syntenic genome ground_truth_data_source gt_error_rate Non-reference true positive rate
0 21962648 GRCh38 HPRC_samples 1.106405 98.893595
1 24194678 GRCh38 HGSVC_samples 1.386977 98.613023
5 28325609 GRCh38 HPRC_HGSVC_all_samples 1.289689 98.710311
6 19168753 CHM13v2.0 HPRC_samples 0.914633 99.085367
8 21523790 CHM13v2.0 HGSVC_samples 1.203934 98.796066
3 25190775 CHM13v2.0 HPRC_HGSVC_all_samples 1.138373 98.861627
Code
overall = overall.to_pandas()[['Syntenic', 'genome', 'ground_truth_data_source', 'gt_error_rate']].sort_values(['genome', 'Syntenic', 'gt_error_rate'])
overall['Non-reference true positive rate'] = (1 - overall.gt_error_rate) * 100
overall['gt_error_rate'] *= 100
overall.loc[overall.ground_truth_data_source.isin(('HPRC_samples', 'HGSVC_samples', 'HPRC_HGSVC_all_samples'))]
Syntenic genome ground_truth_data_source gt_error_rate Non-reference true positive rate
3 21962648 GRCh38 HPRC_samples 1.108993 98.891007
6 24194678 GRCh38 HGSVC_samples 1.387967 98.612033
1 28325609 GRCh38 HPRC_HGSVC_all_samples 1.291473 98.708527
8 19168753 CHM13v2.0 HPRC_samples 0.918296 99.081704
0 21523790 CHM13v2.0 HGSVC_samples 1.208646 98.791354
4 25190775 CHM13v2.0 HPRC_HGSVC_all_samples 1.141592 98.858408
Code
overall_subsetted = overall_subsetted.to_pandas()[['type', 'Syntenic', 'genome', 'ground_truth_data_source', 'gt_error_rate']].sort_values(['genome', 'type', 'Syntenic', 'gt_error_rate'])
overall_subsetted['Non-reference true positive rate'] = (1 - overall_subsetted.gt_error_rate) * 100
overall_subsetted['gt_error_rate'] *= 100
overall_subsetted.loc[overall_subsetted.ground_truth_data_source.isin(('HPRC_samples', 'HGSVC_samples', 'HPRC_HGSVC_all_samples'))]
type Syntenic genome ground_truth_data_source gt_error_rate Non-reference true positive rate
47 SNP False GRCh38 HGSVC_samples 1.442616 98.557384
16 SNP False GRCh38 HPRC_HGSVC_all_samples 2.010190 97.989810
6 SNP False GRCh38 HPRC_samples 2.371092 97.628908
35 SNP True GRCh38 HPRC_samples 0.422490 99.577510
23 SNP True GRCh38 HPRC_HGSVC_all_samples 0.624200 99.375800
45 SNP True GRCh38 HGSVC_samples 0.632463 99.367537
54 Indel False GRCh38 HGSVC_samples 6.394128 93.605872
20 Indel False GRCh38 HPRC_HGSVC_all_samples 6.490134 93.509866
53 Indel False GRCh38 HPRC_samples 6.924883 93.075117
4 Indel True GRCh38 HPRC_samples 4.437390 95.562610
43 Indel True GRCh38 HPRC_HGSVC_all_samples 4.796596 95.203404
34 Indel True GRCh38 HGSVC_samples 5.085148 94.914852
21 SNP False CHM13v2.0 HPRC_HGSVC_all_samples 4.181155 95.818845
1 SNP False CHM13v2.0 HGSVC_samples 4.204832 95.795168
37 SNP False CHM13v2.0 HPRC_samples 4.596960 95.403040
8 SNP True CHM13v2.0 HPRC_samples 0.254362 99.745638
24 SNP True CHM13v2.0 HGSVC_samples 0.490435 99.509565
27 SNP True CHM13v2.0 HPRC_HGSVC_all_samples 0.504153 99.495847
40 Indel False CHM13v2.0 HPRC_samples 9.586009 90.413991
22 Indel False CHM13v2.0 HGSVC_samples 9.598220 90.401780
36 Indel False CHM13v2.0 HPRC_HGSVC_all_samples 9.751645 90.248355
50 Indel True CHM13v2.0 HPRC_samples 4.003127 95.996873
3 Indel True CHM13v2.0 HPRC_HGSVC_all_samples 4.423507 95.576493
30 Indel True CHM13v2.0 HGSVC_samples 4.637721 95.362279

5) Singleton and doubleton accuracy

Code
# Paper cross-reference: manuscript lines 210-214; Figure 2b,d;
# reviewer reply lines 348-354 (singleton SER ~30%).

final_dataset_singletons = MAF_performance_variant_df.filter(
    (pl.col('method_of_phasing') == 'phased_with_parents_and_pedigree') &
    (pl.col('ground_truth_data_source') == 'HGSVC_samples_nontrios_only') &
    (pl.col('rounded_MAF').cast(pl.String) == 'singleton')
)

final_dataset_singleton_ser = (
    final_dataset_singletons
    .group_by(['genome', 'type'])
    .sum()
    .with_columns(SER=pl.col('n_switch_errors') / pl.col('n_checked') * 100)
    .sort(['genome', 'type'])
    .to_pandas()
    .reset_index(drop=True)
)

# Genotype-discordance rows for the manuscript text about singleton and doubleton SNP concordance.
# The doubleton values use all HPRC/HGSVC assembly samples; the singleton value uses HGSVC non-trio samples.
first_non_singleton_maf_bin = '0.00021-0.00042'
paper_maf_concordance = (
    MAF_performance_variant_df
    .filter(
        (pl.col('method_of_phasing') == 'phased_with_parents_and_pedigree') &
        (pl.col('type') == 'SNP') &
        (
            ((pl.col('ground_truth_data_source') == 'HPRC_HGSVC_all_samples') &
             (pl.col('rounded_MAF').cast(pl.String) == first_non_singleton_maf_bin)) |
            ((pl.col('ground_truth_data_source') == 'HGSVC_samples_nontrios_only') &
             (pl.col('rounded_MAF').cast(pl.String) == 'singleton'))
        )
    )
    .group_by(['ground_truth_data_source', 'rounded_MAF', 'genome', 'type'])
    .sum()
    .with_columns(
        gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked') * 100,
        gt_accuracy_rate=100 - (pl.col('n_gt_errors') / pl.col('n_gt_checked') * 100),
    )
    .sort(['ground_truth_data_source', 'rounded_MAF', 'genome', 'type'])
    .to_pandas()
    .reset_index(drop=True)
)

6) Concordance summary (Table 1 source)

Code
synEnum = pl.Enum(categories=['All_regions', 'Syntenic', 'Nonsyntenic'])
concordance = all_variants.filter(
    (pl.col('method_of_phasing') == 'phased_with_parents_and_pedigree') &
    (pl.col('ground_truth_data_source') == 'HPRC_samples')
).with_columns(
    Syntenic=pl.col('Syntenic').cast(str).replace('true', 'Syntenic').replace('false', 'Nonsyntenic').cast(synEnum)
)

concordance_summary = concordance.clone().with_columns(Syntenic=pl.lit('All_regions').cast(synEnum))
concordance_summary = pl.concat((concordance, concordance_summary)).group_by(['Syntenic', 'genome', 'type']).sum().to_pandas().reset_index()
concordance_summary = pd.concat((
    concordance_summary,
    concordance_summary.copy().assign(type='SNPs + Indels').groupby(['Syntenic', 'genome', 'type'], observed=True).sum(numeric_only=True).reset_index(),
))
concordance_summary['gt_error_rate'] = concordance_summary.n_gt_errors / concordance_summary.n_gt_checked * 100
concordance_summary['gt_accuracy_rate'] = 100 - concordance_summary['gt_error_rate']
concordance_summary['switch_error_rate'] = concordance_summary.n_switch_errors / concordance_summary.n_checked * 100
concordance_summary['Syntenic'] = concordance_summary.Syntenic.replace('false', 'Nonsyntenic').replace('true', 'Syntenic')

pd.set_option("display.precision", 3)
concordance_summary.sort_values(['Syntenic', 'genome', 'type']).set_index(['Syntenic', 'genome', 'type'])[
    ['switch_error_rate', 'gt_error_rate', 'n_gt_errors', 'n_gt_checked', 'n_switch_errors', 'n_checked']
].astype(float)
switch_error_rate gt_error_rate n_gt_errors n_gt_checked n_switch_errors n_checked
Syntenic genome type
All_regions GRCh38 Indel 1.385 4.440 6.482e+06 1.460e+08 270831.0 1.956e+07
SNP 0.211 0.425 3.026e+06 7.114e+08 203833.0 9.683e+07
SNPs + Indels 0.408 1.109 9.508e+06 8.573e+08 474664.0 1.164e+08
CHM13v2.0 Indel 1.312 4.010 5.285e+06 1.318e+08 236515.0 1.803e+07
SNP 0.149 0.257 1.581e+06 6.160e+08 125429.0 8.397e+07
SNPs + Indels 0.355 0.918 6.867e+06 7.478e+08 361944.0 1.020e+08
Syntenic GRCh38 Indel 1.385 4.437 6.471e+06 1.458e+08 270657.0 1.954e+07
SNP 0.210 0.422 3.001e+06 7.103e+08 203383.0 9.671e+07
SNPs + Indels 0.408 1.106 9.472e+06 8.561e+08 474040.0 1.162e+08
CHM13v2.0 Indel 1.311 4.003 5.269e+06 1.316e+08 236105.0 1.801e+07
SNP 0.149 0.254 1.566e+06 6.157e+08 125296.0 8.394e+07
SNPs + Indels 0.354 0.915 6.835e+06 7.473e+08 361401.0 1.020e+08
Nonsyntenic GRCh38 Indel 1.123 6.925 1.053e+04 1.520e+05 174.0 1.549e+04
SNP 0.366 2.371 2.502e+04 1.055e+06 450.0 1.229e+05
SNPs + Indels 0.451 2.945 3.554e+04 1.207e+06 624.0 1.384e+05
CHM13v2.0 Indel 2.730 9.586 1.659e+04 1.730e+05 410.0 1.502e+04
SNP 0.497 4.597 1.547e+04 3.365e+05 133.0 2.674e+04
SNPs + Indels 1.300 6.291 3.206e+04 5.095e+05 543.0 4.176e+04

7) STR and isolated variant stratification

Code
# Compute indel discordance within STRs (GIAB + Platinum) and for isolated (biallelic,
# non-STR, non-overlapping) indels, grouped by genome.

indels_only = MAF_performance_variant_df.filter(
    (pl.col('type') == 'Indel') &
    (pl.col('method_of_phasing') == 'phased_with_parents_and_pedigree') &
    (pl.col('ground_truth_data_source') == 'HPRC_samples')
)

# STR indels: in GIAB or Platinum STR regions
str_indels = (
    indels_only
    .filter(pl.col('in_STRs') | pl.col('in_platinum_STRs'))
    .group_by('genome').sum()
    .with_columns(gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked') * 100)
    .to_pandas().set_index('genome')
)

# multiallelic indels: in GIAB, or Platinum STR regions, or overlapping another variant
multiallelic_indels = (
    indels_only
    .filter(pl.col('in_STRs') | pl.col('in_platinum_STRs') | pl.col('multiallelic'))
    .group_by('genome').sum()
    .with_columns(gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked') * 100)
    .to_pandas().set_index('genome')
)


# Isolated indels: not in STRs, not overlapping other variants (biallelic)
isolated_indels = (
    indels_only
    .filter(~pl.col('in_STRs') & ~pl.col('in_platinum_STRs') & ~pl.col('multiallelic'))
    .group_by('genome').sum()
    .with_columns(gt_error_rate=pl.col('n_gt_errors') / pl.col('n_gt_checked') * 100)
    .to_pandas().set_index('genome')
)

chm13_str_indel_disc = str_indels.loc['CHM13v2.0', 'gt_error_rate']
grch38_str_indel_disc = str_indels.loc['GRCh38', 'gt_error_rate']
chm13_multiallelic_indel_disc = multiallelic_indels.loc['CHM13v2.0', 'gt_error_rate']
grch38_multiallelic_indel_disc = multiallelic_indels.loc['GRCh38', 'gt_error_rate']
chm13_isolated_indel_disc = isolated_indels.loc['CHM13v2.0', 'gt_error_rate']
grch38_isolated_indel_disc = isolated_indels.loc['GRCh38', 'gt_error_rate']

print(f'STR indel discordance  -- GRCh38: {grch38_str_indel_disc:.2f}%, CHM13: {chm13_str_indel_disc:.2f}%')
print(f'Multiallelic indel discordance -- GRCh38: {grch38_multiallelic_indel_disc:.2f}%, CHM13: {chm13_multiallelic_indel_disc:.2f}%')
print(f'Isolated indel discordance -- GRCh38: {grch38_isolated_indel_disc:.2f}%, CHM13: {chm13_isolated_indel_disc:.2f}%')
STR indel discordance  -- GRCh38: 5.93%, CHM13: 5.49%
Multiallelic indel discordance -- GRCh38: 5.56%, CHM13: 5.11%
Isolated indel discordance -- GRCh38: 0.45%, CHM13: 0.43%

8) Plug-and-play manuscript text

The sentences below use the exact manuscript sentence structure with computed values inserted. Copy-paste these into the paper and use the surrounding context to ctrl-F to the correct section.

Code
# Helper: extract a value from concordance_summary
def cs(genome, vtype, syntenic, col='gt_error_rate'):
    mask = (
        (concordance_summary.genome == genome) &
        (concordance_summary.type == vtype) &
        (concordance_summary.Syntenic == syntenic)
    )
    return float(concordance_summary.loc[mask, col].iloc[0])

# Helper: extract singleton SER
def sing_ser(genome, vtype):
    mask = (final_dataset_singleton_ser.genome == genome) & (final_dataset_singleton_ser.type == vtype)
    return float(final_dataset_singleton_ser.loc[mask, 'SER'].iloc[0])

# Helper: extract doubleton/singleton discordance from paper_maf_concordance
def maf_disc(gt_source, maf_bin, genome):
    mask = (
        (paper_maf_concordance.ground_truth_data_source == gt_source) &
        (paper_maf_concordance.rounded_MAF == maf_bin) &
        (paper_maf_concordance.genome == genome)
    )
    return float(paper_maf_concordance.loc[mask, 'gt_error_rate'].iloc[0])


# ---- Overall concordance (Table 1) ----
chm13_all_disc = cs('CHM13v2.0', 'SNPs + Indels', 'All_regions')
grch38_all_disc = cs('GRCh38', 'SNPs + Indels', 'All_regions')

print('--- Results: 1kGP short-read variant calls (Table 1 / concordance) ---')
print()
print(f'Overall, T2T-CHM13 1kGP variants were {100 - chm13_all_disc:.2f}% concordant '
      f'with assembly-derived variation, while GRCh38 1kGP variants were '
      f'{100 - grch38_all_disc:.2f}% concordant (Table 1).')
print()

# ---- Shared-region concordance ----
chm13_snp_syn = cs('CHM13v2.0', 'SNP', 'Syntenic')
grch38_snp_syn = cs('GRCh38', 'SNP', 'Syntenic')
chm13_indel_syn = cs('CHM13v2.0', 'Indel', 'Syntenic')
grch38_indel_syn = cs('GRCh38', 'Indel', 'Syntenic')

print(f'SNPs from the T2T-CHM13 panel were less discordant with assembled genomes '
      f'({chm13_snp_syn:.2f}%) than those from the GRCh38 panel ({grch38_snp_syn:.2f}%).')
print()
print(f'Indels in shared regions followed the same pattern '
      f'(T2T-CHM13: {chm13_indel_syn:.2f}%, GRCh38: {grch38_indel_syn:.2f}%, Table 1).')
print()

# ---- Doubleton/singleton discordance ----
grch38_doubleton = maf_disc('HPRC_HGSVC_all_samples', first_non_singleton_maf_bin, 'GRCh38')
chm13_doubleton = maf_disc('HPRC_HGSVC_all_samples', first_non_singleton_maf_bin, 'CHM13v2.0')
chm13_singleton = maf_disc('HGSVC_samples_nontrios_only', 'singleton', 'CHM13v2.0')

print(f'Genotyping accuracy did not vary substantially by minor allele frequency (0.02%-50%), '
      f'consistent with the VQSR method\'s design to maintain a uniform false positive rate '
      f'regardless of allele frequency. SNPs with only two minor alleles present in the panel '
      f'(doubletons) were called almost as accurately as SNPs in the overall panel '
      f'(GRCh38: {grch38_doubleton:.2f}% discordant, T2T-CHM13: {chm13_doubleton:.2f}% discordant), '
      f'and singleton SNPs in the T2T-CHM13 panel were only {chm13_singleton:.2f}% discordant '
      f'with assembly genotypes.')
print()

# ---- STR / isolated indel discordance ----
print('--- Results: STR and isolated variant stratification ---')
print()
print(f'Manual inspection of pangenome-discordant indels showed that they were frequently '
      f'found within multiallelic short tandem repeats (STRs) -- a trend that we confirmed '
      f'with genome-wide analysis (GRCh38: {grch38_multiallelic_indel_disc:.2f}%, T2T-CHM13: '
      f'{chm13_multiallelic_indel_disc:.2f}% indel discordance within STRs, Extended Data Figure 1).')
print()
print(f'In contrast, isolated indels that did not overlap with other variant classes in the '
      f'panel showed substantially lower discordance rates (GRCh38: {grch38_isolated_indel_disc:.2f}%, '
      f'T2T-CHM13: {chm13_isolated_indel_disc:.2f}%, Supplementary Figure 13).')
print()

# ---- Singleton SER ----
print('--- Results: Singleton phasing accuracy ---')
print()
snp_sing_ser = sing_ser('CHM13v2.0', 'SNP')  # manuscript uses CHM13 values
indel_sing_ser = sing_ser('CHM13v2.0', 'Indel')
print(f'Singleton variants were statistically phased with a switch error rate (SER) of ~30% '
      f'({snp_sing_ser:.2f}% for SNPs; {indel_sing_ser:.2f}% for indels) (Figure 2b,d).')
print()

# ---- Summary/derived stats ----
pct_fewer_discordant = (1 - chm13_all_disc / grch38_all_disc) * 100
print('--- Summary / Introduction ---')
print()
print(f'Among HPRC samples with published long-read-based assemblies, alignment to T2T-CHM13 resulted in '
      f'{pct_fewer_discordant:.0f}% fewer assembly-discordant genotypes')
--- Results: 1kGP short-read variant calls (Table 1 / concordance) ---

Overall, T2T-CHM13 1kGP variants were 99.08% concordant with assembly-derived variation, while GRCh38 1kGP variants were 98.89% concordant (Table 1).

SNPs from the T2T-CHM13 panel were less discordant with assembled genomes (0.25%) than those from the GRCh38 panel (0.42%).

Indels in shared regions followed the same pattern (T2T-CHM13: 4.00%, GRCh38: 4.44%, Table 1).

Genotyping accuracy did not vary substantially by minor allele frequency (0.02%-50%), consistent with the VQSR method's design to maintain a uniform false positive rate regardless of allele frequency. SNPs with only two minor alleles present in the panel (doubletons) were called almost as accurately as SNPs in the overall panel (GRCh38: 0.51% discordant, T2T-CHM13: 0.45% discordant), and singleton SNPs in the T2T-CHM13 panel were only 1.42% discordant with assembly genotypes.

--- Results: STR and isolated variant stratification ---

Manual inspection of pangenome-discordant indels showed that they were frequently found within multiallelic short tandem repeats (STRs) -- a trend that we confirmed with genome-wide analysis (GRCh38: 5.56%, T2T-CHM13: 5.11% indel discordance within STRs, Extended Data Figure 1).

In contrast, isolated indels that did not overlap with other variant classes in the panel showed substantially lower discordance rates (GRCh38: 0.45%, T2T-CHM13: 0.43%, Supplementary Figure 13).

--- Results: Singleton phasing accuracy ---

Singleton variants were statistically phased with a switch error rate (SER) of ~30% (29.78% for SNPs; 28.85% for indels) (Figure 2b,d).

--- Summary / Introduction ---

Among HPRC samples with published long-read-based assemblies, alignment to T2T-CHM13 resulted in 17% fewer assembly-discordant genotypes

9) Export: paper_per_variant_inline_statistics_reference.tsv

Consolidated export with one row per unique statistic (both genome values on the same row) and a Paper_Text column containing the plug-and-play manuscript sentence.

Code
type_abbrev_map = {'SNP': 'SNP', 'Indel': 'Ind', 'SNPs + Indels': 'All'}
syn_abbrev_map = {'All_regions': 'All', 'Syntenic': 'Syn', 'Nonsyntenic': 'Nsyn'}

rows = []

# --- Table 1 / concordance: one row per (type, syntenic) combination ---
for (vtype, syn), grp in concordance_summary.groupby(['type', 'Syntenic'], observed=True):
    type_abbrev = type_abbrev_map.get(vtype, vtype)
    syn_abbrev = syn_abbrev_map.get(syn, syn)
    stat_code = f'C_{type_abbrev}_{syn_abbrev}'
    by_genome = grp.set_index('genome')
    val_chm13 = float(by_genome.loc['CHM13v2.0', 'gt_error_rate']) if 'CHM13v2.0' in by_genome.index else np.nan
    val_grch38 = float(by_genome.loc['GRCh38', 'gt_error_rate']) if 'GRCh38' in by_genome.index else np.nan
    rows.append({
        'Stat_Code': stat_code,
        'Section': 'Per-variant concordance',
        'Statistic_Description': f'{syn} | {vtype} | gt_error_rate',
        'Value_CHM13': val_chm13,
        'Value_GRCh38': val_grch38,
        'Percent_Change': (val_chm13 - val_grch38) / val_grch38 * 100 if (val_chm13 and val_grch38) else np.nan,
        'Paper_Text': '',
        'Paper_Location': 'Results: Table 1 / concordance paragraphs',
        'Source_Notebook': 'calc_per_variant_figures_for_paper.ipynb',
        'Notes': 'ground_truth_source=HPRC_samples (phased_with_parents_and_pedigree)',
    })

# Attach Paper_Text to the overall concordance row
for r in rows:
    if r['Stat_Code'] == 'C_All_All':
        r['Paper_Text'] = (
            f'Overall, T2T-CHM13 1kGP variants were {100 - r["Value_CHM13"]:.2f}% concordant '
            f'with assembly-derived variation, while GRCh38 1kGP variants were '
            f'{100 - r["Value_GRCh38"]:.2f}% concordant (Table 1).'
        )
    elif r['Stat_Code'] == 'C_SNP_Syn':
        r['Paper_Text'] = (
            f'SNPs from the T2T-CHM13 panel were less discordant with assembled genomes '
            f'({r["Value_CHM13"]:.2f}%) than those from the GRCh38 panel ({r["Value_GRCh38"]:.2f}%).'
        )
    elif r['Stat_Code'] == 'C_Ind_Syn':
        r['Paper_Text'] = (
            f'Indels in shared regions followed the same pattern '
            f'(T2T-CHM13: {r["Value_CHM13"]:.2f}%, GRCh38: {r["Value_GRCh38"]:.2f}%, Table 1).'
        )

# --- Final-dataset singleton SER: one row per type ---
for vtype in ['SNP', 'Indel']:
    type_abbrev = type_abbrev_map.get(vtype, vtype)
    ser_by_genome = final_dataset_singleton_ser.loc[final_dataset_singleton_ser.type == vtype].set_index('genome')
    val_chm13 = float(ser_by_genome.loc['CHM13v2.0', 'SER']) if 'CHM13v2.0' in ser_by_genome.index else np.nan
    val_grch38 = float(ser_by_genome.loc['GRCh38', 'SER']) if 'GRCh38' in ser_by_genome.index else np.nan
    rows.append({
        'Stat_Code': f'SING_SER_{type_abbrev}',
        'Section': 'Singleton SER',
        'Statistic_Description': f'Final-dataset singleton SER | {vtype}',
        'Value_CHM13': val_chm13,
        'Value_GRCh38': val_grch38,
        'Percent_Change': np.nan,
        'Paper_Text': '',
        'Paper_Location': 'Results: statistically phased singleton text; Figure 2b,d',
        'Source_Notebook': 'calc_per_variant_figures_for_paper.ipynb',
        'Notes': 'method_of_phasing=phased_with_parents_and_pedigree; ground_truth_source=HGSVC_samples_nontrios_only; rounded_MAF=singleton',
    })

# Attach Paper_Text to singleton SNP row
for r in rows:
    if r['Stat_Code'] == 'SING_SER_SNP':
        indel_row = next(x for x in rows if x['Stat_Code'] == 'SING_SER_Ind')
        r['Paper_Text'] = (
            f'Singleton variants were statistically phased with a switch error rate (SER) of ~30% '
            f'({r["Value_CHM13"]:.2f}% for SNPs; {indel_row["Value_CHM13"]:.2f}% for indels) (Figure 2b,d).'
        )

# --- Singleton/doubleton genotype-discordance ---
maf_concordance_defs = {
    ('HPRC_HGSVC_all_samples', first_non_singleton_maf_bin, 'SNP'): {
        'Stat_Code': 'GT_DISC_SNP_DOUBLETON',
        'Statistic_Description': 'Doubleton SNP genotype discordance (first non-singleton MAF bin)',
        'Paper_Location': 'Results: genotype concordance paragraph, manuscript lines 215-218',
        'Notes': (
            f'method_of_phasing=phased_with_parents_and_pedigree; '
            f'ground_truth_source=HPRC_HGSVC_all_samples; rounded_MAF={first_non_singleton_maf_bin}; '
            f'concordance=100-discordance'
        ),
    },
    ('HGSVC_samples_nontrios_only', 'singleton', 'SNP'): {
        'Stat_Code': 'GT_DISC_SNP_SINGLETON_FINAL',
        'Statistic_Description': 'Singleton SNP genotype discordance (HGSVC non-trio final dataset)',
        'Paper_Location': 'Results: genotype concordance paragraph, manuscript lines 215-218',
        'Notes': (
            'method_of_phasing=phased_with_parents_and_pedigree; '
            'ground_truth_source=HGSVC_samples_nontrios_only; rounded_MAF=singleton; '
            'concordance=100-discordance'
        ),
    },
}

for key, group in paper_maf_concordance.groupby(['ground_truth_data_source', 'rounded_MAF', 'type'], observed=True):
    definition = maf_concordance_defs.get(tuple(key))
    if definition is None:
        continue
    by_genome = group.set_index('genome')
    val_chm13 = float(by_genome.loc['CHM13v2.0', 'gt_error_rate']) if 'CHM13v2.0' in by_genome.index else np.nan
    val_grch38 = float(by_genome.loc['GRCh38', 'gt_error_rate']) if 'GRCh38' in by_genome.index else np.nan
    rows.append({
        'Stat_Code': definition['Stat_Code'],
        'Section': 'MAF-bin genotype concordance',
        'Statistic_Description': definition['Statistic_Description'],
        'Value_CHM13': val_chm13,
        'Value_GRCh38': val_grch38,
        'Percent_Change': np.nan,
        'Paper_Text': '',
        'Paper_Location': definition['Paper_Location'],
        'Source_Notebook': 'calc_per_variant_figures_for_paper.ipynb',
        'Notes': definition['Notes'],
    })

# Attach Paper_Text to the doubleton row (which also covers singleton in same sentence)
for r in rows:
    if r['Stat_Code'] == 'GT_DISC_SNP_DOUBLETON':
        sing_row = next(x for x in rows if x['Stat_Code'] == 'GT_DISC_SNP_SINGLETON_FINAL')
        r['Paper_Text'] = (
            f'SNPs with only two minor alleles present in the panel (doubletons) were called '
            f'almost as accurately as SNPs in the overall panel (GRCh38: {r["Value_GRCh38"]:.2f}% discordant, '
            f'T2T-CHM13: {r["Value_CHM13"]:.2f}% discordant), and singleton SNPs in the T2T-CHM13 panel '
            f'were only {sing_row["Value_CHM13"]:.2f}% discordant with assembly genotypes.'
        )

# --- STR / isolated indel discordance ---
rows.append({
    'Stat_Code': 'STR_INDEL_DISC',
    'Section': 'STR indel discordance',
    'Statistic_Description': 'Indel discordance within GIAB/Platinum STR regions',
    'Value_CHM13': chm13_str_indel_disc,
    'Value_GRCh38': grch38_str_indel_disc,
    'Percent_Change': np.nan,
    'Paper_Text': (
        f'Manual inspection of pangenome-discordant indels showed that they were frequently '
        f'found within multiallelic short tandem repeats (STRs) -- a trend that we confirmed '
        f'with genome-wide analysis (GRCh38: {grch38_str_indel_disc:.2f}%, T2T-CHM13: '
        f'{chm13_str_indel_disc:.2f}% indel discordance within STRs, Extended Data Figure 1).'
    ),
    'Paper_Location': 'Results: STR stratification; manuscript lines 222-229',
    'Source_Notebook': 'calc_per_variant_figures_for_paper.ipynb',
    'Notes': 'ground_truth_source=HPRC_samples; type=Indel; in_STRs or in_platinum_STRs',
})

rows.append({
    'Stat_Code': 'ISOLATED_INDEL_DISC',
    'Section': 'Isolated indel discordance',
    'Statistic_Description': 'Isolated (non-STR, biallelic) indel discordance',
    'Value_CHM13': chm13_isolated_indel_disc,
    'Value_GRCh38': grch38_isolated_indel_disc,
    'Percent_Change': np.nan,
    'Paper_Text': (
        f'In contrast, isolated indels that did not overlap with other variant classes in the '
        f'panel showed substantially lower discordance rates (GRCh38: {grch38_isolated_indel_disc:.2f}%, '
        f'T2T-CHM13: {chm13_isolated_indel_disc:.2f}%, Supplementary Figure 13).'
    ),
    'Paper_Location': 'Results: isolated variant stratification; manuscript lines 222-229',
    'Source_Notebook': 'calc_per_variant_figures_for_paper.ipynb',
    'Notes': 'ground_truth_source=HPRC_samples; type=Indel; ~in_STRs & ~in_platinum_STRs & ~multiallelic',
})

# --- Summary derived stat ---
rows.append({
    'Stat_Code': 'PCT_FEWER_DISCORDANT',
    'Section': 'Summary derived',
    'Statistic_Description': 'Percent fewer assembly-discordant genotypes (HPRC samples; CHM13 vs GRCh38)',
    'Value_CHM13': chm13_all_disc,
    'Value_GRCh38': grch38_all_disc,
    'Percent_Change': pct_fewer_discordant,
    'Paper_Text': (
        f'Among HPRC samples with published long-read-based assemblies, alignment to T2T-CHM13 resulted in '
        f'{pct_fewer_discordant:.0f}% fewer assembly-discordant genotypes'
    ),
    'Paper_Location': 'Summary; Introduction; Discussion',
    'Source_Notebook': 'calc_per_variant_figures_for_paper.ipynb',
    'Notes': 'Derived from HPRC samples only: (1 - chm13_disc/grch38_disc) * 100; All_regions SNPs+Indels gt_error_rate',
})

# --- Build and export ---
paper_inline_statistics_per_variant = pd.DataFrame(rows)
out_path = f'{tables_folder}/paper_per_variant_inline_statistics_reference.tsv'
paper_inline_statistics_per_variant.to_csv(out_path, sep='\t', index=False)
print(f'Wrote {len(paper_inline_statistics_per_variant)} rows to {out_path}')
paper_inline_statistics_per_variant
Wrote 16 rows to tables_whole_genome/paper_per_variant_inline_statistics_reference.tsv
Stat_Code Section Statistic_Description Value_CHM13 Value_GRCh38 Percent_Change Paper_Text Paper_Location Source_Notebook Notes
0 C_Ind_All Per-variant concordance All_regions | Indel | gt_error_rate 4.010 4.440 -9.674 Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
1 C_Ind_Syn Per-variant concordance Syntenic | Indel | gt_error_rate 4.003 4.437 -9.786 Indels in shared regions followed the same pat... Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
2 C_Ind_Nsyn Per-variant concordance Nonsyntenic | Indel | gt_error_rate 9.586 6.925 38.428 Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
3 C_SNP_All Per-variant concordance All_regions | SNP | gt_error_rate 0.257 0.425 -39.646 Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
4 C_SNP_Syn Per-variant concordance Syntenic | SNP | gt_error_rate 0.254 0.422 -39.794 SNPs from the T2T-CHM13 panel were less discor... Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
5 C_SNP_Nsyn Per-variant concordance Nonsyntenic | SNP | gt_error_rate 4.597 2.371 93.875 Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
6 C_All_All Per-variant concordance All_regions | SNPs + Indels | gt_error_rate 0.918 1.109 -17.195 Overall, T2T-CHM13 1kGP variants were 99.08% c... Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
7 C_All_Syn Per-variant concordance Syntenic | SNPs + Indels | gt_error_rate 0.915 1.106 -17.333 Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
8 C_All_Nsyn Per-variant concordance Nonsyntenic | SNPs + Indels | gt_error_rate 6.291 2.945 113.648 Results: Table 1 / concordance paragraphs calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples (phased_with_...
9 SING_SER_SNP Singleton SER Final-dataset singleton SER | SNP 29.778 NaN NaN Singleton variants were statistically phased w... Results: statistically phased singleton text; ... calc_per_variant_figures_for_paper.ipynb method_of_phasing=phased_with_parents_and_pedi...
10 SING_SER_Ind Singleton SER Final-dataset singleton SER | Indel 28.854 NaN NaN Results: statistically phased singleton text; ... calc_per_variant_figures_for_paper.ipynb method_of_phasing=phased_with_parents_and_pedi...
11 GT_DISC_SNP_DOUBLETON MAF-bin genotype concordance Doubleton SNP genotype discordance (first non-... 0.452 0.510 NaN SNPs with only two minor alleles present in th... Results: genotype concordance paragraph, manus... calc_per_variant_figures_for_paper.ipynb method_of_phasing=phased_with_parents_and_pedi...
12 GT_DISC_SNP_SINGLETON_FINAL MAF-bin genotype concordance Singleton SNP genotype discordance (HGSVC non-... 1.417 NaN NaN Results: genotype concordance paragraph, manus... calc_per_variant_figures_for_paper.ipynb method_of_phasing=phased_with_parents_and_pedi...
13 STR_INDEL_DISC STR indel discordance Indel discordance within GIAB/Platinum STR reg... 5.495 5.926 NaN Manual inspection of pangenome-discordant inde... Results: STR stratification; manuscript lines ... calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples; type=Indel; ...
14 ISOLATED_INDEL_DISC Isolated indel discordance Isolated (non-STR, biallelic) indel discordance 0.431 0.445 NaN In contrast, isolated indels that did not over... Results: isolated variant stratification; manu... calc_per_variant_figures_for_paper.ipynb ground_truth_source=HPRC_samples; type=Indel; ...
15 PCT_FEWER_DISCORDANT Summary derived Percent fewer assembly-discordant genotypes (C... 0.918 1.109 17.195 alignment to T2T-CHM13 resulted in 17% fewer a... Summary; Introduction calc_per_variant_figures_for_paper.ipynb Derived: (1 - chm13_disc/grch38_disc) * 100; A...