Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,64 @@ import gradio as gr
|
|
| 2 |
from modules.rna_predictor import RNAPredictor
|
| 3 |
from modules.visualizer import RNAVisualizer
|
| 4 |
import plotly.graph_objects as go
|
|
|
|
| 5 |
|
| 6 |
# Initialize the components
|
| 7 |
print("🚀 Initializing RNAFlow...")
|
| 8 |
rna_predictor = RNAPredictor()
|
| 9 |
rna_visualizer = RNAVisualizer()
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
"""
|
| 13 |
if not sequence:
|
| 14 |
-
return "Please enter an RNA sequence"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
sequence = sequence.upper().strip()
|
| 17 |
-
valid_bases = set('ACGU')
|
| 18 |
|
| 19 |
-
if not
|
| 20 |
-
return "❌
|
| 21 |
|
| 22 |
if len(sequence) < 10:
|
| 23 |
-
return "❌ Sequence too short. Please enter at least 10 nucleotides."
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
structure_prediction = rna_predictor.predict_structure(sequence)
|
| 28 |
-
embeddings = rna_predictor.predict_embeddings(sequence)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def format_analysis_results(sequence, properties, structure, embeddings):
|
| 42 |
"""Format the analysis results in a readable way"""
|
|
@@ -86,15 +109,17 @@ with gr.Blocks(title="RNAFlow - Advanced RNA Analysis") as demo:
|
|
| 86 |
# 🧬 RNAFlow - Advanced RNA Analysis
|
| 87 |
|
| 88 |
**Intelligent RNA sequence analysis with AI-powered insights and advanced visualizations**
|
|
|
|
|
|
|
| 89 |
""")
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
with gr.Column():
|
| 93 |
sequence_input = gr.Textbox(
|
| 94 |
label="RNA Sequence",
|
| 95 |
-
placeholder="
|
| 96 |
-
lines=
|
| 97 |
-
max_lines=
|
| 98 |
)
|
| 99 |
analyze_btn = gr.Button("🧬 Analyze Sequence", variant="primary", size="lg")
|
| 100 |
|
|
@@ -102,29 +127,42 @@ with gr.Blocks(title="RNAFlow - Advanced RNA Analysis") as demo:
|
|
| 102 |
with gr.Column(scale=1):
|
| 103 |
output_text = gr.Markdown(label="Analysis Results")
|
| 104 |
with gr.Column(scale=2):
|
| 105 |
-
with gr.
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
gr.Examples(
|
| 118 |
examples=[
|
|
|
|
| 119 |
["AUGCCGUAUCGACGUACGUACGUACGUACGU"],
|
| 120 |
["GCCAUAUGGCUACGUAGCUAGCUAGCUAGCUA"],
|
| 121 |
["UUUAAACCCGGGACGUACGUACGUA"],
|
| 122 |
-
|
| 123 |
-
["
|
|
|
|
| 124 |
],
|
| 125 |
-
inputs=[sequence_input]
|
|
|
|
| 126 |
)
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
analyze_btn.click(
|
| 129 |
fn=analyze_rna_sequence,
|
| 130 |
inputs=[sequence_input],
|
|
|
|
| 2 |
from modules.rna_predictor import RNAPredictor
|
| 3 |
from modules.visualizer import RNAVisualizer
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
# Initialize the components
|
| 8 |
print("🚀 Initializing RNAFlow...")
|
| 9 |
rna_predictor = RNAPredictor()
|
| 10 |
rna_visualizer = RNAVisualizer()
|
| 11 |
|
| 12 |
+
def clean_sequence(sequence):
|
| 13 |
+
"""Clean and validate RNA sequence input"""
|
| 14 |
if not sequence:
|
| 15 |
+
return None, "Please enter an RNA sequence"
|
| 16 |
|
| 17 |
+
# Remove whitespace, numbers, and other non-RNA characters
|
| 18 |
+
sequence = re.sub(r'[^ACGUacgu\s]', '', sequence) # Remove invalid characters
|
| 19 |
+
sequence = re.sub(r'\s+', '', sequence) # Remove all whitespace
|
| 20 |
sequence = sequence.upper().strip()
|
|
|
|
| 21 |
|
| 22 |
+
if not sequence:
|
| 23 |
+
return None, "❌ No valid RNA characters found. Please use only A, C, G, U."
|
| 24 |
|
| 25 |
if len(sequence) < 10:
|
| 26 |
+
return None, "❌ Sequence too short. Please enter at least 10 nucleotides."
|
| 27 |
|
| 28 |
+
if len(sequence) > 500:
|
| 29 |
+
return None, "❌ Sequence too long. Please enter less than 500 nucleotides."
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
return sequence, None
|
| 32 |
+
|
| 33 |
+
def analyze_rna_sequence(sequence):
|
| 34 |
+
"""Comprehensive RNA sequence analysis"""
|
| 35 |
+
# Clean and validate input
|
| 36 |
+
cleaned_sequence, error = clean_sequence(sequence)
|
| 37 |
+
if error:
|
| 38 |
+
error_plot = rna_visualizer.create_error_plot(error)
|
| 39 |
+
return error, error_plot, error_plot, error_plot, error_plot, ""
|
| 40 |
|
| 41 |
+
# Perform comprehensive analysis
|
| 42 |
+
try:
|
| 43 |
+
properties = rna_predictor.calculate_sequence_properties(cleaned_sequence)
|
| 44 |
+
structure_prediction = rna_predictor.predict_structure(cleaned_sequence)
|
| 45 |
+
embeddings = rna_predictor.predict_embeddings(cleaned_sequence)
|
| 46 |
+
|
| 47 |
+
# Generate multiple visualizations
|
| 48 |
+
structure_viz = rna_visualizer.create_advanced_structure_plot(cleaned_sequence, structure_prediction["predicted_structure"])
|
| 49 |
+
embedding_viz = rna_visualizer.create_embedding_visualization(embeddings, cleaned_sequence)
|
| 50 |
+
sequence_logo = rna_visualizer.create_sequence_logo(cleaned_sequence)
|
| 51 |
+
gc_plot = rna_visualizer.create_gc_content_plot(cleaned_sequence)
|
| 52 |
+
|
| 53 |
+
# Format results
|
| 54 |
+
analysis_results = format_analysis_results(cleaned_sequence, properties, structure_prediction, embeddings)
|
| 55 |
+
|
| 56 |
+
return analysis_results, structure_viz, embedding_viz, sequence_logo, gc_plot, str(embeddings)
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
error_msg = f"❌ Analysis error: {str(e)}"
|
| 60 |
+
print(f"Error in analysis: {e}")
|
| 61 |
+
error_plot = rna_visualizer.create_error_plot(error_msg)
|
| 62 |
+
return error_msg, error_plot, error_plot, error_plot, error_plot, ""
|
| 63 |
|
| 64 |
def format_analysis_results(sequence, properties, structure, embeddings):
|
| 65 |
"""Format the analysis results in a readable way"""
|
|
|
|
| 109 |
# 🧬 RNAFlow - Advanced RNA Analysis
|
| 110 |
|
| 111 |
**Intelligent RNA sequence analysis with AI-powered insights and advanced visualizations**
|
| 112 |
+
|
| 113 |
+
*Simply paste any RNA sequence (A, C, G, U characters) and click analyze!*
|
| 114 |
""")
|
| 115 |
|
| 116 |
with gr.Row():
|
| 117 |
with gr.Column():
|
| 118 |
sequence_input = gr.Textbox(
|
| 119 |
label="RNA Sequence",
|
| 120 |
+
placeholder="Paste RNA sequence here (e.g., AUGCCGUAUCG... or AUGC CUGA UCGA C...)",
|
| 121 |
+
lines=3,
|
| 122 |
+
max_lines=5
|
| 123 |
)
|
| 124 |
analyze_btn = gr.Button("🧬 Analyze Sequence", variant="primary", size="lg")
|
| 125 |
|
|
|
|
| 127 |
with gr.Column(scale=1):
|
| 128 |
output_text = gr.Markdown(label="Analysis Results")
|
| 129 |
with gr.Column(scale=2):
|
| 130 |
+
with gr.Tabs():
|
| 131 |
+
with gr.TabItem("Structure"):
|
| 132 |
+
structure_plot = gr.Plot(label="Advanced Structure Analysis")
|
| 133 |
+
with gr.TabItem("Embeddings"):
|
| 134 |
+
embedding_plot = gr.Plot(label="Sequence Embeddings")
|
| 135 |
+
with gr.TabItem("Sequence"):
|
| 136 |
+
sequence_plot = gr.Plot(label="Sequence Logo")
|
| 137 |
+
with gr.TabItem("GC Content"):
|
| 138 |
+
gc_plot = gr.Plot(label="GC Content Analysis")
|
| 139 |
+
with gr.TabItem("Raw Data"):
|
| 140 |
+
raw_output = gr.Textbox(label="Embedding Data", lines=6)
|
| 141 |
+
|
| 142 |
+
# Examples with different formats to show flexibility
|
| 143 |
gr.Examples(
|
| 144 |
examples=[
|
| 145 |
+
# Clean sequences
|
| 146 |
["AUGCCGUAUCGACGUACGUACGUACGUACGU"],
|
| 147 |
["GCCAUAUGGCUACGUAGCUAGCUAGCUAGCUA"],
|
| 148 |
["UUUAAACCCGGGACGUACGUACGUA"],
|
| 149 |
+
# Sequences with spaces (common when pasting)
|
| 150 |
+
["AUG CCG UAU CGA CGU ACG UAC GUA CGU ACG U"],
|
| 151 |
+
["GC CAU AUG GCU ACG UAG CUA GCU AGC UAG CUA"],
|
| 152 |
],
|
| 153 |
+
inputs=[sequence_input],
|
| 154 |
+
label="Try these examples (click any):"
|
| 155 |
)
|
| 156 |
|
| 157 |
+
# Add some usage instructions
|
| 158 |
+
gr.Markdown("""
|
| 159 |
+
### 💡 Usage Tips:
|
| 160 |
+
- You can paste sequences **with or without spaces** - we'll clean them automatically
|
| 161 |
+
- Sequences should contain only **A, C, G, U** characters (case insensitive)
|
| 162 |
+
- Minimum length: **10 nucleotides**, Maximum: **500 nucleotides**
|
| 163 |
+
- Numbers, spaces, and line breaks will be automatically removed
|
| 164 |
+
""")
|
| 165 |
+
|
| 166 |
analyze_btn.click(
|
| 167 |
fn=analyze_rna_sequence,
|
| 168 |
inputs=[sequence_input],
|