/
VFlibHandler.java
226 lines (188 loc) · 7.05 KB
/
VFlibHandler.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
/* 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 org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.exception.CDKException;
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.TemplateCompiler;
import org.openscience.cdk.smsd.tools.MolHandler;
import org.openscience.cdk.smsd.interfaces.AbstractSubGraph;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.smsd.interfaces.IMCSBase;
/**
* This class should be used to find if a query
* graph is a substructure of the target graph.
* if its true, then this will return the mapped objects.
*
*
* @cdk.module smsd
* @cdk.githash
* @author Syed Asad Rahman <asad@ebi.ac.uk>
*/
@TestClass("org.openscience.cdk.smsd.algorithm.vflib.VFlibHandlerTest")
public class VFlibHandler extends AbstractSubGraph implements IMCSBase {
private IAtomContainer source;
private IAtomContainer target;
private static List<Map<IAtom, IAtom>> allAtomMCS = null;
private static Map<IAtom, IAtom> atomsMCS = null;
private static Map<Integer, Integer> firstMCS = null;
private static List<Map<Integer, Integer>> allMCS = null;
/**
* Constructor for the VF Algorithm for substructure search
*/
@TestMethod("setMCSAlgorithm")
public VFlibHandler() {
allAtomMCS = new ArrayList<Map<IAtom, IAtom>>();
atomsMCS = new HashMap<IAtom, IAtom>();
firstMCS = new TreeMap<Integer, Integer>();
allMCS = new ArrayList<Map<Integer, Integer>>();
}
/** {@inheritDoc}
*
*
* @return true if Query/source is a subgraph of Target/target
* else false
*/
@Override
@TestMethod("testIsSubgraph")
public boolean isSubgraph(){
IQuery query = TemplateCompiler.compile(source);
IMapper mapper = new VFMapper(query);
List<Map<INode, IAtom>> vfLibSolutions = mapper.getMaps(target);
int counter = 0;
for (Map<INode, IAtom> solution : vfLibSolutions) {
Map<IAtom, IAtom> atomatomMapping = new HashMap<IAtom, IAtom>();
TreeMap<Integer, Integer> indexindexMapping = new TreeMap<Integer, Integer>();
for (Map.Entry<INode, IAtom> mapping : solution.entrySet()) {
IAtom qAtom = query.getAtom(mapping.getKey());
IAtom tAtom = mapping.getValue();
Integer qIndex = source.getAtomNumber(qAtom);
Integer tIndex = target.getAtomNumber(tAtom);
atomatomMapping.put(qAtom, tAtom);
indexindexMapping.put(qIndex, tIndex);
}
if (!atomatomMapping.isEmpty()) {
allAtomMCS.add(counter, atomatomMapping);
allMCS.add(counter, indexindexMapping);
counter++;
}
}
if (!allAtomMCS.isEmpty() && allAtomMCS.size() > 0) {
atomsMCS.putAll(allAtomMCS.get(0));
firstMCS.putAll(allMCS.get(0));
}
return (!firstMCS.isEmpty() && firstMCS.size() == source.getAtomCount()) ? true : false;
}
/** {@inheritDoc}
*
* @param source
* @param target
*/
@Override
@TestMethod("testSet_IAtomContainer_IAtomContainer")
public void set(IAtomContainer source, IAtomContainer target) {
IAtomContainer mol1 = source;
IAtomContainer mol2 = target;
MolHandler Reactant = new MolHandler(mol1, false);
MolHandler Product = new MolHandler(mol2, false);
set(Reactant, Product);
}
/** {@inheritDoc}
*
* @param source
* @param target
*/
@TestMethod("testSet_IMolecule_IMolecule")
public void set(IMolecule source, IMolecule target) throws CDKException {
IMolecule mol1 = source;
IMolecule mol2 = target;
MolHandler Reactant = new MolHandler(mol1, false);
MolHandler Product = new MolHandler(mol2, false);
set(Reactant, Product);
}
/** {@inheritDoc}
*
* @param sourceMolFileName
* @param targetMolFileName
*/
@Override
@TestMethod("testSet_String_String")
public void set(String sourceMolFileName, String targetMolFileName) {
String mol1 = sourceMolFileName;
String mol2 = targetMolFileName;
MolHandler Reactant = new MolHandler(mol1, false);
MolHandler Product = new MolHandler(mol2, false);
set(Reactant, Product);
}
/** {@inheritDoc}
*
* @param source
* @param target
*/
@Override
@TestMethod("testSet_MolHandler_MolHandler")
public void set(MolHandler source, MolHandler target) {
this.source = source.getMolecule();
this.target = target.getMolecule();
}
/** {@inheritDoc}
*/
@Override
@TestMethod("testGetAllAtomMapping")
public List<Map<IAtom, IAtom>> getAllAtomMapping() {
return Collections.unmodifiableList(allAtomMCS);
}
/** {@inheritDoc}
*/
@Override
@TestMethod("testGetAllMapping")
public List<Map<Integer, Integer>> getAllMapping() {
return Collections.unmodifiableList(allMCS);
}
/** {@inheritDoc}
*/
@Override
@TestMethod("testGetFirstAtomMapping")
public Map<IAtom, IAtom> getFirstAtomMapping() {
return Collections.unmodifiableMap(atomsMCS);
}
/** {@inheritDoc}
*/
@Override
@TestMethod("testGetFirstMapping")
public Map<Integer, Integer> getFirstMapping() {
return Collections.unmodifiableMap(firstMCS);
}
}