$darkmode
DENOPTIM
FitnessExpressionParserTest.java
Go to the documentation of this file.
1package denoptim.fitness;
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.assertTrue;
24import static org.junit.jupiter.api.Assertions.fail;
25
26import java.io.File;
27import java.util.HashMap;
28import java.util.List;
29import java.util.Map;
30
31import org.junit.jupiter.api.BeforeEach;
32import org.junit.jupiter.api.Test;
33import org.junit.jupiter.api.io.TempDir;
34import org.openscience.cdk.interfaces.IAtomContainer;
35import org.openscience.cdk.silent.SilentChemObjectBuilder;
36import org.openscience.cdk.smiles.SmilesParser;
37
38import denoptim.io.DenoptimIO;
39
47{
48
49 private SmilesParser sp;
50 private static final String SEP = System.getProperty("file.separator");
51
52 @TempDir
53 static File tempDir;
54
55 @BeforeEach
56 void setUp()
57 {
58 assertTrue(tempDir.isDirectory(),"Should be a directory ");
59 sp = new SmilesParser(SilentChemObjectBuilder.getInstance());
60 }
61
62//------------------------------------------------------------------------------
63
64 @Test
65 public void testProcessExpressions() throws Exception
66 {
67 String fileName = tempDir.getAbsolutePath() + SEP + "ref.sdf";
68 IAtomContainer ref = sp.parseSmiles("CNC(=O)c1cc(OC)ccc1");
69 DenoptimIO.writeSDFFile(fileName, ref, false);
70
72 String[] lines = new String[] {
73 "FP-Equation=${taniSym + taniBis + 3.3 * Zagreb - aHyb_1 + "
74 + "aHyb_2}",
75 "FP-DescriptorSpecs=${Variable.atomSpecific('aHyb_1','aHyb','[$([C])]')}",
76 "FP-DescriptorSpecs=${Variable.atomSpecific('aHyb_2','aHyb','[$([N])]')}",
77 "FP-DescriptorSpecs=${Variable.parametrized('taniSym',"
78 + "'TanimotoSimilarity','PubchemFingerprinter, "
79 + "FILE:" + fileName + "')}",
80 "FP-DescriptorSpecs=${Variable.parametrized('taniBis',"
81 + "'TanimotoSimilarity','Fingerprinter, "
82 + "FILE:" + fileName + "')}"};
83 for (int i=0; i<lines.length; i++)
84 {
85 String line = lines[i];
86 fitPar.interpretKeyword(line);
87 }
88
89 fitPar.processParameters();
90 List<DescriptorForFitness> descriptors = fitPar.getDescriptors();
91
92 assertEquals(4,descriptors.size(),
93 "Number of descriptor implementation");
94
95 Map<String,Integer> counts = new HashMap<String,Integer>();
96 for (DescriptorForFitness d : fitPar.getDescriptors())
97 {
98 String key = d.getShortName();
99 if (counts.containsKey(key))
100 counts.put(key,counts.get(key) + 1);
101 else
102 counts.put(key,1);
103 }
104 String[] expectedNames = new String[] {"aHyb",
105 "TanimotoSimilarity","Zagreb"};
106 int[] expectedCount = new int[] {1,2,1};
107 for (int i=0; i<3; i++)
108 {
109 assertEquals(expectedCount[i], counts.get(expectedNames[i]),
110 "Number of " + expectedNames[i] + " implementations");
111 }
112
113 boolean foundA = false;
114 boolean foundB = false;
115 for (int i=0; i<descriptors.size(); i++)
116 {
117 DescriptorForFitness d = descriptors.get(i);
118 String descName = d.getShortName();
119 List<Variable> variables = d.getVariables();
120
121 switch(descName)
122 {
123 case "aHyb":
124 assertEquals(2,variables.size(),
125 "Number of variable names for #"+i+" "+descName);
126 boolean foundOne = false;
127 boolean foundTwo = false;
128 for (Variable actual : variables)
129 {
130 if ("aHyb_1".equals(actual.getName()))
131 foundOne = true;
132 if ("aHyb_2".equals(actual.getName()))
133 foundTwo = true;
134 }
135 assertTrue(foundOne, "Found first variable aHyb");
136 assertTrue(foundTwo, "Found second variable aHyb");
137 break;
138
139 case "Zagreb":
140 assertEquals(1,variables.size(),
141 "Number of variable names for #"+i+" "+descName);
142 assertEquals("Zagreb",variables.get(0).getName(),
143 "Name of variable for #"+i);
144 break;
145
146 case "TanimotoSimilarity":
147 assertEquals(1,variables.size(),
148 "Number of variable names for #"+i+" "+descName);
149 if ("taniSym".equals(variables.get(0).getName()))
150 foundA = true;
151 if ("taniBis".equals(variables.get(0).getName()))
152 foundB = true;
153 break;
154
155 default:
156 fail("Unexpected descriptor name "+descName);
157 }
158 }
159 assertTrue(foundA, "Tanimoto-variable A");
160 assertTrue(foundB, "Tanimoto-variable B");
161 }
162
163//------------------------------------------------------------------------------
164
165}
This is a reference to a specific descriptor value.
List< Variable > getVariables()
Get the variables that make use of values produced by this descriptor.
Unit test for parser of fitness-defining expressions.
Settings defining the calculation of fitness.
void processParameters()
Processes all parameters and initialize related objects.
List< DescriptorForFitness > getDescriptors()
void interpretKeyword(String key, String value)
Processes a keyword/value pair and assign the related parameters.
A variable in the expression defining the fitness.
Definition: Variable.java:42
Utility methods for input/output.
static void writeSDFFile(String fileName, IAtomContainer mol)
Writes IAtomContainer to SDF file.