유전자 변경 시 이전 결과 삭제, 보고서 파일명에 입력파일·혈청형·gene 반영
Browse files- app.py +15 -1
- report_builder.py +47 -4
app.py
CHANGED
|
@@ -639,10 +639,13 @@ def build_analysis_report(lang: str = "ko"):
|
|
| 639 |
try:
|
| 640 |
import report_builder
|
| 641 |
paths = get_path_config(s)
|
|
|
|
|
|
|
|
|
|
| 642 |
res = report_builder.build_report(
|
| 643 |
paths['results'], lang=lang, region_map=s.region_map, endemic_countries=s.source_countries,
|
| 644 |
gene_label=str(s.targetGene), serotype_label="DENV" + str(s.targetSerotype),
|
| 645 |
-
prefer_tag=str(s.targetGene).replace("/", "-"),
|
| 646 |
)
|
| 647 |
if not res.get("html"):
|
| 648 |
return "", None, None, res.get("message", "")
|
|
@@ -1048,13 +1051,24 @@ def run_stage1(lang: str = "ko"):
|
|
| 1048 |
)
|
| 1049 |
|
| 1050 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1051 |
def run_stage2(lang: str = "ko"):
|
|
|
|
| 1052 |
yield from _run_stage_streaming(
|
| 1053 |
stage2_predict_origins.main, _STAGE2_LABEL_KO, _STAGE2_LABEL_EN, n_buttons=2, lang=lang
|
| 1054 |
)
|
| 1055 |
|
| 1056 |
|
| 1057 |
def run_stage3(lang: str = "ko"):
|
|
|
|
| 1058 |
yield from _run_stage_streaming(
|
| 1059 |
functools.partial(stage3_geo_analysis.main, lang=lang),
|
| 1060 |
_STAGE3_LABEL_KO, _STAGE3_LABEL_EN, n_buttons=2, lang=lang
|
|
|
|
| 639 |
try:
|
| 640 |
import report_builder
|
| 641 |
paths = get_path_config(s)
|
| 642 |
+
report_builder.purge_other_gene_results(paths['results'], str(s.targetGene))
|
| 643 |
+
user_seqs = sorted(f.stem for f in _list_user_sequence_files())
|
| 644 |
+
input_name = (user_seqs[0] + (f"_plus{len(user_seqs)-1}" if len(user_seqs) > 1 else "")) if user_seqs else ""
|
| 645 |
res = report_builder.build_report(
|
| 646 |
paths['results'], lang=lang, region_map=s.region_map, endemic_countries=s.source_countries,
|
| 647 |
gene_label=str(s.targetGene), serotype_label="DENV" + str(s.targetSerotype),
|
| 648 |
+
prefer_tag=str(s.targetGene).replace("/", "-"), input_name=input_name,
|
| 649 |
)
|
| 650 |
if not res.get("html"):
|
| 651 |
return "", None, None, res.get("message", "")
|
|
|
|
| 1051 |
)
|
| 1052 |
|
| 1053 |
|
| 1054 |
+
def _purge_previous_gene_results():
|
| 1055 |
+
"""대상 유전자를 바꿔 다시 분석할 때, 이전 유전자의 결과 파일(Stage 2/3 CSV·그림)을 지웁니다."""
|
| 1056 |
+
try:
|
| 1057 |
+
import report_builder
|
| 1058 |
+
report_builder.purge_other_gene_results(get_path_config(s)['results'], str(s.targetGene))
|
| 1059 |
+
except Exception:
|
| 1060 |
+
traceback.print_exc()
|
| 1061 |
+
|
| 1062 |
+
|
| 1063 |
def run_stage2(lang: str = "ko"):
|
| 1064 |
+
_purge_previous_gene_results()
|
| 1065 |
yield from _run_stage_streaming(
|
| 1066 |
stage2_predict_origins.main, _STAGE2_LABEL_KO, _STAGE2_LABEL_EN, n_buttons=2, lang=lang
|
| 1067 |
)
|
| 1068 |
|
| 1069 |
|
| 1070 |
def run_stage3(lang: str = "ko"):
|
| 1071 |
+
_purge_previous_gene_results()
|
| 1072 |
yield from _run_stage_streaming(
|
| 1073 |
functools.partial(stage3_geo_analysis.main, lang=lang),
|
| 1074 |
_STAGE3_LABEL_KO, _STAGE3_LABEL_EN, n_buttons=2, lang=lang
|
report_builder.py
CHANGED
|
@@ -240,6 +240,42 @@ def _find(results_dir: Path, pattern: str, sub: str = "", suffix: str | None = N
|
|
| 240 |
return _newest(files)
|
| 241 |
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
def _pct(x, nd=1):
|
| 244 |
try:
|
| 245 |
return f"{float(x):.{nd}f}"
|
|
@@ -273,7 +309,7 @@ def _img_b64(path: str, max_kb: int = 1500):
|
|
| 273 |
|
| 274 |
|
| 275 |
def build_report(results_dir, lang="ko", region_map=None, endemic_countries=None,
|
| 276 |
-
gene_label="", serotype_label="", out_dir=None, prefer_tag=""):
|
| 277 |
L = _TXT.get(lang, _TXT["en"])
|
| 278 |
results_dir = Path(results_dir)
|
| 279 |
region_map = region_map or {}
|
|
@@ -456,14 +492,21 @@ def build_report(results_dir, lang="ko", region_map=None, endemic_countries=None
|
|
| 456 |
# ── 파일 저장 + ZIP ─────────────────────────────────────────────────────
|
| 457 |
out_dir = Path(out_dir) if out_dir else results_dir / "_download"
|
| 458 |
out_dir.mkdir(parents=True, exist_ok=True)
|
| 459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
html_path.write_text(page, encoding="utf-8")
|
| 461 |
-
zip_path = out_dir / f"
|
| 462 |
n_files = 1
|
| 463 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 464 |
zf.write(html_path, arcname=html_path.name)
|
| 465 |
for f in sorted(results_dir.rglob("*")):
|
| 466 |
-
if f.is_file() and f.suffix.lower() in {".csv", ".png"} and "_download" not in f.parts
|
|
|
|
| 467 |
zf.write(f, arcname=str(Path("data") / f.relative_to(results_dir)))
|
| 468 |
n_files += 1
|
| 469 |
return dict(html=screen, html_page=page, md="\n".join(md_lines), html_path=str(html_path),
|
|
|
|
| 240 |
return _newest(files)
|
| 241 |
|
| 242 |
|
| 243 |
+
_GENE_TAGS = ("C-prM", "E", "Polyprotein")
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
def _file_gene_tag(name: str):
|
| 247 |
+
"""파일명에 들어 있는 유전자 태그(C-prM / E / Polyprotein)를 돌려줍니다. 없으면 None."""
|
| 248 |
+
stem = re.sub(r"\.[A-Za-z0-9]+$", "", os.path.basename(name))
|
| 249 |
+
toks = re.split(r"[_.]", stem)
|
| 250 |
+
for g in _GENE_TAGS:
|
| 251 |
+
if g in toks:
|
| 252 |
+
return g
|
| 253 |
+
return None
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def purge_other_gene_results(results_dir, keep_gene):
|
| 257 |
+
"""결과 폴더(하위 폴더 포함)에서 keep_gene 이 아닌 다른 유전자의 결과 파일을 삭제합니다. 삭제한 파일 수 반환."""
|
| 258 |
+
keep = str(keep_gene).replace("/", "-")
|
| 259 |
+
results_dir = Path(results_dir)
|
| 260 |
+
if not results_dir.exists() or keep not in _GENE_TAGS:
|
| 261 |
+
return 0
|
| 262 |
+
n = 0
|
| 263 |
+
for f in list(results_dir.rglob("*")):
|
| 264 |
+
if not f.is_file() or "_download" in f.parts:
|
| 265 |
+
continue
|
| 266 |
+
g = _file_gene_tag(f.name)
|
| 267 |
+
if g and g != keep:
|
| 268 |
+
try:
|
| 269 |
+
f.unlink(); n += 1
|
| 270 |
+
except Exception:
|
| 271 |
+
pass
|
| 272 |
+
return n
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def _safe_part(x: str) -> str:
|
| 276 |
+
return re.sub(r"[^0-9A-Za-z._-]+", "-", str(x)).strip("-._") or "x"
|
| 277 |
+
|
| 278 |
+
|
| 279 |
def _pct(x, nd=1):
|
| 280 |
try:
|
| 281 |
return f"{float(x):.{nd}f}"
|
|
|
|
| 309 |
|
| 310 |
|
| 311 |
def build_report(results_dir, lang="ko", region_map=None, endemic_countries=None,
|
| 312 |
+
gene_label="", serotype_label="", out_dir=None, prefer_tag="", input_name=""):
|
| 313 |
L = _TXT.get(lang, _TXT["en"])
|
| 314 |
results_dir = Path(results_dir)
|
| 315 |
region_map = region_map or {}
|
|
|
|
| 492 |
# ── 파일 저장 + ZIP ─────────────────────────────────────────────────────
|
| 493 |
out_dir = Path(out_dir) if out_dir else results_dir / "_download"
|
| 494 |
out_dir.mkdir(parents=True, exist_ok=True)
|
| 495 |
+
for old_f in out_dir.glob("DenAIGeo_*"):
|
| 496 |
+
try:
|
| 497 |
+
old_f.unlink()
|
| 498 |
+
except Exception:
|
| 499 |
+
pass
|
| 500 |
+
base = "_".join(_safe_part(x) for x in (input_name, serotype_label or "DENV", tag) if x) + f"_report_{lang}"
|
| 501 |
+
html_path = out_dir / f"{base}.html"
|
| 502 |
html_path.write_text(page, encoding="utf-8")
|
| 503 |
+
zip_path = out_dir / f"{base}.zip"
|
| 504 |
n_files = 1
|
| 505 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 506 |
zf.write(html_path, arcname=html_path.name)
|
| 507 |
for f in sorted(results_dir.rglob("*")):
|
| 508 |
+
if f.is_file() and f.suffix.lower() in {".csv", ".png"} and "_download" not in f.parts \
|
| 509 |
+
and _file_gene_tag(f.name) in (None, tag):
|
| 510 |
zf.write(f, arcname=str(Path("data") / f.relative_to(results_dir)))
|
| 511 |
n_files += 1
|
| 512 |
return dict(html=screen, html_page=page, md="\n".join(md_lines), html_path=str(html_path),
|