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

Replace ObjArraj/ObjList with std::vector in IndigoDeconvolution #842

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
78 changes: 46 additions & 32 deletions api/c/indigo/src/indigo_deconvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
* limitations under the License.
***************************************************************************/

#include "indigo_deconvolution.h"
#include <algorithm>

#include "base_cpp/array.h"
#include "base_cpp/obj_array.h"
#include "base_cpp/obj_list.h"
#include "base_cpp/red_black.h"
#include "base_cpp/tlscont.h"
#include "graph/automorphism_search.h"
#include "indigo_array.h"
#include "indigo_deconvolution.h"
#include "indigo_molecule.h"
#include "molecule/elements.h"
#include "molecule/max_common_submolecule.h"
Expand Down Expand Up @@ -217,17 +219,17 @@ int IndigoDeconvolution::_rGroupsEmbedding(Graph& graph1, Graph& graph2, int* ma
* Order - atom number for scaffold
* Index - atom number for Rgroup
*/
ObjArray<Array<int>>& attachment_order = deco_match.attachmentOrder;
ObjArray<Array<int>>& attachment_index = deco_match.attachmentIndex;
std::vector<Array<int>>& attachment_order = deco_match.attachmentOrder;
std::vector<Array<int>>& attachment_index = deco_match.attachmentIndex;

visited_atoms.clear_resize(graph2.vertexEnd());
visited_atoms.zerofill();

attachment_index.clear();
attachment_order.clear();

attachment_index.push();
attachment_order.push();
attachment_index.emplace_back();
attachment_order.emplace_back();
/*
* Calculate scaffold atoms and Rgroup atoms
*/
Expand Down Expand Up @@ -294,8 +296,8 @@ int IndigoDeconvolution::_rGroupsEmbedding(Graph& graph1, Graph& graph2, int* ma
}

++n_rgroups;
attachment_index.push();
attachment_order.push();
attachment_index.emplace_back();
attachment_order.emplace_back();
}

