-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add GO enrichment analysis page for ProteomicsLFQ results #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hjn0415a
wants to merge
1
commit into
OpenMS:main
Choose a base branch
from
hjn0415a:feature/go-terms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| from pathlib import Path | ||
| import streamlit as st | ||
| import pandas as pd | ||
| import numpy as np | ||
| import plotly.express as px | ||
| import mygene | ||
| from collections import defaultdict | ||
| from scipy.stats import ttest_ind, fisher_exact | ||
|
|
||
| from src.common.common import page_setup | ||
| from src.common.results_helpers import get_abundance_data, get_workflow_dir | ||
|
|
||
| params = page_setup() | ||
| st.title("ProteomicsLFQ Results") | ||
|
|
||
| if "workspace" not in st.session_state: | ||
| st.warning("Please initialize your workspace first.") | ||
| st.stop() | ||
|
|
||
| res = get_abundance_data(st.session_state["workspace"]) | ||
| if res is None: | ||
| st.info("Abundance data not available or incomplete. Please run the workflow and configure sample groups first.") | ||
| st.stop() | ||
|
|
||
| pivot_df, expr_df, group_map = res | ||
|
|
||
| protein_tab, = st.tabs(["🧬 Protein Table"]) | ||
|
|
||
| # Protein-level tab | ||
| with protein_tab: | ||
| st.markdown("### 🧬 Protein-Level Abundance Table") | ||
| st.info( | ||
| "This protein-level table is generated by grouping all PSMs that map to the " | ||
| "same protein and aggregating their intensities across samples.\n\n" | ||
| "Additionally, log2 fold change and p-values are calculated between sample groups." | ||
| ) | ||
|
|
||
| if pivot_df.empty: | ||
| st.info("No protein-level data available.") | ||
| else: | ||
| st.session_state["pivot_df"] = pivot_df | ||
| st.dataframe(pivot_df.sort_values("p-value"), use_container_width=True) | ||
|
|
||
| st.markdown("---") | ||
| st.subheader("🧬 GO Enrichment Analysis") | ||
|
|
||
| p_cutoff = st.slider("Select p-value threshold for Foreground Proteins", 0.01, 0.50, 0.05) | ||
| fc_cutoff = st.slider("Select |log2FC| threshold for Foreground Proteins", 0.0, 5.0, 1.0, step=0.1) | ||
|
|
||
| if st.button("Run GO Enrichment"): | ||
| analysis_df = pivot_df.dropna(subset=["p-value", "log2FC"]).copy() | ||
| if analysis_df.empty: | ||
| st.error("No valid statistical data found.") | ||
| else: | ||
| with st.spinner("Fetching GO terms from MyGene.info API..."): | ||
| try: | ||
| mg = mygene.MyGeneInfo() | ||
|
|
||
| def get_clean_uniprot(name): | ||
| try: | ||
| parts = str(name).split("|") | ||
| return parts[1] if len(parts) >= 2 else parts[0] | ||
| except Exception: | ||
| return None | ||
|
|
||
| analysis_df["UniProt"] = analysis_df["ProteinName"].apply(get_clean_uniprot) | ||
|
|
||
| bg_ids = analysis_df["UniProt"].dropna().unique().tolist() | ||
| fg_ids = analysis_df[ | ||
| (analysis_df["p-value"] < p_cutoff) & | ||
| (analysis_df["log2FC"].abs() >= fc_cutoff) | ||
| ]["UniProt"].dropna().unique().tolist() | ||
|
|
||
| if len(fg_ids) < 3: | ||
| st.warning(f"Not enough significant proteins (p < {p_cutoff}, |log2FC| ≥ {fc_cutoff}). Found: {len(fg_ids)}") | ||
| else: | ||
| res_list = mg.querymany(bg_ids, scopes="uniprot", fields="go", as_dataframe=False) | ||
| res = pd.DataFrame(res_list) | ||
| if "notfound" in res.columns: | ||
| res = res[res["notfound"] != True] | ||
|
|
||
| def extract_go_terms(go_data, go_type): | ||
| if not isinstance(go_data, dict) or go_type not in go_data: | ||
| return [] | ||
| terms = go_data[go_type] | ||
| if isinstance(terms, dict): | ||
| terms = [terms] | ||
| return list({t.get("term") for t in terms if "term" in t}) | ||
|
|
||
| for go_type in ["BP", "CC", "MF"]: | ||
| res[f"{go_type}_terms"] = res["go"].apply(lambda x: extract_go_terms(x, go_type)) | ||
|
|
||
| fg_set = set(fg_ids) | ||
| bg_set = set(bg_ids) | ||
|
|
||
| def run_go_enrichment(go_type): | ||
| go2fg = defaultdict(set) | ||
| go2bg = defaultdict(set) | ||
| for _, row in res.iterrows(): | ||
| uid = str(row["query"]) | ||
| for term in row[f"{go_type}_terms"]: | ||
| go2bg[term].add(uid) | ||
| if uid in fg_set: | ||
| go2fg[term].add(uid) | ||
|
|
||
| records = [] | ||
| N_fg = len(fg_set) | ||
| N_bg = len(bg_set) | ||
| for term, fg_genes in go2fg.items(): | ||
| a = len(fg_genes) | ||
| if a == 0: | ||
| continue | ||
| b = N_fg - a | ||
| c = len(go2bg[term]) - a | ||
| d = N_bg - (a + b + c) | ||
| _, p = fisher_exact([[a, b], [c, d]], alternative="greater") | ||
| records.append({"GO_Term": term, "Count": a, "GeneRatio": f"{a}/{N_fg}", "p_value": p}) | ||
|
|
||
| df_go = pd.DataFrame(records) | ||
| if not df_go.empty: | ||
| df_go["-log10(p)"] = -np.log10(df_go["p_value"].replace(0, 1e-10)) | ||
| df_go = df_go.sort_values("p_value") | ||
| return df_go | ||
|
|
||
| enrich_results = {go: run_go_enrichment(go) for go in ["BP", "CC", "MF"]} | ||
|
|
||
| bp_tab, cc_tab, mf_tab = st.tabs(["🧬 Biological Process", "🏠 Cellular Component", "⚙️ Molecular Function"]) | ||
| for tab, go_type in zip([bp_tab, cc_tab, mf_tab], ["BP", "CC", "MF"]): | ||
| with tab: | ||
| df_go = enrich_results[go_type] | ||
| if df_go.empty: | ||
| st.warning(f"No enriched {go_type} terms found.") | ||
| continue | ||
| fig = px.bar(df_go.head(15), x="-log10(p)", y="GO_Term", orientation="h", text="GeneRatio", color="-log10(p)", color_continuous_scale="Viridis") | ||
| fig.update_layout(yaxis={"categoryorder": "total ascending"}, margin=dict(l=300)) | ||
| st.plotly_chart(fig, use_container_width=True) | ||
| st.dataframe(df_go, use_container_width=True) | ||
|
|
||
| st.success("GO Enrichment analysis completed successfully.") | ||
| except Exception as e: | ||
| st.error(f"GO enrichment failed: {e}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,3 +149,4 @@ pyprophet>=2.2.0 | |
| # Redis Queue dependencies (for online mode) | ||
| redis>=5.0.0 | ||
| rq>=1.16.0 | ||
| mygene | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Foreground/background counts include unannotated proteins, biasing Fisher p-values.
N_bg/N_fgare computed from all proteins, even those without GO annotations. This inflates the background and can understate enrichment. Restrict both sets to annotated proteins returned by MyGene before computing Fisher’s exact test.Proposed fix
🧰 Tools
🪛 Ruff (0.14.14)
[error] 80-80: Avoid inequality comparisons to
True; usenot res["notfound"]:for false checksReplace with
not res["notfound"](E712)
[warning] 91-91: Function definition does not bind loop variable
go_type(B023)
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hjn0415a Could you check this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.