Skip to content

Meeko Process Module

A script to process receptors using Meeko in order to prepare them for molecular docking with AutoDock-GPU.

ReceptorProcess

Receptor processing pipeline for preparing proteins for AutoDock-GPU molecular docking.

This class provides a complete workflow for processing receptor PDB files through Meeko's mk_prepare_receptor.py tool and AutoGrid4. It handles common issues like unrecognized residues and heteroatoms, and supports parallel processing via Ray.

The processing pipeline consists of three main steps:

  1. Preprocessing (optional): Remove heteroatoms (ligands, waters, ions) from the receptor using PDBFixer/OpenMM.

  2. Meeko processing: Convert PDB to PDBQT format and generate grid parameter files (.gpf) with specified docking box dimensions.

  3. AutoGrid computation: Precompute affinity maps for faster docking.

The class handles edge cases gracefully:

  • Skips already-processed receptors (checks for _ag.pdbqt and _ag.maps.fld)
  • Retries with --allow_bad_res flag if unrecognized residues are outside the docking box (non-critical)
  • Reports critical errors when unrecognized residues are inside the docking box

Attributes:

Name Type Description
data_path str

Root directory containing receptor data.

pre_process_receptors bool

Whether to remove heteroatoms before Meeko.

logger Logger

Logger instance for this class.

receptor_path str

Path to directory containing PDB files.

pockets Dict[str, Dict[str, Any]]

Docking box specifications loaded from pockets_info.json. Each entry contains 'center' and 'size' keys.

cmd str

Template command for mk_prepare_receptor.py.

Example
import ray
ray.init()

processor = ReceptorProcess(
    data_path="/path/to/data",
    pre_process_receptors=True
)

# Process all receptors defined in pockets_info.json
warnings, errors = processor.process_receptors(use_pbar=True)

# Process specific receptors
warnings, errors = processor.process_receptors(
    receptors=["1abc", "2def"],
    allow_bad_res=True
)
Note

Requires Ray to be initialized before calling process_receptors(). External dependencies: Meeko, AutoGrid4, OpenMM (optional), PDBFixer (optional).

Source code in mol_gen_docking/data/meeko_process.py
 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
