Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use rdkit API to remove implicit hydrogens #2278

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 5 additions & 34 deletions reacnetgenerator/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"""

import itertools
import re
from abc import ABCMeta, abstractmethod
from collections import Counter, defaultdict

Expand Down Expand Up @@ -218,38 +217,6 @@ def _printatomroute(self, atomeach, timeaxis=None):
allmoleculeroute = np.unique(allmoleculeroute, axis=0)
return allmoleculeroute

def _re(self, smi):
"""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]
>>> self._re('Na')
[Na]
>>> self._re('[H]c(Cl)C([H])Cl')
[H][c]([Cl])[C]([H])[Cl]
"""
if "_unknownSMILES" in smi:
# not SMILES
return smi
Satom = sorted(self.atomname, key=len, reverse=True)
elements = "|".join(
[
((an.upper() + "|" + an.lower()) if len(an) == 1 else an)
for an in Satom
if an != "H"
]
)
smi = re.sub(r"(?<!\[)(" + elements + r")(?!H)", r"[\1]", smi)
return smi.replace("[HH]", "[H]")

def convertSMILES(self, atoms, bonds):
"""Convert atoms and bonds information to SMILES.

Expand All @@ -264,8 +231,12 @@ def convertSMILES(self, atoms, bonds):
d[number] = m.AddAtom(Chem.Atom(name))
for atom1, atom2, level in bonds:
m.AddBond(d[atom1], d[atom2], Chem.BondType(level))
# https://github.com/rdkit/rdkit/discussions/6613#discussioncomment-6688021
for a in m.GetAtoms(): # type:ignore
a.SetNoImplicit(True)
a.UpdatePropertyCache()
name = Chem.MolToSmiles(m)
return self._re(name)
return name

def _getatomsandbonds(self, line):
atoms = np.array(bytestolist(line[0]), dtype=int)
Expand Down
12 changes: 0 additions & 12 deletions tests/test_reacnetgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from reacnetgenerator import ReacNetGenerator
from reacnetgenerator._detect import _Detect
from reacnetgenerator._hmmfilter import _HMMFilter
from reacnetgenerator._path import _CollectSMILESPaths
from reacnetgenerator.commandline import parm2cmd
from reacnetgenerator.gui import GUI
from reacnetgenerator.utils import checksha256, download_multifiles, listtobytes
Expand Down Expand Up @@ -147,14 +146,3 @@ def test_benchmark_hmm(self, benchmark, reacnetgen_param):
@benchmark
def bench():
hmmclass._getoriginandhmm(compressed_bytes)

def test_re(self, reacnetgen_param):
"""Test regular expression of _HTMLResult."""
reacnetgen = ReacNetGenerator(**reacnetgen_param["rngparams"])
r = _CollectSMILESPaths(reacnetgen)
r.atomname = ["C", "H", "O", "Na", "Cl"]
assert r._re("C"), "[C]"
assert r._re("[C]"), "[C]"
assert r._re("[CH]"), "[CH]"
assert r._re("Na"), "[Na]"
assert r._re("[H]c(Cl)C([H])Cl"), "[H][c]([Cl])[C]([H])[Cl]"
Loading