Skip to content

MolToSmiles fails with rootedAtAtom for disconnected molecules (need for smiles augmentation) #44

Description

@vidvath7

rdkit MolToSmiles() gives RuntimeError for some molecules (Eg: "[Cl-].[H]N+([H])[H]" ), with configuration of rootedAtAtom>0.

Screenshot 2024-08-18 111436

Screenshot 2024-08-18 111404

Activity

  1. aditya0by0 commented on Aug 10, 2025

    @aditya0by0
    Member

    I'm facing the same error for issue #113 in PR #115 , and the below might the reason

    Description:
    When performing SMILES augmentation using RDKit’s Chem.MolToSmiles with the rootedAtAtom argument, the function raises a RuntimeError: Range Error idx for molecules containing disconnected fragments (e.g., [F-].[Na+]).

    Steps to Reproduce:

    from rdkit import Chem
    
    mol = Chem.MolFromSmiles("[F-].[Na+]")
    Chem.MolToSmiles(mol, rootedAtAtom=1, doRandom=True)

    Observed Behavior:

    Exception has occurred: RuntimeError
    Range Error
    	idx
    	Violation occurred on line 209 in file Code\GraphMol\ROMol.cpp
    	Failed Expression: 1 < 1
    	RDKIT: 2024.09.6
    	BOOST: 1_85
    

    Cause:
    RDKit’s rootedAtAtom assumes the atom index belongs to a single connected component. For disconnected molecules (multiple fragments), this internal assumption fails and results in an out-of-bounds fragment index.

    To elaborate, MolToSmiles’ rootedAtAtom parameter expects the index of atom in a connected fragment — but the molecule [F-].[Na+] is two disconnected fragments. When you pass rootedAtAtom=1 (sodium atom), RDKit internally tries to traverse a single fragment, but the fragment index calculation fails due to it being a disconnected molecule.

    @sfluegel05, Please let me know if you have any suggestions for augmenting this type of smiles which are present in our processed dataset.

  2. changed the title [-]MolToSmiles() gives RuntimeError for some molecules[/-] [+]MolToSmiles fails with rootedAtAtom for disconnected molecules (need for smiles augmentation)[/+] on Aug 10, 2025
  3. changed the title [-]MolToSmiles fails with rootedAtAtom for disconnected molecules (need for smiles augmentation)[/-] [+]`MolToSmiles` fails with `rootedAtAtom` for disconnected molecules (need for smiles augmentation)[/+] on Aug 10, 2025
  4. sfluegel05 commented on Aug 11, 2025

    @sfluegel05
    Collaborator

    you are right. For some reason the rootedAtAtom argument does not accept the atom id, but only takes the first fragment into account. I guess we should treat each fragment separately as well and then recombine them afterwards

    mol = Chem.MolFromSmiles("[F-].[Na+]")
    fragments = Chem.GetMolFrags(mol, asMols=True)
    for i, fragment in enumerate(fragments):
        print(f"Fragment {i}: {Chem.MolToSmiles(fragment)}")
        # randomize SMILES for this fragment
    # stitch together fragment-SMILES (in random order)
    
  5. linked a pull request that will close this issueData Augmentation : SMILES #115on Aug 22, 2025
  6. self-assigned this
    on Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions