$darkmode
DENOPTIM
DenoptimIOTest.java
Go to the documentation of this file.
1package denoptim.io;
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.assertNotNull;
24import static org.junit.jupiter.api.Assertions.assertThrows;
25import static org.junit.jupiter.api.Assertions.assertTrue;
26
27import java.io.BufferedReader;
28import java.io.File;
29import java.io.InputStreamReader;
30import java.util.ArrayList;
31import java.util.Arrays;
32import java.util.List;
33import java.util.Map;
34import java.util.Set;
35
36import javax.vecmath.Point3d;
37
38import org.junit.jupiter.api.Test;
39import org.junit.jupiter.api.io.TempDir;
40import org.openscience.cdk.Atom;
41import org.openscience.cdk.CDKConstants;
42import org.openscience.cdk.interfaces.IAtom;
43import org.openscience.cdk.interfaces.IAtomContainer;
44import org.openscience.cdk.interfaces.IChemObjectBuilder;
45import org.openscience.cdk.silent.SilentChemObjectBuilder;
46
47import denoptim.constants.DENOPTIMConstants;
48import denoptim.exception.DENOPTIMException;
49import denoptim.files.FileFormat;
50import denoptim.files.FileUtils;
51import denoptim.files.UndetectedFileFormatException;
52import denoptim.fitness.FitnessParameters;
53import denoptim.ga.EAUtils;
54import denoptim.ga.Population;
55import denoptim.graph.APClass;
56import denoptim.graph.Candidate;
57import denoptim.graph.CandidateLW;
58import denoptim.graph.DGraph;
59import denoptim.graph.Edge;
60import denoptim.graph.Edge.BondType;
61import denoptim.graph.EmptyVertex;
62import denoptim.graph.Fragment;
63import denoptim.graph.FragmentTest;
64import denoptim.graph.Ring;
65import denoptim.graph.SymmetricVertexes;
66import denoptim.graph.Template;
67import denoptim.graph.TemplateTest;
68import denoptim.graph.Vertex;
69import denoptim.graph.Vertex.BBType;
70import denoptim.programs.RunTimeParameters.ParametersType;
71import denoptim.programs.denovo.GAParameters;
72import denoptim.programs.fragmenter.CuttingRule;
73
80public class DenoptimIOTest
81{
82
83 private static final BondType BT = BondType.SINGLE;
84
85 private final String SEP = System.getProperty("file.separator");
86 private final String NL = System.getProperty("line.separator");
87
88 @TempDir
89 File tempDir;
90
91//------------------------------------------------------------------------------
92
93 @Test
94 public void testReadAllAtomContainersFromCIF() throws Exception {
95 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
96 String tmpFile = tempDir.getAbsolutePath() + SEP + "test.cif";
97
98 String[] sourceCIFs = {"fromCOD.cif","multipleStructure.cif"};
99 int[] expectedMols = new int[]{1, 3};
100 int[][] expectedAtms = {{81}, {42,45,48}};
101 int[][] expectedBnds = {{85}, {44,47,50}};
102
103 for (int i=0; i<sourceCIFs.length; i++)
104 {
105 // Make a copy of the test file to read from file system
106 BufferedReader reader = null;
107 try {
108 reader = new BufferedReader(
109 new InputStreamReader(getClass()
110 .getClassLoader().getResourceAsStream(
111 sourceCIFs[i])));
112 StringBuilder sb = new StringBuilder();
113 String line = null;
114 while ((line = reader.readLine()) != null)
115 {
116 sb.append(line).append(NL);
117 }
118 DenoptimIO.writeData(tmpFile, sb.toString(), false);
119 } finally {
120 if (reader!=null)
121 reader.close();
122 }
123 List<IAtomContainer> mols = DenoptimIO.readAllAtomContainers(
124 new File(tmpFile));
125 assertEquals(expectedMols[i], mols.size());
126 for (int j=0; j<mols.size(); j++)
127 {
128 assertEquals(expectedAtms[i][j], mols.get(j).getAtomCount());
129 assertEquals(expectedBnds[i][j], mols.get(j).getBondCount());
130 }
131 }
132 }
133
134//------------------------------------------------------------------------------
135
136 @Test
137 public void testIOEmptyVertex() throws Exception {
138 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
139
140 EmptyVertex v = new EmptyVertex();
141 v.addAP(APClass.make("myClass:0"));
142 v.addAP(APClass.make("myClass:1"));
143 v.addAP(APClass.make("myClass:2"));
144 v.addAP(APClass.make("myClass:3"));
145
146 ArrayList<Vertex> initVrtxs = new ArrayList<Vertex>();
147 initVrtxs.add(v);
148
149 File tmpFile = new File(tempDir.getAbsolutePath() + SEP + "test.sdf");
151 ArrayList<Vertex> readInVrtxs =
153 assertEquals(1,readInVrtxs.size(),"Number of vertexes");
154 StringBuilder sb = new StringBuilder();
155 assertTrue(v.sameAs(readInVrtxs.get(0),sb),"Same vertex content: "
156 + sb.toString());
157
158 tmpFile = new File(tempDir.getAbsolutePath() + SEP + "test.json");
160 initVrtxs);
161 readInVrtxs = DenoptimIO.readVertexes(tmpFile, BBType.UNDEFINED);
162 assertEquals(1,readInVrtxs.size(),"Number of vertexes");
163 sb = new StringBuilder();
164 assertTrue(v.sameAs(readInVrtxs.get(0),sb),"Same vertex content: "
165 + sb.toString());
166 }
167
168//------------------------------------------------------------------------------
169
170 @Test
171 public void testIOMolFragment() throws Exception {
172 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
173
175
176 ArrayList<Vertex> initVrtxs = new ArrayList<Vertex>();
177 initVrtxs.add(v);
178
179 File tmpFile = new File(tempDir.getAbsolutePath() + SEP + "test.sdf");
181 ArrayList<Vertex> readInVrtxs =
183 assertEquals(1,readInVrtxs.size(),"Number of vertexes");
184 StringBuilder sb = new StringBuilder();
185 assertTrue(v.sameAs(readInVrtxs.get(0),sb),"Same vertex content: "
186 + sb.toString());
187
188 tmpFile = new File(tempDir.getAbsolutePath() + SEP + "test.json");
190 initVrtxs);
191 readInVrtxs = DenoptimIO.readVertexes(tmpFile, BBType.SCAFFOLD);
192 assertEquals(1,readInVrtxs.size(),"Number of vertexes");
193 sb = new StringBuilder();
194 assertTrue(v.sameAs(readInVrtxs.get(0),sb),"Same vertex content: "
195 + sb.toString());
196 }
197
198//------------------------------------------------------------------------------
199
200 @Test
201 public void testIOTemplate() throws Exception {
202 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
203
205
206 ArrayList<Vertex> initVrtxs = new ArrayList<Vertex>();
207 initVrtxs.add(v);
208
209 File tmpFile = new File(tempDir.getAbsolutePath() + SEP + "test.sdf");
211 ArrayList<Vertex> readInVrtxs =
213 assertEquals(1,readInVrtxs.size(),"Number of vertexes");
214 StringBuilder sb = new StringBuilder();
215 assertTrue(v.sameAs(readInVrtxs.get(0),sb),"Same vertex content: "
216 + sb.toString());
217
218 tmpFile = new File(tempDir.getAbsolutePath() + SEP + "test.json");
220 initVrtxs);
221 readInVrtxs = DenoptimIO.readVertexes(tmpFile, BBType.SCAFFOLD);
222 assertEquals(1,readInVrtxs.size(),"Number of vertexes");
223 sb = new StringBuilder();
224 assertTrue(v.sameAs(readInVrtxs.get(0),sb),"Same vertex content: "
225 + sb.toString());
226 }
227
228//------------------------------------------------------------------------------
229
230 @Test
231 public void testReadGenerationFromSummary() throws Exception {
232
233 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
234
235 GAParameters settings = new GAParameters();
236 settings.setPopulationSize(2);
237 settings.setWorkingDirectory(tempDir.getAbsolutePath());
238 settings.setParameters(new FitnessParameters());
239
240 String genDir = EAUtils.getPathNameToGenerationFolder(26,settings);
242
243 Candidate c1 = new Candidate(new DGraph());
244 c1.setFitness(1.111);
245 c1.setUID("111");
246 c1.getGraph().setGraphId(1);
247 String pathname = genDir + SEP + "M001.sdf";
248 c1.setSDFFile(pathname);
249 DenoptimIO.writeCandidateToFile(new File(pathname), c1, false);
250
251 Candidate c2 = new Candidate(new DGraph());
252 c2.setFitness(2.2);
253 c2.setUID("222");
254 c2.getGraph().setGraphId(2);
255 pathname = genDir + SEP + "M002.sdf";
256 c2.setSDFFile(pathname);
257 DenoptimIO.writeCandidateToFile(new File(pathname), c2, false);
258
259 Population pop = new Population(settings);
260 pop.add(c1);
261 pop.add(c2);
262 String summary = EAUtils.getPathNameToGenerationDetailsFile(26,settings);
263 EAUtils.outputPopulationDetails(pop, summary, settings, true);
264
265 List<Candidate> cands = DenoptimIO.readGenerationFromSummary(
266 new File(summary));
267
268 assertEquals(2,cands.size());
269 assertEquals("111",cands.get(0).getUID());
270 assertEquals("222",cands.get(1).getUID());
271 assertEquals(1,cands.get(0).getGraph().getGraphId());
272 assertEquals(2,cands.get(1).getGraph().getGraphId());
273
274 // We test also the possibility that the run folder has been moved, and
275 // thus the pathname have to be made relative to the pathname of the
276 // generation summary
277 String newPath = tempDir.getAbsolutePath() + SEP + "otherPlace";
278 FileUtils.createDirectory(newPath);
279 org.apache.commons.io.FileUtils.copyDirectory(new File(genDir),
280 new File(newPath));
281
282 cands.clear();
284 new File(summary));
285
286 assertEquals(2,cands.size());
287 assertEquals("111",cands.get(0).getUID());
288 assertEquals("222",cands.get(1).getUID());
289 assertEquals(1,cands.get(0).getGraph().getGraphId());
290 assertEquals(2,cands.get(1).getGraph().getGraphId());
291 }
292
293//------------------------------------------------------------------------------
294
295 @Test
296 public void testReadLightWeightCandidate() throws Exception {
297
298 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
299 String pathName = tempDir.getAbsolutePath() + SEP + "test.sdf";
300
301 IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
302 IAtomContainer iac = builder.newAtomContainer();
303 iac.addAtom(new Atom("C"));
304 String uid = "mmyUiD";
305 String name = "myName";
306 double fitness = 999.123;
307 String err = "myError";
308 String msg = "myMSG";
309 int level = 26;
310 iac.setProperty(DENOPTIMConstants.UNIQUEIDTAG, uid);
311 iac.setProperty(CDKConstants.TITLE, name);
312 iac.setProperty(DENOPTIMConstants.FITNESSTAG, fitness);
313 iac.setProperty(DENOPTIMConstants.MOLERRORTAG, err);
314 iac.setProperty(DENOPTIMConstants.PROVENANCE, msg);
315 iac.setProperty(DENOPTIMConstants.GRAPHLEVELTAG, level);
316
317 DenoptimIO.writeSDFFile(pathName, iac, false);
318
319 IAtomContainer iac2 = builder.newAtomContainer();
320 iac.addAtom(new Atom("C"));
321 String uid2 = "mmyUiD2";
322 String name2 = "myName2";
323 double fitness2 = 999.1232;
324 String err2 = "myError2";
325 String msg2 = "myMSG2";
326 int level2 = 262;
327 iac2.setProperty(DENOPTIMConstants.UNIQUEIDTAG, uid2);
328 iac2.setProperty(CDKConstants.TITLE, name2);
329 iac2.setProperty(DENOPTIMConstants.FITNESSTAG, fitness2);
330 iac2.setProperty(DENOPTIMConstants.MOLERRORTAG, err2);
331 iac2.setProperty(DENOPTIMConstants.PROVENANCE, msg2);
332 iac2.setProperty(DENOPTIMConstants.GRAPHLEVELTAG, level2);
333
334 DenoptimIO.writeSDFFile(pathName, iac2, true);
335
336 List<CandidateLW> cands = DenoptimIO.readLightWeightCandidate(
337 new File(pathName));
338
339 assertEquals(2,cands.size(), "number of candidates in file");
340 assertEquals(uid,cands.get(0).getUid(), "UID 1st");
341 assertEquals(uid2,cands.get(1).getUid(), "UID 2nd");
342 assertEquals(name,cands.get(0).getName(), "name 1st");
343 assertEquals(name2,cands.get(1).getName(), "name 2nd");
344 assertTrue(0.001 > Math.abs(fitness- cands.get(0).getFitness()),
345 "fitness 1st");
346 assertTrue(0.001 > Math.abs(fitness2 - cands.get(1).getFitness()),
347 "fitness 2nd");
348 }
349
350//------------------------------------------------------------------------------
351
352 @Test
353 public void testSerializeDeserializeDENOPTIMGraphs() throws Exception {
354 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
355 String jsonFile = tempDir.getAbsolutePath() + SEP + "graph.json";
356
357 DGraph graph = new DGraph();
358 EmptyVertex v0 = new EmptyVertex(0);
359 buildVertexAndConnectToGraph(v0, 3, graph);
360
361 EmptyVertex v1 = new EmptyVertex(1);
362 buildVertexAndConnectToGraph(v1, 2, graph);
363 graph.addEdge(new Edge(v0.getAP(0), v1.getAP(0),BT));
364
365 EmptyVertex v2 = new EmptyVertex(2);
366 buildVertexAndConnectToGraph(v2, 2, graph);
367 graph.addEdge(new Edge(v1.getAP(1), v2.getAP(0),BT));
368
369 EmptyVertex v3 = new EmptyVertex(3);
370 buildVertexAndConnectToGraph(v3, 1, graph);
371 graph.addEdge(new Edge(v2.getAP(1), v3.getAP(0),BT));
372
373 EmptyVertex v4 = new EmptyVertex(4);
374 buildVertexAndConnectToGraph(v4, 3, graph);
375 graph.addEdge(new Edge(v0.getAP(1), v4.getAP(0),BT));
376
377 EmptyVertex v5 = new EmptyVertex(5);
378 buildVertexAndConnectToGraph(v5, 1, graph);
379 graph.addEdge(new Edge(v4.getAP(1), v5.getAP(0),BT));
380
381 EmptyVertex v6 = new EmptyVertex(6);
382 buildVertexAndConnectToGraph(v6, 1, graph);
383 graph.addEdge(new Edge(v0.getAP(2), v6.getAP(0),BT));
384
385 EmptyVertex v7 = new EmptyVertex(7);
386 buildVertexAndConnectToGraph(v7, 1, graph);
387 graph.addEdge(new Edge(v4.getAP(2), v7.getAP(0),BT));
388
389 graph.addRing(new Ring(new ArrayList<>(
390 Arrays.asList(v5, v4, v0, v1, v2, v3))));
391
392 graph.addRing(new Ring(new ArrayList<>(
393 Arrays.asList(v6, v0, v4, v7))));
394
396 ss1.add(v3);
397 ss1.add(v5);
398 graph.addSymmetricSetOfVertices(ss1);
399
401 ss2.add(v6);
402 ss2.add(v7);
403 graph.addSymmetricSetOfVertices(ss2);
404
405 String json = graph.toJson();
406
407 DenoptimIO.writeData(jsonFile, json, false);
409 jsonFile).get(0);
410 assertNotNull(graphJ,"Graph read from JSON file is null");
411
412 StringBuilder reason = new StringBuilder();
413 assertTrue(graph.sameAs(graphJ, reason));
414 }
415
416//------------------------------------------------------------------------------
417
418 @Test
419 public void testReadAllAPClasses() throws Exception {
420 Fragment frag = new Fragment();
421 IAtom atmC = new Atom("C");
422 atmC.setPoint3d(new Point3d(0.0, 0.0, 1.0));
423 IAtom atmH = new Atom("H");
424 atmH.setPoint3d(new Point3d(0.0, 1.0, 1.0));
425 frag.addAtom(atmC);
426 frag.addAtom(atmH);
427 frag.addAPOnAtom(atmC, APClass.make("classAtmC:5"),
428 new Point3d(1.0, 0.0, 0.0));
429 frag.addAPOnAtom(atmC, APClass.make("classAtmC:5"),
430 new Point3d(1.0, 1.0, 0.0));
431 frag.addAPOnAtom(atmC, APClass.make("otherClass:0"),
432 new Point3d(-1.0, 0.0, 0.0));
433 frag.addAPOnAtom(atmH, APClass.make("classAtmH:1"),
434 new Point3d(1.0, 2.0, 2.0));
436
437 Fragment frag2 = new Fragment();
438 IAtom atmO = new Atom("O");
439 atmO.setPoint3d(new Point3d(0.0, 0.0, 1.0));
440 IAtom atmH2 = new Atom("N");
441 atmH.setPoint3d(new Point3d(0.0, 1.0, 1.0));
442 frag2.addAtom(atmO);
443 frag2.addAtom(atmH2);
444 frag2.addAPOnAtom(atmO, APClass.make("apClassO:5"),
445 new Point3d(1.0, 0.0, 0.0));
446 frag2.addAPOnAtom(atmO, APClass.make("apClassO:6"),
447 new Point3d(1.0, 1.0, 0.0));
448 frag2.addAPOnAtom(atmO, APClass.make("apClassObis:0"),
449 new Point3d(-1.0, 0.0, 0.0));
450 frag2.addAPOnAtom(atmH2, APClass.make("classAtmH:1"),
451 new Point3d(1.0, 2.0, 2.0));
453
454 ArrayList<Vertex> frags = new ArrayList<Vertex>();
455 frags.add(frag);
456 frags.add(frag2);
457
458 String tmpFile = FileUtils.getTempFolder()
459 + System.getProperty("file.separator") + "frag.sdf";
460 DenoptimIO.writeVertexesToSDF(new File(tmpFile), frags, false);
461
462 Set<APClass> allAPC = DenoptimIO.readAllAPClasses(new File(tmpFile));
463
464 assertEquals(6, allAPC.size(), "Size did not match");
465 assertTrue(allAPC.contains(APClass.make("apClassObis:0")),
466 "Contains APClass (1)");
467 assertTrue(allAPC.contains(APClass.make("otherClass:0")),
468 "Contains APClass (2)");
469 }
470
471//------------------------------------------------------------------------------
472
473 @Test
474 public void testAppendToJSON() throws Exception
475 {
476 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
477 String pathName = tempDir.getAbsolutePath() + SEP + "vertexes.json";
478
479 Fragment frag = new Fragment();
480 frag.addAtom(new Atom("C",new Point3d(0.0, 0.0, 1.0)));
481 frag.addAtom(new Atom("C",new Point3d(0.0, 1.0, 1.0)));
482 frag.addAPOnAtom(frag.getAtom(0), APClass.make("classAtmC:5"),
483 new Point3d(1.0, 0.0, 0.0));
484 frag.addAPOnAtom(frag.getAtom(1), APClass.make("classAtmC:5"),
485 new Point3d(1.0, 1.0, 0.0));
487
488 Fragment frag2 = new Fragment();
489 frag2.addAtom(new Atom("O",new Point3d(0.0, 0.0, 1.0)));
490 frag2.addAPOnAtom(frag2.getAtom(0), APClass.make("Other:0"),
491 new Point3d(1.0, 0.0, 0.0));
493
494 ArrayList<Vertex> frags = new ArrayList<Vertex>();
495 frags.add(frag);
496 frags.add(frag2);
497
499 frags);
500
501 Fragment frag3 = new Fragment();
502 frag3.addAtom(new Atom("N",new Point3d(1.0, 0.0, 1.0)));
503 frag3.addAPOnAtom(frag3.getAtom(0), APClass.make("Other:0"),
504 new Point3d(1.0, 0.0, 0.0));
506
507 ArrayList<Vertex> frags2 = new ArrayList<Vertex>();
508 frags2.add(frag3);
509
511 frags2, true);
512
513 ArrayList<Vertex> frags3 = DenoptimIO.readDENOPTIMVertexesFromJSONFile(
514 pathName);
515
516 assertEquals(3,frags3.size());
517 assertEquals("C",frags3.get(0).getIAtomContainer().getAtom(0).getSymbol());
518 assertEquals("O",frags3.get(1).getIAtomContainer().getAtom(0).getSymbol());
519 assertEquals("N",frags3.get(2).getIAtomContainer().getAtom(0).getSymbol());
520 }
521
522//------------------------------------------------------------------------------
523
524 private void buildVertexAndConnectToGraph(EmptyVertex v, int apCount,
525 DGraph graph)
526 throws DENOPTIMException {
527 for (int atomPos = 0; atomPos < apCount; atomPos++) {
528 v.addAP();
529 }
530 graph.addVertex(v);
531 }
532
533//------------------------------------------------------------------------------
534
535 @Test
536 public void testDetectFileFormat() throws Exception {
537 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
538 String pathName = tempDir.getAbsolutePath() + SEP + "graph.sdf";
539 File file = new File(pathName);
540 final File ffile = new File(pathName);
541
542 DenoptimIO.writeData(pathName, "dummy text", false);
543 assertThrows(UndetectedFileFormatException.class,
544 () -> FileUtils.detectFileFormat(ffile));
545
546 DenoptimIO.writeData(pathName,
547 "> <" + DENOPTIMConstants.APSTAG + ">"+ NL
548 + "> <" + DENOPTIMConstants.VERTEXJSONTAG + ">", false);
549 assertTrue(FileFormat.VRTXSDF == FileUtils.detectFileFormat(file),
550 "Vertex SDF");
551
552 DenoptimIO.writeData(pathName,
553 "> <" + DENOPTIMConstants.APSTAG + ">"+ NL
554 + "> <" + DENOPTIMConstants.GRAPHJSONTAG + ">", false);
555 assertTrue(FileFormat.GRAPHSDF == FileUtils.detectFileFormat(file),
556 "Graph SDF");
557
559 + ">", false);
560 assertTrue(FileFormat.GRAPHSDF == FileUtils.detectFileFormat(file),
561 "Graph SDF");
562
563 pathName = tempDir.getAbsolutePath() + SEP + "filename";
564 file = new File(pathName);
565
566 DenoptimIO.writeData(pathName, ParametersType.CEBL_PARAMS.getKeywordRoot()
567 + "SOMETING", false);
569 "FSE params");
570
571 DenoptimIO.writeData(pathName, ParametersType.GA_PARAMS.getKeywordRoot()
572 + "SOMETING", false);
573 assertTrue(FileFormat.GA_PARAM == FileUtils.detectFileFormat(file),
574 "GA params");
575
576 DenoptimIO.writeData(pathName, "RCN SOMETING", false);
577 assertTrue(FileFormat.COMP_MAP == FileUtils.detectFileFormat(file),
578 "Compatibility Matrix (1)");
579
580 DenoptimIO.writeData(pathName, "CAP SOMETING", false);
581 assertTrue(FileFormat.COMP_MAP == FileUtils.detectFileFormat(file),
582 "Compatibility Matrix (3)");
583
584 File txtFile = new File(pathName+ ".txt");
585 DenoptimIO.writeData(txtFile.getAbsolutePath(),
587 assertTrue(FileFormat.GENSUMMARY == FileUtils.detectFileFormat(txtFile),
588 "Generation summary");
589
590 String dirName = tempDir.getAbsolutePath() + SEP + "blabla1234";
591 String subDirName = dirName + SEP+DENOPTIMConstants.FSEIDXNAMEROOT+"0";
592
593 FileUtils.createDirectory(dirName);
594 FileUtils.createDirectory(subDirName);
596 new File(dirName)), "FSE output folder");
597
598 dirName = tempDir.getAbsolutePath() + SEP + "blabla5678";
599 subDirName = dirName + SEP+DENOPTIMConstants.GAGENDIRNAMEROOT+"0";
600 FileUtils.createDirectory(dirName);
601 FileUtils.createDirectory(subDirName);
603 new File(dirName)), "GA output folder");
604 }
605
606//------------------------------------------------------------------------------
607
608 @Test
609 public void testReadCuttingRulsedDefault() throws Exception {
610 List<CuttingRule> defaultCuttingRules = new ArrayList<CuttingRule>();
611 BufferedReader reader = null;
612 try {
613 reader = new BufferedReader(
614 new InputStreamReader(getClass()
615 .getClassLoader().getResourceAsStream(
616 "data/cutting_rules")));
617 DenoptimIO.readCuttingRules(reader, defaultCuttingRules,
618 "bundled jar");
619 } finally {
620 if (reader!=null)
621 reader.close();
622 }
623 assertTrue(defaultCuttingRules.size() > 100);
624 }
625
626//------------------------------------------------------------------------------
627
628 @Test
629 public void testIOCuttingRules() throws Exception {
630 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
631
632 CuttingRule ctrA = new CuttingRule("myRuleA",
633 "AAA", "{$([*]@C)}", "!@#", -123, null);
634 CuttingRule ctrB = new CuttingRule("myRuleB",
635 "{$([*]@B)}", "BBB", "-", 1, null);
636
637 ArrayList<CuttingRule> cutRules = new ArrayList<CuttingRule>();
638 cutRules.add(ctrA);
639 cutRules.add(ctrB);
640
641 File tmpFile = new File(tempDir.getAbsolutePath() + SEP + "cutRule");
642 DenoptimIO.writeCuttingRules(tmpFile, cutRules);
643
644 ArrayList<CuttingRule> readInCutRules = new ArrayList<CuttingRule>();
645 DenoptimIO.readCuttingRules(tmpFile, readInCutRules);
646
647 assertEquals(cutRules.size(), readInCutRules.size());
648 }
649
650//------------------------------------------------------------------------------
651
652 @Test
653 public void testReadCSDFormulae() throws Exception {
654 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
655 String txtFile = tempDir.getAbsolutePath() + SEP + "formulae.txt";
656
657 String text = "REFCODE: MOL000001" + NL
658 + "Chemical" + NL
659 + " Formula: H2 O" + NL
660 + NL
661 + "REFCODE: BLABLA" + NL
662 + "Chemical" + NL
663 + " Formula: ABC" + NL
664 + NL
665 + "REFCODE: A" + NL
666 + "Chemical" + NL
667 + " Formula: H2 O" + NL;
668
669 DenoptimIO.writeData(txtFile, text, false);
670 Map<String, String> data = DenoptimIO.readCSDFormulae(new File(txtFile));
671 assertEquals(3, data.size());
672 assertTrue(data.keySet().contains("MOL000001"));
673 assertTrue(data.keySet().contains("BLABLA"));
674 assertTrue(data.keySet().contains("A"));
675 }
676}
General set of constants used in DENOPTIM.
static final String VERTEXJSONTAG
SDF tag containing vertex encoding in JSON format.
static final String PROVENANCE
SDF tag containing provenance data for a graph.
static final String APSTAG
SDF tag defining attachment points.
static final String GRAPHLEVELTAG
SDF tag defining the graph generating level in an FSE run.
static final String GAGENSUMMARYHEADER
Header of text files collection generation details.
static final String UNIQUEIDTAG
SDF tag containing the unique identifier of a candidate.
static final String GAGENDIRNAMEROOT
Prefix for generation folders.
static final String GRAPHJSONTAG
SDF tag containing graph encoding in JSON format.
static final String MOLERRORTAG
SDF tag containing errors during execution of molecule specific tasks.
static final String FSEIDXNAMEROOT
Prefix for graph indexing files.
static final String FITNESSTAG
SDF tag containing the fitness of a candidate.
static boolean createDirectory(String fileName)
Creates a directory.
Definition: FileUtils.java:231
static FileFormat detectFileFormat(File inFile)
Inspects a file/folder and tries to detect if there is one of the data sources that is recognized by ...
Definition: FileUtils.java:399
static String getTempFolder()
Looks for a writable location where to put temporary files and returns an absolute pathname to the fo...
Definition: FileUtils.java:204
Exception thrown when the format of a file is not recognized.
Settings defining the calculation of fitness.
Helper methods for the genetic algorithm.
Definition: EAUtils.java:93
static void outputPopulationDetails(Population population, String filename, GAParameters settings, boolean printpathNames)
Write out summary for the current GA population.
Definition: EAUtils.java:1226
static String getPathNameToGenerationFolder(int genID, GAParameters settings)
Definition: EAUtils.java:1449
static String getPathNameToGenerationDetailsFile(int genID, GAParameters settings)
Definition: EAUtils.java:1465
A collection of candidates.
Definition: Population.java:48
boolean add(Candidate c)
Definition: Population.java:87
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
A candidate is the combination of a denoptim graph with molecular representation and may include also...
Definition: Candidate.java:40
void setSDFFile(String molFile)
Definition: Candidate.java:445
void setFitness(double fitness)
Definition: Candidate.java:480
void setUID(String uid)
Definition: Candidate.java:466
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
String toJson()
Produces a string that represents this graph and that adheres to the JSON format.
Definition: DGraph.java:6660
void setGraphId(int id)
Definition: DGraph.java:258
boolean sameAs(DGraph other, StringBuilder reason)
Compare this and another graph ignoring the vertex IDs.
Definition: DGraph.java:3665
void addSymmetricSetOfVertices(SymmetricVertexes symSet)
Adds a symmetric set of vertices to this graph.
Definition: DGraph.java:661
void addRing(Ring ring)
Definition: DGraph.java:1030
void addEdge(Edge edge)
Adds the edge to the list of edges belonging to this graph.
Definition: DGraph.java:1021
This class represents the edge between two vertices.
Definition: Edge.java:38
An empty vertex has the behaviors of a vertex, but has no molecular structure.
boolean sameAs(EmptyVertex other, StringBuilder reason)
Compares this and another vertex ignoring vertex IDs.
void addAP()
Adds an attachment point with no APClass or other attribute.
Class representing a continuously connected portion of chemical object holding attachment points.
Definition: Fragment.java:61
boolean sameAs(Fragment other, StringBuilder reason)
Compares this and another fragment ignoring vertex IDs.
Definition: Fragment.java:967
AttachmentPoint addAPOnAtom(IAtom srcAtm, APClass apc, Point3d vector)
Add an attachment point to the specifies atom.
Definition: Fragment.java:424
void addAtom(IAtom atom)
Definition: Fragment.java:836
IAtom getAtom(int number)
Definition: Fragment.java:843
void projectAPsToProperties()
Finds the DENOPTIMAttachmentPoint objects defined as properties of the atoms in this container,...
Definition: Fragment.java:693
Unit test for DENOPTIMFragment.
static Fragment makeFragment()
This class represents the closure of a ring in a spanning tree.
Definition: Ring.java:40
boolean add(T item)
Adds an item to this list, if not already present.
A collection of Vertexs that are related by a relation that we call "symmetry", even though this clas...
boolean sameAs(Template other, StringBuilder reason)
Compares this and another template ignoring vertex IDs.
Definition: Template.java:956
Unit test for DENOPTIMTemplate.
static Template getTestAmideTemplate()
Builds a template object meant for tests.
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
Definition: Vertex.java:920
Utility methods for input/output.
static File writeVertexesToFile(File file, FileFormat format, List< Vertex > vertexes)
Writes vertexes to file.
static LinkedHashMap< String, String > readCSDFormulae(File file)
Read molecular formula from TXT data representation produced by Cambridge Structural Database tools (...
static List< Candidate > readGenerationFromSummary(File file)
Reads a FileFormat#GENSUMMARY file and searches all the files defining each member of the population.
static void writeSDFFile(String fileName, IAtomContainer mol)
Writes IAtomContainer to SDF file.
static ArrayList< Vertex > readDENOPTIMVertexesFromJSONFile(String fileName)
Reads a list of Vertexes from a JSON file.
static void readCuttingRules(BufferedReader reader, List< CuttingRule > cutRules, String source)
Read cutting rules from a stream.
static void writeCandidateToFile(File file, Candidate candidate, boolean append)
Writes one candidate item to file.
static void writeCuttingRules(File file, List< CuttingRule > cutRules)
Writes a formatted text file that collects cutting rule.
static Set< APClass > readAllAPClasses(File fragLib)
static ArrayList< DGraph > readDENOPTIMGraphsFromJSONFile(String fileName)
Reads a list of DGraphs from a JSON file.
static List< CandidateLW > readLightWeightCandidate(File file)
Read only selected data from a GA produced items.
static ArrayList< Vertex > readVertexes(File file, Vertex.BBType bbt)
Reads Vertexes from any file that can contain such items.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
static List< IAtomContainer > readAllAtomContainers(File file)
Returns a single collection with all atom containers found in a file of any format.
static void writeVertexesToSDF(File file, List< Vertex > vertexes, boolean append)
Write a list of vertexes to file.
Unit test for input/output.
static final BondType BT
void buildVertexAndConnectToGraph(EmptyVertex v, int apCount, DGraph graph)
void setParameters(RunTimeParameters otherParams)
Parameters for genetic algorithm.
void setWorkingDirectory(String pathName)
A cutting rule with three SMARTS queries (atom 1, bond, atom2) and options.
File formats identified by DENOPTIM.
Definition: FileFormat.java:32
Possible chemical bond types an edge can represent.
Definition: Edge.java:303
The type of building block.
Definition: Vertex.java:86
CEBL_PARAMS
Parameters pertaining the combinatorial exploration by layer.
GA_PARAMS
Parameters pertaining the genetic algorithm.