// ------------------------------------------------------------------------ // // 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 // 2: if atoms i and j are in a 1,4 relationship // 3: otherwise // // NOTE: the caller is responsible for calling delete [] // on the result // // ------------------------------------------------------------------------ boost::shared_array<boost::uint8_t> buildNeighborMatrix(const ROMol &mol) { const boost::uint8_t RELATION_1_X_INIT = RELATION_1_X | (RELATION_1_X << 2) | (RELATION_1_X << 4) | (RELATION_1_X << 6); unsigned int nAtoms = mol.getNumAtoms(); unsigned nTwoBitCells = (nAtoms * (nAtoms + 1) - 1) / 8 + 1; boost::shared_array<boost::uint8_t> res(new boost::uint8_t[nTwoBitCells]); std::memset(res.get(), RELATION_1_X_INIT, nTwoBitCells); for (ROMol::ConstBondIterator bondi = mol.beginBonds(); bondi != mol.endBonds(); ++bondi) { setTwoBitCell(res, twoBitCellPos(nAtoms, (*bondi)->getBeginAtomIdx(), (*bondi)->getEndAtomIdx()), RELATION_1_2); unsigned int bondiBeginAtomIdx = (*bondi)->getBeginAtomIdx(); unsigned int bondiEndAtomIdx = (*bondi)->getEndAtomIdx(); for (ROMol::ConstBondIterator bondj = bondi; ++bondj != mol.endBonds();) { int idx1 = -1; int idx3 = -1; unsigned int bondjBeginAtomIdx = (*bondj)->getBeginAtomIdx(); unsigned int bondjEndAtomIdx = (*bondj)->getEndAtomIdx(); if (bondiBeginAtomIdx == bondjBeginAtomIdx) { idx1 = bondiEndAtomIdx; idx3 = bondjEndAtomIdx; } else if (bondiBeginAtomIdx == bondjEndAtomIdx) { idx1 = bondiEndAtomIdx; idx3 = bondjBeginAtomIdx; } else if (bondiEndAtomIdx == bondjBeginAtomIdx) { idx1 = bondiBeginAtomIdx; idx3 = bondjEndAtomIdx; } else if (bondiEndAtomIdx == bondjEndAtomIdx) { idx1 = bondiBeginAtomIdx; idx3 = bondjBeginAtomIdx; } if (idx1 > -1) { setTwoBitCell(res, twoBitCellPos(nAtoms, idx1, idx3), RELATION_1_3); } } } return res; }
// ------------------------------------------------------------------------ // // 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; }