class ReceptorProcess:
    """Receptor processing pipeline for preparing proteins for AutoDock-GPU molecular docking.

    This class provides a complete workflow for processing receptor PDB files through
    Meeko's `mk_prepare_receptor.py` tool and AutoGrid4. It handles common issues like
    unrecognized residues and heteroatoms, and supports parallel processing via Ray.

    The processing pipeline consists of three main steps:

    1. **Preprocessing (optional)**: Remove heteroatoms (ligands, waters, ions) from the
       receptor using PDBFixer/OpenMM.

    2. **Meeko processing**: Convert PDB to PDBQT format and generate grid parameter
       files (.gpf) with specified docking box dimensions.

    3. **AutoGrid computation**: Precompute affinity maps for faster docking.

    The class handles edge cases gracefully:

    - Skips already-processed receptors (checks for `_ag.pdbqt` and `_ag.maps.fld`)
    - Retries with `--allow_bad_res` flag if unrecognized residues are outside the
      docking box (non-critical)
    - Reports critical errors when unrecognized residues are inside the docking box

    Attributes:
        data_path (str): Root directory containing receptor data.
        pre_process_receptors (bool): Whether to remove heteroatoms before Meeko.
        logger (logging.Logger): Logger instance for this class.
        receptor_path (str): Path to directory containing PDB files.
        pockets (Dict[str, Dict[str, Any]]): Docking box specifications loaded from
            `pockets_info.json`. Each entry contains 'center' and 'size' keys.
        cmd (str): Template command for mk_prepare_receptor.py.

    Example:
        ```python
        import ray
        ray.init()

        processor = ReceptorProcess(
            data_path="/path/to/data",
            pre_process_receptors=True
        )

        # Process all receptors defined in pockets_info.json
        warnings, errors = processor.process_receptors(use_pbar=True)

        # Process specific receptors
        warnings, errors = processor.process_receptors(
            receptors=["1abc", "2def"],
            allow_bad_res=True
        )
        ```

    Note:
        Requires Ray to be initialized before calling `process_receptors()`.
        External dependencies: Meeko, AutoGrid4, OpenMM (optional), PDBFixer (optional).
    """

    def __init__(self, data_path: str, pre_process_receptors: bool = False) -> None:
        """Initialize the receptor processor with data paths and configuration.

        Validates that required files and directories exist, and loads the docking
        box specifications from `pockets_info.json`.

        Args:
            data_path (str): Path to the data directory. Must contain:

                - `pdb_files/`: Directory with receptor PDB files
                - `pockets_info.json`: JSON file mapping receptor names to docking
                  box specifications with 'center' [x, y, z] and 'size' [sx, sy, sz]

            pre_process_receptors (bool): If True, remove heteroatoms (ligands,
                waters, ions) from receptors before Meeko processing. Uses PDBFixer.
                Default: False.

        Raises:
            AssertionError: If `data_path` does not exist.
            AssertionError: If `pdb_files/` subdirectory does not exist.
            AssertionError: If `pockets_info.json` does not exist.
        """
        self.data_path: str = data_path
        self.pre_process_receptors: bool = pre_process_receptors
        self.logger = logging.getLogger(
            __name__ + "/" + self.__class__.__name__,
        )
        self.receptor_path = os.path.join(self.data_path, "pdb_files")

        assert os.path.exists(data_path), f"Data path {data_path} does not exist."
        assert os.path.exists(self.receptor_path), (
            f"Receptor path {self.receptor_path} does not exist."
        )
        assert os.path.exists(os.path.join(data_path, "pockets_info.json")), (
            f"Pockets info file does not exist in {data_path}."
        )

        with open(os.path.join(data_path, "pockets_info.json")) as f:
            self.pockets: Dict[str, Dict[str, Any]] = json.load(f)
        self.cmd = "mk_prepare_receptor.py -i {INPUT} -o {OUTPUT} -p -g --box_size {BOX_SIZE} --box_center {BOX_CENTER}"

    def _run_meeko(
        self, input_path: str, receptor: str, bad_res: bool = False
    ) -> Tuple[str, int, str]:
        """Execute Meeko's mk_prepare_receptor.py on a single receptor.

        Runs the Meeko command to convert a PDB file to PDBQT format and generate
        grid parameter files for AutoDock-GPU. The docking box dimensions are
        retrieved from the loaded pockets configuration.

        Args:
            input_path (str): Full path to the input PDB file.
            receptor (str): Receptor identifier (key in pockets dict) used to
                look up docking box center and size.
            bad_res (bool): If True, add `--allow_bad_res` flag to permit
                unrecognized residues. Default: False.

        Returns:
            Tuple[str, int, str]: A tuple containing:

                - stderr_text: Standard error output from Meeko (useful for
                  parsing error messages about unrecognized residues)
                - returncode: Process return code (0 = success)
                - output_path: Path to output files (without extension)

        Raises:
            subprocess.TimeoutExpired: If Meeko takes longer than 300 seconds.
        """
        output_path = input_path.replace(".pdb", "_ag")

        box_center = " ".join([str(x) for x in self.pockets[receptor]["center"]])
        box_size = " ".join([str(x) for x in self.pockets[receptor]["size"]])

        command = self.cmd.format(
            INPUT=input_path,
            OUTPUT=output_path,
            BOX_SIZE=box_size,
            BOX_CENTER=box_center,
        )
        if bad_res:
            command += " --allow_bad_res"

        self.logger.info(f"Running command: {command}")
        process = sp.Popen(
            command,
            shell=True,
            stdout=sp.DEVNULL,
            stderr=sp.PIPE,
            preexec_fn=os.setpgrp,
        )
        _, stderr = process.communicate(
            timeout=300,
        )

        stderr_text = stderr.decode("utf-8")

        return stderr_text, process.returncode, output_path

    def meeko_process(
        self, receptor: str, allow_bad_res: bool = False
    ) -> Tuple[int, str]:
        """Process a receptor with Meeko, handling unrecognized residue errors.

        This method implements smart error handling for Meeko failures:

        1. First attempts processing without `--allow_bad_res`
        2. If unrecognized residues are found, checks if they're inside the docking box
        3. If outside the box: retries with `--allow_bad_res` (returns 0)
        4. If inside the box: retries with `--allow_bad_res` but flags as warning (returns 1)

        Args:
            receptor (str): Receptor identifier matching a key in `pockets_info.json`
                and a file `{receptor}.pdb` in the pdb_files directory.
            allow_bad_res (bool): If True, always use `--allow_bad_res` flag.
                Default: False.

        Returns:
            Tuple[int, str]: A tuple containing:

                - status code:
                    - 0: Success (no issues or issues outside docking box)
                    - 1: Warning (unrecognized residues inside docking box)
                - output_path: Path to the processed output files (without extension)

        Raises:
            ValueError: If Meeko fails for reasons other than unrecognized residues.
        """
        input_path = os.path.join(self.receptor_path, f"{receptor}.pdb")
        stderr_text, returncode, output_path = self._run_meeko(
            input_path, receptor, allow_bad_res
        )
        if returncode != 0:
            # Check if failing resiudes are inside the docking box
            failed_residues = set()
            for line in stderr_text.splitlines():
                match = re.search(
                    r"No template matched for residue_key='(\w+:\d+)'", line
                )
                if match:
                    failed_residues.add(match.group(1))

            if failed_residues:
                residues_in_box = check_failed_residues_in_box(
                    input_path,
                    failed_residues,
                    self.pockets[receptor]["center"],
                    self.pockets[receptor]["size"],
                )
                if residues_in_box == []:
                    _, returncode, output_path = self._run_meeko(
                        input_path, receptor, bad_res=True
                    )
                    return returncode, output_path
                else:
                    _, returncode, output_path = self._run_meeko(
                        input_path, receptor, bad_res=True
                    )
                    return 1, output_path
            else:
                raise ValueError(
                    f"Error in Meeko processing {receptor}:\n{stderr_text}"
                )
        return 0, output_path

    def _run_autogrid(self, path: str) -> None:
        """Run AutoGrid4 to precompute affinity maps for a processed receptor.

        Executes `autogrid4` on the grid parameter file (.gpf) generated by Meeko.
        The resulting map files are required for AutoDock-GPU docking.

        Args:
            path (str): Path to the receptor files (with or without extension).
                The basename is extracted and used to locate the .gpf file
                in the receptor_path directory.

        Raises:
            RuntimeError: If AutoGrid4 returns a non-zero exit code.

        Note:
            AutoGrid4 must be installed and available in PATH.
        """
        path = os.path.basename(path)
        grid_command = f"autogrid4 -p {path}.gpf -l {path}.glg -d"
        self.logger.info(f"Running command: {grid_command}")
        process = sp.Popen(
            grid_command,
            shell=True,
            stdout=sp.PIPE,
            stderr=sp.PIPE,
            cwd=self.receptor_path,
        )
        stdout, stderr = process.communicate()
        if process.returncode != 0:
            self.logger.error(
                f"Error in running AutoGrid on {path}:\n{stderr.decode()}"
            )
            raise RuntimeError(f"AutoGrid failed for {path}")
        self.logger.info(f"Successfully ran AutoGrid on {path}")

    def remove_heterogenous(self, receptor: str) -> None:
        """Remove heteroatoms from a receptor PDB file using PDBFixer.

        Removes all heteroatoms including ligands, water molecules, and ions
        from the receptor structure. The cleaned structure overwrites the
        original PDB file.

        Args:
            receptor (str): Receptor identifier. The file
                `{receptor_path}/{receptor}.pdb` will be modified in-place.

        Note:
            Requires OpenMM and PDBFixer packages.

        Warning:
            This method modifies the original PDB file. Make a backup if needed.
        """
        from openmm.app import PDBFile
        from pdbfixer import PDBFixer

        pdb_file = os.path.join(self.receptor_path, f"{receptor}.pdb")

        fixer = PDBFixer(filename=pdb_file)
        fixer.removeHeterogens(True)

        PDBFile.writeFile(fixer.topology, fixer.positions, open(pdb_file, "w"))

    def process_receptors(
        self,
        receptors: list[str] = [],
        allow_bad_res: bool = False,
        use_pbar: bool = False,
    ) -> Tuple[list[str], list[str]]:
        """Process multiple receptors in parallel using Ray.

        Orchestrates the full processing pipeline for a batch of receptors:

        1. Filters out already-processed receptors (with existing `_ag.pdbqt`
           and `_ag.maps.fld` files)
        2. Optionally removes heteroatoms (if `pre_process_receptors=True`)
        3. Runs Meeko processing with error recovery
        4. Runs AutoGrid4 for successful conversions
        5. Collects and categorizes failures

        Processing is parallelized using Ray remote functions, with each receptor
        allocated 4 CPU cores.

        Args:
            receptors (list[str]): List of receptor identifiers to process.
                If empty, processes all receptors in `pockets_info.json`.
                Default: [].
            allow_bad_res (bool): If True, always use `--allow_bad_res` flag
                in Meeko. Default: False.
            use_pbar (bool): If True, display a tqdm progress bar via Ray.
                Default: False.

        Returns:
            Tuple[list[str], list[str]]: Two lists of receptor identifiers:

                - warnings: Receptors with non-critical issues (unrecognized
                  residues inside docking box, but processing completed)
                - errors: Receptors with critical failures (exceptions during
                  processing)

        Raises:
            AssertionError: If any receptor in the list is not found in
                `pockets_info.json`.

        Note:
            Ray must be initialized before calling this method.
        """

        @ray.remote(num_cpus=4)
        def process_receptor(
            receptor: str,
            pbar: Any,
            allow_bad_res: bool = False,
        ) -> int:
            """
            Outputs the level of the error:
            0: Success
            1: Failed residues inside the box
            2: Other errors (critical)
            """
            try:
                if self.pre_process_receptors:
                    self.remove_heterogenous(receptor)
                result, processed_path = self.meeko_process(receptor, allow_bad_res)
                if result <= 1:
                    self._run_autogrid(processed_path)
                if pbar is not None:
                    pbar.update.remote(1)
            except Exception as e:
                self.logger.error(f"Error processing {receptor}: {e}")
                if pbar is not None:
                    pbar.update.remote(1)
                return 2
            return result

        if receptors == []:
            receptors = list(self.pockets.keys())
        else:
            assert all(r in self.pockets for r in receptors), (
                "Some receptors are not in pockets_info.json: "
                + ",".join([r for r in receptors if r not in self.pockets])
            )

        remote_tqdm = ray.remote(tqdm_ray.tqdm)
        if use_pbar:
            pbar = remote_tqdm.remote(total=len(receptors), desc="Processing receptors")
        else:
            pbar = None
        # Find receptors that already have a _ag.pdbqt and_ag.maps.fld file
        receptors_to_process = []
        for receptor in receptors:
            ag_pdbqt_path = os.path.join(self.receptor_path, f"{receptor}_ag.pdbqt")
            ag_maps_fld_path = os.path.join(
                self.receptor_path, f"{receptor}_ag.maps.fld"
            )
            if not (os.path.exists(ag_pdbqt_path) and os.path.exists(ag_maps_fld_path)):
                receptors_to_process.append(receptor)
            else:
                self.logger.info(f"Receptor {receptor} already processed. Skipping.")
                if use_pbar:
                    pbar.update.remote(1)  # type: ignore
        if len(receptors_to_process) == 0:
            self.logger.info("All receptors already processed.")
            if use_pbar:
                pbar.close.remote()  # type: ignore
            return [], []

        self.logger.info(f"Processing {len(receptors_to_process)} receptors.")
        futures = [
            process_receptor.remote(receptor, pbar, allow_bad_res)
            for receptor in receptors_to_process
        ]
        results = ray.get(futures)

        missed_receptors_1 = [
            receptor
            for receptor, success in zip(receptors_to_process, results)
            if success == 1
        ]
        missed_receptors_2 = [
            receptor
            for receptor, success in zip(receptors_to_process, results)
            if success == 2
        ]
        return missed_receptors_1, missed_receptors_2

