$darkmode
DENOPTIM
DENOPTIMgsonTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 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.json;
20
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertNotEquals;
23import static org.junit.jupiter.api.Assertions.assertTrue;
24
25import javax.vecmath.Point3d;
26
27import org.junit.jupiter.api.Test;
28import org.openscience.cdk.Atom;
29import org.openscience.cdk.Bond;
30import org.openscience.cdk.PseudoAtom;
31import org.openscience.cdk.interfaces.IAtom;
32import org.openscience.cdk.interfaces.IAtomContainer;
33import org.openscience.cdk.interfaces.IBond;
34import org.openscience.cdk.interfaces.IChemObjectBuilder;
35import org.openscience.cdk.silent.SilentChemObjectBuilder;
36
37import com.google.gson.Gson;
38
39import denoptim.graph.Fragment;
40import denoptim.graph.FragmentTest;
41import denoptim.graph.Template;
42import denoptim.graph.TemplateTest;
43import denoptim.graph.Vertex;
44import denoptim.graph.Vertex.BBType;
45import denoptim.utils.MoleculeUtils;
46import denoptim.utils.MutationType;
47
54public class DENOPTIMgsonTest
55{
56 private IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
57
58//------------------------------------------------------------------------------
59
60 @Test
61 public void testIAtomContainerToJSONAndBack() throws Exception
62 {
63 IAtomContainer iac = builder.newAtomContainer();
64 IAtom a1 = new Atom("H");
65 a1.setPoint3d(new Point3d(2.624, -4.243, 6.465));
66 IAtom a2 = new Atom("Ru");
67 a2.setPoint3d(new Point3d(-0.001, 23.943, -0.115));
68 IAtom a3 = new PseudoAtom("RCA");
69 a3.setPoint3d(new Point3d(0.0, 0.0, 0.0));
70 iac.addAtom(a1);
71 iac.addAtom(a2);
72 iac.addAtom(a3);
73 iac.addBond(new Bond(a1, a2, IBond.Order.SINGLE));
74 iac.addBond(new Bond(a1, a3, IBond.Order.TRIPLE));
75 iac.addBond(new Bond(a2, a3, IBond.Order.UNSET));
76
77 Gson jsonWriter = DENOPTIMgson.getWriter();
78 String jsonString = jsonWriter.toJson(iac);
79
80 //System.out.println("JSON of iac: "+jsonString);
81
82 Gson jsonReader = DENOPTIMgson.getReader();
83 IAtomContainer iacFromJSON = jsonReader.fromJson(jsonString,
84 IAtomContainer.class);
85
86 assertEquals(iac.getAtomCount(),iacFromJSON.getAtomCount(),"#Atoms");
87 assertEquals(iac.getBondCount(),iacFromJSON.getBondCount(),"#Bonds");
88 for (int i=0; i<iac.getAtomCount(); i++)
89 {
90 IAtom atmOri = iac.getAtom(i);
91 IAtom atmJsn = iacFromJSON.getAtom(i);
92 assertEquals(MoleculeUtils.getSymbolOrLabel(atmOri),
94 "Symbol atom "+i);
95 double distance = MoleculeUtils.getPoint3d(atmOri).distance(
97 assertTrue(areCloseEnough(0.0,distance), "Coordinates atom "+i);
98 }
99 for (int i=0; i<iac.getBondCount(); i++)
100 {
101 IBond bndOri = iac.getBond(i);
102 IBond bndJsn = iacFromJSON.getBond(i);
103 assertEquals(iac.indexOf(bndOri.getAtom(0)),
104 iacFromJSON.indexOf(bndJsn.getAtom(0)),
105 "1st atom in bond "+i);
106 assertEquals(iac.indexOf(bndOri.getAtom(1)),
107 iacFromJSON.indexOf(bndJsn.getAtom(1)),
108 "2nd atom in bond "+i);
109 assertEquals(bndOri.getOrder(),bndJsn.getOrder(),
110 "Order in bond "+i);
111 }
112 }
113
114//------------------------------------------------------------------------------
115
116 @Test
117 public void testTemplateSerialization() throws Exception
118 {
120 tmpl.setBuildingBlockId(-206); //just any number
121 tmpl.setBuildingBlockType(BBType.CAP); //just a type easy to spot
124
125 Gson jsonWriter = DENOPTIMgson.getWriter();
126 String jsonString = jsonWriter.toJson(tmpl);
127
128 //System.out.println("JSON of template: "+jsonString);
129
130 Gson jsonReader = DENOPTIMgson.getReader();
131 Vertex tmplFromJSON = jsonReader.fromJson(jsonString,
132 Vertex.class);
133
134 assertEquals(tmpl.getNumberOfAPs(), tmplFromJSON.getNumberOfAPs(),
135 "Number of APs");
136 assertEquals(tmpl.getAttachmentPoints().size(),
137 tmplFromJSON.getAttachmentPoints().size(),
138 "Number of APs (B)");
139 assertEquals(tmpl.getSymmetricAPSets().size(),
140 tmplFromJSON.getSymmetricAPSets().size(),
141 "Number of symmetric sets");
142 assertEquals(tmpl.getVertexId(), tmplFromJSON.getVertexId(),
143 "Vertex ID");
144 assertEquals(tmpl.isRCV(), tmplFromJSON.isRCV(),
145 "RCV flag");
146 assertNotEquals(tmpl.hashCode(), tmplFromJSON.hashCode(),
147 "Hash code");
148 assertEquals(tmpl.getBuildingBlockId(),
149 tmplFromJSON.getBuildingBlockId(),
150 "Building block ID");
151 assertEquals(tmpl.getBuildingBlockType(),
152 tmplFromJSON.getBuildingBlockType(),
153 "Building block type");
154 IAtomContainer iac = tmpl.getIAtomContainer();
155 IAtomContainer iacFromJSON = tmplFromJSON.getIAtomContainer();
156 for (int i=0; i<tmpl.getIAtomContainer().getAtomCount(); i++)
157 {
158 IAtom atmOri = iac.getAtom(i);
159 IAtom atmJsn = iacFromJSON.getAtom(i);
160 assertEquals(MoleculeUtils.getSymbolOrLabel(atmOri),
162 "Symbol atom "+i);
163 double distance = MoleculeUtils.getPoint3d(atmOri).distance(
164 MoleculeUtils.getPoint3d(atmJsn));
165 assertTrue(areCloseEnough(0.0,distance), "Coordinates atom "+i);
166 }
167 for (int i=0; i<iac.getBondCount(); i++)
168 {
169 IBond bndOri = iac.getBond(i);
170 IBond bndJsn = iacFromJSON.getBond(i);
171 assertEquals(iac.indexOf(bndOri.getAtom(0)),
172 iacFromJSON.indexOf(bndJsn.getAtom(0)),
173 "1st atom in bond "+i);
174 assertEquals(iac.indexOf(bndOri.getAtom(1)),
175 iacFromJSON.indexOf(bndJsn.getAtom(1)),
176 "2nd atom in bond "+i);
177 assertEquals(bndOri.getOrder(),bndJsn.getOrder(),
178 "Order in bond "+i);
179 }
180 }
181
182//------------------------------------------------------------------------------
183
184 @Test
185 public void testMolecularFragmentSerialization() throws Exception
186 {
188 frag.setBuildingBlockId(-206); //just any number
189 frag.setBuildingBlockType(BBType.CAP); //just a type easy to spot
192
193 Gson jsonWriter = DENOPTIMgson.getWriter();
194 String jsonString = jsonWriter.toJson(frag);
195
196 //System.out.println("JSON of DENOPTIMFragment: "+jsonString);
197
198 Gson jsonReader = DENOPTIMgson.getReader();
199 Vertex fragFromJSON = jsonReader.fromJson(jsonString,
200 Vertex.class);
201
202 assertEquals(frag.getNumberOfAPs(), fragFromJSON.getNumberOfAPs(),
203 "Number of APs");
204 assertEquals(frag.getAPCountOnAtom(0),
205 ((Fragment) fragFromJSON).getAPCountOnAtom(0),
206 "Size APs on atm0");
207 assertEquals(frag.getAPCountOnAtom(2),
208 ((Fragment) fragFromJSON).getAPCountOnAtom(2),
209 "Size APs on atm2");
210 assertEquals(frag.getAttachmentPoints().size(),
211 fragFromJSON.getAttachmentPoints().size(),
212 "Number of APs (B)");
213 assertEquals(frag.getSymmetricAPSets().size(),
214 fragFromJSON.getSymmetricAPSets().size(),
215 "Number of symmetric sets");
216 assertEquals(frag.getSymmetricAPSets().get(0).size(),
217 fragFromJSON.getSymmetricAPSets().get(0).size(),
218 "Number of symmetric APs in set");
219 assertEquals(frag.getVertexId(), fragFromJSON.getVertexId(),
220 "Vertex ID");
221 assertEquals(frag.isRCV(), fragFromJSON.isRCV(),
222 "RCV flag");
223 assertNotEquals(frag.hashCode(), fragFromJSON.hashCode(),
224 "Hash code");
225 assertEquals(frag.getBuildingBlockId(),
226 fragFromJSON.getBuildingBlockId(),
227 "Building block ID");
228 assertEquals(frag.getBuildingBlockType(),
229 fragFromJSON.getBuildingBlockType(),
230 "Building block type");
231 IAtomContainer iac = frag.getIAtomContainer();
232 IAtomContainer iacFromJSON = fragFromJSON.getIAtomContainer();
233 for (int i=0; i<frag.getIAtomContainer().getAtomCount(); i++)
234 {
235 IAtom atmOri = iac.getAtom(i);
236 IAtom atmJsn = iacFromJSON.getAtom(i);
237 assertEquals(MoleculeUtils.getSymbolOrLabel(atmOri),
239 "Symbol atom "+i);
240 double distance = MoleculeUtils.getPoint3d(atmOri).distance(
241 MoleculeUtils.getPoint3d(atmJsn));
242 assertTrue(areCloseEnough(0.0,distance), "Coordinates atom "+i);
243 }
244 for (int i=0; i<iac.getBondCount(); i++)
245 {
246 IBond bndOri = iac.getBond(i);
247 IBond bndJsn = iacFromJSON.getBond(i);
248 assertEquals(iac.indexOf(bndOri.getAtom(0)),
249 iacFromJSON.indexOf(bndJsn.getAtom(0)),
250 "1st atom in bond "+i);
251 assertEquals(iac.indexOf(bndOri.getAtom(1)),
252 iacFromJSON.indexOf(bndJsn.getAtom(1)),
253 "2nd atom in bond "+i);
254 assertEquals(bndOri.getOrder(),bndJsn.getOrder(),
255 "Order in bond "+i);
256 }
257 }
258
259//------------------------------------------------------------------------------
260
261 private boolean areCloseEnough(double a, double b)
262 {
263 double delta = 0.0000001; //NB: hard-coded threshold
264 return Math.abs(a-b) <= delta;
265 }
266
267//------------------------------------------------------------------------------
268
269}
Class representing a continuously connected portion of chemical object holding attachment points.
Definition: Fragment.java:61
int getAPCountOnAtom(int srcAtmId)
Returns the number of APs currently defined on a specific atom source.
Definition: Fragment.java:475
List< AttachmentPoint > getAttachmentPoints()
Definition: Fragment.java:1120
IAtomContainer getIAtomContainer()
Definition: Fragment.java:788
List< SymmetricAPs > getSymmetricAPSets()
Definition: Fragment.java:1148
Unit test for DENOPTIMFragment.
static Fragment makeFragment()
ArrayList< AttachmentPoint > getAttachmentPoints()
Return the list of attachment points visible from outside the template, i.e., the so-called outer APs...
Definition: Template.java:458
List< SymmetricAPs > getSymmetricAPSets()
The SymmetricSet produced from this method contain indexes of the AttachmentPoints in the list return...
Definition: Template.java:495
IAtomContainer getIAtomContainer()
The molecular representation, if any, is generated by this method and stored until further changes in...
Definition: Template.java:715
Unit test for DENOPTIMTemplate.
static Template getTestAmideTemplate()
Builds a template object meant for tests.
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
Definition: Vertex.java:61
int getBuildingBlockId()
Returns the index of the building block that should correspond to the position of the building block ...
Definition: Vertex.java:284
Vertex.BBType getBuildingBlockType()
Definition: Vertex.java:298
abstract List< AttachmentPoint > getAttachmentPoints()
void setBuildingBlockId(int buildingBlockId)
Definition: Vertex.java:291
abstract List< SymmetricAPs > getSymmetricAPSets()
static Vertex fromJson(String json)
Definition: Vertex.java:1202
boolean removeMutationType(MutationType mt)
Removes the specified mutation type.
Definition: Vertex.java:835
void setBuildingBlockType(Vertex.BBType buildingBlockType)
Definition: Vertex.java:305
abstract IAtomContainer getIAtomContainer()
Class for de/serializing DENOPTIM graphs from/to JSON format.
Unit test for DENOPTIMgson.
boolean areCloseEnough(double a, double b)
Utilities for molecule conversion.
static String getSymbolOrLabel(IAtom atm)
Gets either the elemental symbol (for standard atoms) of the label (for pseudo-atoms).
static Point3d getPoint3d(IAtom atm)
Return the 3D coordinates, if present.
The type of building block.
Definition: Vertex.java:86
Types of mutation defined in relation to what happens to the target vertex (i.e., the actual mutation...
CHANGELINK
Replace the target vertex keeping all the child structure.
CHANGEBRANCH
Replace the target vertex and any of the child vertices.