View Javadoc

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   * This class is used to log translation activities during the parse process.
13   *
14   * @author Michael Darsow
15   * @version 1.0 - 08-Apr-2003
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     * The file name will be the EC number (1_1_1_1.log).
30     */
31    private File outputFile;
32  
33    /**
34     * For each entry a log file is generated.
35     */
36    private FileWriter out;
37  
38    /**
39     * Initialises the log file of the given EC.
40     *
41     * @param ec Used for the file name.
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     * Enables a logger which stores all results in one file.
61     *
62     * @param dir             Directory of the log file.
63     * @param type            Type of comparison (see static fields).
64     * @param enzymeLogFormat
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 &rarr; 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 &rarr; 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 &rarr; ENZYME</b></td></tr>\n");
143           break;
144       }
145     } catch (IOException e) {
146       e.printStackTrace();
147     }
148   }
149 
150 //  /**
151 //   * Adds information to the log file.
152 //   *
153 //   * @param before Not translated string.
154 //   * @param after Translated string.
155 //   */
156 //  public void add(String before, String after) {
157 //    try {
158 //      out.write(before + " --> " + after + "\n");
159 //    } catch (IOException e) {
160 //      e.printStackTrace();
161 //    }
162 //  }
163 
164   /**
165    * Adds information to the log file.
166    *
167    * @param before Not translated string.
168    * @param after  Translated string.
169    * @param ec     The EC number.
170    */
171   public void add(String before, String after, String ec) {
172     if (!enzymeLogFormat) {
173       before = encodeTags(before);
174       after = encodeTags(after);
175     }
176 //    else {
177 //      before = removeTags(before);
178 //      after = removeTags(after);
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 //        System.out.println("case1");
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 //        System.out.println("case2");
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 //        System.out.println("case3");
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 //        System.out.println("case4");
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 //   * Adds information to the log file.
299 //   *
300 //   * @param before Not translated <code>Vector</code> of <code>String</code>s.
301 //   * @param after Translated <code>Vector</code> of <code>String</code>s.
302 //   * @param type Line type.
303 //   */
304 //  public void add(Vector before, Vector after, String type) {
305 //    try {
306 //      out.write(type + ":\n");
307 //      for (int iii = 0; iii < before.size(); iii++) {
308 //        String beforeString = (String) before.elementAt(iii);
309 //        String afterString = (String) after.elementAt(iii);
310 //        out.write(beforeString + " --> " + afterString + "\n");
311 //      }
312 //      out.write("\n");
313 //    } catch (IOException e) {
314 //      e.printStackTrace();
315 //    }
316 //  }
317 
318 //  /**
319 //   * The disease line is a bit different from the other lines (see above).
320 //   *
321 //   * @param before Not translated <code>Vector</code> of <code>String</code>s.
322 //   * @param after Translated <code>Vector</code> of <code>String</code>s.
323 //   */
324 //  public void addDisease(Vector before, Vector after) {
325 //    try {
326 //      out.write("DI:\n");
327 //      for (int iii = 0; iii < before.size(); iii++) {
328 //        Vector beforeVector = (Vector) before.elementAt(iii);
329 //        Vector afterVector = (Vector) after.elementAt(iii);
330 //        String beforeName = (String) beforeVector.get(0);
331 //        String beforeNumber = (String) beforeVector.get(1);
332 //        String afterName = (String) afterVector.get(0);
333 //        String afterNumber = (String) afterVector.get(1);
334 //        out.write(beforeName + " - " + beforeNumber + " --> " + afterName + " - " + afterNumber + "\n");
335 //      }
336 //      out.write("\n");
337 //    } catch (IOException e) {
338 //      e.printStackTrace();
339 //    }
340 //  }
341 
342 //  /**
343 //   * Comparator output is logged here.
344 //   *
345 //   * @param lineType Line type which has been compared with the IUBMB data set.
346 //   * @param result Result message of comparison.
347 //   */
348 //  public void addComparison(String lineType, String result) {
349 //    try {
350 //      out.write("Compared line type:\t" + lineType + "\n");
351 //      out.write("Result of comparison: " + result);
352 //      out.write("\n");
353 //    } catch (IOException e) {
354 //      e.printStackTrace();
355 //    }
356 //  }
357 
358   /**
359    * Must be called to close the log file.
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("&lt;");
374 
375     p = Pattern.compile("\\>");
376     m = p.matcher(text);
377     text = m.replaceAll("&gt;");
378 
379     return text;
380   }
381 
382   private int getDifferenceStartIndex(String before, String after) {
383     // Todo: include this rule into rule set in SIBAndIUBMBComparator!
384 //		if (before.length() > 2)
385 //			before = before.substring(0, 1).toUpperCase() + before.substring(1);
386 //		if (after.length() > 2)
387 //			after = after.substring(0, 1).toUpperCase() + after.substring(1);
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 }