Commit ·
ca40382
1
Parent(s): 723486d
Integrate real AlphaGenome API with fallback
Browse files- app.py +105 -58
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -3,6 +3,15 @@ import pandas as pd
|
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Page Config
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="Virtual Gene Scope",
|
|
@@ -10,25 +19,13 @@ st.set_page_config(
|
|
| 10 |
layout="wide"
|
| 11 |
)
|
| 12 |
|
| 13 |
-
# Custom CSS
|
| 14 |
st.markdown("""
|
| 15 |
<style>
|
| 16 |
-
.main {
|
| 17 |
-
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
color: #2c3e50;
|
| 21 |
-
}
|
| 22 |
-
.stButton>button {
|
| 23 |
-
background-color: #007bff;
|
| 24 |
-
color: white;
|
| 25 |
-
}
|
| 26 |
-
.metric-card {
|
| 27 |
-
background-color: white;
|
| 28 |
-
padding: 20px;
|
| 29 |
-
border-radius: 10px;
|
| 30 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 31 |
-
}
|
| 32 |
</style>
|
| 33 |
""", unsafe_allow_html=True)
|
| 34 |
|
|
@@ -36,7 +33,7 @@ st.markdown("""
|
|
| 36 |
st.title("🧬 Virtual Gene Scope: The Regulatory Genome")
|
| 37 |
st.markdown("""
|
| 38 |
**Nursing Context**: 98% of the genome does not code for proteins. Instead, it acts as a "control panel" turning genes on and off.
|
| 39 |
-
**AlphaGenome** allows us to see this hidden layer.
|
| 40 |
""")
|
| 41 |
|
| 42 |
# Sidebar controls
|
|
@@ -44,53 +41,103 @@ st.sidebar.header("Patient & Gene Settings")
|
|
| 44 |
gene_choice = st.sidebar.selectbox("Select Gene of Interest", ["INS (Insulin - Diabetes)", "SCN9A (Pain Sensitivity)", "MMP9 (Wound Healing)"])
|
| 45 |
mutation_intensity = st.sidebar.slider("Mutation Impact (Simulation)", 0.0, 1.0, 0.5, help="Simulate the severity of the regulatory disruption.")
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# "Mutated" signal - reduced by mutation factor
|
| 56 |
-
mutated_signal = base_signal * (1 - mutation_factor * 0.8) # up to 80% reduction
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
with
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
st.error("⚠️ HIGH RISK: Significant reduction in gene expression predicted.")
|
| 72 |
-
elif mutation_intensity > 0.3:
|
| 73 |
-
st.warning("⚠️ MODERATE RISK: Partial loss of regulation.")
|
| 74 |
-
else:
|
| 75 |
-
st.success("✅ LOW RISK: Benign variant predicted.")
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
ax.set_ylabel("Predicted RNA Expression")
|
| 89 |
-
ax.legend()
|
| 90 |
-
ax.grid(True, alpha=0.3)
|
| 91 |
-
ax.fill_between(x, normal, mutant, color='orange', alpha=0.2, label='Lost Expression')
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
st.markdown("---")
|
| 96 |
-
st.caption("Powered by AlphaGenome Architecture
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
+
# Try importing AlphaGenome, handle failure gracefully
|
| 7 |
+
try:
|
| 8 |
+
from alphagenome.models import dna_client
|
| 9 |
+
from alphagenome.data import genome
|
| 10 |
+
from alphagenome.visualization import plot_components
|
| 11 |
+
HAS_ALPHAGENOME = True
|
| 12 |
+
except ImportError:
|
| 13 |
+
HAS_ALPHAGENOME = False
|
| 14 |
+
|
| 15 |
# Page Config
|
| 16 |
st.set_page_config(
|
| 17 |
page_title="Virtual Gene Scope",
|
|
|
|
| 19 |
layout="wide"
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Custom CSS
|
| 23 |
st.markdown("""
|
| 24 |
<style>
|
| 25 |
+
.main { background-color: #f8f9fa; }
|
| 26 |
+
h1 { color: #2c3e50; }
|
| 27 |
+
.stButton>button { background-color: #007bff; color: white; }
|
| 28 |
+
.metric-card { background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
</style>
|
| 30 |
""", unsafe_allow_html=True)
|
| 31 |
|
|
|
|
| 33 |
st.title("🧬 Virtual Gene Scope: The Regulatory Genome")
|
| 34 |
st.markdown("""
|
| 35 |
**Nursing Context**: 98% of the genome does not code for proteins. Instead, it acts as a "control panel" turning genes on and off.
|
| 36 |
+
**AlphaGenome** allows us to see this hidden layer.
|
| 37 |
""")
|
| 38 |
|
| 39 |
# Sidebar controls
|
|
|
|
| 41 |
gene_choice = st.sidebar.selectbox("Select Gene of Interest", ["INS (Insulin - Diabetes)", "SCN9A (Pain Sensitivity)", "MMP9 (Wound Healing)"])
|
| 42 |
mutation_intensity = st.sidebar.slider("Mutation Impact (Simulation)", 0.0, 1.0, 0.5, help="Simulate the severity of the regulatory disruption.")
|
| 43 |
|
| 44 |
+
# Setup API
|
| 45 |
+
api_key = st.secrets.get("ALPHAGENOME_API_KEY")
|
| 46 |
+
|
| 47 |
+
if not HAS_ALPHAGENOME:
|
| 48 |
+
st.error("AlphaGenome library not installed. Please check requirements.txt")
|
| 49 |
+
elif not api_key:
|
| 50 |
+
st.warning("⚠️ No API Key found! Please add `ALPHAGENOME_API_KEY` to your secrets to see real data. Showing MOCK data for now.")
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# MOCK FALLBACK
|
| 53 |
+
def get_mock_tracks(gene, mutation_factor):
|
| 54 |
+
x = np.linspace(0, 100, 500)
|
| 55 |
+
base_signal = np.exp(-((x - 50)**2) / 20)
|
| 56 |
+
mutated_signal = base_signal * (1 - mutation_factor * 0.8)
|
| 57 |
+
return x, base_signal, mutated_signal
|
| 58 |
|
| 59 |
+
col1, col2 = st.columns([1, 2])
|
| 60 |
+
with col1:
|
| 61 |
+
st.subheader("Variant details (MOCK)")
|
| 62 |
+
st.info(f"Analyzing Regulatory Region for **{gene_choice.split(' ')[0]}**")
|
| 63 |
+
st.write("Variant Type: **Non-Coding / Regulatory**")
|
| 64 |
+
if mutation_intensity > 0.7:
|
| 65 |
+
st.error("⚠️ HIGH RISK: Significant reduction in gene expression predicted.")
|
| 66 |
+
else:
|
| 67 |
+
st.success("✅ LOW RISK: Benign variant predicted.")
|
| 68 |
|
| 69 |
+
with col2:
|
| 70 |
+
st.subheader("AlphaGenome Predicted Activity (MOCK)")
|
| 71 |
+
x, normal, mutant = get_mock_tracks(gene_choice, mutation_intensity)
|
| 72 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
| 73 |
+
ax.plot(x, normal, label="Healthy Control", color='green')
|
| 74 |
+
ax.plot(x, mutant, label="Patient Variant", color='red', linestyle='--')
|
| 75 |
+
ax.legend()
|
| 76 |
+
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
else:
|
| 79 |
+
# REAL API LOGIC
|
| 80 |
+
st.sidebar.success("✅ AlphaGenome API Connected")
|
| 81 |
|
| 82 |
+
# Initialize Client
|
| 83 |
+
model = dna_client.create(api_key)
|
| 84 |
|
| 85 |
+
# Define dummy coordinates for the demo genes (simplified)
|
| 86 |
+
# in a real app, you'd look these up properly
|
| 87 |
+
gene_coords = {
|
| 88 |
+
"INS": {"chr": "chr11", "start": 2159779, "end": 2161221, "pos": 2160000},
|
| 89 |
+
"SCN9A": {"chr": "chr2", "start": 166195190, "end": 166205190, "pos": 166200000}, # rounded
|
| 90 |
+
"MMP9": {"chr": "chr20", "start": 46010000, "end": 46020000, "pos": 46015000}
|
| 91 |
+
}
|
| 92 |
|
| 93 |
+
gene_sym = gene_choice.split(' ')[0]
|
| 94 |
+
coords = gene_coords.get(gene_sym, gene_coords["INS"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
try:
|
| 97 |
+
with st.spinner("Querying Google DeepMind TPUs..."):
|
| 98 |
+
interval = genome.Interval(chromosome=coords["chr"], start=coords["start"], end=coords["end"])
|
| 99 |
+
# Create a dummy variant at the center
|
| 100 |
+
variant = genome.Variant(
|
| 101 |
+
chromosome=coords["chr"],
|
| 102 |
+
position=coords["pos"],
|
| 103 |
+
reference_bases='A',
|
| 104 |
+
alternate_bases='C' # simple mutation
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# Predict
|
| 108 |
+
outputs = model.predict_variant(
|
| 109 |
+
interval=interval,
|
| 110 |
+
variant=variant,
|
| 111 |
+
requested_outputs=[dna_client.OutputType.RNA_SEQ],
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
col1, col2 = st.columns([1, 2])
|
| 115 |
+
with col1:
|
| 116 |
+
st.subheader("Real Analysis")
|
| 117 |
+
st.markdown(f"**Gene**: {gene_sym}")
|
| 118 |
+
st.markdown(f"**Locus**: {coords['chr']}:{coords['start']}-{coords['end']}")
|
| 119 |
+
st.success("Prediction successful!")
|
| 120 |
+
|
| 121 |
+
with col2:
|
| 122 |
+
st.subheader("AlphaGenome Tracks")
|
| 123 |
+
# Use plot_components from the library
|
| 124 |
+
fig = plot_components.plot(
|
| 125 |
+
[
|
| 126 |
+
plot_components.OverlaidTracks(
|
| 127 |
+
tdata={
|
| 128 |
+
'REF': outputs.reference.rna_seq,
|
| 129 |
+
'ALT': outputs.alternate.rna_seq,
|
| 130 |
+
},
|
| 131 |
+
colors={'REF': 'dimgrey', 'ALT': 'red'},
|
| 132 |
+
),
|
| 133 |
+
],
|
| 134 |
+
interval=outputs.reference.rna_seq.interval.resize(2**15),
|
| 135 |
+
annotations=[plot_components.VariantAnnotation([variant], alpha=0.8)],
|
| 136 |
+
)
|
| 137 |
+
st.pyplot(fig)
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
st.error(f"API Error: {str(e)}")
|
| 141 |
|
| 142 |
st.markdown("---")
|
| 143 |
+
st.caption("Powered by AlphaGenome Architecture | Nursing Genomics Education Toolkit")
|
requirements.txt
CHANGED
|
@@ -3,4 +3,4 @@ pandas
|
|
| 3 |
matplotlib
|
| 4 |
numpy
|
| 5 |
huggingface_hub
|
| 6 |
-
|
|
|
|
| 3 |
matplotlib
|
| 4 |
numpy
|
| 5 |
huggingface_hub
|
| 6 |
+
git+https://github.com/google-deepmind/alphagenome.git
|