Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,22 +10,22 @@ import hashlib
|
|
| 10 |
import time
|
| 11 |
import threading
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# ===== 页面设置 =====
|
| 14 |
st.set_page_config(page_title="多肽虚拟筛选工具", layout="wide")
|
| 15 |
st.title("多肽虚拟筛选工具")
|
| 16 |
|
| 17 |
# ===== 防休眠机制 =====
|
| 18 |
def keep_alive():
|
| 19 |
-
"""每隔一段时间在后台执行一次轻量操作,防止应用被云平台冻结"""
|
| 20 |
while True:
|
| 21 |
-
time.sleep(300)
|
| 22 |
try:
|
| 23 |
-
# 对内存中一个极小的数据进行一次无意义的访问,只是为了表明“我还活着”
|
| 24 |
_ = st.session_state.get("_", None)
|
| 25 |
except Exception:
|
| 26 |
pass
|
| 27 |
|
| 28 |
-
# 启动后台保活线程
|
| 29 |
if "alive_thread" not in st.session_state:
|
| 30 |
alive_thread = threading.Thread(target=keep_alive, daemon=True)
|
| 31 |
alive_thread.start()
|
|
@@ -177,11 +177,9 @@ def get_content(src_name):
|
|
| 177 |
st.subheader("开始筛选")
|
| 178 |
|
| 179 |
if st.button("开始筛选", type="primary"):
|
| 180 |
-
# 加载模型
|
| 181 |
tok, mod = load_model()
|
| 182 |
|
| 183 |
-
|
| 184 |
-
food_cache_file = "food_cache.pkl"
|
| 185 |
if os.path.exists(food_cache_file):
|
| 186 |
with open(food_cache_file, "rb") as f:
|
| 187 |
FOOD = pickle.load(f)
|
|
@@ -193,13 +191,11 @@ if st.button("开始筛选", type="primary"):
|
|
| 193 |
pickle.dump(FOOD, f)
|
| 194 |
st.success(f"已获取并缓存 {len(FOOD)} 条蛋白序列")
|
| 195 |
|
| 196 |
-
# 虚拟酶切
|
| 197 |
allp = []
|
| 198 |
for name, seq in FOOD.items():
|
| 199 |
for p in cut(seq, CUT, min_len, max_len):
|
| 200 |
allp.append({"src": name, "p": p})
|
| 201 |
|
| 202 |
-
# 去重(按物种简称 + 肽段序列)
|
| 203 |
seen = set()
|
| 204 |
unip = []
|
| 205 |
for x in allp:
|
|
@@ -214,9 +210,8 @@ if st.button("开始筛选", type="primary"):
|
|
| 214 |
unip.append(x)
|
| 215 |
st.info(f"酶切并去重后共 {len(unip)} 条候选肽")
|
| 216 |
|
| 217 |
-
# AI打分(智能缓存)
|
| 218 |
cache_key = get_cache_key()
|
| 219 |
-
score_cache_file = f"score_cache_{cache_key}.pkl"
|
| 220 |
|
| 221 |
if os.path.exists(score_cache_file):
|
| 222 |
with open(score_cache_file, "rb") as f:
|
|
@@ -241,19 +236,15 @@ if st.button("开始筛选", type="primary"):
|
|
| 241 |
pickle.dump(res, f)
|
| 242 |
st.success(f"已完成打分并缓存")
|
| 243 |
|
| 244 |
-
# 含量标注
|
| 245 |
for x in res:
|
| 246 |
x["content_pct"] = get_content(x["src"])
|
| 247 |
|
| 248 |
-
# 过滤
|
| 249 |
filtered_res = [x for x in res if x["content_pct"] >= content_threshold]
|
| 250 |
if filtered_res:
|
| 251 |
res = filtered_res
|
| 252 |
|
| 253 |
-
# 排序
|
| 254 |
res.sort(key=lambda x: x["score"], reverse=True)
|
| 255 |
|
| 256 |
-
# 显示Top结果(限制显示行数,避免内存溢出)
|
| 257 |
st.subheader("结构相似度排名")
|
| 258 |
df_top = pd.DataFrame(res[:top_n])
|
| 259 |
show_cols = ["src", "p", "score"]
|
|
@@ -263,7 +254,6 @@ if st.button("开始筛选", type="primary"):
|
|
| 263 |
st.caption(f"显示前 {min(top_n, max_display_rows)} 条结果(共 {top_n} 条)")
|
| 264 |
st.dataframe(df_top[show_cols].head(max_display_rows), width='stretch')
|
| 265 |
|
| 266 |
-
# 下载按钮
|
| 267 |
csv_top = df_top.to_csv(index=False).encode("utf-8-sig")
|
| 268 |
st.download_button("下载Top结果CSV", csv_top, "out_top.csv", "text/csv")
|
| 269 |
|
|
@@ -271,7 +261,6 @@ if st.button("开始筛选", type="primary"):
|
|
| 271 |
csv_all = df_all.to_csv(index=False).encode("utf-8-sig")
|
| 272 |
st.download_button("下载全部结果CSV", csv_all, "out_all.csv", "text/csv")
|
| 273 |
|
| 274 |
-
# 物种统计(全部候选肽)
|
| 275 |
st.subheader("物种贡献统计(全部候选肽)")
|
| 276 |
species_counts = {}
|
| 277 |
for x in res:
|
|
|
|
| 10 |
import time
|
| 11 |
import threading
|
| 12 |
|
| 13 |
+
# ===== 永久存储路径 =====
|
| 14 |
+
BASE_DIR = "/mount/src/peptide-screening"
|
| 15 |
+
|
| 16 |
# ===== 页面设置 =====
|
| 17 |
st.set_page_config(page_title="多肽虚拟筛选工具", layout="wide")
|
| 18 |
st.title("多肽虚拟筛选工具")
|
| 19 |
|
| 20 |
# ===== 防休眠机制 =====
|
| 21 |
def keep_alive():
|
|
|
|
| 22 |
while True:
|
| 23 |
+
time.sleep(300)
|
| 24 |
try:
|
|
|
|
| 25 |
_ = st.session_state.get("_", None)
|
| 26 |
except Exception:
|
| 27 |
pass
|
| 28 |
|
|
|
|
| 29 |
if "alive_thread" not in st.session_state:
|
| 30 |
alive_thread = threading.Thread(target=keep_alive, daemon=True)
|
| 31 |
alive_thread.start()
|
|
|
|
| 177 |
st.subheader("开始筛选")
|
| 178 |
|
| 179 |
if st.button("开始筛选", type="primary"):
|
|
|
|
| 180 |
tok, mod = load_model()
|
| 181 |
|
| 182 |
+
food_cache_file = os.path.join(BASE_DIR, "food_cache.pkl")
|
|
|
|
| 183 |
if os.path.exists(food_cache_file):
|
| 184 |
with open(food_cache_file, "rb") as f:
|
| 185 |
FOOD = pickle.load(f)
|
|
|
|
| 191 |
pickle.dump(FOOD, f)
|
| 192 |
st.success(f"已获取并缓存 {len(FOOD)} 条蛋白序列")
|
| 193 |
|
|
|
|
| 194 |
allp = []
|
| 195 |
for name, seq in FOOD.items():
|
| 196 |
for p in cut(seq, CUT, min_len, max_len):
|
| 197 |
allp.append({"src": name, "p": p})
|
| 198 |
|
|
|
|
| 199 |
seen = set()
|
| 200 |
unip = []
|
| 201 |
for x in allp:
|
|
|
|
| 210 |
unip.append(x)
|
| 211 |
st.info(f"酶切并去重后共 {len(unip)} 条候选肽")
|
| 212 |
|
|
|
|
| 213 |
cache_key = get_cache_key()
|
| 214 |
+
score_cache_file = os.path.join(BASE_DIR, f"score_cache_{cache_key}.pkl")
|
| 215 |
|
| 216 |
if os.path.exists(score_cache_file):
|
| 217 |
with open(score_cache_file, "rb") as f:
|
|
|
|
| 236 |
pickle.dump(res, f)
|
| 237 |
st.success(f"已完成打分并缓存")
|
| 238 |
|
|
|
|
| 239 |
for x in res:
|
| 240 |
x["content_pct"] = get_content(x["src"])
|
| 241 |
|
|
|
|
| 242 |
filtered_res = [x for x in res if x["content_pct"] >= content_threshold]
|
| 243 |
if filtered_res:
|
| 244 |
res = filtered_res
|
| 245 |
|
|
|
|
| 246 |
res.sort(key=lambda x: x["score"], reverse=True)
|
| 247 |
|
|
|
|
| 248 |
st.subheader("结构相似度排名")
|
| 249 |
df_top = pd.DataFrame(res[:top_n])
|
| 250 |
show_cols = ["src", "p", "score"]
|
|
|
|
| 254 |
st.caption(f"显示前 {min(top_n, max_display_rows)} 条结果(共 {top_n} 条)")
|
| 255 |
st.dataframe(df_top[show_cols].head(max_display_rows), width='stretch')
|
| 256 |
|
|
|
|
| 257 |
csv_top = df_top.to_csv(index=False).encode("utf-8-sig")
|
| 258 |
st.download_button("下载Top结果CSV", csv_top, "out_top.csv", "text/csv")
|
| 259 |
|
|
|
|
| 261 |
csv_all = df_all.to_csv(index=False).encode("utf-8-sig")
|
| 262 |
st.download_button("下载全部结果CSV", csv_all, "out_all.csv", "text/csv")
|
| 263 |
|
|
|
|
| 264 |
st.subheader("物种贡献统计(全部候选肽)")
|
| 265 |
species_counts = {}
|
| 266 |
for x in res:
|