Spaces:
Configuration error
Configuration error
PGx Analyzer Team commited on
Commit Β·
c7f0385
1
Parent(s): 8491263
Enforce strictly ONE primary visualization per page across all 11 application views
Browse files
app.py
CHANGED
|
@@ -412,24 +412,13 @@ elif nav_option == "π 3. Executive Dashboard":
|
|
| 412 |
k3.metric("Intermediate Metabolizers (IM)", f"{pgx.get('Intermediate Metabolizer (IM)', {}).get('percentage', 0)}%")
|
| 413 |
k4.metric("CYP2C19*2 HWE Status", ov.get('snps', {}).get('CYP2C19*2', {}).get('hwe', {}).get('interpretation', 'In HWE'))
|
| 414 |
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
with ch2:
|
| 424 |
-
st.markdown("### π Regional Variant Allele Frequency Comparison")
|
| 425 |
-
reg = full_results['regional']
|
| 426 |
-
r_names = ['South India', 'North India', 'East India', 'West India', 'Central India']
|
| 427 |
-
cyp2_freqs = [reg.get(r, {}).get('snps', {}).get('CYP2C19*2', {}).get('allele_freqs', {}).get('A', 0) for r in r_names]
|
| 428 |
-
cyp17_freqs = [reg.get(r, {}).get('snps', {}).get('CYP2C19*17', {}).get('allele_freqs', {}).get('T', 0) for r in r_names]
|
| 429 |
-
|
| 430 |
-
comp_df = pd.DataFrame({'Region': r_names, 'CYP2C19*2 (Var A)': cyp2_freqs, 'CYP2C19*17 (Var T)': cyp17_freqs})
|
| 431 |
-
fig_reg = px.bar(comp_df, x='Region', y=['CYP2C19*2 (Var A)', 'CYP2C19*17 (Var T)'], barmode='group', title="Allele Frequencies by Region")
|
| 432 |
-
st.plotly_chart(fig_reg, use_container_width=True)
|
| 433 |
|
| 434 |
# -----------------------------------------------------------------------------
|
| 435 |
# VIEW 4: DATA QUALITY & MISSING AUDIT
|
|
@@ -457,6 +446,15 @@ elif nav_option == "π‘οΈ 4. Data Quality & Missing Audit":
|
|
| 457 |
})
|
| 458 |
st.table(pd.DataFrame(qc_rows))
|
| 459 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
if qc_report['duplicate_sample_ids']:
|
| 461 |
st.warning(f"Duplicate Sample IDs Detected: {', '.join(qc_report['duplicate_sample_ids'][:10])}")
|
| 462 |
|
|
@@ -489,6 +487,15 @@ elif nav_option == "𧬠5. Genotype & Allele Frequencies":
|
|
| 489 |
a_f = s_res['allele_freqs']
|
| 490 |
al_df = pd.DataFrame([{'Allele': k, 'Count': v, 'Frequency (p/q)': a_f.get(k, 0), 'Percentage': f"{a_p.get(k, 0)}%"} for k, v in a_c.items()])
|
| 491 |
st.table(al_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
# -----------------------------------------------------------------------------
|
| 494 |
# VIEW 6: HARDY-WEINBERG EQUILIBRIUM
|
|
@@ -514,6 +521,18 @@ elif nav_option == "βοΈ 6. Hardy-Weinberg Equilibrium":
|
|
| 514 |
'HWE Status': hw['interpretation']
|
| 515 |
})
|
| 516 |
st.table(pd.DataFrame(hwe_rows))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
# -----------------------------------------------------------------------------
|
| 519 |
# VIEW 7: GEOGRAPHIC POPULATION GROUPS
|
|
@@ -607,6 +626,14 @@ elif nav_option == "π 7. Geographic Population Groups":
|
|
| 607 |
r_phenos = r_active.get('cyp2c19_summary', {}).get('phenotypes', {})
|
| 608 |
p_list = [{'Phenotype': k, 'Count': v['count'], 'Percentage': f"{v['percentage']}%"} for k, v in r_phenos.items()]
|
| 609 |
st.table(pd.DataFrame(p_list))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
|
| 611 |
# -----------------------------------------------------------------------------
|
| 612 |
# VIEW 8: GENDER-WISE ANALYSIS
|
|
@@ -687,39 +714,20 @@ elif nav_option == "π« 8. Gender-Wise Analysis":
|
|
| 687 |
})
|
| 688 |
st.dataframe(pd.DataFrame(g_rows), use_container_width=True)
|
| 689 |
|
| 690 |
-
|
| 691 |
-
st.markdown("### π
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
{'Gender': '
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
{'Gender': 'Female', 'Genotype': 'AA', 'Count': f_gc.get('AA', 0)}
|
| 705 |
-
])
|
| 706 |
-
fig_g1 = px.bar(gen_chart_df, x='Genotype', y='Count', color='Gender', barmode='group', title="CYP2C19*2 Genotype Counts by Gender")
|
| 707 |
-
st.plotly_chart(fig_g1, use_container_width=True)
|
| 708 |
-
|
| 709 |
-
with gc2:
|
| 710 |
-
st.markdown("**CPIC Phenotype Distributions by Gender**")
|
| 711 |
-
m_phenos = male.get('cyp2c19_summary', {}).get('phenotypes', {})
|
| 712 |
-
f_phenos = female.get('cyp2c19_summary', {}).get('phenotypes', {})
|
| 713 |
-
|
| 714 |
-
pheno_chart_df = []
|
| 715 |
-
for p_name in PHENOTYPE_ORDER:
|
| 716 |
-
if p_name in m_phenos:
|
| 717 |
-
pheno_chart_df.append({'Gender': 'Male', 'Phenotype': p_name.split(' (')[0], 'Count': m_phenos[p_name]['count']})
|
| 718 |
-
if p_name in f_phenos:
|
| 719 |
-
pheno_chart_df.append({'Gender': 'Female', 'Phenotype': p_name.split(' (')[0], 'Count': f_phenos[p_name]['count']})
|
| 720 |
-
|
| 721 |
-
fig_g2 = px.bar(pd.DataFrame(pheno_chart_df), x='Phenotype', y='Count', color='Gender', barmode='group', title="Metabolizer Phenotypes by Gender")
|
| 722 |
-
st.plotly_chart(fig_g2, use_container_width=True)
|
| 723 |
|
| 724 |
# -----------------------------------------------------------------------------
|
| 725 |
# VIEW 9: DEDICATED STATE-WISE PAGES
|
|
@@ -755,6 +763,13 @@ elif nav_option == "π 9. Dedicated State-Wise Pages":
|
|
| 755 |
st.markdown(f"**State Phenotype Breakdown ({selected_st})**")
|
| 756 |
phenos = st_data.get('cyp2c19_summary', {}).get('phenotypes', {})
|
| 757 |
st.table(pd.DataFrame([{'Phenotype': k, 'Count': v['count'], 'Percentage': f"{v['percentage']}%"} for k, v in phenos.items()]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 758 |
|
| 759 |
# -----------------------------------------------------------------------------
|
| 760 |
# VIEW 10: CYP2C19 CALLING & PHENOTYPES
|
|
@@ -774,6 +789,12 @@ elif nav_option == "π 10. CYP2C19 Calling & Phenotypes":
|
|
| 774 |
with cp2:
|
| 775 |
st.markdown("### 𧬠Diplotype Call Matrix (*1/*1, *1/*2, *2/*17, etc.)")
|
| 776 |
st.table(pd.DataFrame([{'Diplotype Call': k, 'Count': v['count'], 'Frequency': v['frequency'], 'Percentage': f"{v['percentage']}%"} for k, v in pgx['diplotypes'].items()]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
|
| 778 |
# -----------------------------------------------------------------------------
|
| 779 |
# VIEW 11: LIVE PREVIEW & DOWNLOAD REPORT
|
|
|
|
| 412 |
k3.metric("Intermediate Metabolizers (IM)", f"{pgx.get('Intermediate Metabolizer (IM)', {}).get('percentage', 0)}%")
|
| 413 |
k4.metric("CYP2C19*2 HWE Status", ov.get('snps', {}).get('CYP2C19*2', {}).get('hwe', {}).get('interpretation', 'In HWE'))
|
| 414 |
|
| 415 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 416 |
+
st.markdown("### π© Primary Visual: Overall CPIC Metabolizer Phenotype Breakdown")
|
| 417 |
+
st.caption("Distribution of Ultrarapid (UM), Rapid (RM), Normal (NM), Intermediate (IM), and Poor Metabolizers (PM) in the cohort.")
|
| 418 |
+
p_labels = [k.replace(' Metabolizer', '') for k, v in pgx.items() if v['count'] > 0]
|
| 419 |
+
p_vals = [v['count'] for k, v in pgx.items() if v['count'] > 0]
|
| 420 |
+
fig_pie = px.pie(names=p_labels, values=p_vals, hole=0.45, color_discrete_sequence=px.colors.qualitative.Set2, title="Overall Metabolizer Phenotypes Donut Chart")
|
| 421 |
+
st.plotly_chart(fig_pie, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
# -----------------------------------------------------------------------------
|
| 424 |
# VIEW 4: DATA QUALITY & MISSING AUDIT
|
|
|
|
| 446 |
})
|
| 447 |
st.table(pd.DataFrame(qc_rows))
|
| 448 |
|
| 449 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 450 |
+
st.markdown("### π Primary Visual: Valid Samples & Missing Genotypes Breakdown per SNP")
|
| 451 |
+
qc_chart_df = []
|
| 452 |
+
for snp, sdata in qc_report['snp_qc_stats'].items():
|
| 453 |
+
qc_chart_df.append({'SNP Variant': snp, 'Status': 'Valid Samples', 'Count': sdata['valid_samples']})
|
| 454 |
+
qc_chart_df.append({'SNP Variant': snp, 'Status': 'Missing Genotypes', 'Count': sdata['missing_samples']})
|
| 455 |
+
fig_qc = px.bar(pd.DataFrame(qc_chart_df), x='SNP Variant', y='Count', color='Status', barmode='stack', title="Quality Control Audit per SNP", color_discrete_map={'Valid Samples': '#10B981', 'Missing Genotypes': '#EF4444'})
|
| 456 |
+
st.plotly_chart(fig_qc, use_container_width=True)
|
| 457 |
+
|
| 458 |
if qc_report['duplicate_sample_ids']:
|
| 459 |
st.warning(f"Duplicate Sample IDs Detected: {', '.join(qc_report['duplicate_sample_ids'][:10])}")
|
| 460 |
|
|
|
|
| 487 |
a_f = s_res['allele_freqs']
|
| 488 |
al_df = pd.DataFrame([{'Allele': k, 'Count': v, 'Frequency (p/q)': a_f.get(k, 0), 'Percentage': f"{a_p.get(k, 0)}%"} for k, v in a_c.items()])
|
| 489 |
st.table(al_df)
|
| 490 |
+
|
| 491 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 492 |
+
st.markdown("### π Primary Visual: Allele Frequency Distribution (p & q) Across Target SNPs")
|
| 493 |
+
af_chart_rows = []
|
| 494 |
+
for snp_name, s_res in snps_data.items():
|
| 495 |
+
for al, val in s_res['allele_freqs'].items():
|
| 496 |
+
af_chart_rows.append({'SNP Variant': snp_name, 'Allele': f"Allele {al}", 'Frequency (p/q)': val})
|
| 497 |
+
fig_af = px.bar(pd.DataFrame(af_chart_rows), x='SNP Variant', y='Frequency (p/q)', color='Allele', barmode='group', title="Allele Frequency (p & q) Comparison")
|
| 498 |
+
st.plotly_chart(fig_af, use_container_width=True)
|
| 499 |
|
| 500 |
# -----------------------------------------------------------------------------
|
| 501 |
# VIEW 6: HARDY-WEINBERG EQUILIBRIUM
|
|
|
|
| 521 |
'HWE Status': hw['interpretation']
|
| 522 |
})
|
| 523 |
st.table(pd.DataFrame(hwe_rows))
|
| 524 |
+
|
| 525 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 526 |
+
st.markdown("### π Primary Visual: Observed vs Expected Genotype Frequencies under HWE")
|
| 527 |
+
hwe_chart_rows = []
|
| 528 |
+
for snp_name, s_res in snps_data.items():
|
| 529 |
+
obs_dict = s_res['genotype_counts']
|
| 530 |
+
exp_dict = s_res['hwe']['expected_counts']
|
| 531 |
+
for gt in obs_dict.keys():
|
| 532 |
+
hwe_chart_rows.append({'Genotype': f"{snp_name} ({gt})", 'Category': 'Observed Count', 'Samples': obs_dict.get(gt, 0)})
|
| 533 |
+
hwe_chart_rows.append({'Genotype': f"{snp_name} ({gt})", 'Category': 'Expected Count (2Npq, Np^2, Nq^2)', 'Samples': exp_dict.get(gt, 0)})
|
| 534 |
+
fig_hwe = px.bar(pd.DataFrame(hwe_chart_rows), x='Genotype', y='Samples', color='Category', barmode='group', title="Observed vs Expected Genotype Frequencies under HWE")
|
| 535 |
+
st.plotly_chart(fig_hwe, use_container_width=True)
|
| 536 |
|
| 537 |
# -----------------------------------------------------------------------------
|
| 538 |
# VIEW 7: GEOGRAPHIC POPULATION GROUPS
|
|
|
|
| 626 |
r_phenos = r_active.get('cyp2c19_summary', {}).get('phenotypes', {})
|
| 627 |
p_list = [{'Phenotype': k, 'Count': v['count'], 'Percentage': f"{v['percentage']}%"} for k, v in r_phenos.items()]
|
| 628 |
st.table(pd.DataFrame(p_list))
|
| 629 |
+
|
| 630 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 631 |
+
st.markdown("### π Primary Visual: Regional Variant Allele Frequency Comparison")
|
| 632 |
+
cyp2_freqs = [reg.get(r, {}).get('snps', {}).get('CYP2C19*2', {}).get('allele_freqs', {}).get('A', 0) for r in regions]
|
| 633 |
+
cyp17_freqs = [reg.get(r, {}).get('snps', {}).get('CYP2C19*17', {}).get('allele_freqs', {}).get('T', 0) for r in regions]
|
| 634 |
+
reg_comp_df = pd.DataFrame({'Region': regions, 'CYP2C19*2 (Var A)': cyp2_freqs, 'CYP2C19*17 (Var T)': cyp17_freqs})
|
| 635 |
+
fig_reg = px.bar(reg_comp_df, x='Region', y=['CYP2C19*2 (Var A)', 'CYP2C19*17 (Var T)'], barmode='group', title="Variant Allele Frequencies across 5 Indian Geographic Regions")
|
| 636 |
+
st.plotly_chart(fig_reg, use_container_width=True)
|
| 637 |
|
| 638 |
# -----------------------------------------------------------------------------
|
| 639 |
# VIEW 8: GENDER-WISE ANALYSIS
|
|
|
|
| 714 |
})
|
| 715 |
st.dataframe(pd.DataFrame(g_rows), use_container_width=True)
|
| 716 |
|
| 717 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 718 |
+
st.markdown("### π Primary Visual: CPIC Metabolizer Phenotype Distributions by Gender")
|
| 719 |
+
m_phenos = male.get('cyp2c19_summary', {}).get('phenotypes', {})
|
| 720 |
+
f_phenos = female.get('cyp2c19_summary', {}).get('phenotypes', {})
|
| 721 |
+
|
| 722 |
+
pheno_chart_df = []
|
| 723 |
+
for p_name in PHENOTYPE_ORDER:
|
| 724 |
+
if p_name in m_phenos:
|
| 725 |
+
pheno_chart_df.append({'Gender': 'Male', 'Phenotype': p_name.split(' (')[0], 'Count': m_phenos[p_name]['count']})
|
| 726 |
+
if p_name in f_phenos:
|
| 727 |
+
pheno_chart_df.append({'Gender': 'Female', 'Phenotype': p_name.split(' (')[0], 'Count': f_phenos[p_name]['count']})
|
| 728 |
+
|
| 729 |
+
fig_g2 = px.bar(pd.DataFrame(pheno_chart_df), x='Phenotype', y='Count', color='Gender', barmode='group', title="Metabolizer Phenotypes Comparison (Male vs Female)")
|
| 730 |
+
st.plotly_chart(fig_g2, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 731 |
|
| 732 |
# -----------------------------------------------------------------------------
|
| 733 |
# VIEW 9: DEDICATED STATE-WISE PAGES
|
|
|
|
| 763 |
st.markdown(f"**State Phenotype Breakdown ({selected_st})**")
|
| 764 |
phenos = st_data.get('cyp2c19_summary', {}).get('phenotypes', {})
|
| 765 |
st.table(pd.DataFrame([{'Phenotype': k, 'Count': v['count'], 'Percentage': f"{v['percentage']}%"} for k, v in phenos.items()]))
|
| 766 |
+
|
| 767 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 768 |
+
st.markdown(f"### π© Primary Visual: CPIC Phenotype Breakdown for {selected_st}")
|
| 769 |
+
st_p_labels = [k.replace(' Metabolizer', '') for k, v in phenos.items() if v['count'] > 0]
|
| 770 |
+
st_p_vals = [v['count'] for k, v in phenos.items() if v['count'] > 0]
|
| 771 |
+
fig_st_pie = px.pie(names=st_p_labels, values=st_p_vals, hole=0.45, color_discrete_sequence=px.colors.qualitative.Pastel, title=f"Phenotype Distribution in {selected_st}")
|
| 772 |
+
st.plotly_chart(fig_st_pie, use_container_width=True)
|
| 773 |
|
| 774 |
# -----------------------------------------------------------------------------
|
| 775 |
# VIEW 10: CYP2C19 CALLING & PHENOTYPES
|
|
|
|
| 789 |
with cp2:
|
| 790 |
st.markdown("### 𧬠Diplotype Call Matrix (*1/*1, *1/*2, *2/*17, etc.)")
|
| 791 |
st.table(pd.DataFrame([{'Diplotype Call': k, 'Count': v['count'], 'Frequency': v['frequency'], 'Percentage': f"{v['percentage']}%"} for k, v in pgx['diplotypes'].items()]))
|
| 792 |
+
|
| 793 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 794 |
+
st.markdown("### π Primary Visual: CYP2C19 Diplotype Frequency Matrix")
|
| 795 |
+
dip_chart_rows = [{'Diplotype': k, 'Frequency': v['frequency'], 'Count': v['count']} for k, v in pgx['diplotypes'].items() if v['count'] > 0]
|
| 796 |
+
fig_dip = px.bar(pd.DataFrame(dip_chart_rows), x='Frequency', y='Diplotype', orientation='h', title="Population Diplotype Frequency Matrix (*1/*1, *1/*2, *2/*17, etc.)", color='Frequency', color_continuous_scale='Viridis')
|
| 797 |
+
st.plotly_chart(fig_dip, use_container_width=True)
|
| 798 |
|
| 799 |
# -----------------------------------------------------------------------------
|
| 800 |
# VIEW 11: LIVE PREVIEW & DOWNLOAD REPORT
|