Hoaxx / app.py
Dmtlant's picture
Update app.py
ab731e4 verified
raw
history blame
3.65 kB
import numpy as np
import matplotlib.pyplot as plt
import random
# Преобразование последовательности в фрактальные точки на основе торсионных углов
def sequence_to_torsion(seq):
ANGLE_MAP = {'A': 60.0, 'C': 180.0, 'G': -60.0, 'T': -180.0, 'N': 0.0}
return np.array([ANGLE_MAP.get(nt, 0.0) for nt in seq])
# Вычисление корреляционной размерности
def correlation_dimension(data, max_radius=20, min_points=5):
N = len(data)
dimensions = []
for radius in range(1, max_radius):
count = 0
for i in range(N):
for j in range(i + 1, N):
if np.abs(data[i] - data[j]) < radius:
count += 1
if count > min_points:
dimension = np.log(count) / np.log(radius)
dimensions.append(dimension)
return np.mean(dimensions) if dimensions else 0
# --- Основной код ---
seqlen = 60
bases = ['A', 'C', 'G', 'T']
def bio_mutate(seq):
r = random.random()
if r < 0.70:
idx = random.randint(0, len(seq)-1)
orig = seq[idx]
prob = random.random()
if orig in 'AG':
newbase = 'C' if prob < 0.65 else random.choice(['T', 'C'])
elif orig in 'CT':
newbase = 'G' if prob < 0.65 else random.choice(['A', 'G'])
else:
newbase = random.choice([b for b in bases if b != orig])
seq = seq[:idx] + newbase + seq[idx+1:]
elif r < 0.80:
idx = random.randint(0, len(seq)-1)
ins = ''.join(random.choices(bases, k=random.randint(1, 3)))
seq = seq[:idx] + ins + seq[idx:]
if len(seq) > seqlen:
seq = seq[:seqlen]
elif r < 0.90:
if len(seq) > 4:
idx = random.randint(0, len(seq)-2)
dell = random.randint(1, min(3, len(seq)-idx))
seq = seq[:idx] + seq[idx+dell:]
else:
if len(seq) > 10:
start = random.randint(0, len(seq)-6)
end = start + random.randint(3,6)
subseq = seq[start:end][::-1]
seq = seq[:start] + subseq + seq[end:]
while len(seq) < seqlen:
seq += random.choice(bases)
return seq[:seqlen]
# --- UI ---
import streamlit as st
import time
st.title("🔴 Живой эфир мутаций ДНК с фрактальной размерностью")
start = st.button("▶️ Старт эфира")
stop = st.checkbox("⏹️ Остановить")
plot_placeholder = st.empty()
if start:
seq = ''.join(random.choices(bases, k=seqlen))
step = 0
stat_fractal_dimension = []
while True:
if stop:
st.warning("⏹️ Эфир остановлен пользователем.")
break
if step != 0:
seq = bio_mutate(seq)
torsion_profile = sequence_to_torsion(seq)
fractal_dim = correlation_dimension(torsion_profile)
stat_fractal_dimension.append(fractal_dim)
# Визуализация
fig, axs = plt.subplots(2, 1, figsize=(10, 8))
plt.subplots_adjust(hspace=0.45)
axs[0].plot(torsion_profile, color='royalblue')
axs[0].set_title(f"Шаг {step}: {seq}")
axs[0].set_ylabel("Торсионный угол")
axs[1].plot(stat_fractal_dimension, '-o', color='green', markersize=4)
axs[1].set_title(f"Фрактальная размерность: {fractal_dim:.3f}")
axs[1].set_xlabel("Шаг")
plot_placeholder.pyplot(fig)
plt.close(fig)
step += 1
time.sleep(0.3)