$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
69 {
70 this(fragA, fragB, 60000);
71 }
72
73//------------------------------------------------------------------------------
74
85 int timeout)
86 {
87 this.timeout = timeout;
88 Comparator<FragIsomorphNode> vComp = new Comparator<FragIsomorphNode>()
89 {
90 @Override
91 public int compare(FragIsomorphNode n1, FragIsomorphNode n2)
92 {
93 return n1.label.compareTo(n2.label);
94 }
95 };
96
97 Comparator<FragIsomorphEdge> eComp = new Comparator<FragIsomorphEdge>()
98 {
99 @Override
100 public int compare(FragIsomorphEdge e1, FragIsomorphEdge e2)
101 {
102 return e1.label.compareTo(e2.label);
103 }
104 };
105
106 vf2 = new VF2GraphIsomorphismInspector<>(
108 fragB.getJGraphFragIsomorphism(), vComp, eComp);
109 this.fragA = fragA;
110 this.fragB = fragB;
111 }
112
113//------------------------------------------------------------------------------
114
119 public boolean isomorphismExists()
120 {
121 boolean result = false;
122 final ExecutorService service = Executors.newSingleThreadExecutor();
123 Future<Boolean> future = null;
124 try {
125 future = service.submit(() -> {
126 return vf2.isomorphismExists();
127 });
128 result = future.get(timeout, TimeUnit.MILLISECONDS);
129 } catch (final TimeoutException e) {
131 {
132 String fileName = "denoptim_isomorphism_timedout_case.sdf";
133 File file = new File(fileName);
134 System.err.println("WARNING: timeout reached when attempting "
135 + "detection of isomerism between fragments saved to '"
136 + file.getAbsolutePath() + "'. "
137 + "When timeout is reaches, fragments are"
138 + "considered to be non-isomorphic.");
139 List<Vertex> frags = new ArrayList<Vertex>();
140 frags.add(fragA);
141 frags.add(fragB);
142 try
143 {
144 DenoptimIO.writeVertexesToSDF(new File(fileName), frags, true);
145 } catch (DENOPTIMException e1)
146 {
147 System.err.println("WARNING: could not write to '"
148 + fileName + "'. Reporting fragments to STDERR:."
149 + System.getProperty("line.separator")
150 + fragA.toJson()
151 + System.getProperty("line.separator")
152 + fragB.toJson());
153 }
154 }
155 future.cancel(true);
156 } catch (final Exception e) {
157 throw new RuntimeException(e);
158 } finally {
159 service.shutdown();
160 }
161 return result;
162 }
163
164//------------------------------------------------------------------------------
165
166 public Iterator<GraphMapping<FragIsomorphNode, FragIsomorphEdge>> getMappings()
167 {
168 Iterator<GraphMapping<FragIsomorphNode, FragIsomorphEdge>> mapping = null;
169 final ExecutorService service = Executors.newSingleThreadExecutor();
170 Future<Iterator<GraphMapping<FragIsomorphNode, FragIsomorphEdge>>> fut =
171 null;
172 try {
173 fut = service.submit(() -> {
174 return vf2.getMappings();
175 });
176 mapping = fut.get(timeout, TimeUnit.MILLISECONDS);
177 } catch (final TimeoutException e) {
179 {
180 String fileName = "denoptim_isomorphism_timedout_case.sdf";
181 File file = new File(fileName);
182 System.err.println("WARNING: timeout reached when attempting "
183 + "detection of isomerism between fragments saved to '"
184 + file.getAbsolutePath() + "'. "
185 + "When timeout is reaches, fragments are"
186 + "considered to be non-isomorphic.");
187 List<Vertex> frags = new ArrayList<Vertex>();
188 frags.add(fragA);
189 frags.add(fragB);
190 try
191 {
192 DenoptimIO.writeVertexesToSDF(new File(fileName), frags, true);
193 } catch (DENOPTIMException e1)
194 {
195 System.err.println("WARNING: could not write to '"
196 + fileName + "'. Reporting fragments to STDERR:."
197 + System.getProperty("line.separator")
198 + fragA.toJson()
199 + System.getProperty("line.separator")
200 + fragB.toJson());
201 }
202 }
203 fut.cancel(true);
204 } catch (final Exception e) {
205 throw new RuntimeException(e);
206 } finally {
207 service.shutdown();
208 }
209 return mapping;
210 }
211
212//------------------------------------------------------------------------------
213
214}
String label
Bond order or name used to identify the edge type.
String label
Elemental symbol or name used to identify the node content.
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:1188
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, int timeout)
Constructs an inspector with a custom timeout limiet.
boolean isomorphismExists()
Checks if an isomorphism exists between the two fragments.
VF2GraphIsomorphismInspector< FragIsomorphNode, FragIsomorphEdge > vf2
Implementation of the Vento-Foggia 2 algorithm.
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.