File size: 2,858 Bytes
c289d87 | 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 | from __future__ import annotations
import argparse
import csv
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_SOURCE_CSV = ROOT / "data" / "ligands" / "prelim_set_mdm2_4hg7" / "shared_library_dedup.csv"
DEFAULT_BUNDLED_SMI = ROOT / "data" / "examples" / "example_smiles_1000.smi"
def _write_rows(rows: list[tuple[str, str]], out: Path) -> Path:
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text("\n".join(f"{smiles} {ligand_id}" for smiles, ligand_id in rows) + "\n", encoding="utf-8")
return out
def _read_smi(path: Path, n: int) -> list[tuple[str, str]]:
rows: list[tuple[str, str]] = []
for idx, line in enumerate(path.read_text(encoding="utf-8").splitlines()):
text = line.strip()
if not text or text.startswith("#"):
continue
parts = text.split()
smiles = parts[0]
ligand_id = parts[1] if len(parts) > 1 else f"lig_{idx:05d}"
rows.append((smiles, ligand_id))
if len(rows) >= n:
break
return rows
def _read_csv_rows(path: Path, n: int) -> list[tuple[str, str]]:
rows: list[tuple[str, str]] = []
with path.open("r", encoding="utf-8", newline="") as handle:
reader = csv.DictReader(handle)
for idx, item in enumerate(reader):
smiles = str(item.get("smiles", "")).strip()
ligand_id = str(item.get("ligand_id", "")).strip() or f"lig_{idx:05d}"
if not smiles:
continue
rows.append((smiles, ligand_id))
if len(rows) >= n:
break
return rows
def main() -> int:
parser = argparse.ArgumentParser(description="Prepare a real 1000-SMILES example file from the public benchmark library in this repo.")
parser.add_argument("--n", type=int, default=1000)
parser.add_argument("--out", default=str(DEFAULT_BUNDLED_SMI))
args = parser.parse_args()
out = Path(args.out)
if DEFAULT_SOURCE_CSV.exists():
rows = _read_csv_rows(DEFAULT_SOURCE_CSV, args.n)
if len(rows) < args.n:
raise SystemExit(
f"Requested {args.n} SMILES but {DEFAULT_SOURCE_CSV} yielded only {len(rows)} rows."
)
_write_rows(rows, out)
print(out)
return 0
if DEFAULT_BUNDLED_SMI.exists():
rows = _read_smi(DEFAULT_BUNDLED_SMI, args.n)
if len(rows) < args.n:
raise SystemExit(
f"Requested {args.n} SMILES but bundled file {DEFAULT_BUNDLED_SMI} contains only {len(rows)} rows."
)
_write_rows(rows, out)
print(out)
return 0
raise SystemExit(
"No real example 1000-SMILES source is available. Expected either "
f"{DEFAULT_SOURCE_CSV} or {DEFAULT_BUNDLED_SMI}."
)
if __name__ == "__main__":
raise SystemExit(main())
|