File size: 6,093 Bytes
d766458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import argparse
import shutil
import subprocess
from pathlib import Path

import numpy as np

from pxdbench.metrics.diversity import compute_diversity
from pxdbench.utils import str2bool

FOLDSEEK_BIN = "your_foldseek_dir/bin/foldseek"
FOLDSEEK_DB = "your_foldseek_dir/foldseek_db/pdb/pdb"


def compute_fs_diversity(input_dir: Path, num_threads=32):
    num_pdbs = sum(1 for f in input_dir.glob("*.pdb") if f.is_file())
    cluster_out = input_dir / "fs_diversity" / "res"
    tmp_dir = input_dir / "fs_diversity_tmp"
    cluster_out.parent.mkdir(parents=True, exist_ok=True)
    cluster_tsv = cluster_out.with_name(cluster_out.stem + "_cluster.tsv")
    if tmp_dir.exists():
        shutil.rmtree(tmp_dir)
    tmp_dir.mkdir(parents=True, exist_ok=True)
    if not cluster_tsv.exists():
        subprocess.run(
            [
                FOLDSEEK_BIN,
                "easy-cluster",
                str(input_dir),
                str(cluster_out),
                str(tmp_dir),
                "--alignment-type",
                "1",
                "--cov-mode",
                "0",
                "--min-seq-id",
                "0",
                "--tmscore-threshold",
                "0.5",
                "--threads",
                f"{num_threads}",
            ],
            check=True,
        )
    # Count clusters and samples
    num_clusters = 0
    seen_clusters = set()
    if cluster_tsv.exists():
        with open(cluster_tsv) as f:
            for line in f:
                cluster_name, member = line.strip().split("\t")
                if cluster_name not in seen_clusters:
                    seen_clusters.add(cluster_name)
                    num_clusters += 1
    diversity_cluster = num_clusters / max(num_pdbs, 1)
    shutil.rmtree(tmp_dir)
    return diversity_cluster


def compute_fs_novelty(input_dir: Path, use_gpu=True, num_threads=32):
    tmp_dir = input_dir / "fs_novelty_tmp"
    if tmp_dir.exists():
        shutil.rmtree(tmp_dir)
    novelty_out = input_dir / "fs_novelty" / "novelty.tsv"
    novelty_out.parent.mkdir(parents=True, exist_ok=True)

    if not novelty_out.exists():
        cmd = [
            FOLDSEEK_BIN,
            "easy-search",
            str(input_dir),
            FOLDSEEK_DB,
            str(novelty_out),
            str(tmp_dir),
            "--alignment-type",
            "1",
            "--exhaustive-search",
            "--tmscore-threshold",
            "0.0",
            "--max-seqs",
            "10000000000",
            "--format-output",
            "query,target,alntmscore,lddt",
            "--threads",
            f"{num_threads}",
        ]
        if use_gpu:
            cmd.extend(["--gpu", "1", "--prefilter-mode", "1"])
        subprocess.run(
            cmd,
            check=True,
        )

    max_scores = {}
    if novelty_out.exists():
        with open(novelty_out) as f:
            for line in f:
                query, _, tmscore, _ = line.strip().split("\t")
                tmscore = float(tmscore)
                if query not in max_scores or tmscore > max_scores[query]:
                    max_scores[query] = tmscore
    novelty_score = np.mean(list(max_scores.values()))
    shutil.rmtree(tmp_dir)
    return novelty_score


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input_dir", type=str)
    parser.add_argument("--output_dir", type=str, default=None)
    parser.add_argument("--eval_novelty", action="store_true", default=False)
    parser.add_argument("--use_gpu", type=str2bool, default=True)
    parser.add_argument("--num_threads", type=int, default=32)
    args = parser.parse_args()

    input_dir = Path(args.input_dir)
    if not input_dir.exists():
        print(f"Input dir does not exist! {input_dir}")
        return
    if args.output_dir is None:
        output_dir = input_dir / "postprocess"
    else:
        output_dir = Path(args.output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    output_csv_path = output_dir / "diversity_and_novelty.csv"
    fieldnames = ["num_samples", "diversity_tm", "diversity_cluster", "novelty"]

    # scan PDBs
    pdb_paths = sorted(input_dir.glob("*.pdb"))
    pdb_paths = [p for p in pdb_paths if p.is_file()]
    if len(pdb_paths) == 0:
        print(f"No PDB files found in {input_dir}")
        # wrtie an empty file
        with open(output_csv_path, "w") as f:
            f.write(",".join(fieldnames) + "\n")
            f.write("0,,,\n")
        return

    # Diversity (TM)
    try:
        diversity_tm = compute_diversity(pdb_paths) if len(pdb_paths) >= 2 else np.nan
    except Exception as e:
        print(f"compute_diversity failed: {e}")
        diversity_tm = np.nan

    # Diversity (Cluster, Foldseek)
    try:
        diversity_cluster = compute_fs_diversity(
            input_dir, num_threads=args.num_threads
        )
    except Exception as e:
        print(f"compute_fs_diversity failed: {e}")
        diversity_cluster = np.nan

    novelty = np.nan
    if args.eval_novelty:
        if not Path(FOLDSEEK_BIN).exists():
            print(f"Foldseek binary not found: {FOLDSEEK_BIN}, skip novelty.")
        elif (
            not Path(FOLDSEEK_DB + ".dbtype").exists()
            and not Path(FOLDSEEK_DB).exists()
        ):
            print(f"Foldseek DB not found: {FOLDSEEK_DB}, skip novelty.")
        else:
            try:
                novelty = compute_fs_novelty(
                    input_dir, args.use_gpu, num_threads=args.num_threads
                )
            except Exception as e:
                print(f"compute_fs_novelty failed: {e}")
                novelty = np.nan

    with open(output_csv_path, "w") as f:
        f.write(",".join(fieldnames) + "\n")
        f.write(f"{len(pdb_paths)},{diversity_tm},{diversity_cluster},{novelty}\n")
    print(f"Wrote results to {output_csv_path}")


if __name__ == "__main__":
    main()