PGx Analyzer Team commited on
Commit
249ddf4
·
1 Parent(s): e191b1b

Implement client feedback: fix QC duplicate counting, highlight HWE Chi2 > 3.84, remove interpretation column, add *3 and *17 regional breakdown and regional phenotype matrix

Browse files
Files changed (2) hide show
  1. app.py +48 -22
  2. core/qc.py +2 -3
app.py CHANGED
@@ -571,16 +571,17 @@ def render_unified_results(full_results: dict, qc_report: dict):
571
  hw = s_res.get('hwe', {})
572
  gc = s_res.get('genotype_counts', {})
573
  exp_c = hw.get('expected_counts', {})
 
 
574
  hwe_table_rows.append({
575
  'SNP Variant': snp_name,
576
  'Valid Samples (N)': s_res.get('valid_samples', 0),
577
  'Observed Genotypes': " / ".join([f"{k}:{v}" for k, v in gc.items()]),
578
  'Expected Genotypes': " / ".join([f"{k}:{exp_c.get(k, 0)}" for k in gc.keys()]),
579
- 'Chi-Square (χ²)': hw.get('chi2_stat', 0),
580
  'Degrees of Freedom': hw.get('df', 1),
581
- 'P-Value': hw.get('p_value', 1.0),
582
- 'Exact Test P-Value': hw.get('exact_p_value', 1.0),
583
- 'Interpretation': hw.get('interpretation', 'In HWE')
584
  })
585
 
586
  c_hwe_tbl, c_hwe_fig = st.columns([1.2, 0.8])
@@ -800,30 +801,55 @@ def render_unified_results(full_results: dict, qc_report: dict):
800
  st.plotly_chart(fig_geo_pm, use_container_width=True)
801
 
802
  st.markdown("#### Comprehensive Regional Statistical Summary Table")
 
803
 
804
- cyp2_rows = [
805
- ('A Count (Variant Allele)', lambda s: s.get('CYP2C19*2', {}).get('allele_counts', {}).get('A', 0)),
806
- ('G Count (Reference Allele)', lambda s: s.get('CYP2C19*2', {}).get('allele_counts', {}).get('G', 0)),
807
- ('Total Allele Count (2N)', lambda s: sum(s.get('CYP2C19*2', {}).get('allele_counts', {}).values())),
808
- ('GA COUNT (Heterozygote)', lambda s: s.get('CYP2C19*2', {}).get('genotype_counts', {}).get('GA', 0)),
809
- ('AA COUNT (Homozygous Variant)', lambda s: s.get('CYP2C19*2', {}).get('genotype_counts', {}).get('AA', 0)),
810
- ('GG COUNT (Homozygous Wildtype)', lambda s: s.get('CYP2C19*2', {}).get('genotype_counts', {}).get('GG', 0)),
811
- ('Total Sample N', lambda s: sum(s.get('CYP2C19*2', {}).get('genotype_counts', {}).values())),
812
- ('Allele Frequency F(A)', lambda s: s.get('CYP2C19*2', {}).get('allele_freqs', {}).get('A', 0)),
813
- ('Allele Frequency F(G)', lambda s: s.get('CYP2C19*2', {}).get('allele_freqs', {}).get('G', 0)),
814
- ('Chi-Square (χ²)', lambda s: s.get('CYP2C19*2', {}).get('hwe', {}).get('chi2_stat', 0)),
815
- ('P VALUE', lambda s: s.get('CYP2C19*2', {}).get('hwe', {}).get('p_value', 1.0)),
816
- ('HWE Interpretation', lambda s: s.get('CYP2C19*2', {}).get('hwe', {}).get('interpretation', 'In HWE'))
 
 
 
 
 
 
817
  ]
818
 
819
- cyp2_matrix = []
820
- for label, func in cyp2_rows:
821
- r_dict = {'Statistical Parameter (*2)': label}
822
  for r in regions:
823
  r_dict[r] = func(reg.get(r, {}).get('snps', {}))
824
- cyp2_matrix.append(r_dict)
825
 
