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;
27import java.io.BufferedReader;
29import java.io.InputStreamReader;
30import java.util.ArrayList;
31import java.util.Arrays;
36import javax.vecmath.Point3d;
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;
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;
85 private final String
SEP = System.getProperty(
"file.separator");
86 private final String
NL = System.getProperty(
"line.separator");
95 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
96 String tmpFile =
tempDir.getAbsolutePath() +
SEP +
"test.cif";
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}};
103 for (
int i=0; i<sourceCIFs.length; i++)
106 BufferedReader reader =
null;
108 reader =
new BufferedReader(
109 new InputStreamReader(getClass()
110 .getClassLoader().getResourceAsStream(
112 StringBuilder sb =
new StringBuilder();
114 while ((line = reader.readLine()) !=
null)
116 sb.append(line).append(
NL);
125 assertEquals(expectedMols[i], mols.size());
126 for (
int j=0; j<mols.size(); j++)
128 assertEquals(expectedAtms[i][j], mols.get(j).getAtomCount());
129 assertEquals(expectedBnds[i][j], mols.get(j).getBondCount());
138 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
146 ArrayList<Vertex> initVrtxs =
new ArrayList<Vertex>();
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: "
158 tmpFile =
new File(
tempDir.getAbsolutePath() +
SEP +
"test.json");
162 assertEquals(1,readInVrtxs.size(),
"Number of vertexes");
163 sb =
new StringBuilder();
164 assertTrue(v.
sameAs(readInVrtxs.get(0),sb),
"Same vertex content: "
172 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
176 ArrayList<Vertex> initVrtxs =
new ArrayList<Vertex>();
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: "
188 tmpFile =
new File(
tempDir.getAbsolutePath() +
SEP +
"test.json");
192 assertEquals(1,readInVrtxs.size(),
"Number of vertexes");
193 sb =
new StringBuilder();
194 assertTrue(v.
sameAs(readInVrtxs.get(0),sb),
"Same vertex content: "
202 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
206 ArrayList<Vertex> initVrtxs =
new ArrayList<Vertex>();
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: "
218 tmpFile =
new File(
tempDir.getAbsolutePath() +
SEP +
"test.json");
222 assertEquals(1,readInVrtxs.size(),
"Number of vertexes");
223 sb =
new StringBuilder();
224 assertTrue(v.
sameAs(readInVrtxs.get(0),sb),
"Same vertex content: "
233 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
247 String pathname = genDir +
SEP +
"M001.sdf";
255 pathname = genDir +
SEP +
"M002.sdf";
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());
277 String newPath =
tempDir.getAbsolutePath() +
SEP +
"otherPlace";
279 org.apache.commons.io.FileUtils.copyDirectory(
new File(genDir),
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());
298 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
299 String pathName =
tempDir.getAbsolutePath() +
SEP +
"test.sdf";
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";
311 iac.setProperty(CDKConstants.TITLE, name);
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";
328 iac2.setProperty(CDKConstants.TITLE, name2);
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()),
346 assertTrue(0.001 > Math.abs(fitness2 - cands.get(1).getFitness()),
354 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
355 String jsonFile =
tempDir.getAbsolutePath() +
SEP +
"graph.json";
390 Arrays.asList(v5, v4, v0, v1, v2, v3))));
393 Arrays.asList(v6, v0, v4, v7))));
405 String json = graph.
toJson();
410 assertNotNull(graphJ,
"Graph read from JSON file is null");
412 StringBuilder reason =
new StringBuilder();
413 assertTrue(graph.
sameAs(graphJ, reason));
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));
428 new Point3d(1.0, 0.0, 0.0));
430 new Point3d(1.0, 1.0, 0.0));
432 new Point3d(-1.0, 0.0, 0.0));
434 new Point3d(1.0, 2.0, 2.0));
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));
445 new Point3d(1.0, 0.0, 0.0));
447 new Point3d(1.0, 1.0, 0.0));
449 new Point3d(-1.0, 0.0, 0.0));
451 new Point3d(1.0, 2.0, 2.0));
454 ArrayList<Vertex> frags =
new ArrayList<Vertex>();
459 + System.getProperty(
"file.separator") +
"frag.sdf";
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)");
476 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
477 String pathName =
tempDir.getAbsolutePath() +
SEP +
"vertexes.json";
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)));
483 new Point3d(1.0, 0.0, 0.0));
485 new Point3d(1.0, 1.0, 0.0));
489 frag2.
addAtom(
new Atom(
"O",
new Point3d(0.0, 0.0, 1.0)));
491 new Point3d(1.0, 0.0, 0.0));
494 ArrayList<Vertex> frags =
new ArrayList<Vertex>();
502 frag3.
addAtom(
new Atom(
"N",
new Point3d(1.0, 0.0, 1.0)));
504 new Point3d(1.0, 0.0, 0.0));
507 ArrayList<Vertex> frags2 =
new ArrayList<Vertex>();
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());
527 for (
int atomPos = 0; atomPos < apCount; atomPos++) {
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);
563 pathName =
tempDir.getAbsolutePath() +
SEP +
"filename";
564 file =
new File(pathName);
567 +
"SOMETING",
false);
572 +
"SOMETING",
false);
578 "Compatibility Matrix (1)");
582 "Compatibility Matrix (3)");
584 File txtFile =
new File(pathName+
".txt");
588 "Generation summary");
590 String dirName =
tempDir.getAbsolutePath() +
SEP +
"blabla1234";
596 new File(dirName)),
"FSE output folder");
598 dirName =
tempDir.getAbsolutePath() +
SEP +
"blabla5678";
603 new File(dirName)),
"GA output folder");
610 List<CuttingRule> defaultCuttingRules =
new ArrayList<CuttingRule>();
611 BufferedReader reader =
null;
613 reader =
new BufferedReader(
614 new InputStreamReader(getClass()
615 .getClassLoader().getResourceAsStream(
616 "data/cutting_rules")));
623 assertTrue(defaultCuttingRules.size() > 100);
630 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
633 "AAA",
"{$([*]@C)}",
"!@#", -123,
null);
635 "{$([*]@B)}",
"BBB",
"-", 1,
null);
637 ArrayList<CuttingRule> cutRules =
new ArrayList<CuttingRule>();
641 File tmpFile =
new File(
tempDir.getAbsolutePath() +
SEP +
"cutRule");
644 ArrayList<CuttingRule> readInCutRules =
new ArrayList<CuttingRule>();
647 assertEquals(cutRules.size(), readInCutRules.size());
654 assertTrue(this.tempDir.isDirectory(),
"Should be a directory ");
655 String txtFile =
tempDir.getAbsolutePath() +
SEP +
"formulae.txt";
657 String text =
"REFCODE: MOL000001" +
NL
659 +
" Formula: H2 O" +
NL
661 +
"REFCODE: BLABLA" +
NL
663 +
" Formula: ABC" +
NL
667 +
" Formula: H2 O" +
NL;
671 assertEquals(3, data.size());
672 assertTrue(data.keySet().contains(
"MOL000001"));
673 assertTrue(data.keySet().contains(
"BLABLA"));
674 assertTrue(data.keySet().contains(
"A"));
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.
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 ...
static String getTempFolder()
Looks for a writable location where to put temporary files and returns an absolute pathname to the fo...
Settings defining the calculation of fitness.
Helper methods for the genetic algorithm.
static void outputPopulationDetails(Population population, String filename, GAParameters settings, boolean printpathNames)
Write out summary for the current GA population.
static String getPathNameToGenerationFolder(int genID, GAParameters settings)
static String getPathNameToGenerationDetailsFile(int genID, GAParameters settings)
A collection of candidates.
static APClass make(String ruleAndSubclass)
Creates an APClass if it does not exist already, or returns the reference to the existing instance.
A candidate is the combination of a denoptim graph with molecular representation and may include also...
void setSDFFile(String molFile)
void setFitness(double fitness)
Container for the list of vertices and the edges that connect them.
String toJson()
Produces a string that represents this graph and that adheres to the JSON format.
boolean sameAs(DGraph other, StringBuilder reason)
Compare this and another graph ignoring the vertex IDs.
void addSymmetricSetOfVertices(SymmetricVertexes symSet)
Adds a symmetric set of vertices to this graph.
void addEdge(Edge edge)
Adds the edge to the list of edges belonging to this graph.
This class represents the edge between two vertices.
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.
boolean sameAs(Fragment other, StringBuilder reason)
Compares this and another fragment ignoring vertex IDs.
AttachmentPoint addAPOnAtom(IAtom srcAtm, APClass apc, Point3d vector)
Add an attachment point to the specifies atom.
IAtom getAtom(int number)
void projectAPsToProperties()
Finds the DENOPTIMAttachmentPoint objects defined as properties of the atoms in this container,...
Unit test for DENOPTIMFragment.
static Fragment makeFragment()
This class represents the closure of a ring in a spanning tree.
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.
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.
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.
void testReadLightWeightCandidate()
void testSerializeDeserializeDENOPTIMGraphs()
void testReadAllAPClasses()
void testIOCuttingRules()
void testReadCSDFormulae()
void testDetectFileFormat()
void testReadAllAtomContainersFromCIF()
void buildVertexAndConnectToGraph(EmptyVertex v, int apCount, DGraph graph)
void testReadCuttingRulsedDefault()
void testReadGenerationFromSummary()
void setParameters(RunTimeParameters otherParams)
Parameters for genetic algorithm.
void setPopulationSize(int size)
void setWorkingDirectory(String pathName)
A cutting rule with three SMARTS queries (atom 1, bond, atom2) and options.
Possible chemical bond types an edge can represent.
The type of building block.
Identifier of the type of parameters.
CEBL_PARAMS
Parameters pertaining the combinatorial exploration by layer.
GA_PARAMS
Parameters pertaining the genetic algorithm.