File size: 9,199 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Copyright 2025 ByteDance and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
import subprocess
import tempfile
from typing import Any, Dict, List

from pxdbench.globals import MPNN_CKPT_PATH, _require


class VanillaMPNNPredictor:
    def __init__(self, cfg, device_id: int = 0, seed: int = None, verbose=True):
        self.cfg = cfg
        self.device_id = device_id
        self.seed = seed
        self.verbose = verbose
        _require(
            os.path.join(
                MPNN_CKPT_PATH[self.cfg["model_type"]], self.cfg["model_name"] + ".pt"
            )
        )

        dir_name = os.path.dirname(__file__)
        self.prepare_script_path = os.path.join(dir_name, "parse_multiple_chains.py")
        self.run_script_path = os.path.join(dir_name, "protein_mpnn_run.py")
        self.env = os.environ.copy()
        self.env["CUDA_VISIBLE_DEVICES"] = str(device_id)

    def prepare_jsonl(self, input_dir: str, pdb_names: list[str]):
        """

        Prepare a JSONL file for ProteinMPNN input by parsing multiple chains from PDB files.



        Args:

            input_dir (str): Directory containing input PDB files.

            pdb_names (list[str]): List of PDB base names (without '.pdb') to process.



        Returns:

            str: Path to the output JSONL file.

        """

        with tempfile.NamedTemporaryFile(suffix=".jsonl", delete=False) as f:
            output_path = f.name

        output_path = os.path.join(input_dir, "parsed_pdbs.jsonl")
        cmd = [
            "python3",
            "-u",
            self.prepare_script_path,
            "--input_path",
            input_dir,
            "--output",
            output_path,
        ]

        try:
            process = subprocess.Popen(
                cmd,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                text=True,
                env=self.env,
            )
            while True:
                output = process.stdout.readline()
                if output == "" and process.poll() is not None:
                    break
                if output:
                    print(output.strip())

            while True:
                error = process.stderr.readline()
                if error == "" and process.poll() is not None:
                    break
                if error:
                    print(error.strip())

            returncode = process.wait()
            print(f"Run subprocess success: {returncode}")

        except Exception as e:
            print(f"Run subprocess fail: {str(e)}")
            raise

        result = []
        with open(output_path, "r") as infile:
            for line in infile:
                entry = json.loads(line)
                if entry.get("name") in pdb_names:
                    result.append(entry)
        with open(output_path, "w") as f:
            for entry in result:
                f.write(json.dumps(entry) + "\n")
        return output_path

    def run_mpnn(self, jsonl_path: str, pdb_names: list[str], num_seqs: int):
        """

        Run ProteinMPNN to design sequences for the given PDB names.



        Args:

            jsonl_path (str): Path to the input JSONL file containing parsed PDB data.

            pdb_names (list[str]): List of PDB base names (without '.pdb') to process.

            num_seqs (int): Number of sequences to generate per PDB.



        Returns:

            None

        """

        model_type = self.cfg["model_type"]
        model_name = self.cfg["model_name"]
        assert model_type in ["ca", "bb", "soluable"]
        path_to_model_weights = MPNN_CKPT_PATH[model_type]

        output_dir = os.path.dirname(jsonl_path)
        cmd = [
            "python3",
            "-u",
            self.run_script_path,
            "--jsonl_path",
            jsonl_path,
            "--out_folder",
            output_dir,
            "--num_seq_per_target",
            str(num_seqs),
            "--sampling_temp",
            self.cfg["temperature"],
            "--batch_size",
            str(num_seqs),
            "--model_name",
            model_name,
            "--path_to_model_weights",
            path_to_model_weights,
        ]
        if model_type == "ca":
            cmd.extend(["--ca_only"])
        elif model_type == "soluble":
            cmd.extend(["--use_soluble_model"])
        if self.seed is not None:
            cmd.extend(["--seed", str(self.seed)])

        print("Run Protein MPNN with %s" % (" ".join(cmd)))
        try:
            process = subprocess.Popen(
                cmd,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                text=True,
                env=self.env,
            )
            while True:
                output = process.stdout.readline()
                if output == "" and process.poll() is not None:
                    break
                if output:
                    print(output.strip())

            while True:
                error = process.stderr.readline()
                if error == "" and process.poll() is not None:
                    break
                if error:
                    print(error.strip())

            returncode = process.wait()
            print(f"Run subprocess success: {returncode}")

        except Exception as e:
            print(f"Run subprocess fail: {str(e)}")
            raise

        # postprocess
        seq_dir = os.path.join(output_dir, "seqs")
        result = []
        for name in pdb_names:
            fasta_file = name + ".fa"
            fasta_path = os.path.join(seq_dir, fasta_file)
            if not os.path.exists(fasta_path):
                continue

            with open(fasta_path, "r") as f:
                lines = f.readlines()

            for i, line_no in enumerate(
                range(4, int(num_seqs) * 2 + 3, 2)
            ):  # lines 4,6,...,18 (1-based)
                if line_no <= len(lines):
                    sequence = lines[line_no - 1].strip()
                    result.append({"name": name, "seq_idx": i, "sequence": sequence})
        return result

    def design_monomer(

        self, pdb_dir: str, pdb_names: List[str], num_samples: int

    ) -> List[Dict]:
        """

        Design sequences for monomer proteins using ProteinMPNN.



        Args:

            pdb_dir (str): Directory containing input PDB files.

            pdb_names (list[str]): List of PDB base names (without '.pdb') to process.

            num_samples (int): Number of sequences to generate per PDB.



        Returns:

            list[dict]: List of design results with keys 'name' (PDB name), 'seq_idx' (sequence index),

                        and 'sequence' (designed amino acid sequence).

        """

        # input_data = {
        #     "pdb_dir": pdb_dir,
        #     "pdb_names": pdb_names,
        #     "num_samples": num_samples,
        #     "mpnn_cfg": self.cfg.to_dict(),
        #     "design_type": "monomer",
        # }
        jsonl_path = self.prepare_jsonl(pdb_dir, pdb_names)
        output = self.run_mpnn(jsonl_path, pdb_names, num_samples)
        os.unlink(jsonl_path)
        return output

    def design_binder(

        self,

        pdb_dir: str,

        pdb_names: List[str],

        num_samples: int,

        binder_chains: List[str],

        cond_chains: List[str],

    ) -> List[Dict]:
        """

        Design sequences for binder proteins using ProteinMPNN.



        Args:

            pdb_dir (str): Directory containing input PDB files.

            pdb_names (list[str]): List of PDB base names (without '.pdb') to process.

            num_samples (int): Number of sequences to generate per PDB.

            binder_chains (list[str]): List of binder chain IDs.

            cond_chains (list[str]): List of conditional chain IDs.



        Returns:

            list[dict]: List of design results with keys 'name' (PDB name), 'seq_idx' (sequence index),

                        and 'sequence' (designed amino acid sequence).

        """
        input_data = {
            "pdb_dir": pdb_dir,
            "pdb_names": pdb_names,
            "num_samples": num_samples,
            "binder_chains": binder_chains,
            "cond_chains": cond_chains,
            "mpnn_cfg": self.cfg.to_dict(),
            "design_type": "binder",
        }
        output = self.run(input_data)
        return output