OpenFreeEnergy / gufe

grand unified free energy by OpenFE
https://gufe.readthedocs.io
MIT License
28 stars 8 forks source link

Not possible to use `SmallMoleculeComponent.copy_with_replacements(name='...')` #264

Closed dotsdl closed 8 months ago

dotsdl commented 8 months ago

If we have a SmallMoleculeComponent for a ligand, it can sometimes be useful to create an identical SmallMoleculeComponent but with a different name attribute. However, the following gives:

> sm: SmallMoleculeComponent
> sm.copy_with_replacements(name='lol')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[20], line 1
----> 1 sm.copy_with_replacements(name='lol')

File ~/.conda/envs/asapdiscovery-test-2023.12.22/lib/python3.10/site-packages/gufe/tokenization.py:375, in GufeTokenizable.copy_with_replacements(self, **replacements)
    373 dct = self._to_dict()
    374 if invalid := set(replacements) - set(dct):
--> 375     raise TypeError(f"Invalid replacement keys: {invalid}. "
    376                     f"Allowed keys are: {set(dct)}")
    378 dct.update(replacements)
    379 return self._from_dict(dct)

TypeError: Invalid replacement keys: {'name'}. Allowed keys are: {'conformer', 'molprops', 'bonds', 'atoms'}

This appears to be because the dict given by SmallMoleculeComponent._to_dict lacks the name key, as on reconstruction with SmallMoleculeComponent._from_dict, this is pulled out of the underlying RDKit representation.

Ideally, it should be possible to use SmallMoleculeComponent.copy_with_replacements for name changes, as this works just fine with e.g. ProteinComponent.