Created
January 18, 2018 14:52
-
-
Save fredrikw/17738a8d3d9da46e8520f0b9affec8ff to your computer and use it in GitHub Desktop.
Small example function for permutating stereochemistry in SMILES strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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