Example #1
0
std::string LogNeighbourhood(
    const ROMol &mol, unsigned int idx,
    const std::vector<Neighbourhood> &neighbour_array) {
  std::stringstream oss;
  // FIX ME turn into utility func?
  std::string name("");
  mol.getPropIfPresent(common_properties::_Name, name);
  oss << "atom '" << name << "' idx=" << idx << " AA: ";
  const Atom &atm = *mol.getAtomWithIdx(idx);
  oss << atm.getSymbol();

  if (atm.getFormalCharge())
    oss << (atm.getFormalCharge() > 0 ? "+" : "") << atm.getFormalCharge();

  if (atm.getNumRadicalElectrons()) oss << atm.getNumRadicalElectrons();

  // these neighbors should be sorted properly?
  size_t numNbrs = neighbour_array[idx].Atoms.size();
  // CHECK_INVARIANT(numNBrs == neighbour_array[idx].Bonds.size());

  std::vector<NbrData> nbrs;

  for (size_t i = 0; i < numNbrs; ++i) {
    const Bond *bond = mol.getBondWithIdx(neighbour_array[idx].Bonds[i]);
    const Atom &nbr = *mol.getAtomWithIdx(neighbour_array[idx].Atoms[i]);
    nbrs.push_back(NbrData(nbr.getSymbol(), bond->getBondType(),
                           nbr.getFormalCharge(),
                           nbr.getNumRadicalElectrons()));
  }

  std::sort(nbrs.begin(), nbrs.end(), lessTuple);
  for (size_t i = 0; i < nbrs.size(); ++i) {
    NbrData &nbr = nbrs[i];
    std::string bs = "";
    switch (nbr.get<1>()) {
      case Bond::SINGLE:
        bs = "-";
        break;
      case Bond::DOUBLE:
        bs = "=";
        break;
      case Bond::TRIPLE:
        bs = "#";
        break;
      case Bond::AROMATIC:
        bs = "~";
        break;
    }
    if (bs.size())
      oss << "(" << bs << nbr.get<0>();
    else
      oss << "("
          << "?" << (int)nbr.get<1>() << "?" << nbr.get<0>();
    if (nbr.get<2>()) oss << (nbr.get<2>() > 0 ? "+" : "") << nbr.get<2>();

    if (nbr.get<3>()) oss << nbr.get<3>();
    oss << ")";
  }
  return oss.str();
}
Example #2
0
void test5() {
  std::cout << " ----------------- Test 5" << std::endl;
  {
    std::string smiles = "*c1cc(*)cc(*)c1";
    std::string nameBase = "test5_1";
    ROMol *m = SmilesToMol(smiles);
    TEST_ASSERT(m);
    RDDepict::compute2DCoords(*m);
    WedgeMolBonds(*m, &(m->getConformer()));
    MolDrawOptions options;
    options.dummiesAreAttachments = true;
    options.atomLabels[0] = "R1";
#ifdef RDK_CAIRO_BUILD
    {
      MolDraw2DCairo drawer(300, 300);
      drawer.drawOptions() = options;
      drawer.drawMolecule(*m);
      drawer.finishDrawing();
      drawer.writeDrawingText(nameBase + ".png");
    }
#endif
    {
      std::ofstream outs((nameBase + ".svg").c_str());
      MolDraw2DSVG drawer(300, 300, outs);
      drawer.drawOptions() = options;
      drawer.drawMolecule(*m);
      drawer.finishDrawing();
      outs.flush();
    }
    delete m;
  }
  std::cout << " Done" << std::endl;
}
Example #3
0
void Seed::fillNewBonds(const ROMol& qmol) {
  std::vector<bool> excludedBonds = ExcludedBonds;
  for (unsigned srcAtomIdx = LastAddedAtomsBeginIdx; srcAtomIdx < getNumAtoms();
       srcAtomIdx++) {  // all atoms added on previous growing only
    const Atom* atom = MoleculeFragment.Atoms[srcAtomIdx];
    ROMol::OEDGE_ITER beg, end;
    for (boost::tie(beg, end) = qmol.getAtomBonds(atom); beg != end;
         beg++) {  // all bonds from MoleculeFragment.Atoms[srcAtomIdx]
      const Bond* bond = &*(qmol[*beg]);
      if (!excludedBonds[bond->getIdx()]) {
        // already in the seed or NewBonds list from another atom in a RING
        excludedBonds[bond->getIdx()] = true;
        unsigned ai = (atom == bond->getBeginAtom()) ? bond->getEndAtomIdx()
                                                     : bond->getBeginAtomIdx();
        const Atom* end_atom = qmol.getAtomWithIdx(ai);
        unsigned end_atom_idx = NotSet;
        for (unsigned i = 0; i < getNumAtoms(); i++)
          if (end_atom ==
              MoleculeFragment.Atoms[i]) {  // already exists in this seed
            end_atom_idx = i;
            break;
          }
        NewBonds.push_back(NewBond(srcAtomIdx, bond->getIdx(), ai, end_atom_idx,
                                   NotSet == end_atom_idx ? end_atom : 0));
      }
    }
  }
}
Example #4
0
    void calcCrippenDescriptors(const ROMol &mol,double &logp,double &mr,bool includeHs,
				bool force){
      if(!force && mol.hasProp("_crippenLogP")){
	mol.getProp("_crippenLogP",logp);
	mol.getProp("_crippenMR",mr);
	return;
      }

      // this isn't as bad as it looks, we aren't actually going
      // to harm the molecule in any way!
      ROMol *workMol=const_cast<ROMol *>(&mol);
      if(includeHs){
	workMol = MolOps::addHs(mol,false,false);
      }
      std::vector<double> logpContribs(workMol->getNumAtoms());
      std::vector<double> mrContribs(workMol->getNumAtoms());
      getCrippenAtomContribs(*workMol,logpContribs,mrContribs,force);
      logp=0.0;
      for(std::vector<double>::const_iterator iter=logpContribs.begin();
	  iter!=logpContribs.end();++iter){
	logp+= *iter;
      }
      mr=0.0;
      for(std::vector<double>::const_iterator iter=mrContribs.begin();
	  iter!=mrContribs.end();++iter){
	mr+= *iter;
      }

      if(includeHs){
	delete workMol;
      }

      mol.setProp("_crippenLogP",logp,true);
      mol.setProp("_crippenMR",mr,true);
    };
Example #5
0
  //*************************************************************************************
  //
  //  Adds 2D coordinates to a molecule using the Depict.dll
  //
  //  ARGUMENTS:
  //    mol:          the molecule to be altered
  //    tempFilename: (OPTIONAL) the name of the temporary file 
  //
  //  RETURNS:
  //   1 on success, 0 otherwise
  //
  //  Here's the process by which this works (it's kind of contorted):
  //   1) convert the mol to SMILES
  //   2) use the DLL to convert the SMILES to a mol file (on disk)
  //   3) parse the mol file into a temporary molecule
  //   4) do a substructure match from the old molecule to the
  //      temp one (which may have a different atom numbering or additional
  //      atoms added).
  //   5) Update the positions of the atoms on the old molecule.
  //   6) Free the temp molecule.
  //
  //  NOTES:
  //   - *FIX:* at the moment we're not doing anything to clear up the
  //     temp file created in this process.  It'll always have the same
  //     name unless the user explicitly asks that we do something different.
  //   - To use the DLL, it's essential that the COMBICHEM_ROOT and COMBICHEM_RELEASE
  //     environment variables be set.  If this isn't done, this whole process
  //     will fail.
  //   - See the notes above about failures when opening the DLL.
  //
  //*************************************************************************************
  int Add2DCoordsToMolDLL(ROMol &mol,std::string tempFilename){
    std::string smi=MolToSmiles(mol,true);
    int tmp = SmilesToMolFileDLL(smi,tempFilename);
    int res = -1;
    if(tmp){
      // build another mol from that mol file:
      RWMol *tmpMol = MolFileToMol(tempFilename,false);
      // match it up with the starting mol:
      //  (We need to do this because the depict.dll conversion
      //   to a mol file may have added Hs)
      MatchVectType matchVect;
      bool hasMatch=SubstructMatch(tmpMol,&mol,matchVect);
      if(hasMatch){
        const Conformer &conf = tmpMol->getCoformer(0);
        Coformer *nconf = new Coformer(mol.getNumAtoms());
	for(MatchVectType::const_iterator mvi=matchVect.begin();
	    mvi!=matchVect.end();mvi++){
	  
          nconf->setAtomPos(conf.getAtomPos(mvi->first));
	}
        confId = (int)mol.addConformer(nconf, true);
      }
      delete tmpMol;
    }
    return res;
  }
Example #6
0
    INT_LIST getShortestPath(const ROMol &mol, int aid1, int aid2) {

      int nats = mol.getNumAtoms();
      RANGE_CHECK(0,aid1,nats-1);
      RANGE_CHECK(0,aid2,nats-1);
      CHECK_INVARIANT(aid1 != aid2, "");

      INT_VECT pred, doneAtms;
  
      //pred.reserve(nats);
      //doneAtms.reserve(nats);
      pred.resize(nats);
      doneAtms.resize(nats);
      int ai;
      for (ai = 0; ai < nats; ++ai) {
	doneAtms[ai] = 0;
      }

      std::deque<int> bfsQ;
  
      bfsQ.push_back(aid1);
      bool done = false;
      ROMol::ADJ_ITER nbrIdx,endNbrs;
      while ((!done) && (bfsQ.size() > 0)) {
	int curAid = bfsQ.front();
	boost::tie(nbrIdx,endNbrs) = mol.getAtomNeighbors(mol.getAtomWithIdx(curAid));
	while (nbrIdx != endNbrs) {
	  if (doneAtms[*nbrIdx] == 0) {
	    pred[*nbrIdx] = curAid;
	    if (static_cast<int>(*nbrIdx) == aid2) {
	      done = true;
	      break;
	    }
	    bfsQ.push_back(*nbrIdx);
	  }
	  nbrIdx++;
	}
	doneAtms[curAid] = 1;
	bfsQ.pop_front();
      }

      INT_LIST res;
      if(done){
        done = false;
        int prev = aid2;
        res.push_back(aid2);
        while (!done) {
          prev = pred[prev];
          if (prev != aid1) {
            res.push_front(prev);
          } else {
            done = true;
          }
        }
        res.push_front(aid1);
      }  
      return res;
 
    }
Example #7
0
void alignMolConformers(ROMol &mol, const std::vector<unsigned int> *atomIds,
                        const std::vector<unsigned int> *confIds,
                        const RDNumeric::DoubleVector *weights, bool reflect,
                        unsigned int maxIters, std::vector<double> *RMSlist) {
  if (mol.getNumConformers() == 0) {
    // nothing to be done ;
    return;
  }

  RDGeom::Point3DConstPtrVect refPoints, prbPoints;
  int cid = -1;
  if ((confIds != 0) && (confIds->size() > 0)) {
    cid = confIds->front();
  }
  const Conformer &refCnf = mol.getConformer(cid);
  _fillAtomPositions(refPoints, refCnf, atomIds);

  // now loop throught the remaininf conformations and transform them
  RDGeom::Transform3D trans;
  double ssd;
  if (confIds == 0) {
    unsigned int i = 0;
    ROMol::ConformerIterator cnfi;
    // Conformer *conf;
    for (cnfi = mol.beginConformers(); cnfi != mol.endConformers(); cnfi++) {
      // conf = (*cnfi);
      i += 1;
      if (i == 1) {
        continue;
      }
      _fillAtomPositions(prbPoints, *(*cnfi), atomIds);
      ssd = RDNumeric::Alignments::AlignPoints(refPoints, prbPoints, trans,
                                               weights, reflect, maxIters);
      if (RMSlist) {
        ssd /= (prbPoints.size());
        RMSlist->push_back(sqrt(ssd));
      }
      MolTransforms::transformConformer(*(*cnfi), trans);
    }
  } else {
    std::vector<unsigned int>::const_iterator cai;
    unsigned int i = 0;
    for (cai = confIds->begin(); cai != confIds->end(); cai++) {
      i += 1;
      if (i == 1) {
        continue;
      }
      Conformer &conf = mol.getConformer(*cai);
      _fillAtomPositions(prbPoints, conf, atomIds);
      ssd = RDNumeric::Alignments::AlignPoints(refPoints, prbPoints, trans,
                                               weights, reflect, maxIters);
      if (RMSlist) {
        ssd /= (prbPoints.size());
        RMSlist->push_back(sqrt(ssd));
      }
      MolTransforms::transformConformer(conf, trans);
    }
  }
}
Example #8
0
 bool MCSAtomCompareIsotopes (const MCSAtomCompareParameters& p, const ROMol& mol1, unsigned int atom1, const ROMol& mol2, unsigned int atom2, void* ud) {
     // ignore everything except isotope information:
     //if( ! MCSAtomCompareElements (p, mol1, atom1, mol2, atom2, ud))
     //    return false;
     const Atom& a1 = *mol1.getAtomWithIdx(atom1);
     const Atom& a2 = *mol2.getAtomWithIdx(atom2);
     return a1.getIsotope() == a2.getIsotope();
 }
Example #9
0
bool classifyAtoms(ROMol &mol, std::vector<double> &radii,
                   const SASAOpts &opts) {
  radii.clear();
  const freesasa_classifier *classifier = nullptr;
  switch (opts.classifier) {
    case SASAOpts::Protor:
      classifier = &freesasa_protor_classifier;
      break;
    case SASAOpts::NACCESS:
      classifier = &freesasa_naccess_classifier;
      break;
    case SASAOpts::OONS:
      classifier = &freesasa_oons_classifier;
      break;
    default:
      throw ValueErrorException("unknown FreeSASA classifier specified");
      return false;
  }

  bool success = true;
  for (ROMol::AtomIterator at = mol.beginAtoms(); at != mol.endAtoms(); ++at) {
    Atom *atom = *at;
    freesasa_atom_class cls = FREESASA_ATOM_UNKNOWN;
    std::string classification = "Unclassified";
    double radius = 0.0;

    const AtomMonomerInfo *info = atom->getMonomerInfo();
    if (info) {
      const char *atom_name = info->getName().c_str();
      const char *res_name = nullptr;

      if (info->getMonomerType() == AtomMonomerInfo::PDBRESIDUE) {
        res_name = ((AtomPDBResidueInfo *)info)->getResidueName().c_str();
        radius = freesasa_classifier_radius(classifier, res_name, atom_name);

        if (radius == 0.0) {
          BOOST_LOG(rdWarningLog)
              << "Atom " << atom->getIdx() << " has zero radius" << std::endl;
        }

        cls = freesasa_classifier_class(classifier, res_name, atom_name);
        if (cls == FREESASA_ATOM_UNKNOWN) {
          BOOST_LOG(rdWarningLog) << "Atom " << atom->getIdx()
                                  << " could not be classified" << std::endl;
          success = false;
        } else {
          classification = freesasa_classifier_class2str(cls);
        }
      }
    }

    radii.push_back(radius);
    atom->setProp<int>(common_properties::Atom::SASAClass, (int)cls);
    atom->setProp(common_properties::Atom::SASAClassName, classification);
  }

  return success;
}
Example #10
0
void test6() {
  BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
  BOOST_LOG(rdErrorLog) << "Feature Location testing." << std::endl;

  ROMol *testMol;
  Conformer *conf;
  std::string inText;
  FeatSPtrList featSPtrs;
  boost::shared_ptr<MolChemicalFeature> featSPtr;

  MolChemicalFeatureFactory *factory;

  MolChemicalFeatureDef::CollectionType::value_type featDef;

  inText =
      "DefineFeature HDonor1 [N,O;!H0]\n"
      "  Family HBondDonor\n"
      "  Weights 1.0\n"
      "EndFeature\n"
      "DefineFeature Carboxyl1 C(=O)[O;H1,-]\n"
      "  Family ZnBinder\n"
      "  Weights 1.0,1.0,1.0\n"
      "EndFeature\n";

  factory = buildFeatureFactory(inText);
  TEST_ASSERT(factory);
  TEST_ASSERT(factory->getNumFeatureDefs() == 2);

  testMol = SmilesToMol("C(=O)O");
  TEST_ASSERT(testMol);
  conf = new Conformer(3);
  testMol->addConformer(conf);
  conf->setAtomPos(0, RDGeom::Point3D(0, 0, 0.0));
  conf->setAtomPos(1, RDGeom::Point3D(1.2, 0, 0.0));
  conf->setAtomPos(2, RDGeom::Point3D(0, 1.5, 0.0));

  featSPtrs = factory->getFeaturesForMol(*testMol);
  TEST_ASSERT(featSPtrs.size() == 2);
  featSPtr = *featSPtrs.begin();
  TEST_ASSERT(featSPtr->getFamily() == "HBondDonor");
  TEST_ASSERT(featSPtr->getType() == "HDonor1");
  TEST_ASSERT(feq(featSPtr->getPos().x, 0.0));
  TEST_ASSERT(feq(featSPtr->getPos().y, 1.5));
  TEST_ASSERT(feq(featSPtr->getPos().z, 0.0));

  featSPtr = *(++featSPtrs.begin());
  TEST_ASSERT(featSPtr->getFamily() == "ZnBinder");
  TEST_ASSERT(featSPtr->getType() == "Carboxyl1");
  TEST_ASSERT(feq(featSPtr->getPos().x, 0.4));
  TEST_ASSERT(feq(featSPtr->getPos().y, 0.5));
  TEST_ASSERT(feq(featSPtr->getPos().z, 0.0));
  delete testMol;

  delete factory;

  BOOST_LOG(rdErrorLog) << "  done" << std::endl;
}
Example #11
0
 bool MCSAtomCompareElements (const MCSAtomCompareParameters& p, const ROMol& mol1, unsigned int atom1, const ROMol& mol2, unsigned int atom2, void* ) {
     const Atom& a1 = *mol1.getAtomWithIdx(atom1);
     const Atom& a2 = *mol2.getAtomWithIdx(atom2);
     if(a1.getAtomicNum() != a2.getAtomicNum())
       return false;
     if(p.MatchValences && a1.getTotalValence() != a2.getTotalValence())
         return false;
     return true;
 }
Example #12
0
void testSDMemoryCorruption() {
  std::string rdbase = getenv("RDBASE");
  std::string fname = rdbase + "/Data/NCI/first_200.props.sdf";
  SDMolSupplier sdsup(fname,true);
  std::string ofile = rdbase + "/Code/GraphMol/FileParsers/test_data/outNCI_first_200.props.sdf";
  std::ostream *os=new std::ofstream(ofile.c_str());
  //std::ostream *os=new std::stringstream();
  SDWriter *writer = new SDWriter(os,false);

  STR_VECT names;
#if 1
  ROMol *m1=sdsup.next();
  MolOps::sanitizeMol(*(RWMol *)m1);
#else
  ROMol *m1=SmilesToMol("C1CC1");
  TEST_ASSERT(m1);
#endif
  sdsup.reset();
  int nDone=0;
  while (!sdsup.atEnd()) {
    //std::cerr<<nDone<<std::endl;
    ROMol *mol = sdsup.next();
    //std::cerr<<"m:"<<mol<<std::endl;
    TEST_ASSERT(mol);
    std::string mname;
    mol->getProp("_Name", mname);
    names.push_back(mname);
    //std::cerr<<"  w"<<std::endl;
    writer->write(*mol);

    //std::cerr<<"  ok"<<std::endl;
    
    delete mol;
    nDone++;
  }
  CHECK_INVARIANT(nDone == 200, "");
  writer->flush();
  CHECK_INVARIANT(writer->numMols() == 200, "");

  delete writer;
#if 1
  // now read in the file we just finished writing
  SDMolSupplier reader(ofile);
  int i = 0;
  while (!reader.atEnd()) {
    ROMol *mol = reader.next();
    std::string mname;
    mol->getProp("_Name", mname);
    CHECK_INVARIANT(mname == names[i], "");

    
    delete mol;
    i++;
  }
#endif
}
Example #13
0
void ClearSingleBondDirFlags(ROMol &mol) {
    for (RWMol::BondIterator bondIt = mol.beginBonds(); bondIt != mol.endBonds();
            ++bondIt) {
        if ((*bondIt)->getBondType() == Bond::SINGLE) {
            if ((*bondIt)->getBondDir() == Bond::UNKNOWN)
                (*bondIt)->setProp(common_properties::_UnknownStereo, 1);
            (*bondIt)->setBondDir(Bond::NONE);
        }
    }
}
Example #14
0
double tverskyIndex(const ROMol &mol1, const ROMol &mol2, double alpha, double beta, int confId1,
                        int confId2, double gridSpacing,
                        DiscreteValueVect::DiscreteValueType bitsPerPoint,
                        double vdwScale, double stepSize, int maxLayers,
                        bool ignoreHs) {
  const Conformer &conf1 = mol1.getConformer(confId1);
  const Conformer &conf2 = mol2.getConformer(confId2);
  return tverskyIndex(conf1, conf2, alpha, beta, gridSpacing, bitsPerPoint,
                          vdwScale, stepSize, maxLayers, ignoreHs);
}
Example #15
0
double tanimotoDistance(const ROMol &mol1, const ROMol &mol2, int confId1,
                        int confId2, double gridSpacing,
                        DiscreteValueVect::DiscreteValueType bitsPerPoint,
                        double vdwScale, double stepSize, int maxLayers,
                        bool ignoreHs) {
  const Conformer &conf1 = mol1.getConformer(confId1);
  const Conformer &conf2 = mol2.getConformer(confId2);
  return tanimotoDistance(conf1, conf2, gridSpacing = 0.5, bitsPerPoint,
                          vdwScale, stepSize, maxLayers, ignoreHs);
}
Example #16
0
File: EEM.cpp Project: giallu/rdkit
EEM_arrays::EEM_arrays(const ROMol &mol, unsigned int sz) : n(sz) {
  /* Fill vector b i.e. -A */
  Atomindex = new unsigned int[n];
  EEMatomtype = new unsigned int[n];

  for (unsigned int j = 0; j < n; j++) {
    EEMatomtype[j] = getAtomtype(mol, mol.getAtomWithIdx(j));
    Atomindex[j] = mol.getAtomWithIdx(j)->getAtomicNum();
  }
}
Example #17
0
void testSDWriterStrm() {
  std::string rdbase = getenv("RDBASE");
  {
    std::string fname = rdbase + "/Code/GraphMol/FileParsers/test_data/NCI_aids_few.sdf";
    SDMolSupplier sdsup(fname);
  
    std::string ofile = rdbase + "/Code/GraphMol/FileParsers/test_data/outNCI_few.sdf";
    std::ofstream *oStream=new std::ofstream(ofile.c_str());

    SDWriter *writer = new SDWriter(oStream);

    STR_VECT names;
 
    while (!sdsup.atEnd()) {
      ROMol *mol = sdsup.next();
      std::string mname;
      mol->getProp("_Name", mname);
      names.push_back(mname);

      writer->write(*mol);
      delete mol;
    }
    writer->flush();
    CHECK_INVARIANT(writer->numMols() == 16, "");
    delete writer;

    // now read in the file we just finished writing
    SDMolSupplier reader(ofile);
    int i = 0;
    while (!reader.atEnd()) {
      ROMol *mol = reader.next();
      std::string mname;
      mol->getProp("_Name", mname);
      CHECK_INVARIANT(mname == names[i], "");
    
      delete mol;
      i++;
    }
  }

  {
    // now read in a file with aromatic information on the bonds
    std::string infile = rdbase + "/Code/GraphMol/FileParsers/test_data/outNCI_arom.sdf";
    SDMolSupplier nreader(infile);
    unsigned int i = 0;
    while (!nreader.atEnd()) {
      ROMol *mol = nreader.next();
      TEST_ASSERT(mol);
      ++i;
      delete mol;
    }
    TEST_ASSERT(i==16);
  }
}
Example #18
0
double calcTPSA(const ROMol &mol, bool force) {
  if (!force && mol.hasProp(common_properties::_tpsa)) {
    double res;
    mol.getProp(common_properties::_tpsa, res);
    return res;
  }
  std::vector<double> contribs;
  contribs.resize(mol.getNumAtoms());
  double res;
  res = getTPSAAtomContribs(mol, contribs, force);
  return res;
}
Example #19
0
void testSmilesWriterStrm() {
  std::string rdbase = getenv("RDBASE");
  std::string fname = rdbase + "/Code/GraphMol/FileParsers/test_data/fewSmi.csv";
  SmilesMolSupplier *nSup = new SmilesMolSupplier(fname, ",", 1, 0, false);
  std::string oname = rdbase + "/Code/GraphMol/FileParsers/test_data/outSmiles.csv";
  std::ofstream *oStream=new std::ofstream(oname.c_str());
  
  STR_VECT propNames;
  propNames.push_back(std::string("Column_2"));
  SmilesWriter *writer = new SmilesWriter(oStream, " ");
  writer->setProps(propNames);

  STR_VECT names;
  STR_VECT props;
  ROMol *mol = nSup->next();
  while (mol) {
    std::string mname, pval;
    mol->getProp("_Name", mname);
    mol->getProp("Column_2", pval);
    names.push_back(mname);
    props.push_back(pval);
    writer->write(*mol);
    delete mol;
    try {
      mol = nSup->next();
    } catch (FileParseException &) {
      break;
    }
  }
  writer->flush();
  delete writer;
  delete nSup;

  // now read the molecules back in a check if we have the same properties etc
  nSup = new SmilesMolSupplier(oname);
  int i = 0;
  mol = nSup->next();
  while (mol){
    std::string mname, pval;
    mol->getProp("_Name", mname);
    mol->getProp("Column_2", pval);
    CHECK_INVARIANT(mname == names[i], "");
    CHECK_INVARIANT(pval == props[i], "");
    i++;
    delete mol;
    try {
      mol = nSup->next();
    } catch (FileParseException &) {
      break;
    }
  }

}
Example #20
0
array<Point3D, 4> calcRefPoints(const ROMol& mol, const vector<int>& heavyAtoms)
{
	const auto num_points = heavyAtoms.size();
	assert(num_points == mol.getNumHeavyAtoms());
	const auto& conf = mol.getConformer();
	array<Point3D, 4> refPoints;
	for (auto& ref : refPoints)
	{
		assert(ref[0] == 0);
		assert(ref[1] == 0);
		assert(ref[2] == 0);
	}
	auto& ctd = refPoints[0];
	auto& cst = refPoints[1];
	auto& fct = refPoints[2];
	auto& ftf = refPoints[3];
	for (const auto i : heavyAtoms)
	{
		const auto& a = conf.getAtomPos(i);
		ctd += a;
	}
	ctd /= num_points;
	double cst_dist = numeric_limits<double>::max();
	double fct_dist = numeric_limits<double>::lowest();
	double ftf_dist = numeric_limits<double>::lowest();
	for (const auto i : heavyAtoms)
	{
		const auto& a = conf.getAtomPos(i);
		const auto this_dist = dist2(a, ctd);
		if (this_dist < cst_dist)
		{
			cst = a;
			cst_dist = this_dist;
		}
		if (this_dist > fct_dist)
		{
			fct = a;
			fct_dist = this_dist;
		}
	}
	for (const auto i : heavyAtoms)
	{
		const auto& a = conf.getAtomPos(i);
		const auto this_dist = dist2(a, fct);
		if (this_dist > ftf_dist)
		{
			ftf = a;
			ftf_dist = this_dist;
		}
	}
	return refPoints;
}
Example #21
0
double calcLabuteASA(const ROMol &mol, bool includeHs, bool force) {
  if (!force && mol.hasProp(common_properties::_labuteASA)) {
    double res;
    mol.getProp(common_properties::_labuteASA, res);
    return res;
  }
  std::vector<double> contribs;
  contribs.resize(mol.getNumAtoms());
  double hContrib;
  double res;
  res = getLabuteAtomContribs(mol, contribs, hContrib, includeHs, force);
  return res;
}
Example #22
0
    void setHybridization(ROMol &mol) {
      ROMol::AtomIterator ai;
      int norbs;
      for (ai = mol.beginAtoms(); ai != mol.endAtoms(); ai++) {
        if((*ai)->getAtomicNum()==0){
	  (*ai)->setHybridization(Atom::UNSPECIFIED);
        } else {
          norbs = numBondsPlusLonePairs(*ai);
          switch(norbs) {
          case 0:
            // This occurs for things like Na+
            (*ai)->setHybridization(Atom::S);
            break;
          case 1:
            (*ai)->setHybridization(Atom::S);
            break;
          case 2:
            (*ai)->setHybridization(Atom::SP);
            break;
          case 3:
            (*ai)->setHybridization(Atom::SP2);
            break;
          case 4:
            // potentially SP3, but we'll set it down to SP2
            // if we have a conjugated bond (like the second O
            // in O=CO)
            // we'll also avoid setting the hybridization down to
            // SP2 in the case of an atom with degree higher than 3
            // (e.g. things like CP1(C)=CC=CN=C1C, where the P
            //   has norbs = 4, and a conjugated bond, but clearly should
            //   not be SP2)
            // This is Issue276
            if((*ai)->getDegree()>3 || !MolOps::atomHasConjugatedBond(*ai)){
              (*ai)->setHybridization(Atom::SP3);
            } else {
              (*ai)->setHybridization(Atom::SP2);
            }
            break;
          case 5:
            (*ai)->setHybridization(Atom::SP3D);
            break;
          case 6:
            (*ai)->setHybridization(Atom::SP3D2);
            break;
          default :
            (*ai)->setHybridization(Atom::UNSPECIFIED);
          }
        }
      }
    }
