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

#1287 New S-Group type Query component level grouping #1288

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*** KET with query components ***
ket_with_query_components.ket:SUCCEED
35 changes: 35 additions & 0 deletions api/tests/integration/tests/formats/ket_with_query_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import difflib
import os
import sys


def find_diff(a, b):
return "\n".join(difflib.unified_diff(a.splitlines(), b.splitlines()))


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

indigo = Indigo()
indigo.setOption("json-saving-pretty", True)

print("*** KET with query components ***")

ref_path = joinPathPy("ref/", __file__)
name = "ket_with_query_components.ket"
filename = os.path.join(ref_path, name)

mol = indigo.loadQueryMoleculeFromFile(filename)
with open(filename, "r") as file:
ket_ref = file.read()
ket = mol.json()
diff = find_diff(ket_ref, ket)
if not diff:
print(name + ":SUCCEED")
else:
print(name + ":FAILED")
print(diff)
198 changes: 198 additions & 0 deletions api/tests/integration/tests/formats/ref/ket_with_query_components.ket
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
{
"root": {
"nodes": [
{
"$ref": "mol0"
},
{
"$ref": "mol1"
},
{
"$ref": "mol2"
}
]
},
"mol0": {
"type": "molecule",
"atoms": [
{
"label": "C",
"location": [
3.2669873237609865,
-2.674999952316284,
0.0
]
},
{
"label": "C",
"location": [
4.133012771606445,
-2.174999952316284,
0.0
],
"queryProperties": {
"connectivity": 2,
"ringSize": 4
}
},
{
"label": "C",
"location": [
5.016987323760986,
-2.299999952316284,
0.0
]
},
{
"label": "C",
"location": [
5.883012771606445,
-1.7999999523162842,
0.0
]
}
],
"bonds": [
{
"type": 1,
"atoms": [
0,
1
]
},
{
"type": 1,
"atoms": [
2,
3
]
},
{
"type": 1,
"atoms": [
1,
2
]
}
]
},
"mol1": {
"type": "molecule",
"atoms": [
{
"label": "C",
"location": [
7.550000190734863,
-3.674999952316284,
0.0
]
},
{
"label": "C",
"location": [
8.050000190734864,
-2.808974504470825,
0.0
]
},
{
"label": "C",
"location": [
9.324999809265137,
-3.299999952316284,
0.0
]
},
{
"label": "N",
"location": [
8.61789321899414,
-4.007106781005859,
0.0
]
}
],
"bonds": [
{
"type": 1,
"atoms": [
0,
1
]
},
{
"type": 1,
"atoms": [
2,
3
]
}
],
"sgroups": [
{
"type": "queryComponent",
"atoms": [
0,
1,
2,
3
]
}
]
},
"mol2": {
"type": "molecule",
"atoms": [
{
"label": "C",
"location": [
3.0999999046325685,
-6.375,
0.0
]
},
{
"label": "C",
"location": [
3.8071067333221437,
-7.082106590270996,
0.0
]
},
{
"label": "C",
"location": [
2.9410812854766847,
-7.582106590270996,
0.0
]
}
],
"bonds": [
{
"type": 1,
"atoms": [
0,
1
]
},
{
"type": 1,
"atoms": [
1,
2
]
}
],
"sgroups": [
{
"type": "queryComponent",
"atoms": [
0,
1,
2
]
}
]
}
}
6 changes: 6 additions & 0 deletions core/indigo-core/molecule/ket_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "common/math/algebra.h"
#include "graph/graph.h"
#include "molecule/molecule_cip_calculator.h"
#include "molecule/query_molecule.h"
#include "reaction/base_reaction.h"

namespace indigo
Expand Down Expand Up @@ -111,6 +112,11 @@ namespace indigo
for (auto atom_idx : sgroup.atoms)
sg_set.insert(atom_idx);
}
if (mol.isQueryMolecule())
{
QueryMolecule& qmol = static_cast<QueryMolecule&>(mol);
qmol.getComponentNeighbors(neighbors);
}
}

class KETSimpleObject : public MetaObject
Expand Down
1 change: 1 addition & 0 deletions core/indigo-core/molecule/molecule_json_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace indigo
Molecule* _pmol;
QueryMolecule* _pqmol;
std::vector<EnhancedStereoCenter> _stereo_centers;
unsigned int components_count;
};

} // namespace indigo
Expand Down
27 changes: 24 additions & 3 deletions core/indigo-core/molecule/src/molecule_json_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ using namespace std;
IMPL_ERROR(MoleculeJsonLoader, "molecule json loader");

