Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
|
|
3 |
import random
|
4 |
from scipy.stats import entropy as scipy_entropy
|
5 |
import time
|
6 |
|
|
|
|
|
7 |
# --- НАСТРОЙКИ ---
|
8 |
seqlen = 60
|
9 |
steps = 120
|
@@ -11,8 +14,7 @@ min_run, max_run = 1, 2
|
|
11 |
ANGLE_MAP = {'A': 60.0, 'C': 180.0, 'G': -60.0, 'T': -180.0, 'N': 0.0}
|
12 |
bases = ['A', 'C', 'G', 'T']
|
13 |
|
14 |
-
# ---
|
15 |
-
|
16 |
def find_local_min_runs(profile, min_run=1, max_run=2):
|
17 |
result = []
|
18 |
N = len(profile)
|
@@ -55,14 +57,11 @@ def bio_mutate(seq):
|
|
55 |
if len(seq) > 10:
|
56 |
start = random.randint(0, len(seq)-6)
|
57 |
end = start + random.randint(3,6)
|
58 |
-
subseq = seq[start:end]
|
59 |
-
subseq = subseq[::-1]
|
60 |
seq = seq[:start] + subseq + seq[end:]
|
61 |
while len(seq) < seqlen:
|
62 |
seq += random.choice(bases)
|
63 |
-
|
64 |
-
seq = seq[:seqlen]
|
65 |
-
return seq
|
66 |
|
67 |
def compute_autocorr(profile):
|
68 |
profile = profile - np.mean(profile)
|
@@ -76,17 +75,18 @@ def compute_entropy(profile):
|
|
76 |
p = counts / counts.sum()
|
77 |
return scipy_entropy(p, base=2)
|
78 |
|
79 |
-
# ---
|
80 |
-
st.title("🧬
|
81 |
-
st.markdown("
|
82 |
|
83 |
-
|
|
|
|
|
84 |
seq = ''.join(random.choices(bases, k=seqlen))
|
85 |
stat_bist_counts = []
|
86 |
stat_entropy = []
|
87 |
stat_autocorr = []
|
88 |
|
89 |
-
# Симуляция изменения последовательности
|
90 |
for step in range(steps):
|
91 |
if step != 0:
|
92 |
seq = bio_mutate(seq)
|
@@ -97,17 +97,25 @@ if st.button("▶️ Запустить симуляцию"):
|
|
97 |
stat_entropy.append(ent)
|
98 |
acorr = compute_autocorr(torsion_profile)
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
|
105 |
-
|
|
|
106 |
|
107 |
-
|
108 |
-
|
|
|
109 |
|
110 |
-
|
111 |
-
|
112 |
|
113 |
-
time.sleep(0.
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
import random
|
5 |
from scipy.stats import entropy as scipy_entropy
|
6 |
import time
|
7 |
|
8 |
+
st.set_page_config(layout="wide")
|
9 |
+
|
10 |
# --- НАСТРОЙКИ ---
|
11 |
seqlen = 60
|
12 |
steps = 120
|
|
|
14 |
ANGLE_MAP = {'A': 60.0, 'C': 180.0, 'G': -60.0, 'T': -180.0, 'N': 0.0}
|
15 |
bases = ['A', 'C', 'G', 'T']
|
16 |
|
17 |
+
# --- БИО-ФУНКЦИИ ---
|
|
|
18 |
def find_local_min_runs(profile, min_run=1, max_run=2):
|
19 |
result = []
|
20 |
N = len(profile)
|
|
|
57 |
if len(seq) > 10:
|
58 |
start = random.randint(0, len(seq)-6)
|
59 |
end = start + random.randint(3,6)
|
60 |
+
subseq = seq[start:end][::-1]
|
|
|
61 |
seq = seq[:start] + subseq + seq[end:]
|
62 |
while len(seq) < seqlen:
|
63 |
seq += random.choice(bases)
|
64 |
+
return seq[:seqlen]
|
|
|
|
|
65 |
|
66 |
def compute_autocorr(profile):
|
67 |
profile = profile - np.mean(profile)
|
|
|
75 |
p = counts / counts.sum()
|
76 |
return scipy_entropy(p, base=2)
|
77 |
|
78 |
+
# --- ИНТЕРФЕЙС ---
|
79 |
+
st.title("🧬 Эфир: Живой поток мутаций ДНК")
|
80 |
+
st.markdown("Анализ биологических свойств последовательности в реальном времени.")
|
81 |
|
82 |
+
plot_placeholder = st.empty()
|
83 |
+
|
84 |
+
if st.button("▶️ Начать эфир"):
|
85 |
seq = ''.join(random.choices(bases, k=seqlen))
|
86 |
stat_bist_counts = []
|
87 |
stat_entropy = []
|
88 |
stat_autocorr = []
|
89 |
|
|
|
90 |
for step in range(steps):
|
91 |
if step != 0:
|
92 |
seq = bio_mutate(seq)
|
|
|
97 |
stat_entropy.append(ent)
|
98 |
acorr = compute_autocorr(torsion_profile)
|
99 |
|
100 |
+
fig, axs = plt.subplots(3, 1, figsize=(10, 8))
|
101 |
+
plt.subplots_adjust(hspace=0.45)
|
102 |
+
|
103 |
+
axs[0].plot(torsion_profile, color='royalblue')
|
104 |
+
for start, end, val in runs:
|
105 |
+
axs[0].axvspan(start, end, color="red", alpha=0.3)
|
106 |
+
axs[0].set_ylim(-200, 200)
|
107 |
+
axs[0].set_title(f"Шаг {step}: {seq}")
|
108 |
+
axs[0].set_ylabel("Торсионный угол")
|
109 |
|
110 |
+
axs[1].plot(stat_bist_counts, '-o', color='crimson', markersize=4)
|
111 |
+
axs[1].set_ylabel("Биомашины")
|
112 |
+
axs[1].set_title("Количество машин")
|
113 |
|
114 |
+
axs[2].bar(np.arange(6), acorr[:6], color='teal')
|
115 |
+
axs[2].set_title(f"Автокорреляция / Энтропия: {ent:.2f}")
|
116 |
+
axs[2].set_xlabel("Лаг")
|
117 |
|
118 |
+
plot_placeholder.pyplot(fig)
|
119 |
+
plt.close(fig)
|
120 |
|
121 |
+
time.sleep(0.3)
|