Skip to content

Commit

Permalink
#1260: fix SMARTS saver for non-query entities with atom mappings (#1261
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mkviatkovskii authored Sep 8, 2023
1 parent d01d729 commit 6ed3230
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 2 deletions.
14 changes: 13 additions & 1 deletion api/cpp/src/IndigoBaseReaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
***************************************************************************/

#include "IndigoBaseReaction.h"
#include "IndigoSession.h"

#include <array>

#include <indigo.h>

using namespace indigo_cpp;

namespace
{
const std::array<const char*, 4> AUTOMAP_MODES{"discard", "keep", "alter", "clear"};
}

IndigoBaseReaction::IndigoBaseReaction(const int id, IndigoSessionPtr session) : IndigoChemicalStructure(id, std::move(session))
{
}
Expand All @@ -41,3 +47,9 @@ std::string IndigoBaseReaction::ctfile() const
{
return rxnfile();
}

void IndigoBaseReaction::automap(const IndigoAutomapMode& mode)
{
session()->setSessionId();
session()->_checkResult(indigoAutomap(id(), AUTOMAP_MODES.at(static_cast<size_t>(mode))));
}
10 changes: 10 additions & 0 deletions api/cpp/src/IndigoBaseReaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@

namespace indigo_cpp
{
enum class IndigoAutomapMode
{
DISCARD = 0,
KEEP,
ALTER,
CLEAR
};

class IndigoBaseReaction : public IndigoChemicalStructure
{
protected:
Expand All @@ -35,5 +43,7 @@ namespace indigo_cpp
public:
std::string rxnfile() const;
std::string ctfile() const override;

void automap(const IndigoAutomapMode& mode);
};
}
6 changes: 6 additions & 0 deletions api/cpp/src/IndigoChemicalStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ std::string IndigoChemicalStructure::smiles() const
return session()->_checkResultString(indigoSmiles(id()));
}

std::string IndigoChemicalStructure::smarts() const
{
session()->setSessionId();
return session()->_checkResultString(indigoSmarts(id()));
}