__init__(data_path, pre_process_receptors=False)

Initialize the receptor processor with data paths and configuration.

Validates that required files and directories exist, and loads the docking box specifications from pockets_info.json.

Parameters:

Name Type Description Default
data_path str

Path to the data directory. Must contain:

  • pdb_files/: Directory with receptor PDB files
  • pockets_info.json: JSON file mapping receptor names to docking box specifications with 'center' [x, y, z] and 'size' [sx, sy, sz]
required
pre_process_receptors bool

If True, remove heteroatoms (ligands, waters, ions) from receptors before Meeko processing. Uses PDBFixer. Default: False.

False

Raises:

Type Description
AssertionError

If data_path does not exist.

AssertionError

If pdb_files/ subdirectory does not exist.

AssertionError

If pockets_info.json does not exist.

Source code in mol_gen_docking/data/meeko_process.py
def __init__(self, data_path: str, pre_process_receptors: bool = False) -> None:
    """Initialize the receptor processor with data paths and configuration.

    Validates that required files and directories exist, and loads the docking
    box specifications from `pockets_info.json`.

    Args:
        data_path (str): Path to the data directory. Must contain:

            - `pdb_files/`: Directory with receptor PDB files
            - `pockets_info.json`: JSON file mapping receptor names to docking
              box specifications with 'center' [x, y, z] and 'size' [sx, sy, sz]

        pre_process_receptors (bool): If True, remove heteroatoms (ligands,
            waters, ions) from receptors before Meeko processing. Uses PDBFixer.
            Default: False.

    Raises:
        AssertionError: If `data_path` does not exist.
        AssertionError: If `pdb_files/` subdirectory does not exist.
        AssertionError: If `pockets_info.json` does not exist.
    """
    self.data_path: str = data_path
    self.pre_process_receptors: bool = pre_process_receptors
    self.logger = logging.getLogger(
        __name__ + "/" + self.__class__.__name__,
    )
    self.receptor_path = os.path.join(self.data_path, "pdb_files")

    assert os.path.exists(data_path), f"Data path {data_path} does not exist."
    assert os.path.exists(self.receptor_path), (
        f"Receptor path {self.receptor_path} does not exist."
    )
    assert os.path.exists(os.path.join(data_path, "pockets_info.json")), (
        f"Pockets info file does not exist in {data_path}."
    )

    with open(os.path.join(data_path, "pockets_info.json")) as f:
        self.pockets: Dict[str, Dict[str, Any]] = json.load(f)
    self.cmd = "mk_prepare_receptor.py -i {INPUT} -o {OUTPUT} -p -g --box_size {BOX_SIZE} --box_center {BOX_CENTER}"

