File size: 782 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
from __future__ import annotations

from pathlib import Path

import pandas as pd


REQUIRED_COLUMNS = {"ligand_id", "smiles"}


def read_smiles_table(path: str | Path) -> pd.DataFrame:
    """Read ligand table with required columns ligand_id and smiles."""
    source = Path(path)
    if source.suffix.lower() in {".tsv", ".txt"}:
        df = pd.read_csv(source, sep="\t")
    else:
        df = pd.read_csv(source)
    missing = REQUIRED_COLUMNS - set(df.columns)
    if missing:
        raise ValueError(f"Missing required columns in {source}: {sorted(missing)}")
    return df


def write_smiles_table(df: pd.DataFrame, path: str | Path) -> Path:
    target = Path(path)
    target.parent.mkdir(parents=True, exist_ok=True)
    df.to_csv(target, index=False)
    return target