Skip to content

Instantly share code, notes, and snippets.

@n-yoshikawa
Created July 14, 2018 08:18
Show Gist options
  • Save n-yoshikawa/a70f997388d163eb85afcb5bd21ebe6c to your computer and use it in GitHub Desktop.
Save n-yoshikawa/a70f997388d163eb85afcb5bd21ebe6c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <sstream>
#include <openbabel/mol.h>
#include <openbabel/obconversion.h>
#include <openbabel/builder.h>
using namespace std;
using namespace OpenBabel;
int main(int argc, char** argv){
string smiles = "c1ccc2c(c1)ccc(c2c3c4ccccc4ccc3C(=O)O)C(=O)O";
stringstream ss(smiles);
OBConversion conv(&ss, &std::cout);
conv.SetInAndOutFormats("smi", "smi");
OBMol mol;
conv.Read(&mol);
// Get fragments using CopySubstructure
// Copy all atoms
OBBitVec atomsToCopy;
FOR_ATOMS_OF_MOL(atom, mol) {
atomsToCopy.SetBitOn(atom->GetIdx());
}
// Exclude rotatable bonds
OBBitVec bondsToExclude;
FOR_BONDS_OF_MOL(bond, mol) {
if (bond->IsRotor()) {
bondsToExclude.SetBitOn(bond->GetIdx());
}
}
// Generate fragments by copy
OBMol mol_copy;
mol.CopySubstructure(mol_copy, &atomsToCopy, &bondsToExclude);
// Separate each disconnected fragments as different molecules
vector<OBMol> fragments = mol_copy.Separate();
cout << "Original separation" << endl;
for(vector<OBMol>::iterator i=fragments.begin(); i!=fragments.end(); i++) {
cout << conv.WriteString(&*i, true) << endl;
}
OBMol mol2 = mol;
FOR_BONDS_OF_MOL(bond, mol2) {
if (bond->IsRotor()) {
mol2.DeleteBond(&*bond);
}
}
cout << "New separation" << endl;
fragments = mol2.Separate();
for(vector<OBMol>::iterator i=fragments.begin(); i!=fragments.end(); i++) {
cout << conv.WriteString(&*i, true) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment