$darkmode
DENOPTIM
ZMatrixTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2024 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.molecularmodeling.zmatrix;
20
21import static org.junit.Assert.assertNotEquals;
22import static org.junit.jupiter.api.Assertions.assertEquals;
23import static org.junit.jupiter.api.Assertions.assertFalse;
24import static org.junit.jupiter.api.Assertions.assertNotNull;
25import static org.junit.jupiter.api.Assertions.assertNull;
26import static org.junit.jupiter.api.Assertions.assertTrue;
27
28import java.util.List;
29
30import javax.vecmath.Point3d;
31
32import org.junit.jupiter.api.Test;
33import org.openscience.cdk.Atom;
34import org.openscience.cdk.interfaces.IAtom;
35import org.openscience.cdk.interfaces.IBond;
36import org.openscience.cdk.interfaces.IAtomContainer;
37import org.openscience.cdk.silent.SilentChemObjectBuilder;
38
45public class ZMatrixTest
46{
47
48//------------------------------------------------------------------------------
49
50 public ZMatrix getTestZMatrix() throws Exception
51 {
52 ZMatrix zMatrix = new ZMatrix();
53
54 ZMatrixAtom atom0 = new ZMatrixAtom(
55 0, "H", "H.1", null, null, null,
56 null, null, null, null);
57
58 ZMatrixAtom atom1 = new ZMatrixAtom(
59 1, "O", "O.2", atom0, null, null,
60 1.5, null, null, null);
61
62 ZMatrixAtom atom2 = new ZMatrixAtom(
63 2, "C", "C.3", atom1, atom0, null,
64 1.4, 109.5, null, null);
65
66 ZMatrixAtom atom3 = new ZMatrixAtom(
67 3, "H", "H.1", atom2, atom1, atom0,
68 1.4, 109.5, 60.0, 0);
69
70 ZMatrixAtom atom4 = new ZMatrixAtom(
71 4, "H", "H.1", atom2, atom1, atom3,
72 1.4, 109.5, 109.0, 1);
73
74 ZMatrixAtom atom5 = new ZMatrixAtom(
75 5, "H", "H.1", atom4, atom2, atom1,
76 1.4, 109.5, 0.0, 0);
77
78 zMatrix.addAtom(atom0);
79 zMatrix.addAtom(atom1);
80 zMatrix.addAtom(atom2);
81 zMatrix.addAtom(atom3);
82 zMatrix.addAtom(atom4);
83 zMatrix.addAtom(atom5);
84
85 zMatrix.addBond(atom0, atom1);
86 zMatrix.addBond(atom1, atom2);
87 zMatrix.addBond(atom2, atom3);
88 zMatrix.addBond(atom2, atom4);
89 zMatrix.addBond(atom2, atom5);
90
91 return zMatrix;
92 }
93
94//------------------------------------------------------------------------------
95
96 @Test
97 public void testAddAtom() throws Exception
98 {
99 ZMatrix zMatrix = getTestZMatrix();
100
101 assertEquals(6, zMatrix.getAtomCount());
102 assertEquals(5, zMatrix.getBondCount());
103
104 ZMatrixAtom atom1 = zMatrix.getAtom(1);
105
106 //NB: this is a duplicate atom!
107 zMatrix.addAtom(atom1);
108
109 assertEquals(7, zMatrix.getAtomCount());
110 assertEquals(5, zMatrix.getBondCount());
111 }
112
113//------------------------------------------------------------------------------
114
115 @Test
116 public void testRemoveAtom() throws Exception
117 {
118 ZMatrix zMatrix = getTestZMatrix();
119
120 zMatrix.removeAtom(zMatrix.getAtom(2));
121 assertEquals(5, zMatrix.getAtomCount());
122 assertEquals(1, zMatrix.getBondCount());
123 }
124
125//------------------------------------------------------------------------------
126
127 @Test
128 public void testGetIndex() throws Exception
129 {
130 ZMatrix zMatrix = getTestZMatrix();
131
132 assertEquals(0, zMatrix.getIndex(zMatrix.getAtom(0)));
133 assertEquals(1, zMatrix.getIndex(zMatrix.getAtom(1)));
134 assertEquals(2, zMatrix.getIndex(zMatrix.getAtom(2)));
135
136 ZMatrixAtom atomNotInList = new ZMatrixAtom(
137 99, "H", "H.1", null, null, null, 0.0, 0.0, 0.0, 0);
138 assertEquals(-1, zMatrix.getIndex(atomNotInList));
139 }
140
141//------------------------------------------------------------------------------
142
143 @Test
144 public void testGetBondRefAtomIndex() throws Exception
145 {
146 ZMatrix zMatrix = getTestZMatrix();
147
148 assertEquals(-1, zMatrix.getBondRefAtomIndex(0));
149 assertEquals(0, zMatrix.getBondRefAtomIndex(1));
150 assertEquals(1, zMatrix.getBondRefAtomIndex(2));
151 assertEquals(4, zMatrix.getBondRefAtomIndex(5));
152 }
153
154//------------------------------------------------------------------------------
155
156 @Test
157 public void testGetAngleRefAtomIndex() throws Exception
158 {
159 ZMatrix zMatrix = getTestZMatrix();
160
161 assertEquals(-1, zMatrix.getAngleRefAtomIndex(0));
162 assertEquals(-1, zMatrix.getAngleRefAtomIndex(1));
163 assertEquals(0, zMatrix.getAngleRefAtomIndex(2));
164 assertEquals(2, zMatrix.getAngleRefAtomIndex(5));
165 }
166
167//------------------------------------------------------------------------------
168
169 @Test
170 public void testGetAngle2RefAtomIndex() throws Exception
171 {
172 ZMatrix zMatrix = getTestZMatrix();
173
174 assertEquals(-1, zMatrix.getAngle2RefAtomIndex(0));
175 assertEquals(-1, zMatrix.getAngle2RefAtomIndex(1));
176 assertEquals(-1, zMatrix.getAngle2RefAtomIndex(2));
177 assertEquals(3, zMatrix.getAngle2RefAtomIndex(4));
178 assertEquals(1, zMatrix.getAngle2RefAtomIndex(5));
179 }
180
181//------------------------------------------------------------------------------
182
183 @Test
184 public void testGetChiralFlag() throws Exception
185 {
186 ZMatrix zMatrix = getTestZMatrix();
187
188 assertNull(zMatrix.getChiralFlag(0));
189 assertNull(zMatrix.getChiralFlag(1));
190 assertNull(zMatrix.getChiralFlag(2));
191 assertEquals(0, zMatrix.getChiralFlag(3));
192 assertEquals(1, zMatrix.getChiralFlag(4));
193 assertEquals(0, zMatrix.getChiralFlag(5));
194 }
195
196//------------------------------------------------------------------------------
197
198 @Test
199 public void testGetIdAndSetId() throws Exception
200 {
201 ZMatrix zMatrix = getTestZMatrix();
202 assertNull(zMatrix.getId());
203
204 zMatrix.setId("test-zmatrix-1");
205 assertEquals("test-zmatrix-1", zMatrix.getId());
206
207 zMatrix.setId("another-id");
208 assertEquals("another-id", zMatrix.getId());
209
210 zMatrix.setId(null);
211 assertNull(zMatrix.getId());
212 }
213
214//------------------------------------------------------------------------------
215
216 @Test
217 public void testAddBond() throws Exception
218 {
219 ZMatrix zMatrix = getTestZMatrix();
220 assertEquals(5, zMatrix.getBondCount());
221
222 zMatrix.addBond(zMatrix.getAtom(0), zMatrix.getAtom(2));
223 assertEquals(6, zMatrix.getBondCount());
224 }
225
226//------------------------------------------------------------------------------
227
228 @Test
229 public void testDelBond() throws Exception
230 {
231 ZMatrix zMatrix = getTestZMatrix();
232 assertEquals(5, zMatrix.getBondCount());
233
234 zMatrix.delBond(zMatrix.getAtom(0), zMatrix.getAtom(1));
235 assertEquals(4, zMatrix.getBondCount());
236 }
237
238//------------------------------------------------------------------------------
239
240 @Test
241 public void testGetBondsToAdd() throws Exception
242 {
243 ZMatrix zMatrix = getTestZMatrix();
244
245 List<int[]> bondsToAdd = zMatrix.getBondsToAdd();
246
247 assertEquals(1, bondsToAdd.size());
248
249 int[] bondToAdd = bondsToAdd.get(0);
250 assertEquals(2, Math.min(bondToAdd[0], bondToAdd[1]));
251 assertEquals(5, Math.max(bondToAdd[0], bondToAdd[1]));
252 }
253
254//------------------------------------------------------------------------------
255
256 @Test
257 public void testGetBondsToDel() throws Exception
258 {
259 ZMatrix zMatrix = getTestZMatrix();
260 List<int[]> bondsToDel = zMatrix.getBondsToDel();
261
262 assertEquals(1, bondsToDel.size());
263
264 int[] bondToDel = bondsToDel.get(0);
265 assertEquals(4, Math.min(bondToDel[0], bondToDel[1]));
266 assertEquals(5, Math.max(bondToDel[0], bondToDel[1]));
267 }
268
269//------------------------------------------------------------------------------
270
271 @Test
272 public void testClone() throws Exception
273 {
274 ZMatrix original = getTestZMatrix();
275
276 ZMatrix cloned = original.clone();
277
278 assertNotNull(cloned);
279 assertEquals(original.getId(), cloned.getId());
280 assertEquals(original.getAtomCount(), cloned.getAtomCount());
281 assertEquals(original.getBondCount(), cloned.getBondCount());
282
283 // Verify atom properties are preserved
284 for (int i = 0; i < original.getAtomCount(); i++)
285 {
286 ZMatrixAtom originalAtom = original.getAtom(i);
287 ZMatrixAtom clonedAtom = cloned.getAtom(i);
288 assertEquals(originalAtom.getId(), clonedAtom.getId());
289 assertEquals(originalAtom.getSymbol(), clonedAtom.getSymbol());
290 assertEquals(originalAtom.getType(), clonedAtom.getType());
291 }
292
293 for (int i = 0; i < original.getBondCount(); i++)
294 {
295 ZMatrixBond originalBond = original.getBond(i);
296 ZMatrixBond clonedBond = cloned.getBond(i);
297 assertEquals(originalBond.getAtm1().getId(), clonedBond.getAtm1().getId());
298 assertEquals(originalBond.getAtm2().getId(), clonedBond.getAtm2().getId());
299 }
300 }
301
302//------------------------------------------------------------------------------
303
304 @Test
305 public void testGetBondData() throws Exception
306 {
307 ZMatrix zMatrix = getTestZMatrix();
308 List<int[]> bondData = zMatrix.getBondData();
309 assertEquals(5, bondData.size());
310
311 int[] bond = bondData.get(0);
312 assertEquals(0, bond[0]);
313 assertEquals(1, bond[1]);
314 // Note: getBondData returns only [id1, id2], no bond order
315 }
316
317//------------------------------------------------------------------------------
318
319 @Test
320 public void testZMatrixEquals() throws Exception
321 {
322 ZMatrix zmat1 = getTestZMatrix();
323 ZMatrix zmat2 = getTestZMatrix();
324
325 assertEquals(zmat1, zmat1);
326 assertEquals(zmat1, zmat2);
327 assertNotEquals(zmat1, null);
328
329 zmat2.setId("Different");
330 assertNotEquals(zmat1, zmat2);
331
332 zmat2 = getTestZMatrix();
333 zmat2.addAtom(new ZMatrixAtom(6, "H", "H.1", null, null, null, 0.0, 0.0, 0.0, 0));
334 assertNotEquals(zmat1, zmat2);
335
336 zmat2 = getTestZMatrix();
337 zmat2.addBond(zmat2.getAtom(0), zmat2.getAtom(2));
338 assertNotEquals(zmat1, zmat2);
339
340 zmat2 = getTestZMatrix();
341 zmat2.delBond(zmat2.getAtom(0), zmat2.getAtom(1));
342 assertNotEquals(zmat1, zmat2);
343 }
344
345//------------------------------------------------------------------------------
346
347 @Test
348 public void testZMatrixHashCode() throws Exception
349 {
350 ZMatrix zmat1 = getTestZMatrix();
351 ZMatrix zmat2 = getTestZMatrix();
352
353 assertEquals(zmat1.hashCode(), zmat2.hashCode(),
354 "Equal ZMatrices should have same hash code");
355
356 zmat2.setId("Different");
357 assertNotEquals(zmat1.hashCode(), zmat2.hashCode());
358 }
359
360//------------------------------------------------------------------------------
361
362 @Test
363 public void testGetZMatrixFromIAC() throws Exception
364 {
365 IAtomContainer mol = SilentChemObjectBuilder.getInstance().newAtomContainer();
366 IAtom atom0 = new Atom("C", new Point3d(0.0, 0.0, 0.0));
367 IAtom atom1 = new Atom("C", new Point3d(0.0, 0.0, 1.0));
368 IAtom atom2 = new Atom("H", new Point3d(1.0, 0.0, 1.0));
369 IAtom atom3 = new Atom("H", new Point3d(1.0, 1.0, 1.0));
370 IAtom atom4 = new Atom("P", new Point3d(1.0, 1.0, 0.0));
371 IAtom atom5 = new Atom("O", new Point3d(1.0, 2.0, 0.0));
372 IAtom atom6 = new Atom("Cl", new Point3d(0.0, 1.0, 0.0));
373 IAtom atom7 = new Atom("H", new Point3d(2.0, 1.0, 0.0));
374 mol.addAtom(atom0);
375 mol.addAtom(atom1);
376 mol.addAtom(atom2);
377 mol.addAtom(atom3);
378 mol.addAtom(atom4);
379 mol.addAtom(atom5);
380 mol.addAtom(atom6);
381 mol.addAtom(atom7);
382 mol.addBond(0, 1, IBond.Order.SINGLE);
383 mol.addBond(1, 2, IBond.Order.SINGLE);
384 mol.addBond(2, 3, IBond.Order.SINGLE);
385 mol.addBond(3, 4, IBond.Order.SINGLE);
386 mol.addBond(4, 5, IBond.Order.SINGLE);
387 mol.addBond(4, 6, IBond.Order.SINGLE);
388 mol.addBond(4, 7, IBond.Order.SINGLE);
389
391
392 assertEquals(8, zmat.getAtomCount());
393 assertEquals(7, zmat.getBondCount());
394 }
395
396//------------------------------------------------------------------------------
397
398 @Test
399 public void testUsesProperDihedral() throws Exception
400 {
401 ZMatrix zmat = getTestZMatrix();
402 assertFalse(zmat.usesProperDihedral(0, 0));
403 assertFalse(zmat.usesProperDihedral(0, 1));
404 assertFalse(zmat.usesProperDihedral(1, 1));
405 assertFalse(zmat.usesProperDihedral(0, 0));
406 assertTrue(zmat.usesProperDihedral(1, 2));
407 assertTrue(zmat.usesProperDihedral(2, 1));
408 assertTrue(zmat.usesProperDihedral(2, 4));
409 assertTrue(zmat.usesProperDihedral(4, 2));
410 assertFalse(zmat.usesProperDihedral(4, 5));
411 assertFalse(zmat.usesProperDihedral(5, 4));
412 assertFalse(zmat.usesProperDihedral(4, 4));
413 assertFalse(zmat.usesProperDihedral(5, 5));
414 assertFalse(zmat.usesProperDihedral(3, 5));
415 assertFalse(zmat.usesProperDihedral(4, 3));
416 assertFalse(zmat.usesProperDihedral(1, 5));
417 }
418
419//------------------------------------------------------------------------------
420}
421
Representation of an atom in the ZMatrix.
Definition: ZMatrixAtom.java:9
String getSymbol()
Get the symbol of the atom.
String getType()
Get the type of the atom.
Representation of a bond in the ZMatrix.
Definition: ZMatrixBond.java:7
ZMatrixAtom getAtm2()
Get the second atom in the bond.
ZMatrixAtom getAtm1()
Get the first atom in the bond.
Representation of an atom container's geometry with internal coordinates.
Definition: ZMatrix.java:27
List< int[]> getBondsToDel()
Get the bonds to delete from the ZMatrix.
Definition: ZMatrix.java:188
int getIndex(ZMatrixAtom atm)
Get the index of the atom.
Definition: ZMatrix.java:246
List< int[]> getBondData()
Get the bond data for the ZMatrix.
Definition: ZMatrix.java:114
void delBond(int a1, int a2)
Delete the bond between the two atoms at the given indices.
Definition: ZMatrix.java:426
int getAtomCount()
Get the number of atoms in the ZMatrix.
Definition: ZMatrix.java:92
Integer getChiralFlag(int index)
Get the chiral flag for the atom at the given index.
Definition: ZMatrix.java:375
void removeAtom(ZMatrixAtom atm)
Remove an atom from the ZMatrix.
Definition: ZMatrix.java:72
String getId()
Get the id of the ZMatrix.
Definition: ZMatrix.java:131
int getBondCount()
Get the number of bonds in the ZMatrix.
Definition: ZMatrix.java:103
int getBondRefAtomIndex(int index)
Get the index of the bond reference atom for the atom at the given index.
Definition: ZMatrix.java:270
ZMatrix clone()
Clone the ZMatrix.
Definition: ZMatrix.java:485
static ZMatrix getZMatrixFromIAC(IAtomContainer mol)
Convert IAtomContainer to ZMatrix.
Definition: ZMatrix.java:545
ZMatrixBond getBond(int index)
Get the bond at the given index.
Definition: ZMatrix.java:414
boolean usesProperDihedral(int idx1, int idx2)
Check if the dihedral between the two atoms at the given indices uses proper torsion.
Definition: ZMatrix.java:389
void setId(String id)
Set the id of the ZMatrix.
Definition: ZMatrix.java:142
void addBond(int a1, int a2)
Add a bond between the two atoms at the given indices.
Definition: ZMatrix.java:461
List< int[]> getBondsToAdd()
Get the bonds to add to the Z-matrix.
Definition: ZMatrix.java:154
ZMatrixAtom getAtom(int index)
Get the atom at the given index.
Definition: ZMatrix.java:223
void addAtom(ZMatrixAtom atom)
Add an atom to the ZMatrix.
Definition: ZMatrix.java:61
int getAngle2RefAtomIndex(int index)
Get the index of the second angle reference atom for the atom at the given index.
Definition: ZMatrix.java:325
int getAngleRefAtomIndex(int index)
Get the index of the angle reference atom for the atom at the given index.
Definition: ZMatrix.java:297