19package denoptim.fitness;
22import java.util.ArrayList;
23import java.util.HashMap;
24import java.util.HashSet;
29import org.openscience.cdk.exception.CDKException;
30import org.openscience.cdk.fingerprint.IBitFingerprint;
31import org.openscience.cdk.fingerprint.IFingerprinter;
32import org.openscience.cdk.fingerprint.ShortestPathFingerprinter;
33import org.openscience.cdk.fingerprint.SubstructureFingerprinter;
34import org.openscience.cdk.interfaces.IAtomContainer;
35import org.openscience.cdk.qsar.IDescriptor;
36import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
38import denoptim.exception.DENOPTIMException;
39import denoptim.fitness.descriptors.TanimotoMolSimilarity;
40import denoptim.fitness.descriptors.TanimotoMolSimilarityBySubstructure;
41import denoptim.io.DenoptimIO;
42import jakarta.el.BeanELResolver;
43import jakarta.el.ELContext;
44import jakarta.el.ELResolver;
45import jakarta.el.ExpressionFactory;
46import jakarta.el.FunctionMapper;
47import jakarta.el.MethodExpression;
48import jakarta.el.ValueExpression;
49import jakarta.el.VariableMapper;
61 private List<Variable>
variables =
new ArrayList<Variable>();
68 new ArrayList<DescriptorForFitness>();
100 public void parse(String fitnessExpression,
104 ExpressionFactory expFactory = ExpressionFactory.newInstance();
106 for (String variableDefinition : customVarDescExpressions)
115 boolean useWindowsFileSeparator = variableDefinition.contains(
"\\");
116 String modVarDef = variableDefinition;
117 if (useWindowsFileSeparator)
118 modVarDef = variableDefinition.replace(
"\\",
"/");
120 MethodExpression me = expFactory.createMethodExpression(cvdc,
124 if (useWindowsFileSeparator)
126 for (
int ipar=0; ipar<v.
params.length; ipar++)
139 expFactory.createValueExpression(ncc, fitnessExpression, Double.class);
150 Set<String> descriptorImplementationNames =
new HashSet<String>();
153 descriptorImplementationNames.add(v.getDescriptorName());
155 List<DescriptorForFitness> rawDescriptors =
157 descriptorImplementationNames);
161 Map<String,DescriptorForFitness> standardDescriptors =
162 new HashMap<String,DescriptorForFitness>();
176 .filter(d -> descName.equals(d.getShortName()))
181 + descName +
"' that should be used to produce "
182 +
"variable '" + varName
183 +
"', but no match found in the list of descriptor "
184 +
"implementations. "
185 +
"Check the name of the descriptor you are trying to "
192 if (standardDescriptors.containsKey(rawDff.
getShortName()))
195 .addDependentVariable(v);
209 String[] parNames = impl.getParameterNames();
210 if (parNames.length != v.
params.length)
213 +
"configuration of descriptor '"
215 + varName +
"'. Found " + v.
params.length +
" but "
216 +
"the descriptor requires " + parNames.length+
".");
221 Object[] params =
new Object[parNames.length];
222 for (
int j=0; j<parNames.length; j++)
224 Object parType = impl.getParameterType(parNames[j]);
229 +
"the type of a parameter.");
232 if (parType instanceof Integer){
233 p = Integer.parseInt(v.
params[j]);
234 }
else if (parType instanceof Double) {
235 p = Double.parseDouble(v.
params[j]);
236 }
else if (parType instanceof Boolean) {
237 p = Boolean.getBoolean(v.
params[j]);
238 }
else if (parType instanceof String) {
240 }
else if (parType instanceof Class) {
242 String type = ((Class<?>) parType).getSimpleName();
245 case "IBitFingerprint":
252 v.
params[j],((String[])params[j-1]));
262 +
"not expected to need an "
263 +
"IBitFingerprint parameter.");
273 + parNames[j] +
"' for descriptor '"
274 + descName +
"' is requested to be of "
275 +
"type '" + type +
"' but no "
276 +
"handling of such type is available "
277 +
"in FitnessParameters. Please, "
278 +
"report this to the develoment "
283 + parNames[j] +
"' for descriptor '"
284 + descName +
"' in an instance of a class"
285 +
"that is not expected by "
286 +
"in FitnessParameters. Please, "
287 +
"report this to the develoment team.");
293 impl.setParameters(params);
294 }
catch (CDKException e)
299 +
"for descriptor '" + descName
300 +
"in FitnessParameters. Please, "
301 +
"report this to the develoment team.",e);
317 private FunctionMapper
fm =
null;
322 private VariableMapper
vm =
new VariableMapper() {
323 @SuppressWarnings(
"serial")
325 public ValueExpression resolveVariable(String variable) {
326 ValueExpression ve =
new ValueExpression() {
329 public Object getValue(ELContext context)
336 public void setValue(ELContext context, Object value)
342 public boolean isReadOnly(ELContext context)
348 public Class<?> getType(ELContext context)
354 public Class<?> getExpectedType()
360 public String getExpressionString()
366 public boolean equals(Object obj)
372 public int hashCode()
378 public boolean isLiteralText()
386 public ValueExpression setVariable(String variable,
387 ValueExpression expression) {
395 private ELResolver
resolver =
new BeanELResolver();
430 private FunctionMapper
fm =
null;
440 private VariableMapper
vm =
new VariableMapper() {
446 public ValueExpression resolveVariable(String variableName) {
449 if (variableName.equals(existing.getName()))
459 public ValueExpression setVariable(String variable,
460 ValueExpression expression) {
468 private ELResolver
resolver =
new BeanELResolver();
503 String key =
"FILE:";
505 if (!line.toUpperCase().startsWith(key))
508 +
"'String[]' can only be generated upon "
509 +
"reading a text lines from file. To this end, the "
510 +
"definition of the parameter *MUST* start with '"
511 + key +
"'. Input line '" + line +
"' does not.");
514 String fileName = line.substring(key.length()).trim();
516 String[] arr =
new String[lst.size()];
517 arr = lst.toArray(arr);
530 IFingerprinter fingerprinter =
new SubstructureFingerprinter(smarts);
543 String key =
"FILE:";
545 if (!line.toUpperCase().startsWith(key))
548 +
"'IBitFingerprint' can only be generated upon "
549 +
"reading a molecule from file. To this end, the "
550 +
"definition of the parameter *MUST* start with '"
551 + key +
"'. Input line '" + line +
"' does not.");
553 if (fingerprinter ==
null)
555 throw new IllegalArgumentException(
"ERROR! Fingerprinter should be "
556 +
"created before attempting generation of a fingerprint.");
558 String fileName = line.substring(key.length()).trim();
559 IBitFingerprint fp =
null;
563 new File(fileName)).get(0);
565 if (fingerprinter instanceof ShortestPathFingerprinter)
569 AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(
571 }
catch (CDKException e1)
574 +
"to calculate fingerprint of reference molecule.",
579 fp = fingerprinter.getBitFingerprint(refMol);
580 }
catch (Exception e)
583 +
"file '" + fileName +
"'.",e);
593 IFingerprinter fp =
null;
597 }
catch (Throwable t)
600 + classShortName +
"'.", t);
This is a reference to a specific descriptor value.
IDescriptor implementation
Implementation of the descriptor's calculator.
DescriptorForFitness makeCopy()
Copy this descriptor and created an independent instance of the underlying descriptor implementation.
void addDependentVariable(Variable v)
Append the reference to a variable that used data produced by the calculation of this descriptor.
static List< DescriptorForFitness > findAllDescriptorImplementations(Set< String > requiredDescriptors)
Searches for descriptor implementations.
In this context we only need to know that "Variable" is mapped to the class Variable.
VariableMapper getVariableMapper()
FunctionMapper getFunctionMapper()
ELResolver getELResolver()
VariableMapper vm
Mapper that always returns a new blank instance of Variable.
ELResolver resolver
Resolver exploiting JavaBean contract.
This is the context on which we read a fitness expression, and parse it to identify all variables in ...
VariableMapper vm
This mapper does not map variables, but it creates an instance of Variable named with the given strin...
VariableMapper getVariableMapper()
FunctionMapper getFunctionMapper()
List< Variable > variables
Reference to where we collect all the created variables.
ELResolver getELResolver()
VariableDefiningContext(List< Variable > variables)
Constructor specifying the destination of the variables created when parsing the expression.
ELResolver resolver
Resolver exploiting JavaBean contract.
Class parsing fitness expression by means of Expression Language.
String[] makeStringArray(String line)
List< DescriptorForFitness > descriptors
The list of descriptors needed to calculate the variables that are used to calculate the fitness with...
List< DescriptorForFitness > getDescriptors()
Returns the list of descriptors needed to compute the numerical values of the variables in the expres...
List< Variable > variables
List of variables used in the calculation of the fitness.
void parse(String fitnessExpression, List< String > customVarDescExpressions)
Parses the given expressions.
IBitFingerprint makeIBitFingerprintBySubstructure(String line, String[] smarts)
For now we only accept a filename from which we read in a molecule.
IFingerprinter makeIFingerprinter(String classShortName)
List< Variable > getVariables()
Returns the list of variables in the expression of the fitness.
IBitFingerprint makeIBitFingerprint(String line, IFingerprinter fingerprinter)
For now we only accept a filename from which we read in a molecule.
FitnessExpressionParser()
Creates a new parser.
A variable in the expression defining the fitness.
String[] params
Definition of custom parameters for the configuration of the descriptor implementation.
String getName()
Get the name of this variable, i.e., the string used in the fitness-defining expression.
String getDescriptorName()
Get the short name of the descriptor implementation.
void setDescriptorName(String descName)
Set the short name of the descriptor implementation.
Calculates the molecular similarity against a target compound the fingerprint of which is given as pa...
Calculates the molecular similarity against a target compound the fingerprint of which is given as pa...
static IFingerprinter makeIFingerprinter(String classShortName)
Utility methods for input/output.
static ArrayList< String > readList(String fileName)
Read list of data as text.
static List< IAtomContainer > readAllAtomContainers(File file)
Returns a single collection with all atom containers found in a file of any format.