JAL-2994 put ‘\’ first in character class clause of regex - otherwise doesn’t compile...
[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.equalsIgnoreCase(
223                         JalviewColourScheme.TCoffee.toString()))
224         {
225           jsonAlignmentPojo.setGlobalColorScheme(ResidueColourScheme.NONE);
226         }
227       }
228
229       if (exportSettings.isExportFeatures())
230       {
231         jsonAlignmentPojo.setSeqFeatures(sequenceFeatureToJsonPojo(sqs));
232       }
233
234       if (exportSettings.isExportGroups() && seqGroups != null
235               && seqGroups.size() > 0)
236       {
237         for (SequenceGroup seqGrp : seqGroups)
238         {
239           SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
240           seqGrpPojo.setGroupName(seqGrp.getName());
241           seqGrpPojo.setColourScheme(ColourSchemeProperty
242                   .getColourName(seqGrp.getColourScheme()));
243           seqGrpPojo.setColourText(seqGrp.getColourText());
244           seqGrpPojo.setDescription(seqGrp.getDescription());
245           seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
246           seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
247           seqGrpPojo.setEndRes(seqGrp.getEndRes());
248           seqGrpPojo.setStartRes(seqGrp.getStartRes());
249           seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
250           for (SequenceI seq : seqGrp.getSequences())
251           {
252             seqGrpPojo.getSequenceRefs()
253                     .add(String.valueOf(seq.hashCode()));
254           }
255           jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo);
256         }
257       }
258       org.json.JSONObject generatedJSon = new org.json.JSONObject(
259               jsonAlignmentPojo);
260       jsonOutput = generatedJSon.toString();
261       return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend",
262               "xEnd");
263     } catch (Exception e)
264     {
265       e.printStackTrace();
266     }
267     return jsonOutput;
268   }
269
270   public String[] getHiddenSections()
271   {
272     String[] hiddenSections = new String[2];
273     if (getViewport() == null)
274     {
275       return null;
276     }
277
278     // hidden column business
279     if (getViewport().hasHiddenColumns())
280     {
281       hiddenSections[0] = getViewport().getAlignment().getHiddenColumns()
282               .regionsToString(";", "-");
283     }
284
285     // hidden rows/seqs business
286     HiddenSequences hiddenSeqsObj = getViewport().getAlignment()
287             .getHiddenSequences();
288     if (hiddenSeqsObj == null || hiddenSeqsObj.hiddenSequences == null)
289     {
290       return hiddenSections;
291     }
292
293     SequenceI[] hiddenSeqs = hiddenSeqsObj.hiddenSequences;
294     StringBuilder hiddenSeqsBuilder = new StringBuilder();
295     for (SequenceI hiddenSeq : hiddenSeqs)
296     {
297       if (hiddenSeq != null)
298       {
299         hiddenSeqsBuilder.append(";").append(hiddenSeq.hashCode());
300       }
301     }
302     if (hiddenSeqsBuilder.length() > 0)
303     {
304       hiddenSeqsBuilder.deleteCharAt(0);
305     }
306     hiddenSections[1] = hiddenSeqsBuilder.toString();
307
308     return hiddenSections;
309   }
310
311   protected List<SequenceFeaturesPojo> sequenceFeatureToJsonPojo(
312           SequenceI[] sqs)
313   {
314     displayedFeatures = (fr == null) ? null : fr.getFeaturesDisplayed();
315     List<SequenceFeaturesPojo> sequenceFeaturesPojo = new ArrayList<>();
316     if (sqs == null)
317     {
318       return sequenceFeaturesPojo;
319     }
320
321     FeatureColourFinder finder = new FeatureColourFinder(fr);
322
323     String[] visibleFeatureTypes = displayedFeatures == null ? null
324             : displayedFeatures.getVisibleFeatures().toArray(
325                     new String[displayedFeatures.getVisibleFeatureCount()]);
326
327     for (SequenceI seq : sqs)
328     {
329       /*
330        * get all features currently visible (and any non-positional features)
331        */
332       List<SequenceFeature> seqFeatures = seq.getFeatures().getAllFeatures(
333               visibleFeatureTypes);
334       for (SequenceFeature sf : seqFeatures)
335       {
336         SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
337                 String.valueOf(seq.hashCode()));
338
339         String featureColour = (fr == null) ? null : Format
340                 .getHexString(finder.findFeatureColour(Color.white, seq,
341                         seq.findIndex(sf.getBegin())));
342         int xStart = sf.getBegin() == 0 ? 0
343                 : seq.findIndex(sf.getBegin()) - 1;
344         int xEnd = sf.getEnd() == 0 ? 0 : seq.findIndex(sf.getEnd());
345         jsonFeature.setXstart(xStart);
346         jsonFeature.setXend(xEnd);
347         jsonFeature.setType(sf.getType());
348         jsonFeature.setDescription(sf.getDescription());
349         jsonFeature.setLinks(sf.links);
350         jsonFeature.setOtherDetails(sf.otherDetails);
351         jsonFeature.setScore(sf.getScore());
352         jsonFeature.setFillColor(featureColour);
353         jsonFeature.setFeatureGroup(sf.getFeatureGroup());
354         sequenceFeaturesPojo.add(jsonFeature);
355       }
356     }
357     return sequenceFeaturesPojo;
358   }
359
360   public static List<AlignmentAnnotationPojo> annotationToJsonPojo(
361           Vector<AlignmentAnnotation> annotations)
362   {
363     List<AlignmentAnnotationPojo> jsonAnnotations = new ArrayList<>();
364     if (annotations == null)
365     {
366       return jsonAnnotations;
367     }
368     for (AlignmentAnnotation annot : annotations)
369     {
370       AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
371       alignAnnotPojo.setDescription(annot.description);
372       alignAnnotPojo.setLabel(annot.label);
373       if (!Double.isNaN(annot.score))
374       {
375         alignAnnotPojo.setScore(annot.score);
376       }
377       alignAnnotPojo.setCalcId(annot.getCalcId());
378       alignAnnotPojo.setGraphType(annot.graph);
379
380       AnnotationDisplaySettingPojo annotSetting = new AnnotationDisplaySettingPojo();
381       annotSetting.setBelowAlignment(annot.belowAlignment);
382       annotSetting.setCentreColLabels(annot.centreColLabels);
383       annotSetting.setScaleColLabel(annot.scaleColLabel);
384       annotSetting.setShowAllColLabels(annot.showAllColLabels);
385       annotSetting.setVisible(annot.visible);
386       annotSetting.setHasIcon(annot.hasIcons);
387       alignAnnotPojo.setAnnotationSettings(annotSetting);
388       SequenceI refSeq = annot.sequenceRef;
389       if (refSeq != null)
390       {
391         alignAnnotPojo.setSequenceRef(String.valueOf(refSeq.hashCode()));
392       }
393       for (Annotation annotation : annot.annotations)
394       {
395         AnnotationPojo annotationPojo = new AnnotationPojo();
396         if (annotation != null)
397         {
398           annotationPojo.setDescription(annotation.description);
399           annotationPojo.setValue(annotation.value);
400           annotationPojo
401                   .setSecondaryStructure(annotation.secondaryStructure);
402           String displayChar = annotation.displayCharacter == null ? null
403                   : annotation.displayCharacter;
404           // System.out.println("--------------------->[" + displayChar + "]");
405           annotationPojo.setDisplayCharacter(displayChar);
406           if (annotation.colour != null)
407           {
408             annotationPojo.setColour(
409                     jalview.util.Format.getHexString(annotation.colour));
410           }
411           alignAnnotPojo.getAnnotations().add(annotationPojo);
412         }
413         else
414         {
415           if (annot.getCalcId() != null
416                   && annot.getCalcId().equalsIgnoreCase(TCOFFEE_SCORE))
417           {
418             // do nothing
419           }
420           else
421           {
422             alignAnnotPojo.getAnnotations().add(annotationPojo);
423           }
424         }
425       }
426       jsonAnnotations.add(alignAnnotPojo);
427     }
428     return jsonAnnotations;
429   }
430
431   @SuppressWarnings("unchecked")
432   public JSONFile parse(Reader jsonAlignmentString)
433   {
434     try
435     {
436       JSONParser jsonParser = new JSONParser();
437       JSONObject alignmentJsonObj = (JSONObject) jsonParser
438               .parse(jsonAlignmentString);
439       JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
440       JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj
441               .get("alignAnnotation");
442       JSONArray jsonSeqArray = (JSONArray) alignmentJsonObj
443               .get("seqFeatures");
444       JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
445               .get("seqGroups");
446       JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
447               .get("appSettings");
448
449       if (jvSettingsJsonObj != null)
450       {
451         globalColourScheme = (String) jvSettingsJsonObj
452                 .get("globalColorScheme");
453         Boolean showFeatures = Boolean.valueOf(
454                 jvSettingsJsonObj.get("showSeqFeatures").toString());
455         setShowSeqFeatures(showFeatures);
456         parseHiddenSeqRefsAsList(jvSettingsJsonObj);
457         parseHiddenCols(jvSettingsJsonObj);
458       }
459
460       hiddenSequences = new ArrayList<>();
461       seqMap = new Hashtable<>();
462       for (Iterator<JSONObject> sequenceIter = seqJsonArray
463               .iterator(); sequenceIter.hasNext();)
464       {
465         JSONObject sequence = sequenceIter.next();
466         String sequcenceString = sequence.get("seq").toString();
467         String sequenceName = sequence.get("name").toString();
468         String seqUniqueId = sequence.get("id").toString();
469         int start = Integer.valueOf(sequence.get("start").toString());
470         int end = Integer.valueOf(sequence.get("end").toString());
471         Sequence seq = new Sequence(sequenceName, sequcenceString, start,
472                 end);
473         if (hiddenSeqRefs != null && hiddenSeqRefs.contains(seqUniqueId))
474         {
475           hiddenSequences.add(seq);
476         }
477         seqs.add(seq);
478         seqMap.put(seqUniqueId, seq);
479       }
480
481       parseFeatures(jsonSeqArray);
482
483       for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray
484               .iterator(); seqGrpIter.hasNext();)
485       {
486         JSONObject seqGrpObj = seqGrpIter.next();
487         String grpName = seqGrpObj.get("groupName").toString();
488         String colourScheme = seqGrpObj.get("colourScheme").toString();
489         String description = (seqGrpObj.get("description") == null) ? null
490                 : seqGrpObj.get("description").toString();
491         boolean displayBoxes = Boolean
492                 .valueOf(seqGrpObj.get("displayBoxes").toString());
493         boolean displayText = Boolean
494                 .valueOf(seqGrpObj.get("displayText").toString());
495         boolean colourText = Boolean
496                 .valueOf(seqGrpObj.get("colourText").toString());
497         boolean showNonconserved = Boolean
498                 .valueOf(seqGrpObj.get("showNonconserved").toString());
499         int startRes = Integer
500                 .valueOf(seqGrpObj.get("startRes").toString());
501         int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
502         JSONArray sequenceRefs = (JSONArray) seqGrpObj.get("sequenceRefs");
503
504         ArrayList<SequenceI> grpSeqs = new ArrayList<>();
505         if (sequenceRefs.size() > 0)
506         {
507           Iterator<String> seqHashIter = sequenceRefs.iterator();
508           while (seqHashIter.hasNext())
509           {
510             String seqHash = seqHashIter.next();
511             Sequence sequence = seqMap.get(seqHash);
512             if (sequence != null)
513             {
514               grpSeqs.add(sequence);
515             }
516           }
517         }
518         SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, null,
519                 displayBoxes, displayText, colourText, startRes, endRes);
520         seqGrp.setColourScheme(ColourSchemeMapper
521                 .getJalviewColourScheme(colourScheme, seqGrp));
522         seqGrp.setShowNonconserved(showNonconserved);
523         seqGrp.setDescription(description);
524         this.seqGroups.add(seqGrp);
525
526       }
527
528       for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray
529               .iterator(); alAnnotIter.hasNext();)
530       {
531         JSONObject alAnnot = alAnnotIter.next();
532         JSONArray annotJsonArray = (JSONArray) alAnnot.get("annotations");
533         Annotation[] annotations = new Annotation[annotJsonArray.size()];
534         int count = 0;
535         for (Iterator<JSONObject> annotIter = annotJsonArray
536                 .iterator(); annotIter.hasNext();)
537         {
538           JSONObject annot = annotIter.next();
539           if (annot == null)
540           {
541             annotations[count] = null;
542           }
543           else
544           {
545             float val = annot.get("value") == null ? null
546                     : Float.valueOf(annot.get("value").toString());
547             String desc = annot.get("description") == null ? null
548                     : annot.get("description").toString();
549             char ss = annot.get("secondaryStructure") == null
550                     || annot.get("secondaryStructure").toString()
551                             .equalsIgnoreCase("u0000") ? ' '
552                                     : annot.get("secondaryStructure")
553                                             .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
561                       .parseColourString(annot.get("colour").toString());
562               annotations[count].colour = color;
563             }
564           }
565           ++count;
566         }
567
568         AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
569                 alAnnot.get("label").toString(),
570                 alAnnot.get("description").toString(), annotations);
571         alignAnnot.graph = (alAnnot.get("graphType") == null) ? 0
572                 : Integer.valueOf(alAnnot.get("graphType").toString());
573
574         JSONObject diplaySettings = (JSONObject) alAnnot
575                 .get("annotationSettings");
576         if (diplaySettings != null)
577         {
578
579           alignAnnot.scaleColLabel = (diplaySettings
580                   .get("scaleColLabel") == null) ? false
581                           : Boolean.valueOf(diplaySettings
582                                   .get("scaleColLabel").toString());
583           alignAnnot.showAllColLabels = (diplaySettings
584                   .get("showAllColLabels") == null) ? true
585                           : Boolean.valueOf(diplaySettings
586                                   .get("showAllColLabels").toString());
587           alignAnnot.centreColLabels = (diplaySettings
588                   .get("centreColLabels") == null) ? true
589                           : Boolean.valueOf(diplaySettings
590                                   .get("centreColLabels").toString());
591           alignAnnot.belowAlignment = (diplaySettings
592                   .get("belowAlignment") == null) ? false
593                           : Boolean.valueOf(diplaySettings
594                                   .get("belowAlignment").toString());
595           alignAnnot.visible = (diplaySettings.get("visible") == null)
596                   ? true
597                   : Boolean.valueOf(
598                           diplaySettings.get("visible").toString());
599           alignAnnot.hasIcons = (diplaySettings.get("hasIcon") == null)
600                   ? true
601                   : Boolean.valueOf(
602                           diplaySettings.get("hasIcon").toString());
603
604         }
605         if (alAnnot.get("score") != null)
606         {
607           alignAnnot.score = Double
608                   .valueOf(alAnnot.get("score").toString());
609         }
610
611         String calcId = (alAnnot.get("calcId") == null) ? ""
612                 : alAnnot.get("calcId").toString();
613         alignAnnot.setCalcId(calcId);
614         String seqHash = (alAnnot.get("sequenceRef") != null)
615                 ? alAnnot.get("sequenceRef").toString()
616                 : null;
617
618         Sequence sequence = (seqHash != null) ? seqMap.get(seqHash) : null;
619         if (sequence != null)
620         {
621           alignAnnot.sequenceRef = sequence;
622           sequence.addAlignmentAnnotation(alignAnnot);
623           if (alignAnnot.label.equalsIgnoreCase("T-COFFEE"))
624           {
625             alignAnnot.createSequenceMapping(sequence, sequence.getStart(),
626                     false);
627             sequence.addAlignmentAnnotation(alignAnnot);
628             alignAnnot.adjustForAlignment();
629           }
630         }
631         alignAnnot.validateRangeAndDisplay();
632         this.annotations.add(alignAnnot);
633
634       }
635     } catch (Exception e)
636     {
637       e.printStackTrace();
638     }
639     return this;
640   }
641
642   public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson)
643   {
644     hiddenSeqRefs = new ArrayList<>();
645     String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs");
646     if (hiddenSeqs != null && !hiddenSeqs.isEmpty())
647     {
648       String[] seqRefs = hiddenSeqs.split(";");
649       for (String seqRef : seqRefs)
650       {
651         hiddenSeqRefs.add(seqRef);
652       }
653     }
654   }
655
656   public void parseHiddenCols(JSONObject jvSettingsJson)
657   {
658     String hiddenCols = (String) jvSettingsJson.get("hiddenCols");
659     if (hiddenCols != null && !hiddenCols.isEmpty())
660     {
661       hiddenColumns = new HiddenColumns();
662       String[] rangeStrings = hiddenCols.split(";");
663       for (String rangeString : rangeStrings)
664       {
665         String[] range = rangeString.split("-");
666         hiddenColumns.hideColumns(Integer.valueOf(range[0]),
667                 Integer.valueOf(range[1]));
668       }
669     }
670   }
671
672   @SuppressWarnings("unchecked")
673   private void parseFeatures(JSONArray jsonSeqFeatures)
674   {
675     if (jsonSeqFeatures != null)
676     {
677       displayedFeatures = new FeaturesDisplayed();
678       for (Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures
679               .iterator(); seqFeatureItr.hasNext();)
680       {
681         JSONObject jsonFeature = seqFeatureItr.next();
682         Long begin = (Long) jsonFeature.get("xStart");
683         Long end = (Long) jsonFeature.get("xEnd");
684         String type = (String) jsonFeature.get("type");
685         String featureGrp = (String) jsonFeature.get("featureGroup");
686         String description = (String) jsonFeature.get("description");
687         String seqRef = (String) jsonFeature.get("sequenceRef");
688         Float score = Float.valueOf(jsonFeature.get("score").toString());
689
690         Sequence seq = seqMap.get(seqRef);
691
692         /*
693          * begin/end of 0 is for a non-positional feature
694          */
695         int featureBegin = begin.intValue() == 0 ? 0 : seq
696                 .findPosition(begin.intValue());
697         int featureEnd = end.intValue() == 0 ? 0 : seq.findPosition(end
698                 .intValue()) - 1;
699
700         SequenceFeature sequenceFeature = new SequenceFeature(type,
701                 description, featureBegin, featureEnd, score, featureGrp);
702
703         JSONArray linksJsonArray = (JSONArray) jsonFeature.get("links");
704         if (linksJsonArray != null && linksJsonArray.size() > 0)
705         {
706           Iterator<String> linkList = linksJsonArray.iterator();
707           while (linkList.hasNext())
708           {
709             String link = linkList.next();
710             sequenceFeature.addLink(link);
711           }
712         }
713
714         seq.addSequenceFeature(sequenceFeature);
715         displayedFeatures.setVisible(type);
716       }
717     }
718   }
719
720   @Override
721   public String getGlobalColourScheme()
722   {
723     return globalColourScheme;
724   }
725
726   public void setGlobalColorScheme(String globalColourScheme)
727   {
728     this.globalColourScheme = globalColourScheme;
729   }
730
731   @Override
732   public FeaturesDisplayedI getDisplayedFeatures()
733   {
734     return displayedFeatures;
735   }
736
737   public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures)
738   {
739     this.displayedFeatures = displayedFeatures;
740   }
741
742   @Override
743   public void configureForView(AlignmentViewPanel avpanel)
744   {
745     if (avpanel == null)
746     {
747       return;
748     }
749     super.configureForView(avpanel);
750     AlignViewportI viewport = avpanel.getAlignViewport();
751     AlignmentI alignment = viewport.getAlignment();
752     AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
753
754     seqGroups = alignment.getGroups();
755     fr = avpanel.cloneFeatureRenderer();
756
757     // Add non auto calculated annotation to AlignFile
758     if (annots != null)
759     {
760       for (AlignmentAnnotation annot : annots)
761       {
762         if (annot != null && !annot.autoCalculated)
763         {
764           annotations.add(annot);
765         }
766       }
767     }
768     globalColourScheme = ColourSchemeProperty
769             .getColourName(viewport.getGlobalColourScheme());
770     setDisplayedFeatures(viewport.getFeaturesDisplayed());
771     showSeqFeatures = viewport.isShowSequenceFeatures();
772
773   }
774
775   @Override
776   public boolean isShowSeqFeatures()
777   {
778     return showSeqFeatures;
779   }
780
781   public void setShowSeqFeatures(boolean showSeqFeatures)
782   {
783     this.showSeqFeatures = showSeqFeatures;
784   }
785
786   public Vector<AlignmentAnnotation> getAnnotations()
787   {
788     return annotations;
789   }
790
791   @Override
792   public HiddenColumns getHiddenColumns()
793   {
794     return hiddenColumns;
795   }
796
797   public void setHiddenColumns(HiddenColumns hidden)
798   {
799     this.hiddenColumns = hidden;
800   }
801
802   @Override
803   public SequenceI[] getHiddenSequences()
804   {
805     if (hiddenSequences == null || hiddenSequences.isEmpty())
806     {
807       return new SequenceI[] {};
808     }
809     synchronized (hiddenSequences)
810     {
811       return hiddenSequences.toArray(new SequenceI[hiddenSequences.size()]);
812     }
813   }
814
815   public void setHiddenSequences(ArrayList<SequenceI> hiddenSequences)
816   {
817     this.hiddenSequences = hiddenSequences;
818   }
819
820   public class JSONExportSettings
821   {
822     private boolean exportSequence;
823
824     private boolean exportSequenceFeatures;
825
826     private boolean exportAnnotations;
827
828     private boolean exportGroups;
829
830     private boolean exportJalviewSettings;
831
832     public boolean isExportSequence()
833     {
834       return exportSequence;
835     }
836
837     public void setExportSequence(boolean exportSequence)
838     {
839       this.exportSequence = exportSequence;
840     }
841
842     public boolean isExportSequenceFeatures()
843     {
844       return exportSequenceFeatures;
845     }
846
847     public void setExportSequenceFeatures(boolean exportSequenceFeatures)
848     {
849       this.exportSequenceFeatures = exportSequenceFeatures;
850     }
851
852     public boolean isExportAnnotations()
853     {
854       return exportAnnotations;
855     }
856
857     public void setExportAnnotations(boolean exportAnnotations)
858     {
859       this.exportAnnotations = exportAnnotations;
860     }
861
862     public boolean isExportGroups()
863     {
864       return exportGroups;
865     }
866
867     public void setExportGroups(boolean exportGroups)
868     {
869       this.exportGroups = exportGroups;
870     }
871
872     public boolean isExportJalviewSettings()
873     {
874       return exportJalviewSettings;
875     }
876
877     public void setExportJalviewSettings(boolean exportJalviewSettings)
878     {
879       this.exportJalviewSettings = exportJalviewSettings;
880     }
881   }
882
883   /**
884    * Returns a descriptor for suitable feature display settings with
885    * <ul>
886    * <li>ResNums or insertions features visible</li>
887    * <li>insertions features coloured red</li>
888    * <li>ResNum features coloured by label</li>
889    * <li>Insertions displayed above (on top of) ResNums</li>
890    * </ul>
891    */
892   @Override
893   public FeatureSettingsModelI getFeatureColourScheme()
894   {
895     return new PDBFeatureSettings();
896   }
897 }