$darkmode
DENOPTIM
FragmentTest.java
Go to the documentation of this file.
1package denoptim.graph;
2
3/*
4 * DENOPTIM
5 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
6 * and Marco Foscato <marco.foscato@uib.no>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published
10 * by the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22import static org.junit.jupiter.api.Assertions.assertEquals;
23import static org.junit.jupiter.api.Assertions.assertFalse;
24import static org.junit.jupiter.api.Assertions.assertNotEquals;
25import static org.junit.jupiter.api.Assertions.assertTrue;
26
27import java.util.ArrayList;
28
29import javax.vecmath.Point3d;
30
31import org.junit.jupiter.api.Test;
32import org.openscience.cdk.Atom;
33import org.openscience.cdk.interfaces.IAtomContainer;
34import org.openscience.cdk.silent.Bond;
35
36import denoptim.constants.DENOPTIMConstants;
37import denoptim.exception.DENOPTIMException;
38import denoptim.utils.DummyAtomHandler;
39
46public class FragmentTest
47{
48 private static final String APRULE = "MyRule";
49 private static final String APSUBRULE = "1";
50 private static final String APCLASS = APRULE
51 + DENOPTIMConstants.SEPARATORAPPROPSCL + APSUBRULE;
52 private static final String APCSEP = DENOPTIMConstants.SEPARATORAPPROPSCL;
53
54//------------------------------------------------------------------------------
55
56 @Test
57 public void testHandlingAPsAsObjOrProperty() throws Exception
58 {
59 Fragment frg1 = new Fragment();
60 Atom a1 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
61 Atom a2 = new Atom("C", new Point3d(new double[]{1.0, 1.1, 2.2}));
62 Atom a3 = new Atom("C", new Point3d(new double[]{2.0, 1.1, 2.2}));
63 frg1.addAtom(a1);
64 frg1.addAtom(a2);
65 frg1.addAtom(a3);
66 frg1.addBond(new Bond(a1, a2));
67 frg1.addBond(new Bond(a2, a3));
69 new Point3d(new double[]{0.0, 2.2, 3.3}));
71 new Point3d(new double[]{0.0, 0.0, 3.3}));
73 new Point3d(new double[]{0.0, 0.0, 1.1}));
75 new Point3d(new double[]{3.0, 0.0, 3.3}));
76
78 String clsStr = frg1.getProperty(DENOPTIMConstants.APSTAG).toString();
79
80 Fragment frg2 = new Fragment();
81 Atom a4 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
82 Atom a5 = new Atom("C", new Point3d(new double[]{1.0, 1.1, 2.2}));
83 Atom a6 = new Atom("C", new Point3d(new double[]{2.0, 1.1, 2.2}));
84 frg2.addAtom(a4);
85 frg2.addAtom(a5);
86 frg2.addAtom(a6);
87 frg2.addBond(new Bond(a4, a5));
88 frg2.addBond(new Bond(a5, a6));
91
92 assertEquals(frg1.getNumberOfAPs(),frg2.getNumberOfAPs(),
93 "Equality of #AP");
94 assertEquals(frg1.getAPCountOnAtom(0),frg2.getAPCountOnAtom(0),
95 "Equality of #AP-on-atom");
96 }
97
98//------------------------------------------------------------------------------
99
100 @Test
101 public void testConversionToIAC() throws Exception
102 {
103 // WARNING: the conversion does not project the atom properties into
104 // molecular properties. So the APs do not appear in the mol properties
105 // unless we project the APs to properties (see projectAPsToProperties)
106
107 Fragment frg1 = new Fragment();
108 Atom a1 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
109 Atom a2 = new Atom("O", new Point3d(new double[]{1.0, 1.1, 2.2}));
110 Atom a3 = new Atom("C", new Point3d(new double[]{2.0, 1.1, 2.2}));
111 frg1.addAtom(a1);
112 frg1.addAtom(a2);
113 frg1.addAtom(a3);
114 frg1.addBond(new Bond(a1, a2));
115 frg1.addBond(new Bond(a2, a3));
116 frg1.addAPOnAtom(a1, APClass.make(APCLASS), // 0
117 new Point3d(new double[]{1.0, 2.5, 3.3}));
118 frg1.addAPOnAtom(a1, APClass.make(APRULE+APCSEP+"2"), // 1
119 new Point3d(new double[]{2.0, -2.5, 3.3}));
120 frg1.addAPOnAtom(a1, APClass.make(APRULE+APCSEP+"3"), // 2
121 new Point3d(new double[]{-2.0, -2.5, 3.3}));
122 frg1.addAPOnAtom(a2, APClass.make(APCLASS), // 3
123 new Point3d(new double[]{2.5, 2.5, 3.3}));
124 frg1.addAPOnAtom(a3, APClass.make(APCLASS), // 4
125 new Point3d(new double[]{3.0, 2.5, 3.3}));
126 frg1.addAPOnAtom(a3, APClass.make(APRULE+APCSEP+"2"), // 5
127 new Point3d(new double[]{4.0, -2.5, 3.3}));
128 frg1.addAPOnAtom(a3, APClass.make(APRULE+APCSEP+"4"), // 6
129 new Point3d(new double[]{-4.0, -2.5, 3.3}));
131
132 IAtomContainer iac = frg1.getIAtomContainer();
133
134 Fragment frg2 = new Fragment(iac,
136
137 assertEquals(7,frg1.getNumberOfAPs(),"#APs in frg1");
138 assertEquals(7,frg2.getNumberOfAPs(),"#APs in frg2");
139 assertEquals(3,frg1.getAPCountOnAtom(0),"#APs in frg1 atm0");
140 assertEquals(3,frg2.getAPCountOnAtom(0),"#APs in frg2 atm0");
141 assertEquals(3,frg1.getAPCountOnAtom(2),"#APs in frg1 atm2");
142 assertEquals(3,frg2.getAPCountOnAtom(2),"#APs in frg2 atm2");
143 assertEquals(2,frg1.getSymmetricAPSets().size(),"#SymmAPSets in frg1");
144 assertEquals(2,frg2.getSymmetricAPSets().size(),"#SymmAPSets in frg2");
145 assertTrue(frg1.getSymmetricAPs(frg1.getAP(0)).contains(frg1.getAP(4)),
146 "SymmSet [0,4] in frg1");
147 assertTrue(frg2.getSymmetricAPs(frg2.getAP(0)).contains(frg2.getAP(4)),
148 "SymmSet [0,4] in frg2");
149 assertTrue(frg1.getSymmetricAPs(frg1.getAP(1)).contains(frg1.getAP(5)),
150 "SymmSet [1,5] in frg1");
151 assertTrue(frg2.getSymmetricAPs(frg2.getAP(1)).contains(frg2.getAP(5)),
152 "SymmSet [1,5] in frg2");
153 }
154
155//------------------------------------------------------------------------------
156
158 {
159 Fragment v = new Fragment();
160 Atom a1 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
161 Atom a2 = new Atom("C", new Point3d(new double[]{1.0, 1.1, 2.2}));
162 Atom a3 = new Atom("C", new Point3d(new double[]{2.0, 1.1, 2.2}));
163 v.addAtom(a1);
164 v.addAtom(a2);
165 v.addAtom(a3);
166 v.addBond(new Bond(a1, a2));
167 v.addBond(new Bond(a2, a3));
169 new Point3d(new double[]{0.0, 2.2, 3.3}));
171 new Point3d(new double[]{0.0, 0.0, 3.3}));
173 new Point3d(new double[]{0.0, 0.0, 1.1}));
175 new Point3d(new double[]{3.0, 0.0, 3.3}));
176
177 ArrayList<SymmetricAPs> ssaps = new ArrayList<SymmetricAPs>();
178 SymmetricAPs ss = new SymmetricAPs();
179 ss.add(v.getAP(0));
180 ss.add(v.getAP(1));
181 ss.add(v.getAP(2));
182 ssaps.add(ss);
183 //NB: customised symmetry set that does not correspond to the
184 // definition of symmetry as perceived automatically by
185 // Fragment.identifySymmetryRelatedAPSets
186 // This because we want to test if the symmetric set is properly
187 // serialized/deserialized.
188 v.setSymmetricAPSets(ssaps);
189 v.setVertexId(18);
190 v.setAsRCV(true);
191 //NB thy bond type is check by other methods: do not change it.
193
194 return v;
195 }
196
197//------------------------------------------------------------------------------
198
199 @Test
200 public void testClone() throws Exception
201 {
203 v.setProperty("PROPNAME","PROVALUE");
204
205 Vertex c = v.clone();
206
207 assertEquals(4,((Fragment) c).getNumberOfAPs(),
208 "Number of APs");
209 assertEquals(1,((Fragment) c).getAPCountOnAtom(0),
210 "Size APs on atm0");
211 assertEquals(3,((Fragment) c).getAPCountOnAtom(2),
212 "Size APs on atm2");
213 assertEquals(4,c.getAttachmentPoints().size(),
214 "Number of APs (B)");
215 assertEquals(1,c.getSymmetricAPSets().size(),
216 "Number of symmetric sets");
217 assertEquals(3,c.getSymmetricAPSets().get(0).size(),
218 "Number of symmetric APs in set");
219 assertEquals(v.getVertexId(), c.getVertexId(),
220 "Vertex ID");
221 assertEquals(v.getNumberOfAPs(), c.getNumberOfAPs(),
222 "Number of APS");
223 assertEquals(v.getSymmetricAPSets().size(),
224 c.getSymmetricAPSets().size(),
225 "Number of SymAPs sets");
226 assertEquals(v.isRCV(), c.isRCV(),
227 "RCV flag");
228 assertNotEquals(v.hashCode(), c.hashCode(),
229 "Hash code");
230 assertEquals(v.getBuildingBlockType(),
232 "Building bloc ktype");
233 assertEquals("PROVALUE",c.getProperty("PROPNAME"));
234 }
235
236//------------------------------------------------------------------------------
237
239 {
240 Fragment v = new Fragment();
241 Atom a1 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
242 Atom a2 = new Atom("O", new Point3d(new double[]{1.0, 1.1, 2.2}));
243 Atom a3 = new Atom("C", new Point3d(new double[]{2.0, 1.1, 2.2}));
244 v.addAtom(a1);
245 v.addAtom(a2);
246 v.addAtom(a3);
247 v.addBond(new Bond(a1, a2));
248 v.addBond(new Bond(a2, a3));
249 v.addAPOnAtom(a3, APClass.make("apc:1"),
250 new Point3d(new double[]{0.0, 2.2, 3.3}));
251 v.addAPOnAtom(a3, APClass.make("apc:2"),
252 new Point3d(new double[]{0.0, 0.0, 3.3}));
253 v.addAPOnAtom(a3, APClass.make("foo:0"),
254 new Point3d(new double[]{0.0, 0.0, 1.1}));
255 v.addAPOnAtom(a1, APClass.make("foo:0"),
256 new Point3d(new double[]{3.0, 0.0, 3.3}));
257 return v;
258 }
259
260//------------------------------------------------------------------------------
261
268 {
269 Fragment v = new Fragment();
270 Atom a1 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
271 Atom a2 = new Atom("O", new Point3d(new double[]{1.0, 1.1, 2.2}));
272 Atom a3 = new Atom("H", new Point3d(new double[]{2.0, 1.1, 2.2})); // <<= here is the difference
273 v.addAtom(a1);
274 v.addAtom(a2);
275 v.addAtom(a3);
276 v.addBond(new Bond(a1, a2));
277 v.addBond(new Bond(a2, a3));
278 v.addAPOnAtom(a3, APClass.make("apc:1"),
279 new Point3d(new double[]{0.0, 2.2, 3.3}));
280 v.addAPOnAtom(a3, APClass.make("apc:2"),
281 new Point3d(new double[]{0.0, 0.0, 3.3}));
282 v.addAPOnAtom(a3, APClass.make("foo:0"),
283 new Point3d(new double[]{0.0, 0.0, 1.1}));
284 v.addAPOnAtom(a1, APClass.make("foo:0"),
285 new Point3d(new double[]{3.0, 0.0, 3.3}));
286 return v;
287 }
288
289//------------------------------------------------------------------------------
290
297 {
298 Fragment v = new Fragment();
299 Atom a1 = new Atom("C", new Point3d(new double[]{0.0, 1.1, 2.2}));
300 Atom a2 = new Atom("O", new Point3d(new double[]{1.0, 1.1, 2.2}));
301 Atom a3 = new Atom("C", new Point3d(new double[]{2.0, 1.1, 2.2}));
302 v.addAtom(a1);
303 v.addAtom(a2);
304 v.addAtom(a3);
305 v.addBond(new Bond(a1, a2));
306 v.addBond(new Bond(a2, a3));
307 v.addAPOnAtom(a3, APClass.make("apc:1"),
308 new Point3d(new double[]{0.0, 2.2, 3.3}));
309 v.addAPOnAtom(a3, APClass.make("apc:2"),
310 new Point3d(new double[]{0.0, 0.0, 3.3}));
311 v.addAPOnAtom(a3, APClass.make("foo:0"),
312 new Point3d(new double[]{0.0, 0.0, 1.1}));
313 v.addAPOnAtom(a1, APClass.make("foo:1"), // <<= here is the difference
314 new Point3d(new double[]{3.0, 0.0, 3.3}));
315 return v;
316 }
317
318//------------------------------------------------------------------------------
319
320 @Test
321 public void testIsomorphicTo() throws Exception
322 {
323 Fragment vA = makeFragmentA();
324 Fragment vB = makeFragmentB();
325 Fragment vC = makeFragmentC();
326 assertTrue(vA.isIsomorphicTo(vA));
327 assertTrue(vB.isIsomorphicTo(vB));
328 assertTrue(vC.isIsomorphicTo(vC));
329
330 assertFalse(vA.isIsomorphicTo(vB));
331 assertFalse(vB.isIsomorphicTo(vA));
332 assertFalse(vA.isIsomorphicTo(vC));
333 assertFalse(vB.isIsomorphicTo(vC));
334 }
335
336//------------------------------------------------------------------------------
337
338 @Test
339 public void testIsomorphicInLinear() throws Exception
340 {
341 Fragment vA = makeFragmentA();
343 Fragment vB = makeFragmentA();
344
345 assertTrue(vA.isIsomorphicTo(vA));
346 assertTrue(vA.isIsomorphicTo(vB));
347 assertTrue(vB.isIsomorphicTo(vA));
348 }
349
350//------------------------------------------------------------------------------
351
352
353}
General set of constants used in DENOPTIM.
static final String APSTAG
SDF tag defining attachment points.
static final String SEPARATORAPPROPSCL
Separator between APClass and APSubClass and coordinates.
static APClass make(String ruleAndSubclass)
Creates an APClass if it does not exist already, or returns the reference to the existing instance.
Definition: APClass.java:136
Class representing a continuously connected portion of chemical object holding attachment points.
Definition: Fragment.java:61
void setSymmetricAPSets(List< SymmetricAPs > sAPs)
Definition: Fragment.java:1128
int getAPCountOnAtom(int srcAtmId)
Returns the number of APs currently defined on a specific atom source.
Definition: Fragment.java:475
AttachmentPoint addAPOnAtom(IAtom srcAtm, APClass apc, Point3d vector)
Add an attachment point to the specifies atom.
Definition: Fragment.java:424
void addBond(IBond bond)
Definition: Fragment.java:871
Fragment clone()
Returns a deep copy of this fragments.
Definition: Fragment.java:733
boolean isIsomorphicTo(Vertex other)
Checks for isomorphism of the graph representation of this and another fragment.
Definition: Fragment.java:1066
void addAtom(IAtom atom)
Definition: Fragment.java:836
IAtomContainer getIAtomContainer()
Definition: Fragment.java:788
void projectAPsToProperties()
Finds the DENOPTIMAttachmentPoint objects defined as properties of the atoms in this container,...
Definition: Fragment.java:693
List< SymmetricAPs > getSymmetricAPSets()
Definition: Fragment.java:1148
void projectPropertyToAP()
Uses the molecular property defining attachment points to create the DENOPTIMAttachmentPoint objects ...
Definition: Fragment.java:572
Unit test for DENOPTIMFragment.
static Fragment makeFragment()
static final String APCSEP
static Fragment makeFragmentC()
Differs from the fragment produced by makeFragmentA() only by the one APCLass.
static final String APCLASS
static Fragment makeFragmentA()
static final String APSUBRULE
static final String APRULE
static Fragment makeFragmentB()
Differs from the fragment produced by makeFragmentA() only by the identity of one atom.
A collection of AttachmentPoints that are related by a relation that we call "symmetry",...
boolean add(T item)
Adds an item to this list, if not already present.
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
void setVertexId(long vertexId2)
Definition: Vertex.java:261
Vertex.BBType getBuildingBlockType()
Definition: Vertex.java:298
void setAsRCV(boolean isRCV)
Definition: Vertex.java:254
abstract List< AttachmentPoint > getAttachmentPoints()
abstract List< SymmetricAPs > getSymmetricAPSets()
SymmetricAPs getSymmetricAPs(AttachmentPoint ap)
For the given attachment point index locate the symmetric partners i.e.
Definition: Vertex.java:333
Object getProperty(Object property)
Definition: Vertex.java:1136
void setBuildingBlockType(Vertex.BBType buildingBlockType)
Definition: Vertex.java:305
void setProperty(Object key, Object property)
Definition: Vertex.java:1148
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Definition: Vertex.java:920
Toll to add/remove dummy atoms from linearities or multi-hapto sites.
static void addDummiesOnLinearities(Fragment frag, double angLim)
Append dummy atoms on otherwise linear arrangements of atoms.
The type of building block.
Definition: Vertex.java:86