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

Support writing CX extensions in reactions #7340

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Code/GraphMol/ChemReactions/ReactionParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ inline ChemicalReaction *RxnSmartsToChemicalReaction(
} // namespace v1
//! returns the reaction SMARTS for a reaction
RDKIT_CHEMREACTIONS_EXPORT std::string ChemicalReactionToRxnSmarts(
const ChemicalReaction &rxn);
const ChemicalReaction &rxn, bool includeCX = false);

//! returns the reaction SMILES for a reaction
RDKIT_CHEMREACTIONS_EXPORT std::string ChemicalReactionToRxnSmiles(
Expand Down
61 changes: 57 additions & 4 deletions Code/GraphMol/ChemReactions/ReactionWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,35 @@ std::string chemicalReactionTemplatesToString(
return res;
}

void insertTemplates(const RDKit::ChemicalReaction &rxn,
RDKit::RWMol &rwmol, std::vector<unsigned int> &atomOrdering,
std::vector<unsigned int> &bondOrdering, RDKit::ReactionMoleculeType type) {
auto at_count = rwmol.getNumAtoms();
auto bnd_count = rwmol.getNumBonds();
std::vector<unsigned int> prevAtomOrdering;
std::vector<unsigned int> prevBondOrdering;
Comment on lines +99 to +100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is duplicated, it's declared again inside the loop.


for (auto begin = getStartIterator(rxn, type);
begin != getEndIterator(rxn, type); ++begin) {
rwmol.insertMol(**begin);

std::vector<unsigned int> prevAtomOrdering;
std::vector<unsigned int> prevBondOrdering;
(*begin)->getProp(RDKit::common_properties::_smilesAtomOutputOrder, prevAtomOrdering);
(*begin)->getProp(RDKit::common_properties::_smilesBondOutputOrder, prevBondOrdering);
for (auto i : prevAtomOrdering) {
atomOrdering.push_back(i + at_count);
}
for (auto i : prevBondOrdering) {
bondOrdering.push_back(i + bnd_count);
}
at_count = rwmol.getNumAtoms();
bnd_count = rwmol.getNumBonds();
}
}

std::string chemicalReactionToRxnToString(const RDKit::ChemicalReaction &rxn,
bool toSmiles, bool canonical) {
bool toSmiles, bool canonical, bool includeCX) {
std::string res = "";
res += chemicalReactionTemplatesToString(rxn, RDKit::Reactant, toSmiles,
canonical);
Expand All @@ -102,6 +129,32 @@ std::string chemicalReactionToRxnToString(const RDKit::ChemicalReaction &rxn,
res += ">";
res += chemicalReactionTemplatesToString(rxn, RDKit::Product, toSmiles,
canonical);

if (includeCX) {
// I think this will work? combine reactants, agents and products into a
// single molecule and get the cxextension for that
// blank rwmol
RDKit::RWMol rwmol;
std::vector<unsigned int> atomOrdering;
std::vector<unsigned int> bondOrdering;

insertTemplates(rxn, rwmol, atomOrdering, bondOrdering, RDKit::Reactant);
insertTemplates(rxn, rwmol, atomOrdering, bondOrdering, RDKit::Agent);
insertTemplates(rxn, rwmol, atomOrdering, bondOrdering, RDKit::Product);

rwmol.setProp(RDKit::common_properties::_smilesAtomOutputOrder, atomOrdering, true);
rwmol.setProp(RDKit::common_properties::_smilesBondOutputOrder, bondOrdering, true);

// ignore atom properties -- really this is to avoid the atom map numbers, maybe we just want to remove
// those
auto flags = RDKit::SmilesWrite::CXSmilesFields::CX_ATOM_PROPS ^ RDKit::SmilesWrite::CXSmilesFields::CX_ALL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you doing this to remove the RGroup labels from the output?

auto ext = RDKit::SmilesWrite::getCXExtensions(rwmol, flags);
if (!ext.empty()) {
res += " ";
res += ext;
}
}

return res;
}

Expand All @@ -121,14 +174,14 @@ void write_template(std::ostringstream &res, RDKit::ROMol &tpl) {
namespace RDKit {

//! returns the reaction SMARTS for a reaction
std::string ChemicalReactionToRxnSmarts(const ChemicalReaction &rxn) {
return chemicalReactionToRxnToString(rxn, false, false);
std::string ChemicalReactionToRxnSmarts(const ChemicalReaction &rxn, bool includeCX) {
return chemicalReactionToRxnToString(rxn, false, false, includeCX);
};

//! returns the reaction SMILES for a reaction
std::string ChemicalReactionToRxnSmiles(const ChemicalReaction &rxn,
bool canonical) {
return chemicalReactionToRxnToString(rxn, true, canonical);
return chemicalReactionToRxnToString(rxn, true, canonical, false);
};

//! returns an RXN block for a reaction
Expand Down
2 changes: 1 addition & 1 deletion Code/GraphMol/ChemReactions/Wrap/rdChemReactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ see the documentation for rdkit.Chem.MolFromSmiles for an explanation\n\
of the replacements argument.",
python::return_value_policy<python::manage_new_object>());
python::def("ReactionToSmarts", RDKit::ChemicalReactionToRxnSmarts,
(python::arg("reaction")),
(python::arg("reaction"), python::arg("includeCX") = false),
"construct a reaction SMARTS string for a ChemicalReaction");
python::def("ReactionToSmiles", RDKit::ChemicalReactionToRxnSmiles,
(python::arg("reaction"), python::arg("canonical") = true),
Expand Down
62 changes: 62 additions & 0 deletions Code/GraphMol/ChemReactions/catch_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,17 @@ TEST_CASE("CXSMILES for reactions", "[cxsmiles]") {
CHECK(rxn->getReactants()[1]->getAtomWithIdx(2)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");

bool includeCX = true;
auto roundtrip = v2::ReactionParser::ReactionFromSmarts(ChemicalReactionToRxnSmarts(*rxn, includeCX));
REQUIRE(roundtrip);
CHECK(roundtrip->getReactants().size() == 2);
CHECK(roundtrip->getReactants()[0]->getAtomWithIdx(3)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");
CHECK(roundtrip->getReactants()[1]->getAtomWithIdx(2)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");
}
SECTION("basics with agents") {
// clang-format off
Expand All @@ -1115,6 +1126,17 @@ TEST_CASE("CXSMILES for reactions", "[cxsmiles]") {
CHECK(rxn->getReactants()[1]->getAtomWithIdx(2)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");

bool includeCX = true;
auto roundtrip = v2::ReactionParser::ReactionFromSmarts(ChemicalReactionToRxnSmarts(*rxn, includeCX));
REQUIRE(roundtrip);
CHECK(roundtrip->getReactants().size() == 2);
CHECK(roundtrip->getReactants()[0]->getAtomWithIdx(3)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");
CHECK(roundtrip->getReactants()[1]->getAtomWithIdx(2)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");
}
SECTION("missing products") {
// clang-format off
Expand All @@ -1129,6 +1151,17 @@ TEST_CASE("CXSMILES for reactions", "[cxsmiles]") {
CHECK(rxn->getReactants()[1]->getAtomWithIdx(2)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");

bool includeCX = true;
auto roundtrip = v2::ReactionParser::ReactionFromSmarts(ChemicalReactionToRxnSmarts(*rxn, includeCX));
REQUIRE(roundtrip);
CHECK(roundtrip->getReactants().size() == 2);
CHECK(roundtrip->getReactants()[0]->getAtomWithIdx(3)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");
CHECK(roundtrip->getReactants()[1]->getAtomWithIdx(2)->getPropIfPresent(
common_properties::atomLabel, alabel));
CHECK(alabel == "_AP1");
}
SECTION("coordinate bonds and sgroups") {
// when initially writing this, coordinate bonds were not properly parsed
Expand Down Expand Up @@ -1164,6 +1197,12 @@ TEST_CASE("CXSMILES for reactions", "[cxsmiles]") {
REQUIRE(rxn);
CHECK(getSubstanceGroups(*rxn->getReactants()[0]).size() == 2);
CHECK(getSubstanceGroups(*rxn->getProducts()[0]).size() == 2);

bool includeCX = true;
auto roundtrip = v2::ReactionParser::ReactionFromSmarts(ChemicalReactionToRxnSmarts(*rxn, includeCX));
REQUIRE(roundtrip);
CHECK(getSubstanceGroups(*roundtrip->getReactants()[0]).size() == 2);
CHECK(getSubstanceGroups(*roundtrip->getProducts()[0]).size() == 2);
}
SECTION("link nodes") {
// clang-format off
Expand Down Expand Up @@ -1222,6 +1261,16 @@ TEST_CASE("CXSMILES for reactions", "[cxsmiles]") {
Bond::BondStereo::STEREOCIS);
CHECK(rxn->getProducts()[0]->getBondWithIdx(1)->getStereo() ==
Bond::BondStereo::STEREOCIS);

bool includeCX = true;
auto roundtrip = v2::ReactionParser::ReactionFromSmarts(ChemicalReactionToRxnSmarts(*rxn, includeCX));
REQUIRE(roundtrip);
CHECK(roundtrip->getReactants().size() == 1);
CHECK(roundtrip->getProducts().size() == 1);
CHECK(roundtrip->getReactants()[0]->getBondWithIdx(1)->getStereo() ==
Bond::BondStereo::STEREOCIS);
CHECK(roundtrip->getProducts()[0]->getBondWithIdx(1)->getStereo() ==
Bond::BondStereo::STEREOCIS);
}
SECTION("wedged bonds") {
auto rxn = "CC(O)(F)Cl>>CC(N)(F)Cl |w:1.0,6.5|"_rxnsmiles;
Expand All @@ -1235,6 +1284,19 @@ TEST_CASE("CXSMILES for reactions", "[cxsmiles]") {
CHECK(rxn->getProducts()[0]->getBondWithIdx(1)->getPropIfPresent(
"_MolFileBondCfg", bondcfg));
CHECK(bondcfg == 2);

bool includeCX = true;
auto roundtrip = v2::ReactionParser::ReactionFromSmarts(ChemicalReactionToRxnSmarts(*rxn, includeCX));
REQUIRE(roundtrip);
CHECK(roundtrip->getReactants().size() == 1);
CHECK(roundtrip->getProducts().size() == 1);
bondcfg = 0;
CHECK(roundtrip->getReactants()[0]->getBondWithIdx(0)->getPropIfPresent(
"_MolFileBondCfg", bondcfg));
CHECK(bondcfg == 2);
CHECK(roundtrip->getProducts()[0]->getBondWithIdx(1)->getPropIfPresent(
"_MolFileBondCfg", bondcfg));
CHECK(bondcfg == 2);
}
}

Expand Down