Skill Tracer
Skills › Scientific Agent Skills › datamol › core_api.md

core_api.md

81 lines · skills/datamol/references/core_api.md at 5f2bc03

Open on GitHub

Most marks come from matching words in the file, so treat them as pointers.

1# Datamol core API (0.13.0)
2
3The [current API](https://docs.datamol.io/stable/api/datamol.mol.html) and installed
40.13.0 source were checked. These are call patterns, not exhaustive signatures.
5

Datamol core API (0.13.0)

The current API and installed 0.13.0 source were checked. These are call patterns, not exhaustive signatures.

Raw lines 1 to 5
6## Conversion and identity
7
8- `dm.to_mol(smiles)` accepts SMILES/CXSMILES, an existing Mol, or RDKit binary bytes.
9 It does not auto-detect InChI or arbitrary formats. Parsing failures can return `None`;
10 unsupported argument types can raise. Check the result before every downstream step.
11- `dm.from_inchi(inchi)` and `dm.from_smarts(smarts)` return Mol or `None`.
12- `dm.from_selfies(text)` returns a SMILES string by default; use `as_mol=True` for Mol.
13- `dm.copy_mol(mol)` creates a copy. `dm.to_binary(mol)` handles one molecule; `dm.to_dict([mol])` and
14 `dm.from_dict(payload)` serialize/restore a sequence of molecules through RDKit JSON. They support interchange; record RDKit version with persisted data.
15- `dm.to_smiles(mol, canonical=True, isomeric=True)` retains specified stereochemistry.
16 `dm.to_inchi`, `dm.to_inchikey`, `dm.to_smarts`, and `dm.to_selfies` are separate exports.
17- `dm.hash_mol(mol, hash_scheme="all")` supports `all`, `no_stereo`, and `no_tautomers`.
18 Choose the identity policy deliberately; collapsing stereoisomers or tautomers can
19 merge biologically distinct records.
20- `dm.reorder_atoms(mol)` changes atom indices. Store mappings before combining reordered
21 structures with atom-indexed labels. `dm.add_hs`/`dm.remove_hs` also change atom topology.

Conversion and identity

  • dm.to_mol(smiles) accepts SMILES/CXSMILES, an existing Mol, or RDKit binary bytes. It does not auto-detect InChI or arbitrary formats. Parsing failures can return None; unsupported argument types can raise. Check the result before every downstream step.
  • dm.from_inchi(inchi) and dm.from_smarts(smarts) return Mol or None.
  • dm.from_selfies(text) returns a SMILES string by default; use as_mol=True for Mol.
  • dm.copy_mol(mol) creates a copy. dm.to_binary(mol) handles one molecule; dm.to_dict([mol]) and dm.from_dict(payload) serialize/restore a sequence of molecules through RDKit JSON. They support interchange; record RDKit version with persisted data.
  • dm.to_smiles(mol, canonical=True, isomeric=True) retains specified stereochemistry. dm.to_inchi, dm.to_inchikey, dm.to_smarts, and dm.to_selfies are separate exports.
  • dm.hash_mol(mol, hash_scheme="all") supports all, no_stereo, and no_tautomers. Choose the identity policy deliberately; collapsing stereoisomers or tautomers can merge biologically distinct records.
  • dm.reorder_atoms(mol) changes atom indices. Store mappings before combining reordered structures with atom-indexed labels. dm.add_hs/dm.remove_hs also change atom topology.
Raw lines 6 to 22
23## Sanitization and standardization
25`dm.sanitize_mol(mol, charge_neutral=False)` attempts a sanitization round trip and can
26return `None`. `dm.fix_mol`, `dm.fix_valence`, and `dm.fix_valence_charge` change structures
27heuristically; a successful return does not establish that the original chemistry was recovered.
29`dm.standardize_mol(mol, disconnect_metals=False, normalize=True, reionize=True,
30uncharge=False, stereo=True)` returns a standardized molecule. It does not select the
31largest fragment. Preserve source SMILES and identifiers; explicitly decide salt, charge,
32metal, isotope and stereochemistry policy. `dm.standardize_smiles(smiles)` accepts no
33policy keyword arguments in 0.13.0; use the molecule API for explicit choices.

Sanitization and standardization

dm.sanitize_mol(mol, charge_neutral=False) attempts a sanitization round trip and can return None. dm.fix_mol, dm.fix_valence, and dm.fix_valence_charge change structures heuristically; a successful return does not establish that the original chemistry was recovered.

dm.standardize_mol(mol, disconnect_metals=False, normalize=True, reionize=True, uncharge=False, stereo=True) returns a standardized molecule. It does not select the largest fragment. Preserve source SMILES and identifiers; explicitly decide salt, charge, metal, isotope and stereochemistry policy. dm.standardize_smiles(smiles) accepts no policy keyword arguments in 0.13.0; use the molecule API for explicit choices.

Raw lines 23 to 34
35## Fingerprints and distances
37```python
38import datamol as dm
39import numpy as np
40mols = [dm.to_mol(s) for s in ["CCO", "CCCO", "c1ccccc1"]]
41fp = dm.to_fp(mols[0], fp_type="ecfp", radius=2, fpSize=2048)
42assert fp.shape == (2048,)
43distances = dm.pdist(mols, radius=2, fpSize=2048) # square N x N by default
44condensed = dm.pdist(mols, squareform=False, radius=2, fpSize=2048)
45cross = dm.cdist(mols[:1], mols, radius=2, fpSize=2048) # 1 x N
46assert distances.shape == (3, 3) and condensed.shape == (3,)
47assert np.allclose(np.diag(distances), 0)
48```
50ECFP defaults to radius 3 (ECFP6); FCFP defaults to radius 2. Generator fingerprints
51use `fpSize`, not `n_bits`. Use `includeChirality=True` if stereochemistry should affect
52similarity, and use identical options for every query/library operation. `as_array=False`
53returns the RDKit fingerprint. `topological` means topological torsions; `rdkit` is the
54RDKit path fingerprint. MACCS has its own fixed size (167); it does not accept `fpSize`.
55Count variants include `ecfp-count`, `fcfp-count`, `atompair-count`, `topological-count`,
56and `rdkit-count`. The similarity helpers compute binary Jaccard/Tanimoto distances;
57do not use them to imply a count-vector Tanimoto metric. Lower distance means higher
58similarity, with similarity = 1 - distance for the binary fingerprints used here.

Fingerprints and distances

import datamol as dm
import numpy as np
mols = [dm.to_mol(s) for s in ["CCO", "CCCO", "c1ccccc1"]]
fp = dm.to_fp(mols[0], fp_type="ecfp", radius=2, fpSize=2048)
assert fp.shape == (2048,)
distances = dm.pdist(mols, radius=2, fpSize=2048)  # square N x N by default
condensed = dm.pdist(mols, squareform=False, radius=2, fpSize=2048)
cross = dm.cdist(mols[:1], mols, radius=2, fpSize=2048)  # 1 x N
assert distances.shape == (3, 3) and condensed.shape == (3,)
assert np.allclose(np.diag(distances), 0)

ECFP defaults to radius 3 (ECFP6); FCFP defaults to radius 2. Generator fingerprints use fpSize, not n_bits. Use includeChirality=True if stereochemistry should affect similarity, and use identical options for every query/library operation. as_array=False returns the RDKit fingerprint. topological means topological torsions; rdkit is the RDKit path fingerprint. MACCS has its own fixed size (167); it does not accept fpSize. Count variants include ecfp-count, fcfp-count, atompair-count, topological-count, and rdkit-count. The similarity helpers compute binary Jaccard/Tanimoto distances; do not use them to imply a count-vector Tanimoto metric. Lower distance means higher similarity, with similarity = 1 - distance for the binary fingerprints used here.

Raw lines 35 to 59
Names a skill rdkit :53
60## Clusters and selections
62```python
63cluster_indices, cluster_molecules = dm.cluster_mols(mols, cutoff=0.2)
64picked_indices, picked_molecules = dm.pick_diverse(mols, npick=min(2, len(mols)), seed=42)
65centroid_indices, centroid_molecules = dm.pick_centroids(mols, npick=2, method="sphere")
66assert len(picked_indices) == len(picked_molecules) == 2
67```
69All three return two components. Cluster indices are groups of source positions; selection
70indices address the original list. Retain these indices to preserve labels/IDs. `pick_centroids`
71is a distance-based representative selector; the default sphere method may return fewer
72than `npick` due to its threshold. It is not a geometric mean. Butina stores a quadratic
73pairwise-distance list; no universal safe molecule count applies. Bound resources before
74clustering. `pick_diverse` avoids storing the full pairwise matrix but is still work-dependent.

Clusters and selections

cluster_indices, cluster_molecules = dm.cluster_mols(mols, cutoff=0.2)
picked_indices, picked_molecules = dm.pick_diverse(mols, npick=min(2, len(mols)), seed=42)
centroid_indices, centroid_molecules = dm.pick_centroids(mols, npick=2, method="sphere")
assert len(picked_indices) == len(picked_molecules) == 2

All three return two components. Cluster indices are groups of source positions; selection indices address the original list. Retain these indices to preserve labels/IDs. pick_centroids is a distance-based representative selector; the default sphere method may return fewer than npick due to its threshold. It is not a geometric mean. Butina stores a quadratic pairwise-distance list; no universal safe molecule count applies. Bound resources before clustering. pick_diverse avoids storing the full pairwise matrix but is still work-dependent.

Raw lines 60 to 75
76## Graphs and tables
78`dm.to_graph(mol)` creates a NetworkX graph; `dm.get_all_path_between(mol, atom1, atom2)`
79can grow combinatorially on ring-rich graphs. `dm.to_df(mols, mol_column="mol")` includes
80molecules explicitly (the default is `None`). `dm.from_df(df, mol_column="mol")` returns
81one molecule per row, attaching other columns as molecule properties; handle invalid rows.

Graphs and tables

dm.to_graph(mol) creates a NetworkX graph; dm.get_all_path_between(mol, atom1, atom2) can grow combinatorially on ring-rich graphs. dm.to_df(mols, mol_column="mol") includes molecules explicitly (the default is None). dm.from_df(df, mol_column="mol") returns one molecule per row, attaching other columns as molecule properties; handle invalid rows.

Raw lines 76 to 81