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.AlignExportSettingI;
25 import jalview.api.AlignViewportI;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.api.ComplexAlignFile;
28 import jalview.api.FeatureRenderer;
29 import jalview.api.FeaturesDisplayedI;
30 import jalview.bin.BuildDetails;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.Annotation;
34 import jalview.datamodel.ColumnSelection;
35 import jalview.datamodel.HiddenSequences;
36 import jalview.datamodel.Sequence;
37 import jalview.datamodel.SequenceFeature;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.datamodel.SequenceI;
40 import jalview.json.binding.biojson.v1.AlignmentAnnotationPojo;
41 import jalview.json.binding.biojson.v1.AlignmentPojo;
42 import jalview.json.binding.biojson.v1.AnnotationPojo;
43 import jalview.json.binding.biojson.v1.JalviewBioJsColorSchemeMapper;
44 import jalview.json.binding.biojson.v1.SequenceFeaturesPojo;
45 import jalview.json.binding.biojson.v1.SequenceGrpPojo;
46 import jalview.json.binding.biojson.v1.SequencePojo;
47 import jalview.schemes.ColourSchemeI;
48 import jalview.schemes.ColourSchemeProperty;
49 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
51 import java.awt.Color;
52 import java.io.IOException;
53 import java.io.Reader;
54 import java.util.ArrayList;
55 import java.util.Hashtable;
56 import java.util.Iterator;
57 import java.util.List;
58 import java.util.Vector;
60 import org.json.simple.JSONArray;
61 import org.json.simple.JSONObject;
62 import org.json.simple.parser.JSONParser;
64 public class JSONFile extends AlignFile implements ComplexAlignFile
66 private ColourSchemeI colourScheme;
68 private static String version = new BuildDetails().getVersion();
70 private String webstartUrl = "http://www.jalview.org/services/launchApp";
72 private String application = "Jalview";
74 public static final String FILE_EXT = "json";
76 public static final String FILE_DESC = "JSON";
78 private String globalColorScheme;
80 private boolean showSeqFeatures;
82 private Hashtable<String, Sequence> seqMap;
84 private FeaturesDisplayedI displayedFeatures;
86 private FeatureRenderer fr;
88 private List<int[]> hiddenColumns;
90 private ColumnSelection columnSelection;
92 private List<String> hiddenSeqRefs;
94 private ArrayList<SequenceI> hiddenSequences;
101 public JSONFile(FileParse source) throws IOException
106 public JSONFile(String inFile, String type) throws IOException
112 public void parse() throws IOException
119 public String print()
121 String jsonOutput = null;
124 AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
125 AlignExportSettingI exportSettings = getExportSettings();
127 // if no export settings were supplied use the following with all values
128 // defaulting to true
129 if (exportSettings == null)
131 exportSettings = new AlignExportSettingI()
134 public boolean isExportHiddenSequences()
140 public boolean isExportHiddenColumns()
146 public boolean isExportGroups()
152 public boolean isExportFeatures()
158 public boolean isExportAnnotations()
166 for (SequenceI seq : seqs)
168 StringBuilder name = new StringBuilder();
169 name.append(seq.getName()).append("/").append(seq.getStart())
170 .append("-").append(seq.getEnd());
171 SequencePojo jsonSeqPojo = new SequencePojo();
172 jsonSeqPojo.setId(String.valueOf(seq.hashCode()));
173 jsonSeqPojo.setOrder(++count);
174 jsonSeqPojo.setEnd(seq.getEnd());
175 jsonSeqPojo.setStart(seq.getStart());
176 jsonSeqPojo.setName(name.toString());
177 jsonSeqPojo.setSeq(seq.getSequenceAsString());
178 jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
180 jsonAlignmentPojo.setGlobalColorScheme(globalColorScheme);
181 jsonAlignmentPojo.getAppSettings().put("application", application);
182 jsonAlignmentPojo.getAppSettings().put("version", version);
183 jsonAlignmentPojo.getAppSettings().put("webStartUrl", webstartUrl);
184 jsonAlignmentPojo.getAppSettings().put("showSeqFeatures",
185 String.valueOf(showSeqFeatures));
187 String[] hiddenSections = getHiddenSections();
188 if (hiddenSections != null)
190 if (hiddenSections[0] != null
191 && exportSettings.isExportHiddenColumns())
193 jsonAlignmentPojo.getAppSettings().put("hiddenCols",
194 String.valueOf(hiddenSections[0]));
196 if (hiddenSections[1] != null
197 && exportSettings.isExportHiddenSequences())
199 jsonAlignmentPojo.getAppSettings().put("hiddenSeqs",
200 String.valueOf(hiddenSections[1]));
204 if (exportSettings.isExportAnnotations())
207 .setAlignAnnotation(annotationToJsonPojo(annotations));
210 if (exportSettings.isExportFeatures())
213 .setSeqFeatures(sequenceFeatureToJsonPojo(seqs, fr));
216 if (exportSettings.isExportGroups() && seqGroups != null
217 && seqGroups.size() > 0)
219 for (SequenceGroup seqGrp : seqGroups)
221 SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
222 seqGrpPojo.setGroupName(seqGrp.getName());
223 seqGrpPojo.setColourScheme(ColourSchemeProperty
224 .getColourName(seqGrp.cs));
225 seqGrpPojo.setColourText(seqGrp.getColourText());
226 seqGrpPojo.setDescription(seqGrp.getDescription());
227 seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
228 seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
229 seqGrpPojo.setEndRes(seqGrp.getEndRes());
230 seqGrpPojo.setStartRes(seqGrp.getStartRes());
231 seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
232 for (SequenceI seq : seqGrp.getSequences())
234 seqGrpPojo.getSequenceRefs()
235 .add(String.valueOf(seq.hashCode()));
237 jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo);
240 org.json.JSONObject generatedJSon = new org.json.JSONObject(
242 jsonOutput = generatedJSon.toString();
243 return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend",
245 } catch (Exception e)
252 public String[] getHiddenSections()
254 String[] hiddenSections = new String[2];
255 if (getViewport() == null)
260 // hidden column business
261 if (getViewport().hasHiddenColumns())
263 List<int[]> hiddenCols = getViewport().getColumnSelection()
265 StringBuilder hiddenColsBuilder = new StringBuilder();
266 for (int[] range : hiddenCols)
268 hiddenColsBuilder.append(";").append(range[0]).append("-")
272 hiddenColsBuilder.deleteCharAt(0);
273 hiddenSections[0] = hiddenColsBuilder.toString();
276 // hidden rows/seqs business
277 HiddenSequences hiddenSeqsObj = getViewport().getAlignment()
278 .getHiddenSequences();
279 if (hiddenSeqsObj == null || hiddenSeqsObj.hiddenSequences == null)
281 return hiddenSections;
284 SequenceI[] hiddenSeqs = hiddenSeqsObj.hiddenSequences;
285 StringBuilder hiddenSeqsBuilder = new StringBuilder();
286 for (SequenceI hiddenSeq : hiddenSeqs)
288 if (hiddenSeq != null)
290 hiddenSeqsBuilder.append(";").append(hiddenSeq.hashCode());
293 if (hiddenSeqsBuilder.length() > 0)
295 hiddenSeqsBuilder.deleteCharAt(0);
297 hiddenSections[1] = hiddenSeqsBuilder.toString();
299 return hiddenSections;
302 public List<SequenceFeaturesPojo> sequenceFeatureToJsonPojo(
303 List<SequenceI> seqs, FeatureRenderer fr)
305 displayedFeatures = (fr == null) ? null : fr.getFeaturesDisplayed();
306 List<SequenceFeaturesPojo> sequenceFeaturesPojo = new ArrayList<SequenceFeaturesPojo>();
307 for (SequenceI seq : seqs)
309 SequenceI dataSetSequence = seq.getDatasetSequence();
310 SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null
311 : seq.getDatasetSequence().getSequenceFeatures();
313 seqFeatures = (seqFeatures == null) ? seq.getSequenceFeatures()
315 if (seqFeatures == null)
320 for (SequenceFeature sf : seqFeatures)
322 if (displayedFeatures != null
323 && displayedFeatures.isVisible(sf.getType()))
325 SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
326 String.valueOf(seq.hashCode()));
328 String featureColour = (fr == null) ? null : jalview.util.Format
329 .getHexString(fr.findFeatureColour(Color.white, seq,
330 seq.findIndex(sf.getBegin())));
331 jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
332 jsonFeature.setXend(seq.findIndex(sf.getEnd()));
333 jsonFeature.setType(sf.getType());
334 jsonFeature.setDescription(sf.getDescription());
335 jsonFeature.setLinks(sf.links);
336 jsonFeature.setOtherDetails(sf.otherDetails);
337 jsonFeature.setScore(sf.getScore());
338 jsonFeature.setFillColor(featureColour);
339 jsonFeature.setFeatureGroup(sf.getFeatureGroup());
340 sequenceFeaturesPojo.add(jsonFeature);
344 return sequenceFeaturesPojo;
347 public static List<AlignmentAnnotationPojo> annotationToJsonPojo(
348 Vector<AlignmentAnnotation> annotations)
350 List<AlignmentAnnotationPojo> jsonAnnotations = new ArrayList<AlignmentAnnotationPojo>();
351 if (annotations == null)
353 return jsonAnnotations;
355 for (AlignmentAnnotation annot : annotations)
357 AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
358 alignAnnotPojo.setDescription(annot.description);
359 alignAnnotPojo.setLabel(annot.label);
360 for (Annotation annotation : annot.annotations)
362 AnnotationPojo annotationPojo = new AnnotationPojo();
363 if (annotation != null)
365 annotationPojo.setDescription(annotation.description);
366 annotationPojo.setValue(annotation.value);
368 .setSecondaryStructure(annotation.secondaryStructure);
369 annotationPojo.setDisplayCharacter(annotation.displayCharacter);
370 alignAnnotPojo.getAnnotations().add(annotationPojo);
374 alignAnnotPojo.getAnnotations().add(annotationPojo);
377 jsonAnnotations.add(alignAnnotPojo);
379 return jsonAnnotations;
382 @SuppressWarnings("unchecked")
383 public JSONFile parse(Reader jsonAlignmentString)
387 JSONParser jsonParser = new JSONParser();
388 JSONObject alignmentJsonObj = (JSONObject) jsonParser
389 .parse(jsonAlignmentString);
390 JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
391 JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj
392 .get("alignAnnotation");
393 JSONArray jsonSeqArray = (JSONArray) alignmentJsonObj
395 JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
397 JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
400 if (jvSettingsJsonObj != null)
402 String jsColourScheme = (String) jvSettingsJsonObj
403 .get("globalColorScheme");
404 Boolean showFeatures = Boolean.valueOf(jvSettingsJsonObj.get(
405 "showSeqFeatures").toString());
406 setColourScheme(getJalviewColorScheme(jsColourScheme));
407 setShowSeqFeatures(showFeatures);
408 parseHiddenSeqRefsAsList(jvSettingsJsonObj);
409 parseHiddenCols(jvSettingsJsonObj);
412 hiddenSequences = new ArrayList<SequenceI>();
413 seqMap = new Hashtable<String, Sequence>();
414 for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
417 JSONObject sequence = sequenceIter.next();
418 String sequcenceString = sequence.get("seq").toString();
419 String sequenceName = sequence.get("name").toString();
420 String seqUniqueId = sequence.get("id").toString();
421 int start = Integer.valueOf(sequence.get("start").toString());
422 int end = Integer.valueOf(sequence.get("end").toString());
423 Sequence seq = new Sequence(sequenceName, sequcenceString, start,
425 if (hiddenSeqRefs != null && hiddenSeqRefs.contains(seqUniqueId))
427 hiddenSequences.add(seq);
430 seqMap.put(seqUniqueId, seq);
432 parseFeatures(jsonSeqArray);
434 for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray.iterator(); seqGrpIter
437 JSONObject seqGrpObj = seqGrpIter.next();
438 String grpName = seqGrpObj.get("groupName").toString();
439 String colourScheme = seqGrpObj.get("colourScheme").toString();
440 String description = (seqGrpObj.get("description") == null) ? null
441 : seqGrpObj.get("description").toString();
442 boolean displayBoxes = Boolean.valueOf(seqGrpObj
443 .get("displayBoxes").toString());
444 boolean displayText = Boolean.valueOf(seqGrpObj.get("displayText")
446 boolean colourText = Boolean.valueOf(seqGrpObj.get("colourText")
448 boolean showNonconserved = Boolean.valueOf(seqGrpObj.get(
449 "showNonconserved").toString());
450 int startRes = Integer
451 .valueOf(seqGrpObj.get("startRes").toString());
452 int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
453 JSONArray sequenceRefs = (JSONArray) seqGrpObj.get("sequenceRefs");
455 ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
456 if (sequenceRefs.size() > 0)
458 Iterator<String> seqHashIter = sequenceRefs.iterator();
459 while (seqHashIter.hasNext())
461 String seqHash = seqHashIter.next();
462 Sequence sequence = seqMap.get(seqHash);
463 if (sequence != null)
465 grpSeqs.add(sequence);
469 ColourSchemeI grpColourScheme = getJalviewColorScheme(colourScheme);
470 SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName,
471 grpColourScheme, displayBoxes, displayText, colourText,
473 seqGrp.setShowNonconserved(showNonconserved);
474 seqGrp.setDescription(description);
475 this.seqGroups.add(seqGrp);
479 for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
482 JSONObject alAnnot = alAnnotIter.next();
483 JSONArray annotJsonArray = (JSONArray) alAnnot.get("annotations");
484 Annotation[] annotations = new Annotation[annotJsonArray.size()];
486 for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
489 JSONObject annot = annotIter.next();
492 annotations[count] = null;
496 float val = annot.get("value") == null ? null : Float
497 .valueOf(annot.get("value").toString());
498 String desc = annot.get("description") == null ? null : annot
499 .get("description").toString();
501 char ss = annot.get("secondaryStructure") == null ? ' ' : annot
502 .get("secondaryStructure").toString().charAt(0);
503 String displayChar = annot.get("displayCharacter") == null ? ""
504 : annot.get("displayCharacter").toString();
506 annotations[count] = new Annotation(displayChar, desc, ss, val);
511 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(alAnnot
512 .get("label").toString(), alAnnot.get("description")
513 .toString(), annotations);
514 this.annotations.add(alignAnnot);
517 } catch (Exception e)
524 public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson)
526 hiddenSeqRefs = new ArrayList<String>();
527 String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs");
528 if (hiddenSeqs != null && !hiddenSeqs.isEmpty())
530 String[] seqRefs = hiddenSeqs.split(";");
531 for (String seqRef : seqRefs)
533 hiddenSeqRefs.add(seqRef);
538 public void parseHiddenCols(JSONObject jvSettingsJson)
540 String hiddenCols = (String) jvSettingsJson.get("hiddenCols");
541 if (hiddenCols != null && !hiddenCols.isEmpty())
543 columnSelection = new ColumnSelection();
544 String[] rangeStrings = hiddenCols.split(";");
545 for (String rangeString : rangeStrings)
547 String[] range = rangeString.split("-");
548 columnSelection.hideColumns(Integer.valueOf(range[0]),
549 Integer.valueOf(range[1]));
554 @SuppressWarnings("unchecked")
555 private void parseFeatures(JSONArray jsonSeqFeatures)
557 if (jsonSeqFeatures != null)
559 displayedFeatures = new FeaturesDisplayed();
560 for (Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
563 JSONObject jsonFeature = seqFeatureItr.next();
564 Long begin = (Long) jsonFeature.get("xStart");
565 Long end = (Long) jsonFeature.get("xEnd");
566 String type = (String) jsonFeature.get("type");
567 String featureGrp = (String) jsonFeature.get("featureGroup");
568 String descripiton = (String) jsonFeature.get("description");
569 String seqRef = (String) jsonFeature.get("sequenceRef");
570 Float score = Float.valueOf(jsonFeature.get("score").toString());
572 Sequence seq = seqMap.get(seqRef);
573 SequenceFeature sequenceFeature = new SequenceFeature();
574 JSONArray linksJsonArray = (JSONArray) jsonFeature.get("links");
575 if (linksJsonArray != null && linksJsonArray.size() > 0)
577 Iterator<String> linkList = linksJsonArray.iterator();
578 while (linkList.hasNext())
580 String link = linkList.next();
581 sequenceFeature.addLink(link);
584 sequenceFeature.setFeatureGroup(featureGrp);
585 sequenceFeature.setScore(score);
586 sequenceFeature.setDescription(descripiton);
587 sequenceFeature.setType(type);
588 sequenceFeature.setBegin(seq.findPosition(begin.intValue()));
589 sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1);
590 seq.addSequenceFeature(sequenceFeature);
591 displayedFeatures.setVisible(type);
596 public static ColourSchemeI getJalviewColorScheme(
597 String bioJsColourSchemeName)
599 ColourSchemeI jalviewColor = null;
600 for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper
603 if (cs.getBioJsName().equalsIgnoreCase(bioJsColourSchemeName))
605 jalviewColor = cs.getJvColourScheme();
612 public String getGlobalColorScheme()
614 return globalColorScheme;
617 public void setGlobalColorScheme(String globalColorScheme)
619 this.globalColorScheme = globalColorScheme;
622 public ColourSchemeI getColourScheme()
627 public void setColourScheme(ColourSchemeI colourScheme)
629 this.colourScheme = colourScheme;
633 public FeaturesDisplayedI getDisplayedFeatures()
635 return displayedFeatures;
638 public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures)
640 this.displayedFeatures = displayedFeatures;
643 public void configureForView(AlignmentViewPanel avpanel)
645 super.configureForView(avpanel);
646 AlignViewportI viewport = avpanel.getAlignViewport();
647 AlignmentI alignment = viewport.getAlignment();
648 AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
650 seqGroups = alignment.getGroups();
651 fr = avpanel.cloneFeatureRenderer();
653 // Add non auto calculated annotation to AlignFile
654 for (AlignmentAnnotation annot : annots)
656 if (annot != null && !annot.autoCalculated)
662 annotations.add(annot);
665 globalColorScheme = ColourSchemeProperty.getColourName(viewport
666 .getGlobalColourScheme());
667 setDisplayedFeatures(viewport.getFeaturesDisplayed());
668 showSeqFeatures = viewport.isShowSequenceFeatures();
672 public boolean isShowSeqFeatures()
674 return showSeqFeatures;
677 public void setShowSeqFeatures(boolean showSeqFeatures)
679 this.showSeqFeatures = showSeqFeatures;
682 public Vector<AlignmentAnnotation> getAnnotations()
687 public List<int[]> getHiddenColumns()
689 return hiddenColumns;
692 public ColumnSelection getColumnSelection()
694 return columnSelection;
697 public void setColumnSelection(ColumnSelection columnSelection)
699 this.columnSelection = columnSelection;
702 public SequenceI[] getHiddenSequences()
704 if (hiddenSequences == null || hiddenSequences.isEmpty())
706 return new SequenceI[] {};
708 synchronized (hiddenSequences)
710 return hiddenSequences.toArray(new SequenceI[hiddenSequences.size()]);
714 public void setHiddenSequences(ArrayList<SequenceI> hiddenSequences)
716 this.hiddenSequences = hiddenSequences;
719 public class JSONExportSettings
721 private boolean exportSequence;
723 private boolean exportSequenceFeatures;
725 private boolean exportAnnotations;
727 private boolean exportGroups;
729 private boolean exportJalviewSettings;
731 public boolean isExportSequence()
733 return exportSequence;
736 public void setExportSequence(boolean exportSequence)
738 this.exportSequence = exportSequence;
741 public boolean isExportSequenceFeatures()
743 return exportSequenceFeatures;
746 public void setExportSequenceFeatures(boolean exportSequenceFeatures)
748 this.exportSequenceFeatures = exportSequenceFeatures;
751 public boolean isExportAnnotations()
753 return exportAnnotations;
756 public void setExportAnnotations(boolean exportAnnotations)
758 this.exportAnnotations = exportAnnotations;
761 public boolean isExportGroups()
766 public void setExportGroups(boolean exportGroups)
768 this.exportGroups = exportGroups;
771 public boolean isExportJalviewSettings()
773 return exportJalviewSettings;
776 public void setExportJalviewSettings(boolean exportJalviewSettings)
778 this.exportJalviewSettings = exportJalviewSettings;