2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import jalview.api.AlignViewControllerGuiI;
25 import jalview.api.AlignViewportI;
26 import jalview.api.FeatureRenderer;
27 import jalview.api.FeaturesDisplayedI;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceGroup;
33 import jalview.datamodel.SequenceI;
34 import jalview.json.binding.v1.AlignmentAnnotationPojo;
35 import jalview.json.binding.v1.AlignmentPojo;
36 import jalview.json.binding.v1.AnnotationPojo;
37 import jalview.json.binding.v1.JalviewSettingsPojo;
38 import jalview.json.binding.v1.JalviewSettingsPojo.JalviewBioJsColorSchemeMapper;
39 import jalview.json.binding.v1.SequenceFeaturesPojo;
40 import jalview.json.binding.v1.SequenceGrpPojo;
41 import jalview.json.binding.v1.SequencePojo;
42 import jalview.schemes.ColourSchemeI;
43 import jalview.schemes.ColourSchemeProperty;
45 import java.awt.Color;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Hashtable;
49 import java.util.Iterator;
50 import java.util.List;
51 import java.util.Vector;
53 import org.json.simple.JSONArray;
54 import org.json.simple.JSONObject;
55 import org.json.simple.parser.JSONParser;
57 public class JSONFile extends AlignFile
59 private ColourSchemeI colourScheme;
61 private String jalviewVersion;
63 private String webStartLaunchServletUrl;
65 public static final String FILE_EXT = "json";
67 public static final String FILE_DESC = "JSON";
69 private String globalColorScheme;
71 private boolean showSeqFeatures;
73 private Hashtable<String, Sequence> seqMap;
75 private FeaturesDisplayedI displayedFeatures;
77 private FeatureRenderer fr;
79 private JSONExportSettings jsonExportSettings;
86 public JSONFile(FileParse source) throws IOException
91 public JSONFile(String inFile, String type) throws IOException
97 public void parse() throws IOException
99 StringBuilder jsonStringBuilder = new StringBuilder();
101 while ((currentLine = nextLine()) != null)
103 jsonStringBuilder.append(currentLine);
105 parse(jsonStringBuilder.toString());
110 public String print()
112 String jsonOutput = null;
115 if (getJsonExportSettings() == null)
117 jsonExportSettings = new JSONExportSettings();
118 jsonExportSettings.setExportAnnotations(true);
119 jsonExportSettings.setExportGroups(true);
120 jsonExportSettings.setExportJalviewSettings(true);
121 jsonExportSettings.setExportSequenceFeatures(true);
124 AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
125 if (getViewport() != null)
127 globalColorScheme = ColourSchemeProperty
128 .getColourName(getViewport()
129 .getGlobalColourScheme());
130 setDisplayedFeatures(getViewport().getFeaturesDisplayed());
131 showSeqFeatures = getViewport().isShowSequenceFeatures();
132 fr = getViewport().getFeatureRenderer();
136 for (SequenceI seq : seqs)
138 StringBuilder name = new StringBuilder();
139 name.append(seq.getName()).append("/").append(seq.getStart())
140 .append("-").append(seq.getEnd());
141 SequencePojo jsonSeqPojo = new SequencePojo();
142 jsonSeqPojo.setId(seq.getName() + "_" + seq.hashCode());
143 jsonSeqPojo.setOrder(++count);
144 jsonSeqPojo.setEnd(seq.getEnd());
145 jsonSeqPojo.setStart(seq.getStart());
146 jsonSeqPojo.setName(name.toString());
147 jsonSeqPojo.setSeq(seq.getSequenceAsString());
148 jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
151 if (jsonExportSettings.isExportJalviewSettings())
154 webStartLaunchServletUrl = "www.jalview.org/services/launchApp";
156 JalviewSettingsPojo jvSettings = new JalviewSettingsPojo();
157 jvSettings.setGlobalColorScheme(globalColorScheme);
158 jvSettings.setJalviewVersion(jalviewVersion);
159 jvSettings.setWebStartUrl(webStartLaunchServletUrl);
160 jvSettings.setShowSeqFeatures(showSeqFeatures);
161 jsonAlignmentPojo.setJalviewSettings(jvSettings);
164 if (jsonExportSettings.isExportAnnotations())
167 .setAlignAnnotation(annotationToJsonPojo(annotations));
170 if (jsonExportSettings.isExportSequenceFeatures())
173 .setSeqFeatures(sequenceFeatureToJsonPojo(seqs, fr));
176 if (jsonExportSettings.isExportGroups() && seqGroups != null
177 && seqGroups.size() > 0)
179 for (SequenceGroup seqGrp : seqGroups)
181 SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
182 seqGrpPojo.setGroupName(seqGrp.getName());
183 seqGrpPojo.setColourScheme(ColourSchemeProperty
184 .getColourName(seqGrp.cs));
185 seqGrpPojo.setColourText(seqGrp.getColourText());
186 seqGrpPojo.setDescription(seqGrp.getDescription());
187 seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
188 seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
189 seqGrpPojo.setEndRes(seqGrp.getEndRes());
190 seqGrpPojo.setStartRes(seqGrp.getStartRes());
191 seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
192 for (SequenceI seq : seqGrp.getSequences())
194 seqGrpPojo.getSeqsHash().add(
195 seq.getName() + "_" + seq.hashCode());
197 jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo);
200 org.json.JSONObject generatedJSon = new org.json.JSONObject(
202 jsonOutput = generatedJSon.toString();
203 return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend",
205 } catch (Exception e)
212 public static List<SequenceFeaturesPojo> sequenceFeatureToJsonPojo(
213 List<SequenceI> seqs, FeatureRenderer fr)
215 FeaturesDisplayedI displayedFeatures = (fr == null) ? null : fr
216 .getFeaturesDisplayed();
217 List<SequenceFeaturesPojo> sequenceFeaturesPojo = new ArrayList<SequenceFeaturesPojo>();
218 for (SequenceI seq : seqs)
220 SequenceI dataSetSequence = seq.getDatasetSequence();
221 SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null
222 : seq.getDatasetSequence().getSequenceFeatures();
224 if (seqFeatures == null)
229 for (SequenceFeature sf : seqFeatures)
231 if (displayedFeatures != null
232 && displayedFeatures.isVisible(sf.getType()))
234 SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
235 seq.getName() + "_" + seq.hashCode());
236 String featureColour = (fr == null) ? null : jalview.util.Format
238 .findFeatureColour(Color.white, seq,
239 seq.findIndex(sf.getBegin())));
240 jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
241 jsonFeature.setXend(seq.findIndex(sf.getEnd()));
242 jsonFeature.setType(sf.getType());
243 jsonFeature.setDescription(sf.getDescription());
244 jsonFeature.setLinks(sf.links);
245 jsonFeature.setOtherDetails(sf.otherDetails);
246 jsonFeature.setScore(sf.getScore());
247 jsonFeature.setFillColor(featureColour);
248 jsonFeature.setFeatureGroup(sf.getFeatureGroup());
249 sequenceFeaturesPojo.add(jsonFeature);
253 return sequenceFeaturesPojo;
256 public static List<AlignmentAnnotationPojo> annotationToJsonPojo(
257 Vector<AlignmentAnnotation> annotations)
259 List<AlignmentAnnotationPojo> jsonAnnotations = new ArrayList<AlignmentAnnotationPojo>();
260 if (annotations == null)
262 return jsonAnnotations;
264 for (AlignmentAnnotation annot : annotations)
266 AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
267 alignAnnotPojo.setDescription(annot.description);
268 alignAnnotPojo.setLabel(annot.label);
269 for (Annotation annotation : annot.annotations)
271 AnnotationPojo annotationPojo = new AnnotationPojo();
272 if (annotation != null)
274 annotationPojo.setDescription(annotation.description);
275 annotationPojo.setValue(annotation.value);
277 .setSecondaryStructure(annotation.secondaryStructure);
278 annotationPojo.setDisplayCharacter(annotation.displayCharacter);
279 alignAnnotPojo.getAnnotations().add(annotationPojo);
283 alignAnnotPojo.getAnnotations().add(annotationPojo);
286 jsonAnnotations.add(alignAnnotPojo);
288 return jsonAnnotations;
291 @SuppressWarnings("unchecked")
292 public JSONFile parse(String jsonAlignmentString)
296 JSONParser jsonParser = new JSONParser();
297 JSONObject alignmentJsonObj = (JSONObject) jsonParser
298 .parse(jsonAlignmentString);
299 JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
300 JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj
301 .get("alignAnnotation");
302 JSONArray jsonSeqArray = (JSONArray) alignmentJsonObj
304 JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
306 JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
307 .get("jalviewSettings");
309 if (jvSettingsJsonObj != null)
311 String jsColourScheme = (String) jvSettingsJsonObj
312 .get("globalColorScheme");
313 Boolean showFeatures = Boolean.valueOf(jvSettingsJsonObj.get(
314 "showSeqFeatures").toString());
315 setColourScheme(getJalviewColorScheme(jsColourScheme));
316 setShowSeqFeatures(showFeatures);
319 seqMap = new Hashtable<String, Sequence>();
320 for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
323 JSONObject sequence = sequenceIter.next();
324 String sequcenceString = sequence.get("seq").toString();
325 String sequenceName = sequence.get("name").toString();
326 String seqUniqueId = sequence.get("id").toString();
327 int start = Integer.valueOf(sequence.get("start").toString());
328 int end = Integer.valueOf(sequence.get("end").toString());
329 Sequence seq = new Sequence(sequenceName, sequcenceString, start,
332 seqMap.put(seqUniqueId, seq);
334 parseFeatures(jsonSeqArray);
336 for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray.iterator(); seqGrpIter
339 JSONObject seqGrpObj = seqGrpIter.next();
340 String grpName = seqGrpObj.get("groupName").toString();
341 String colourScheme = seqGrpObj.get("colourScheme").toString();
342 String description = (seqGrpObj.get("description") == null) ? null
343 : seqGrpObj.get("description").toString();
344 boolean displayBoxes = Boolean.valueOf(seqGrpObj
345 .get("displayBoxes").toString());
346 boolean displayText = Boolean.valueOf(seqGrpObj.get("displayText")
348 boolean colourText = Boolean.valueOf(seqGrpObj.get("colourText")
350 boolean showNonconserved = Boolean.valueOf(seqGrpObj.get(
351 "showNonconserved").toString());
352 int startRes = Integer
353 .valueOf(seqGrpObj.get("startRes").toString());
354 int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
355 JSONArray seqsHashArray = (JSONArray) seqGrpObj.get("seqsHash");
357 ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
358 if (seqsHashArray.size() > 0)
360 Iterator<String> seqHashIter = seqsHashArray.iterator();
361 while (seqHashIter.hasNext())
363 String seqHash = seqHashIter.next();
364 Sequence sequence = seqMap.get(seqHash);
365 if (sequence != null)
367 grpSeqs.add(sequence);
371 ColourSchemeI scheme = getJalviewColorScheme(colourScheme);
372 SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, scheme,
373 displayBoxes, displayText, colourText, startRes, endRes);
374 seqGrp.setShowNonconserved(showNonconserved);
375 seqGrp.setDescription(description);
376 this.seqGroups.add(seqGrp);
380 for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
383 JSONObject alAnnot = alAnnotIter.next();
384 JSONArray annotJsonArray = (JSONArray) alAnnot.get("annotations");
385 Annotation[] annotations = new Annotation[annotJsonArray.size()];
387 for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
390 JSONObject annot = annotIter.next();
393 annotations[count] = null;
397 float val = annot.get("value") == null ? null : Float
398 .valueOf(annot.get("value").toString());
399 String desc = annot.get("description") == null ? null : annot
400 .get("description").toString();
402 char ss = annot.get("secondaryStructure") == null ? ' '
403 : annot.get("secondaryStructure").toString().charAt(0);
404 String displayChar = annot.get("displayCharacter").toString();
406 annotations[count] = new Annotation(displayChar, desc, ss, val);
411 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(alAnnot
412 .get("label").toString(), alAnnot.get("description")
413 .toString(), annotations);
414 this.annotations.add(alignAnnot);
417 } catch (Exception e)
425 @SuppressWarnings("unchecked")
426 private void parseFeatures(JSONArray jsonSeqFeatures)
428 if (jsonSeqFeatures != null)
430 for (Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
433 JSONObject jsonFeature = seqFeatureItr.next();
434 Long begin = (Long) jsonFeature.get("xStart");
435 Long end = (Long) jsonFeature.get("xEnd");
436 String type = (String) jsonFeature.get("type");
437 String featureGrp = (String) jsonFeature.get("featureGroup");
438 String descripiton = (String) jsonFeature.get("description");
439 String seqRef = (String) jsonFeature.get("sequenceRef");
440 Float score = Float.valueOf(jsonFeature.get("score").toString());
441 // Hashtable otherDetails = (Hashtable) jsonFeature
442 // .get("otherDetails");
443 // sequenceFeature.otherDetails = otherDetails;
445 Sequence seq = seqMap.get(seqRef);
446 SequenceFeature sequenceFeature = new SequenceFeature();
447 JSONArray linksJsonArray = (JSONArray) jsonFeature.get("links");
448 if (linksJsonArray != null && linksJsonArray.size() > 0)
450 Iterator<String> linkList = linksJsonArray.iterator();
451 while (linkList.hasNext())
453 String link = linkList.next();
454 sequenceFeature.addLink(link);
457 sequenceFeature.setFeatureGroup(featureGrp);
458 sequenceFeature.setScore(score);
459 sequenceFeature.setDescription(descripiton);
460 sequenceFeature.setType(type);
461 sequenceFeature.setBegin(seq.findPosition(begin.intValue()));
462 sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1);
463 seq.addSequenceFeature(sequenceFeature);
468 public static ColourSchemeI getJalviewColorScheme(
469 String bioJsColourSchemeName)
471 ColourSchemeI jalviewColor = null;
472 for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper
475 if (cs.getBioJsName().equalsIgnoreCase(bioJsColourSchemeName))
477 jalviewColor = cs.getJvColourScheme();
484 public void applySettingsToAlignFrame(AlignViewControllerGuiI af)
486 af.setShowSeqFeatures(isShowSeqFeatures());
487 af.changeColour(getColourScheme());
488 af.setMenusForViewport();
491 public String getGlobalColorScheme()
493 return globalColorScheme;
496 public void setGlobalColorScheme(String globalColorScheme)
498 this.globalColorScheme = globalColorScheme;
501 public ColourSchemeI getColourScheme()
506 public void setColourScheme(ColourSchemeI colourScheme)
508 this.colourScheme = colourScheme;
511 public FeaturesDisplayedI getDisplayedFeatures()
513 return displayedFeatures;
516 public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures)
518 this.displayedFeatures = displayedFeatures;
521 public JSONExportSettings getJsonExportSettings()
523 return jsonExportSettings;
526 public void setJsonExportSettings(JSONExportSettings jsonExportSettings)
528 this.jsonExportSettings = jsonExportSettings;
531 public static String getJSONData(AlignViewportI av)
533 JSONFile jsonFile = new JSONFile();
534 jsonFile.setViewport(av);
535 jsonFile.seqGroups = av.getAlignment().getGroups();
536 jsonFile.setDisplayedFeatures(av.getFeaturesDisplayed());
538 for (SequenceI seq : av.getAlignment().getSequences())
540 jsonFile.seqs.add(seq);
543 // Add non auto calculated annotation to AlignFile
544 for (AlignmentAnnotation annot : av.getAlignment()
545 .getAlignmentAnnotation())
547 if (annot != null && !annot.autoCalculated)
549 if (annot.label.equals("PDB.CATempFactor"))
553 jsonFile.annotations.add(annot);
556 String jsonString = jsonFile.print();
560 public boolean isShowSeqFeatures()
562 return showSeqFeatures;
565 public void setShowSeqFeatures(boolean showSeqFeatures)
567 this.showSeqFeatures = showSeqFeatures;
570 public Vector<AlignmentAnnotation> getAnnotations()
575 public class JSONExportSettings
577 private boolean exportSequence;
579 private boolean exportSequenceFeatures;
581 private boolean exportAnnotations;
583 private boolean exportGroups;
585 private boolean exportJalviewSettings;
587 public boolean isExportSequence()
589 return exportSequence;
592 public void setExportSequence(boolean exportSequence)
594 this.exportSequence = exportSequence;
597 public boolean isExportSequenceFeatures()
599 return exportSequenceFeatures;
602 public void setExportSequenceFeatures(boolean exportSequenceFeatures)
604 this.exportSequenceFeatures = exportSequenceFeatures;
607 public boolean isExportAnnotations()
609 return exportAnnotations;
612 public void setExportAnnotations(boolean exportAnnotations)
614 this.exportAnnotations = exportAnnotations;
617 public boolean isExportGroups()
622 public void setExportGroups(boolean exportGroups)
624 this.exportGroups = exportGroups;
627 public boolean isExportJalviewSettings()
629 return exportJalviewSettings;
632 public void setExportJalviewSettings(boolean exportJalviewSettings)
634 this.exportJalviewSettings = exportJalviewSettings;