visited_atoms[start_idx] = 1;
Expand Down Expand Up @@ -338,8 +340,8 @@ int IndigoDeconvolution::_rGroupsEmbedding(Graph& graph1, Graph& graph2, int* ma
attachment_index[n_rgroups].push(edge.end);
attachment_order[n_rgroups].push(edge.beg);

attachment_index.push();
attachment_order.push();
attachment_index.emplace_back();
attachment_order.emplace_back();

++n_rgroups;
}
Expand Down Expand Up @@ -376,8 +378,8 @@ void IndigoDeconvolution::createRgroups(IndigoDecompositionMatch& deco_match, bo

Array<int>& visited_atoms = deco_match.visitedAtoms;

ObjArray<Array<int>>& attachment_order = deco_match.attachmentOrder;
ObjArray<Array<int>>& attachment_index = deco_match.attachmentIndex;
std::vector<Array<int>>& attachment_order = deco_match.attachmentOrder;
std::vector<Array<int>>& attachment_index = deco_match.attachmentIndex;

int n_rgroups = deco_match.getRgroupNumber();
/*
Expand Down Expand Up @@ -657,11 +659,13 @@ void IndigoDecompositionMatch::copy(IndigoDecompositionMatch& other)
attachmentIndex.clear();
for (int i = 0; i < other.attachmentOrder.size(); ++i)
{
attachmentOrder.push().copy(other.attachmentOrder[i]);
attachmentOrder.emplace_back();
attachmentOrder.back().copy(other.attachmentOrder[i]);
}
for (int i = 0; i < other.attachmentIndex.size(); ++i)
{
attachmentIndex.push().copy(other.attachmentIndex[i]);
attachmentIndex.emplace_back();
attachmentIndex.back().copy(other.attachmentIndex[i]);
}

mol_out.clone_KeepIndices(other.mol_out, 0);
Expand All @@ -688,13 +692,13 @@ void IndigoDecompositionMatch::removeRsitesFromMaps(Graph& query_graph)
}
}

void IndigoDecompositionMatch::copyScafAutoMaps(ObjList<Array<int>>& autoMaps)
void IndigoDecompositionMatch::copyScafAutoMaps(const std::vector<Array<int>>& autoMaps)
{
scafAutoMaps.clear();
for (int i = autoMaps.begin(); i != autoMaps.end(); i = autoMaps.next(i))
for (const auto& src : autoMaps)
{
int idx = scafAutoMaps.add();
scafAutoMaps.at(idx).copy(autoMaps[i]);
scafAutoMaps.emplace_back();
scafAutoMaps.back().copy(src);
}
}

Expand Down Expand Up @@ -951,7 +955,7 @@ void IndigoDeconvolution::addCompleteRGroup(IndigoDecompositionMatch& deco_match
if (deco_match.scafAutoMaps.size() == 0)
throw Error("internal error: can not calculate scaffold matchings for null automorphism");

for (int aut_idx = deco_match.scafAutoMaps.begin(); aut_idx != deco_match.scafAutoMaps.end(); aut_idx = deco_match.scafAutoMaps.next(aut_idx))
for (int aut_idx = 0; aut_idx < deco_match.scafAutoMaps.size(); ++aut_idx)
{
int new_rg_num = _createRgMap(deco_match, aut_idx, match_rgroups, &rg_map_buf, false);
/*
Expand Down Expand Up @@ -1123,7 +1127,7 @@ int IndigoDeconvolution::_createRgMap(IndigoDecompositionMatch& deco_match, int
int max_rg_idx = match_rgroups.at("max_rg_idx").at(0);

int n_rgroups = deco_match.getRgroupNumber();
ObjArray<Array<int>>& attachment_order = deco_match.attachmentOrder;
std::vector<Array<int>>& attachment_order = deco_match.attachmentOrder;
Array<int>& map = deco_match.lastInvMapping;
int result_num = 0;

Expand Down Expand Up @@ -1237,8 +1241,8 @@ void IndigoDeconvolution::DecompositionEnumerator::calculateAutoMaps(Graph& sub)
*/
_scafAutoMaps.clear();

int l_idx = _scafAutoMaps.add();
Array<int>& d_map = _scafAutoMaps.at(l_idx);
_scafAutoMaps.emplace_back();
Array<int>& d_map = _scafAutoMaps.back();
d_map.resize(sub.vertexEnd());
for (int i = 0; i < d_map.size(); ++i)
{
Expand Down Expand Up @@ -1340,9 +1344,8 @@ void IndigoDeconvolution::DecompositionEnumerator::addMatch(IndigoDecompositionM
{
direct_order.push(pair.first);
}
for (int auto_idx = _autoMaps.begin(); auto_idx != _autoMaps.end(); auto_idx = _autoMaps.next(auto_idx))
for (const Array<int>& auto_map : _autoMaps)
{
Array<int>& auto_map = _autoMaps[auto_idx];
/*
* Check for correctness and condition
*/
Expand Down Expand Up @@ -1408,8 +1411,8 @@ bool IndigoDeconvolution::DecompositionEnumerator::_foundOrder(ObjArray<Array<in
void IndigoDeconvolution::DecompositionEnumerator::_swapIndexes(IndigoDecompositionMatch& match, int old_idx, int new_idx)
{
QS_DEF(Array<int>, tmp_buf);
ObjArray<Array<int>>& attachment_order = match.attachmentOrder;
ObjArray<Array<int>>& attachment_index = match.attachmentIndex;
std::vector<Array<int>>& attachment_order = match.attachmentOrder;
std::vector<Array<int>>& attachment_index = match.attachmentIndex;

tmp_buf.copy(attachment_order[old_idx]);
attachment_order[old_idx].copy(attachment_order[new_idx]);
Expand All @@ -1434,15 +1437,15 @@ void IndigoDeconvolution::DecompositionEnumerator::_swapIndexes(IndigoDecomposit
}
}

void IndigoDeconvolution::DecompositionEnumerator::_refineAutoMaps(ObjList<Array<int>>& auto_maps, Graph& sub_in, Graph& super, Array<int>& scaf_map)
void IndigoDeconvolution::DecompositionEnumerator::_refineAutoMaps(std::vector<Array<int>>& auto_maps, Graph& sub_in, Graph& super, Array<int>& scaf_map)
{
QS_DEF(Array<int>, indices_to_remove);
indices_to_remove.clear();

BaseMolecule& sub = (BaseMolecule&)sub_in;

int sub_idx, super_idx, ae_idx_beg, ae_idx_end, e_beg, e_end;
for (int auto_idx = auto_maps.begin(); auto_idx != auto_maps.end(); auto_idx = auto_maps.next(auto_idx))
for (int auto_idx = 0; auto_idx < auto_maps.size(); ++auto_idx)
{
Array<int>& auto_map = auto_maps.at(auto_idx);

Expand Down Expand Up @@ -1513,10 +1516,21 @@ void IndigoDeconvolution::DecompositionEnumerator::_refineAutoMaps(ObjList<Array
indices_to_remove.push(auto_idx);
}
}
for (int i = 0; i < indices_to_remove.size(); ++i)

// clean-up auto_maps according to indices_to_remove
auto tmp_maps = std::move(auto_maps);
auto_maps.clear();

int start = 0;
for (int idx : indices_to_remove)
{
auto_maps.remove(indices_to_remove[i]);
const auto startIt = tmp_maps.begin() + start;
const auto endIt = tmp_maps.begin() + idx;
std::move(startIt, endIt, std::back_inserter(auto_maps));

start = idx + 1;
}
std::move(tmp_maps.begin() + start, tmp_maps.end(), std::back_inserter(auto_maps));
}

void IndigoDeconvolution::DecompositionEnumerator::_addAllRsites(QueryMolecule& r_molecule, IndigoDecompositionMatch& deco_match, std::map<int, int>& r_sites)
Expand Down Expand Up @@ -1558,9 +1572,9 @@ void IndigoDeconvolution::DecompositionEnumerator::_addAllRsites(QueryMolecule&

bool IndigoDeconvolution::DecompositionEnumerator::_cbAutoCheckAutomorphism(Graph&, const Array<int>& mapping, const void* context)
{
ObjList<Array<int>>& auto_maps = *(ObjList<Array<int>>*)context;
int l_idx = auto_maps.add();
auto_maps.at(l_idx).copy(mapping);
std::vector<Array<int>>& auto_maps = *(std::vector<Array<int>>*)context;
auto_maps.emplace_back();
auto_maps.back().copy(mapping);
return false;
}

Expand Down
15 changes: 8 additions & 7 deletions api/c/indigo/src/indigo_deconvolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define __indigo_deconvolution__

#include <map>
#include <vector>

#include "base_cpp/obj_list.h"
#include "base_cpp/properties_map.h"
Expand Down Expand Up @@ -108,12 +109,12 @@ class DLLEXPORT IndigoDeconvolution : public IndigoObject
DecompositionEnumerator(const DecompositionEnumerator&); // no implicit copy
bool _foundOrder(ObjArray<Array<int>>& rsite_orders, Array<int>& swap_order);
void _swapIndexes(IndigoDecompositionMatch&, int old_idx, int new_idx);
void _refineAutoMaps(ObjList<Array<int>>& auto_maps, Graph& sub, Graph& super, Array<int>& scaf_map);
void _refineAutoMaps(std::vector<Array<int>>& auto_maps, Graph& sub, Graph& super, Array<int>& scaf_map);
void _addAllRsites(QueryMolecule&, IndigoDecompositionMatch&, std::map<int, int>&);

static bool _cbAutoCheckAutomorphism(Graph& graph, const Array<int>& mapping, const void* context);
ObjList<Array<int>> _autoMaps;
ObjList<Array<int>> _scafAutoMaps;
std::vector<Array<int>> _autoMaps;
std::vector<Array<int>> _scafAutoMaps;
};

void addCompleteRGroup(IndigoDecompositionMatch& emb_context, bool change_scaffold, Array<int>* rg_map);
Expand Down Expand Up @@ -178,9 +179,9 @@ class DLLEXPORT IndigoDecompositionMatch : public IndigoObject
Array<int> scaffoldAtoms;
Array<int> lastMapping;
Array<int> lastInvMapping;
ObjArray<Array<int>> attachmentOrder;
ObjArray<Array<int>> attachmentIndex;
ObjList<Array<int>> scafAutoMaps;
std::vector<Array<int>> attachmentOrder;
std::vector<Array<int>> attachmentIndex;
std::vector<Array<int>> scafAutoMaps;

int getRgroupNumber() const
{
Expand All @@ -190,7 +191,7 @@ class DLLEXPORT IndigoDecompositionMatch : public IndigoObject
void renumber(Array<int>& map, Array<int>& inv_map);
void copy(IndigoDecompositionMatch& other);
void removeRsitesFromMaps(Graph& query_graph);
void copyScafAutoMaps(ObjList<Array<int>>& autoMaps);
void copyScafAutoMaps(const std::vector<Array<int>>& autoMaps);
void completeScaffold();

Molecule mol_out;
Expand Down
2 changes: 1 addition & 1 deletion core/indigo-core/common/base_cpp/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace indigo
{
}

Array(Array&& other) : _reserved(other._reserved), _length(other._length), _array(other._array)
Array(Array&& other) noexcept : _reserved(other._reserved), _length(other._length), _array(other._array)
{
other._array = nullptr;
other._length = 0;
Expand Down