Commit 7dedb46d by maarten

optimalisatie?

git-svn-id: svn+ssh://gitlab/srv/svn-repos/pdb-redo/trunk@223 a1961a4f-ab94-4bcc-80e8-33b5a54de466
parent 0d18d477
...@@ -69,6 +69,7 @@ struct CompoundBondLess ...@@ -69,6 +69,7 @@ struct CompoundBondLess
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// Brute force comparison of two structures, when they are isomers the // Brute force comparison of two structures, when they are isomers the
// mapping between the atoms of both is returned // mapping between the atoms of both is returned
// This is not an optimal solution, but it works good enough for now
struct Node struct Node
{ {
...@@ -82,21 +83,21 @@ bool SubStructuresAreIsomeric( ...@@ -82,21 +83,21 @@ bool SubStructuresAreIsomeric(
const vector<Node>& a, const vector<Node>& b, size_t iA, size_t iB, const vector<Node>& a, const vector<Node>& b, size_t iA, size_t iB,
vector<bool> visitedA, vector<bool> visitedB, vector<tuple<string,string>>& outMapping) vector<bool> visitedA, vector<bool> visitedB, vector<tuple<string,string>>& outMapping)
{ {
bool result = false;
auto& na = a[iA]; auto& na = a[iA];
auto& nb = b[iB]; auto& nb = b[iB];
size_t N = na.links.size(); size_t N = na.links.size();
if (na.symbol == nb.symbol and nb.links.size() == N) assert(na.symbol == nb.symbol);
{ assert(nb.links.size() == N);
result = true;
// we're optimistic today
bool result = true;
visitedA[iA] = true; visitedA[iA] = true;
visitedB[iB] = true; visitedB[iB] = true;
// we now have two sets of links to compare. // we now have two sets of links to compare.
// To keep code clean, first create the list of possible permutations // Compare each permutation of the second set of indices with the first
vector<size_t> ilb(N); vector<size_t> ilb(N);
iota(ilb.begin(), ilb.end(), 0); iota(ilb.begin(), ilb.end(), 0);
...@@ -104,17 +105,17 @@ bool SubStructuresAreIsomeric( ...@@ -104,17 +105,17 @@ bool SubStructuresAreIsomeric(
for (;;) for (;;)
{ {
result = true; result = true;
vector<tuple<string,string>> m = outMapping; vector<tuple<string,string>> m;
for (size_t i = 0; result and i < N; ++i) for (size_t i = 0; result and i < N; ++i)
{ {
size_t lA, lB; size_t lA, lB;
CompoundBondType typeA, typeB; CompoundBondType typeA, typeB;
tie(lA, typeA) = na.links[i]; tie(lA, typeA) = na.links[i]; assert(lA < a.size());
tie(lB, typeB) = nb.links[ilb[i]]; tie(lB, typeB) = nb.links[ilb[i]]; assert(lB < b.size());
if (typeA != typeB or visitedA[lA] != visitedB[lB]) if (typeA != typeB or visitedA[lA] != visitedB[lB] or a[lA].symbol != b[lB].symbol or a[lA].links.size() != b[lB].links.size())
result = false; result = false;
else if (not visitedA[lA]) else if (not visitedA[lA])
result = SubStructuresAreIsomeric(a, b, lA, lB, visitedA, visitedB, m); result = SubStructuresAreIsomeric(a, b, lA, lB, visitedA, visitedB, m);
...@@ -122,7 +123,7 @@ bool SubStructuresAreIsomeric( ...@@ -122,7 +123,7 @@ bool SubStructuresAreIsomeric(
if (result) if (result)
{ {
swap(m, outMapping); outMapping.insert(outMapping.end(), m.begin(), m.end());
break; break;
} }
...@@ -132,7 +133,6 @@ bool SubStructuresAreIsomeric( ...@@ -132,7 +133,6 @@ bool SubStructuresAreIsomeric(
if (result) if (result)
outMapping.emplace_back(na.id, nb.id); outMapping.emplace_back(na.id, nb.id);
}
return result; return result;
} }
...@@ -178,15 +178,21 @@ bool StructuresAreIsomeric(vector<CompoundAtom> atomsA, const vector<CompoundBon ...@@ -178,15 +178,21 @@ bool StructuresAreIsomeric(vector<CompoundAtom> atomsA, const vector<CompoundBon
} }
size_t N = atomsA.size(); size_t N = atomsA.size();
size_t ia = find_if(a.begin(), a.end(), [](auto& a) { return a.symbol != libcif::H; }) - a.begin();
if (ia == N)
throw runtime_error("This compound contains no atoms other than Hydrogen?");
bool result = false; bool result = false;
// try each atom in B to see if it can be traced to be similar to A starting at zero // try each atom in B to see if it can be traced to be similar to A starting at zero
for (size_t ib = 0; ib < N; ++ib) for (size_t ib = 0; ib < N; ++ib)
{ {
if (b[ib].symbol != a[ia].symbol or a[ib].links.size() != b[ia].links.size())
continue;
vector<bool> va(N, false), vb(N, false); vector<bool> va(N, false), vb(N, false);
if (SubStructuresAreIsomeric(a, b, 0, ib, va, vb, outMapping)) if (SubStructuresAreIsomeric(a, b, ia, ib, va, vb, outMapping))
{ {
result = true; result = true;
break; break;
...@@ -451,9 +457,9 @@ const Compound* CompoundFactory::create(std::string id) ...@@ -451,9 +457,9 @@ const Compound* CompoundFactory::create(std::string id)
using cif::iequals; using cif::iequals;
if (iequals(type, "single")) b.type = singleBond; if (iequals(type, "single") or iequals(type, "sing")) b.type = singleBond;
else if (iequals(type, "double")) b.type = doubleBond; else if (iequals(type, "double") or iequals(type, "doub")) b.type = doubleBond;
else if (iequals(type, "triple")) b.type = tripleBond; else if (iequals(type, "triple") or iequals(type, "trip")) b.type = tripleBond;
else if (iequals(type, "deloc") or iequals(type, "aromat") or iequals(type, "aromatic")) else if (iequals(type, "deloc") or iequals(type, "aromat") or iequals(type, "aromatic"))
b.type = delocalizedBond; b.type = delocalizedBond;
else else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment