1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
|
// $Id$
//
// Copyright (C) 2002-2012 Greg Landrum and Rational Discovery LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <GraphMol/RDKitBase.h>
#include <RDGeneral/utils.h>
#include <GraphMol/RankAtoms.h>
#include <boost/cstdint.hpp>
#include <boost/foreach.hpp>
#include <boost/dynamic_bitset.hpp>
#include <RDGeneral/hash/hash.hpp>
#include <boost/lambda/lambda.hpp>
#include <list>
#include <algorithm>
using namespace boost::lambda;
//#define VERBOSE_CANON 1
//#define VERYVERBOSE_CANON 1
namespace RankAtoms{
using namespace RDKit;
// --------------------------------------------------
//
// grabs the corresponding primes for the rank vector ranks
//
// --------------------------------------------------
void getPrimes(const INT_VECT &ranks,INT_VECT &res){
PRECONDITION(res.size()==0,"");
res.reserve(ranks.size());
for(INT_VECT_CI ivCIt=ranks.begin();ivCIt!=ranks.end();++ivCIt){
res.push_back(firstThousandPrimes[(*ivCIt)%NUM_PRIMES_AVAIL]);
}
}
// --------------------------------------------------
//
// blows out any indices in indicesInPlay which correspond to unique ranks
//
// --------------------------------------------------
void updateInPlayIndices(const INT_VECT &ranks,INT_LIST &indicesInPlay){
INT_LIST::iterator ivIt=indicesInPlay.begin();
while(ivIt!=indicesInPlay.end()){
// find the first instance of this rank:
INT_VECT::const_iterator pos=std::find(ranks.begin(),ranks.end(),ranks[*ivIt]);
++pos;
// now check to see if there is at least one more:
if( std::find(pos,ranks.end(),ranks[*ivIt])==ranks.end()){
INT_LIST::iterator tmpIt = ivIt;
++ivIt;
indicesInPlay.erase(tmpIt);
} else {
++ivIt;
}
}
}
// --------------------------------------------------
//
// for each index in indicesInPlay, generate the products of the adjacent
// elements
//
// The products are weighted by the order of the bond connecting the atoms.
//
// --------------------------------------------------
void calcAdjacentProducts(unsigned int nAtoms,
const INT_VECT &valVect,
double const *adjMat,
const INT_LIST &indicesInPlay,
DOUBLE_VECT &res,
bool useSelf=true,
double tol=1e-6){
PRECONDITION(valVect.size() >= nAtoms,"");
PRECONDITION(res.size() == 0,"");
PRECONDITION(adjMat,"");
for(INT_LIST::const_iterator idxIt=indicesInPlay.begin();
idxIt != indicesInPlay.end();
++idxIt){
double accum;
if(useSelf)
accum=valVect[*idxIt];
else
accum=1.0;
const unsigned int iTab = (*idxIt)*nAtoms;
for(unsigned int j=0;j<nAtoms;++j){
double elem=adjMat[iTab+j];
if(elem>tol){
if(elem<2.-tol){
accum *= valVect[j];
} else {
accum *= pow(static_cast<double>(valVect[j]),
static_cast<int>(elem));
}
}
}
res.push_back(accum);
}
}
template <typename T>
void debugVect(const std::vector<T> arg){
typename std::vector<T>::const_iterator viIt;
for(viIt=arg.begin();viIt!=arg.end();++viIt){
BOOST_LOG(rdDebugLog)<< *viIt << " ";
}
BOOST_LOG(rdDebugLog)<< std::endl;
}
// --------------------------------------------------
//
// This is one round of the process from Step III in the Daylight
// paper
//
// --------------------------------------------------
unsigned int iterateRanks(unsigned int nAtoms,INT_VECT &primeVect,
DOUBLE_VECT &atomicVect,
INT_LIST &indicesInPlay,
double *adjMat,
INT_VECT &ranks,
VECT_INT_VECT *rankHistory,unsigned int stagnantTol){
PRECONDITION(!rankHistory||rankHistory->size()>=nAtoms,"bad rankHistory size");
bool done = false;
unsigned int numClasses = countClasses(ranks);
unsigned int lastNumClasses = 0;
unsigned int nCycles = 0;
unsigned int nStagnant=0;
//
// loop until either we finish or no improvement is seen
//
#ifdef VERBOSE_CANON
for(unsigned int i=0;i<nAtoms;i++) {
BOOST_LOG(rdDebugLog)<< "\t\t>:" << i << " " << ranks[i] << std::endl;
}
BOOST_LOG(rdDebugLog)<< "\t\t-*-*-*-*-" << std::endl;
#endif
while(!done && nCycles < nAtoms){
// determine which atomic indices are in play (which have duplicate ranks)
if(rankHistory){
for(INT_LIST_CI idx=indicesInPlay.begin();idx!=indicesInPlay.end();++idx){
(*rankHistory)[*idx].push_back(ranks[*idx]);
}
}
updateInPlayIndices(ranks,indicesInPlay);
if(indicesInPlay.empty()) break;
#ifdef VERYVERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "IN PLAY:" << std::endl;
BOOST_LOG(rdDebugLog)<< "\t\t->";
for(INT_LIST::const_iterator tmpI=indicesInPlay.begin();tmpI != indicesInPlay.end();tmpI++){
BOOST_LOG(rdDebugLog)<< " " << *tmpI;
}
BOOST_LOG(rdDebugLog)<< std::endl;
BOOST_LOG(rdDebugLog)<< "\t\t---------" << std::endl;
#endif
//-------------------------
// Step (2):
// Get the products of adjacent primes
//-------------------------
primeVect.resize(0);
getPrimes(ranks,primeVect);
atomicVect.resize(0);
calcAdjacentProducts(nAtoms,primeVect,adjMat,indicesInPlay,atomicVect,false);
#ifdef VERYVERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "primes: ";
debugVect(primeVect);
BOOST_LOG(rdDebugLog)<< "products: ";
debugVect(atomicVect);
#endif
//-------------------------
// Steps (3) and (4)
// sort the products and count classes
//-------------------------
sortAndRankVect(nAtoms,atomicVect,indicesInPlay,ranks);
lastNumClasses = numClasses;
numClasses = countClasses(ranks);
if(numClasses == lastNumClasses) nStagnant++;
#ifdef VERYVERBOSE_CANON
int tmpOff=0;
for(unsigned int i=0;i<nAtoms;i++){
//for(INT_LIST::const_iterator tmpI=indicesInPlay.begin();tmpI != indicesInPlay.end();tmpI++){
BOOST_LOG(rdDebugLog)<< "\t\ti:" << i << "\t" << ranks[i] << "\t" << primeVect[i];
if(std::find(indicesInPlay.begin(),indicesInPlay.end(),i)!=indicesInPlay.end()){
BOOST_LOG(rdDebugLog)<< "\t" << atomicVect[tmpOff];
tmpOff++;
}
BOOST_LOG(rdDebugLog)<< std::endl; }
BOOST_LOG(rdDebugLog)<< "\t\t---------" << std::endl;
#endif
// terminal condition, we'll allow a single round of stagnancy
if(numClasses == nAtoms || nStagnant > stagnantTol) done = 1;
nCycles++;
}
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< ">>>>>> done inner iteration. static: "<< nStagnant << " ";
BOOST_LOG(rdDebugLog)<< nCycles << " " << nAtoms << " " << numClasses << std::endl;
#ifdef VERYVERBOSE_CANON
for(unsigned int i=0;i<nAtoms;i++) {
BOOST_LOG(rdDebugLog)<< "\t" << i << " " << ranks[i] << std::endl;
}
#endif
if(nCycles == nAtoms){
BOOST_LOG(rdWarningLog) << "WARNING: ranking bottomed out" << std::endl;
}
#endif
return numClasses;
}
unsigned int iterateRanks2(unsigned int nAtoms,INT_VECT &primeVect,
DOUBLE_VECT &atomicVect,
INT_LIST &indicesInPlay,
double *adjMat,
INT_VECT &ranks,VECT_DOUBLE_VECT &nRanks,
VECT_INT_VECT *rankHistory,unsigned int stagnantTol){
PRECONDITION(!rankHistory||rankHistory->size()>=nAtoms,"bad rankHistory size");
bool done = false;
unsigned int numClasses = countClasses(ranks);
unsigned int lastNumClasses = 0;
unsigned int nCycles = 0;
unsigned int nStagnant=0;
//
// loop until either we finish or no improvement is seen
//
#ifdef VERBOSE_CANON
for(unsigned int i=0;i<nAtoms;i++) {
BOOST_LOG(rdDebugLog)<< "\t\t>:" << i << " " << ranks[i] << std::endl;
}
BOOST_LOG(rdDebugLog)<< "\t\t-*-*-*-*-" << std::endl;
#endif
while(!done && nCycles < nAtoms){
// determine which atomic indices are in play (which have duplicate ranks)
if(rankHistory){
BOOST_FOREACH(int idx,indicesInPlay){
(*rankHistory)[idx].push_back(ranks[idx]);
}
}
updateInPlayIndices(ranks,indicesInPlay);
if(indicesInPlay.empty()) break;
#ifdef VERYVERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "IN PLAY:" << std::endl;
BOOST_LOG(rdDebugLog)<< "\t\t->";
for(INT_LIST::const_iterator tmpI=indicesInPlay.begin();tmpI != indicesInPlay.end();tmpI++){
BOOST_LOG(rdDebugLog)<< " " << *tmpI;
}
BOOST_LOG(rdDebugLog)<< std::endl;
BOOST_LOG(rdDebugLog)<< "\t\t---------" << std::endl;
#endif
//-------------------------
// Step (2):
// Get the products of adjacent primes
//-------------------------
primeVect.resize(0);
getPrimes(ranks,primeVect);
atomicVect.resize(0);
calcAdjacentProducts(nAtoms,primeVect,adjMat,indicesInPlay,atomicVect,false);
#ifdef VERYVERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "primes: ";
debugVect(primeVect);
BOOST_LOG(rdDebugLog)<< "products: ";
debugVect(atomicVect);
#endif
unsigned int p=0;
BOOST_FOREACH(int idx,indicesInPlay){
nRanks[idx].push_back(atomicVect[p++]);
}
#ifdef VERYVERBOSE_CANON
for(int idx=0;idx<nAtoms;++idx){
std::cerr<<" nranks["<<idx<<"]: ";
std::copy(nRanks[idx].begin(),nRanks[idx].end(),std::ostream_iterator<double>(std::cerr," "));
std::cerr<<"\n";
}
#endif
//-------------------------
// Steps (3) and (4)
// sort the products and count classes
//-------------------------
rankVect(nRanks,ranks);
//sortAndRankVect2(nRanks,indicesInPlay,ranks);
lastNumClasses = numClasses;
numClasses = countClasses(ranks);
if(numClasses == lastNumClasses) nStagnant++;
#ifdef VERYVERBOSE_CANON
int tmpOff=0;
for(unsigned int i=0;i<nAtoms;i++){
//for(INT_LIST::const_iterator tmpI=indicesInPlay.begin();tmpI != indicesInPlay.end();tmpI++){
BOOST_LOG(rdDebugLog)<< "\t\ti:" << i << "\t" << ranks[i] << "\t" << primeVect[i];
if(std::find(indicesInPlay.begin(),indicesInPlay.end(),i)!=indicesInPlay.end()){
BOOST_LOG(rdDebugLog)<< "\t" << atomicVect[tmpOff];
tmpOff++;
}
BOOST_LOG(rdDebugLog)<< std::endl; }
BOOST_LOG(rdDebugLog)<< "\t\t---------" << std::endl;
#endif
// terminal condition, we'll allow a single round of stagnancy
if(numClasses == nAtoms || nStagnant > stagnantTol) done = 1;
nCycles++;
}
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< ">>>>>> done inner iteration. static: "<< nStagnant << " ";
BOOST_LOG(rdDebugLog)<< nCycles << " " << nAtoms << " " << numClasses << std::endl;
#ifdef VERYVERBOSE_CANON
for(unsigned int i=0;i<nAtoms;i++) {
BOOST_LOG(rdDebugLog)<< "\t" << i << " " << ranks[i] << std::endl;
}
#endif
if(nCycles == nAtoms){
BOOST_LOG(rdWarningLog) << "WARNING: ranking bottomed out" << std::endl;
}
#endif
return numClasses;
}
// --------------------------------------------------
//
// Calculates invariants for the atoms of a molecule
//
// NOTE: if the atom has not had chirality info pre-calculated, it doesn't
// much matter what value includeChirality has!
// --------------------------------------------------
void buildAtomInvariants(const ROMol &mol,INVAR_VECT &res,
bool includeChirality,
bool includeIsotopes){
PRECONDITION(res.size()>=mol.getNumAtoms(),"res vect too small");
unsigned int atsSoFar=0;
std::vector<boost::uint64_t> tres(mol.getNumAtoms());
for(ROMol::ConstAtomIterator atIt=mol.beginAtoms();atIt!=mol.endAtoms();atIt++){
Atom const *atom = *atIt;
int nHs = atom->getTotalNumHs() % 8;
int chg = abs(atom->getFormalCharge()) % 8;
int chgSign = atom->getFormalCharge() > 0;
int num = atom->getAtomicNum() % 128;
int nConns = atom->getDegree() % 8;
int deltaMass=0;
if(includeIsotopes && atom->getIsotope()){
deltaMass = static_cast<int>(atom->getIsotope() -
PeriodicTable::getTable()->getMostCommonIsotope(atom->getAtomicNum()));
deltaMass += 128;
if(deltaMass < 0) deltaMass = 0;
else deltaMass = deltaMass % 256;
}
// figure out the minimum-sized ring we're involved in
int inRing = 0;
if(atom->getOwningMol().getRingInfo()->numAtomRings(atom->getIdx())){
RingInfo *ringInfo=atom->getOwningMol().getRingInfo();
inRing=3;
while(inRing<256){
if(ringInfo->isAtomInRingOfSize(atom->getIdx(),inRing)){
break;
} else {
inRing++;
}
}
}
inRing = inRing % 16;
boost::uint64_t invariant = 0;
invariant = (invariant << 3) | nConns;
// we used to include the number of explicitHs, but that
// didn't make much sense. TotalValence is another possible
// discriminator here, but the information is essentially
// redundant with nCons, num, and nHs.
// invariant = (invariant << 4) | totalVal;
invariant = (invariant << 7) | num;
invariant = (invariant << 8) | deltaMass;
invariant = (invariant << 3) | nHs;
invariant = (invariant << 4) | inRing;
invariant = (invariant << 3) | chg;
invariant = (invariant << 1) | chgSign;
if(includeChirality ){
int isR=0;
if( atom->hasProp("_CIPCode")){
std::string cipCode;
atom->getProp("_CIPCode",cipCode);
if(cipCode=="R"){
isR=1;
} else {
isR=2;
}
}
invariant = (invariant << 2) | isR;
}
// now deal with cis/trans - this is meant to address issue 174
// loop over the bonds on this atom and check if we have a double bond with
// a chiral code marking
if (includeChirality) {
ROMol::OBOND_ITER_PAIR atomBonds = atom->getOwningMol().getAtomBonds(atom);
int isT=0;
while (atomBonds.first != atomBonds.second){
BOND_SPTR tBond = atom->getOwningMol()[*(atomBonds.first)];
if( (tBond->getBondType() == Bond::DOUBLE) &&
(tBond->getStereo()>Bond::STEREOANY )) {
if (tBond->getStereo()==Bond::STEREOE) {
isT = 1;
} else if(tBond->getStereo()==Bond::STEREOZ) {
isT=2;
}
break;
}
atomBonds.first++;
}
invariant = (invariant << 2) | isT;
}
tres[atsSoFar++] = invariant;
}
if(includeChirality){
// ring stereochemistry
boost::dynamic_bitset<> adjusted(mol.getNumAtoms());
for(ROMol::ConstAtomIterator atIt=mol.beginAtoms();atIt!=mol.endAtoms();atIt++){
Atom const *atom = *atIt;
tres[atom->getIdx()] = tres[atom->getIdx()]<<2;
}
for(ROMol::ConstAtomIterator atIt=mol.beginAtoms();atIt!=mol.endAtoms();atIt++){
Atom const *atom = *atIt;
if((atom->getChiralTag()==Atom::CHI_TETRAHEDRAL_CW ||
atom->getChiralTag()==Atom::CHI_TETRAHEDRAL_CCW) &&
atom->hasProp("_ringStereoAtoms")){
//atom->hasProp("_CIPRank") &&
//!atom->hasProp("_CIPCode")){
ROMol::ADJ_ITER beg,end;
boost::tie(beg,end) = mol.getAtomNeighbors(atom);
unsigned int nCount=0;
while(beg!=end){
unsigned int nbrIdx=mol[*beg]->getIdx();
if(!adjusted[nbrIdx]){
tres[nbrIdx] |= nCount%4;
adjusted.set(nbrIdx);
}
++nCount;
++beg;
}
}
}
}
for(unsigned int i=0;i<mol.getNumAtoms();++i) res[i]=tres[i];
}
void buildFragmentAtomInvariants(const ROMol &mol,INVAR_VECT &res,
bool includeChirality,
const boost::dynamic_bitset<> &atomsToUse,
const boost::dynamic_bitset<> &bondsToUse,
const std::vector<std::string> *atomSymbols
){
PRECONDITION(res.size()>=mol.getNumAtoms(),"res vect too small");
std::vector<int> degrees(mol.getNumAtoms(),0);
for(unsigned int i=0;i<bondsToUse.size();++i){
if(bondsToUse[i]){
const Bond *bnd=mol.getBondWithIdx(i);
degrees[bnd->getBeginAtomIdx()]++;
degrees[bnd->getEndAtomIdx()]++;
}
}
for(ROMol::ConstAtomIterator atIt=mol.beginAtoms();atIt!=mol.endAtoms();++atIt){
Atom const *atom = *atIt;
int aIdx=atom->getIdx();
if(!atomsToUse[aIdx]){
res[aIdx] = 0;
continue;
}
boost::uint64_t invariant = 0;
int nConns = degrees[aIdx]% 8;
invariant = (invariant << 3) | nConns;
if(!atomSymbols){
int chg = abs(atom->getFormalCharge()) % 8;
int chgSign = atom->getFormalCharge() > 0;
int num = atom->getAtomicNum() % 128;
int deltaMass=0;
if(atom->getIsotope()){
deltaMass = static_cast<int>(atom->getIsotope() -
PeriodicTable::getTable()->getMostCommonIsotope(atom->getAtomicNum()));
deltaMass += 128;
if(deltaMass < 0) deltaMass = 0;
else deltaMass = deltaMass % 256;
}
invariant = (invariant << 7) | num;
invariant = (invariant << 8) | deltaMass;
invariant = (invariant << 3) | chg;
invariant = (invariant << 1) | chgSign;
invariant = (invariant << 1) | atom->getIsAromatic();
} else {
const std::string &symb=(*atomSymbols)[aIdx];
boost::uint32_t hsh=gboost::hash_range(symb.begin(),symb.end());
invariant = (invariant << 20) | (hsh%(1<<20));
}
// figure out the minimum-sized ring we're involved in
int inRing = mol.getRingInfo()->minAtomRingSize(aIdx);
inRing = inRing % 16;
invariant = (invariant << 4) | inRing;
if(includeChirality ){
int isR=0;
if( atom->hasProp("_CIPCode")){
std::string cipCode;
atom->getProp("_CIPCode",cipCode);
if(cipCode=="R"){
isR=1;
} else {
isR=2;
}
}
invariant = (invariant << 2) | isR;
}
// now deal with cis/trans - this is meant to address issue 174
// loop over the bonds on this atom and check if we have a double bond with
// a chiral code marking
if (includeChirality) {
ROMol::OBOND_ITER_PAIR atomBonds = mol.getAtomBonds(atom);
int isT=0;
while (atomBonds.first != atomBonds.second){
BOND_SPTR tBond = mol[*(atomBonds.first)];
atomBonds.first++;
if(!bondsToUse[tBond->getIdx()]) continue;
if( (tBond->getBondType() == Bond::DOUBLE) &&
(tBond->getStereo()>Bond::STEREOANY )) {
if (tBond->getStereo()==Bond::STEREOE) {
isT = 1;
} else if(tBond->getStereo()==Bond::STEREOZ) {
isT=2;
}
break;
}
}
invariant = (invariant << 2) | isT;
}
res[aIdx] = invariant;
}
}
}// end of RankAtoms namespace
namespace RDKit{
namespace MolOps {
// --------------------------------------------------
//
// Daylight canonicalization, loosely based up on algorithm described in
// JCICS 29, 97-101, (1989)
// When appropriate, specific references are made to the algorithm
// description in that paper. Steps refer to Table III of the paper
//
// --------------------------------------------------
void rankAtoms(const ROMol &mol,INT_VECT &ranks,
bool breakTies,
bool includeChirality,
bool includeIsotopes,
VECT_INT_VECT *rankHistory){
unsigned int i;
unsigned int nAtoms = mol.getNumAtoms();
PRECONDITION(ranks.size()>=nAtoms,"");
PRECONDITION(!rankHistory||rankHistory->size()>=nAtoms,"bad rankHistory size");
unsigned int stagnantTol=1;
if(!mol.getRingInfo()->isInitialized()){
MolOps::findSSSR(mol);
}
if(nAtoms > 1){
double *adjMat = MolOps::getAdjacencyMatrix(mol, true);
// ----------------------
// generate atomic invariants, Step (1)
// ----------------------
INVAR_VECT invariants;
invariants.resize(nAtoms);
RankAtoms::buildAtomInvariants(mol,invariants,includeChirality,includeIsotopes);
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "invariants:" << std::endl;
for(i=0;i<nAtoms;i++){
BOOST_LOG(rdDebugLog)<< i << " " << (long)invariants[i]<< std::endl;
}
#endif
DOUBLE_VECT atomicVect;
atomicVect.resize(nAtoms);
// ----------------------
// iteration 1: Steps (3) and (4)
// ----------------------
// Unlike the original paper, we're going to keep track of the
// ranks at each iteration and use those vectors to rank
// atoms. This seems to lead to more stable evolution of the
// ranks by avoiding ranks oscillating back and forth across
// iterations.
VECT_DOUBLE_VECT nRanks(nAtoms);
for(i=0;i<nAtoms;i++) nRanks[i].push_back(invariants[i]);
// start by ranking the atoms using the invariants
ranks.resize(nAtoms);
RankAtoms::rankVect(nRanks,ranks);
if(rankHistory){
for(i=0;i<nAtoms;i++){
(*rankHistory)[i].push_back(ranks[i]);
}
}
// how many classes are present?
unsigned int numClasses = RankAtoms::countClasses(ranks);
if(numClasses != nAtoms){
INT_VECT primeVect;
primeVect.reserve(nAtoms);
DOUBLE_VECT atomicVect;
atomicVect.reserve(nAtoms);
// indicesInPlay is used to track the atoms with non-unique ranks
// (we'll be modifying these in each step)
INT_LIST indicesInPlay;
for(i=0;i<nAtoms;i++) indicesInPlay.push_back(i);
// if we aren't breaking ties here, allow the rank iteration to
// go the full number of atoms:
if(!breakTies) stagnantTol=nAtoms;
bool done=indicesInPlay.empty();
while(!done){
//
// do one round of iterations
//
numClasses = RankAtoms::iterateRanks2(nAtoms,primeVect,atomicVect,
indicesInPlay,adjMat,ranks,nRanks,
rankHistory,stagnantTol);
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "************************ done outer iteration" << std::endl;
#endif
#ifdef VERBOSE_CANON
unsigned int tmpI;
BOOST_LOG(rdDebugLog)<< "RANKS:" << std::endl;
for(tmpI=0;tmpI<ranks.size();tmpI++){
BOOST_LOG(rdDebugLog)<< "\t\t" << tmpI << " " << ranks[tmpI] << std::endl;
}
BOOST_LOG(rdDebugLog)<< std::endl;
#endif
//
// This is the tiebreaker stage of things
//
if( breakTies && !indicesInPlay.empty() && numClasses<nAtoms){
INT_VECT newRanks = ranks;
// Add one to all ranks and multiply by two
BOOST_FOREACH(int &nr,newRanks) {
nr=(nr+1)*2;
}
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "postmult:" << std::endl;
for(tmpI=0;tmpI<newRanks.size();tmpI++){
BOOST_LOG(rdDebugLog)<< "\t\t" << newRanks[tmpI] << std::endl;
}
BOOST_LOG(rdDebugLog)<< std::endl;
#endif
//
// find lowest duplicate rank with lowest invariant:
//
int lowestIdx=indicesInPlay.front();
double lowestInvariant = invariants[lowestIdx];
int lowestRank=newRanks[lowestIdx];
BOOST_FOREACH(int ilidx,indicesInPlay){
if(newRanks[ilidx]<=lowestRank){
if(newRanks[ilidx]<lowestRank ||
invariants[ilidx] <= lowestInvariant){
lowestRank = newRanks[ilidx];
lowestIdx = ilidx;
lowestInvariant = invariants[ilidx];
}
}
}
//
// subtract one from the lowest index, rerank and proceed
//
newRanks[lowestIdx] -= 1;
RankAtoms::rankVect(newRanks,ranks);
BOOST_FOREACH(int ilidx,indicesInPlay){
nRanks[ilidx].push_back(ranks[ilidx]);
}
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "RE-RANKED ON:" << lowestIdx << std::endl;
for(tmpI=0;tmpI<newRanks.size();tmpI++){
BOOST_LOG(rdDebugLog)<< "\t\t" << newRanks[tmpI] << " " << ranks[tmpI] << std::endl;
}
BOOST_LOG(rdDebugLog)<< std::endl;
#endif
} else {
done = true;
}
}
}
}
} // end of function rankAtoms
void rankAtomsInFragment(const ROMol &mol,INT_VECT &ranks,
const boost::dynamic_bitset<> &atomsToUse,
const boost::dynamic_bitset<> &bondsToUse,
const std::vector<std::string> *atomSymbols,
const std::vector<std::string> *bondSymbols,
bool breakTies,
VECT_INT_VECT *rankHistory){
unsigned int nAtoms = mol.getNumAtoms();
unsigned int nActiveAtoms = atomsToUse.count();
PRECONDITION(ranks.size()>=nAtoms,"");
PRECONDITION(!atomSymbols||atomSymbols->size()>=nAtoms,"bad atomSymbols");
PRECONDITION(!rankHistory||rankHistory->size()>=nAtoms,"bad rankHistory size");
PRECONDITION(mol.getRingInfo()->isInitialized(),"no ring information present");
PRECONDITION(!rankHistory,"rankHistory not currently supported.");
unsigned int stagnantTol=1;
if(nActiveAtoms > 1){
// ----------------------
// generate atomic invariants, Step (1)
// ----------------------
INVAR_VECT invariants;
invariants.resize(nAtoms);
RankAtoms::buildFragmentAtomInvariants(mol,invariants,true,
atomsToUse,bondsToUse,
atomSymbols);
INVAR_VECT tinvariants;
tinvariants.resize(nActiveAtoms);
unsigned int activeIdx=0;
for(unsigned int aidx=0;aidx<nAtoms;++aidx){
if(atomsToUse[aidx]){
tinvariants[activeIdx++]=invariants[aidx];
}
}
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "invariants:" << std::endl;
for(unsigned int i=0;i<nActiveAtoms;i++){
BOOST_LOG(rdDebugLog)<< i << " " << (long)tinvariants[i]<< std::endl;
}
#endif
// ----------------------
// iteration 1: Steps (3) and (4)
// ----------------------
// start by ranking the atoms using the invariants
VECT_DOUBLE_VECT nRanks(nActiveAtoms);
for(unsigned int i=0;i<nActiveAtoms;i++) nRanks[i].push_back(tinvariants[i]);
INT_VECT tranks(nActiveAtoms,0);
RankAtoms::rankVect(nRanks,tranks);
#if 0
if(rankHistory){
for(unsigned int i=0;i<nAtoms;i++){
(*rankHistory)[i].push_back(ranks[i]);
}
}
#endif
// how many classes are present?
unsigned int numClasses = RankAtoms::countClasses(tranks);
if(numClasses != nActiveAtoms){
double *tadjMat = new double[nActiveAtoms*nActiveAtoms];
memset(static_cast<void *>(tadjMat),0,nActiveAtoms*nActiveAtoms*sizeof(double));
if(!bondSymbols){
double *adjMat = MolOps::getAdjacencyMatrix(mol,true,0,true,0,&bondsToUse);
activeIdx=0;
for(unsigned int aidx=0;aidx<nAtoms;++aidx){
if(atomsToUse[aidx]){
unsigned int activeIdx2=activeIdx+1;
for(unsigned int aidx2=aidx+1;aidx2<nAtoms;++aidx2){
if(atomsToUse[aidx2]){
tadjMat[activeIdx*nActiveAtoms+activeIdx2]=adjMat[aidx*nAtoms+aidx2];
tadjMat[activeIdx2*nActiveAtoms+activeIdx]=adjMat[aidx2*nAtoms+aidx];
++activeIdx2;
}
}
++activeIdx;
}
}
} else {
// rank the bond symbols we have:
std::vector<boost::uint32_t> tbranks(bondsToUse.size(),
0);
for(unsigned int bidx=0;bidx<bondsToUse.size();++bidx){
if(!bondsToUse[bidx]) continue;
const std::string &symb=(*bondSymbols)[bidx];
boost::uint32_t hsh=gboost::hash_range(symb.begin(),symb.end());
tbranks[bidx]=hsh;
}
INT_VECT branks(bondsToUse.size(),1000000);
#ifdef VERBOSE_CANON
std::cerr<<" tbranks:";
std::copy(tbranks.begin(),tbranks.end(),std::ostream_iterator<boost::uint32_t>(std::cerr," "));
std::cerr<<std::endl;
#endif
RankAtoms::rankVect(tbranks,branks);
#ifdef VERBOSE_CANON
std::cerr<<" branks:";
std::copy(branks.begin(),branks.end(),std::ostream_iterator<int>(std::cerr," "));
std::cerr<<std::endl;
#endif
for(unsigned int bidx=0;bidx<bondsToUse.size();++bidx){
if(!bondsToUse[bidx]) continue;
const Bond *bond=mol.getBondWithIdx(bidx);
unsigned int aidx1=bond->getBeginAtomIdx();
unsigned int aidx2=bond->getEndAtomIdx();
unsigned int tidx1=0;
for(unsigned int iidx=0;iidx<aidx1;++iidx){
if(atomsToUse[iidx]) ++tidx1;
}
unsigned int tidx2=0;
for(unsigned int iidx=0;iidx<aidx2;++iidx){
if(atomsToUse[iidx]) ++tidx2;
}
//const std::string &symb=(*bondSymbols)[bidx];
//boost::uint32_t hsh=gboost::hash_range(symb.begin(),symb.end());
//std::cerr<<" ::: "<<bidx<<"->"<<branks[bidx]<<std::endl;
tadjMat[tidx1*nActiveAtoms+tidx2]=branks[bidx];
tadjMat[tidx2*nActiveAtoms+tidx1]=branks[bidx];
}
}
INT_VECT primeVect;
primeVect.reserve(nActiveAtoms);
DOUBLE_VECT atomicVect;
atomicVect.reserve(nActiveAtoms);
#ifdef VERBOSE_CANON
for(unsigned int aidx1=0;aidx1<nActiveAtoms;++aidx1){
std::cerr<<aidx1<<" : ";
for(unsigned int aidx2=aidx1+1;aidx2<nActiveAtoms;++aidx2){
std::cerr<< tadjMat[aidx1*nActiveAtoms+aidx2]<<" ";
}
std::cerr<<std::endl;
}
#endif
// indicesInPlay is used to track the atoms with non-unique ranks
// (we'll be modifying these in each step)
INT_LIST indicesInPlay;
for(unsigned int i=0;i<nActiveAtoms;i++) indicesInPlay.push_back(i);
// if we aren't breaking ties here, allow the rank iteration to
// go the full number of atoms:
if(!breakTies) stagnantTol=nActiveAtoms;
bool done=indicesInPlay.empty();
while(!done){
//
// do one round of iterations
//
numClasses = RankAtoms::iterateRanks2(nActiveAtoms,primeVect,atomicVect,
indicesInPlay,tadjMat,tranks,nRanks,
rankHistory,stagnantTol);
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "************************ done outer iteration" << std::endl;
#endif
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "RANKS:" << std::endl;
for(unsigned int tmpI=0;tmpI<tranks.size();tmpI++){
BOOST_LOG(rdDebugLog)<< "\t\t" << tmpI << " " << tranks[tmpI] << std::endl;
}
BOOST_LOG(rdDebugLog)<< std::endl;
#endif
//
// This is the tiebreaker stage of things
//
if( breakTies && !indicesInPlay.empty() && numClasses<nActiveAtoms){
INT_VECT newRanks = tranks;
// Add one to all ranks and multiply by two
std::for_each(newRanks.begin(),newRanks.end(),_1=(_1+1)*2);
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "postmult:" << std::endl;
for(unsigned tmpI=0;tmpI<newRanks.size();tmpI++){
BOOST_LOG(rdDebugLog)<< "\t\t" << newRanks[tmpI] << std::endl;
}
BOOST_LOG(rdDebugLog)<< std::endl;
#endif
//
// find lowest duplicate rank with lowest invariant:
//
int lowestIdx=indicesInPlay.front();
double lowestInvariant = tinvariants[lowestIdx];
int lowestRank=newRanks[lowestIdx];
for(INT_LIST_I ilIt=indicesInPlay.begin();
ilIt!=indicesInPlay.end();
++ilIt){
if(newRanks[*ilIt]<=lowestRank){
if(newRanks[*ilIt]<lowestRank ||
tinvariants[*ilIt] <= lowestInvariant){
lowestRank = newRanks[*ilIt];
lowestIdx = *ilIt;
lowestInvariant = tinvariants[*ilIt];
}
}
}
//
// subtract one from the lowest index, rerank and proceed
//
newRanks[lowestIdx] -= 1;
RankAtoms::rankVect(newRanks,tranks);
BOOST_FOREACH(int ilidx,indicesInPlay){
nRanks[ilidx].push_back(ranks[ilidx]);
}
#ifdef VERBOSE_CANON
BOOST_LOG(rdDebugLog)<< "RE-RANKED ON:" << lowestIdx << std::endl;
for(unsigned int tmpI=0;tmpI<newRanks.size();tmpI++){
BOOST_LOG(rdDebugLog)<< "\t\t" << newRanks[tmpI] << " " << tranks[tmpI] << std::endl;
}
BOOST_LOG(rdDebugLog)<< std::endl;
#endif
} else {
done = true;
}
}
delete [] tadjMat;
}
unsigned int tidx=0;
for(unsigned int aidx=0;aidx<nAtoms;++aidx){
ranks[aidx]=0;
if(atomsToUse[aidx]){
ranks[aidx]=tranks[tidx++];
}
}
}
} // end of function rankAtomsInFragment
} // end of namespace MolOps
} // End Of RDKit namespace
|