MoleculeJsonLoader::MoleculeJsonLoader(Document& ket)
: _mol_array(kArrayType), _mol_nodes(_mol_array), _meta_objects(kArrayType), _pmol(0), _pqmol(0), ignore_noncritical_query_features(false)
: _mol_array(kArrayType), _mol_nodes(_mol_array), _meta_objects(kArrayType), _pmol(0), _pqmol(0), ignore_noncritical_query_features(false),
components_count(0)
{
Value& root = ket["root"];
Value& nodes = root["nodes"];
Expand Down Expand Up @@ -56,7 +57,7 @@ MoleculeJsonLoader::MoleculeJsonLoader(Document& ket)

MoleculeJsonLoader::MoleculeJsonLoader(Value& mol_nodes)
: _mol_nodes(mol_nodes), _meta_objects(kArrayType), _pmol(0), _pqmol(0), ignore_noncritical_query_features(false), ignore_no_chiral_flag(false),
skip_3d_chirality(false), treat_x_as_pseudoatom(false), treat_stereo_as(0)
skip_3d_chirality(false), treat_x_as_pseudoatom(false), treat_stereo_as(0), components_count(0)
{
}

Expand Down Expand Up @@ -672,6 +673,12 @@ void MoleculeJsonLoader::parseBonds(const rapidjson::Value& bonds, BaseMolecule&
}
}

if (b.HasMember("customQuery"))
{
std::string customQuery = b["customQuery"].GetString();
// 2do process custom query
}

if (b.HasMember("cip"))
{
std::string cip = b["cip"].GetString();
Expand Down Expand Up @@ -844,11 +851,25 @@ void MoleculeJsonLoader::parseSGroups(const rapidjson::Value& sgroups, BaseMolec
for (SizeType i = 0; i < sgroups.Size(); i++)
{
const Value& s = sgroups[i];
const Value& atoms = s["atoms"];
std::string sg_type_str = s["type"].GetString(); // GEN, MUL, SRU, SUP
if (sg_type_str == "queryComponent")
{
if (_pqmol)
{
_pqmol->components.expandFill(_pqmol->components.size() + atoms.Size(), 0);
components_count++;
for (int j = 0; j < atoms.Size(); ++j)
{
int atom_idx = atoms[j].GetInt();
_pqmol->components[atom_idx] = components_count;
}
}
continue;
}
int sg_type = SGroup::getType(sg_type_str.c_str());
int grp_idx = mol.sgroups.addSGroup(sg_type);
SGroup& sgroup = mol.sgroups.getSGroup(grp_idx);
const Value& atoms = s["atoms"];
// add atoms
std::unordered_set<int> sgroup_atoms;
for (int j = 0; j < atoms.Size(); ++j)
Expand Down
33 changes: 32 additions & 1 deletion core/indigo-core/molecule/src/molecule_json_saver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,19 @@ void MoleculeJsonSaver::saveSGroups(BaseMolecule& mol, JsonWriter& writer)
{
QS_DEF(Array<int>, sgs_sorted);
_checkSGroupIndices(mol, sgs_sorted);
if (mol.countSGroups() > 0)
int sGroupsCount = mol.countSGroups();
bool componentDefined = false;
if (mol.isQueryMolecule())
{
QueryMolecule& qmol = static_cast<QueryMolecule&>(mol);
if (qmol.components.size() > 0 && qmol.components[0])
{
componentDefined = true;
sGroupsCount++;
}
}

if (sGroupsCount > 0)
{
writer.Key("sgroups");
writer.StartArray();
Expand All @@ -160,6 +172,25 @@ void MoleculeJsonSaver::saveSGroups(BaseMolecule& mol, JsonWriter& writer)
auto& sgrp = mol.sgroups.getSGroup(sg_idx);
saveSGroup(sgrp, writer);
}
// save queryComponent
if (mol.isQueryMolecule() && componentDefined)
{
QueryMolecule& qmol = static_cast<QueryMolecule&>(mol);
writer.StartObject();
writer.Key("type");
writer.String("queryComponent");
writer.Key("atoms");
writer.StartArray();
for (int i = 0; i < qmol.vertexCount(); i++)
{
if (qmol.components[i])
{
writer.Int(i);
}
}
writer.EndArray();
writer.EndObject();
}
writer.EndArray();
}
}
Expand Down
1 change: 0 additions & 1 deletion core/indigo-core/molecule/src/query_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,6 @@ void QueryMolecule::getComponentNeighbors(std::list<std::unordered_set<int>>& co
componentAtoms[componentId].insert(i);
}
}
componentNeighbors.clear();
for (auto elem : componentAtoms)
{
auto atoms = elem.second;
Expand Down
Loading