View Javadoc

1   package uk.ac.ebi.intenz.tools.export;
2   
3   import java.io.IOException;
4   import java.io.OutputStream;
5   import java.util.Collection;
6   import java.util.List;
7   
8   import javax.json.Json;
9   import javax.json.JsonArrayBuilder;
10  import javax.json.JsonObject;
11  import javax.json.JsonObjectBuilder;
12  import javax.json.JsonWriter;
13  
14  import uk.ac.ebi.intenz.domain.constants.View;
15  import uk.ac.ebi.intenz.domain.enzyme.EnzymeClass;
16  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
17  import uk.ac.ebi.intenz.domain.enzyme.EnzymeName;
18  import uk.ac.ebi.intenz.domain.enzyme.EnzymeSubSubclass;
19  import uk.ac.ebi.intenz.domain.enzyme.EnzymeSubclass;
20  import uk.ac.ebi.xchars.SpecialCharacters;
21  
22  /**
23   * Utility class to export enzyme entries - but also classes, subclasses and
24   * subsubclasses - as JSON.
25   * @author rafa
26   * @since 1.3.18
27   */
28  public class JsonExporter implements IntenzExporter {
29  
30      public void export(Collection<EnzymeEntry> enzymes, OutputStream os)
31      throws IOException {
32          JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
33          for (EnzymeEntry enzyme : enzymes) {
34              jsonArrayBuilder.add(jsonFromEnzyme(enzyme));
35          }
36          JsonWriter jsonWriter = Json.createWriter(os);
37          jsonWriter.writeArray(jsonArrayBuilder.build());
38          jsonWriter.close();
39      }
40  
41      public void export(EnzymeEntry enzyme, OutputStream os) throws IOException {
42          JsonWriter jsonWriter = Json.createWriter(os);
43          jsonWriter.writeObject(jsonFromEnzyme(enzyme));
44          jsonWriter.close();
45      }
46  
47      /**
48       * Exports an EC sub-subclass as JSON, including only its <i>active</i> EC
49       * numbers (not deleted, not transferred).
50       * @param essc The EC sub-subclass.
51       * @param os The output stream to write to.
52       */
53      public void export(EnzymeSubSubclass essc, OutputStream os){
54          export(essc, os, true);
55      }
56  
57      /**
58       * Exports an EC sub-subclass as JSON.
59       * @param essc The EC sub-subclass.
60       * @param os The output stram to write to.
61       * @param onlyActive Do we want to export only the <i>active</i> (not
62       *      deleted, not transferred) EC numbers?
63       */
64      public void export(EnzymeSubSubclass essc, OutputStream os,
65              boolean onlyActive){
66          JsonWriter jsonWriter = Json.createWriter(os);
67          jsonWriter.writeObject(jsonFromSubsubClass(essc, onlyActive));
68          jsonWriter.close();
69      }
70  
71      /**
72       * Exports an EC subclass as JSON.
73       * @param esc The EC subclass.
74       * @param os The output stream to write to.
75       */
76      public void export(EnzymeSubclass esc, OutputStream os){
77          JsonWriter jsonWriter = Json.createWriter(os);
78          jsonWriter.writeObject(jsonFromSubclass(esc));
79          jsonWriter.close();
80      }
81  
82      /**
83       * Exports an EC class as JSON.
84       * @param ec The EC class.
85       * @param os The output stream to write to.
86       */
87      public void export(EnzymeClass ec, OutputStream os){
88          JsonWriter jsonWriter = Json.createWriter(os);
89          jsonWriter.writeObject(jsonFromClass(ec));
90          jsonWriter.close();
91      }
92  
93      /**
94       * Turns a list of EC classes into a JSON array.
95       * @param ecs The EC classes.
96       * @param os The output stream to write to.
97       */
98      public void export(List<EnzymeClass> ecs, OutputStream os){
99          JsonWriter jsonWriter = Json.createWriter(os);
100         JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
101         for (EnzymeClass ec : ecs) {
102             jsonArrayBuilder.add(jsonFromClass(ec));
103         }
104         jsonWriter.writeArray(jsonArrayBuilder.build());
105         jsonWriter.close();
106     }
107 
108     /**
109      * Turns an EC entry into a JSON object. The included properties are:
110      * <ul>
111      *  <li>ec (string)</li>
112      *  <li>name (string)</li>
113      *  <li>synonyms (array of string)</li>
114      * </ul>
115      * @param enzyme
116      * @return a JSON object
117      */
118     private JsonObject jsonFromEnzyme(EnzymeEntry enzyme) {
119         JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
120         jsonObjectBuilder.add("ec", enzyme.getEc().toString());
121         jsonObjectBuilder.add("name", SpecialCharacters.getInstance(null)
122                 .xml2Display(enzyme.getCommonName().getName()));
123         JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
124         for (EnzymeName synonym : enzyme.getSynonyms(View.INTENZ)){
125             jsonArrayBuilder.add(SpecialCharacters.getInstance(null)
126                     .xml2Display(synonym.getName()));
127         }
128         jsonObjectBuilder.add("synonyms", jsonArrayBuilder);
129         // ...
130         return jsonObjectBuilder.build();
131     }
132 
133     /**
134      * Turns an EC sub-subclass into a JSON object. The included properties are:
135      * <ul>
136      *  <li>ec (string)</li>
137      *  <li>name (string)</li>
138      *  <li>description (string)</li>
139      *  <li>entries (array of JSON enzyme objects, see {@link
140      *      #jsonFromEnzyme(uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry)})</li>
141      * </ul>
142      * @param essc the EC sub-subclass.
143      * @param onlyActive Do we want to include only the active EC numbers
144      * @return a JSON object.
145      */
146     private JsonObject jsonFromSubsubClass(EnzymeSubSubclass essc,
147             boolean onlyActive){
148         JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
149         jsonObjectBuilder.add("ec", essc.getEc().toString());
150         jsonObjectBuilder.add("name", SpecialCharacters.getInstance(null)
151                 .xml2Display(essc.getClassName()));
152         jsonObjectBuilder.add("description", SpecialCharacters.getInstance(null)
153                 .xml2Display(essc.getDescription()));
154         JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
155         for (EnzymeEntry e : essc.getEntries()) {
156             if (onlyActive && !e.isActive()) continue;
157             jsonArrayBuilder.add(jsonFromEnzyme(e));
158         }
159         jsonObjectBuilder.add("entries", jsonArrayBuilder.build());
160         return jsonObjectBuilder.build();
161     }
162 
163     /**
164      * Turns an EC subclass into a JSON object. The included properties are:
165      * <ul>
166      *  <li>ec (string)</li>
167      *  <li>name (string)</li>
168      *  <li>description (string)</li>
169      *  <li>subsubclasses (array of JSON objects with just ec, name and
170      *      description).</li>
171      * </ul>
172      * @param esc The EC subclass.
173      * @return a JSON object.
174      */
175     private JsonObject jsonFromSubclass(EnzymeSubclass esc){
176         JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
177         jsonObjectBuilder.add("ec", esc.getEc().toString());
178         jsonObjectBuilder.add("name", SpecialCharacters.getInstance(null)
179                 .xml2Display(esc.getName()));
180         jsonObjectBuilder.add("description", SpecialCharacters.getInstance(null)
181                 .xml2Display(esc.getDescription()));
182         JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
183         for (EnzymeSubSubclass essc : esc.getSubSubclasses()) {
184             jsonArrayBuilder.add(Json.createObjectBuilder()
185                     .add("ec", essc.getEc().toString())
186                     .add("name", SpecialCharacters.getInstance(null)
187                             .xml2Display(essc.getName()))
188                     .add("description", SpecialCharacters.getInstance(null)
189                             .xml2Display(essc.getDescription())));
190         }
191         jsonObjectBuilder.add("subsubclasses", jsonArrayBuilder.build());
192         return jsonObjectBuilder.build();
193     }
194 
195     /**
196      * Turns an EC class into a JSON object. The included properties are:
197      * <ul>
198      *  <li>ec (string)</li>
199      *  <li>name (string)</li>
200      *  <li>description (string)</li>
201      *  <li>subclasses (array of JSON objects with just ec, name and
202      *      description).</li>
203      * </ul>
204      * @param ec the EC class.
205      * @return a JSON object.
206      */
207     private JsonObject jsonFromClass(EnzymeClass ec){
208         JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
209         jsonObjectBuilder.add("ec", ec.getEc().toString());
210         jsonObjectBuilder.add("name", SpecialCharacters.getInstance(null)
211                 .xml2Display(ec.getName()));
212         jsonObjectBuilder.add("description", SpecialCharacters.getInstance(null)
213                 .xml2Display(ec.getDescription()));
214         JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
215         for (EnzymeSubclass esc : ec.getSubclasses()) {
216             jsonArrayBuilder.add(Json.createObjectBuilder()
217                 .add("ec", esc.getEc().toString())
218                 .add("name", SpecialCharacters.getInstance(null)
219                         .xml2Display(esc.getName()))
220                 .add("description", SpecialCharacters.getInstance(null)
221                         .xml2Display(esc.getDescription())));
222         }
223         jsonObjectBuilder.add("subclasses", jsonArrayBuilder.build());
224         return jsonObjectBuilder.build();
225     }
226 }