torusfold-preview/examples/predict_single.py

examples / predict_single.py

examples/predict_single.py · 1.6 KB

#!/usr/bin/env python
"""
单条 fasta → 3D 坐标 .npy 的最小示例。

用法:
  python examples/predict_single.py --fasta seq.fasta --output coords.npy --platform CPU
"""
from __future__ import annotations
import argparse
import time
from pathlib import Path

import numpy as np

from torusfold_scheme2 import predict_3d


def read_fasta(path: str) -> str:
    lines = Path(path).read_text().strip().split("\n")
    return "".join(l.strip() for l in lines if not l.startswith(">"))


def main():
    ap = argparse.ArgumentParser(description="circRNA 序列 → 3D 坐标 (Scheme2 + OpenMM)")
    ap.add_argument("--fasta", required=True, help="输入 fasta 文件")
    ap.add_argument("--output", required=True, help="输出 .npy 路径")
    ap.add_argument("--platform", default="CPU", help="OpenMM 平台: CPU(默认)/OpenCL/Reference")
    ap.add_argument("--n-samples", type=int, default=8, help="几何求解采样数")
    args = ap.parse_args()

    seq = read_fasta(args.fasta)
    print(f"序列长度: {len(seq)} nt")

    t0 = time.time()
    result = predict_3d(seq, n_samples=args.n_samples, platform_name=args.platform)
    print(f"ViennaRNA 配对约束: {len(result['pairs'])} 条")
    print(f"BSJ 闭合距离: {result['bsj_before']:.2f} → {result['bsj_after']:.2f} Å (理想 ~5.9)")
    print(f"OpenMM 能量: {result['e0']:.0f} → {result['e1']:.0f} kJ/mol")
    print(f"总耗时: {time.time()-t0:.1f}s")

    Path(args.output).parent.mkdir(parents=True, exist_ok=True)
    np.save(args.output, result["coords"])
    print(f"\n已保存: {args.output}  shape={result['coords'].shape}")


if __name__ == "__main__":
    main()

← 返回文件树 · 在 GitHub 上查看