1package denoptim.fitness;
22import static org.junit.jupiter.api.Assertions.assertEquals;
23import static org.junit.jupiter.api.Assertions.assertTrue;
26import java.util.ArrayList;
29import java.util.logging.Level;
30import java.util.logging.Logger;
32import org.junit.jupiter.api.BeforeEach;
33import org.junit.jupiter.api.Test;
34import org.junit.jupiter.api.io.TempDir;
35import org.openscience.cdk.DefaultChemObjectBuilder;
36import org.openscience.cdk.exception.InvalidSmilesException;
37import org.openscience.cdk.fingerprint.IBitFingerprint;
38import org.openscience.cdk.fingerprint.PubchemFingerprinter;
39import org.openscience.cdk.interfaces.IAtomContainer;
40import org.openscience.cdk.qsar.DescriptorEngine;
41import org.openscience.cdk.qsar.IDescriptor;
42import org.openscience.cdk.silent.SilentChemObjectBuilder;
43import org.openscience.cdk.smiles.SmilesParser;
45import denoptim.constants.DENOPTIMConstants;
46import denoptim.fitness.descriptors.TanimotoMolSimilarity;
47import denoptim.io.DenoptimIO;
57 private SmilesParser
sp;
58 private static final String
SEP = System.getProperty(
"file.separator");
68 assertTrue(
tempDir.isDirectory(),
"Should be a directory ");
69 sp =
new SmilesParser(SilentChemObjectBuilder.getInstance());
70 logger = Logger.getLogger(
"DummyLogger");
71 logger.setLevel(Level.SEVERE);
79 List<String> classNames =
new ArrayList<String>();
80 classNames.add(
"org.openscience.cdk.qsar.descriptors.molecular."
81 +
"ZagrebIndexDescriptor");
82 classNames.add(
"org.openscience.cdk.qsar.descriptors.molecular."
83 +
"AtomCountDescriptor");
84 DescriptorEngine engine =
new DescriptorEngine(classNames,
null);
85 List<IDescriptor> iDescs = engine.instantiateDescriptors(classNames);
87 List<DescriptorForFitness> descriptors =
88 new ArrayList<DescriptorForFitness>();
89 for (
int i=0; i<iDescs.size(); i++)
91 IDescriptor iDesc = iDescs.get(i);
93 iDesc.getDescriptorNames()[0],
94 classNames.get(i), iDesc, 0);
101 assertEquals(2, fp.
engine.getDescriptorInstances().size(),
102 "Number of descriptors from custom list");
110 IAtomContainer mol =
null;
112 mol =
sp.parseSmiles(
"C(C)CO");
113 }
catch (InvalidSmilesException e) {
117 List<String> classNames =
new ArrayList<String>();
118 classNames.add(
"org.openscience.cdk.qsar.descriptors.molecular."
119 +
"ZagrebIndexDescriptor");
120 classNames.add(
"org.openscience.cdk.qsar.descriptors.molecular."
121 +
"AtomCountDescriptor");
122 DescriptorEngine engine =
new DescriptorEngine(classNames,
null);
123 List<IDescriptor> iDescs = engine.instantiateDescriptors(classNames);
125 List<DescriptorForFitness> descriptors =
126 new ArrayList<DescriptorForFitness>();
127 String[] varNames =
new String[] {
"desc0",
"desc1"};
128 for (
int i=0; i<iDescs.size(); i++)
130 IDescriptor iDesc = iDescs.get(i);
132 iDesc.getDescriptorNames()[0],
133 classNames.get(i), iDesc, 0);
135 descriptors.add(dff);
138 String expression =
"${" + varNames[0] +
" + " + varNames[1] +
"}";
145 Map<Object, Object> props = mol.getProperties();
147 assertEquals(6,props.size(),
"Number of properties in processed mol");
148 List<Object> keys =
new ArrayList<Object>();
149 for (Object k : props.keySet())
153 if (props.get(varNames[0]).toString().equals(
"10.0"))
155 assertEquals(
"12.0",props.get(varNames[1]).toString(),
156 "Unexpected descriptor value (A)");
157 }
else if (props.get(varNames[1]).toString().equals(
"10.0"))
159 assertEquals(
"10.0",props.get(varNames[0]).toString(),
160 "Unexpected descriptor value (B)");
162 assertTrue(
false,
"Unexpected descriptor value (C)");
166 assertTrue(Math.abs(22.0 - fitness) < trsh,
167 "Fitness value should be 22.0 but is " + fitness);
176 List<String> classNames =
new ArrayList<String>();
177 classNames.add(
"denoptim.fitness.descriptors.TanimotoMolSimilarity");
178 DescriptorEngine engine =
new DescriptorEngine(classNames,
null);
179 engine.instantiateDescriptors(classNames);
183 IAtomContainer ref =
sp.parseSmiles(
"CNC(=O)c1cc(OC)ccc1");
184 PubchemFingerprinter fpMaker =
new PubchemFingerprinter(
185 DefaultChemObjectBuilder.getInstance());
186 IBitFingerprint fpRef = fpMaker.getBitFingerprint(ref);
187 Object[] params = {
"PubchemFingerprinter", fpRef};
188 iDesc.setParameters(params);
190 String myVarName =
"myVar";
194 iDesc.getDescriptorNames()[0],
195 iDesc.getClass().getName(), iDesc, 0);
197 List<DescriptorForFitness> descriptors =
198 new ArrayList<DescriptorForFitness>();
199 descriptors.add(dff);
200 String expression =
"${" + myVarName +
"}";
205 IAtomContainer mol =
sp.parseSmiles(
"COc1ccccc1");
212 assertTrue(propObj!=
null,
"Fitness is not null.");
214 assertTrue(Math.abs(((
double) propObj) - fitness) < trsh,
215 "Fitness value should be 0.6 but is " + fitness);
216 assertTrue(Math.abs(0.6 - fitness) < trsh,
217 "Fitness value should be 0.6 but is " + fitness);
230 String fileName =
tempDir.getAbsolutePath() +
SEP +
"ref.sdf";
231 IAtomContainer ref =
sp.parseSmiles(
"CNC(=O)c1cc(OC)ccc1");
236 String[] lines =
new String[] {
237 "FP-Equation=${taniSym + taniBis + 0.02 * Zagreb - aHyb_1 +"
239 "FP-DescriptorSpecs=${Variable.atomSpecific('aHyb_1','aHyb','[$([C])]')}",
240 "FP-DescriptorSpecs=${Variable.atomSpecific('aHyb_2','aHyb','[$([O])]')}",
241 "FP-DescriptorSpecs=${Variable.parametrized('taniSym',"
242 +
"'TanimotoSimilarity','PubchemFingerprinter, "
243 +
"FILE:" + fileName +
"')}",
244 "FP-DescriptorSpecs=${Variable.parametrized('taniBis',"
245 +
"'TanimotoSimilarity','GraphOnlyFingerprinter, "
246 +
"FILE:" + fileName +
"')}"};
247 for (
int i=0; i<lines.length; i++)
249 String line = lines[i];
259 IAtomContainer mol =
sp.parseSmiles(
"COc1ccccc1");
264 "taniSym",
"aHyb_1",
"aHyb_2"};
265 double[] expectedValue =
new double[] {
273 for (
int i=0; i<expectedProps.length; i++)
275 Object p = mol.getProperty(expectedProps[i]);
276 double value = Double.parseDouble(p.toString());
278 "Value of property '" + expectedProps[i] +
"' should be "
279 + expectedValue[i] +
" but is " + value);
297 IAtomContainer mol =
sp.parseSmiles(
"COc1ccccc1");
302 assertTrue(prop !=
null,
"Fitness property found in molecule");
303 assertTrue(
closeEnough(1.23456, Double.parseDouble(prop.toString())),
304 "Numerical result (" + Double.parseDouble(prop.toString())
312 double threshold = 0.0001;
313 double delta = Math.abs(expected-actual);
314 return delta < threshold;
General set of constants used in DENOPTIM.
static final String FITNESSTAG
SDF tag containing the fitness of a candidate.
This is a reference to a specific descriptor value.
void addDependentVariable(Variable v)
Append the reference to a variable that used data produced by the calculation of this descriptor.
Settings defining the calculation of fitness.
void processParameters()
Processes all parameters and initialize related objects.
String getFitnessExpression()
List< DescriptorForFitness > getDescriptors()
void interpretKeyword(String key, String value)
Processes a keyword/value pair and assign the related parameters.
DENOPTIM's (internal) fitness provider calculates the value of Variables that are used in an expressi...
DescriptorEngine engine
The engine that collects and calculates descriptors.
double getFitness(IAtomContainer iac)
Calculated the fitness according to the current configuration.
Unit test for internal fitness provider.
void testGetConstantFitness()
boolean closeEnough(double expected, double actual)
void testGetFitnessWithCustomDescriptors()
void testConfigureDescriptorsList()
void testGetFitnessWithParametrizedDescriptors()
This test is reproducing most of what done in FitnessParametersTest#testProcessExpressions() so if bo...
A variable in the expression defining the fitness.
Calculates the molecular similarity against a target compound the fingerprint of which is given as pa...
Utility methods for input/output.
static void writeSDFFile(String fileName, IAtomContainer mol)
Writes IAtomContainer to SDF file.