Example #23
0
      // ------------------------------------------------------------------------
      //
      //
      //
      // ------------------------------------------------------------------------
      void addAngleSpecialCases(const ROMol &mol, int confId, const AtomicParamVect &params,
                                ForceFields::ForceField *field){
        PRECONDITION(mol.getNumAtoms()==params.size(),"bad parameters");
        PRECONDITION(field,"bad forcefield");

        unsigned int nAtoms=mol.getNumAtoms();
        for(unsigned int i=0;i<nAtoms;i++){
          const Atom *atom = mol.getAtomWithIdx(i);
          // trigonal bipyramidal:
          if( (atom->getHybridization()==Atom::SP3D && atom->getDegree()==5) ){
            addTrigonalBipyramidAngles(atom,mol,confId, params,field);
          }
        }
      }
Example #24
0
void fragmentOnSomeBonds(
    const ROMol &mol, const std::vector<unsigned int> &bondIndices,
    std::vector<ROMOL_SPTR> &resMols, unsigned int maxToCut, bool addDummies,
    const std::vector<std::pair<unsigned int, unsigned int>> *dummyLabels,
    const std::vector<Bond::BondType> *bondTypes,
    std::vector<std::vector<unsigned int>> *nCutsPerAtom) {
  PRECONDITION((!dummyLabels || dummyLabels->size() == bondIndices.size()),
               "bad dummyLabel vector");
  PRECONDITION((!bondTypes || bondTypes->size() == bondIndices.size()),
               "bad bondType vector");
  if (bondIndices.size() > 63)
    throw ValueErrorException("currently can only fragment on up to 63 bonds");
  if (!maxToCut || !mol.getNumAtoms() || !bondIndices.size()) return;

  boost::uint64_t state = (0x1L << maxToCut) - 1;
  boost::uint64_t stop = 0x1L << bondIndices.size();
  std::vector<unsigned int> fragmentHere(maxToCut);
  std::vector<std::pair<unsigned int, unsigned int>> *dummyLabelsHere = nullptr;
  if (dummyLabels) {
    dummyLabelsHere =
        new std::vector<std::pair<unsigned int, unsigned int>>(maxToCut);
  }
  std::vector<Bond::BondType> *bondTypesHere = nullptr;
  if (bondTypes) {
    bondTypesHere = new std::vector<Bond::BondType>(maxToCut);
  }
  while (state < stop) {
    unsigned int nSeen = 0;
    for (unsigned int i = 0; i < bondIndices.size() && nSeen < maxToCut; ++i) {
      if (state & (0x1L << i)) {
        fragmentHere[nSeen] = bondIndices[i];
        if (dummyLabelsHere) (*dummyLabelsHere)[nSeen] = (*dummyLabels)[i];
        if (bondTypesHere) (*bondTypesHere)[nSeen] = (*bondTypes)[i];
        ++nSeen;
      }
    }
    std::vector<unsigned int> *lCutsPerAtom = nullptr;
    if (nCutsPerAtom) {
      nCutsPerAtom->push_back(std::vector<unsigned int>(mol.getNumAtoms()));
      lCutsPerAtom = &(nCutsPerAtom->back());
    }
    ROMol *nm = fragmentOnBonds(mol, fragmentHere, addDummies, dummyLabelsHere,
                                bondTypesHere, lCutsPerAtom);
    resMols.push_back(ROMOL_SPTR(nm));

    state = nextBitCombo(state);
  }
  delete dummyLabelsHere;
  delete bondTypesHere;
}
Example #25
0
void WedgeMolBonds(ROMol &mol, const Conformer *conf) {
    PRECONDITION(conf, "no conformer");
    INT_MAP_INT wedgeBonds = pickBondsToWedge(mol);
    for (ROMol::BondIterator bondIt = mol.beginBonds(); bondIt != mol.endBonds();
            ++bondIt) {
        Bond *bond = *bondIt;
        if (bond->getBondType() == Bond::SINGLE) {
            Bond::BondDir dir = DetermineBondWedgeState(bond, wedgeBonds, conf);
            if (dir == Bond::BEGINWEDGE || dir == Bond::BEGINDASH) {
                bond->setBondDir(dir);
            }
        }
    }
}
Example #26
0
 bool MCSBondCompareOrderExact (const MCSBondCompareParameters& p, const ROMol& mol1, unsigned int bond1, const ROMol& mol2, unsigned int bond2, void* ud) {
     static const BondMatchOrderMatrix match(false); // AROMATIC != SINGLE
     const Bond* b1 = mol1.getBondWithIdx(bond1);
     const Bond* b2 = mol2.getBondWithIdx(bond2);
     Bond::BondType t1 = b1->getBondType();
     Bond::BondType t2 = b2->getBondType();
     if(match.isEqual(t1,t2)) {
         if(p.RingMatchesRingOnly)
             return checkRingMatch(p, mol1, bond1, mol2, bond2, ud);
         else
             return true;
     }
     return false;
 }
