Skip to content

Commit

Permalink
#1252 SMARTS loader load grouped components as separate molecules (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AliaksandrDziarkach authored Sep 8, 2023
1 parent 43a8b9c commit 62d2126
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
1 change: 1 addition & 0 deletions api/tests/integration/ref/rsmiles/rsmiles_smarts.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SMARTS component-level grouping load ok
17 changes: 17 additions & 0 deletions api/tests/integration/tests/rsmiles/rsmiles_smarts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
import sys

sys.path.append(
os.path.normpath(
os.path.join(os.path.abspath(__file__), "..", "..", "..", "common")
)
)
from env_indigo import *

indigo = Indigo()


rxn1 = indigo.loadReactionSmarts("([#8:1].[#6:2])>>([#8:1].[#6:2])")
assert rxn1.countReactants() == 1
assert rxn1.countProducts() == 1
print("SMARTS component-level grouping load ok")
10 changes: 5 additions & 5 deletions core/indigo-core/graph/src/graph_decomposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int GraphDecomposer::decompose(const Filter* filter, const Filter* edge_filter,
int top = 1, bottom = 0;

queue[0] = vertex_idx;
while (top != bottom)
while (top != bottom) // while queue not empty
{
auto v_bottom_id = queue[bottom];
const Vertex& vertex = _graph.getVertex(v_bottom_id);
Expand All @@ -92,10 +92,10 @@ int GraphDecomposer::decompose(const Filter* filter, const Filter* edge_filter,
if (edge_filter != 0 && !edge_filter->valid(vertex.neiEdge(i)))
continue;

if (_component_ids[other] == -1)
if (_component_ids[other] == -1) // if other unused
{
queue[top++] = other;
_component_ids[other] = -2;
queue[top++] = other; // queue.push(other)
_component_ids[other] = -2; // mark as in_queue
}

if (_component_ids[other] == -2)
Expand Down Expand Up @@ -129,7 +129,7 @@ int GraphDecomposer::decompose(const Filter* filter, const Filter* edge_filter,
}
}
}
bottom++;
bottom++; // queue.pop()
}

n_comp++;
Expand Down
37 changes: 29 additions & 8 deletions core/indigo-core/reaction/src/rsmiles_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ void RSmilesLoader::loadQueryReaction(QueryReaction& rxn)
_loadReaction();
}

static void _createComponenetsExternalNeighbors(QueryMolecule& qmol, std::list<std::unordered_set<int>>& externNeib)
{
std::unordered_map<int, std::unordered_set<int>> componentAtoms;
for (int i = 0; i < qmol.components.size(); ++i)
{
int componentId = qmol.components[i];
if (componentId > 0)
{ // vertice[i] belongs to component #Id
componentAtoms[componentId].insert(i);
}
}
for (auto elem : componentAtoms)
{
auto atoms = elem.second;
if (atoms.size() > 1)
externNeib.emplace_back(atoms);
}
}

void RSmilesLoader::_loadReaction()
{
_brxn->clear();
Expand All @@ -83,6 +102,10 @@ void RSmilesLoader::_loadReaction()
std::unique_ptr<BaseMolecule>& rcnt = mols[0];
std::unique_ptr<BaseMolecule>& ctlt = mols[1];
std::unique_ptr<BaseMolecule>& prod = mols[2];
std::list<std::unordered_set<int>> mols_extNeibs[3];
std::list<std::unordered_set<int>>& rcnt_extNeibs = mols_extNeibs[0];
std::list<std::unordered_set<int>>& ctlt_extNeibs = mols_extNeibs[1];
std::list<std::unordered_set<int>>& prod_extNeibs = mols_extNeibs[2];

QS_DEF(Array<int>, rcnt_aam);
QS_DEF(Array<int>, ctlt_aam);
Expand Down Expand Up @@ -124,6 +147,7 @@ void RSmilesLoader::_loadReaction()
{
rcnt = std::make_unique<QueryMolecule>();
r_loader.loadQueryMolecule(static_cast<QueryMolecule&>(*rcnt));
_createComponenetsExternalNeighbors(static_cast<QueryMolecule&>(*rcnt), rcnt_extNeibs);
}
rcnt_aam.copy(rcnt->reaction_atom_mapping);

Expand All @@ -138,11 +162,6 @@ void RSmilesLoader::_loadReaction()
buf.push(c);
}

if (_rxn != 0)
ctlt = std::make_unique<Molecule>();
else
ctlt = std::make_unique<QueryMolecule>();

BufferScanner c_scanner(buf);
SmilesLoader c_loader(c_scanner);

Expand All @@ -162,6 +181,7 @@ void RSmilesLoader::_loadReaction()
{
ctlt = std::make_unique<QueryMolecule>();
c_loader.loadQueryMolecule(static_cast<QueryMolecule&>(*ctlt));
_createComponenetsExternalNeighbors(static_cast<QueryMolecule&>(*ctlt), ctlt_extNeibs);
}
ctlt_aam.copy(ctlt->reaction_atom_mapping);

Expand Down Expand Up @@ -201,6 +221,7 @@ void RSmilesLoader::_loadReaction()
{
prod = std::make_unique<QueryMolecule>();
p_loader.loadQueryMolecule(static_cast<QueryMolecule&>(*prod));
_createComponenetsExternalNeighbors(static_cast<QueryMolecule&>(*prod), prod_extNeibs);
}
prod_aam.copy(prod->reaction_atom_mapping);

Expand All @@ -209,9 +230,9 @@ void RSmilesLoader::_loadReaction()
QS_DEF(Array<int>, p_fragments);
Array<int>* fragments[] = {&r_fragments, &c_fragments, &p_fragments};

r_fragments.clear_resize(rcnt->countComponents());
c_fragments.clear_resize(ctlt->countComponents());
p_fragments.clear_resize(prod->countComponents());
r_fragments.clear_resize(rcnt->countComponents(rcnt_extNeibs));
c_fragments.clear_resize(ctlt->countComponents(ctlt_extNeibs));
p_fragments.clear_resize(prod->countComponents(prod_extNeibs));

for (int i = 0; i < r_fragments.size(); i++)
r_fragments[i] = i;
Expand Down
9 changes: 9 additions & 0 deletions core/indigo-core/tests/tests/reaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,12 @@ TEST_F(IndigoCoreReactionTest, aliases_complex)
saverReactionJson(reaction).c_str());
*/
}

TEST_F(IndigoCoreReactionTest, smarts_reaction)
{
QueryReaction qr;
std::string smarts_in = "([#8:1].[#6:2])>>([#8:1].[#6:2])";
loadQueryReaction(smarts_in.c_str(), qr);
ASSERT_EQ(qr.reactantsCount(), 1);
ASSERT_EQ(qr.productsCount(), 1);
}

0 comments on commit 62d2126

Please sign in to comment.