Skip to content

Instantly share code, notes, and snippets.

@fredrikw
Created January 18, 2018 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrikw/17738a8d3d9da46e8520f0b9affec8ff to your computer and use it in GitHub Desktop.
Save fredrikw/17738a8d3d9da46e8520f0b9affec8ff to your computer and use it in GitHub Desktop.
Small example function for permutating stereochemistry in SMILES strings
import re
import itertools
import pybel
def permuteSMILESStereo(smile):
stereo = re.search(r"(\[[^\]]+@+[^\]]*\]|[/\\])", smile)
if stereo:
left = smile[:stereo.start()]
rights = permuteSMILESStereo(smile[stereo.end():])
if stereo.group() in ['/', '\\']:
mids = ['/', "\\"]
else:
mids = [re.sub('@+', st, stereo.group()) for st in ['@', '@@']]
lefts = [''.join([left, mid]) for mid in mids]
return [''.join(i) for i in itertools.product(lefts, rights)]
else:
return [smile]
if __name__ == '__main__':
print('\n'.join([''.join(set([
pybel.readstring("smi", ps).write("can")
for ps in permuteSMILESStereo(s)
])) for s in ["CC(=O)/C=C/O", "CC[C@](B)(O)C"]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment