Docking_project / portable_rdock_pipeline /scripts /download_example_smiles.py
QPromaQ's picture
Reset repository and upload final project (part 30)
504d922 verified
Raw
History Blame Contribute Delete
2.86 kB
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())