meeko_process(receptor, allow_bad_res=False)

Process a receptor with Meeko, handling unrecognized residue errors.

This method implements smart error handling for Meeko failures:

  1. First attempts processing without --allow_bad_res
  2. If unrecognized residues are found, checks if they're inside the docking box
  3. If outside the box: retries with --allow_bad_res (returns 0)
  4. If inside the box: retries with --allow_bad_res but flags as warning (returns 1)

Parameters:

Name Type Description Default
receptor str

Receptor identifier matching a key in pockets_info.json and a file {receptor}.pdb in the pdb_files directory.

required
allow_bad_res bool

If True, always use --allow_bad_res flag. Default: False.

False

Returns:

Type Description
Tuple[int, str]

Tuple[int, str]: A tuple containing:

  • status code:
    • 0: Success (no issues or issues outside docking box)
    • 1: Warning (unrecognized residues inside docking box)
  • output_path: Path to the processed output files (without extension)

Raises:

Type Description
ValueError

If Meeko fails for reasons other than unrecognized residues.

Source code in mol_gen_docking/data/meeko_process.py
def meeko_process(
    self, receptor: str, allow_bad_res: bool = False
) -> Tuple[int, str]:
    """Process a receptor with Meeko, handling unrecognized residue errors.

    This method implements smart error handling for Meeko failures:

    1. First attempts processing without `--allow_bad_res`
    2. If unrecognized residues are found, checks if they're inside the docking box
    3. If outside the box: retries with `--allow_bad_res` (returns 0)
    4. If inside the box: retries with `--allow_bad_res` but flags as warning (returns 1)

    Args:
        receptor (str): Receptor identifier matching a key in `pockets_info.json`
            and a file `{receptor}.pdb` in the pdb_files directory.
        allow_bad_res (bool): If True, always use `--allow_bad_res` flag.
            Default: False.

    Returns:
        Tuple[int, str]: A tuple containing:

            - status code:
                - 0: Success (no issues or issues outside docking box)
                - 1: Warning (unrecognized residues inside docking box)
            - output_path: Path to the processed output files (without extension)

    Raises:
        ValueError: If Meeko fails for reasons other than unrecognized residues.
    """
    input_path = os.path.join(self.receptor_path, f"{receptor}.pdb")
    stderr_text, returncode, output_path = self._run_meeko(
        input_path, receptor, allow_bad_res
    )
    if returncode != 0:
        # Check if failing resiudes are inside the docking box
        failed_residues = set()
        for line in stderr_text.splitlines():
            match = re.search(
                r"No template matched for residue_key='(\w+:\d+)'", line
            )
            if match:
                failed_residues.add(match.group(1))

        if failed_residues:
            residues_in_box = check_failed_residues_in_box(
                input_path,
                failed_residues,
                self.pockets[receptor]["center"],
                self.pockets[receptor]["size"],
            )
            if residues_in_box == []:
                _, returncode, output_path = self._run_meeko(
                    input_path, receptor, bad_res=True
                )
                return returncode, output_path
            else:
                _, returncode, output_path = self._run_meeko(
                    input_path, receptor, bad_res=True
                )
                return 1, output_path
        else:
            raise ValueError(
                f"Error in Meeko processing {receptor}:\n{stderr_text}"
            )
    return 0, output_path

