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