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