std::string IndigoChemicalStructure::canonicalSmiles() const
{
session()->setSessionId();
Expand Down
2 changes: 2 additions & 0 deletions api/cpp/src/IndigoChemicalStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace indigo_cpp

std::string smiles() const;

std::string smarts() const;

std::string cml() const;

std::string inchi() const;
Expand Down
29 changes: 29 additions & 0 deletions api/cpp/src/IndigoQueryReaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/****************************************************************************
* Copyright (C) from 2009 to Present EPAM Systems.
*
* This file is part of Indigo toolkit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/

#include "IndigoQueryReaction.h"

using namespace indigo_cpp;

IndigoQueryReaction::IndigoQueryReaction(const int id, IndigoSessionPtr session) : IndigoBaseReaction(id, std::move(session))
{
}

IndigoQueryReaction::IndigoQueryReaction(const IndigoQueryReaction& other) : IndigoBaseReaction(other)
{
}
35 changes: 35 additions & 0 deletions api/cpp/src/IndigoQueryReaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/****************************************************************************
* Copyright (C) from 2009 to Present EPAM Systems.
*
* This file is part of Indigo toolkit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/

#pragma once

#include "IndigoBaseReaction.h"

namespace indigo_cpp
{
class IndigoQueryReaction final : public IndigoBaseReaction
{
public:
IndigoQueryReaction(int id, IndigoSessionPtr session);
IndigoQueryReaction(IndigoQueryReaction&&) = default;
IndigoQueryReaction& operator=(IndigoQueryReaction&&) = default;
IndigoQueryReaction(const IndigoQueryReaction&);
IndigoQueryReaction& operator=(const IndigoQueryReaction&) = default;
~IndigoQueryReaction() final = default;
};
}
6 changes: 6 additions & 0 deletions api/cpp/src/IndigoSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,9 @@ IndigoReaction IndigoSession::loadReaction(const std::string& data)
setSessionId();
return {_checkResult(indigoLoadReactionFromString(data.c_str())), shared_from_this()};
}

IndigoReaction IndigoSession::loadQueryReaction(const std::string& data)
{
setSessionId();
return {_checkResult(indigoLoadQueryReactionFromString(data.c_str())), shared_from_this()};
}
5 changes: 4 additions & 1 deletion api/cpp/src/IndigoSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ namespace indigo_cpp
IndigoMolecule loadMolecule(const std::string& data);
IndigoMolecule loadMoleculeFromFile(const std::string& path);

IndigoQueryMolecule loadQueryMolecule(const std::string& data);

IndigoReaction loadReaction(const std::string& data);

IndigoQueryMolecule loadQueryMolecule(const std::string& data);
IndigoReaction loadQueryReaction(const std::string& data);

IndigoWriteBuffer writeBuffer();
IndigoIterator<IndigoReaction> iterateRDFile(const std::string& path);
IndigoIterator<IndigoMolecule> iterateSDFile(const std::string& path);
Expand Down
41 changes: 41 additions & 0 deletions api/cpp/tests/basic/reaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/****************************************************************************
* Copyright (C) from 2009 to Present EPAM Systems.
*
* This file is part of Indigo toolkit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/

#include <gtest/gtest.h>

#include <IndigoReaction.h>

#include "common.h"

using namespace indigo_cpp;

TEST(Reaction, automapReactionSmarts)
{
const auto& session = IndigoSession::create();
auto reaction = session->loadReaction("[C:1]=[C:2][C:3].[C:4]=[C:5][C:6]>>[C:3][C:2]=[C:5][C:6]");
reaction.automap(IndigoAutomapMode::CLEAR);
ASSERT_STREQ("[#6]=[#6]-[#6].[#6]=[#6]-[#6]>>[#6]-[#6]=[#6]-[#6] |^3:0,3,^1:1,4,7,8|", reaction.smarts().c_str());
}

TEST(Reaction, automapQueryReactionSmarts)
{
const auto& session = IndigoSession::create();
auto reaction = session->loadQueryReaction("[C:1]=[C:2][C:3].[C:4]=[C:5][C:6]>>[C:3][C:2]=[C:5][C:6] |$;;R1;;;R2;R1;;;R2$|");
reaction.automap(IndigoAutomapMode::CLEAR);
ASSERT_STREQ("[#6]=[#6]-[#6].[#6]=[#6]-[#6]>>[#6]-[#6]=[#6]-[#6] |$;;R1;;;R2;R1;;;R2$|", reaction.smarts().c_str());
}
10 changes: 10 additions & 0 deletions core/indigo-core/molecule/src/smiles_saver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,11 @@ void SmilesSaver::_writeSmartsAtom(int idx, QueryMolecule::Atom* atom, int chira
case QueryMolecule::OP_AND: {
for (i = 0; i < atom->children.size(); i++)
{
if (atom->children[i]->type == QueryMolecule::ATOM_RADICAL || atom->children[i]->type == QueryMolecule::ATOM_VALENCE)
{
continue;
}

if (i > 0)
_output.writeChar(has_or_parent ? '&' : ';');
_writeSmartsAtom(idx, (QueryMolecule::Atom*)atom->children[i], chirality, depth + 1, has_or_parent, has_not_parent);
Expand All @@ -887,6 +892,11 @@ void SmilesSaver::_writeSmartsAtom(int idx, QueryMolecule::Atom* atom, int chira
case QueryMolecule::OP_OR: {
for (i = 0; i < atom->children.size(); i++)
{
if (atom->children[i]->type == QueryMolecule::ATOM_RADICAL || atom->children[i]->type == QueryMolecule::ATOM_VALENCE)
{
continue;
}

if (i > 0)
_output.printf(has_not_parent ? "!" : ",");
_writeSmartsAtom(idx, (QueryMolecule::Atom*)atom->children[i], chirality, depth + 1, true, has_not_parent);
Expand Down

0 comments on commit 6ed3230

Please sign in to comment.