1 package uk.ac.ebi.intenz.tools.sib.writer;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.StringWriter;
8 import java.io.Writer;
9 import java.text.SimpleDateFormat;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.Date;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Properties;
20 import org.apache.log4j.Logger;
21
22 import uk.ac.ebi.biobabel.validator.DbIdentifierValidator;
23 import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeCrossReference;
24 import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeEntryImpl;
25 import uk.ac.ebi.interfaces.sptr.SPTRCrossReference;
26 import uk.ac.ebi.interfaces.sptr.SPTRException;
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 public class EnzymeFlatFileWriter {
99
100 private static final Logger LOGGER =
101 Logger.getLogger(EnzymeFlatFileWriter.class.getName());
102
103
104
105
106
107 public static final Properties DE_MODIFIED_ENTRIES = new Properties();
108
109 static {
110 InputStream stream;
111 stream = EnzymeFlatFileWriter.class.getClassLoader().getResourceAsStream("DE_line_modifications.properties");
112 try {
113 DE_MODIFIED_ENTRIES.load(stream);
114 } catch (IOException e) {
115 LOGGER.error(e.getMessage(), e);
116 } finally {
117 try {
118 if (stream != null) stream.close();
119 } catch (IOException e){}
120 }
121 }
122
123
124
125
126 private static String header = "";
127
128
129
130
131 private EnzymeFlatFileWriter() {
132 }
133
134
135
136
137
138
139
140 public static String export(EnzymeEntryImpl enzyme) throws SPTRException {
141 if (enzyme == null) throw new NullPointerException("Parameter 'enzyme' must not be null.");
142 StringWriter sw = new StringWriter();
143 try {
144 export(enzyme, sw);
145 } catch (IOException e) {
146 throw new EnzymeFlatFileWriteException(e);
147 }
148 return sw.toString();
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163 public static long export(Collection enzymes, String version, File outputFile) throws SPTRException {
164 if (enzymes == null) throw new NullPointerException("Parameter enzymes == null not allowed here.");
165 if (version == null) throw new NullPointerException("Parameter version == null not allowed here.");
166
167
168
169 if (outputFile == null) throw new NullPointerException("Parameter outputFile == null not allowed here.");
170
171 FileWriter fileWriter = null;
172 long start = System.currentTimeMillis();
173
174 try {
175 fileWriter = new FileWriter(outputFile);
176 fileWriter.write(getHeader(version));
177
178 Iterator enzymeIterator = enzymes.iterator();
179
180 while (enzymeIterator.hasNext()) {
181 EnzymeEntryImpl enzymeEntry = (EnzymeEntryImpl) enzymeIterator.next();
182 export(enzymeEntry, fileWriter);
183 }
184 } catch (UnsupportedOperationException e) {
185 throw new EnzymeFlatFileWriteException(e);
186 } catch (IOException e) {
187 throw new EnzymeFlatFileWriteException(e);
188 } finally {
189 try {
190 if (fileWriter != null) fileWriter.close();
191 } catch (IOException e) {
192 throw new EnzymeFlatFileWriteException(e);
193 }
194 }
195
196 return System.currentTimeMillis() - start;
197 }
198
199
200
201
202
203
204
205 public static void export(EnzymeEntryImpl enzymeEntry, Writer writer)
206 throws SPTRException, IOException {
207 Map enzymeCrossReferences = getGroupedEnzymeCrossReferences(enzymeEntry.getCrossReferences());
208
209 writer.write(createIDLine(enzymeEntry.getEC()));
210 if (!enzymeEntry.isTransferredOrDeleted()) {
211
212 writer.write(createDELine(enzymeEntry.getCommonName(), true));
213
214 writer.write(createANLines(enzymeEntry.getSynonyms(), true));
215
216 writer.write(createCALines(enzymeEntry.getReactions(), true));
217
218 writer.write(createCFLines(enzymeEntry.getCofactors(), true));
219
220 writer.write(createCCLines(enzymeEntry.getComment(), true));
221
222 writer.write(createDILines((ArrayList) enzymeCrossReferences.get(EnzymeCrossReference.MIM)));
223
224 writer.write(createPRLines((ArrayList) enzymeCrossReferences.get(EnzymeCrossReference.PROSITE)));
225
226 writer.write(createDRLines((ArrayList) enzymeCrossReferences.get(EnzymeCrossReference.SWISSPROT)));
227 } else {
228
229
230
231
232 String deLine = DE_MODIFIED_ENTRIES.containsKey(enzymeEntry.getEC())?
233 DE_MODIFIED_ENTRIES.getProperty(enzymeEntry.getEC()):
234 enzymeEntry.getCommonName();
235 writer.write(createDELine(deLine, true));
236 }
237 writer.write("//\n");
238 writer.flush();
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 public static long export(Collection enzymes, String version, String dir, String fileName) throws SPTRException {
258 if (enzymes == null) throw new NullPointerException("Parameter enzymes == null not allowed here.");
259 if (version == null) throw new NullPointerException("Parameter version == null not allowed here.");
260
261
262
263
264 StringBuilder pathname = new StringBuilder();
265 if (dir != null && !dir.equals("")) {
266 File dirTest = new File(dir);
267 if (!dirTest.isDirectory()) throw new IllegalArgumentException("Parameter dir is not a valid pathname");
268 pathname.append(dir);
269 if (!dir.endsWith("/") && !dir.endsWith("\\")) pathname.append(File.separator);
270 }
271 if (fileName == null || fileName.equals(""))
272 pathname.append("enzyme.dat");
273 else
274 pathname.append(fileName);
275 File outputFile = new File(pathname.toString());
276
277 return export(enzymes, version, outputFile);
278 }
279
280
281
282
283
284
285
286
287
288
289 private static String[] sortLines (String[] synonyms) throws SPTRException {
290 List synonymListToSort = Arrays.asList(synonyms);
291 Collections.sort(synonymListToSort);
292 return (String[]) synonymListToSort.toArray();
293 }
294
295
296
297
298
299
300
301
302
303
304
305 private static String createIDLine(String ec) throws SPTRException {
306 assert ec != null : ec;
307 if (!DbIdentifierValidator.getInstance().validate(ec, DbIdentifierValidator.EC_NUMBER))
308 throw new EnzymeFlatFileWriteException("Parameter ec does not contain a valid ec.");
309
310 LOGGER.debug("EC: " + ec);
311
312 StringBuilder IDContent = new StringBuilder();
313 IDContent.append("ID ");
314 IDContent.append(ec);
315 IDContent.append("\n");
316
317 return IDContent.toString();
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332 private static String createDELine(String commonName, boolean wrapLines) throws SPTRException {
333 assert commonName != null : commonName;
334
335
336
337 StringBuilder DEContent = new StringBuilder();
338 if (wrapLines) commonName = insertLineBreaks(commonName, LineType.DE);
339 DEContent.append(commonName);
340 DEContent.append("\n");
341
342 return DEContent.toString();
343 }
344
345
346
347
348
349
350
351
352
353
354
355 private static String createANLines(String[] synonyms, boolean wrapLines) throws SPTRException {
356 assert synonyms != null : synonyms;
357
358 StringBuilder ANContent = new StringBuilder();
359 for (int iii = 0; iii < synonyms.length; iii++) {
360 String synonym = synonyms[iii];
361 if (synonym.equals("") || synonym.equals("-")) continue;
362 if (wrapLines) synonym = insertLineBreaks(synonyms[iii], LineType.AN);
363 ANContent.append(synonym);
364 ANContent.append("\n");
365 }
366
367 return ANContent.toString();
368 }
369
370
371
372
373
374
375
376
377
378 private static String createCALines(String[] reactions, boolean wrapLines) throws SPTRException {
379 assert reactions != null : reactions;
380
381 StringBuilder CAContent = new StringBuilder();
382 for (int iii = 0; iii < reactions.length; iii++) {
383 if (reactions[iii].equals("")) continue;
384
385 StringBuffer reaction = null;
386 if (reactions[iii].charAt(reactions[iii].length() - 1) == '.')
387 reaction = new StringBuffer(reactions[iii].substring(0, reactions[iii].length() - 1));
388 else
389 reaction = new StringBuffer(reactions[iii]);
390 if (reactions.length > 1) {
391 StringBuilder number = new StringBuilder("(");
392 number.append(iii + 1);
393 number.append(") ");
394 reaction.insert(0, number.toString());
395 }
396 if (wrapLines)
397 CAContent.append(insertLineBreaks(reaction.toString(), LineType.CA));
398 else
399 CAContent.append(reaction.toString());
400 CAContent.append("\n");
401 }
402
403 return CAContent.toString();
404 }
405
406
407
408
409
410
411
412
413
414 private static String createCFLines(String cofactors, boolean wrapLines) throws SPTRException {
415 assert cofactors != null : cofactors;
416
417 if (cofactors.equals("")) return cofactors;
418
419 StringBuilder CFContent = new StringBuilder();
420 if (wrapLines) cofactors = insertLineBreaks(cofactors, LineType.CF);
421 CFContent.append(cofactors);
422 CFContent.append("\n");
423
424 return CFContent.toString();
425 }
426
427
428
429
430
431
432
433
434
435
436
437 private static String createCCLines(String comment, boolean wrapLines) throws SPTRException {
438 assert comment != null : comment;
439 if (comment.equals("")) return comment;
440 if (wrapLines) comment = insertLineBreaks(comment, LineType.CC);
441 return comment;
442 }
443
444
445
446
447
448
449
450 private static String createDILines(ArrayList DICrossReferences) {
451 assert DICrossReferences != null : DICrossReferences;
452
453 StringBuilder DIContent = new StringBuilder();
454 for (int iii = 0; iii < DICrossReferences.size(); iii++) {
455 EnzymeCrossReference enzymeCrossReference = (EnzymeCrossReference) DICrossReferences.get(iii);
456 DIContent.append("DI ");
457 DIContent.append(enzymeCrossReference.getPropertyValue(EnzymeCrossReference.PROPERTY_DESCRIPTION));
458 DIContent.append("; MIM:");
459 DIContent.append(enzymeCrossReference.getAccessionNumber());
460 DIContent.append(".\n");
461 }
462
463 return DIContent.toString();
464 }
465
466
467
468
469
470
471
472 private static String createPRLines(ArrayList PRCrossReferences) {
473 assert PRCrossReferences != null : PRCrossReferences;
474
475 StringBuilder PRContent = new StringBuilder();
476 for (int iii = 0; iii < PRCrossReferences.size(); iii++) {
477 EnzymeCrossReference enzymeCrossReference = (EnzymeCrossReference) PRCrossReferences.get(iii);
478 PRContent.append("PR ");
479 PRContent.append(enzymeCrossReference.getDatabaseName());
480 PRContent.append("; ");
481 PRContent.append(enzymeCrossReference.getAccessionNumber());
482 PRContent.append(";\n");
483 }
484
485 return PRContent.toString();
486 }
487
488
489
490
491
492
493
494
495
496 private static String createDRLines(ArrayList DRCrossReferences) {
497 assert DRCrossReferences != null : DRCrossReferences;
498
499 StringBuilder DRContent = new StringBuilder();
500 int count = 0;
501 for (int iii = 0; iii < DRCrossReferences.size(); iii++) {
502 EnzymeCrossReference enzymeCrossReference = (EnzymeCrossReference) DRCrossReferences.get(iii);
503 if (count == 0){
504 DRContent.append("DR ");
505 }
506
507 DRContent.append(getPaddedString(
508 enzymeCrossReference.getAccessionNumber(), 10));
509 DRContent.append(", ");
510 DRContent.append(getPaddedString(enzymeCrossReference.getPropertyValue(
511 EnzymeCrossReference.PROPERTY_DESCRIPTION), 11));
512 DRContent.append("; ");
513 if (count == 2) {
514 count = 0;
515 DRContent.delete(DRContent.length() - 2, DRContent.length());
516 DRContent.append("\n");
517 continue;
518 }
519
520 count++;
521 }
522
523 if (DRContent.length() > 1) {
524 if (DRContent.charAt(DRContent.length() - 1) == ' ')
525 DRContent.delete(DRContent.length() - 2, DRContent.length());
526 if (count > 0 && count < 3)
527 DRContent.append("\n");
528 }
529
530 return DRContent.toString();
531 }
532
533
534
535
536
537
538
539
540
541
542 private static Map getGroupedEnzymeCrossReferences(SPTRCrossReference[] allCrossReferences) {
543 Map groupedCrossReferences = new HashMap();
544
545 ArrayList DICrossReferences = new ArrayList();
546 ArrayList PRCrossReferences = new ArrayList();
547 ArrayList DRCrossReferences = new ArrayList();
548
549 for (int iii = 0; iii < allCrossReferences.length; iii++) {
550 EnzymeCrossReference crossReference = (EnzymeCrossReference) allCrossReferences[iii];
551 if (crossReference.getDatabaseName().equals(EnzymeCrossReference.MIM)) {
552 DICrossReferences.add(crossReference);
553 }
554 if (crossReference.getDatabaseName().equals(EnzymeCrossReference.PROSITE)) {
555 PRCrossReferences.add(crossReference);
556 }
557 if (crossReference.getDatabaseName().equals(EnzymeCrossReference.SWISSPROT)) {
558 DRCrossReferences.add(crossReference);
559 }
560 }
561
562 groupedCrossReferences.put(EnzymeCrossReference.MIM, DICrossReferences);
563 groupedCrossReferences.put(EnzymeCrossReference.PROSITE, PRCrossReferences);
564 groupedCrossReferences.put(EnzymeCrossReference.SWISSPROT, DRCrossReferences);
565
566 return groupedCrossReferences;
567 }
568
569
570
571
572
573
574
575 private static String getPaddedString(String s, int length){
576 if (s.length() == length) return s;
577 StringBuilder sb = new StringBuilder(s);
578 for (int i = s.length(); i < length; i++){
579 sb.append(' ');
580 }
581 return sb.toString();
582 }
583
584
585
586
587
588
589
590
591 private static String getHeader(String version) {
592 assert version != null : version;
593
594 if (header.equals("")) {
595 Date today = new Date();
596 SimpleDateFormat formatter = new SimpleDateFormat("d-MMM-yyyy");
597
598 StringBuffer enzymeHeader = new StringBuffer()
599 .append("CC -----------------------------------------------------------------------\n")
600 .append("CC\n")
601 .append("CC ENZYME nomenclature database\n")
602 .append("CC\n")
603 .append("CC -----------------------------------------------------------------------\n")
604 .append("CC Release of ")
605 .append(formatter.format(today))
606 .append("\n")
607 .append("CC -----------------------------------------------------------------------\n")
608 .append("CC\n")
609 .append("CC Ioannis Xenarios and Kristian Axelsen\n")
610 .append("CC SIB Swiss Institute of Bioinformatics\n")
611 .append("CC Centre Medical Universitaire (CMU)\n")
612 .append("CC 1, rue Michel Servet\n")
613 .append("CC 1211 Geneva 4\n")
614 .append("CC Switzerland\n")
615 .append("CC\n")
616 .append("CC Email: enzyme@isb-sib.ch\n")
617 .append("CC\n")
618 .append("CC WWW server: http://enzyme.expasy.org/\n")
619 .append("CC\n")
620 .append("CC -----------------------------------------------------------------------\n")
621 .append("CC Copyrighted by the SIB Swiss Institute of Bioinformatics. \n")
622 .append("CC There are no restrictions on its use by any institutions as long as\n")
623 .append("CC its content is in no way modified.\n")
624 .append("CC -----------------------------------------------------------------------\n")
625 .append("//\n");
626 header = enzymeHeader.toString();
627 }
628
629 return header;
630 }
631
632
633
634
635
636
637
638
639
640
641
642 public static String insertLineBreaks(String nonWrappedText, LineType lineType) throws SPTRException {
643 if (nonWrappedText == null) throw new NullPointerException("Parameter nonWrappedText == null not allowed here.");
644 if (lineType == null) throw new NullPointerException("Parameter lineType == null not allowed here.");
645
646 LineFormatter lineWrapper;
647
648
649 if (lineType == LineType.CC) {
650 lineWrapper = new CC_LineFormatter();
651 } else {
652 lineWrapper = new DefaultLineFormatter();
653 }
654
655 return lineWrapper.formatLines(nonWrappedText, lineType);
656 }
657 }