Merge branch 'develop' into features/JAL-2446NCList
[jalview.git] / src / jalview / io / JSONFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21
22 package jalview.io;
23
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.FeatureSettingsModelI;
30 import jalview.api.FeaturesDisplayedI;
31 import jalview.bin.BuildDetails;
32 import jalview.datamodel.AlignmentAnnotation;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.Annotation;
35 import jalview.datamodel.HiddenColumns;
36 import jalview.datamodel.HiddenSequences;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceFeature;
39 import jalview.datamodel.SequenceGroup;
40 import jalview.datamodel.SequenceI;
41 import jalview.json.binding.biojson.v1.AlignmentAnnotationPojo;
42 import jalview.json.binding.biojson.v1.AlignmentPojo;
43 import jalview.json.binding.biojson.v1.AnnotationDisplaySettingPojo;
44 import jalview.json.binding.biojson.v1.AnnotationPojo;
45 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
46 import jalview.json.binding.biojson.v1.SequenceFeaturesPojo;
47 import jalview.json.binding.biojson.v1.SequenceGrpPojo;
48 import jalview.json.binding.biojson.v1.SequencePojo;
49 import jalview.renderer.seqfeatures.FeatureColourFinder;
50 import jalview.schemes.ColourSchemeProperty;
51 import jalview.schemes.JalviewColourScheme;
52 import jalview.schemes.ResidueColourScheme;
53 import jalview.util.ColorUtils;
54 import jalview.util.Format;
55 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
56
57 import java.awt.Color;
58 import java.io.IOException;
59 import java.io.Reader;
60 import java.util.ArrayList;
61 import java.util.Hashtable;
62 import java.util.Iterator;
63 import java.util.List;
64 import java.util.Vector;
65
66 import org.json.simple.JSONArray;
67 import org.json.simple.JSONObject;
68 import org.json.simple.parser.JSONParser;
69
70 public class JSONFile extends AlignFile implements ComplexAlignFile
71 {
72   private static String version = new BuildDetails().getVersion();
73
74   private String webstartUrl = "http://www.jalview.org/services/launchApp";
75
76   private String application = "Jalview";
77
78   private String globalColourScheme;
79
80   private boolean showSeqFeatures;
81
82   private Hashtable<String, Sequence> seqMap;
83
84   private FeaturesDisplayedI displayedFeatures;
85
86   private FeatureRenderer fr;
87
88   private HiddenColumns hiddenColumns;
89
90   private List<String> hiddenSeqRefs;
91
92   private ArrayList<SequenceI> hiddenSequences;
93
94   private final static String TCOFFEE_SCORE = "TCoffeeScore";
95
96   public JSONFile()
97   {
98     super();
99   }
100
101   public JSONFile(FileParse source) throws IOException
102   {
103     super(source);
104   }
105
106   public JSONFile(String inFile, DataSourceType sourceType)
107           throws IOException
108   {
109     super(inFile, sourceType);
110   }
111
112   @Override
113   public void parse() throws IOException
114   {
115     parse(getReader());
116
117   }
118
119   @Override
120   public String print(SequenceI[] sqs, boolean jvsuffix)
121   {
122     String jsonOutput = null;
123     try
124     {
125       AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
126       AlignExportSettingI exportSettings = getExportSettings();
127
128       // if no export settings were supplied use the following with all values
129       // defaulting to true
130       if (exportSettings == null)
131       {
132         exportSettings = new AlignExportSettingI()
133         {
134           @Override
135           public boolean isExportHiddenSequences()
136           {
137             return true;
138           }
139
140           @Override
141           public boolean isExportHiddenColumns()
142           {
143             return true;
144           }
145
146           @Override
147           public boolean isExportGroups()
148           {
149             return true;
150           }
151
152           @Override
153           public boolean isExportFeatures()
154           {
155             return true;
156           }
157
158           @Override
159           public boolean isExportAnnotations()
160           {
161             return true;
162           }
163
164           @Override
165           public boolean isCancelled()
166           {
167             return false;
168           }
169         };
170       }
171
172       int count = 0;
173       for (SequenceI seq : sqs)
174       {
175         StringBuilder name = new StringBuilder();
176         name.append(seq.getName()).append("/").append(seq.getStart())
177                 .append("-").append(seq.getEnd());
178         SequencePojo jsonSeqPojo = new SequencePojo();
179         jsonSeqPojo.setId(String.valueOf(seq.hashCode()));
180         jsonSeqPojo.setOrder(++count);
181         jsonSeqPojo.setEnd(seq.getEnd());
182         jsonSeqPojo.setStart(seq.getStart());
183         jsonSeqPojo.setName(name.toString());
184         jsonSeqPojo.setSeq(seq.getSequenceAsString());
185         jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
186       }
187       jsonAlignmentPojo.setGlobalColorScheme(globalColourScheme);
188       jsonAlignmentPojo.getAppSettings().put("application", application);
189       jsonAlignmentPojo.getAppSettings().put("version", version);
190       jsonAlignmentPojo.getAppSettings().put("webStartUrl", webstartUrl);
191       jsonAlignmentPojo.getAppSettings().put("showSeqFeatures",
192               String.valueOf(showSeqFeatures));
193
194       String[] hiddenSections = getHiddenSections();
195       if (hiddenSections != null)
196       {
197         if (hiddenSections[0] != null
198                 && exportSettings.isExportHiddenColumns())
199         {
200           jsonAlignmentPojo.getAppSettings().put("hiddenCols",
201                   String.valueOf(hiddenSections[0]));
202         }
203         if (hiddenSections[1] != null
204                 && exportSettings.isExportHiddenSequences())
205         {
206           jsonAlignmentPojo.getAppSettings().put("hiddenSeqs",
207                   String.valueOf(hiddenSections[1]));
208         }
209       }
210
211       if (exportSettings.isExportAnnotations())
212       {
213         jsonAlignmentPojo
214                 .setAlignAnnotation(annotationToJsonPojo(annotations));
215       }
216       else
217       {
218         // These color schemes require annotation, disable them if annotations
219         // are not exported
220         if (globalColourScheme
221                 .equalsIgnoreCase(JalviewColourScheme.RNAHelices.toString())
222                 || globalColourScheme
223                         .equalsIgnoreCase(JalviewColourScheme.TCoffee
224                                 .toString()))
225         {
226           jsonAlignmentPojo.setGlobalColorScheme(ResidueColourScheme.NONE);
227         }
228       }
229
230       if (exportSettings.isExportFeatures())
231       {
232         jsonAlignmentPojo.setSeqFeatures(sequenceFeatureToJsonPojo(sqs));
233       }
234
235       if (exportSettings.isExportGroups() && seqGroups != null
236               && seqGroups.size() > 0)
237       {
238         for (SequenceGroup seqGrp : seqGroups)
239         {
240           SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
241           seqGrpPojo.setGroupName(seqGrp.getName());
242           seqGrpPojo.setColourScheme(ColourSchemeProperty
243                   .getColourName(seqGrp.getColourScheme()));
244           seqGrpPojo.setColourText(seqGrp.getColourText());
245           seqGrpPojo.setDescription(seqGrp.getDescription());
246           seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
247           seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
248           seqGrpPojo.setEndRes(seqGrp.getEndRes());
249           seqGrpPojo.setStartRes(seqGrp.getStartRes());
250           seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
251           for (SequenceI seq : seqGrp.getSequences())
252           {
253             seqGrpPojo.getSequenceRefs()
254                     .add(String.valueOf(seq.hashCode()));
255           }
256           jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo);
257         }
258       }
259       org.json.JSONObject generatedJSon = new org.json.JSONObject(
260               jsonAlignmentPojo);
261       jsonOutput = generatedJSon.toString();
262       return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend",
263               "xEnd");
264     } catch (Exception e)
265     {
266       e.printStackTrace();
267     }
268     return jsonOutput;
269   }
270
271   public String[] getHiddenSections()
272   {
273     String[] hiddenSections = new String[2];
274     if (getViewport() == null)
275     {
276       return null;
277     }
278
279     // hidden column business
280     if (getViewport().hasHiddenColumns())
281     {
282       hiddenSections[0] = getViewport().getAlignment().getHiddenColumns()
283               .regionsToString(";", "-");
284     }
285
286     // hidden rows/seqs business
287     HiddenSequences hiddenSeqsObj = getViewport().getAlignment()
288             .getHiddenSequences();
289     if (hiddenSeqsObj == null || hiddenSeqsObj.hiddenSequences == null)
290     {
291       return hiddenSections;
292     }
293
294     SequenceI[] hiddenSeqs = hiddenSeqsObj.hiddenSequences;
295     StringBuilder hiddenSeqsBuilder = new StringBuilder();
296     for (SequenceI hiddenSeq : hiddenSeqs)
297     {
298       if (hiddenSeq != null)
299       {
300         hiddenSeqsBuilder.append(";").append(hiddenSeq.hashCode());
301       }
302     }
303     if (hiddenSeqsBuilder.length() > 0)
304     {
305       hiddenSeqsBuilder.deleteCharAt(0);
306     }
307     hiddenSections[1] = hiddenSeqsBuilder.toString();
308
309     return hiddenSections;
310   }
311
312   protected List<SequenceFeaturesPojo> sequenceFeatureToJsonPojo(
313           SequenceI[] sqs)
314   {
315     displayedFeatures = (fr == null) ? null : fr.getFeaturesDisplayed();
316     List<SequenceFeaturesPojo> sequenceFeaturesPojo = new ArrayList<>();
317     if (sqs == null)
318     {
319       return sequenceFeaturesPojo;
320     }
321
322     FeatureColourFinder finder = new FeatureColourFinder(fr);
323
324     String[] visibleFeatureTypes = displayedFeatures == null ? null
325             : displayedFeatures.getVisibleFeatures().toArray(
326                     new String[displayedFeatures.getVisibleFeatureCount()]);
327
328     for (SequenceI seq : sqs)
329     {
330       /*
331        * get all features currently visible (and any non-positional features)
332        */
333       List<SequenceFeature> seqFeatures = seq.getFeatures().getAllFeatures(
334               visibleFeatureTypes);
335       for (SequenceFeature sf : seqFeatures)
336       {
337         SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
338                 String.valueOf(seq.hashCode()));
339
340         String featureColour = (fr == null) ? null : Format
341                 .getHexString(finder.findFeatureColour(Color.white, seq,
342                         seq.findIndex(sf.getBegin())));
343         int xStart = sf.getBegin() == 0 ? 0
344                 : seq.findIndex(sf.getBegin()) - 1;
345         int xEnd = sf.getEnd() == 0 ? 0 : seq.findIndex(sf.getEnd());
346         jsonFeature.setXstart(xStart);
347         jsonFeature.setXend(xEnd);
348         jsonFeature.setType(sf.getType());
349         jsonFeature.setDescription(sf.getDescription());
350         jsonFeature.setLinks(sf.links);
351         jsonFeature.setOtherDetails(sf.otherDetails);
352         jsonFeature.setScore(sf.getScore());
353         jsonFeature.setFillColor(featureColour);
354         jsonFeature.setFeatureGroup(sf.getFeatureGroup());
355         sequenceFeaturesPojo.add(jsonFeature);
356       }
357     }
358     return sequenceFeaturesPojo;
359   }
360
361   public static List<AlignmentAnnotationPojo> annotationToJsonPojo(
362           Vector<AlignmentAnnotation> annotations)
363   {
364     List<AlignmentAnnotationPojo> jsonAnnotations = new ArrayList<>();
365     if (annotations == null)
366     {
367       return jsonAnnotations;
368     }
369     for (AlignmentAnnotation annot : annotations)
370     {
371       AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
372       alignAnnotPojo.setDescription(annot.description);
373       alignAnnotPojo.setLabel(annot.label);
374       if (!Double.isNaN(annot.score))
375       {
376         alignAnnotPojo.setScore(annot.score);
377       }
378       alignAnnotPojo.setCalcId(annot.getCalcId());
379       alignAnnotPojo.setGraphType(annot.graph);
380
381       AnnotationDisplaySettingPojo annotSetting = new AnnotationDisplaySettingPojo();
382       annotSetting.setBelowAlignment(annot.belowAlignment);
383       annotSetting.setCentreColLabels(annot.centreColLabels);
384       annotSetting.setScaleColLabel(annot.scaleColLabel);
385       annotSetting.setShowAllColLabels(annot.showAllColLabels);
386       annotSetting.setVisible(annot.visible);
387       annotSetting.setHasIcon(annot.hasIcons);
388       alignAnnotPojo.setAnnotationSettings(annotSetting);
389       SequenceI refSeq = annot.sequenceRef;
390       if (refSeq != null)
391       {
392         alignAnnotPojo.setSequenceRef(String.valueOf(refSeq.hashCode()));
393       }
394       for (Annotation annotation : annot.annotations)
395       {
396         AnnotationPojo annotationPojo = new AnnotationPojo();
397         if (annotation != null)
398         {
399           annotationPojo.setDescription(annotation.description);
400           annotationPojo.setValue(annotation.value);
401           annotationPojo
402                   .setSecondaryStructure(annotation.secondaryStructure);
403           String displayChar = annotation.displayCharacter == null ? null
404                   : annotation.displayCharacter;
405           // System.out.println("--------------------->[" + displayChar + "]");
406           annotationPojo.setDisplayCharacter(displayChar);
407           if (annotation.colour != null)
408           {
409             annotationPojo.setColour(jalview.util.Format
410                     .getHexString(annotation.colour));
411           }
412           alignAnnotPojo.getAnnotations().add(annotationPojo);
413         }
414         else
415         {
416           if (annot.getCalcId() != null
417                   && annot.getCalcId().equalsIgnoreCase(TCOFFEE_SCORE))
418           {
419             // do nothing
420           }
421           else
422           {
423             alignAnnotPojo.getAnnotations().add(annotationPojo);
424           }
425         }
426       }
427       jsonAnnotations.add(alignAnnotPojo);
428     }
429     return jsonAnnotations;
430   }
431
432   @SuppressWarnings("unchecked")
433   public JSONFile parse(Reader jsonAlignmentString)
434   {
435     try
436     {
437       JSONParser jsonParser = new JSONParser();
438       JSONObject alignmentJsonObj = (JSONObject) jsonParser
439               .parse(jsonAlignmentString);
440       JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
441       JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj
442               .get("alignAnnotation");
443       JSONArray jsonSeqArray = (JSONArray) alignmentJsonObj
444               .get("seqFeatures");
445       JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
446               .get("seqGroups");
447       JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
448               .get("appSettings");
449
450       if (jvSettingsJsonObj != null)
451       {
452         globalColourScheme = (String) jvSettingsJsonObj
453                 .get("globalColorScheme");
454         Boolean showFeatures = Boolean.valueOf(jvSettingsJsonObj.get(
455                 "showSeqFeatures").toString());
456         setShowSeqFeatures(showFeatures);
457         parseHiddenSeqRefsAsList(jvSettingsJsonObj);
458         parseHiddenCols(jvSettingsJsonObj);
459       }
460
461       hiddenSequences = new ArrayList<>();
462       seqMap = new Hashtable<>();
463       for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
464               .hasNext();)
465       {
466         JSONObject sequence = sequenceIter.next();
467         String sequcenceString = sequence.get("seq").toString();
468         String sequenceName = sequence.get("name").toString();
469         String seqUniqueId = sequence.get("id").toString();
470         int start = Integer.valueOf(sequence.get("start").toString());
471         int end = Integer.valueOf(sequence.get("end").toString());
472         Sequence seq = new Sequence(sequenceName, sequcenceString, start,
473                 end);
474         if (hiddenSeqRefs != null && hiddenSeqRefs.contains(seqUniqueId))
475         {
476           hiddenSequences.add(seq);
477         }
478         seqs.add(seq);
479         seqMap.put(seqUniqueId, seq);
480       }
481
482       parseFeatures(jsonSeqArray);
483
484       for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray.iterator(); seqGrpIter
485               .hasNext();)
486       {
487         JSONObject seqGrpObj = seqGrpIter.next();
488         String grpName = seqGrpObj.get("groupName").toString();
489         String colourScheme = seqGrpObj.get("colourScheme").toString();
490         String description = (seqGrpObj.get("description") == null) ? null
491                 : seqGrpObj.get("description").toString();
492         boolean displayBoxes = Boolean.valueOf(seqGrpObj
493                 .get("displayBoxes").toString());
494         boolean displayText = Boolean.valueOf(seqGrpObj.get("displayText")
495                 .toString());
496         boolean colourText = Boolean.valueOf(seqGrpObj.get("colourText")
497                 .toString());
498         boolean showNonconserved = Boolean.valueOf(seqGrpObj.get(
499                 "showNonconserved").toString());
500         int startRes = Integer
501                 .valueOf(seqGrpObj.get("startRes").toString());
502         int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
503         JSONArray sequenceRefs = (JSONArray) seqGrpObj.get("sequenceRefs");
504
505         ArrayList<SequenceI> grpSeqs = new ArrayList<>();
506         if (sequenceRefs.size() > 0)
507         {
508           Iterator<String> seqHashIter = sequenceRefs.iterator();
509           while (seqHashIter.hasNext())
510           {
511             String seqHash = seqHashIter.next();
512             Sequence sequence = seqMap.get(seqHash);
513             if (sequence != null)
514             {
515               grpSeqs.add(sequence);
516             }
517           }
518         }
519         SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, null,
520                 displayBoxes, displayText, colourText, startRes, endRes);
521         seqGrp.setColourScheme(ColourSchemeMapper.getJalviewColourScheme(
522                 colourScheme, seqGrp));
523         seqGrp.setShowNonconserved(showNonconserved);
524         seqGrp.setDescription(description);
525         this.seqGroups.add(seqGrp);
526
527       }
528
529       for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
530               .hasNext();)
531       {
532         JSONObject alAnnot = alAnnotIter.next();
533         JSONArray annotJsonArray = (JSONArray) alAnnot.get("annotations");
534         Annotation[] annotations = new Annotation[annotJsonArray.size()];
535         int count = 0;
536         for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
537                 .hasNext();)
538         {
539           JSONObject annot = annotIter.next();
540           if (annot == null)
541           {
542             annotations[count] = null;
543           }
544           else
545           {
546             float val = annot.get("value") == null ? null : Float
547                     .valueOf(annot.get("value").toString());
548             String desc = annot.get("description") == null ? null : annot
549                     .get("description").toString();
550             char ss = annot.get("secondaryStructure") == null
551                     || annot.get("secondaryStructure").toString()
552                             .equalsIgnoreCase("u0000") ? ' ' : annot
553                     .get("secondaryStructure").toString().charAt(0);
554             String displayChar = annot.get("displayCharacter") == null ? ""
555                     : annot.get("displayCharacter").toString();
556
557             annotations[count] = new Annotation(displayChar, desc, ss, val);
558             if (annot.get("colour") != null)
559             {
560               Color color = ColorUtils.parseColourString(annot.get(
561                       "colour").toString());
562               annotations[count].colour = color;
563             }
564           }
565           ++count;
566         }
567
568         AlignmentAnnotation alignAnnot = new AlignmentAnnotation(alAnnot
569                 .get("label").toString(), alAnnot.get("description")
570                 .toString(), annotations);
571         alignAnnot.graph = (alAnnot.get("graphType") == null) ? 0 : Integer
572                 .valueOf(alAnnot.get("graphType").toString());
573
574         JSONObject diplaySettings = (JSONObject) alAnnot
575                 .get("annotationSettings");
576         if (diplaySettings != null)
577         {
578
579           alignAnnot.scaleColLabel = (diplaySettings.get("scaleColLabel") == null) ? false
580                   : Boolean.valueOf(diplaySettings.get("scaleColLabel")
581                           .toString());
582           alignAnnot.showAllColLabels = (diplaySettings
583                   .get("showAllColLabels") == null) ? true : Boolean
584                   .valueOf(diplaySettings.get("showAllColLabels")
585                           .toString());
586           alignAnnot.centreColLabels = (diplaySettings
587                   .get("centreColLabels") == null) ? true
588                   : Boolean.valueOf(diplaySettings.get("centreColLabels")
589                           .toString());
590           alignAnnot.belowAlignment = (diplaySettings.get("belowAlignment") == null) ? false
591                   : Boolean.valueOf(diplaySettings.get("belowAlignment")
592                           .toString());
593           alignAnnot.visible = (diplaySettings.get("visible") == null) ? true
594                   : Boolean.valueOf(diplaySettings.get("visible")
595                           .toString());
596           alignAnnot.hasIcons = (diplaySettings.get("hasIcon") == null) ? true
597                   : Boolean.valueOf(diplaySettings.get("hasIcon")
598                           .toString());
599
600         }
601         if (alAnnot.get("score") != null)
602         {
603           alignAnnot.score = Double
604                   .valueOf(alAnnot.get("score").toString());
605         }
606
607         String calcId = (alAnnot.get("calcId") == null) ? "" : alAnnot.get(
608                 "calcId").toString();
609         alignAnnot.setCalcId(calcId);
610         String seqHash = (alAnnot.get("sequenceRef") != null) ? alAnnot
611                 .get("sequenceRef").toString() : null;
612
613         Sequence sequence = (seqHash != null) ? seqMap.get(seqHash) : null;
614         if (sequence != null)
615         {
616           alignAnnot.sequenceRef = sequence;
617           sequence.addAlignmentAnnotation(alignAnnot);
618           if (alignAnnot.label.equalsIgnoreCase("T-COFFEE"))
619           {
620             alignAnnot.createSequenceMapping(sequence, sequence.getStart(),
621                     false);
622             sequence.addAlignmentAnnotation(alignAnnot);
623             alignAnnot.adjustForAlignment();
624           }
625         }
626         alignAnnot.validateRangeAndDisplay();
627         this.annotations.add(alignAnnot);
628
629       }
630     } catch (Exception e)
631     {
632       e.printStackTrace();
633     }
634     return this;
635   }
636
637   public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson)
638   {
639     hiddenSeqRefs = new ArrayList<>();
640     String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs");
641     if (hiddenSeqs != null && !hiddenSeqs.isEmpty())
642     {
643       String[] seqRefs = hiddenSeqs.split(";");
644       for (String seqRef : seqRefs)
645       {
646         hiddenSeqRefs.add(seqRef);
647       }
648     }
649   }
650
651   public void parseHiddenCols(JSONObject jvSettingsJson)
652   {
653     String hiddenCols = (String) jvSettingsJson.get("hiddenCols");
654     if (hiddenCols != null && !hiddenCols.isEmpty())
655     {
656       hiddenColumns = new HiddenColumns();
657       String[] rangeStrings = hiddenCols.split(";");
658       for (String rangeString : rangeStrings)
659       {
660         String[] range = rangeString.split("-");
661         hiddenColumns.hideColumns(Integer.valueOf(range[0]),
662                 Integer.valueOf(range[1]));
663       }
664     }
665   }
666
667   @SuppressWarnings("unchecked")
668   private void parseFeatures(JSONArray jsonSeqFeatures)
669   {
670     if (jsonSeqFeatures != null)
671     {
672       displayedFeatures = new FeaturesDisplayed();
673       for (Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
674               .hasNext();)
675       {
676         JSONObject jsonFeature = seqFeatureItr.next();
677         Long begin = (Long) jsonFeature.get("xStart");
678         Long end = (Long) jsonFeature.get("xEnd");
679         String type = (String) jsonFeature.get("type");
680         String featureGrp = (String) jsonFeature.get("featureGroup");
681         String description = (String) jsonFeature.get("description");
682         String seqRef = (String) jsonFeature.get("sequenceRef");
683         Float score = Float.valueOf(jsonFeature.get("score").toString());
684
685         Sequence seq = seqMap.get(seqRef);
686
687         /*
688          * begin/end of 0 is for a non-positional feature
689          */
690         int featureBegin = begin.intValue() == 0 ? 0 : seq
691                 .findPosition(begin.intValue());
692         int featureEnd = end.intValue() == 0 ? 0 : seq.findPosition(end
693                 .intValue()) - 1;
694
695         SequenceFeature sequenceFeature = new SequenceFeature(type,
696                 description, featureBegin, featureEnd, score, featureGrp);
697
698         JSONArray linksJsonArray = (JSONArray) jsonFeature.get("links");
699         if (linksJsonArray != null && linksJsonArray.size() > 0)
700         {
701           Iterator<String> linkList = linksJsonArray.iterator();
702           while (linkList.hasNext())
703           {
704             String link = linkList.next();
705             sequenceFeature.addLink(link);
706           }
707         }
708
709         seq.addSequenceFeature(sequenceFeature);
710         displayedFeatures.setVisible(type);
711       }
712     }
713   }
714
715   @Override
716   public String getGlobalColourScheme()
717   {
718     return globalColourScheme;
719   }
720
721   public void setGlobalColorScheme(String globalColourScheme)
722   {
723     this.globalColourScheme = globalColourScheme;
724   }
725
726   @Override
727   public FeaturesDisplayedI getDisplayedFeatures()
728   {
729     return displayedFeatures;
730   }
731
732   public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures)
733   {
734     this.displayedFeatures = displayedFeatures;
735   }
736
737   @Override
738   public void configureForView(AlignmentViewPanel avpanel)
739   {
740     if (avpanel == null)
741     {
742       return;
743     }
744     super.configureForView(avpanel);
745     AlignViewportI viewport = avpanel.getAlignViewport();
746     AlignmentI alignment = viewport.getAlignment();
747     AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
748
749     seqGroups = alignment.getGroups();
750     fr = avpanel.cloneFeatureRenderer();
751
752     // Add non auto calculated annotation to AlignFile
753     if (annots != null)
754     {
755       for (AlignmentAnnotation annot : annots)
756       {
757         if (annot != null && !annot.autoCalculated)
758         {
759           annotations.add(annot);
760         }
761       }
762     }
763     globalColourScheme = ColourSchemeProperty.getColourName(viewport
764             .getGlobalColourScheme());
765     setDisplayedFeatures(viewport.getFeaturesDisplayed());
766     showSeqFeatures = viewport.isShowSequenceFeatures();
767
768   }
769
770   @Override
771   public boolean isShowSeqFeatures()
772   {
773     return showSeqFeatures;
774   }
775
776   public void setShowSeqFeatures(boolean showSeqFeatures)
777   {
778     this.showSeqFeatures = showSeqFeatures;
779   }
780
781   public Vector<AlignmentAnnotation> getAnnotations()
782   {
783     return annotations;
784   }
785
786   @Override
787   public HiddenColumns getHiddenColumns()
788   {
789     return hiddenColumns;
790   }
791
792   public void setHiddenColumns(HiddenColumns hidden)
793   {
794     this.hiddenColumns = hidden;
795   }
796
797   @Override
798   public SequenceI[] getHiddenSequences()
799   {
800     if (hiddenSequences == null || hiddenSequences.isEmpty())
801     {
802       return new SequenceI[] {};
803     }
804     synchronized (hiddenSequences)
805     {
806       return hiddenSequences.toArray(new SequenceI[hiddenSequences.size()]);
807     }
808   }
809
810   public void setHiddenSequences(ArrayList<SequenceI> hiddenSequences)
811   {
812     this.hiddenSequences = hiddenSequences;
813   }
814
815   public class JSONExportSettings
816   {
817     private boolean exportSequence;
818
819     private boolean exportSequenceFeatures;
820
821     private boolean exportAnnotations;
822
823     private boolean exportGroups;
824
825     private boolean exportJalviewSettings;
826
827     public boolean isExportSequence()
828     {
829       return exportSequence;
830     }
831
832     public void setExportSequence(boolean exportSequence)
833     {
834       this.exportSequence = exportSequence;
835     }
836
837     public boolean isExportSequenceFeatures()
838     {
839       return exportSequenceFeatures;
840     }
841
842     public void setExportSequenceFeatures(boolean exportSequenceFeatures)
843     {
844       this.exportSequenceFeatures = exportSequenceFeatures;
845     }
846
847     public boolean isExportAnnotations()
848     {
849       return exportAnnotations;
850     }
851
852     public void setExportAnnotations(boolean exportAnnotations)
853     {
854       this.exportAnnotations = exportAnnotations;
855     }
856
857     public boolean isExportGroups()
858     {
859       return exportGroups;
860     }
861
862     public void setExportGroups(boolean exportGroups)
863     {
864       this.exportGroups = exportGroups;
865     }
866
867     public boolean isExportJalviewSettings()
868     {
869       return exportJalviewSettings;
870     }
871
872     public void setExportJalviewSettings(boolean exportJalviewSettings)
873     {
874       this.exportJalviewSettings = exportJalviewSettings;
875     }
876   }
877
878   /**
879    * Returns a descriptor for suitable feature display settings with
880    * <ul>
881    * <li>ResNums or insertions features visible</li>
882    * <li>insertions features coloured red</li>
883    * <li>ResNum features coloured by label</li>
884    * <li>Insertions displayed above (on top of) ResNums</li>
885    * </ul>
886    */
887   @Override
888   public FeatureSettingsModelI getFeatureColourScheme()
889   {
890     return new PDBFeatureSettings();
891   }
892 }