-
Notifications
You must be signed in to change notification settings - Fork 154
/
VFlibSubStructureHandler.java
310 lines (287 loc) · 11.6 KB
/
VFlibSubStructureHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright (C) 2009-2010 Syed Asad Rahman <asad@ebi.ac.uk>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.smsd.algorithm.vflib;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer;
import org.openscience.cdk.smsd.algorithm.vflib.interfaces.IMapper;
import org.openscience.cdk.smsd.algorithm.vflib.interfaces.INode;
import org.openscience.cdk.smsd.algorithm.vflib.interfaces.IQuery;
import org.openscience.cdk.smsd.algorithm.vflib.map.VFMapper;
import org.openscience.cdk.smsd.algorithm.vflib.query.QueryCompiler;
import org.openscience.cdk.smsd.interfaces.AbstractSubGraph;
import org.openscience.cdk.smsd.interfaces.IMCSBase;
import org.openscience.cdk.smsd.tools.MolHandler;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
/**
* This is an ultra fast method to report if query
* is a substructure for target molecule. If this case is true
* then it returns only all mapping.
*
* This is much faster than {@link
* org.openscience.cdk.smsd.algorithm.vflib.VFlibMCSHandler} class
* as it only reports first match and backtracks.
*
* This class should only be used to report if a query
* graph is a substructure of the target graph.
*
* @cdk.module smsd
* @cdk.githash
* @author Syed Asad Rahman <asad@ebi.ac.uk>
*/
public class VFlibSubStructureHandler extends AbstractSubGraph implements IMCSBase {
private static List<Map<IAtom, IAtom>> allAtomMCS = null;
private static Map<IAtom, IAtom> atomsMCS = null;
private static List<Map<IAtom, IAtom>> allAtomMCSCopy = null;
private static Map<Integer, Integer> firstMCS = null;
private static List<Map<Integer, Integer>> allMCS = null;
private static List<Map<Integer, Integer>> allMCSCopy = null;
private IQueryAtomContainer queryMol = null;
private IAtomContainer mol1 = null;
private IAtomContainer mol2 = null;
private List<Map<INode, IAtom>> vfLibSolutions = null;
private int vfMCSSize = -1;
private boolean bondMatchFlag = false;
private final static ILoggingTool LOGGER = LoggingToolFactory
.createLoggingTool(VFlibSubStructureHandler.class);
/**
* Constructor for an extended VF Algorithm for the MCS search
*/
public VFlibSubStructureHandler() {
allAtomMCS = new ArrayList<Map<IAtom, IAtom>>();
allAtomMCSCopy = new ArrayList<Map<IAtom, IAtom>>();
atomsMCS = new HashMap<IAtom, IAtom>();
firstMCS = new TreeMap<Integer, Integer>();
allMCS = new ArrayList<Map<Integer, Integer>>();
allMCSCopy = new ArrayList<Map<Integer, Integer>>();
}
private void setFirstMappings() {
if (!allAtomMCS.isEmpty()) {
atomsMCS.putAll(allAtomMCS.get(0));
firstMCS.putAll(allMCS.get(0));
}
}
private boolean mcgregorFlag() {
int commonAtomCount = checkCommonAtomCount(getReactantMol(), getProductMol());
if (commonAtomCount > vfMCSSize && commonAtomCount > vfMCSSize) {
return true;
}
return false;
}
/** {@inheritDoc}
*
* Set the VFLib MCS software
*
* @param reactant
* @param product
*/
@Override
public void set(MolHandler reactant, MolHandler product) {
mol1 = reactant.getMolecule();
mol2 = product.getMolecule();
}
/** {@inheritDoc}
*
* @param source
* @param target
*/
@Override
public void set(IQueryAtomContainer source, IAtomContainer target) {
queryMol = source;
mol2 = target;
}
private boolean hasMap(Map<Integer, Integer> map, List<Map<Integer, Integer>> mapGlobal) {
for (Map<Integer, Integer> test : mapGlobal) {
if (test.equals(map)) {
return true;
}
}
return false;
}
/** {@inheritDoc} */
@Override
public List<Map<IAtom, IAtom>> getAllAtomMapping() {
return Collections.unmodifiableList(allAtomMCS);
}
/** {@inheritDoc} */
@Override
public List<Map<Integer, Integer>> getAllMapping() {
return Collections.unmodifiableList(allMCS);
}
/** {@inheritDoc} */
@Override
public Map<IAtom, IAtom> getFirstAtomMapping() {
return Collections.unmodifiableMap(atomsMCS);
}
/** {@inheritDoc} */
@Override
public Map<Integer, Integer> getFirstMapping() {
return Collections.unmodifiableMap(firstMCS);
}
private int checkCommonAtomCount(IAtomContainer reactantMolecule, IAtomContainer productMolecule) {
ArrayList<String> atoms = new ArrayList<String>();
for (int i = 0; i < reactantMolecule.getAtomCount(); i++) {
atoms.add(reactantMolecule.getAtom(i).getSymbol());
}
int common = 0;
for (int i = 0; i < productMolecule.getAtomCount(); i++) {
String symbol = productMolecule.getAtom(i).getSymbol();
if (atoms.contains(symbol)) {
atoms.remove(symbol);
common++;
}
}
return common;
}
private void searchVFMappings() {
// System.out.println("searchVFMappings ");
IQuery query = null;
IMapper mapper = null;
vfLibSolutions = new ArrayList<Map<INode, IAtom>>();
if (queryMol != null) {
query = new QueryCompiler(queryMol).compile();
mapper = new VFMapper(query);
if (mapper.hasMap(getProductMol())) {
List<Map<INode, IAtom>> maps = mapper.getMaps(getProductMol());
if (maps != null) {
vfLibSolutions.addAll(maps);
}
}
setVFMappings(true, query);
} else if (getReactantMol().getAtomCount() <= getProductMol().getAtomCount()) {
query = new QueryCompiler(mol1, isBondMatchFlag()).compile();
mapper = new VFMapper(query);
if (mapper.hasMap(getProductMol())) {
List<Map<INode, IAtom>> maps = mapper.getMaps(getProductMol());
if (maps != null) {
vfLibSolutions.addAll(maps);
}
}
setVFMappings(true, query);
} else {
query = new QueryCompiler(getProductMol(), isBondMatchFlag()).compile();
mapper = new VFMapper(query);
if (mapper.hasMap(getReactantMol())) {
List<Map<INode, IAtom>> maps = mapper.getMaps(getReactantMol());
if (maps != null) {
vfLibSolutions.addAll(maps);
}
}
setVFMappings(false, query);
}
}
private void setVFMappings(boolean ronp, IQuery query) {
int counter = 0;
for (Map<INode, IAtom> solution : vfLibSolutions) {
Map<IAtom, IAtom> atomatomMapping = new HashMap<IAtom, IAtom>();
Map<Integer, Integer> indexindexMapping = new TreeMap<Integer, Integer>();
if (solution.size() > vfMCSSize) {
this.vfMCSSize = solution.size();
allAtomMCSCopy.clear();
allMCSCopy.clear();
counter = 0;
}
for (Map.Entry<INode, IAtom> mapping : solution.entrySet()) {
IAtom qAtom = null;
IAtom tAtom = null;
if (ronp) {
qAtom = query.getAtom(mapping.getKey());
tAtom = mapping.getValue();
} else {
tAtom = query.getAtom(mapping.getKey());
qAtom = mapping.getValue();
}
Integer qIndex = Integer.valueOf(getReactantMol().getAtomNumber(qAtom));
Integer tIndex = Integer.valueOf(getProductMol().getAtomNumber(tAtom));
if (qIndex != -1 && tIndex != -1) {
atomatomMapping.put(qAtom, tAtom);
indexindexMapping.put(qIndex, tIndex);
} else {
try {
throw new CDKException("Atom index pointing to NULL");
} catch (CDKException ex) {
LOGGER.error(Level.SEVERE, null, ex);
}
}
}
// System.out.println("indexindexMapping " + indexindexMapping.size());
// System.out.println("MCS Size " + vfMCSSize);
if (!atomatomMapping.isEmpty() && !hasMap(indexindexMapping, allMCSCopy)
&& indexindexMapping.size() == vfMCSSize) {
allAtomMCSCopy.add(counter, atomatomMapping);
allMCSCopy.add(counter, indexindexMapping);
counter++;
}
}
// System.out.println("allMCSCopy " + allMCSCopy.size());
}
@Override
public boolean isSubgraph(boolean shouldMatchBonds) {
setBondMatchFlag(shouldMatchBonds);
searchVFMappings();
// boolean flag = mcgregorFlag();
// if (flag && !vfLibSolutions.isEmpty()) {
// try {
// searchMcGregorMapping();
// } catch (CDKException ex) {
// LOGGER.error(Level.SEVERE, null, ex);
// } catch (IOException ex) {
// LOGGER.error(Level.SEVERE, null, ex);
// }
//
// } else
if (!allAtomMCSCopy.isEmpty()) {
allAtomMCS.addAll(allAtomMCSCopy);
allMCS.addAll(allMCSCopy);
}
setFirstMappings();
return (!allMCS.isEmpty() && allMCS.iterator().next().size() == getReactantMol().getAtomCount()) ? true : false;
}
/**
* @return the shouldMatchBonds
*/
public boolean isBondMatchFlag() {
return bondMatchFlag;
}
/**
* @param shouldMatchBonds the shouldMatchBonds to set
*/
public void setBondMatchFlag(boolean shouldMatchBonds) {
this.bondMatchFlag = shouldMatchBonds;
}
private IAtomContainer getReactantMol() {
return queryMol == null ? mol1 : queryMol;
}
private IAtomContainer getProductMol() {
return mol2;
}
}