Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created January 26, 2026 08:31
Show Gist options
  • Select an option

  • Save romanbsd/f51ef41853b5c26e062ee2650589fe3e to your computer and use it in GitHub Desktop.

Select an option

Save romanbsd/f51ef41853b5c26e062ee2650589fe3e to your computer and use it in GitHub Desktop.
GenerateCoordinatesForReaction in OpenBabel
#include <openbabel/mol.h>
#include <openbabel/op.h>
#include <openbabel/reactionfacade.h>
bool GenerateCoordinatesForReaction(OBMol* pmol, bool use3D = false) {
if (!pmol->IsReaction())
return false;
// Find the coordinate generation operation
OBOp* pOp = OBOp::FindType(use3D ? "gen3D" : "gen2D");
if (!pOp)
return false;
OBReactionFacade facade(pmol);
// Collect all components with their roles
std::vector<std::pair<OBMol, OBReactionRole>> components;
OBReactionRole roles[] = {REACTANT, PRODUCT, AGENT};
for (OBReactionRole role : roles) {
for (unsigned int i = 0; i < facade.NumComponents(role); i++) {
OBMol mol;
if (facade.GetComponent(&mol, role, i)) {
// Generate coordinates for this component
pOp->Do(&mol);
components.push_back({mol, role});
}
}
}
// Rebuild the reaction with updated coordinates
std::string title = pmol->GetTitle();
pmol->Clear();
pmol->SetIsReaction();
// Create a new facade for the cleared mol
OBReactionFacade newFacade(pmol);
for (auto& [mol, role] : components) {
newFacade.AddComponent(&mol, role);
}
if (!title.empty())
pmol->SetTitle(title.c_str());
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment