FilterCatalogEntry *MakeFilterCatalogEntry(const FilterData_t &data, unsigned int num_props, const FilterProperty_t *props) { const int debugParse = 0; const bool mergeHs = true; ROMOL_SPTR pattern(SmartsToMol(data.smarts, debugParse, mergeHs)); if (!pattern.get()) return 0; // The filter has the concept of the maximum number of times the pattern is allowed // without triggering // The matcher has the concept of the minimum pattern count before the pattern // is triggered. // Hence pattern.minCount == filter.maxCount + 1 const unsigned int minCount = data.max ? data.max + 1 : 1; FilterCatalogEntry *entry = new FilterCatalogEntry( data.name, boost::shared_ptr<FilterMatcherBase>( new SmartsMatcher(data.name, pattern, minCount))); if (!entry->isValid()) { delete entry; return 0; } // add the props for(unsigned int i=0; i<num_props; i++) { std::string val(props[i].value); entry->getProps().setVal<std::string>(std::string(props[i].key), val); } return entry; }
ROMol *constructMolFromString(const std::string &txt, std::map<std::string, std::string> *replacements, bool useSmiles) { ROMol *mol; if (!useSmiles) { mol = SmartsToMol(txt, 0, false, replacements); } else { mol = SmilesToMol(txt, 0, false, replacements); } return mol; }
ROMol *MolFromSmarts(const char *smarts,bool mergeHs, python::dict replDict){ std::map<std::string,std::string> replacements; for(unsigned int i=0;i<python::extract<unsigned int>(replDict.keys().attr("__len__")());++i){ replacements[python::extract<std::string>(replDict.keys()[i])]=python::extract<std::string>(replDict.values()[i]); } RWMol *newM; try { newM = SmartsToMol(smarts,0,mergeHs,&replacements); } catch (...) { newM=0; } return static_cast<ROMol *>(newM); }
CrippenParamCollection::CrippenParamCollection(const std::string ¶mData) { std::string params; boost::char_separator<char> tabSep("\t", "", boost::keep_empty_tokens); if (paramData == "") params = defaultParamData; else params = paramData; std::istringstream inStream(params); std::string inLine = RDKit::getLine(inStream); unsigned int idx = 0; while (!inStream.eof()) { if (inLine[0] != '#') { CrippenParams paramObj; paramObj.idx = idx++; tokenizer tokens(inLine, tabSep); tokenizer::iterator token = tokens.begin(); paramObj.label = *token; ++token; paramObj.smarts = *token; ++token; if (*token != "") { paramObj.logp = boost::lexical_cast<double>(*token); } else { paramObj.logp = 0.0; } ++token; if (*token != "") { try { paramObj.mr = boost::lexical_cast<double>(*token); } catch (boost::bad_lexical_cast &) { paramObj.mr = 0.0; } } else { paramObj.mr = 0.0; } paramObj.dp_pattern = boost::shared_ptr<const ROMol>(SmartsToMol(paramObj.smarts)); d_params.push_back(paramObj); } inLine = RDKit::getLine(inStream); } }
void constructFragmenterBondTypes(std::istream *inStream, const std::map<unsigned int,std::string> &atomTypes, std::vector<FragmenterBondType> &defs, std::string comment,bool validate,bool labelByConnector){ PRECONDITION(inStream,"no stream"); defs.clear(); defs.resize(0); unsigned int line=0; while(!inStream->eof()){ ++line; std::string tempStr=getLine(inStream); if(tempStr=="" || tempStr.find(comment)==0 ) continue; std::vector<std::string> tokens; boost::split(tokens,tempStr,boost::is_any_of(" \t"),boost::token_compress_on); if(tokens.size()<3){ BOOST_LOG(rdWarningLog)<<"line "<<line<<" is too short"<<std::endl; continue; } unsigned int idx1=boost::lexical_cast<unsigned int>(tokens[0]); if(atomTypes.find(idx1)==atomTypes.end()){ BOOST_LOG(rdWarningLog)<<"atom type #"<<idx1<<" not recognized."<<std::endl; continue; } unsigned int idx2=boost::lexical_cast<unsigned int>(tokens[1]); if(atomTypes.find(idx2)==atomTypes.end()){ BOOST_LOG(rdWarningLog)<<"atom type #"<<idx2<<" not recognized."<<std::endl; continue; } std::string sma1=atomTypes.find(idx1)->second; std::string sma2=atomTypes.find(idx2)->second; std::string smarts="[$("+ sma1 +")]"+tokens[2]+"[$("+ sma2 +")]"; ROMol *p=SmartsToMol(smarts); if(validate){ if(!p){ BOOST_LOG(rdWarningLog)<<"cannot convert SMARTS "<<smarts<<" to molecule at line "<<line<<std::endl; continue; } } FragmenterBondType fbt; fbt.atom1Type=idx1; fbt.atom2Type=idx2; if(labelByConnector){ fbt.atom1Label=idx1; fbt.atom2Label=idx2; } else { fbt.atom1Label=idx2; fbt.atom2Label=idx1; } if(p){ // for the purposes of replacing the bond, we'll use just the first // character to set the bond type (if we recognize it): switch(tokens[2][0]){ case '-': fbt.bondType = Bond::SINGLE;break; case '=': fbt.bondType = Bond::DOUBLE;break; case '#': fbt.bondType = Bond::TRIPLE;break; case ':': fbt.bondType = Bond::AROMATIC;break; default: fbt.bondType = p->getBondWithIdx(0)->getBondType(); } fbt.query = ROMOL_SPTR(p); } else { fbt.bondType=Bond::UNSPECIFIED; fbt.query=ROMOL_SPTR(); } defs.push_back(fbt); } }
void addTorsions(const ROMol &mol, MMFFMolProperties *mmffMolProperties, ForceFields::ForceField *field, const std::string &torsionBondSmarts) { PRECONDITION(field, "bad ForceField"); PRECONDITION(mmffMolProperties, "bad MMFFMolProperties"); PRECONDITION(mmffMolProperties->isValid(), "missing atom types - invalid force-field"); std::ostream &oStream = mmffMolProperties->getMMFFOStream(); ROMol::ADJ_ITER nbr1Idx; ROMol::ADJ_ITER end1Nbrs; ROMol::ADJ_ITER nbr2Idx; ROMol::ADJ_ITER end2Nbrs; double totalTorsionEnergy = 0.0; RDGeom::PointPtrVect points; if (mmffMolProperties->getMMFFVerbosity()) { if (mmffMolProperties->getMMFFVerbosity() == MMFF_VERBOSITY_HIGH) { oStream << "\n" "T O R S I O N A L\n\n" "--------------ATOMS--------------- ---ATOM TYPES--- " "FF TORSION -----FORCE Params-----\n" " I J K L I J K L " "CLASS ANGLE ENERGY V1 V2 V3\n" "-------------------------------------------------------------" "-----------------------------------------------------" << std::endl; } points = field->positions(); } std::vector<MatchVectType> matchVect; const ROMol *defaultQuery = DefaultTorsionBondSmarts::query(); const ROMol *query = (torsionBondSmarts == DefaultTorsionBondSmarts::string()) ? defaultQuery : SmartsToMol(torsionBondSmarts); TEST_ASSERT(query); unsigned int nHits = SubstructMatch(mol, *query, matchVect); if (query != defaultQuery) delete query; for (unsigned int i = 0; i < nHits; ++i) { MatchVectType match = matchVect[i]; TEST_ASSERT(match.size() == 2); int idx2 = match[0].second; int idx3 = match[1].second; const Bond *bond = mol.getBondBetweenAtoms(idx2, idx3); TEST_ASSERT(bond); const Atom *jAtom = mol.getAtomWithIdx(idx2); const Atom *kAtom = mol.getAtomWithIdx(idx3); if (((jAtom->getHybridization() == Atom::SP2) || (jAtom->getHybridization() == Atom::SP3)) && ((kAtom->getHybridization() == Atom::SP2) || (kAtom->getHybridization() == Atom::SP3))) { ROMol::OEDGE_ITER beg1, end1; boost::tie(beg1, end1) = mol.getAtomBonds(jAtom); while (beg1 != end1) { const Bond *tBond1 = mol[*beg1].get(); if (tBond1 != bond) { int idx1 = tBond1->getOtherAtomIdx(idx2); ROMol::OEDGE_ITER beg2, end2; boost::tie(beg2, end2) = mol.getAtomBonds(kAtom); while (beg2 != end2) { const Bond *tBond2 = mol[*beg2].get(); if ((tBond2 != bond) && (tBond2 != tBond1)) { int idx4 = tBond2->getOtherAtomIdx(idx3); // make sure this isn't a three-membered ring: if (idx4 != idx1) { // we now have a torsion involving atoms (bonds): // bIdx - (tBond1) - idx1 - (bond) - idx2 - (tBond2) - eIdx unsigned int torType; MMFFTor mmffTorParams; if (mmffMolProperties->getMMFFTorsionParams( mol, idx1, idx2, idx3, idx4, torType, mmffTorParams)) { TorsionAngleContrib *contrib = new TorsionAngleContrib( field, idx1, idx2, idx3, idx4, &mmffTorParams); field->contribs().push_back(ForceFields::ContribPtr(contrib)); if (mmffMolProperties->getMMFFVerbosity()) { const Atom *iAtom = mol.getAtomWithIdx(idx1); const Atom *lAtom = mol.getAtomWithIdx(idx4); unsigned int iAtomType = mmffMolProperties->getMMFFAtomType(idx1); unsigned int jAtomType = mmffMolProperties->getMMFFAtomType(idx2); unsigned int kAtomType = mmffMolProperties->getMMFFAtomType(idx3); unsigned int lAtomType = mmffMolProperties->getMMFFAtomType(idx4); const RDGeom::Point3D p1((*(points[idx1]))[0], (*(points[idx1]))[1], (*(points[idx1]))[2]); const RDGeom::Point3D p2((*(points[idx2]))[0], (*(points[idx2]))[1], (*(points[idx2]))[2]); const RDGeom::Point3D p3((*(points[idx3]))[0], (*(points[idx3]))[1], (*(points[idx3]))[2]); const RDGeom::Point3D p4((*(points[idx4]))[0], (*(points[idx4]))[1], (*(points[idx4]))[2]); const double cosPhi = MMFF::Utils::calcTorsionCosPhi(p1, p2, p3, p4); const double torsionEnergy = MMFF::Utils::calcTorsionEnergy( mmffTorParams.V1, mmffTorParams.V2, mmffTorParams.V3, cosPhi); if (mmffMolProperties->getMMFFVerbosity() == MMFF_VERBOSITY_HIGH) { oStream << std::left << std::setw(2) << iAtom->getSymbol() << " #" << std::setw(5) << idx1 + 1 << std::setw(2) << jAtom->getSymbol() << " #" << std::setw(5) << idx2 + 1 << std::setw(2) << kAtom->getSymbol() << " #" << std::setw(5) << idx3 + 1 << std::setw(2) << lAtom->getSymbol() << " #" << std::setw(5) << idx4 + 1 << std::right << std::setw(5) << iAtomType << std::setw(5) << jAtomType << std::setw(5) << kAtomType << std::setw(5) << lAtomType << std::setw(6) << torType << " " << std::fixed << std::setprecision(3) << std::setw(10) << RAD2DEG * acos(cosPhi) << std::setw(10) << torsionEnergy << std::setw(10) << mmffTorParams.V1 << std::setw(10) << mmffTorParams.V2 << std::setw(10) << mmffTorParams.V3 << std::endl; } totalTorsionEnergy += torsionEnergy; } } } } beg2++; } } beg1++; } } } if (mmffMolProperties->getMMFFVerbosity()) { if (mmffMolProperties->getMMFFVerbosity() == MMFF_VERBOSITY_HIGH) { oStream << std::endl; } oStream << "TOTAL TORSIONAL ENERGY =" << std::right << std::setw(16) << std::fixed << std::setprecision(4) << totalTorsionEnergy << std::endl; } }
void DefaultTorsionBondSmarts::create() { ds_instance.reset(SmartsToMol(ds_string)); }
// ------------------------------------------------------------------------ // // // // ------------------------------------------------------------------------ void addTorsions(const ROMol &mol,const AtomicParamVect ¶ms, ForceFields::ForceField *field, std::string torsionBondSmarts){ PRECONDITION(mol.getNumAtoms()==params.size(),"bad parameters"); PRECONDITION(field,"bad forcefield"); // find all of the torsion bonds: std::vector<MatchVectType> matchVect; ROMol *query=SmartsToMol(torsionBondSmarts); TEST_ASSERT(query); unsigned int nHits=SubstructMatch(mol,*query,matchVect); delete query; for(unsigned int i=0; i<nHits; i++){ MatchVectType match=matchVect[i]; TEST_ASSERT(match.size()==2); int idx1=match[0].second; int idx2=match[1].second; if(!params[idx1]||!params[idx2]) continue; const Bond *bond=mol.getBondBetweenAtoms(idx1,idx2); std::vector<TorsionAngleContrib *> contribsHere; TEST_ASSERT(bond); const Atom *atom1=mol.getAtomWithIdx(idx1); const Atom *atom2=mol.getAtomWithIdx(idx2); if( (atom1->getHybridization()==Atom::SP2||atom1->getHybridization()==Atom::SP3) && (atom2->getHybridization()==Atom::SP2||atom2->getHybridization()==Atom::SP3) ){ ROMol::OEDGE_ITER beg1,end1; boost::tie(beg1,end1) = mol.getAtomBonds(atom1); while(beg1!=end1){ const Bond *tBond1=mol[*beg1].get(); if(tBond1!=bond){ int bIdx = tBond1->getOtherAtomIdx(idx1); ROMol::OEDGE_ITER beg2,end2; boost::tie(beg2,end2) = mol.getAtomBonds(atom2); while(beg2 != end2){ const Bond *tBond2=mol[*beg2].get(); if(tBond2!=bond && tBond2!=tBond1){ int eIdx=tBond2->getOtherAtomIdx(idx2); // make sure this isn't a three-membered ring: if(eIdx != bIdx){ // we now have a torsion involving atoms (bonds): // bIdx - (tBond1) - idx1 - (bond) - idx2 - (tBond2) - eIdx TorsionAngleContrib *contrib; // if either of the end atoms is SP2 hybridized, set a flag // here. bool hasSP2=false; if(mol.getAtomWithIdx(bIdx)->getHybridization()==Atom::SP2 || mol.getAtomWithIdx(bIdx)->getHybridization()==Atom::SP2) { hasSP2 = true; } //std::cout << "Torsion: " << bIdx << "-" << idx1 << "-" << idx2 << "-" << eIdx << std::endl; //if(okToIncludeTorsion(mol,bond,bIdx,idx1,idx2,eIdx)){ //std::cout << " INCLUDED" << std::endl; contrib = new TorsionAngleContrib(field,bIdx,idx1,idx2,eIdx, bond->getBondTypeAsDouble(), atom1->getAtomicNum(), atom2->getAtomicNum(), atom1->getHybridization(), atom2->getHybridization(), params[idx1],params[idx2], hasSP2); field->contribs().push_back(ForceFields::ContribPtr(contrib)); contribsHere.push_back(contrib); //} } } beg2++; } } beg1++; } } // now divide the force constant for each contribution to the torsion energy // about this bond by the number of contribs about this bond: for(std::vector<TorsionAngleContrib *>::iterator chI=contribsHere.begin(); chI!=contribsHere.end();++chI){ (*chI)->scaleForceConstant(contribsHere.size()); } } }
bool fragmentMol(const ROMol& mol, std::vector<std::pair<ROMOL_SPTR, ROMOL_SPTR> >& res, unsigned int minCuts, unsigned int maxCuts, unsigned int maxCutBonds, const std::string& pattern) { #ifdef _DEBUG for (size_t i = 0; i < mol.getNumAtoms(); i++) { std::string symbol = mol.getAtomWithIdx(i)->getSymbol(); int label = 0; mol.getAtomWithIdx(i)->getPropIfPresent(common_properties::molAtomMapNumber, label); char a1[32]; if (0 == label) sprintf(a1, "\'%s\'", symbol.c_str(), label); else sprintf(a1, "\'%s:%u\'", symbol.c_str(), label); std::cout << "Atom " << i << ": " << a1; //<<" Bonds:"; std::cout << "\n"; } #endif res.clear(); std::auto_ptr<const ROMol> smarts((const ROMol*)SmartsToMol(pattern)); std::vector<MatchVectType> matching_atoms; // one bond per match ! with default pattern unsigned int total = SubstructMatch(mol, *smarts, matching_atoms); #ifdef _DEBUG std::cout << "total substructs =" << total << "\nmatching bonds (atom1, atom2):\n"; #endif if (0 == total) // Not found. Return empty set of molecules return false; #ifdef _DEBUG for (size_t i = 0; i < matching_atoms.size(); i++) { std::string symbol = mol.getAtomWithIdx(matching_atoms[i][0].second)->getSymbol(); int label = 0; mol.getAtomWithIdx(matching_atoms[i][0].second) ->getPropIfPresent(common_properties::molAtomMapNumber, label); char a1[32]; if (0 == label) sprintf(a1, "\'%s\'", symbol.c_str(), label); else sprintf(a1, "\'%s:%u\'", symbol.c_str(), label); symbol = mol.getAtomWithIdx(matching_atoms[i][1].second)->getSymbol(); label = 0; mol.getAtomWithIdx(matching_atoms[i][1].second) ->getPropIfPresent(common_properties::molAtomMapNumber, label); char a2[32]; if (0 == label) sprintf(a2, "\'%s\'", symbol.c_str(), label); else sprintf(a2, "\'%s:%u\'", symbol.c_str(), label); std::cout << i << ": (" << matching_atoms[i][0].second << a1 << "," << matching_atoms[i][1].second << a2 << ") \n"; } #endif std::vector<BondVector_t> matching_bonds; // List of matched query's bonds convertMatchingToBondVect(matching_bonds, matching_atoms, mol); if (matching_bonds.size() > maxCutBonds) return false; #ifdef _DEBUG std::cout << "total matching_bonds = " << matching_bonds.size() << "\n"; #endif // loop to generate every cut in the molecule BondVector_t bonds_selected; processCuts(0, minCuts, maxCuts, bonds_selected, matching_bonds, mol, res); return true; }