paper_inline_rows = []
def add_stat(section, statistic_description, value_chm13=np.nan, value_grch38=np.nan,
percent_change=np.nan, paper_text='', paper_location='',
source_notebook='calc_figures_for_paper', notes=''):
paper_inline_rows.append({
'Section': section,
'Statistic_Description': statistic_description,
'Value_CHM13': value_chm13,
'Value_GRCh38': value_grch38,
'Percent_Change': percent_change,
'Paper_Text': paper_text,
'Paper_Location': paper_location,
'Source_Notebook': source_notebook,
'Notes': notes,
})
# -- Variant filtering and panel composition --
variant_filter_note = 'Selected denominator: filtered variants / total variants for the same genome in filter_summary_stats.'
add_stat('Variant filtering', 'CHM13 variants lost to GATK-based filters',
value_chm13=float(num_variants_lost_to_gatk),
paper_text=f'Our VQSLOD filter resulted in the exclusion of an additional {num_variants_lost_to_gatk:,} variants compared to the filtering methods used by Byrska-Bishop et al. to produce the GRCh38 1kGP panel.',
paper_location='Results: variant QC paragraph',
notes=variant_filter_note)
add_stat('Variant filtering', 'CHM13 singleton variants gained',
value_chm13=float(num_singletons_gained),
paper_text=f'The inclusion of singleton alleles added {num_singletons_gained:,} unique variants to our dataset.',
paper_location='Results: variant QC paragraph',
notes=variant_filter_note)
add_stat('Variant filtering', 'GRCh38 % variants filtered',
value_grch38=pct_grch38_filtered,
paper_text=f'Overall, variant filtering had removed {pct_grch38_filtered:.1f}% of variants in the Byrska-Bishop et al. 1kGP GRCh38 panel, including {pct_grch38_singleton_only:.1f}% of all variants that were filtered out solely because they were singletons.',
paper_location='Results: variant QC paragraph',
notes=variant_filter_note)
add_stat('Variant filtering', 'GRCh38 % filtered solely as singletons',
value_grch38=pct_grch38_singleton_only,
paper_location='Results: variant QC paragraph',
notes=variant_filter_note)
add_stat('Variant filtering', 'CHM13 % variants filtered',
value_chm13=pct_chm13_filtered,
paper_text=f'In contrast, our filtering strategy removed {pct_chm13_filtered:.0f}% of 1kGP T2T-CHM13 variants, with {pct_chm13_vqslod_only:.1f}% of variants filtered solely due to the VQSLOD filter (Supplementary Figure 1).',
paper_location='Results: variant QC paragraph',
notes=variant_filter_note)
add_stat('Variant filtering', 'CHM13 % filtered solely by VQSLOD',
value_chm13=pct_chm13_vqslod_only,
paper_location='Results: variant QC paragraph',
notes=variant_filter_note)
chm13_unique_filter_note = (
'Denominator: GATK PASS (pass_filter=False) variants in CHM13v2.0 nonsyntenic regions. '
'VQSLOD-only uses the CHM13 filter-criteria list; AC_filter is not a CHM13 criterion.'
)
add_stat('CHM13-unique filtering', 'CHM13-unique GATK PASS variants (denominator)',
value_chm13=float(chm13_unique_gatk_passing),
paper_location='Results: CHM13-unique filtering paragraph',
notes=chm13_unique_filter_note)
add_stat('CHM13-unique filtering', 'CHM13-unique % GATK PASS variants removed solely by VQSLOD',
value_chm13=pct_chm13_unique_vqslod_only_of_gatk_passing,
paper_text=f'Our stricter VQSLOD filter removes {pct_chm13_unique_vqslod_only_of_gatk_passing:.1f}% of GATK-passing variants in T2T-CHM13-unique regions. When all filters are considered, only {pct_chm13_unique_retained_of_gatk_passing:.1f}% of GATK-passing variants in these regions are retained (Supplementary Figure 1).',
paper_location='Results: CHM13-unique filtering paragraph',
notes=f'{chm13_unique_vqslod_only:,}/{chm13_unique_gatk_passing:,}; {chm13_unique_filter_note}')
add_stat('CHM13-unique filtering', 'CHM13-unique % GATK PASS variants retained after all CHM13 filters',
value_chm13=pct_chm13_unique_retained_of_gatk_passing,
paper_location='Results: CHM13-unique filtering paragraph',
notes=f'{chm13_unique_retained:,}/{chm13_unique_gatk_passing:,}; {chm13_unique_filter_note}')
add_stat('Panel composition', 'Unique CHM13 variants in panel',
value_chm13=float(number_unique_CHM13_variants),
paper_text=f'The resulting panel contains 6,404 haplotypes and {number_unique_CHM13_variants:,} unique variants, including {number_unique_syntenic_CHM13_variants:,} high-confidence phased variants in previously unresolved regions of the T2T-CHM13 genome.',
paper_location='Results: panel composition paragraph',
notes='Selected value after reconciliation; count is CHM13 criteria-pass variants in filter_summary_stats.')
add_stat('Panel composition', 'Unique CHM13 novel-region variants in panel',
value_chm13=float(number_unique_syntenic_CHM13_variants),
paper_location='Results: panel composition paragraph',
notes='Selected value after reconciliation; nonsyntenic CHM13 criteria-pass variants.')
# -- Pedigree composition --
pedigree_note = 'Target 1kGP category count used for Figure 2/panel-wide weighting; sample roles can overlap in the raw pedigree.'
add_stat('Pedigree composition', 'Number of trio/duo probands',
value_chm13=float(target_probands),
paper_text=f'As a result, we expect probands ({target_probands/3202*100:.0f}% of 1kGP samples) and trio parents ({target_parents/3202*100:.0f}% of 1kGP) to be more accurately phased than samples that are not part of a trio ({target_nontrios/3202*100:.0f}%).',
paper_location='Results: phasing accuracy paragraph',
notes=pedigree_note)
add_stat('Pedigree composition', 'Number of trio/duo parents',
value_chm13=float(target_parents),
paper_location='Results: phasing accuracy paragraph',
notes=pedigree_note)
add_stat('Pedigree composition', 'Number of statistically phased samples',
value_chm13=float(target_nontrios),
paper_location='Results: phasing accuracy paragraph',
notes=pedigree_note)
# -- Panel-wide SER --
for _, row in panel_wide_category_ser.iterrows():
add_stat('Panel-wide SER components',
f"Panel-wide SER component ({row.genome}; {row.panel_category})",
value_chm13=float(row.switch_error_rate) if 'CHM13' in str(row.genome) else np.nan,
value_grch38=float(row.switch_error_rate) if 'GRCh38' in str(row.genome) else np.nan,
paper_location='Results: phasing accuracy paragraph / Figure 2 estimated panel-wide row',
notes=(f"ground_truth_data_source={row.ground_truth_data_source}; "
f"target_count={int(row.target_count)}; evaluated_sample_count={int(row.evaluated_sample_count)}"))
for _, row in panel_wide_ser.iterrows():
add_stat('Panel-wide SER', f'Weighted panel-wide SER ({row.genome})',
value_chm13=float(row.switch_error_rate) if 'CHM13' in str(row.genome) else np.nan,
value_grch38=float(row.switch_error_rate) if 'GRCh38' in str(row.genome) else np.nan,
paper_text=f'Weighting the observed proband/parent/non-trio SER by panel membership ({target_probands:,} probands, {target_parents:,} parents, and {target_nontrios:,} non-trio individuals) allowed us to estimate a {target_probands + target_parents + target_nontrios:,}-member panel-wide SER of {grch38_pw_ser:.2f}% for the GRCh38 panel and {chm13_pw_ser:.2f}% for the T2T-CHM13 panel.' if 'GRCh38' in str(row.genome) else '',
paper_location='Results: phasing accuracy paragraph',
notes='Weighted average of proband, parent, and non-trio category SER using target counts.')
# -- ChrX SER --
for _, row in chrX_ser_grouped.iterrows():
g = row.genome
add_stat('ChrX accuracy', f'ChrX SER ({g})',
value_chm13=float(row.switch_error_rate) if 'CHM13' in str(g) else np.nan,
value_grch38=float(row.switch_error_rate) if 'GRCh38' in str(g) else np.nan,
paper_text=f'In statistically-phased HGSVC non-trio samples, chromosome X SER decreased from {chrX_ser_grouped.loc[chrX_ser_grouped.genome == "GRCh38", "switch_error_rate"].values[0]:.2f}% in the GRCh38 panel to {chrX_ser_grouped.loc[chrX_ser_grouped.genome == "CHM13v2.0", "switch_error_rate"].values[0]:.2f}% in the T2T-CHM13 panel.' if 'GRCh38' in str(g) else '',
paper_location='Results: phasing accuracy paragraph')
# -- Switch error decomposition --
for _, row in hprc_proband_grouped.iterrows():
g = row.genome
add_stat('Switch error decomposition', f'HPRC proband SER ({g})',
value_chm13=float(row.switch_error_rate) if 'CHM13' in str(g) else np.nan,
value_grch38=float(row.switch_error_rate) if 'GRCh38' in str(g) else np.nan,
paper_location='Summary; Introduction; Discussion',
notes='HPRC-only switch error rate used for the headline phasing switch-error reduction.')
add_stat('Switch error decomposition', f'HPRC proband FER ({g})',
value_chm13=float(row.flip_error_rate) if 'CHM13' in str(g) else np.nan,
value_grch38=float(row.flip_error_rate) if 'GRCh38' in str(g) else np.nan,
paper_location='Results: switch error decomposition')
add_stat('Switch error decomposition', f'HPRC proband tSER ({g})',
value_chm13=float(row.true_switch_error_rate) if 'CHM13' in str(g) else np.nan,
value_grch38=float(row.true_switch_error_rate) if 'GRCh38' in str(g) else np.nan,
paper_text=f'The measured flip error rate for HPRC probands from the 1kGP GRCh38 panel was {grch38_fer:.3f}%, while the tSER was {grch38_proband_tser:.3f}%. HPRC probands from the T2T-CHM13 panel had a flip error rate of {chm13_fer:.3f}% and a tSER of {chm13_proband_tser:.3f}% (Figure 3b).' if 'GRCh38' in str(g) else '',
paper_location='Results: switch error decomposition')
# -- Switch-error correlation / regression values --
if isinstance(error_rate_corr, pd.DataFrame) and not error_rate_corr.empty:
corr_export_labels = {
'Log(Switch Error Rate)': 'SER',
'Log(Flip Error Rate)': 'FER',
'Log(True Switch Error Rate)': 'tSER',
}
corr_rows = error_rate_corr.loc[error_rate_corr['Mendelian-based error correction'].astype(str).eq('True')]
for _, row in corr_rows.iterrows():
label = corr_export_labels.get(row['Log error rate'], row['Log error rate'])
add_stat(
'Switch-error correlation',
f'Pearson r^2 genotype-error vs {label} (HPRC probands, Mendelian-corrected)',
value_chm13=float(row['CHM13v2.0']),
value_grch38=float(row['GRCh38']),
paper_location='Results: switch error decomposition',
notes=('Separate CSV export: figures_whole_genome/correlation_error_rates_and_gt_error_rate.csv; '
'Pearson r^2 from log-transformed rates.'),
)
# -- ChrX tSER --
for _, row in chrX_grouped.iterrows():
g = row.genome
add_stat('ChrX accuracy', f'ChrX tSER HPRC ({g})',
value_chm13=float(row.true_switch_error_rate) if 'CHM13' in str(g) else np.nan,
value_grch38=float(row.true_switch_error_rate) if 'GRCh38' in str(g) else np.nan,
paper_text=f'When stratified by chromosome, we observed a {chrx_tser_fold_change:.0f}-fold reduction in tSERs on the chromosome X (GRCh38: {grch38_chrx_tser:.2f}% tSER; T2T-CHM13: {chm13_chrx_tser:.3f}% tSER).' if 'GRCh38' in str(g) else '',
paper_location='Results: switch error decomposition')
# -- Summary / derived --
add_stat('Summary', 'Percent fewer assembly-discordant genotypes (HPRC samples; CHM13 vs GRCh38)',
value_chm13=pct_fewer_discordant,
paper_text=f'Among HPRC samples with published long-read-based assemblies, alignment to T2T-CHM13 resulted in {pct_fewer_discordant:.0f}% fewer assembly-discordant genotypes and {pct_fewer_switch_errors:.0f}% fewer phasing switch errors',
paper_location='Summary; Introduction; Discussion',
notes='Derived from HPRC-only overall concordance (per-variant TSV) and HPRC-only SER.')
add_stat('Summary', 'Percent fewer switch errors (HPRC samples; SER)',
value_chm13=pct_fewer_switch_errors,
paper_location='Summary; Introduction; Discussion',
notes='Derived from HPRC-only SER using samples with published long-read-based assemblies.')
add_stat('Summary', 'Percent lower tSER (HPRC probands)',
value_chm13=pct_fewer_tser,
paper_text=f'Among HPRC samples with published long-read-based assemblies, true phasing error rates are {pct_fewer_tser:.0f}% lower in the T2T-CHM13 panel genome-wide and {chrx_tser_fold_change:.0f}-fold lower on chromosome X',
paper_location='Summary; Discussion',
notes='Derived from HPRC proband tSER and ChrX tSER.')
paper_inline_statistics_df = pd.DataFrame(paper_inline_rows)
os.makedirs('tables', exist_ok=True)
paper_inline_statistics_path = f'{tables_folder}/paper_inline_statistics_reference.tsv'
paper_inline_statistics_df.to_csv(paper_inline_statistics_path, sep='\t', index=False)
print(f'Wrote: {paper_inline_statistics_path}')
paper_inline_statistics_df