CoolFace
Apppublic

vivekharry/dna_sequence_visualizer

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
app.py102 linesDownload Raw Back to root
1import gradio as gr2import plotly.graph_objects as go3import plotly.express as px4from collections import Counter5import numpy as np6 7CODON_TABLE = {8    'TTT': 'F', 'TTC': 'F', 'TTA': 'L', 'TTG': 'L',9    'CTT': 'L', 'CTC': 'L', 'CTA': 'L', 'CTG': 'L',10    'ATT': 'I', 'ATC': 'I', 'ATA': 'I', 'ATG': 'M',11    'GTT': 'V', 'GTC': 'V', 'GTA': 'V', 'GTG': 'V',12    'TCT': 'S', 'TCC': 'S', 'TCA': 'S', 'TCG': 'S',13    'CCT': 'P', 'CCC': 'P', 'CCA': 'P', 'CCG': 'P',14    'ACT': 'T', 'ACC': 'T', 'ACA': 'T', 'ACG': 'T',15    'GCT': 'A', 'GCC': 'A', 'GCA': 'A', 'GCG': 'A',16    'TAT': 'Y', 'TAC': 'Y', 'TAA': '*', 'TAG': '*',17    'CAT': 'H', 'CAC': 'H', 'CAA': 'Q', 'CAG': 'Q',18    'AAT': 'N', 'AAC': 'N', 'AAA': 'K', 'AAG': 'K',19    'GAT': 'D', 'GAC': 'D', 'GAA': 'E', 'GAG': 'E',20    'TGT': 'C', 'TGC': 'C', 'TGA': '*', 'TGG': 'W',21    'CGT': 'R', 'CGC': 'R', 'CGA': 'R', 'CGG': 'R',22    'AGT': 'S', 'AGC': 'S', 'AGA': 'R', 'AGG': 'R',23    'GGT': 'G', 'GGC': 'G', 'GGA': 'G', 'GGG': 'G',24}25 26COLOR_MAP = {'A': '#FF6B6B', 'T': '#4ECDC4', 'G': '#45B7D1', 'C': '#FFA07A'}27 28def analyze_dna(sequence):29    seq = sequence.upper().replace(" ", "").replace("\n", "")30    seq = ''.join(c for c in seq if c in 'ATGC')31    32    if len(seq) < 3:33        return "Enter at least 3 nucleotides", None, None, ""34    35    # GC Content sliding window36    window = min(50, len(seq) // 2) or 137    gc_values = []38    for i in range(len(seq) - window + 1):39        w = seq[i:i+window]40        gc = (w.count('G') + w.count('C')) / len(w) * 10041        gc_values.append(gc)42    43    fig_gc = go.Figure()44    fig_gc.add_trace(go.Scatter(45        y=gc_values, mode='lines',46        fill='tozeroy', fillcolor='rgba(78,205,196,0.2)',47        line=dict(color='#4ECDC4', width=2)48    ))49    fig_gc.update_layout(50        title="GC Content (Sliding Window)",51        xaxis_title="Position", yaxis_title="GC %",52        template="plotly_dark", height=350,53        paper_bgcolor='#0f1117', plot_bgcolor='#0f1117'54    )55    56    # Nucleotide composition57    counts = Counter(seq)58    fig_comp = px.pie(59        names=list(counts.keys()),60        values=list(counts.values()),61        color=list(counts.keys()),62        color_discrete_map=COLOR_MAP,63        title="Nucleotide Composition"64    )65    fig_comp.update_layout(66        template="plotly_dark", height=350,67        paper_bgcolor='#0f1117', plot_bgcolor='#0f1117'68    )69    70    # Translate71    protein = []72    for i in range(0, len(seq) - 2, 3):73        codon = seq[i:i+3]74        aa = CODON_TABLE.get(codon, '?')75        protein.append(aa)76    protein_str = ''.join(protein)77    78    stats = f"""### ๐Ÿ“Š Sequence Statistics79- **Length:** {len(seq)} bp80- **GC Content:** {(seq.count('G')+seq.count('C'))/len(seq)*100:.1f}%81- **A:** {counts.get('A',0)} | **T:** {counts.get('T',0)} | **G:** {counts.get('G',0)} | **C:** {counts.get('C',0)}82- **Protein:** `{protein_str[:60]}{'...' if len(protein_str)>60 else ''}`"""83    84    return stats, fig_gc, fig_comp, protein_str85 86demo = gr.Interface(87    fn=analyze_dna,88    inputs=gr.Textbox(label="DNA Sequence", lines=4,89        placeholder="Paste DNA sequence (e.g., ATGCGATCGATCG...)"),90    outputs=[91        gr.Markdown(label="Statistics"),92        gr.Plot(label="GC Content"),93        gr.Plot(label="Composition"),94        gr.Textbox(label="Protein Translation"),95    ],96    title="๐Ÿงฌ DNA Sequence Visualizer",97    description="Analyze and visualize DNA sequences with GC content, composition, and translation.",98    theme=gr.themes.Soft(),99)100 101if __name__ == "__main__":102    demo.launch()