$darkmode
DENOPTIM
TinkerUtilsTest.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.integration.tinker;
20
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertTrue;
23
24import java.io.File;
25import java.nio.file.Files;
26import java.nio.file.Path;
27import java.util.ArrayList;
28import java.util.List;
29
30import org.junit.jupiter.api.Test;
31import org.junit.jupiter.api.io.TempDir;
32
33import denoptim.molecularmodeling.zmatrix.ZMatrix;
34import denoptim.molecularmodeling.zmatrix.ZMatrixAtom;
35
42public class TinkerUtilsTest
43{
44 @TempDir
45 Path tempDir;
46
47//------------------------------------------------------------------------------
48
49 @Test
50 public void testINTRoundTrip() throws Exception
51 {
52 ZMatrix zmat = new ZMatrix();
53 zmat.setId("TestMolecule");
54 ZMatrixAtom zAtom0 = new ZMatrixAtom(
55 0, "C", "C.3", null, null, null,
56 null, null, null, null);
57 ZMatrixAtom zAtom1 = new ZMatrixAtom(
58 1, "C", "C.3", zAtom0, null, null,
59 1.0, null, null, null);
60 ZMatrixAtom zAtom2 = new ZMatrixAtom(
61 2, "H", "H.1", zAtom1, zAtom0, null,
62 1.0, 90.0, null, null);
63 ZMatrixAtom zAtom3 = new ZMatrixAtom(
64 3, "H", "H.1", zAtom2, zAtom1, zAtom0,
65 1.0, 90.0, 90.0, 0);
66 ZMatrixAtom zAtom4 = new ZMatrixAtom(
67 4, "P", "P.3", zAtom3, zAtom2, zAtom1,
68 1.0, 90.0, -90.0, 0);
69 ZMatrixAtom zAtom5 = new ZMatrixAtom(
70 5, "O", "O.3", zAtom4, zAtom3, zAtom2,
71 1.0, 90.0, 180.0, 0);
72 ZMatrixAtom zAtom6 = new ZMatrixAtom(
73 6, "Cl", "Cl.1", zAtom4, zAtom3, zAtom5,
74 1.0, 90.0, 90.0, 1);
75 ZMatrixAtom zAtom7 = new ZMatrixAtom(
76 7, "H", "H.1", zAtom4, zAtom3, zAtom5,
77 1.0, 90.0, 90.0, -1);
78
79 zmat.addAtom(zAtom0);
80 zmat.addAtom(zAtom1);
81 zmat.addAtom(zAtom2);
82 zmat.addAtom(zAtom3);
83 zmat.addAtom(zAtom4);
84 zmat.addAtom(zAtom5);
85 zmat.addAtom(zAtom6);
86 zmat.addAtom(zAtom7);
87
88 zmat.addBond(0, 1);
89 zmat.addBond(1, 2);
90 zmat.addBond(2, 3);
91 zmat.addBond(5, 4);
92 zmat.addBond(7, 4);
93 zmat.addBond(6, 4);
94 zmat.addBond(0, 6);
95
96 File outputFile = tempDir.resolve("output.int").toFile();
97 TinkerUtils.writeTinkerINT(outputFile.getAbsolutePath(), zmat);
98
99 ZMatrix readInZMat = TinkerUtils.readTinkerINT(outputFile.getAbsolutePath());
100
101 assertTrue(readInZMat.equals(zmat));
102 }
103
104//------------------------------------------------------------------------------
105
106 @Test
107 public void testWriteTinkerINT() throws Exception
108 {
109 ZMatrix zmat = new ZMatrix();
110 zmat.setId("TestMolecule");
111 ZMatrixAtom zAtom0 = new ZMatrixAtom(
112 0, "C", "C.3", null, null, null,
113 null, null, null, null);
114 ZMatrixAtom zAtom1 = new ZMatrixAtom(
115 1, "C", "C.3", zAtom0, null, null,
116 1.0, null, null, null);
117 ZMatrixAtom zAtom2 = new ZMatrixAtom(
118 2, "H", "H.1", zAtom1, zAtom0, null,
119 1.0, 90.0, null, null);
120 ZMatrixAtom zAtom3 = new ZMatrixAtom(
121 3, "H", "H.1", zAtom2, zAtom1, zAtom0,
122 1.0, 90.0, 90.0, 0);
123 ZMatrixAtom zAtom4 = new ZMatrixAtom(
124 4, "P", "P.3", zAtom3, zAtom2, zAtom1,
125 1.0, 90.0, -90.0, 0);
126 ZMatrixAtom zAtom5 = new ZMatrixAtom(
127 5, "O", "O.3", zAtom4, zAtom3, zAtom2,
128 1.0, 90.0, 180.0, 0);
129 ZMatrixAtom zAtom6 = new ZMatrixAtom(
130 6, "Cl", "Cl.1", zAtom4, zAtom3, zAtom5,
131 1.0, 90.0, 90.0, 1);
132 ZMatrixAtom zAtom7 = new ZMatrixAtom(
133 7, "H", "H.1", zAtom4, zAtom3, zAtom5,
134 1.0, 90.0, 90.0, -1);
135
136 zmat.addAtom(zAtom0);
137 zmat.addAtom(zAtom1);
138 zmat.addAtom(zAtom2);
139 zmat.addAtom(zAtom3);
140 zmat.addAtom(zAtom4);
141 zmat.addAtom(zAtom5);
142 zmat.addAtom(zAtom6);
143 zmat.addAtom(zAtom7);
144
145 zmat.addBond(0, 1);
146 zmat.addBond(1, 2);
147 zmat.addBond(2, 3);
148 zmat.addBond(5, 4);
149 zmat.addBond(7, 4);
150 zmat.addBond(6, 4);
151 zmat.addBond(0, 6);
152
153 File outputFile = tempDir.resolve("output.int").toFile();
154 TinkerUtils.writeTinkerINT(outputFile.getAbsolutePath(), zmat);
155
156 assertTrue(outputFile.exists(), "Output file should be created");
157 assertTrue(outputFile.length() > 0, "Output file should not be empty");
158
159 // Verify file content by reading it as text and checking individual lines
160 String content = Files.readString(outputFile.toPath());
161 String[] allLines = content.split("\\r?\\n");
162 List<String> strippedLines = new ArrayList<>();
163 for (String line : allLines)
164 {
165 String stripped = line.trim();
166 if (!stripped.isEmpty())
167 {
168 strippedLines.add(stripped);
169 }
170 }
171
172 // Check that specific lines match the expected stripped content
173 assertEquals("8 TestMolecule", strippedLines.get(0));
174 assertEquals("1 C C.3", strippedLines.get(1));
175 assertEquals("2 C C.3 1 1.00000", strippedLines.get(2));
176 assertEquals("3 H H.1 2 1.00000 1 90.0000", strippedLines.get(3));
177 assertEquals("8 H H.1 5 1.00000 4 90.0000 6 90.0000 -1",
178 strippedLines.get(8));
179
180 // Check bond pairs to add (after blank line)
181 int bondSectionStart = -1;
182 for (int i = 0; i < strippedLines.size(); i++)
183 {
184 if (strippedLines.get(i).isEmpty() && i + 1 < strippedLines.size())
185 {
186 bondSectionStart = i + 1;
187 break;
188 }
189 }
190 if (bondSectionStart >= 0)
191 {
192 assertTrue(strippedLines.contains("1 7"));
193 assertTrue(strippedLines.contains("5 4"));
194 }
195
196 }
197
198//------------------------------------------------------------------------------
199
200
201//------------------------------------------------------------------------------
202
203}
204
Toolbox of utilities for Tinker style molecular representation.
static ZMatrix readTinkerINT(String filename)
Reads a Tinker INT file.
static void writeTinkerINT(String filename, ZMatrix zmat)
Write Tinker INT file.
Representation of an atom in the ZMatrix.
Definition: ZMatrixAtom.java:9
Representation of an atom container's geometry with internal coordinates.
Definition: ZMatrix.java:27
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
void addAtom(ZMatrixAtom atom)
Add an atom to the ZMatrix.
Definition: ZMatrix.java:61