View Javadoc

1   package uk.ac.ebi.intenz.tools.release.helper;
2   
3   import java.util.Iterator;
4   import java.util.List;
5   import java.util.Set;
6   import java.util.SortedSet;
7   
8   import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
9   import uk.ac.ebi.intenz.domain.enzyme.EnzymeComment;
10  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
11  import uk.ac.ebi.intenz.domain.enzyme.EnzymeLink;
12  import uk.ac.ebi.intenz.domain.enzyme.EnzymeName;
13  import uk.ac.ebi.intenz.domain.history.HistoryGraph;
14  import uk.ac.ebi.intenz.domain.reference.Reference;
15  import uk.ac.ebi.rhea.domain.Reaction;
16  import uk.ac.ebi.xchars.SpecialCharacters;
17  import uk.ac.ebi.xchars.domain.EncodingType;
18  
19  /**
20   * This class provides methods for HTML and XML export of enzyme data.
21   * @deprecated
22   * @author Michael Darsow
23   * @version $Revision: 1.1 $ $Date: 2008/02/21 12:44:56 $
24   */
25  public class EnzymeEntryHelper {
26  
27     public static String toXML (EnzymeEntry enzymeEntry, SpecialCharacters encoding, EnzymeViewConstant view,
28                                 boolean intenzTextXML) {
29        if ( enzymeEntry.getHistory().isDeletedRootNode() || enzymeEntry.getHistory().isTransferredRootNode() ) {
30           return intenzTextXML ?
31          		 deletedEntryToIntEnzTextXML(enzymeEntry, encoding) : "";
32        } else {
33           return intenzTextXML ?
34          		 entryToIntEnzTextXML(enzymeEntry, encoding, view) : "";
35        }
36     }
37  
38     private static String deletedEntryToIntEnzTextXML (EnzymeEntry enzymeEntry, SpecialCharacters encoding) {
39        StringBuffer xmlStringBuffer = new StringBuffer();
40        xmlStringBuffer.append("<enzyme>");
41        xmlStringBuffer.append("<ec>");
42        xmlStringBuffer.append(enzymeEntry.getEc().toString());
43        xmlStringBuffer.append("</ec>");
44        xmlStringBuffer.append("<active>");
45  //      xmlStringBuffer.append(enzymeEntry.isActive());
46        xmlStringBuffer.append("</active>");
47        xmlStringBuffer.append("<deleted_text>");
48        final HistoryGraph history = enzymeEntry.getHistory();
49        if ( history.isDeletedRootNode() )
50           xmlStringBuffer.append("Deleted entry: ");
51        if ( history.isTransferredRootNode() ) {
52           xmlStringBuffer.append("Transferred entry: ");
53           if ( !history.getLatestHistoryEventOfAll().getNote().equals("") ) {
54              xmlStringBuffer.append(" ");
55              xmlStringBuffer.append(removeFormatting(encoding.xml2Display(
56                    history.getLatestHistoryEventOfAll().getNote(),
57                    EncodingType.SWISSPROT_CODE)));
58              xmlStringBuffer.append(
59                    removeFormatting(encoding.xml2Display(history.getLatestHistoryEventOfAll().getNote())));
60           }
61           // Add EC number of new enzyme.
62           xmlStringBuffer.append(
63                 enzymeEntry.getHistory().getLatestHistoryEventOfRoot().getAfterNode().getEnzymeEntry().getEc().toString());
64        }
65        xmlStringBuffer.append("</deleted_text>");
66  
67        // Get IntEnz common name. If no IntEnz common name exists then use NC-IUBMB common name.
68        xmlStringBuffer.append("<common_names>");
69        String commonName = enzymeEntry.getCommonName(EnzymeViewConstant.INTENZ).getName();
70        if ( commonName == null ) commonName = enzymeEntry.getCommonName(EnzymeViewConstant.IUBMB).getName();
71        xmlStringBuffer.append("<common_name>");
72        xmlStringBuffer.append(removeFormatting(encoding.xml2Display(commonName)));
73        xmlStringBuffer.append("</common_name>");
74        xmlStringBuffer.append("<common_name>");
75        xmlStringBuffer.append(removeFormatting(encoding.xml2Display(commonName, EncodingType.SWISSPROT_CODE)));
76        xmlStringBuffer.append("</common_name>");
77        xmlStringBuffer.append("</common_names>");
78  
79        // All other fields are empty.
80        xmlStringBuffer.append("<description>");
81        xmlStringBuffer.append("</description>");
82        xmlStringBuffer.append("<reactions>");
83        xmlStringBuffer.append("<reaction>");
84        xmlStringBuffer.append("</reaction>");
85        xmlStringBuffer.append("</reactions>");
86        xmlStringBuffer.append("<cofactors>");
87        xmlStringBuffer.append("<cofactor>");
88        xmlStringBuffer.append("</cofactor>");
89        xmlStringBuffer.append("</cofactors>");
90        xmlStringBuffer.append("<synonyms>");
91        xmlStringBuffer.append("<synonym>");
92        xmlStringBuffer.append("</synonym>");
93        xmlStringBuffer.append("</synonyms>");
94        xmlStringBuffer.append("<syst_name>");
95        xmlStringBuffer.append("</syst_name>");
96        xmlStringBuffer.append("<comments>");
97        xmlStringBuffer.append("</comments>");
98        xmlStringBuffer.append("<links>");
99        xmlStringBuffer.append("<link>");
100       xmlStringBuffer.append("</link>");
101       xmlStringBuffer.append("</links>");
102       xmlStringBuffer.append("<references>");
103       xmlStringBuffer.append("<reference>");
104       xmlStringBuffer.append("<number>");
105       xmlStringBuffer.append("</number>");
106       xmlStringBuffer.append("<authors>");
107       xmlStringBuffer.append("</authors>");
108       xmlStringBuffer.append("<title>");
109       xmlStringBuffer.append("</title>");
110       xmlStringBuffer.append("<year>");
111       xmlStringBuffer.append("</year>");
112       xmlStringBuffer.append("<issue>");
113       xmlStringBuffer.append("</issue>");
114       xmlStringBuffer.append("<patent_no>");
115       xmlStringBuffer.append("</patent_no>");
116       xmlStringBuffer.append("<first_page>");
117       xmlStringBuffer.append("</first_page>");
118       xmlStringBuffer.append("<last_page>");
119       xmlStringBuffer.append("</last_page>");
120       xmlStringBuffer.append("<edition>");
121       xmlStringBuffer.append("</edition>");
122       xmlStringBuffer.append("<editor>");
123       xmlStringBuffer.append("</editor>");
124       xmlStringBuffer.append("<volume>");
125       xmlStringBuffer.append("</volume>");
126       xmlStringBuffer.append("<pub_place>");
127       xmlStringBuffer.append("</pub_place>");
128       xmlStringBuffer.append("<pub_company>");
129       xmlStringBuffer.append("</pub_company>");
130       xmlStringBuffer.append("<pub_med>");
131       xmlStringBuffer.append("</pub_med>");
132       xmlStringBuffer.append("<medline>");
133       xmlStringBuffer.append("</medline>");
134       xmlStringBuffer.append("</reference>");
135       xmlStringBuffer.append("</references>");
136 
137       xmlStringBuffer.append("<history>");
138       xmlStringBuffer.append("[" + history.getRootNode().getHistoryLine() + "]");
139       xmlStringBuffer.append("</history>");
140 
141       xmlStringBuffer.append("</enzyme>");
142 
143       return xmlStringBuffer.toString();
144    }
145 
146    private static String entryToIntEnzTextXML (EnzymeEntry enzymeEntry, SpecialCharacters encoding,
147                                                EnzymeViewConstant view) {
148 
149       StringBuffer xmlStringBuffer = new StringBuffer();
150 
151       xmlStringBuffer.append("<enzyme>");
152 
153       xmlStringBuffer.append("<ec>");
154       xmlStringBuffer.append("EC " + enzymeEntry.getEc().toString());
155       xmlStringBuffer.append("</ec>");
156 
157       xmlStringBuffer.append("<active>");
158       xmlStringBuffer.append(enzymeEntry.isActive());
159       xmlStringBuffer.append("</active>");
160 
161       xmlStringBuffer.append("<deleted_text>");
162       xmlStringBuffer.append("</deleted_text>");
163 
164       // Get IntEnz common name. If no IntEnz common name exists then use NC-IUBMB common name.
165       xmlStringBuffer.append("<common_names>");
166       String commonName = enzymeEntry.getCommonName(view).getName();
167       if ( commonName == null ) commonName = enzymeEntry.getCommonName(EnzymeViewConstant.IUBMB).getName();
168       xmlStringBuffer.append("<common_name>");
169       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(commonName)));
170       xmlStringBuffer.append("</common_name>");
171       xmlStringBuffer.append("<common_name>");
172       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(commonName, EncodingType.SWISSPROT_CODE)));
173       xmlStringBuffer.append("</common_name>");
174       xmlStringBuffer.append("</common_names>");
175 
176       xmlStringBuffer.append("<description>");
177       xmlStringBuffer.append("</description>");
178 
179       xmlStringBuffer.append("<reactions>");
180       List reactions = enzymeEntry.getReactions(view);
181       if ( reactions != null ) {
182          for ( int iii = 0; iii < reactions.size(); iii++ ) {
183             Reaction reaction = (Reaction) reactions.get(iii);
184 			if (reaction.getId() > Reaction.NO_ID_ASSIGNED
185 					&& !reaction.getStatus().isPublic()){
186 				continue; // Rhea reaction not public
187 			}
188             xmlStringBuffer.append("<reaction>");
189             xmlStringBuffer.append(removeFormatting(
190                 encoding.xml2Display(reaction.getTextualRepresentation())));
191             xmlStringBuffer.append("</reaction>");
192             xmlStringBuffer.append("<reaction>");
193             xmlStringBuffer.append(removeFormatting(
194                 encoding.xml2Display(reaction.getTextualRepresentation(), EncodingType.SWISSPROT_CODE)));
195             xmlStringBuffer.append("</reaction>");
196             if (reaction.getId() > Reaction.NO_ID_ASSIGNED){
197                 xmlStringBuffer.append("<reaction>");
198                 xmlStringBuffer.append("RHEA:" + reaction.getId());
199                 xmlStringBuffer.append("</reaction>");
200             }
201          }
202       }
203       xmlStringBuffer.append("</reactions>");
204 
205       if ( view == EnzymeViewConstant.INTENZ ) {
206          xmlStringBuffer.append("<cofactors>");
207          Set<Object> cofactors = enzymeEntry.getCofactors();
208          if ( cofactors != null ) {
209             for (Object cofactor : cofactors) {
210 			   String cfText = cofactor.toString().replaceAll(" OR\\d ", " or ").replaceAll(" AND ", " and ");
211                xmlStringBuffer.append("<cofactor>");
212                xmlStringBuffer.append(removeFormatting(encoding.xml2Display(cfText)));
213                xmlStringBuffer.append("</cofactor>");
214                xmlStringBuffer.append("<cofactor>");
215                xmlStringBuffer.append(
216                      removeFormatting(encoding.xml2Display(cfText, EncodingType.SWISSPROT_CODE)));
217                xmlStringBuffer.append("</cofactor>");
218             }
219          }
220          xmlStringBuffer.append("</cofactors>");
221       }
222 
223       xmlStringBuffer.append("<syst_name>");
224       if (!enzymeEntry.getSystematicName().getName().equals("-")){
225          xmlStringBuffer.append(removeFormatting(encoding.xml2Display(enzymeEntry.getSystematicName().getName())));
226       }
227       xmlStringBuffer.append("</syst_name>");
228 
229       if (!enzymeEntry.getSystematicName().getName().equals("-")){
230          xmlStringBuffer.append("<syst_name>");
231          xmlStringBuffer.append(removeFormatting(
232                encoding.xml2Display(enzymeEntry.getSystematicName().getName(), EncodingType.SWISSPROT_CODE)));
233          xmlStringBuffer.append("</syst_name>");
234       }
235 
236       xmlStringBuffer.append("<synonyms>");
237       List synonyms = enzymeEntry.getSynonyms(view);
238       if ( synonyms.size() > 0 ) {
239          for ( int iii = 0; iii < synonyms.size(); iii++ ) {
240             EnzymeName synonym = (EnzymeName) synonyms.get(iii);
241             xmlStringBuffer.append("<synonym>");
242             xmlStringBuffer.append(removeFormatting(encoding.xml2Display(synonym.getName())));
243             if ( synonym.getQualifier().toString().equals("MIS") ) {
244                xmlStringBuffer.append(" [misleading]");
245             }
246 
247             if ( synonym.getQualifier().toString().equals("OBS") ) {
248                xmlStringBuffer.append(" [obsolete]");
249             }
250 
251             if ( synonym.getQualifier().toString().equals("AMB") ) {
252                xmlStringBuffer.append(" [ambiguous]");
253             }
254             xmlStringBuffer.append("</synonym>");
255 
256             xmlStringBuffer.append("<synonym>");
257             xmlStringBuffer.append(
258                   removeFormatting(encoding.xml2Display(synonym.getName(), EncodingType.SWISSPROT_CODE)));
259             if ( synonym.getQualifier().toString().equals("MIS") ) {
260                xmlStringBuffer.append(" [misleading]");
261             }
262 
263             if ( synonym.getQualifier().toString().equals("OBS") ) {
264                xmlStringBuffer.append(" [obsolete]");
265             }
266 
267             if ( synonym.getQualifier().toString().equals("AMB") ) {
268                xmlStringBuffer.append(" [ambiguous]");
269             }
270             xmlStringBuffer.append("</synonym>");
271          }
272       } else {
273          xmlStringBuffer.append("<synonym>");
274          xmlStringBuffer.append("</synonym>");
275       }
276       xmlStringBuffer.append("</synonyms>");
277 
278       xmlStringBuffer.append("<comments>");
279       List comments = enzymeEntry.getComments(view);
280       if ( comments != null ) {
281          for ( int iii = 0; iii < comments.size(); iii++ ) {
282             EnzymeComment comment = (EnzymeComment) comments.get(iii);
283             if ( !comment.getCommentText().trim().equals("") ) {
284                xmlStringBuffer.append("<comment>");
285                xmlStringBuffer.append(removeFormatting(encoding.xml2Display(comment.getCommentText())));
286                xmlStringBuffer.append("</comment>");
287                xmlStringBuffer.append("<comment>");
288                xmlStringBuffer.append(
289                      removeFormatting(encoding.xml2Display(comment.getCommentText(), EncodingType.SWISSPROT_CODE)));
290                xmlStringBuffer.append("</comment>");
291             }
292          }
293       }
294       xmlStringBuffer.append("</comments>");
295 
296       xmlStringBuffer.append("<links>");
297       SortedSet links = enzymeEntry.getLinks(view);
298       if ( links.size() > 0 || links.size() > 0 ) {
299          for ( Iterator it = links.iterator(); it.hasNext(); ) {
300         	 // remove links from preliminary ECs to external preliminary ECs?
301             EnzymeLink link = (EnzymeLink) it.next();
302             xmlStringBuffer.append("<link>");
303             xmlStringBuffer.append(link.getAccession());
304             xmlStringBuffer.append("</link>");
305          }
306       } else {
307          xmlStringBuffer.append("<link>");
308          xmlStringBuffer.append("</link>");
309       }
310       xmlStringBuffer.append("</links>");
311 
312       xmlStringBuffer.append("<references>");
313       List references = enzymeEntry.getReferences();
314       boolean refExists = false;
315       for ( int iii = 0; iii < references.size(); iii++ ) {
316          refExists = true;
317          Reference reference = (Reference) references.get(iii);
318          xmlStringBuffer.append("<reference>");
319          xmlStringBuffer.append(EnzymeReferenceHelper.referenceToIntEnzTextXML(reference, iii, encoding));
320          xmlStringBuffer.append("</reference>");
321       }
322       if ( !refExists ) {
323          xmlStringBuffer.append("<reference>");
324          xmlStringBuffer.append("<number>");
325          xmlStringBuffer.append("</number>");
326          xmlStringBuffer.append("<authors>");
327          xmlStringBuffer.append("</authors>");
328          xmlStringBuffer.append("<title>");
329          xmlStringBuffer.append("</title>");
330          xmlStringBuffer.append("<year>");
331          xmlStringBuffer.append("</year>");
332          xmlStringBuffer.append("<issue>");
333          xmlStringBuffer.append("</issue>");
334          xmlStringBuffer.append("<patent_no>");
335          xmlStringBuffer.append("</patent_no>");
336          xmlStringBuffer.append("<first_page>");
337          xmlStringBuffer.append("</first_page>");
338          xmlStringBuffer.append("<last_page>");
339          xmlStringBuffer.append("</last_page>");
340          xmlStringBuffer.append("<edition>");
341          xmlStringBuffer.append("</edition>");
342          xmlStringBuffer.append("<editor>");
343          xmlStringBuffer.append("</editor>");
344          xmlStringBuffer.append("<volume>");
345          xmlStringBuffer.append("</volume>");
346          xmlStringBuffer.append("<pub_place>");
347          xmlStringBuffer.append("</pub_place>");
348          xmlStringBuffer.append("<pub_company>");
349          xmlStringBuffer.append("</pub_company>");
350          xmlStringBuffer.append("<pub_med>");
351          xmlStringBuffer.append("</pub_med>");
352          xmlStringBuffer.append("<medline>");
353          xmlStringBuffer.append("</medline>");
354          xmlStringBuffer.append("</reference>");
355       }
356       xmlStringBuffer.append("</references>");
357 
358       xmlStringBuffer.append("<history>");
359       xmlStringBuffer.append("[" + enzymeEntry.getHistory().getRootNode().getHistoryLine() + "]");
360       xmlStringBuffer.append("</history>");
361 
362       xmlStringBuffer.append("</enzyme>");
363 
364       return xmlStringBuffer.toString();
365    }
366 
367    protected static String removeFormatting (String text) {
368       text = text.replaceAll("\\<\\/?small\\>", "");
369       text = text.replaceAll("\\<\\/?sup\\>", "");
370       text = text.replaceAll("\\<\\/?sub\\>", "");
371       text = text.replaceAll("\\<\\/?b\\>", "");
372       text = text.replaceAll("\\<\\/?i\\>", "");
373       text = text.replaceAll("\\<\\/?p\\/?\\>", "");
374       return text;
375    }
376 
377 
378 
379 
380 }