process_receptors(receptors=[], allow_bad_res=False, use_pbar=False)

Process multiple receptors in parallel using Ray.

Orchestrates the full processing pipeline for a batch of receptors:

  1. Filters out already-processed receptors (with existing _ag.pdbqt and _ag.maps.fld files)
  2. Optionally removes heteroatoms (if pre_process_receptors=True)
  3. Runs Meeko processing with error recovery
  4. Runs AutoGrid4 for successful conversions
  5. Collects and categorizes failures

Processing is parallelized using Ray remote functions, with each receptor allocated 4 CPU cores.

Parameters:

Name Type Description Default
receptors list[str]

List of receptor identifiers to process. If empty, processes all receptors in pockets_info.json. Default: [].

[]
allow_bad_res bool

If True, always use --allow_bad_res flag in Meeko. Default: False.

False
use_pbar bool

If True, display a tqdm progress bar via Ray. Default: False.

False

Returns:

Type Description
Tuple[list[str], list[str]]

Tuple[list[str], list[str]]: Two lists of receptor identifiers:

  • warnings: Receptors with non-critical issues (unrecognized residues inside docking box, but processing completed)
  • errors: Receptors with critical failures (exceptions during processing)

Raises:

Type Description
AssertionError

If any receptor in the list is not found in pockets_info.json.

