Skip to content

Commit

Permalink
#1316 SMARTS saver miss component level grouping
Browse files Browse the repository at this point in the history
Fix smiles_saver.cpp
For cycles sequence length may be greater than atom count
  • Loading branch information
AliaksandrDziarkach committed Oct 10, 2023
1 parent a333f0f commit c59dcdf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions api/tests/integration/ref/formats/smarts.py.out
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ CC[C+5]CCCCC
[#9]/[#6]=[C]/[#17] is ok. smarts_in==smarts_out
[O;H] is ok. smarts_in==smarts_out
[!O;H] is ok. smarts_in==smarts_out
([#6]1-[#6]-[#6]-1.[#6]) is ok. smarts_in==smarts_out
1 change: 1 addition & 0 deletions api/tests/integration/tests/formats/smarts.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ def test_smarts_load_save(smarts_in):
test_smarts_load_save("[#9]/[#6]=[C]/[#17]")
test_smarts_load_save("[O;H]")
test_smarts_load_save("[!O;H]")
test_smarts_load_save("([#6]1-[#6]-[#6]-1.[#6])")
24 changes: 14 additions & 10 deletions core/indigo-core/molecule/src/smiles_saver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ void SmilesSaver::_saveMolecule()
v_to_comp_group.resize(v_seq.size());
v_to_comp_group.fffill();

if (_qmol != nullptr && smarts_mode && _qmol->components.size() >= v_seq.size())
if (_qmol != nullptr && smarts_mode)
{
if (v_seq.size() < 1)
return; // No atoms to save
std::set<int> components;
int cur_component = -1;
for (int i = 0; i < v_seq.size(); ++i)
for (int i = 0; i < v_seq.size(); ++i && _qmol->components.size() > 0)
{
// In v_seq each fragment started with vertex which parent == -1
// In SMARTS some fragments could be grouped (component-level grouping)
Expand All @@ -204,20 +204,24 @@ void SmilesSaver::_saveMolecule()
// All group fragments should go one by one - in SMARTS its inside "()".
if (v_seq[i].parent_vertex < 0) // New Fragment
{
int new_component = _qmol->components[v_seq[i].idx];
// if component defined for new fragment(id>0) and its different from previous and seen before
if (new_component > 0 && new_component != cur_component && components.count(new_component))
int new_component = 0;
if (_qmol->components.size() > v_seq[i].idx)
{
// According to the DfsWalk code, the groups components should be neighbors.
// If will be found case when it wrong - add code to rearrange fragments
throw Error("SMARTS fragments need to reaarange.");
new_component = _qmol->components[v_seq[i].idx];
// if component defined for new fragment(id>0) and its different from previous and seen before
if (new_component > 0 && new_component != cur_component && components.count(new_component))
{
// According to the DfsWalk code, the groups components should be neighbors.
// If will be found case when it wrong - add code to rearrange fragments
throw Error("SMARTS fragments need to reaarange.");
}
components.emplace(new_component);
}
components.emplace(new_component);
cur_component = new_component;
}
else
{
if (cur_component != _qmol->components[v_seq[i].idx])
if (_qmol->components.size() > v_seq[i].idx && cur_component != _qmol->components[v_seq[i].idx])
{
// Fragment contains atoms from different components - something went wrong
throw Error("Fragment contains atoms from different components.");
Expand Down

0 comments on commit c59dcdf

Please sign in to comment.