1 package uk.ac.ebi.intenz.tools.sib.translator;
2
3 import java.util.regex.Pattern;
4 import java.util.regex.Matcher;
5 import java.util.Vector;
6 import java.io.File;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.sql.Date;
10
11
12
13
14
15
16
17 public class SIBEnzymeLogger {
18 public static final int COMMON_NAMES = 0;
19
20 public static final int REACTIONS = 1;
21
22 public static final int OTHER_NAMES = 2;
23
24 public static final int COMMENTS = 3;
25
26 private boolean enzymeLogFormat;
27
28
29
30
31 private File outputFile;
32
33
34
35
36 private FileWriter out;
37
38
39
40
41
42
43 public SIBEnzymeLogger(String dir, String ec) {
44 enzymeLogFormat = false;
45 String ecOrig = ec;
46 Pattern p = Pattern.compile("\\.");
47 Matcher m = p.matcher(ec);
48 ec = m.replaceAll("_");
49
50 try {
51 outputFile = new File(dir + ec + ".log");
52 out = new FileWriter(outputFile);
53 out.write("EC " + ecOrig + "\n");
54 } catch (IOException e) {
55 e.printStackTrace();
56 }
57 }
58
59
60
61
62
63
64
65
66 public SIBEnzymeLogger(String dir, int type, boolean enzymeLogFormat) {
67 this.enzymeLogFormat = enzymeLogFormat;
68 if (dir.charAt(dir.length() - 1) != '/') dir += "/";
69 if (enzymeLogFormat)
70 writeEnzymeLog(type, dir);
71 else
72 writeHtmlLog(type, dir);
73 }
74
75 private void writeEnzymeLog(int type, String dir) {
76 Date today = new Date(System.currentTimeMillis());
77 try {
78 switch (type) {
79 case 0:
80 outputFile = new File(dir + "common_names_clashes.log");
81 out = new FileWriter(outputFile);
82 out.write("Common name clashes - generated on ");
83 out.write(today.toString());
84 break;
85 case 1:
86 outputFile = new File(dir + "reaction_clashes.log");
87 out = new FileWriter(outputFile);
88 out.write("Reaction clashes - generated on ");
89 out.write(today.toString());
90 break;
91 case 2:
92 outputFile = new File(dir + "other_names.log");
93 out = new FileWriter(outputFile);
94 out.write("Synonyms clashes - generated on ");
95 out.write(today.toString());
96 break;
97 case 3:
98 outputFile = new File(dir + "comments.log");
99 out = new FileWriter(outputFile);
100 out.write("Comments clashes - generated on ");
101 out.write(today.toString());
102 break;
103 }
104 } catch (IOException e) {
105 e.printStackTrace();
106 }
107 }
108
109 private void writeHtmlLog(int type, String dir) {
110 try {
111 switch (type) {
112 case 0:
113 outputFile = new File(dir + "common_names_log.html");
114 out = new FileWriter(outputFile);
115 out.write("<html><head><title>Common name clashes</title></title></head><body>\n");
116 out.write("<center><h2>Common names clashes</h2></center>\n");
117 out.write("<table width=\"100%\" border=\"0\">\n");
118 out.write("<tr><td align=\"center\"><b>EC</b></td><td align=\"center\"><b>IUBMB → ENZYME</b></td></tr>\n");
119 break;
120 case 1:
121 outputFile = new File(dir + "reactions_log.html");
122 out = new FileWriter(outputFile);
123 out.write("<html><head><title>Reaction clashes</title></title></head><body>\n");
124 out.write("<center><h2>Reaction clashes</h2></center>\n");
125 out.write("<table width=\"100%\" border=\"0\">\n");
126 out.write("<tr><td align=\"center\"><b>EC</b></td><td align=\"center\"><b>IUBMB → ENZYME</b></td></tr>\n");
127 break;
128 case 2:
129 outputFile = new File(dir + "other_names_log.html");
130 out = new FileWriter(outputFile);
131 out.write("<html><head><title>Other names clashes</title></title></head><body>\n");
132 out.write("<center><h2>Other names clashes</h2></center>\n");
133 out.write("<table width=\"100%\" border=\"0\">\n");
134 out.write("<tr><td align=\"center\"><b>EC</b></td><td align=\"center\"><b>Smaller set</b><td align=\"left\"><b>Names not matching\nin smaller set</b></td></tr>\n");
135 break;
136 case 3:
137 outputFile = new File(dir + "comments_log.html");
138 out = new FileWriter(outputFile);
139 out.write("<html><head><title>Comments clashes</title></title></head><body>\n");
140 out.write("<center><h2>Comments clashes</h2></center>\n");
141 out.write("<table width=\"100%\" border=\"0\">\n");
142 out.write("<tr><td align=\"center\"><b>EC</b></td><td align=\"center\"><b>IUBMB → ENZYME</b></td></tr>\n");
143 break;
144 }
145 } catch (IOException e) {
146 e.printStackTrace();
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 public void add(String before, String after, String ec) {
172 if (!enzymeLogFormat) {
173 before = encodeTags(before);
174 after = encodeTags(after);
175 }
176
177
178
179
180 if (before.length() > 2)
181 before = before.substring(0, 1).toUpperCase() + before.substring(1);
182 if (after.length() > 2)
183 after = after.substring(0, 1).toUpperCase() + after.substring(1);
184 int diffStartIndex = getDifferenceStartIndex(before, after);
185 try {
186 if (diffStartIndex + 1 < before.length() && diffStartIndex + 1 < after.length()) {
187
188 int beforeEnd = 1;
189 int afterEnd = 1;
190 if (before.charAt(diffStartIndex) == '&') beforeEnd = 4;
191 if (after.charAt(diffStartIndex) == '&') afterEnd = 4;
192 if (enzymeLogFormat) {
193 out.write("\nAC:\n");
194 out.write(ec);
195 out.write("\nFROM:\n");
196 out.write(after.substring(0, diffStartIndex));
197 out.write(after.substring(diffStartIndex, diffStartIndex + afterEnd));
198 out.write(after.substring(diffStartIndex + afterEnd));
199 out.write(".\nTO:\n");
200 out.write(before.substring(0, diffStartIndex));
201 out.write(before.substring(diffStartIndex, diffStartIndex + beforeEnd));
202 out.write(before.substring(diffStartIndex + beforeEnd));
203 out.write(".\n//");
204 } else {
205 out.write("<tr><td valign=\"top\"><font color=\"blue\"><a href=\"http://www.ebi.ac.uk/~mdarsow/cgi-bin/development/test/intenz_curator/handler?cmd=SearchAllEC&ec=" + ec + "\">" + ec + "</a></font></td><td>" + before.substring(0, diffStartIndex) +
206 "<font color=\"red\">" + before.substring(diffStartIndex, diffStartIndex + beforeEnd) + "</font>" + before.substring(diffStartIndex + beforeEnd) +
207 "<br/>" + after.substring(0, diffStartIndex) +
208 "<font color=\"red\">" + after.substring(diffStartIndex, diffStartIndex + afterEnd) + "</font>" + after.substring(diffStartIndex + afterEnd) + "</td></tr>\n");
209 }
210 }
211 if (diffStartIndex + 1 >= before.length() && diffStartIndex + 1 < after.length()) {
212
213 int afterEnd = 1;
214 if (after.charAt(diffStartIndex) == '&') afterEnd = 4;
215 if (enzymeLogFormat) {
216 out.write("\nAC:\n");
217 out.write(ec);
218 out.write("\nFROM:\n");
219 out.write(after.substring(0, diffStartIndex));
220 out.write(after.substring(diffStartIndex, diffStartIndex + afterEnd));
221 out.write(after.substring(diffStartIndex + afterEnd));
222 out.write(".\nTO:\n");
223 out.write(before.substring(0, diffStartIndex));
224 out.write(before.substring(diffStartIndex));
225 out.write(".\n//");
226 } else {
227 out.write("<tr><td valign=\"top\"><font color=\"blue\"><a href=\"http://www.ebi.ac.uk/~mdarsow/cgi-bin/development/test/intenz_curator/handler?cmd=SearchAllEC&ec=" + ec + "\">" + ec + "</a></font></td><td>" + before.substring(0, diffStartIndex) +
228 "<font color=\"red\">" + before.substring(diffStartIndex) + "</font><br/>" + after.substring(0, diffStartIndex) +
229 "<font color=\"red\">" + after.substring(diffStartIndex, diffStartIndex + afterEnd) + "</font>" + after.substring(diffStartIndex + afterEnd) + "</td></tr>\n");
230 }
231 }
232 if (diffStartIndex + 1 < before.length() && diffStartIndex + 1 >= after.length()) {
233
234 int beforeEnd = 1;
235 if (before.charAt(diffStartIndex) == '&') beforeEnd = 4;
236 if (enzymeLogFormat) {
237 out.write("\nAC:\n");
238 out.write(ec);
239 out.write("\nFROM:\n");
240 out.write(after.substring(0, diffStartIndex));
241 out.write(after.substring(diffStartIndex));
242 out.write(".\nTO:\n");
243 out.write(before.substring(0, diffStartIndex));
244 out.write(before.substring(diffStartIndex, diffStartIndex + beforeEnd));
245 out.write(before.substring(diffStartIndex + beforeEnd));
246 out.write(".\n//");
247 } else {
248 out.write("<tr><td valign=\"top\"><font color=\"blue\"><a href=\"http://www.ebi.ac.uk/~mdarsow/cgi-bin/development/test/intenz_curator/handler?cmd=SearchAllEC&ec=" + ec + "\">" + ec + "</a></font></td><td>" + before.substring(0, diffStartIndex) +
249 "<font color=\"red\">" + before.substring(diffStartIndex, diffStartIndex + beforeEnd) + "</font>" + before.substring(diffStartIndex + beforeEnd) +
250 "<br/>" + after.substring(0, diffStartIndex) +
251 "<font color=\"red\">" + after.substring(diffStartIndex) + "</font></td></tr>\n");
252 }
253 }
254 if (diffStartIndex + 1 >= before.length() && diffStartIndex + 1 >= after.length()) {
255
256 if (enzymeLogFormat) {
257 out.write("\nAC:\n");
258 out.write(ec);
259 out.write("\nFROM:\n");
260 out.write(after.substring(0, diffStartIndex));
261 out.write(after.substring(diffStartIndex));
262 out.write(".\nTO:\n");
263 out.write(before.substring(0, diffStartIndex));
264 out.write(before.substring(diffStartIndex));
265 out.write(".\n//");
266 } else {
267 out.write("<tr><td valign=\"top\"><font color=\"blue\"><a href=\"http://www.ebi.ac.uk/~mdarsow/cgi-bin/development/test/intenz_curator/handler?cmd=SearchAllEC&ec=" + ec + "\">" + ec + "</a></font></td><td>" + before.substring(0, diffStartIndex) +
268 "<font color=\"red\">" + before.substring(diffStartIndex) + "</font><br/>" + after.substring(0, diffStartIndex) +
269 "<font color=\"red\">" + after.substring(diffStartIndex) + "</font></td></tr>\n");
270 }
271 }
272 } catch (IOException e) {
273 e.printStackTrace();
274 }
275 }
276
277 private String removeTags(String text) {
278 return text.replaceAll("\\<.+?\\>", "");
279 }
280
281 public void addOtherNameLog(String nameNotMatching, String set, String ec) {
282 nameNotMatching = encodeTags(nameNotMatching);
283 System.out.println("nameNotMatching = " + nameNotMatching);
284 System.out.println("set = " + set);
285 System.out.println("ec = " + ec);
286 try {
287 if (enzymeLogFormat) {
288
289 } else
290 out.write("<tr><td valign=\"top\"><font color=\"blue\"><a href=\"http://www.ebi.ac.uk/~mdarsow/cgi-bin/development/test/intenz_curator/handler?cmd=SearchAllEC&ec=" + ec + "\">" + ec + "</a></font></td>" +
291 "<td>" + set + "</td><td>" + nameNotMatching + "</td></tr>");
292 } catch (IOException e) {
293 e.printStackTrace();
294 }
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 public void close() {
362 try {
363 if(!enzymeLogFormat) out.write("</table></body></html>");
364 out.close();
365 } catch (IOException e) {
366 e.printStackTrace();
367 }
368 }
369
370 private String encodeTags(String text) {
371 Pattern p = Pattern.compile("\\<");
372 Matcher m = p.matcher(text);
373 text = m.replaceAll("<");
374
375 p = Pattern.compile("\\>");
376 m = p.matcher(text);
377 text = m.replaceAll(">");
378
379 return text;
380 }
381
382 private int getDifferenceStartIndex(String before, String after) {
383
384
385
386
387
388 char[] beforeArray = before.toCharArray();
389 char[] afterArray = after.toCharArray();
390 int iii = 0;
391 for (; iii < beforeArray.length; iii++) {
392 char characterBefore = ' ', characterAfter = ' ';
393 if (iii < beforeArray.length)
394 characterBefore = beforeArray[iii];
395 else
396 return 0;
397 if (iii < afterArray.length)
398 characterAfter = afterArray[iii];
399 else
400 return 0;
401 if (characterBefore != characterAfter) return iii;
402 }
403
404 return iii;
405 }
406 }