Skip to main content

    File Formats & Conversion

    Convert between SMILES, SDF, and PDB formats with RDKit for cheminformatics workflows.

    Format conversion lets you move molecules between SMILES, SDF, and PDB so each stage of a workflow gets the representation it expects. Careful parsing — checking for None, sanitizing, and preserving provenance — is what most affects whether a batch runs clean or silently drops records.

    Parse molecules

    from rdkit import Chem
    
    mol = Chem.MolFromSmiles("CC(=O)Oc1ccccc1C(=O)O")
    if mol is None:
        raise ValueError("Invalid SMILES")
    
    mol = Chem.AddHs(mol)
    Chem.SanitizeMol(mol)

    SDF to SMILES

    from rdkit.Chem import SDMolSupplier
    
    supplier = SDMolSupplier("input.sdf", removeHs=False)
    smiles: list[str] = []
    for mol in supplier:
        if mol is None:
            continue
        smiles.append(Chem.MolToSmiles(Chem.RemoveHs(mol)))

    SMILES to SDF

    from rdkit.Chem import SDWriter
    
    writer = SDWriter("/workspace/outputs/library.sdf")
    for s in smiles:
        mol = Chem.MolFromSmiles(s)
        if mol is None:
            continue
        Chem.SanitizeMol(mol)
        writer.write(Chem.AddHs(mol))
    writer.close()

    PDB hygiene

    • Use Chem.MolFromPDBFile("structure.pdb", removeHs=False, sanitize=True).
    • Strip waters/ions before minimization if they are not needed.
    • Ensure residue/atom names are preserved before exporting back to PDB.

    Validation tips

    • Always check for None after parsing; skip invalid records rather than crashing the batch.
    • Normalize tautomers if your downstream model expects a canonical form.
    • Record provenance: store the source file path in mol.SetProp("source", path).

    Was this page helpful?

    On this page

    On this page

    Loading...