1package denoptim.fitness;
3import static org.junit.Assert.assertNotNull;
24import static org.junit.jupiter.api.Assertions.assertEquals;
25import static org.junit.jupiter.api.Assertions.assertTrue;
28import java.util.ArrayList;
31import java.util.logging.Level;
32import java.util.logging.Logger;
34import org.junit.jupiter.api.BeforeEach;
35import org.junit.jupiter.api.Test;
36import org.junit.jupiter.api.io.TempDir;
37import org.openscience.cdk.DefaultChemObjectBuilder;
38import org.openscience.cdk.exception.InvalidSmilesException;
39import org.openscience.cdk.fingerprint.IBitFingerprint;
40import org.openscience.cdk.fingerprint.PubchemFingerprinter;
41import org.openscience.cdk.interfaces.IAtomContainer;
42import org.openscience.cdk.qsar.DescriptorEngine;
43import org.openscience.cdk.qsar.IDescriptor;
44import org.openscience.cdk.silent.SilentChemObjectBuilder;
45import org.openscience.cdk.smiles.SmilesParser;
47import denoptim.constants.DENOPTIMConstants;
48import denoptim.fitness.descriptors.SocketProvidedDescriptorTest;
49import denoptim.fitness.descriptors.TanimotoMolSimilarity;
50import denoptim.io.DenoptimIO;
60 private SmilesParser
sp;
61 private static final String
SEP = System.getProperty(
"file.separator");
71 assertTrue(
tempDir.isDirectory(),
"Should be a directory ");
72 sp =
new SmilesParser(SilentChemObjectBuilder.getInstance());
73 logger = Logger.getLogger(
"DummyLogger");
74 logger.setLevel(Level.SEVERE);
82 List<String> classNames =
new ArrayList<String>();
83 classNames.add(
"org.openscience.cdk.qsar.descriptors.molecular."
84 +
"ZagrebIndexDescriptor");
85 classNames.add(
"org.openscience.cdk.qsar.descriptors.molecular."
86 +
"AtomCountDescriptor");
87 DescriptorEngine engine =
new DescriptorEngine(classNames,
null);
88 List<IDescriptor> iDescs = engine.instantiateDescriptors(classNames);
90 List<DescriptorForFitness> descriptors =
91 new ArrayList<DescriptorForFitness>();
92 for (
int i=0; i<iDescs.size(); i++)
94 IDescriptor iDesc = iDescs.get(i);
96 iDesc.getDescriptorNames()[0],
97 classNames.get(i), iDesc, 0);
104 assertEquals(2, fp.
engine.getDescriptorInstances().size(),
105 "Number of descriptors from custom list");
113 IAtomContainer mol =
null;
115 mol =
sp.parseSmiles(
"C(C)CO");
116 }
catch (InvalidSmilesException e) {
120 List<String> classNames =
new ArrayList<String>();
121 classNames.add(
"org.openscience.cdk.qsar.descriptors.molecular."
122 +
"ZagrebIndexDescriptor");
123 classNames.add(
"org.openscience.cdk.qsar.descriptors.molecular."
124 +
"AtomCountDescriptor");
125 DescriptorEngine engine =
new DescriptorEngine(classNames,
null);
126 List<IDescriptor> iDescs = engine.instantiateDescriptors(classNames);
128 List<DescriptorForFitness> descriptors =
129 new ArrayList<DescriptorForFitness>();
130 String[] varNames =
new String[] {
"desc0",
"desc1"};
131 for (
int i=0; i<iDescs.size(); i++)
133 IDescriptor iDesc = iDescs.get(i);
135 iDesc.getDescriptorNames()[0],
136 classNames.get(i), iDesc, 0);
138 descriptors.add(dff);
141 String expression =
"${" + varNames[0] +
" + " + varNames[1] +
"}";
148 Map<Object, Object> props = mol.getProperties();
150 assertEquals(6,props.size(),
"Number of properties in processed mol");
151 List<Object> keys =
new ArrayList<Object>();
152 for (Object k : props.keySet())
156 if (props.get(varNames[0]).toString().equals(
"10.0"))
158 assertEquals(
"12.0",props.get(varNames[1]).toString(),
159 "Unexpected descriptor value (A)");
160 }
else if (props.get(varNames[1]).toString().equals(
"10.0"))
162 assertEquals(
"10.0",props.get(varNames[0]).toString(),
163 "Unexpected descriptor value (B)");
165 assertTrue(
false,
"Unexpected descriptor value (C)");
169 assertTrue(Math.abs(22.0 - fitness) < trsh,
170 "Fitness value should be 22.0 but is " + fitness);
179 List<String> classNames =
new ArrayList<String>();
180 classNames.add(
"denoptim.fitness.descriptors.TanimotoMolSimilarity");
181 DescriptorEngine engine =
new DescriptorEngine(classNames,
null);
182 engine.instantiateDescriptors(classNames);
186 IAtomContainer ref =
sp.parseSmiles(
"CNC(=O)c1cc(OC)ccc1");
187 PubchemFingerprinter fpMaker =
new PubchemFingerprinter(
188 DefaultChemObjectBuilder.getInstance());
189 IBitFingerprint fpRef = fpMaker.getBitFingerprint(ref);
190 Object[] params = {
"PubchemFingerprinter", fpRef};
191 iDesc.setParameters(params);
193 String myVarName =
"myVar";
197 iDesc.getDescriptorNames()[0],
198 iDesc.getClass().getName(), iDesc, 0);
200 List<DescriptorForFitness> descriptors =
201 new ArrayList<DescriptorForFitness>();
202 descriptors.add(dff);
203 String expression =
"${" + myVarName +
"}";
208 IAtomContainer mol =
sp.parseSmiles(
"COc1ccccc1");
215 assertTrue(propObj!=
null,
"Fitness is not null.");
217 assertTrue(Math.abs(((
double) propObj) - fitness) < trsh,
218 "Fitness value should be 0.6 but is " + fitness);
219 assertTrue(Math.abs(0.6 - fitness) < trsh,
220 "Fitness value should be 0.6 but is " + fitness);
233 String fileName =
tempDir.getAbsolutePath() +
SEP +
"ref.sdf";
234 IAtomContainer ref =
sp.parseSmiles(
"CNC(=O)c1cc(OC)ccc1");
239 String[] lines =
new String[] {
240 "FP-Equation=${taniSym + taniBis + 0.02 * Zagreb - aHyb_1 +"
242 "FP-DescriptorSpecs=${Variable.atomSpecific('aHyb_1','aHyb','[$([C])]')}",
243 "FP-DescriptorSpecs=${Variable.atomSpecific('aHyb_2','aHyb','[$([O])]')}",
244 "FP-DescriptorSpecs=${Variable.parametrized('taniSym',"
245 +
"'TanimotoSimilarity','PubchemFingerprinter, "
246 +
"FILE:" + fileName +
"')}",
247 "FP-DescriptorSpecs=${Variable.parametrized('taniBis',"
248 +
"'TanimotoSimilarity','GraphOnlyFingerprinter, "
249 +
"FILE:" + fileName +
"')}"};
250 for (
int i=0; i<lines.length; i++)
252 String line = lines[i];
262 IAtomContainer mol =
sp.parseSmiles(
"COc1ccccc1");
267 "Zagreb",
"taniBis",
"taniSym",
"aHyb_1",
"aHyb_2"};
268 double[] expectedValue =
new double[] {
276 for (
int i=0; i<expectedProps.length; i++)
278 Object p = mol.getProperty(expectedProps[i]);
279 double value = Double.parseDouble(p.toString());
281 "Value of property '" + expectedProps[i] +
"' should be "
282 + expectedValue[i] +
" but is " + value);
300 IAtomContainer mol =
sp.parseSmiles(
"COc1ccccc1");
305 assertTrue(prop !=
null,
"Fitness property found in molecule");
306 assertTrue(
closeEnough(1.23456, Double.parseDouble(prop.toString())),
307 "Numerical result (" + Double.parseDouble(prop.toString())
324 +
"${Variable.parametrized('fromSocket',"
325 +
"'SocketProvidedDescriptor','localhost, "
335 IAtomContainer mol =
sp.parseSmiles(smiles);
336 mol.setProperty(
"SMILES", smiles);
340 assertTrue(Double.isNaN(fitness));
343 assertTrue(prop.toString().contains(socketServerUtil.
fakeErrorMsg));
354 double threshold = 0.0001;
355 double delta = Math.abs(expected-actual);
356 return delta < threshold;
General set of constants used in DENOPTIM.
static final String MOLERRORTAG
SDF tag containing errors during execution of molecule specific tasks.
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.
Unit test for descriptor SocketProvidedDescriptor.
SocketProvidedDescriptor descriptor
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.