826
- st.dataframe(pd.DataFrame(cyp2_matrix), use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
827
 
828
  # -------------------------------------------------------------------------
829
  # TAB 8: GENDER-WISE ANALYSIS
 
571
  hw = s_res.get('hwe', {})
572
  gc = s_res.get('genotype_counts', {})
573
  exp_c = hw.get('expected_counts', {})
574
+ chi2_val = hw.get('chi2_stat', 0.0)
575
+ chi2_str = f"⚠️ {chi2_val:.4f} (Chi² > 3.841)" if chi2_val >= 3.841 else f"{chi2_val:.4f}"
576
  hwe_table_rows.append({
577
  'SNP Variant': snp_name,
578
  'Valid Samples (N)': s_res.get('valid_samples', 0),
579
  'Observed Genotypes': " / ".join([f"{k}:{v}" for k, v in gc.items()]),
580
  'Expected Genotypes': " / ".join([f"{k}:{exp_c.get(k, 0)}" for k in gc.keys()]),
581
+ 'Chi-Square (χ²)': chi2_str,
582
  'Degrees of Freedom': hw.get('df', 1),
583
+ 'P-Value': f"{hw.get('p_value', 1.0):.4f}",
584
+ 'Exact Test P-Value': f"{hw.get('exact_p_value', 1.0):.4f}"
 
585
  })
586
 
587
  c_hwe_tbl, c_hwe_fig = st.columns([1.2, 0.8])
 
801
  st.plotly_chart(fig_geo_pm, use_container_width=True)
802
 
803
  st.markdown("#### Comprehensive Regional Statistical Summary Table")
804
+ sel_reg_snp = st.selectbox("Select Target SNP Variant for Regional Breakdown:", ["CYP2C19*2", "CYP2C19*3", "CYP2C19*17"], index=0, key="sb_reg_snp_select")
805
 
806
+ snp_conf_sel = SNP_CONFIG.get(sel_reg_snp, {})
807
+ ref_al = snp_conf_sel.get('ref_allele', 'G')
808
+ var_al = snp_conf_sel.get('var_allele', 'A')
809
+ wt_g = snp_conf_sel.get('wildtype_genotype', 'GG')
810
+ het_g = snp_conf_sel.get('het_genotype', 'GA')
811
+ var_g = snp_conf_sel.get('hom_var_genotype', 'AA')
812
+
813
+ reg_rows_config = [
814
+ (f'{var_al} Count (Variant Allele)', lambda s, snp=sel_reg_snp, va=var_al: s.get(snp, {}).get('allele_counts', {}).get(va, 0)),
815
+ (f'{ref_al} Count (Reference Allele)', lambda s, snp=sel_reg_snp, ra=ref_al: s.get(snp, {}).get('allele_counts', {}).get(ra, 0)),
816
+ ('Total Allele Count (2N)', lambda s, snp=sel_reg_snp: sum(s.get(snp, {}).get('allele_counts', {}).values())),
817
+ (f'{het_g} COUNT (Heterozygote)', lambda s, snp=sel_reg_snp, hg=het_g: s.get(snp, {}).get('genotype_counts', {}).get(hg, 0)),
818
+ (f'{var_g} COUNT (Homozygous Variant)', lambda s, snp=sel_reg_snp, vg=var_g: s.get(snp, {}).get('genotype_counts', {}).get(vg, 0)),
819
+ (f'{wt_g} COUNT (Homozygous Wildtype)', lambda s, snp=sel_reg_snp, wg=wt_g: s.get(snp, {}).get('genotype_counts', {}).get(wg, 0)),
820
+ ('Total Sample N', lambda s, snp=sel_reg_snp: sum(s.get(snp, {}).get('genotype_counts', {}).values())),
821
+ (f'Allele Frequency F({var_al})', lambda s, snp=sel_reg_snp, va=var_al: s.get(snp, {}).get('allele_freqs', {}).get(va, 0)),
822
+ (f'Allele Frequency F({ref_al})', lambda s, snp=sel_reg_snp, ra=ref_al: s.get(snp, {}).get('allele_freqs', {}).get(ra, 0)),
823
+ ('Chi-Square (χ²)', lambda s, snp=sel_reg_snp: s.get(snp, {}).get('hwe', {}).get('chi2_stat', 0)),
824
+ ('P VALUE', lambda s, snp=sel_reg_snp: s.get(snp, {}).get('hwe', {}).get('p_value', 1.0))
825
  ]
826
 
827
+ reg_matrix = []
828
+ for label, func in reg_rows_config:
829
+ r_dict = {f'Statistical Parameter ({sel_reg_snp})': label}
830
  for r in regions:
831
  r_dict[r] = func(reg.get(r, {}).get('snps', {}))
832
+ reg_matrix.append(r_dict)
833
 
834
+ st.dataframe(pd.DataFrame(reg_matrix), use_container_width=True)
835
+
836
+ st.markdown("<br>", unsafe_allow_html=True)
837
+ st.markdown("#### 💊 Regional & State-Wise Metabolizer Phenotype Distribution Matrix")
838
+ st.caption("Distribution of CPIC CYP2C19 Metabolizer Phenotypes across South, North, East, West, and Central India.")
839
+
840
+ reg_pheno_matrix = []
841
+ for r_name in regions:
842
+ r_data = reg.get(r_name, {})
843
+ p_dict = r_data.get('cyp2c19_summary', {}).get('phenotypes', {})
844
+ row = {'Population Region': r_name, 'Total Sample N': r_data.get('sample_count', 0)}
845
+ for p_cat in PHENOTYPE_ORDER:
846
+ cnt = p_dict.get(p_cat, {}).get('count', 0)
847
+ pct = p_dict.get(p_cat, {}).get('percentage', 0.0)
848
+ p_label = p_cat.split(' (')[0]
849
+ row[p_label] = f"{cnt} ({pct:.1f}%)"
850
+ reg_pheno_matrix.append(row)
851
+
852
+ st.table(pd.DataFrame(reg_pheno_matrix))
853
 
854
  # -------------------------------------------------------------------------
855
  # TAB 8: GENDER-WISE ANALYSIS
core/qc.py CHANGED
@@ -96,10 +96,9 @@ class DataQCEngine:
96
  sample_ids = self._get_series(df, 'Sample_ID').astype(str).str.strip()
97
  df['Sample_ID'] = sample_ids
98
 
99
- # Check duplicates within batch and against existing DB records
100
  duplicated_in_file = df[df['Sample_ID'].duplicated()]['Sample_ID'].tolist()
101
- duplicated_in_db = df[df['Sample_ID'].isin(self.existing_sample_ids)]['Sample_ID'].tolist()
102
- all_duplicates = list(set(duplicated_in_file + duplicated_in_db))
103
 
104
  # Clean Gender
105
  if 'Gender' in df.columns:
 
96
  sample_ids = self._get_series(df, 'Sample_ID').astype(str).str.strip()
97
  df['Sample_ID'] = sample_ids
98
 
99
+ # Check duplicates within batch file
100
  duplicated_in_file = df[df['Sample_ID'].duplicated()]['Sample_ID'].tolist()
101
+ all_duplicates = list(set(duplicated_in_file))
 
102
 
103
  # Clean Gender
104
  if 'Gender' in df.columns: