1 // $Id$
2 //
3 //  Copyright (C) 2005-2006 Rational Discovery LLC
4 //
5 //   @@ All Rights Reserved @@
6 //  This file is part of the RDKit.
7 //  The contents are covered by the terms of the BSD license
8 //  which is included in the file license.txt, found at the root
9 //  of the RDKit source tree.
10 //
11 #include <RDGeneral/test.h>
12 #include <RDGeneral/RDLog.h>
13 #include <boost/log/functions.hpp>
14 #include <GraphMol/RDKitBase.h>
15 #include <string>
16 #include <iostream>
17 #include <GraphMol/FeatTrees/FeatTree.h>
18 #include <GraphMol/FeatTrees/FeatTreeUtils.h>
19 #include <GraphMol/SmilesParse/SmilesParse.h>
20 #include <boost/graph/graph_utility.hpp>
21 
22 using namespace RDKit;
23 
24 using namespace FeatTrees;
showFeatTree(FeatTreeGraph & featGraph)25 void showFeatTree(FeatTreeGraph &featGraph) {
26   typedef boost::property_map<FeatTreeGraph, FeatTreeNode_t>::type
27       FeatTreeNodePMap;
28   FeatTreeNodePMap nodes = boost::get(FeatTreeNode_t(), featGraph);
29   boost::graph_traits<FeatTreeGraph>::vertex_iterator vtx, endVtx;
30   unsigned int i;
31   for (boost::tie(vtx, endVtx) = boost::vertices(featGraph), i = 0;
32        vtx != endVtx; ++vtx, ++i) {
33     UINT_SET s = nodes[*vtx];
34     std::cout << "\t" << i << ": [";
35     std::copy(s.begin(), s.end(), std::ostream_iterator<int>(std::cout, ","));
36     std::cout << "]" << std::endl;
37   }
38 }
39 
test1()40 void test1() {
41   BOOST_LOG(rdInfoLog) << "\t---------------------------------\n";
42   BOOST_LOG(rdInfoLog) << "\t test1: building FeatTreeGraphs \n\n";
43 
44   std::string smi;
45   ROMol *m;
46   FeatTreeGraphSPtr fGraph;
47   std::vector<unsigned int> atomIndices;
48 
49   smi = "CCC";
50   std::cout << smi << std::endl;
51   m = SmilesToMol(smi);
52   TEST_ASSERT(m);
53   fGraph.reset(new FeatTreeGraph());
54   addRingsAndConnectors(*m, *fGraph);
55   TEST_ASSERT(boost::num_vertices(*fGraph) == 0);
56   TEST_ASSERT(boost::num_edges(*fGraph) == 0);
57   replaceCycles(*fGraph);
58   TEST_ASSERT(boost::num_vertices(*fGraph) == 0);
59   TEST_ASSERT(boost::num_edges(*fGraph) == 0);
60   addRingRingBonds(*m, *fGraph);
61   TEST_ASSERT(boost::num_vertices(*fGraph) == 0);
62   TEST_ASSERT(boost::num_edges(*fGraph) == 0);
63   atomIndices = addNonringAtoms(*m, *fGraph);
64   TEST_ASSERT(atomIndices.size() == 3);
65   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
66   TEST_ASSERT(boost::num_edges(*fGraph) == 0);
67   addBondsFromNonringAtoms(*m, *fGraph, atomIndices);
68   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
69   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
70   addZeroNodes(*fGraph);
71   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
72   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
73   fGraph = molToBaseTree(*m);
74   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
75   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
76 
77   smi = "C1CC1OOC1CCC1";
78   std::cout << smi << std::endl;
79   m = SmilesToMol(smi);
80   TEST_ASSERT(m);
81   fGraph.reset(new FeatTreeGraph());
82   addRingsAndConnectors(*m, *fGraph);
83   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
84   TEST_ASSERT(boost::num_edges(*fGraph) == 0);
85   replaceCycles(*fGraph);
86   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
87   TEST_ASSERT(boost::num_edges(*fGraph) == 0);
88   addRingRingBonds(*m, *fGraph);
89   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
90   TEST_ASSERT(boost::num_edges(*fGraph) == 0);
91   atomIndices = addNonringAtoms(*m, *fGraph);
92   TEST_ASSERT(atomIndices.size() == 9);
93   TEST_ASSERT(boost::num_vertices(*fGraph) == 4);
94   TEST_ASSERT(boost::num_edges(*fGraph) == 0);
95   addBondsFromNonringAtoms(*m, *fGraph, atomIndices);
96   TEST_ASSERT(boost::num_vertices(*fGraph) == 4);
97   TEST_ASSERT(boost::num_edges(*fGraph) == 3);
98   addZeroNodes(*fGraph);
99   TEST_ASSERT(boost::num_vertices(*fGraph) == 4);
100   TEST_ASSERT(boost::num_edges(*fGraph) == 3);
101   fGraph = molToBaseTree(*m);
102   TEST_ASSERT(boost::num_vertices(*fGraph) == 4);
103   TEST_ASSERT(boost::num_edges(*fGraph) == 3);
104 
105   delete m;
106   smi = "C1CC2C1CCC2C(=O)";
107   std::cout << smi << std::endl;
108   m = SmilesToMol(smi);
109   TEST_ASSERT(m);
110   fGraph.reset(new FeatTreeGraph());
111   addRingsAndConnectors(*m, *fGraph);
112   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
113   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
114   replaceCycles(*fGraph);
115   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
116   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
117   addRingRingBonds(*m, *fGraph);
118   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
119   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
120   atomIndices = addNonringAtoms(*m, *fGraph);
121   TEST_ASSERT(atomIndices.size() == 9);
122   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
123   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
124   addBondsFromNonringAtoms(*m, *fGraph, atomIndices);
125   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
126   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
127   addZeroNodes(*fGraph);
128   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
129   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
130   fGraph = molToBaseTree(*m);
131   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
132   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
133 
134   delete m;
135   smi = "CC12CCCCC1CCCC2";
136   std::cout << smi << std::endl;
137   m = SmilesToMol(smi);
138   TEST_ASSERT(m);
139   fGraph.reset(new FeatTreeGraph());
140   addRingsAndConnectors(*m, *fGraph);
141   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
142   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
143   replaceCycles(*fGraph);
144   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
145   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
146   addRingRingBonds(*m, *fGraph);
147   TEST_ASSERT(boost::num_vertices(*fGraph) == 2);
148   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
149   atomIndices = addNonringAtoms(*m, *fGraph);
150   TEST_ASSERT(atomIndices.size() == 11);
151   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
152   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
153   addBondsFromNonringAtoms(*m, *fGraph, atomIndices);
154   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
155   TEST_ASSERT(boost::num_edges(*fGraph) == 3);
156   addZeroNodes(*fGraph);
157   TEST_ASSERT(boost::num_vertices(*fGraph) == 4);
158   TEST_ASSERT(boost::num_edges(*fGraph) == 3);
159   fGraph = molToBaseTree(*m);
160   TEST_ASSERT(boost::num_vertices(*fGraph) == 4);
161   TEST_ASSERT(boost::num_edges(*fGraph) == 3);
162 
163   delete m;
164   smi = "C1CC2CCCCC2CC1C1CC(CC2)CCC21";
165   std::cout << smi << std::endl;
166   m = SmilesToMol(smi);
167   TEST_ASSERT(m);
168   fGraph.reset(new FeatTreeGraph());
169   addRingsAndConnectors(*m, *fGraph);
170   TEST_ASSERT(boost::num_vertices(*fGraph) == 5);
171   TEST_ASSERT(boost::num_edges(*fGraph) == 4);
172   replaceCycles(*fGraph);
173   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
174   TEST_ASSERT(boost::num_edges(*fGraph) == 1);
175   addRingRingBonds(*m, *fGraph);
176   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
177   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
178   atomIndices = addNonringAtoms(*m, *fGraph);
179   TEST_ASSERT(atomIndices.size() == 18);
180   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
181   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
182   addBondsFromNonringAtoms(*m, *fGraph, atomIndices);
183   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
184   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
185   addZeroNodes(*fGraph);
186   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
187   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
188   fGraph = molToBaseTree(*m);
189   TEST_ASSERT(boost::num_vertices(*fGraph) == 3);
190   TEST_ASSERT(boost::num_edges(*fGraph) == 2);
191 
192   BOOST_LOG(rdInfoLog) << "\tDone\n";
193 }
194 
main()195 int main() {
196   RDLog::InitLogs();
197   // boost::logging::disable_logs("rdApp.info");
198 
199   // BOOST_LOG(rdInfoLog) <<
200   // "********************************************************\n";
201   // BOOST_LOG(rdInfoLog) << "Testing FeatTrees\n";
202 
203   test1();
204 
205   // BOOST_LOG(rdInfoLog) <<
206   // "*******************************************************\n";
207   return (0);
208 }
209