JAL-1641 Re-implemented lost changes after sequenceFeature refactor
[jalview.git] / src / jalview / io / JSONFile.java
1 package jalview.io;
2
3 import jalview.api.FeaturesDisplayedI;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.Annotation;
6 import jalview.datamodel.Sequence;
7 import jalview.datamodel.SequenceFeature;
8 import jalview.datamodel.SequenceGroup;
9 import jalview.datamodel.SequenceI;
10 import jalview.gui.AlignFrame;
11 import jalview.gui.Desktop;
12 import jalview.json.binding.v1.AlignmentAnnotationPojo;
13 import jalview.json.binding.v1.AlignmentPojo;
14 import jalview.json.binding.v1.AlignmentPojo.JalviewBioJsColorSchemeMapper;
15 import jalview.json.binding.v1.AnnotationPojo;
16 import jalview.json.binding.v1.FeaturePojo;
17 import jalview.json.binding.v1.SequenceGrpPojo;
18 import jalview.json.binding.v1.SequencePojo;
19 import jalview.schemes.ColourSchemeI;
20 import jalview.schemes.ColourSchemeProperty;
21 import jalview.viewmodel.AlignmentViewport;
22
23 import java.awt.Color;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Hashtable;
27 import java.util.Iterator;
28
29 import org.json.simple.JSONArray;
30 import org.json.simple.JSONObject;
31 import org.json.simple.parser.JSONParser;
32
33 public class JSONFile extends AlignFile
34 {
35   private ColourSchemeI cs;
36
37   private String jalviewVersion;
38
39   private String webStartLaunchServletUrl = "http://www.jalview.org/services/launchApp";
40
41   public static final String FILE_EXT = "json";
42
43   public static final String FILE_DESC = "JSON";
44
45   private String globalColorScheme;
46
47   private Hashtable<String, Sequence> seqMap;
48
49   private FeaturesDisplayedI displayedFeatures;
50
51   // private AlignmentI al;
52
53   private AlignmentViewport av;
54
55   private jalview.api.FeatureRenderer fr;
56
57   public JSONFile()
58   {
59     super();
60   }
61
62   public JSONFile(FileParse source) throws IOException
63   {
64     super(source);
65   }
66
67   public JSONFile(String inFile, String type) throws IOException
68   {
69     super(inFile, type);
70   }
71
72
73   @Override
74   public void parse() throws IOException
75   {
76     StringBuilder jsonStringBuilder = new StringBuilder();
77     String currentLine;
78     while ((currentLine = nextLine()) != null)
79     {
80       jsonStringBuilder.append(currentLine);
81     }
82     parse(jsonStringBuilder.toString());
83
84   }
85
86   @Override
87   public String print()
88   {
89     AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
90     if (Desktop.getCurrentAlignFrame() != null)
91     {
92       jsonAlignmentPojo.setGlobalColorScheme(ColourSchemeProperty
93               .getColourName(Desktop.getCurrentAlignFrame().getViewport()
94                       .getGlobalColourScheme()));
95       this.av = Desktop.getCurrentAlignFrame().getCurrentView();
96       this.fr = Desktop.getCurrentAlignFrame().alignPanel
97               .cloneFeatureRenderer();
98       displayedFeatures = av.getFeaturesDisplayed();
99     }
100     jsonAlignmentPojo.setJalviewVersion(jalviewVersion);
101     jsonAlignmentPojo.setWebStartUrl(webStartLaunchServletUrl);
102
103     System.out.println(">>>>>>>>>>>>>> found grps : " + seqGroups.size());
104     if (seqGroups.size() > 0)
105     {
106       ArrayList<SequenceGrpPojo> sequenceGroupsPojo = new ArrayList<SequenceGrpPojo>();
107       for (SequenceGroup seqGrp : seqGroups)
108       {
109         System.out.println(">>>>>>>>>>>> grp : " + seqGrp.getName());
110         SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
111         seqGrpPojo.setGroupName(seqGrp.getName());
112         seqGrpPojo.setColourScheme(ColourSchemeProperty
113                 .getColourName(seqGrp.cs));
114         seqGrpPojo.setColourText(seqGrp.getColourText());
115         seqGrpPojo.setDescription(seqGrp.getDescription());
116         seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
117         seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
118         seqGrpPojo.setEndRes(seqGrp.getEndRes());
119         seqGrpPojo.setStartRes(seqGrp.getStartRes());
120         seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
121         jsonAlignmentPojo.getSequenceGroups().add(seqGrpPojo);
122         for(SequenceI seq : seqGrp.getSequences()){
123           seqGrpPojo.getSeqsHash().add(seq.getName() + "_" + seq.hashCode());
124         }
125         // sequenceGroupsPojo.add(seqGrpPojo);
126       }
127     }
128     for (AlignmentAnnotation annot : annotations)
129     {
130       AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
131       alignAnnotPojo.setDescription(annot.description);
132       alignAnnotPojo.setLabel(annot.label);
133       for (Annotation annotation : annot.annotations)
134       {
135         AnnotationPojo annotationPojo = new AnnotationPojo();
136         if (annotation != null)
137         {
138           annotationPojo.setDescription(annotation.description);
139           annotationPojo.setValue(annotation.value);
140           annotationPojo
141                   .setSecondaryStructure(annotation.secondaryStructure);
142           annotationPojo.setDisplayCharacter(annotation.displayCharacter);
143           alignAnnotPojo.getAnnotations().add(annotationPojo);
144         }
145         else
146         {
147           alignAnnotPojo.getAnnotations().add(annotationPojo);
148         }
149       }
150       jsonAlignmentPojo.getAlignmentAnnotation().add(alignAnnotPojo);
151     }
152
153     int count = 0;
154     for (SequenceI seq : seqs)
155     {
156       StringBuilder name = new StringBuilder();
157       name.append(seq.getName()).append("/").append(seq.getStart())
158               .append("-").append(seq.getEnd());
159
160       SequencePojo jsonSeqPojo = new SequencePojo();
161
162       jsonSeqPojo.setId(seq.getName() + "_" + seq.hashCode());
163       jsonSeqPojo.setOrder(++count);
164       jsonSeqPojo.setEnd(seq.getEnd());
165       jsonSeqPojo.setStart(seq.getStart());
166       jsonSeqPojo.setName(name.toString());
167       jsonSeqPojo.setSeq(seq.getSequenceAsString());
168       jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
169
170       if (seq.getDatasetSequence() != null
171               && seq.getDatasetSequence().getSequenceFeatures() != null)
172       {
173         ArrayList<FeaturePojo> seqFeaturesPojo = new ArrayList<FeaturePojo>();
174         for (SequenceFeature sf : seq.getDatasetSequence()
175                 .getSequenceFeatures())
176         {
177
178           if (displayedFeatures != null
179                   && displayedFeatures.isVisible(sf.getType()))
180           {
181
182             String fillColor = ((fr != null) ? jalview.util.Format
183                     .getHexString(fr.findFeatureColour(Color.white, seq,
184                             seq.findIndex(sf.getBegin()))) : null);
185             FeaturePojo jsonFeature = new FeaturePojo();
186             jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
187             jsonFeature.setXend(seq.findIndex(sf.getEnd()));
188             jsonFeature.setType(sf.getType());
189             jsonFeature.setDescription(sf.getDescription());
190             jsonFeature.setLinks(sf.links);
191             jsonFeature.setOtherDetails(sf.otherDetails);
192             jsonFeature.setScore(sf.getScore());
193             jsonFeature.setFillColor(fillColor);
194             jsonFeature.setFeatureGroup(sf.getFeatureGroup());
195             seqFeaturesPojo.add(jsonFeature);
196           }
197         }
198         jsonSeqPojo.setFeatures(seqFeaturesPojo);
199       }
200     }
201     return new com.json.JSONObject(jsonAlignmentPojo).toString()
202             .replaceAll("xstart", "xStart").replaceAll("xend", "xEnd");
203   }
204
205   public void parse(String jsonAlignmentString)
206   {
207     try
208     {
209       JSONParser jsonParser = new JSONParser();
210       JSONObject alignmentJsonObj = (JSONObject) jsonParser
211               .parse(jsonAlignmentString);
212       JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
213       JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj.get("alignmentAnnotation");
214       JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj.get("sequenceGroups");
215       String jsColourScheme = (String) alignmentJsonObj
216               .get("globalColorScheme");
217       System.out.println(">>>>>>>>>>>>>>>> global cs : " + jsColourScheme);
218       cs = getJalviewColorScheme(jsColourScheme);
219       seqMap = new Hashtable<String, Sequence>();
220       Desktop.setCurrentGlobalColourScheme(cs);
221       for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
222               .hasNext();)
223       {
224         JSONObject sequence = sequenceIter.next();
225         String sequcenceString = sequence.get("seq").toString();
226         String sequenceName = sequence.get("name").toString();
227         String seqUniqueId = sequence.get("id").toString();
228         int start = Integer.valueOf(sequence.get("start").toString());
229         int end = Integer.valueOf(sequence.get("end").toString());
230         Sequence seq = new Sequence(sequenceName, sequcenceString, start,
231                 end);
232         JSONArray jsonSeqArray = (JSONArray) sequence.get("features");
233         SequenceFeature[] retrievedSeqFeatures = getJalviewSequenceFeatures(
234                 jsonSeqArray, seq);
235         if (retrievedSeqFeatures != null)
236         {
237           seq.setSequenceFeatures(retrievedSeqFeatures);
238         }
239         seqs.add(seq);
240         seqMap.put(seqUniqueId, seq);
241       }
242       
243       // for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray.iterator();
244       // seqGrpIter
245       // .hasNext();)
246       // {
247       // JSONObject seqGrpObj = seqGrpIter.next();
248       // String grpName = seqGrpObj.get("groupName").toString();
249       // String colourScheme = seqGrpObj.get("colourScheme").toString();
250       // String description = (seqGrpObj.get("description") == null) ? null
251       // : seqGrpObj.get("description").toString();
252       // boolean displayBoxes = Boolean.valueOf(seqGrpObj
253       // .get("displayBoxes").toString());
254       // boolean displayText = Boolean.valueOf(seqGrpObj.get("displayText")
255       // .toString());
256       // boolean colourText = Boolean.valueOf(seqGrpObj.get("colourText")
257       // .toString());
258       // boolean showNonconserved = Boolean.valueOf(seqGrpObj.get(
259       // "showNonconserved").toString());
260       // int startRes = Integer
261       // .valueOf(seqGrpObj.get("startRes").toString());
262       // int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
263       // JSONArray seqsHashArray = (JSONArray) seqGrpObj.get("seqsHash");
264       //
265       // ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
266       // if (seqsHashArray.size() > 0)
267       // {
268       // Iterator<String> seqHashIter = seqsHashArray.iterator();
269       //
270       // while (seqHashIter.hasNext())
271       // {
272       // String seqHash = seqHashIter.next();
273       // Sequence sequence = seqMap.get(seqHash);
274       // if (sequence != null)
275       // {
276       // grpSeqs.add(sequence);
277       // }
278       // // System.out.println(">>>>>>>>>>>>>>>>>>>>>>> seqHash : "
279       // // + seqHash);
280       // }
281       // }
282       // System.out.println(">>>>>>>>>>>>>>>>>> scheme string : "
283       // + colourScheme);
284       // ColourSchemeI scheme = getJalviewColorScheme(colourScheme);
285       // System.out.println(">>>>>>>>>>>>>>>>>> scheme: " + scheme);
286       // SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, scheme,
287       // displayBoxes, displayText, colourText, startRes, endRes);
288       // this.seqGroups.add(seqGrp);
289       //
290       // }
291
292
293       for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
294               .hasNext();)
295       {
296         JSONObject alAnnot = alAnnotIter.next();
297         JSONArray annotJsonArray = (JSONArray) alAnnot
298                 .get("annotations");
299         Annotation[] annotations = new Annotation[annotJsonArray.size()];
300         int count = 0;
301         for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
302                 .hasNext();)
303         {
304           JSONObject annot = annotIter.next();
305           if (annot == null)
306           {
307             annotations[count] = null;
308           }
309           else
310           {
311             float val = annot.get("value") == null ? null
312                     : Float.valueOf(annot.get("value")
313                     .toString());
314             String desc = annot.get("description") == null ? null : annot
315                     .get("description").toString();
316
317             char ss = annot.get("secondaryStructure") == null ? null
318                     : annot
319                     .get("secondaryStructure").toString().charAt(0);
320             String displayChar = annot.get(
321                     "displayCharacter").toString();
322
323             annotations[count] = new Annotation(displayChar, desc, ss, val);
324           }
325           ++count;
326         }
327
328         AlignmentAnnotation alignAnnot = new AlignmentAnnotation(alAnnot
329                 .get("label").toString(), alAnnot.get("description")
330                 .toString(), annotations);
331         this.annotations.add(alignAnnot);
332       }
333
334     } catch (Exception e)
335     {
336       e.printStackTrace();
337     }
338   }
339
340   public SequenceFeature[] getJalviewSequenceFeatures(
341           JSONArray jsonSeqFeatures, Sequence seq)
342   {
343     SequenceFeature[] seqFeatures = null;
344     int count = 0;
345     if (jsonSeqFeatures != null)
346     {
347       seqFeatures = new SequenceFeature[jsonSeqFeatures.size()];
348       for (@SuppressWarnings("unchecked")
349       Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
350               .hasNext();)
351       {
352
353         SequenceFeature sequenceFeature = new SequenceFeature();
354         JSONObject jsonFeature = seqFeatureItr.next();
355         Long begin = (Long) jsonFeature.get("xStart");
356         Long end = (Long) jsonFeature.get("xEnd");
357         String type = (String) jsonFeature.get("type");
358         // String color = (String) jsonFeature.get("fillColor");
359         String featureGrp = (String) jsonFeature.get("featureGroup");
360         String descripiton = (String) jsonFeature.get("description");
361         Float score = Float.valueOf(jsonFeature.get("score").toString());
362         // Hashtable otherDetails = (Hashtable) jsonFeature
363         // .get("otherDetails");
364         // Vector<String> links = (Vector<String>) jsonFeature.get("links");
365
366         // sequenceFeature.links = links;
367         // sequenceFeature.otherDetails = otherDetails;
368         sequenceFeature.setScore(score);
369         sequenceFeature.setDescription(descripiton);
370
371         sequenceFeature.setBegin(seq.findPosition(begin.intValue()));
372         sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1);
373         sequenceFeature.setType(type);
374         seqFeatures[count++] = sequenceFeature;
375       }
376     }
377     return seqFeatures;
378   }
379
380   private ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName)
381   {
382     ColourSchemeI jalviewColor = null;
383     for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper
384             .values())
385     {
386       if (cs.getBioJsName().equalsIgnoreCase(bioJsColourSchemeName))
387       {
388         jalviewColor = cs.getJvColourScheme();
389         break;
390       }
391     }
392     return jalviewColor;
393   }
394
395   public void LoadAlignmentFeatures(AlignFrame af)
396   {
397     af.setShowSeqFeatures(true);
398     af.changeColour(cs);
399     af.setMenusForViewport();
400   }
401
402   public String getGlobalColorScheme()
403   {
404     return globalColorScheme;
405   }
406
407   public void setGlobalColorScheme(String globalColorScheme)
408   {
409     this.globalColorScheme = globalColorScheme;
410   }
411 }