$darkmode
DENOPTIM
TanimotoMolSimilarityBySubstructure.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.fitness.descriptors;
20
21import org.openscience.cdk.exception.CDKException;
22import org.openscience.cdk.fingerprint.IBitFingerprint;
23import org.openscience.cdk.fingerprint.IFingerprinter;
24import org.openscience.cdk.fingerprint.SubstructureFingerprinter;
25import org.openscience.cdk.interfaces.IAtomContainer;
26import org.openscience.cdk.qsar.AbstractMolecularDescriptor;
27import org.openscience.cdk.qsar.DescriptorSpecification;
28import org.openscience.cdk.qsar.DescriptorValue;
29import org.openscience.cdk.qsar.IMolecularDescriptor;
30import org.openscience.cdk.qsar.result.DoubleResult;
31import org.openscience.cdk.qsar.result.DoubleResultType;
32import org.openscience.cdk.qsar.result.IDescriptorResult;
33import org.openscience.cdk.similarity.Tanimoto;
34
35import denoptim.fitness.IDenoptimDescriptor;
36
37
44public class TanimotoMolSimilarityBySubstructure extends AbstractMolecularDescriptor
45implements IMolecularDescriptor, IDenoptimDescriptor
46{
47 //private static ILoggingTool logger = LoggingToolFactory.createLoggingTool(
48 // TanimotoMolSimilarity.class);
49 private IBitFingerprint referenceFingerprint;
50 private IFingerprinter fingerprinter;
51 private String[] substructuressmarts;
52 private static final String[] PARAMNAMES = new String[] {
53 "substructuressmarts","referenceFingerprint"};
54
55 private static final String[] NAMES = {"TanimotoSimilarityBySubstructure"};
56
57//------------------------------------------------------------------------------
58
63
64//------------------------------------------------------------------------------
65
76 @Override
77 public DescriptorSpecification getSpecification()
78 {
79 String paramID = "";
80 if (fingerprinter!=null && referenceFingerprint!=null)
81 {
82 paramID = "" + substructuressmarts.hashCode()
83 + referenceFingerprint.hashCode();
84 }
85 return new DescriptorSpecification("Denoptim source code",
86 this.getClass().getName(), paramID, "DENOPTIM project");
87 }
88
89//------------------------------------------------------------------------------
90
96 @Override
97 public String[] getParameterNames() {
98 return PARAMNAMES;
99 }
100
101//------------------------------------------------------------------------------
102
104 @Override
105 public Object getParameterType(String name)
106 {
107 if (name.equals(PARAMNAMES[1]))
108 {
109 return IBitFingerprint.class;
110 } else if (name.equals(PARAMNAMES[0])) {
111 return String[].class;
112 } else {
113 throw new IllegalArgumentException("No parameter for name: "+name);
114 }
115 }
116
117//------------------------------------------------------------------------------
118
127 @Override
128 public void setParameters(Object[] params) throws CDKException
129 {
130 if (params.length != 2)
131 {
132 throw new IllegalArgumentException(""
133 + "TanimotoMolSimilarityBySubstructure requires two "
134 + "parameters");
135 }
136 if (!(params[1] instanceof IBitFingerprint))
137 {
138 throw new IllegalArgumentException("Parameter does not implemet "
139 + "IBitFingerprint.");
140 }
141 if (!(params[0] instanceof String[]))
142 {
143 throw new IllegalArgumentException("Parameter is not String[] ("
144 + params[0].getClass().getName() + ").");
145 }
146
147 substructuressmarts = ((String[])params[0]);
148 fingerprinter = new SubstructureFingerprinter(substructuressmarts);
149 referenceFingerprint = (IBitFingerprint) params[1];
150 }
151
152//------------------------------------------------------------------------------
153
155 @Override
156 public Object[] getParameters()
157 {
158 Object[] params = new Object[2];
159 params[1] = referenceFingerprint;
160 params[0] = substructuressmarts;
161 return params;
162 }
163
164//------------------------------------------------------------------------------
165
167 @Override
168 public String[] getDescriptorNames()
169 {
170 return NAMES;
171 }
172
173//------------------------------------------------------------------------------
174
176 @Override
177 public DescriptorValue calculate(IAtomContainer mol)
178 {
179 DoubleResult result;
180 if (referenceFingerprint==null)
181 {
182 throw new IllegalStateException("Reference fingerprint not set. "
183 + "Cannot calculate Tanimoto similarity.");
184 }
185 if (fingerprinter==null)
186 {
187 throw new IllegalStateException("Fingerprinter not set. "
188 + "Cannot calculate Tanimoto similarity.");
189 }
190
191 try
192 {
193 result = new DoubleResult(Tanimoto.calculate(referenceFingerprint,
194 fingerprinter.getBitFingerprint(mol)));
195 } catch (IllegalArgumentException e)
196 {
197 e.printStackTrace();
198 result = new DoubleResult(Double.NaN);
199 } catch (CDKException e)
200 {
201 e.printStackTrace();
202 result = new DoubleResult(Double.NaN);
203 }
204
205 return new DescriptorValue(getSpecification(),
208 result,
210 }
211
212//------------------------------------------------------------------------------
213
215 @Override
216 public IDescriptorResult getDescriptorResultType()
217 {
218 return new DoubleResultType();
219 }
220
221//------------------------------------------------------------------------------
222
224 @Override
225 public String getDictionaryTitle()
226 {
227 return "Tanimoto Molecular Similarity By Substructures";
228 }
229
230//------------------------------------------------------------------------------
231
233 @Override
235 {
236 return "The Tanimoto Molecular Similarity By Susbtructure "
237 + "considers a given set of substructures to calculate "
238 + "the Tanimoto similarity between the "
239 + "reference fingerprint given upon definition of the "
240 + "descriptor (see parameters), and the fingerprint of a "
241 + "molecule given as "
242 + "argument when calculating the value of the descriptor. "
243 + "Fingerprints are obtained from a new instance of"
244 + "<code>SubstructureFingerprinter</code> defined by the "
245 + "list of SMARTS strings given "
246 + "as parameter <code>" + PARAMNAMES[0] + "</code>.";
247 }
248
249//------------------------------------------------------------------------------
250
252 @Override
253 public String[] getDictionaryClass()
254 {
255 return new String[] {"molecular"};
256 }
257
258//------------------------------------------------------------------------------
259
260}
Calculates the molecular similarity against a target compound the fingerprint of which is given as pa...
void setParameters(Object[] params)
Set the parameters attribute of TanimotoMolSimilarityBySubstructure object.
String[] getParameterNames()
Gets the parameterNames attribute of the TanimotoMolSimilarityBySubstructure object.
String getDictionaryTitle()
Gets the title of this descriptor as it should be in the dictionary.the title
DescriptorSpecification getSpecification()
Get the specification attribute of Tanimoto molecular similarity.
TanimotoMolSimilarityBySubstructure()
Constructor for a TanimotoMolSimilarity object.
String getDictionaryDefinition()
Get a string that describes the descriptor in detail.Might contain mathematical formulation....
String[] getDictionaryClass()
Get the classification of this descriptor.A descriptor can belong to one or more classes simultaneous...
This interface forces descriptors that are not defined in the CDK ontology to provide information tha...