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
|
// $Id: testPickler.cpp 1986 2012-03-04 15:37:50Z glandrum $
//
// Copyright (C) 2004-2011 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 <RDGeneral/utils.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/RDKitQueries.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/FileParsers/MolSupplier.h>
#include <GraphMol/FileParsers/FileParsers.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/SmilesParse/SmartsWrite.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <RDGeneral/RDLog.h>
#include <stdlib.h>
#include <ctime>
#include <iostream>
#include <fstream>
#include <boost/algorithm/string.hpp>
using namespace RDKit;
void test1(bool doLong=0){
std::string fName = getenv("RDBASE");
fName += "/Code/GraphMol/test_data/canonSmiles.smi";
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing String Pickle Roundtrips." << std::endl;
SmilesMolSupplier suppl(fName,"\t",0,1,false);
int count = 0;
while(!suppl.atEnd()){
ROMol *m = suppl.next();
TEST_ASSERT(m);
std::string pickle;
MolPickler::pickleMol(*m,pickle);
TEST_ASSERT(pickle.size());
ROMol m2;
MolPickler::molFromPickle(pickle,m2);
std::string smi1=MolToSmiles(*m);
//BOOST_LOG(rdInfoLog) << smi << " " << smi1 << std::endl;
std::string smi2=MolToSmiles(m2);
if(smi1!=smi2){
BOOST_LOG(rdInfoLog) << "Line: " << count << "\n " << smi1 << "\n != \n " << smi2 << std::endl;
}
TEST_ASSERT(smi1==smi2);
delete m;
count++;
if(!doLong && count >= 100) break;
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void _createPickleFile(){
std::string smiName = getenv("RDBASE");
smiName += "/Code/GraphMol/test_data/canonSmiles.smi";
std::string pklName = getenv("RDBASE");
#ifdef OLD_PICKLE
pklName += "/Code/GraphMol/test_data/canonSmiles.v1.pkl";
#else
pklName += "/Code/GraphMol/test_data/canonSmiles.v2.pkl";
#endif
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "creating pickle file." << std::endl;
SmilesMolSupplier suppl(smiName,"\t",0,1,false);
std::ofstream outStream(pklName.c_str(),std::ios_base::binary);
int count = 0;
while(!suppl.atEnd()){
ROMol *m = suppl.next();
TEST_ASSERT(m);
std::string pickle;
MolPickler::pickleMol(*m,outStream);
delete m;
count++;
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void test2(bool doLong=0){
std::string smiName = getenv("RDBASE");
smiName += "/Code/GraphMol/test_data/canonSmiles.smi";
std::string pklName = getenv("RDBASE");
pklName += "/Code/GraphMol/test_data/canonSmiles.v1.pkl";
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing reading existing pickle file (v1)." << std::endl;
SmilesMolSupplier suppl(smiName,"\t",0,1,false);
std::ifstream inStream(pklName.c_str(),std::ios_base::binary);
int count = 0;
while(!suppl.atEnd()){
ROMol *m1 = suppl.next();
TEST_ASSERT(m1);
ROMol m2;
MolPickler::molFromPickle(inStream,m2);
std::string smi1=MolToSmiles(*m1);
std::string smi2=MolToSmiles(m2);
if(smi1!=smi2){
BOOST_LOG(rdInfoLog) << "Line: " << count << "\n " << smi1 << "\n != \n " << smi2 << std::endl;
}
TEST_ASSERT(smi1==smi2);
delete m1;
count++;
if(!doLong && count >= 100) break;
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void test3(bool doLong=0){
std::string smiName = getenv("RDBASE");
smiName += "/Code/GraphMol/test_data/canonSmiles.smi";
std::string pklName = getenv("RDBASE");
pklName += "/Code/GraphMol/test_data/canonSmiles.v2.pkl";
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing reading existing pickle file (v2)." << std::endl;
SmilesMolSupplier suppl(smiName,"\t",0,1,false);
std::ifstream inStream(pklName.c_str(),std::ios_base::binary);
int count = 0;
while(!suppl.atEnd()){
ROMol *m1 = suppl.next();
TEST_ASSERT(m1);
ROMol m2;
MolPickler::molFromPickle(inStream,m2);
std::string smi1=MolToSmiles(*m1,1);
std::string smi2=MolToSmiles(m2,1);
if(smi1!=smi2){
BOOST_LOG(rdInfoLog) << "Line: " << count << "\n " << smi1 << "\n != \n " << smi2 << std::endl;
}
TEST_ASSERT(smi1==smi2);
delete m1;
count++;
if(!doLong && count >= 100) break;
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void timeTest(bool doLong=0){
time_t t1,t2;
std::string smiName = getenv("RDBASE");
smiName += "/Code/GraphMol/test_data/canonSmiles.smi";
std::string pklName = getenv("RDBASE");
pklName += "/Code/GraphMol/test_data/canonSmiles.v2.pkl";
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Timing reads." << std::endl;
t1 = std::time(0);
SmilesMolSupplier suppl(smiName,"\t",0,1,false);
int count = 0;
while(!suppl.atEnd()){
ROMol *m1 = suppl.next();
TEST_ASSERT(m1);
count++;
if(!doLong && count >= 100) break;
delete m1;
}
t2 = std::time(0);
BOOST_LOG(rdInfoLog) << " Smiles time: " << std::difftime(t2,t1) << std::endl;;
std::ifstream inStream(pklName.c_str(),std::ios_base::binary);
t1 = std::time(0);
while(count>0){
ROMol m2;
MolPickler::molFromPickle(inStream,m2);
count--;
if(!doLong && count >= 100) break;
}
t2 = std::time(0);
BOOST_LOG(rdInfoLog) << " Pickle time: " << std::difftime(t2,t1) << std::endl;;
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void test4(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing combining molecules using pickles." << std::endl;
ROMol *m1 = SmilesToMol("C1CC1");
TEST_ASSERT(m1);
ROMol *m2 = SmilesToMol("C1COC1");
TEST_ASSERT(m2);
ROMol m3;
std::string pickle;
MolPickler::pickleMol(*m1,pickle);
MolPickler::molFromPickle(pickle,m3);
MolPickler::pickleMol(*m2,pickle);
MolPickler::molFromPickle(pickle,m3);
m3.debugMol(std::cout);
TEST_ASSERT(m3.getNumAtoms()==7);
TEST_ASSERT(m3.getNumBonds()==7);
TEST_ASSERT(m3.getRingInfo()->numRings()==2);
TEST_ASSERT(m3.getRingInfo()->isAtomInRingOfSize(0,3));
TEST_ASSERT(!m3.getRingInfo()->isAtomInRingOfSize(0,4));
TEST_ASSERT(!m3.getRingInfo()->isAtomInRingOfSize(4,3));
TEST_ASSERT(m3.getRingInfo()->isAtomInRingOfSize(4,4));
TEST_ASSERT(m3.getRingInfo()->isBondInRingOfSize(0,3));
TEST_ASSERT(!m3.getRingInfo()->isBondInRingOfSize(0,4));
TEST_ASSERT(!m3.getRingInfo()->isBondInRingOfSize(4,3));
TEST_ASSERT(m3.getRingInfo()->isBondInRingOfSize(4,4));
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testIssue164(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing Issue164: Roundtrip Pickle Failure." << std::endl;
std::string smi="NCCCCC(NC(C(C(C)C)NC(=O)C1CCCN1C(C(N)CCCNC(=N)N)=O)=O)C(NC(C(C)C)C(NC(Cc2ccc(O)cc2)C(=O)N6C(C(NC(CC(N)=O)C(NCC(NC(C)C(NC(CCC(O)=O)C(NC(CC(O)=O)C(NC(C(NC(CO)C(NC(C)C(NC(CCC(O)=O)C(NC(C)C(NC(Cc3ccccc3)C(=O)N5C(C(NC(CC(C)C)C(NC(C(NC(C(O)=O)Cc4ccccc4)=O)CCC(O)=O)=O)=O)CCC5)=O)=O)=O)=O)=O)CCC(O)=O)=O)=O)=O)=O)=O)=O)CCC6)=O)=O";
ROMol *m1 = SmilesToMol(smi);
TEST_ASSERT(m1);
std::string pickle;
ROMol m2;
MolPickler::pickleMol(*m1,pickle);
MolPickler::molFromPickle(pickle,m2);
TEST_ASSERT(m1->getNumAtoms()==m2.getNumAtoms());
// the issue had to do with number of atoms, so let's make an enormous
// molecule and try again:
RWMol *m3 = static_cast<RWMol *>(SmilesToMol(smi));
m3->insertMol(*m1);
m3->insertMol(*m1);
MolOps::sanitizeMol(*m3);
MolPickler::pickleMol(*m3,pickle);
ROMol m4;
MolPickler::molFromPickle(pickle,m4);
TEST_ASSERT(m3->getNumAtoms()==m4.getNumAtoms());
TEST_ASSERT(m3->getNumBonds()==m4.getNumBonds());
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testIssue219(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing Issue219: Pickle Failure with Conformations." << std::endl;
std::string smi="CC";
ROMol *m1 = SmilesToMol(smi);
TEST_ASSERT(m1);
std::string pickle;
ROMol *m2;
MolPickler::pickleMol(*m1,pickle);
m2 = new ROMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(m1->getNumAtoms()==m2->getNumAtoms());
Conformer *conf = new Conformer(2);
conf->setId(23);
m1->addConformer(conf);
MolPickler::pickleMol(*m1,pickle);
delete m2;
m2 = new ROMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(m1->getNumAtoms()==m2->getNumAtoms());
TEST_ASSERT(m2->getConformer().getId()==23);
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testIssue220(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing Issue220: Pickle problems with bond stereochemistry." << std::endl;
std::string smi="N/N=C/C";
ROMol *m1 = SmilesToMol(smi);
TEST_ASSERT(m1);
TEST_ASSERT(m1->getBondWithIdx(1)->getStereo()!=Bond::STEREONONE);
std::string pickle;
ROMol *m2;
MolPickler::pickleMol(*m1,pickle);
m2 = new ROMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(m1->getNumAtoms()==m2->getNumAtoms());
TEST_ASSERT(m1->getNumBonds()==m2->getNumBonds());
TEST_ASSERT(m2->getBondWithIdx(1)->getStereo()!=Bond::STEREONONE);
for(unsigned int i=0;i<m1->getNumBonds();++i){
TEST_ASSERT(m1->getBondWithIdx(i)->getStereo() ==
m2->getBondWithIdx(i)->getStereo());
TEST_ASSERT(m1->getBondWithIdx(i)->getStereoAtoms() ==
m2->getBondWithIdx(i)->getStereoAtoms());
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testQueries(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing query serialization." << std::endl;
std::string smi,pickle,smi2;
int tmpInt;
ROMol *m1,*m2;
MatchVectType matchV;
// start simple : atom map numbers
smi="C";
m1= SmilesToMol(smi);
TEST_ASSERT(m1);
m1->getAtomWithIdx(0)->setProp("molAtomMapNumber",1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==1);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasProp("molAtomMapNumber"));
m1->getAtomWithIdx(0)->getProp("molAtomMapNumber",tmpInt);
TEST_ASSERT(tmpInt==1);
delete m1;
// now a basic query:
smi="C";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==1);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(0)->getQuery()->getDescription()=="AtomAnd");
// query should be for aliphatic C:
smi="C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="O";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="c1ccccc1";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m1;
// atom lists:
smi="[C,N,O]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==1);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(0)->getQuery()->getDescription()=="AtomOr");
smi="C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="O";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="N";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="F";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="c1nocc1";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(1)));
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(2)));
delete m1;
// more complex atom queries:
smi="[C&D2,N&D1]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==1);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(0)->getQuery()->getDescription()=="AtomOr");
smi="C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="CC";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="C(C)C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="C(C)(C)C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="N";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="NC";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="N(C)C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
// basic recursive queries:
smi="[$([#6])]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==1);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(0)->getQuery()->getDescription()=="RecursiveStructure");
smi="C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="N";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="c1ccccc1";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(SubstructMatch(*m2,*m1,matchV));
delete m1;
smi="[R2]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==1);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(0)->getQuery()->getDescription()=="AtomInNRings");
smi="C1CCC1";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
delete m2;
smi="C12(CCC2)CCC1";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(0)));
TEST_ASSERT(!m1->getAtomWithIdx(0)->Match(m2->getAtomWithIdx(1)));
delete m2;
// basic bond queries:
smi="[#6][#6]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==2);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(1)->hasQuery());
TEST_ASSERT(m1->getBondWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getBondWithIdx(0)->getQuery()->getDescription()=="BondOr");
smi="CC";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="C=C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="CN";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="c1ccccc1";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="[#6]-[#6]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==2);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(1)->hasQuery());
TEST_ASSERT(m1->getBondWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getBondWithIdx(0)->getQuery()->getDescription()=="BondOrder");
smi="CC";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="C=C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="CN";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="c1ccccc1";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="C=C";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==2);
smi=MolToSmarts(*m1);
TEST_ASSERT(smi=="C=C");
smi="C=[$(C=O)]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==2);
smi=MolToSmarts(*m1);
BOOST_LOG(rdErrorLog)<<smi<<std::endl;
TEST_ASSERT(smi=="C=[$(C=O)]");
smi="S(=O)(=O)-[C;H2]([F,Br,I,Cl])";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
smi=MolToSmarts(*m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
smi2=MolToSmarts(*m1);
BOOST_LOG(rdErrorLog)<<smi<<" "<<smi2<<std::endl;
TEST_ASSERT(smi==smi2);
// more complex recursive queries:
smi="[$(C)]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==1);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(0)->getQuery()->getDescription()=="RecursiveStructure");
smi="C";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="N";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="c1ccccc1";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m1;
// more complex recursive queries:
smi="[$(C=O);$(C-N)]";
m1= SmartsToMol(smi);
TEST_ASSERT(m1);
MolPickler::pickleMol(*m1,pickle);
delete m1;
m1 = new ROMol();
MolPickler::molFromPickle(pickle,*m1);
TEST_ASSERT(m1->getNumAtoms()==1);
TEST_ASSERT(m1->getAtomWithIdx(0)->hasQuery());
TEST_ASSERT(m1->getAtomWithIdx(0)->getQuery()->getDescription()=="AtomAnd");
smi="NCO";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(!SubstructMatch(*m2,*m1,matchV));
delete m2;
smi="CNC(=O)";
m2= SmilesToMol(smi);
TEST_ASSERT(m2);
TEST_ASSERT(SubstructMatch(*m2,*m1,matchV));
delete m2;
delete m1;
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testRadicals(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing radical handling in pickles." << std::endl;
std::string smi="C[CH]C";
ROMol *m1 = SmilesToMol(smi);
TEST_ASSERT(m1);
TEST_ASSERT(m1->getAtomWithIdx(1)->getNumRadicalElectrons()==1);
std::string pickle;
ROMol *m2;
MolPickler::pickleMol(*m1,pickle);
m2 = new ROMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(m1->getNumAtoms()==m2->getNumAtoms());
TEST_ASSERT(m1->getNumBonds()==m2->getNumBonds());
TEST_ASSERT(m2->getAtomWithIdx(1)->getNumRadicalElectrons()==1);
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testIssue2788233(bool doLong=0){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing sf.net issue 2788233." << std::endl;
{
std::string fName = getenv("RDBASE");
fName += "/Code/GraphMol/test_data/Issue2788233.mol";
RWMol *m=MolFileToMol(fName);
TEST_ASSERT(m);
TEST_ASSERT(m->getNumAtoms()==2);
TEST_ASSERT(m->getNumBonds()==0);
std::string pickle;
MolPickler::pickleMol(*m,pickle);
RWMol *m2 = new RWMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(m2->getNumAtoms()==2);
TEST_ASSERT(m2->getNumBonds()==0);
delete m;
delete m2;
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testIssue3202580(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing sf.net issue 3202580." << std::endl;
{
ROMol *m1 = SmilesToMol("C");
TEST_ASSERT(m1);
TEST_ASSERT(feq(m1->getAtomWithIdx(0)->getMass(),12.011,.001));
std::string pickle;
MolPickler::pickleMol(*m1,pickle);
RWMol *m2 = new RWMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(feq(m2->getAtomWithIdx(0)->getMass(),12.011,.001));
delete m1;
delete m2;
}
{
ROMol *m1 = SmilesToMol("[12CH4]");
TEST_ASSERT(m1);
TEST_ASSERT(feq(m1->getAtomWithIdx(0)->getMass(),12.000,.001));
std::string pickle;
MolPickler::pickleMol(*m1,pickle);
RWMol *m2 = new RWMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(feq(m2->getAtomWithIdx(0)->getMass(),12.000,.001));
delete m1;
delete m2;
}
{
ROMol *m1 = SmilesToMol("[13CH4]");
TEST_ASSERT(m1);
TEST_ASSERT(feq(m1->getAtomWithIdx(0)->getMass(),13.000,.001));
std::string pickle;
MolPickler::pickleMol(*m1,pickle);
RWMol *m2 = new RWMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(feq(m2->getAtomWithIdx(0)->getMass(),13.000,.001));
delete m1;
delete m2;
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testIssue3316407(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing sf.net issue 3316407." << std::endl;
{
std::string fName = getenv("RDBASE");
fName += "/Code/GraphMol/FileParsers/test_data/rgroups1.mol";
RWMol *m=MolFileToMol(fName);
TEST_ASSERT(m);
TEST_ASSERT(m->getNumAtoms()==5);
std::string pickle;
MolPickler::pickleMol(*m,pickle);
RWMol *m2 = new RWMol();
MolPickler::molFromPickle(pickle,*m2);
TEST_ASSERT(m2->getNumAtoms()==5);
for(unsigned int i=0;i<m->getNumAtoms();++i){
TEST_ASSERT(m->getAtomWithIdx(i)->getExplicitValence()==m2->getAtomWithIdx(i)->getExplicitValence());
TEST_ASSERT(m->getAtomWithIdx(i)->getImplicitValence()==m2->getAtomWithIdx(i)->getImplicitValence());
}
delete m;
delete m2;
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
void testIssue3496759(){
BOOST_LOG(rdErrorLog) << "-------------------------------------" << std::endl;
BOOST_LOG(rdErrorLog) << "Testing sf.net issue 3496759." << std::endl;
{
ROMol *m1 = SmartsToMol("c1ncncn1");
TEST_ASSERT(m1);
std::string smi1=MolToSmiles(*m1,1);
TEST_ASSERT(smi1=="c1ncncn1");
std::string pickle;
MolPickler::pickleMol(*m1,pickle);
RWMol *m2 = new RWMol(pickle);
TEST_ASSERT(m2);
std::string smi2=MolToSmiles(*m2,1);
TEST_ASSERT(smi2=="c1ncncn1");
delete m1;
delete m2;
}
{
std::string fName = getenv("RDBASE");
fName += "/Data/SmartsLib/RLewis_smarts.txt";
std::ifstream inf(fName.c_str());
while(!inf.eof()){
std::string inl;
std::getline(inf,inl);
if(inl[0]=='#'||inl.size()<2) continue;
std::vector<std::string> tokens;
boost::split(tokens,inl,boost::is_any_of(" \t"));
//std::cerr<<"smarts: "<<tokens[0]<<std::endl;
ROMol *m1 = SmartsToMol(tokens[0]);
TEST_ASSERT(m1);
std::string smi1=MolToSmiles(*m1,1);
std::string sma1=MolToSmarts(*m1);
std::string pickle;
MolPickler::pickleMol(*m1,pickle);
RWMol *m2 = new RWMol(pickle);
TEST_ASSERT(m2);
std::string smi2=MolToSmiles(*m2,1);
std::string sma2=MolToSmarts(*m2);
//std::cerr<<"smi match: "<<smi1<<" "<<smi2<<std::endl;
TEST_ASSERT(smi1==smi2);
//std::cerr<<"sma match: "<<sma1<<" "<<sma2<<std::endl;
TEST_ASSERT(sma1==sma2);
delete m1;
delete m2;
}
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;
}
int main(int argc, char *argv[]) {
RDLog::InitLogs();
bool doLong=false;
if(argc>1) {
if(!strncmp(argv[1],"-l",2)){
doLong=true;
}
}
//_createPickleFile();
#if 1
test1(doLong);
//test2(doLong);
test3(doLong);
test4();
testIssue164();
testIssue219();
testIssue220();
//timeTest(doLong);
testQueries();
testRadicals();
#endif
testIssue2788233();
testIssue3202580();
testIssue3316407();
testIssue3496759();
return 0;
}
|