Note

Ray must be initialized before calling this method.

Source code in mol_gen_docking/data/meeko_process.py
def process_receptors(
    self,
    receptors: list[str] = [],
    allow_bad_res: bool = False,
    use_pbar: bool = False,
) -> Tuple[list[str], list[str]]:
    """Process multiple receptors in parallel using Ray.

    Orchestrates the full processing pipeline for a batch of receptors:

    1. Filters out already-processed receptors (with existing `_ag.pdbqt`
       and `_ag.maps.fld` files)
    2. Optionally removes heteroatoms (if `pre_process_receptors=True`)
    3. Runs Meeko processing with error recovery
    4. Runs AutoGrid4 for successful conversions
    5. Collects and categorizes failures

    Processing is parallelized using Ray remote functions, with each receptor
    allocated 4 CPU cores.

    Args:
        receptors (list[str]): List of receptor identifiers to process.
            If empty, processes all receptors in `pockets_info.json`.
            Default: [].
        allow_bad_res (bool): If True, always use `--allow_bad_res` flag
            in Meeko. Default: False.
        use_pbar (bool): If True, display a tqdm progress bar via Ray.
            Default: False.

    Returns:
        Tuple[list[str], list[str]]: Two lists of receptor identifiers:

            - warnings: Receptors with non-critical issues (unrecognized
              residues inside docking box, but processing completed)
            - errors: Receptors with critical failures (exceptions during
              processing)

    Raises:
        AssertionError: If any receptor in the list is not found in
            `pockets_info.json`.

    Note:
        Ray must be initialized before calling this method.
    """

    @ray.remote(num_cpus=4)
    def process_receptor(
        receptor: str,
        pbar: Any,
        allow_bad_res: bool = False,
    ) -> int:
        """
        Outputs the level of the error:
        0: Success
        1: Failed residues inside the box
        2: Other errors (critical)
        """
        try:
            if self.pre_process_receptors:
                self.remove_heterogenous(receptor)
            result, processed_path = self.meeko_process(receptor, allow_bad_res)
            if result <= 1:
                self._run_autogrid(processed_path)
            if pbar is not None:
                pbar.update.remote(1)
        except Exception as e:
            self.logger.error(f"Error processing {receptor}: {e}")
            if pbar is not None:
                pbar.update.remote(1)
            return 2
        return result

    if receptors == []:
        receptors = list(self.pockets.keys())
    else:
        assert all(r in self.pockets for r in receptors), (
            "Some receptors are not in pockets_info.json: "
            + ",".join([r for r in receptors if r not in self.pockets])
        )

    remote_tqdm = ray.remote(tqdm_ray.tqdm)
    if use_pbar:
        pbar = remote_tqdm.remote(total=len(receptors), desc="Processing receptors")
    else:
        pbar = None
    # Find receptors that already have a _ag.pdbqt and_ag.maps.fld file
    receptors_to_process = []
    for receptor in receptors:
        ag_pdbqt_path = os.path.join(self.receptor_path, f"{receptor}_ag.pdbqt")
        ag_maps_fld_path = os.path.join(
            self.receptor_path, f"{receptor}_ag.maps.fld"
        )
        if not (os.path.exists(ag_pdbqt_path) and os.path.exists(ag_maps_fld_path)):
            receptors_to_process.append(receptor)
        else:
            self.logger.info(f"Receptor {receptor} already processed. Skipping.")
            if use_pbar:
                pbar.update.remote(1)  # type: ignore
    if len(receptors_to_process) == 0:
        self.logger.info("All receptors already processed.")
        if use_pbar:
            pbar.close.remote()  # type: ignore
        return [], []

    self.logger.info(f"Processing {len(receptors_to_process)} receptors.")
    futures = [
        process_receptor.remote(receptor, pbar, allow_bad_res)
        for receptor in receptors_to_process
    ]
    results = ray.get(futures)

    missed_receptors_1 = [
        receptor
        for receptor, success in zip(receptors_to_process, results)
        if success == 1
    ]
    missed_receptors_2 = [
        receptor
        for receptor, success in zip(receptors_to_process, results)
        if success == 2
    ]
    return missed_receptors_1, missed_receptors_2

