forked from NVIDIA-BioNeMo/megalodon
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprocess_sdf.py
More file actions
141 lines (118 loc) · 4.64 KB
/
Copy pathprocess_sdf.py
File metadata and controls
141 lines (118 loc) · 4.64 KB
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
#!/usr/bin/env python3
"""Convert a 3D SDF file to the standard LoQI training dataset format."""
from __future__ import annotations
import argparse
from pathlib import Path
import numpy as np
from rdkit import Chem
from tqdm import tqdm
from process_chembl3d import (
_convert_record,
expected_output_paths,
save_graph_splits,
split_conformers,
)
def read_sdf_graphs(
sdf_path: Path,
limit_molecules: int | None = None,
) -> tuple[list, int]:
"""Read one 3D conformer per SDF record and convert it to a LoQI graph."""
graphs = []
failed_conversions = 0
supplier = Chem.SDMolSupplier(str(sdf_path), removeHs=False, sanitize=False)
records = enumerate(tqdm(supplier, desc="Converting SDF records"))
for record_index, molecule in records:
if molecule is None:
failed_conversions += 1
print(f"Warning: failed to read SDF record {record_index}")
continue
try:
if molecule.GetNumConformers() != 1:
raise ValueError(
f"expected exactly one conformer, found {molecule.GetNumConformers()}"
)
conformer = molecule.GetConformer()
if not conformer.Is3D():
raise ValueError("conformer is not marked as 3D")
coordinates = np.asarray(conformer.GetPositions())
if not np.isfinite(coordinates).all():
raise ValueError("conformer contains non-finite coordinates")
mol_id = (
molecule.GetProp("_Name").strip()
if molecule.HasProp("_Name") and molecule.GetProp("_Name").strip()
else f"sdf_{record_index}"
)
graphs.append(_convert_record(molecule, coordinates, mol_id))
except Exception as exc:
failed_conversions += 1
print(f"Warning: failed to process SDF record {record_index}: {exc}")
if limit_molecules is not None and len(graphs) >= limit_molecules:
break
return graphs, failed_conversions
def process(args: argparse.Namespace) -> None:
sdf_path = Path(args.sdf_path).expanduser().resolve()
if not sdf_path.is_file():
raise FileNotFoundError(f"SDF file not found: {sdf_path}")
if args.limit_molecules < 0:
raise ValueError("limit_molecules cannot be negative")
limit_molecules = args.limit_molecules or None
if args.test_mode and limit_molecules is None:
limit_molecules = 30
processed_path = Path(args.save_data_folder).expanduser().resolve() / "processed"
existing = [path for path in expected_output_paths(processed_path) if path.exists()]
if existing and not args.overwrite:
raise FileExistsError(
f"Refusing to overwrite {len(existing)} existing output files; pass --overwrite"
)
processed_path.mkdir(parents=True, exist_ok=True)
graphs, failed = read_sdf_graphs(sdf_path, limit_molecules)
print(f"Converted {len(graphs)} SDF records")
splits = split_conformers(
graphs,
train_ratio=args.train_ratio,
val_ratio=args.val_ratio,
seed=args.seed,
)
split_sizes = ", ".join(f"{key}={len(value)}" for key, value in splits.items())
print(f"Split sizes: {split_sizes}")
save_graph_splits(splits, processed_path)
print(f"Completed: output={processed_path}, failed_conversions={failed}")
def setup_argument_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Convert a 3D SDF file to standard LoQI training artifacts",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--sdf_path",
required=True,
help="Input SDF with one 3D conformer per record",
)
parser.add_argument(
"--save_data_folder",
required=True,
help="Output dataset root; standard artifacts are written under processed/",
)
parser.add_argument(
"--limit_molecules",
type=int,
default=0,
help="Optional successful-record limit (0 means unlimited)",
)
parser.add_argument("--train_ratio", type=float, default=0.8)
parser.add_argument("--val_ratio", type=float, default=0.1)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument(
"--test_mode",
action="store_true",
help="Process at most 30 valid SDF records",
)
parser.add_argument(
"--overwrite",
action="store_true",
help="Allow replacement of standard output files in the destination",
)
return parser
def main() -> None:
process(setup_argument_parser().parse_args())
if __name__ == "__main__":
main()