Skip to content

Commit

Permalink
fix: fix SMILES converting bug (resolve #1056) (#1066)
Browse files Browse the repository at this point in the history
* fix: fix SMILES converting bug (resolve #1056)

Co-authored-by: Hesy <[email protected]>

* add doctests

Co-authored-by: Hesy <[email protected]>
  • Loading branch information
njzjz and hexi519 authored Mar 25, 2021
1 parent ffdb443 commit 52c0d4a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions reacnetgenerator/_reachtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@ def report(self):
f"Report is generated. Please see {self.resultfilename} for more details.")

def _re(self, smi):
for an in self.atomname:
if an != 'H':
smi = smi.replace(an.upper(), f"[{an.upper()}]").replace(
an.lower(), f"[{an.lower()}]")
"""If you use RDkit to convert a methyl radical to SMILES, you will get something
like [H]C([H])[H]. However, OpenBabel will consider it as a methane molecule. So,
you have to use [H][C]([H])[H], if you need to process some radicals.
Examples:
>>> self._re('C')
[C]
>>> self._re('[C]')
[C]
>>> self._re('[CH]')
[CH]
"""
elements = "".join([an.upper() + an.lower() for an in self.atomname if an != 'H'])
smi = re.sub(r'(?<!\[)([' + elements + r'])(?!H)', r'[\1]', smi)
return smi.replace("[HH]", "[H]")

def _handlereaction(self, line):
Expand Down

1 comment on commit 52c0d4a

@vercel
Copy link

@vercel vercel bot commented on 52c0d4a Mar 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.