-
Notifications
You must be signed in to change notification settings - Fork 154
/
MCSPlusHandler.java
206 lines (182 loc) · 6.93 KB
/
MCSPlusHandler.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
/* Copyright (C) 2006-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 sourceAtom 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.mcsplus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
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.filters.PostFilter;
import org.openscience.cdk.smsd.helper.FinalMappings;
import org.openscience.cdk.smsd.interfaces.AbstractMCSAlgorithm;
import org.openscience.cdk.smsd.interfaces.IMCSBase;
import org.openscience.cdk.smsd.tools.MolHandler;
/**
* This class acts as a handler class for MCSPlus algorithm.
* {@link org.openscience.cdk.smsd.algorithm.mcsplus.MCSPlus}
* @cdk.module smsd
* @cdk.githash
* @author Syed Asad Rahman <asad@ebi.ac.uk>
*/
public class MCSPlusHandler extends AbstractMCSAlgorithm implements IMCSBase {
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;
private IAtomContainer source = null;
private IAtomContainer target = null;
private boolean flagExchange = false;
/**
* Constructor for the MCS Plus algorithm class
*/
public MCSPlusHandler() {
allAtomMCS = new ArrayList<Map<IAtom, IAtom>>();
atomsMCS = new HashMap<IAtom, IAtom>();
firstMCS = new TreeMap<Integer, Integer>();
allMCS = new ArrayList<Map<Integer, Integer>>();
}
/** {@inheritDoc}
*
* @param source
* @param target
*/
@Override
public synchronized void set(MolHandler source, MolHandler target) {
this.source = source.getMolecule();
this.target = target.getMolecule();
}
/** {@inheritDoc}
*
* @param source
* @param target
*/
@Override
public void set(IQueryAtomContainer source, IAtomContainer target) {
this.source = source;
this.target = target;
}
/** {@inheritDoc}
* Function is called by the main program and serves as a starting point for the comparison procedure.
*
* @param shouldMatchBonds
*/
@Override
public synchronized void searchMCS(boolean shouldMatchBonds) {
List<List<Integer>> mappings = null;
try {
if (source.getAtomCount() >= target.getAtomCount()) {
mappings = new MCSPlus().getOverlaps(source, target, shouldMatchBonds);
} else {
flagExchange = true;
mappings = new MCSPlus().getOverlaps(target, source, shouldMatchBonds);
}
PostFilter.filter(mappings);
setAllMapping();
setAllAtomMapping();
setFirstMapping();
setFirstAtomMapping();
} catch (CDKException e) {
mappings = null;
}
}
private synchronized void setAllMapping() {
try {
List<Map<Integer, Integer>> finalSolution = FinalMappings.getInstance().getFinalMapping();
int counter = 0;
for (Map<Integer, Integer> solution : finalSolution) {
// System.out.println("Number of MCS solution: " + solution);
Map<Integer, Integer> validSolution = new TreeMap<Integer, Integer>();
if (!flagExchange) {
for (Map.Entry<Integer, Integer> map : solution.entrySet()) {
validSolution.put(map.getKey(), map.getValue());
}
} else {
for (Map.Entry<Integer, Integer> map : solution.entrySet()) {
validSolution.put(map.getValue(), map.getKey());
}
}
allMCS.add(counter++, validSolution);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private synchronized void setAllAtomMapping() {
try {
int counter = 0;
for (Map<Integer, Integer> solution : allMCS) {
Map<IAtom, IAtom> atomMappings = new HashMap<IAtom, IAtom>();
for (Map.Entry<Integer, Integer> map : solution.entrySet()) {
int iIndex = map.getKey();
int jIndex = map.getValue();
IAtom sourceAtom = null;
IAtom targetAtom = null;
sourceAtom = source.getAtom(iIndex);
targetAtom = target.getAtom(jIndex);
atomMappings.put(sourceAtom, targetAtom);
}
allAtomMCS.add(counter++, atomMappings);
}
} catch (Exception I) {
I.getCause();
}
}
private synchronized void setFirstMapping() {
if (!allMCS.isEmpty()) {
firstMCS = new TreeMap<Integer, Integer>(allMCS.iterator().next());
}
}
private synchronized void setFirstAtomMapping() {
if (!allAtomMCS.isEmpty()) {
atomsMCS = new HashMap<IAtom, IAtom>(allAtomMCS.iterator().next());
}
}
/** {@inheritDoc}
*/
@Override
public synchronized List<Map<Integer, Integer>> getAllMapping() {
return allMCS;
}
/** {@inheritDoc}
*/
@Override
public synchronized Map<Integer, Integer> getFirstMapping() {
return firstMCS;
}
/** {@inheritDoc}
*/
@Override
public synchronized List<Map<IAtom, IAtom>> getAllAtomMapping() {
return allAtomMCS;
}
/** {@inheritDoc}
*/
@Override
public synchronized Map<IAtom, IAtom> getFirstAtomMapping() {
return atomsMCS;
}
}