// Helper function -- handle SMARTS selections // Called by performAction() void SelectExtension::selectSMARTS(GLWidget *widget) { bool ok; QString pattern = QInputDialog::getText(qobject_cast<QWidget*>(parent()), tr("SMARTS Selection"), tr("SMARTS pattern to select"), QLineEdit::Normal, "", &ok); if (ok && !pattern.isEmpty()) { OBSmartsPattern smarts; smarts.Init(pattern.toStdString()); OpenBabel::OBMol obmol = m_molecule->OBMol(); smarts.Match(obmol); // if we have matches, select them if(smarts.NumMatches() != 0) { QList<Primitive *> matchedAtoms; vector< vector <int> > mapList = smarts.GetUMapList(); vector< vector <int> >::iterator i; // a set of matching atoms vector<int>::iterator j; // atom ids in each match for (i = mapList.begin(); i != mapList.end(); ++i) { for (j = i->begin(); j != i->end(); ++j) { matchedAtoms.append(m_molecule->atom(obmol.GetAtom(*j)->GetIdx()-1)); } } widget->clearSelected(); widget->setSelected(matchedAtoms, true); widget->update(); } // end matches } return; }
int main() { OBAtom a, b, c; a.SetAtomicNum(8); b.SetAtomicNum(6); c.SetAtomicNum(8); OBMol mol; mol.AddAtom(a); mol.AddAtom(b); mol.AddAtom(c); mol.AddBond(1,2,2); mol.AddBond(2,3,2); OBConversion conv; conv.SetOutFormat("SMI"); cout << conv.WriteString(&mol,1) << endl; OBSmartsPattern sp; sp.Init ("C~*"); sp.Match (mol,false); cout << sp.NumMatches() << endl; cout << sp.GetUMapList().size() << endl; return EXIT_SUCCESS; }