Example #27
0
      // ------------------------------------------------------------------------
      //
      // the two-bit matrix returned by this contains:
      //   0: if atoms i and j are directly connected
      //   1: if atoms i and j are connected via an atom
      //   3: otherwise
      //
      //  NOTE: the caller is responsible for calling delete []
      //  on the result
      //
      // ------------------------------------------------------------------------
      boost::shared_array<boost::uint8_t> buildNeighborMatrix(const ROMol &mol)
      {
        unsigned int nAtoms = mol.getNumAtoms();
        unsigned nTwoBitCells = (nAtoms * nAtoms - 1) / 4 + 1;
        boost::shared_array<boost::uint8_t> res(new boost::uint8_t[nTwoBitCells]);
        for (unsigned int i = 0; i < nTwoBitCells; ++i) {
          res[i] = 0;
        }
        for (unsigned int i = 0; i < nAtoms; ++i) {
          unsigned int iTab = i * nAtoms;
          for (unsigned int j = i; j < nAtoms; ++j) {
            setTwoBitCell(res, iTab + j, RELATION_1_X);
            setTwoBitCell(res, i + j * nAtoms, RELATION_1_X);
          }
        }
        for (unsigned int i = 0; i < mol.getNumBonds(); ++i) {
          const Bond *bondi = mol.getBondWithIdx(i);

          setTwoBitCell(res, bondi->getBeginAtomIdx() * nAtoms + bondi->getEndAtomIdx(), RELATION_1_2);
          setTwoBitCell(res, bondi->getEndAtomIdx() * nAtoms + bondi->getBeginAtomIdx(), RELATION_1_2);

          for (unsigned int j = i + 1; j < mol.getNumBonds(); ++j) {
            const Bond *bondj = mol.getBondWithIdx(j);
            int idx1 = -1;
            int idx3 = -1;
            if (bondi->getBeginAtomIdx() == bondj->getBeginAtomIdx()) {
              idx1 = bondi->getEndAtomIdx();
              idx3 = bondj->getEndAtomIdx();
            }
            else if (bondi->getBeginAtomIdx() == bondj->getEndAtomIdx()) {
              idx1 = bondi->getEndAtomIdx();
              idx3 = bondj->getBeginAtomIdx();
            }
            else if (bondi->getEndAtomIdx() == bondj->getBeginAtomIdx()) {
              idx1 = bondi->getBeginAtomIdx();
              idx3 = bondj->getEndAtomIdx();
            }
            else if (bondi->getEndAtomIdx() == bondj->getEndAtomIdx()) {
              idx1 = bondi->getBeginAtomIdx();
              idx3 = bondj->getBeginAtomIdx();
            }
            if (idx1 > -1) {
              setTwoBitCell(res, idx1 * nAtoms + idx3, RELATION_1_3);
              setTwoBitCell(res, idx3 * nAtoms + idx1, RELATION_1_3);
            }
          }
        }
        return res;
      }