remove_heterogenous(receptor)

Remove heteroatoms from a receptor PDB file using PDBFixer.

Removes all heteroatoms including ligands, water molecules, and ions from the receptor structure. The cleaned structure overwrites the original PDB file.

Parameters:

Name Type Description Default
receptor str

Receptor identifier. The file {receptor_path}/{receptor}.pdb will be modified in-place.

required
Note

Requires OpenMM and PDBFixer packages.

Warning

This method modifies the original PDB file. Make a backup if needed.

Source code in mol_gen_docking/data/meeko_process.py
def remove_heterogenous(self, receptor: str) -> None:
    """Remove heteroatoms from a receptor PDB file using PDBFixer.

    Removes all heteroatoms including ligands, water molecules, and ions
    from the receptor structure. The cleaned structure overwrites the
    original PDB file.

    Args:
        receptor (str): Receptor identifier. The file
            `{receptor_path}/{receptor}.pdb` will be modified in-place.

    Note:
        Requires OpenMM and PDBFixer packages.

    Warning:
        This method modifies the original PDB file. Make a backup if needed.
    """
    from openmm.app import PDBFile
    from pdbfixer import PDBFixer

    pdb_file = os.path.join(self.receptor_path, f"{receptor}.pdb")

    fixer = PDBFixer(filename=pdb_file)
    fixer.removeHeterogens(True)

    PDBFile.writeFile(fixer.topology, fixer.positions, open(pdb_file, "w"))