Skip to content

fix: add useRandomCoords to embed long chains - #141

Merged
PythonFZ merged 2 commits into
mainfrom
fix/140-long-chain-embedding
Nov 10, 2026
Merged

PythonFZ merged 2 commits into
mainfrom
fix/140-long-chain-embedding

Conversation

@PythonFZ

Copy link
Copy Markdown
Member

Closes #140.

What

smiles2conformers, smiles2atoms and rdkit2ase take useRandomCoords: bool = False, which RDKit receives unchanged. molify smiles2atoms takes --random-coords. The RDKit call and the conformer-count check move into one unexported helper, molify.utils.embed_conformers.

molify.smiles2atoms("C" * 64, useRandomCoords=True)

Why

RDKit starts each embedding from eigenvectors of the distance matrix. For long chains that step fails on every attempt (INITIAL_COORDS in RDKit's failure counts), so retrying doesn't help. Random starting coordinates embed them. RDKit 2026.03.6, 5 seeds, maxAttempts=1000:

chain default useRandomCoords=True
C48 5/5 5/5, 0.31 s
C56 2/5 5/5, 0.44 s
C64 1/5 5/5, 0.98 s
C80–C128 0/5 5/5, 1.0–3.2 s

Why opt-in, not the default or an automatic retry

For ordinary molecules RDKit's default is the better choice. Random starts are 1.5–6× slower for cubane, norbornane, ibuprofen, 18-crown-6 and a C24 ring. They also give higher median MMFF energies before optimisation for flexible rings: 219 vs 195 kcal/mol for 18-crown-6, 83 vs 64 for the C24 ring.

  • Random coordinates by default: would change every seeded structure and slow down small molecules.
  • Automatic retry with random coordinates: would switch method silently, depending on the seed. Large molecules would always pay for the failed first pass, and there would be no way to skip it.
  • Passing all RDKit kwargs through: pruneRmsThresh conflicts with the conformer-count check, the CLI can't express it, and type checking is lost.

Behaviour changes

  • Embedding errors get a hint when random coordinates were not used: rdkit embedded 0 of 1 conformers of 'CCC…'; random starting coordinates often embed large molecules.
  • rdkit2ase raises that ValueError instead of RDKit's ValueError: Bad Conformer Id when embedding fails. It now embeds through EmbedMultipleConfs(numConfs=1, maxAttempts=0), which gives the same coordinates as the previous EmbedMolecule call on the molecules I compared.
  • Without the flag, seeded structures are unchanged.

Not in this PR

The issue also suggests letting rdkit2ase keep a conformer the molecule already has. That changes behaviour in its own way and is left for a separate issue.

Checks

  • uv run pytest: 618 passed, 49 skipped (vesin not installed).
  • uvx prek --all-files: passes.

🤖 Generated with Claude Code

RDKit's default embedding starts from eigenvectors of the distance
matrix, which fails for long chains (n-alkanes from C56 on, depending on
the seed). smiles2conformers, smiles2atoms and rdkit2ase take
useRandomCoords to start from random coordinates instead, and the CLI
takes --random-coords. The default is unchanged, so seeded structures
stay the same; the embedding error now suggests random coordinates.

rdkit2ase checks the embedding and raises the same ValueError instead of
RDKit's "Bad Conformer Id".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Removed note about embedding long n-alkanes from RDKit's default coordinates.
@PythonFZ
PythonFZ merged commit 8fe1929 into main Nov 10, 2026
17 checks passed
@PythonFZ
PythonFZ deleted the fix/140-long-chain-embedding branch November 10, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

smiles2conformers cannot embed long chains (n-alkanes from C56): expose useRandomCoords

1 participant