Dmtlant commited on
Commit
455c7bd
·
verified ·
1 Parent(s): 5890d41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -1,15 +1,11 @@
1
-
2
  import streamlit as st
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
  import random
6
  from scipy.stats import entropy as scipy_entropy
7
  import time
8
- import imageio
9
  from datetime import datetime
10
 
11
- st.set_page_config(layout="wide")
12
-
13
  # --- ПАРАМЕТРЫ ---
14
  seqlen = 60
15
  min_run, max_run = 1, 2
@@ -17,6 +13,7 @@ ANGLE_MAP = {'A': 60.0, 'C': 180.0, 'G': -60.0, 'T': -180.0, 'N': 0.0}
17
  bases = ['A', 'C', 'G', 'T']
18
 
19
  # --- ФУНКЦИИ ---
 
20
  def find_local_min_runs(profile, min_run=1, max_run=2):
21
  result = []
22
  N = len(profile)
@@ -77,6 +74,15 @@ def compute_entropy(profile):
77
  p = counts / counts.sum()
78
  return scipy_entropy(p, base=2)
79
 
 
 
 
 
 
 
 
 
 
80
  # --- UI ---
81
  st.title("🔴 Живой эфир мутаций ДНК")
82
  start = st.button("▶️ Старт эфира")
@@ -86,6 +92,7 @@ plot_placeholder = st.empty()
86
 
87
  if start:
88
  seq = ''.join(random.choices(bases, k=seqlen))
 
89
  stat_bist_counts = []
90
  stat_entropy = []
91
 
@@ -97,6 +104,7 @@ if start:
97
 
98
  if step != 0:
99
  seq = bio_mutate(seq)
 
100
 
101
  torsion_profile = np.array([ANGLE_MAP.get(nt, 0.0) for nt in seq])
102
  runs = find_local_min_runs(torsion_profile, min_run, max_run)
@@ -105,7 +113,10 @@ if start:
105
  stat_entropy.append(ent)
106
  acorr = compute_autocorr(torsion_profile)
107
 
108
- fig, axs = plt.subplots(3, 1, figsize=(10, 8))
 
 
 
109
  plt.subplots_adjust(hspace=0.45)
110
 
111
  axs[0].plot(torsion_profile, color='royalblue')
@@ -123,6 +134,10 @@ if start:
123
  axs[2].set_title(f"Автокорреляция / Энтропия: {ent:.2f}")
124
  axs[2].set_xlabel("Лаг")
125
 
 
 
 
 
126
  plot_placeholder.pyplot(fig)
127
  plt.close(fig)
128
 
 
 
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
  from datetime import datetime
8
 
 
 
9
  # --- ПАРАМЕТРЫ ---
10
  seqlen = 60
11
  min_run, max_run = 1, 2
 
13
  bases = ['A', 'C', 'G', 'T']
14
 
15
  # --- ФУНКЦИИ ---
16
+
17
  def find_local_min_runs(profile, min_run=1, max_run=2):
18
  result = []
19
  N = len(profile)
 
74
  p = counts / counts.sum()
75
  return scipy_entropy(p, base=2)
76
 
77
+ def compute_global_entropy(seq_history):
78
+ """
79
+ Вычисление глобальной энтропии на основе исторических последовательностей.
80
+ """
81
+ all_torsions = np.concatenate([np.array([ANGLE_MAP.get(nt, 0.0) for nt in seq]) for seq in seq_history])
82
+ vals, counts = np.unique(all_torsions, return_counts=True)
83
+ p = counts / counts.sum()
84
+ return scipy_entropy(p, base=2)
85
+
86
  # --- UI ---
87
  st.title("🔴 Живой эфир мутаций ДНК")
88
  start = st.button("▶️ Старт эфира")
 
92
 
93
  if start:
94
  seq = ''.join(random.choices(bases, k=seqlen))
95
+ seq_history = [seq] # История последовательностей для расчета глобальной энтропии
96
  stat_bist_counts = []
97
  stat_entropy = []
98
 
 
104
 
105
  if step != 0:
106
  seq = bio_mutate(seq)
107
+ seq_history.append(seq) # Добавляем в историю
108
 
109
  torsion_profile = np.array([ANGLE_MAP.get(nt, 0.0) for nt in seq])
110
  runs = find_local_min_runs(torsion_profile, min_run, max_run)
 
113
  stat_entropy.append(ent)
114
  acorr = compute_autocorr(torsion_profile)
115
 
116
+ # Вычисляем глобальную энтропию
117
+ global_ent = compute_global_entropy(seq_history)
118
+
119
+ fig, axs = plt.subplots(4, 1, figsize=(10, 10))
120
  plt.subplots_adjust(hspace=0.45)
121
 
122
  axs[0].plot(torsion_profile, color='royalblue')
 
134
  axs[2].set_title(f"Автокорреляция / Энтропия: {ent:.2f}")
135
  axs[2].set_xlabel("Лаг")
136
 
137
+ axs[3].plot(stat_entropy, '-o', color='green', markersize=4)
138
+ axs[3].set_title(f"Глобальная Энтропия: {global_ent:.2f}")
139
+ axs[3].set_ylabel("Глобальная энтропия")
140
+
141
  plot_placeholder.pyplot(fig)
142
  plt.close(fig)
143