$darkmode
DENOPTIM
FragmentIsomorphismInspector.java
Go to the documentation of this file.
1package denoptim.graph;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.Comparator;
6import java.util.Iterator;
7import java.util.List;
8import java.util.concurrent.ExecutorService;
9import java.util.concurrent.Executors;
10import java.util.concurrent.Future;
11import java.util.concurrent.TimeUnit;
12import java.util.concurrent.TimeoutException;
13
14import org.jgrapht.GraphMapping;
15import org.jgrapht.alg.isomorphism.VF2GraphIsomorphismInspector;
16
17import denoptim.exception.DENOPTIMException;
18import denoptim.io.DenoptimIO;
19
35{
39 private int timeout;
40
44 private Fragment fragA;
45
49 private Fragment fragB;
50
54 protected boolean reportTimeoutIncidents = true;
55
59 VF2GraphIsomorphismInspector<FragIsomorphNode, FragIsomorphEdge> vf2;
60
61
62//------------------------------------------------------------------------------
63
70 {
71 this(fragA, fragB, 60000, false);
72 }
73
74//------------------------------------------------------------------------------
75
83 boolean ignoreAPClasses)
84 {
85 this(fragA, fragB, 60000, ignoreAPClasses);
86 }
87
88//------------------------------------------------------------------------------
89
100 int timeout, boolean ignoreAPClasses)
101 {
102 this.timeout = timeout;
103 Comparator<FragIsomorphNode> vComp = new Comparator<FragIsomorphNode>()
104 {
105 @Override
106 public int compare(FragIsomorphNode n1, FragIsomorphNode n2)
107 {
108 if (ignoreAPClasses)
109 {
110 String tmpLbl1 = "ap";
111 String tmpLbl2 = "ap";
112 if (n1.isAtm)
113 tmpLbl1 = n1.label;
114 if (n2.isAtm)
115 tmpLbl2 = n2.label;
116 return tmpLbl1.compareTo(tmpLbl2);
117 } else {
118 return n1.label.compareTo(n2.label);
119 }
120 }
121 };
122
123 Comparator<FragIsomorphEdge> eComp = new Comparator<FragIsomorphEdge>()
124 {
125 @Override
126 public int compare(FragIsomorphEdge e1, FragIsomorphEdge e2)
127 {
128 return e1.label.compareTo(e2.label);
129 }
130 };
131
132 vf2 = new VF2GraphIsomorphismInspector<>(
134 fragB.getJGraphFragIsomorphism(), vComp, eComp);
135 this.fragA = fragA;
136 this.fragB = fragB;
137 }
138
139//------------------------------------------------------------------------------
140
145 public boolean isomorphismExists()
146 {
147 boolean result = false;
148 final ExecutorService service = Executors.newSingleThreadExecutor();
149 Future<Boolean> future = null;
150 try {
151 future = service.submit(() -> {
152 return vf2.isomorphismExists();
153 });
154 result = future.get(timeout, TimeUnit.MILLISECONDS);
155 } catch (final TimeoutException e) {
157 {
158 String fileName = "denoptim_isomorphism_timedout_case.sdf";
159 File file = new File(fileName);
160 System.err.println("WARNING: timeout reached when attempting "
161 + "detection of isomerism between fragments saved to '"
162 + file.getAbsolutePath() + "'. "
163 + "When timeout is reaches, fragments are"
164 + "considered to be non-isomorphic.");
165 List<Vertex> frags = new ArrayList<Vertex>();
166 frags.add(fragA);
167 frags.add(fragB);
168 try
169 {
170 DenoptimIO.writeVertexesToSDF(new File(fileName), frags, true);
171 } catch (DENOPTIMException e1)
172 {
173 System.err.println("WARNING: could not write to '"
174 + fileName + "'. Reporting fragments to STDERR:."
175 + System.getProperty("line.separator")
176 + fragA.toJson()
177 + System.getProperty("line.separator")
178 + fragB.toJson());
179 }
180 }
181 future.cancel(true);
182 } catch (final Exception e) {
183 throw new RuntimeException(e);
184 } finally {
185 service.shutdown();
186 }
187 return result;
188 }
189
190//------------------------------------------------------------------------------
191
192 public Iterator<GraphMapping<FragIsomorphNode, FragIsomorphEdge>> getMappings()
193 {
194 Iterator<GraphMapping<FragIsomorphNode, FragIsomorphEdge>> mapping = null;
195 final ExecutorService service = Executors.newSingleThreadExecutor();
196 Future<Iterator<GraphMapping<FragIsomorphNode, FragIsomorphEdge>>> fut =
197 null;
198 try {
199 fut = service.submit(() -> {
200 return vf2.getMappings();
201 });
202 mapping = fut.get(timeout, TimeUnit.MILLISECONDS);
203 } catch (final TimeoutException e) {
205 {
206 String fileName = "denoptim_isomorphism_timedout_case.sdf";
207 File file = new File(fileName);
208 System.err.println("WARNING: timeout reached when attempting "
209 + "detection of isomerism between fragments saved to '"
210 + file.getAbsolutePath() + "'. "
211 + "When timeout is reaches, fragments are"
212 + "considered to be non-isomorphic.");
213 List<Vertex> frags = new ArrayList<Vertex>();
214 frags.add(fragA);
215 frags.add(fragB);
216 try
217 {
218 DenoptimIO.writeVertexesToSDF(new File(fileName), frags, true);
219 } catch (DENOPTIMException e1)
220 {
221 System.err.println("WARNING: could not write to '"
222 + fileName + "'. Reporting fragments to STDERR:."
223 + System.getProperty("line.separator")
224 + fragA.toJson()
225 + System.getProperty("line.separator")
226 + fragB.toJson());
227 }
228 }
229 fut.cancel(true);
230 } catch (final Exception e) {
231 throw new RuntimeException(e);
232 } finally {
233 service.shutdown();
234 }
235 return mapping;
236 }
237
238//------------------------------------------------------------------------------
239
240}
String label
Bond order or name used to identify the edge type.
String label
Elemental symbol or name used to identify the node content.
boolean isAtm
true if the original object was an atom.
Class representing a continuously connected portion of chemical object holding attachment points.
Definition: Fragment.java:61
DefaultUndirectedGraph< FragIsomorphNode, FragIsomorphEdge > getJGraphFragIsomorphism()
Creates a graph representation of this fragment where both atoms and AttachmentPoints are represented...
Definition: Fragment.java:996
String toJson()
Produces a string that represents this vertex and that adheres to the JSON format.
Definition: Fragment.java:1209
int timeout
The maximum time we give the VF2 algorithm (seconds)
boolean reportTimeoutIncidents
Flag indicating if we print earning in case of timeout or not.
Fragment fragB
The other fragment being analyzed.
FragmentIsomorphismInspector(Fragment fragA, Fragment fragB, boolean ignoreAPClasses)
Constructs a default inspector with a timeout runtime of 60 seconds.
boolean isomorphismExists()
Checks if an isomorphism exists between the two fragments.
VF2GraphIsomorphismInspector< FragIsomorphNode, FragIsomorphEdge > vf2
Implementation of the Vento-Foggia 2 algorithm.
FragmentIsomorphismInspector(Fragment fragA, Fragment fragB, int timeout, boolean ignoreAPClasses)
Constructs an inspector with a custom timeout limit.
Iterator< GraphMapping< FragIsomorphNode, FragIsomorphEdge > > getMappings()
FragmentIsomorphismInspector(Fragment fragA, Fragment fragB)
Constructs a default inspector with a timeout runtime of 60 seconds.
Utility methods for input/output.
static void writeVertexesToSDF(File file, List< Vertex > vertexes, boolean append)
Write a list of vertexes to file.