Example #28
0
bool atomIsCandidateForRingStereochem(const ROMol &mol, const Atom *atom) {
  PRECONDITION(atom, "bad atom");
  bool res = false;
  if (!atom->getPropIfPresent(common_properties::_ringStereochemCand, res)) {
    const RingInfo *ringInfo = mol.getRingInfo();
    if (ringInfo->isInitialized() && ringInfo->numAtomRings(atom->getIdx())) {
      ROMol::OEDGE_ITER beg, end;
      boost::tie(beg, end) = mol.getAtomBonds(atom);
      std::vector<const Atom *> nonRingNbrs;
      std::vector<const Atom *> ringNbrs;
      while (beg != end) {
        const BOND_SPTR bond = mol[*beg];
        if (!ringInfo->numBondRings(bond->getIdx())) {
          nonRingNbrs.push_back(bond->getOtherAtom(atom));
        } else {
          ringNbrs.push_back(bond->getOtherAtom(atom));
        }
        ++beg;
      }

      unsigned int rank1 = 0, rank2 = 0;
      switch (nonRingNbrs.size()) {
        case 0:
          // don't do spiro:
          res = false;
          break;
        case 1:
          if (ringNbrs.size() == 2) res = true;
          break;
        case 2:
          if (nonRingNbrs[0]->getPropIfPresent(common_properties::_CIPRank,
                                               rank1) &&
              nonRingNbrs[1]->getPropIfPresent(common_properties::_CIPRank,
                                               rank2)) {
            if (rank1 == rank2) {
              res = false;
            } else {
              res = true;
            }
          }
          break;
        default:
          res = false;
      }
    }
    atom->setProp(common_properties::_ringStereochemCand, res, 1);
  }
  return res;
}
Example #29
0
std::string MolToSequence(const ROMol &mol)
{
  std::string result;

  for (ROMol::ConstAtomIterator atomIt=mol.beginAtoms();
        atomIt!=mol.endAtoms();++atomIt){
    const Atom *atom = *atomIt;
    AtomPDBResidueInfo *info = (AtomPDBResidueInfo*)(atom->getMonomerInfo());
    if (info && info->getMonomerType()==AtomMonomerInfo::PDBRESIDUE &&
        info->getName() == " CA ") {
      result += getOneLetterCode(info);
    }
  }
  return result;
}
Example #30
0
void testCrippenO3AConstraintsAndLocalOnly() {
  std::string rdbase = getenv("RDBASE");
  std::string sdf = rdbase + "/Code/GraphMol/MolAlign/test_data/ref_e2.sdf";
  SDMolSupplier supplier(sdf, true, false);
  const int refNum = 23;
  const int prbNum = 32;
  ROMol *refMol = supplier[refNum];
  ROMol *prbMol = supplier[prbNum];
  MMFF::MMFFMolProperties refMP(*refMol);
  TEST_ASSERT(refMP.isValid());
  MMFF::MMFFMolProperties prbMP(*prbMol);
  TEST_ASSERT(prbMP.isValid());
  RWMol *patt = SmartsToMol("S");
  MatchVectType matchVect;
  TEST_ASSERT(SubstructMatch(*refMol, (ROMol &)*patt, matchVect));
  delete patt;
  unsigned int refSIdx = matchVect[0].second;
  matchVect.clear();
  patt = SmartsToMol("O");
  TEST_ASSERT(SubstructMatch(*prbMol, (ROMol &)*patt, matchVect));
  delete patt;
  unsigned int prbOIdx = matchVect[0].second;
  std::vector<double> distOS(2);
  distOS[0] = 3.2;
  distOS[1] = 0.3;
  std::vector<double> weights(2);
  weights[0] = 10.0;
  weights[1] = 100.0;
  for (unsigned int i = 0; i < 2; ++i) {
    MatchVectType constraintMap;
    constraintMap.push_back(std::make_pair(prbOIdx, refSIdx));
    RDNumeric::DoubleVector constraintWeights(1);
    constraintWeights[0] = weights[i];
    MolAlign::O3A *o3a = new MolAlign::O3A(*prbMol, *refMol, &prbMP, &refMP,
      MolAlign::O3A::MMFF94, -1, -1, false, 50, 0, &constraintMap, &constraintWeights);
    TEST_ASSERT(o3a);
    o3a->align();
    delete o3a;
    o3a = new MolAlign::O3A(*prbMol, *refMol, &prbMP, &refMP,
      MolAlign::O3A::MMFF94, -1, -1, false, 50, MolAlign::O3_LOCAL_ONLY);
    TEST_ASSERT(o3a);
    o3a->align();
    delete o3a;
    double d = (prbMol->getConformer().getAtomPos(prbOIdx)
      - refMol->getConformer().getAtomPos(refSIdx)).length();
    TEST_ASSERT(feq(d, distOS[i], 0.1));
  }
}