JAL-3048 refactored export settings to bean+dialog with callbacks - basically works...
[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.AlignExportSettingBeanI;
33 import jalview.datamodel.AlignmentAnnotation;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.Annotation;
36 import jalview.datamodel.HiddenColumns;
37 import jalview.datamodel.HiddenSequences;
38 import jalview.datamodel.Sequence;
39 import jalview.datamodel.SequenceFeature;
40 import jalview.datamodel.SequenceGroup;
41 import jalview.datamodel.SequenceI;
42 import jalview.json.binding.biojson.v1.AlignmentAnnotationPojo;
43 import jalview.json.binding.biojson.v1.AlignmentPojo;
44 import jalview.json.binding.biojson.v1.AnnotationDisplaySettingPojo;
45 import jalview.json.binding.biojson.v1.AnnotationPojo;
46 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
47 import jalview.json.binding.biojson.v1.SequenceFeaturesPojo;
48 import jalview.json.binding.biojson.v1.SequenceGrpPojo;
49 import jalview.json.binding.biojson.v1.SequencePojo;
50 import jalview.renderer.seqfeatures.FeatureColourFinder;
51 import jalview.schemes.ColourSchemeProperty;
52 import jalview.schemes.JalviewColourScheme;
53 import jalview.schemes.ResidueColourScheme;
54 import jalview.util.ColorUtils;
55 import jalview.util.Format;
56 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
57
58 import java.awt.Color;
59 import java.io.IOException;
60 import java.io.Reader;
61 import java.util.ArrayList;
62 import java.util.Hashtable;
63 import java.util.Iterator;
64 import java.util.List;
65 import java.util.Vector;
66
67 import org.json.simple.JSONArray;
68 import org.json.simple.JSONObject;
69 import org.json.simple.parser.JSONParser;
70
71 public class JSONFile extends AlignFile implements ComplexAlignFile
72 {
73   private static String version = new BuildDetails().getVersion();
74
75   private String webstartUrl = "http://www.jalview.org/services/launchApp";
76
77   private String application = "Jalview";
78
79   private String globalColourScheme;
80
81   private boolean showSeqFeatures;
82
83   private Hashtable<String, Sequence> seqMap;
84
85   private FeaturesDisplayedI displayedFeatures;
86
87   private FeatureRenderer fr;
88
89   private HiddenColumns hiddenColumns;
90
91   private List<String> hiddenSeqRefs;
92
93   private ArrayList<SequenceI> hiddenSequences;
94
95   private final static String TCOFFEE_SCORE = "TCoffeeScore";
96
97   public JSONFile()
98   {
99     super();
100   }
101
102   public JSONFile(FileParse source) throws IOException
103   {
104     super(source);
105   }
106
107   public JSONFile(String inFile, DataSourceType sourceType)
108           throws IOException
109   {
110     super(inFile, sourceType);
111   }
112
113   @Override
114   public void parse() throws IOException
115   {
116     parse(getReader());
117
118   }
119
120   @Override
121   public String print(SequenceI[] sqs, boolean jvsuffix)
122   {
123     String jsonOutput = null;
124     try
125     {
126       AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
127       AlignExportSettingBeanI exportSettings = getExportSettings();
128
129       // if no export settings were supplied use the following with all values
130       // defaulting to true
131       if (exportSettings == null)
132       {
133         exportSettings = new AlignExportSettingBeanI()
134         {
135           @Override
136           public boolean isExportHiddenSequences()
137           {
138             return true;
139           }
140
141           @Override
142           public boolean isExportHiddenColumns()
143           {
144             return true;
145           }
146
147           @Override
148           public boolean isExportGroups()
149           {
150             return true;
151           }
152
153           @Override
154           public boolean isExportFeatures()
155           {
156             return true;
157           }
158
159           @Override
160           public boolean isExportAnnotations()
161           {
162             return true;
163           }
164
165           @Override
166           public boolean isCancelled()
167           {
168             return false;
169           }
170         };
171       }
172
173       int count = 0;
174       for (SequenceI seq : sqs)
175       {
176         StringBuilder name = new StringBuilder();
177         name.append(seq.getName()).append("/").append(seq.getStart())
178                 .append("-").append(seq.getEnd());
179         SequencePojo jsonSeqPojo = new SequencePojo();
180         jsonSeqPojo.setId(String.valueOf(seq.hashCode()));
181         jsonSeqPojo.setOrder(++count);
182         jsonSeqPojo.setEnd(seq.getEnd());
183         jsonSeqPojo.setStart(seq.getStart());
184         jsonSeqPojo.setName(name.toString());
185         jsonSeqPojo.setSeq(seq.getSequenceAsString());
186         jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
187       }
188       jsonAlignmentPojo.setGlobalColorScheme(globalColourScheme);
189       jsonAlignmentPojo.getAppSettings().put("application", application);
190       jsonAlignmentPojo.getAppSettings().put("version", version);
191       jsonAlignmentPojo.getAppSettings().put("webStartUrl", webstartUrl);
192       jsonAlignmentPojo.getAppSettings().put("showSeqFeatures",
193               String.valueOf(showSeqFeatures));
194
195       String[] hiddenSections = getHiddenSections();
196       if (hiddenSections != null)
197       {
198         if (hiddenSections[0] != null
199                 && exportSettings.isExportHiddenColumns())
200         {
201           jsonAlignmentPojo.getAppSettings().put("hiddenCols",
202                   String.valueOf(hiddenSections[0]));
203         }
204         if (hiddenSections[1] != null
205                 && exportSettings.isExportHiddenSequences())
206         {
207           jsonAlignmentPojo.getAppSettings().put("hiddenSeqs",
208                   String.valueOf(hiddenSections[1]));
209         }
210       }
211
212       if (exportSettings.isExportAnnotations())
213       {
214         jsonAlignmentPojo
215                 .setAlignAnnotation(annotationToJsonPojo(annotations));
216       }
217       else
218       {
219         // These color schemes require annotation, disable them if annotations
220         // are not exported
221         if (globalColourScheme
222                 .equalsIgnoreCase(JalviewColourScheme.RNAHelices.toString())
223                 || globalColourScheme.equalsIgnoreCase(
224                         JalviewColourScheme.TCoffee.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(
410                     jalview.util.Format.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(
455                 jvSettingsJsonObj.get("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
464               .iterator(); sequenceIter.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
485               .iterator(); seqGrpIter.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
493                 .valueOf(seqGrpObj.get("displayBoxes").toString());
494         boolean displayText = Boolean
495                 .valueOf(seqGrpObj.get("displayText").toString());
496         boolean colourText = Boolean
497                 .valueOf(seqGrpObj.get("colourText").toString());
498         boolean showNonconserved = Boolean
499                 .valueOf(seqGrpObj.get("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
522                 .getJalviewColourScheme(colourScheme, seqGrp));
523         seqGrp.setShowNonconserved(showNonconserved);
524         seqGrp.setDescription(description);
525         this.seqGroups.add(seqGrp);
526
527       }
528
529       for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray
530               .iterator(); alAnnotIter.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
537                 .iterator(); annotIter.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
547                     : Float.valueOf(annot.get("value").toString());
548             String desc = annot.get("description") == null ? null
549                     : annot.get("description").toString();
550             char ss = annot.get("secondaryStructure") == null
551                     || annot.get("secondaryStructure").toString()
552                             .equalsIgnoreCase("u0000") ? ' '
553                                     : annot.get("secondaryStructure")
554                                             .toString().charAt(0);
555             String displayChar = annot.get("displayCharacter") == null ? ""
556                     : annot.get("displayCharacter").toString();
557
558             annotations[count] = new Annotation(displayChar, desc, ss, val);
559             if (annot.get("colour") != null)
560             {
561               Color color = ColorUtils
562                       .parseColourString(annot.get("colour").toString());
563               annotations[count].colour = color;
564             }
565           }
566           ++count;
567         }
568
569         AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
570                 alAnnot.get("label").toString(),
571                 alAnnot.get("description").toString(), annotations);
572         alignAnnot.graph = (alAnnot.get("graphType") == null) ? 0
573                 : Integer.valueOf(alAnnot.get("graphType").toString());
574
575         JSONObject diplaySettings = (JSONObject) alAnnot
576                 .get("annotationSettings");
577         if (diplaySettings != null)
578         {
579
580           alignAnnot.scaleColLabel = (diplaySettings
581                   .get("scaleColLabel") == null) ? false
582                           : Boolean.valueOf(diplaySettings
583                                   .get("scaleColLabel").toString());
584           alignAnnot.showAllColLabels = (diplaySettings
585                   .get("showAllColLabels") == null) ? true
586                           : Boolean.valueOf(diplaySettings
587                                   .get("showAllColLabels").toString());
588           alignAnnot.centreColLabels = (diplaySettings
589                   .get("centreColLabels") == null) ? true
590                           : Boolean.valueOf(diplaySettings
591                                   .get("centreColLabels").toString());
592           alignAnnot.belowAlignment = (diplaySettings
593                   .get("belowAlignment") == null) ? false
594                           : Boolean.valueOf(diplaySettings
595                                   .get("belowAlignment").toString());
596           alignAnnot.visible = (diplaySettings.get("visible") == null)
597                   ? true
598                   : Boolean.valueOf(
599                           diplaySettings.get("visible").toString());
600           alignAnnot.hasIcons = (diplaySettings.get("hasIcon") == null)
601                   ? true
602                   : Boolean.valueOf(
603                           diplaySettings.get("hasIcon").toString());
604
605         }
606         if (alAnnot.get("score") != null)
607         {
608           alignAnnot.score = Double
609                   .valueOf(alAnnot.get("score").toString());
610         }
611
612         String calcId = (alAnnot.get("calcId") == null) ? ""
613                 : alAnnot.get("calcId").toString();
614         alignAnnot.setCalcId(calcId);
615         String seqHash = (alAnnot.get("sequenceRef") != null)
616                 ? alAnnot.get("sequenceRef").toString()
617                 : null;
618
619         Sequence sequence = (seqHash != null) ? seqMap.get(seqHash) : null;
620         if (sequence != null)
621         {
622           alignAnnot.sequenceRef = sequence;
623           sequence.addAlignmentAnnotation(alignAnnot);
624           if (alignAnnot.label.equalsIgnoreCase("T-COFFEE"))
625           {
626             alignAnnot.createSequenceMapping(sequence, sequence.getStart(),
627                     false);
628             sequence.addAlignmentAnnotation(alignAnnot);
629             alignAnnot.adjustForAlignment();
630           }
631         }
632         alignAnnot.validateRangeAndDisplay();
633         this.annotations.add(alignAnnot);
634
635       }
636     } catch (Exception e)
637     {
638       e.printStackTrace();
639     }
640     return this;
641   }
642
643   public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson)
644   {
645     hiddenSeqRefs = new ArrayList<>();
646     String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs");
647     if (hiddenSeqs != null && !hiddenSeqs.isEmpty())
648     {
649       String[] seqRefs = hiddenSeqs.split(";");
650       for (String seqRef : seqRefs)
651       {
652         hiddenSeqRefs.add(seqRef);
653       }
654     }
655   }
656
657   public void parseHiddenCols(JSONObject jvSettingsJson)
658   {
659     String hiddenCols = (String) jvSettingsJson.get("hiddenCols");
660     if (hiddenCols != null && !hiddenCols.isEmpty())
661     {
662       hiddenColumns = new HiddenColumns();
663       String[] rangeStrings = hiddenCols.split(";");
664       for (String rangeString : rangeStrings)
665       {
666         String[] range = rangeString.split("-");
667         hiddenColumns.hideColumns(Integer.valueOf(range[0]),
668                 Integer.valueOf(range[1]));
669       }
670     }
671   }
672
673   @SuppressWarnings("unchecked")
674   private void parseFeatures(JSONArray jsonSeqFeatures)
675   {
676     if (jsonSeqFeatures != null)
677     {
678       displayedFeatures = new FeaturesDisplayed();
679       for (Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures
680               .iterator(); seqFeatureItr.hasNext();)
681       {
682         JSONObject jsonFeature = seqFeatureItr.next();
683         Long begin = (Long) jsonFeature.get("xStart");
684         Long end = (Long) jsonFeature.get("xEnd");
685         String type = (String) jsonFeature.get("type");
686         String featureGrp = (String) jsonFeature.get("featureGroup");
687         String description = (String) jsonFeature.get("description");
688         String seqRef = (String) jsonFeature.get("sequenceRef");
689         Float score = Float.valueOf(jsonFeature.get("score").toString());
690
691         Sequence seq = seqMap.get(seqRef);
692
693         /*
694          * begin/end of 0 is for a non-positional feature
695          */
696         int featureBegin = begin.intValue() == 0 ? 0 : seq
697                 .findPosition(begin.intValue());
698         int featureEnd = end.intValue() == 0 ? 0 : seq.findPosition(end
699                 .intValue()) - 1;
700
701         SequenceFeature sequenceFeature = new SequenceFeature(type,
702                 description, featureBegin, featureEnd, score, featureGrp);
703
704         JSONArray linksJsonArray = (JSONArray) jsonFeature.get("links");
705         if (linksJsonArray != null && linksJsonArray.size() > 0)
706         {
707           Iterator<String> linkList = linksJsonArray.iterator();
708           while (linkList.hasNext())
709           {
710             String link = linkList.next();
711             sequenceFeature.addLink(link);
712           }
713         }
714
715         seq.addSequenceFeature(sequenceFeature);
716         displayedFeatures.setVisible(type);
717       }
718     }
719   }
720
721   @Override
722   public String getGlobalColourScheme()
723   {
724     return globalColourScheme;
725   }
726
727   public void setGlobalColorScheme(String globalColourScheme)
728   {
729     this.globalColourScheme = globalColourScheme;
730   }
731
732   @Override
733   public FeaturesDisplayedI getDisplayedFeatures()
734   {
735     return displayedFeatures;
736   }
737
738   public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures)
739   {
740     this.displayedFeatures = displayedFeatures;
741   }
742
743   @Override
744   public void configureForView(AlignmentViewPanel avpanel)
745   {
746     if (avpanel == null)
747     {
748       return;
749     }
750     super.configureForView(avpanel);
751     AlignViewportI viewport = avpanel.getAlignViewport();
752     AlignmentI alignment = viewport.getAlignment();
753     AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
754
755     seqGroups = alignment.getGroups();
756     fr = avpanel.cloneFeatureRenderer();
757
758     // Add non auto calculated annotation to AlignFile
759     if (annots != null)
760     {
761       for (AlignmentAnnotation annot : annots)
762       {
763         if (annot != null && !annot.autoCalculated)
764         {
765           annotations.add(annot);
766         }
767       }
768     }
769     globalColourScheme = ColourSchemeProperty
770             .getColourName(viewport.getGlobalColourScheme());
771     setDisplayedFeatures(viewport.getFeaturesDisplayed());
772     showSeqFeatures = viewport.isShowSequenceFeatures();
773
774   }
775
776   @Override
777   public boolean isShowSeqFeatures()
778   {
779     return showSeqFeatures;
780   }
781
782   public void setShowSeqFeatures(boolean showSeqFeatures)
783   {
784     this.showSeqFeatures = showSeqFeatures;
785   }
786
787   public Vector<AlignmentAnnotation> getAnnotations()
788   {
789     return annotations;
790   }
791
792   @Override
793   public HiddenColumns getHiddenColumns()
794   {
795     return hiddenColumns;
796   }
797
798   public void setHiddenColumns(HiddenColumns hidden)
799   {
800     this.hiddenColumns = hidden;
801   }
802
803   @Override
804   public SequenceI[] getHiddenSequences()
805   {
806     if (hiddenSequences == null || hiddenSequences.isEmpty())
807     {
808       return new SequenceI[] {};
809     }
810     synchronized (hiddenSequences)
811     {
812       return hiddenSequences.toArray(new SequenceI[hiddenSequences.size()]);
813     }
814   }
815
816   public void setHiddenSequences(ArrayList<SequenceI> hiddenSequences)
817   {
818     this.hiddenSequences = hiddenSequences;
819   }
820
821   public class JSONExportSettings
822   {
823     private boolean exportSequence;
824
825     private boolean exportSequenceFeatures;
826
827     private boolean exportAnnotations;
828
829     private boolean exportGroups;
830
831     private boolean exportJalviewSettings;
832
833     public boolean isExportSequence()
834     {
835       return exportSequence;
836     }
837
838     public void setExportSequence(boolean exportSequence)
839     {
840       this.exportSequence = exportSequence;
841     }
842
843     public boolean isExportSequenceFeatures()
844     {
845       return exportSequenceFeatures;
846     }
847
848     public void setExportSequenceFeatures(boolean exportSequenceFeatures)
849     {
850       this.exportSequenceFeatures = exportSequenceFeatures;
851     }
852
853     public boolean isExportAnnotations()
854     {
855       return exportAnnotations;
856     }
857
858     public void setExportAnnotations(boolean exportAnnotations)
859     {
860       this.exportAnnotations = exportAnnotations;
861     }
862
863     public boolean isExportGroups()
864     {
865       return exportGroups;
866     }
867
868     public void setExportGroups(boolean exportGroups)
869     {
870       this.exportGroups = exportGroups;
871     }
872
873     public boolean isExportJalviewSettings()
874     {
875       return exportJalviewSettings;
876     }
877
878     public void setExportJalviewSettings(boolean exportJalviewSettings)
879     {
880       this.exportJalviewSettings = exportJalviewSettings;
881     }
882   }
883
884   /**
885    * Returns a descriptor for suitable feature display settings with
886    * <ul>
887    * <li>ResNums or insertions features visible</li>
888    * <li>insertions features coloured red</li>
889    * <li>ResNum features coloured by label</li>
890    * <li>Insertions displayed above (on top of) ResNums</li>
891    * </ul>
892    */
893   @Override
894   public FeatureSettingsModelI getFeatureColourScheme()
895   {
896     return new PDBFeatureSettings();
897   }
898 }