JAL-3438 spotless for 2.11.2.0
[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()
295               .getAllFeatures(visibleFeatureTypes);
296       for (SequenceFeature sf : seqFeatures)
297       {
298         SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
299                 String.valueOf(seq.hashCode()));
300
301         String featureColour = (fr == null) ? null
302                 : Format.getHexString(finder.findFeatureColour(Color.white,
303                         seq, 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
399               .parse(jsonAlignmentString);
400       List<Object> seqJsonArray = (List<Object>) alignmentJsonObj
401               .get("seqs");
402       List<Object> alAnnotJsonArray = (List<Object>) alignmentJsonObj
403               .get("alignAnnotation");
404       List<Object> jsonSeqArray = (List<Object>) alignmentJsonObj
405               .get("seqFeatures");
406       List<Object> seqGrpJsonArray = (List<Object>) alignmentJsonObj
407               .get("seqGroups");
408       Map<String, Object> jvSettingsJsonObj = (Map<String, Object>) alignmentJsonObj
409               .get("appSettings");
410
411       if (jvSettingsJsonObj != null)
412       {
413         globalColourScheme = (String) jvSettingsJsonObj
414                 .get("globalColorScheme");
415         Boolean showFeatures = Boolean.valueOf(
416                 jvSettingsJsonObj.get("showSeqFeatures").toString());
417         setShowSeqFeatures(showFeatures);
418         parseHiddenSeqRefsAsList(jvSettingsJsonObj);
419         parseHiddenCols(jvSettingsJsonObj);
420       }
421
422       hiddenSequences = new ArrayList<>();
423       seqMap = new Hashtable<>();
424       for (Iterator<Object> sequenceIter = seqJsonArray
425               .iterator(); sequenceIter.hasNext();)
426       {
427         Map<String, Object> sequence = (Map<String, Object>) sequenceIter
428                 .next();
429         String sequcenceString = sequence.get("seq").toString();
430         String sequenceName = sequence.get("name").toString();
431         String seqUniqueId = sequence.get("id").toString();
432         int start = Integer.valueOf(sequence.get("start").toString());
433         int end = Integer.valueOf(sequence.get("end").toString());
434         Sequence seq = new Sequence(sequenceName, sequcenceString, start,
435                 end);
436         if (hiddenSeqRefs != null && hiddenSeqRefs.contains(seqUniqueId))
437         {
438           hiddenSequences.add(seq);
439         }
440         seqs.add(seq);
441         seqMap.put(seqUniqueId, seq);
442       }
443
444       parseFeatures(jsonSeqArray);
445
446       for (Iterator<Object> seqGrpIter = seqGrpJsonArray
447               .iterator(); seqGrpIter.hasNext();)
448       {
449         Map<String, Object> seqGrpObj = (Map<String, Object>) seqGrpIter
450                 .next();
451         String grpName = seqGrpObj.get("groupName").toString();
452         String colourScheme = seqGrpObj.get("colourScheme").toString();
453         String description = (seqGrpObj.get("description") == null) ? null
454                 : seqGrpObj.get("description").toString();
455         boolean displayBoxes = Boolean
456                 .valueOf(seqGrpObj.get("displayBoxes").toString());
457         boolean displayText = Boolean
458                 .valueOf(seqGrpObj.get("displayText").toString());
459         boolean colourText = Boolean
460                 .valueOf(seqGrpObj.get("colourText").toString());
461         boolean showNonconserved = Boolean
462                 .valueOf(seqGrpObj.get("showNonconserved").toString());
463         int startRes = Integer
464                 .valueOf(seqGrpObj.get("startRes").toString());
465         int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
466         List<Object> sequenceRefs = (List<Object>) seqGrpObj
467                 .get("sequenceRefs");
468
469         ArrayList<SequenceI> grpSeqs = new ArrayList<>();
470         if (sequenceRefs.size() > 0)
471         {
472           Iterator<Object> seqHashIter = sequenceRefs.iterator();
473           while (seqHashIter.hasNext())
474           {
475             Sequence sequence = seqMap.get(seqHashIter.next());
476             if (sequence != null)
477             {
478               grpSeqs.add(sequence);
479             }
480           }
481         }
482         SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, null,
483                 displayBoxes, displayText, colourText, startRes, endRes);
484         seqGrp.setColourScheme(ColourSchemeMapper
485                 .getJalviewColourScheme(colourScheme, seqGrp));
486         seqGrp.setShowNonconserved(showNonconserved);
487         seqGrp.setDescription(description);
488         this.seqGroups.add(seqGrp);
489
490       }
491
492       for (Iterator<Object> alAnnotIter = alAnnotJsonArray
493               .iterator(); alAnnotIter.hasNext();)
494       {
495         Map<String, Object> alAnnot = (Map<String, Object>) alAnnotIter
496                 .next();
497         List<Object> annotJsonArray = (List<Object>) alAnnot
498                 .get("annotations");
499         Annotation[] annotations = new Annotation[annotJsonArray.size()];
500         int count = 0;
501         for (Iterator<Object> annotIter = annotJsonArray
502                 .iterator(); annotIter.hasNext();)
503         {
504           Map<String, Object> annot = (Map<String, Object>) annotIter
505                   .next();
506           if (annot == null)
507           {
508             annotations[count] = null;
509           }
510           else
511           {
512             float val = annot.get("value") == null ? null
513                     : Float.valueOf(annot.get("value").toString());
514             String desc = annot.get("description") == null ? null
515                     : annot.get("description").toString();
516             char ss = annot.get("secondaryStructure") == null
517                     || annot.get("secondaryStructure").toString()
518                             .equalsIgnoreCase("u0000") ? ' '
519                                     : annot.get("secondaryStructure")
520                                             .toString().charAt(0);
521             String displayChar = annot.get("displayCharacter") == null ? ""
522                     : annot.get("displayCharacter").toString();
523
524             annotations[count] = new Annotation(displayChar, desc, ss, val);
525             if (annot.get("colour") != null)
526             {
527               Color color = ColorUtils
528                       .parseColourString(annot.get("colour").toString());
529               annotations[count].colour = color;
530             }
531           }
532           ++count;
533         }
534
535         AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
536                 alAnnot.get("label").toString(),
537                 alAnnot.get("description").toString(), annotations);
538         alignAnnot.graph = (alAnnot.get("graphType") == null) ? 0
539                 : Integer.valueOf(alAnnot.get("graphType").toString());
540
541         Map<String, Object> diplaySettings = (Map<String, Object>) alAnnot
542                 .get("annotationSettings");
543         if (diplaySettings != null)
544         {
545
546           alignAnnot.scaleColLabel = (diplaySettings
547                   .get("scaleColLabel") == null) ? false
548                           : Boolean.valueOf(diplaySettings
549                                   .get("scaleColLabel").toString());
550           alignAnnot.showAllColLabels = (diplaySettings
551                   .get("showAllColLabels") == null) ? true
552                           : Boolean.valueOf(diplaySettings
553                                   .get("showAllColLabels").toString());
554           alignAnnot.centreColLabels = (diplaySettings
555                   .get("centreColLabels") == null) ? true
556                           : Boolean.valueOf(diplaySettings
557                                   .get("centreColLabels").toString());
558           alignAnnot.belowAlignment = (diplaySettings
559                   .get("belowAlignment") == null) ? false
560                           : Boolean.valueOf(diplaySettings
561                                   .get("belowAlignment").toString());
562           alignAnnot.visible = (diplaySettings.get("visible") == null)
563                   ? true
564                   : Boolean.valueOf(
565                           diplaySettings.get("visible").toString());
566           alignAnnot.hasIcons = (diplaySettings.get("hasIcon") == null)
567                   ? true
568                   : Boolean.valueOf(
569                           diplaySettings.get("hasIcon").toString());
570
571         }
572         if (alAnnot.get("score") != null)
573         {
574           alignAnnot.score = Double
575                   .valueOf(alAnnot.get("score").toString());
576         }
577
578         String calcId = (alAnnot.get("calcId") == null) ? ""
579                 : alAnnot.get("calcId").toString();
580         alignAnnot.setCalcId(calcId);
581         String seqHash = (alAnnot.get("sequenceRef") != null)
582                 ? alAnnot.get("sequenceRef").toString()
583                 : null;
584
585         Sequence sequence = (seqHash != null) ? seqMap.get(seqHash) : null;
586         if (sequence != null)
587         {
588           alignAnnot.sequenceRef = sequence;
589           sequence.addAlignmentAnnotation(alignAnnot);
590           if (alignAnnot.label.equalsIgnoreCase("T-COFFEE"))
591           {
592             alignAnnot.createSequenceMapping(sequence, sequence.getStart(),
593                     false);
594             sequence.addAlignmentAnnotation(alignAnnot);
595             alignAnnot.adjustForAlignment();
596           }
597         }
598         alignAnnot.validateRangeAndDisplay();
599         this.annotations.add(alignAnnot);
600
601       }
602     } catch (Exception e)
603     {
604       e.printStackTrace();
605     }
606     return this;
607   }
608
609   public void parseHiddenSeqRefsAsList(Map<String, Object> jvSettingsJson)
610   {
611     hiddenSeqRefs = new ArrayList<>();
612     String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs");
613     if (hiddenSeqs != null && !hiddenSeqs.isEmpty())
614     {
615       String[] seqRefs = hiddenSeqs.split(";");
616       for (int i = 0, n = seqRefs.length; i < n; i++)
617       {
618         hiddenSeqRefs.add(seqRefs[i]);
619       }
620     }
621   }
622
623   public void parseHiddenCols(Map<String, Object> jvSettingsJson)
624   {
625     String hiddenCols = (String) jvSettingsJson.get("hiddenCols");
626     if (hiddenCols != null && !hiddenCols.isEmpty())
627     {
628       hiddenColumns = new HiddenColumns();
629       String[] rangeStrings = hiddenCols.split(";");
630       for (int i = 0, n = rangeStrings.length; i < n; i++)
631       {
632         String[] range = rangeStrings[i].split("-");
633         hiddenColumns.hideColumns(Integer.valueOf(range[0]),
634                 Integer.valueOf(range[1]));
635       }
636     }
637   }
638
639   @SuppressWarnings("unchecked")
640   private void parseFeatures(List<Object> jsonSeqFeatures)
641   {
642     if (jsonSeqFeatures != null)
643     {
644       displayedFeatures = new FeaturesDisplayed();
645       for (Object o : jsonSeqFeatures)
646       {
647         Map<String, Object> jsonFeature = (Map<String, Object>) o;
648         Long begin = (Long) jsonFeature.get("xStart");
649         Long end = (Long) jsonFeature.get("xEnd");
650         String type = (String) jsonFeature.get("type");
651         String featureGrp = (String) jsonFeature.get("featureGroup");
652         String description = (String) jsonFeature.get("description");
653         String seqRef = (String) jsonFeature.get("sequenceRef");
654         Float score = Float.valueOf(jsonFeature.get("score").toString());
655
656         Sequence seq = seqMap.get(seqRef);
657
658         /*
659          * begin/end of 0 is for a non-positional feature
660          */
661         int featureBegin = begin.intValue() == 0 ? 0
662                 : seq.findPosition(begin.intValue());
663         int featureEnd = end.intValue() == 0 ? 0
664                 : seq.findPosition(end.intValue()) - 1;
665
666         SequenceFeature sequenceFeature = new SequenceFeature(type,
667                 description, featureBegin, featureEnd, score, featureGrp);
668
669         List<Object> linksJsonArray = (List<Object>) jsonFeature
670                 .get("links");
671         if (linksJsonArray != null && linksJsonArray.size() > 0)
672         {
673           Iterator<Object> linkList = linksJsonArray.iterator();
674           while (linkList.hasNext())
675           {
676             sequenceFeature.addLink((String) linkList.next());
677           }
678         }
679
680         seq.addSequenceFeature(sequenceFeature);
681         displayedFeatures.setVisible(type);
682       }
683     }
684   }
685
686   @Override
687   public String getGlobalColourScheme()
688   {
689     return globalColourScheme;
690   }
691
692   public void setGlobalColorScheme(String globalColourScheme)
693   {
694     this.globalColourScheme = globalColourScheme;
695   }
696
697   @Override
698   public FeaturesDisplayedI getDisplayedFeatures()
699   {
700     return displayedFeatures;
701   }
702
703   public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures)
704   {
705     this.displayedFeatures = displayedFeatures;
706   }
707
708   @Override
709   public void configureForView(AlignmentViewPanel avpanel)
710   {
711     if (avpanel == null)
712     {
713       return;
714     }
715     super.configureForView(avpanel);
716     AlignViewportI viewport = avpanel.getAlignViewport();
717     AlignmentI alignment = viewport.getAlignment();
718     AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
719
720     seqGroups = alignment.getGroups();
721     fr = avpanel.cloneFeatureRenderer();
722
723     // Add non auto calculated annotation to AlignFile
724     if (annots != null)
725     {
726       for (AlignmentAnnotation annot : annots)
727       {
728         if (annot != null && !annot.autoCalculated)
729         {
730           annotations.add(annot);
731         }
732       }
733     }
734     globalColourScheme = ColourSchemeProperty
735             .getColourName(viewport.getGlobalColourScheme());
736     setDisplayedFeatures(viewport.getFeaturesDisplayed());
737     showSeqFeatures = viewport.isShowSequenceFeatures();
738
739   }
740
741   @Override
742   public boolean isShowSeqFeatures()
743   {
744     return showSeqFeatures;
745   }
746
747   public void setShowSeqFeatures(boolean showSeqFeatures)
748   {
749     this.showSeqFeatures = showSeqFeatures;
750   }
751
752   public Vector<AlignmentAnnotation> getAnnotations()
753   {
754     return annotations;
755   }
756
757   @Override
758   public HiddenColumns getHiddenColumns()
759   {
760     return hiddenColumns;
761   }
762
763   public void setHiddenColumns(HiddenColumns hidden)
764   {
765     this.hiddenColumns = hidden;
766   }
767
768   @Override
769   public SequenceI[] getHiddenSequences()
770   {
771     if (hiddenSequences == null || hiddenSequences.isEmpty())
772     {
773       return new SequenceI[] {};
774     }
775     synchronized (hiddenSequences)
776     {
777       return hiddenSequences.toArray(new SequenceI[hiddenSequences.size()]);
778     }
779   }
780
781   public void setHiddenSequences(ArrayList<SequenceI> hiddenSequences)
782   {
783     this.hiddenSequences = hiddenSequences;
784   }
785
786   public class JSONExportSettings
787   {
788     private boolean exportSequence;
789
790     private boolean exportSequenceFeatures;
791
792     private boolean exportAnnotations;
793
794     private boolean exportGroups;
795
796     private boolean exportJalviewSettings;
797
798     public boolean isExportSequence()
799     {
800       return exportSequence;
801     }
802
803     public void setExportSequence(boolean exportSequence)
804     {
805       this.exportSequence = exportSequence;
806     }
807
808     public boolean isExportSequenceFeatures()
809     {
810       return exportSequenceFeatures;
811     }
812
813     public void setExportSequenceFeatures(boolean exportSequenceFeatures)
814     {
815       this.exportSequenceFeatures = exportSequenceFeatures;
816     }
817
818     public boolean isExportAnnotations()
819     {
820       return exportAnnotations;
821     }
822
823     public void setExportAnnotations(boolean exportAnnotations)
824     {
825       this.exportAnnotations = exportAnnotations;
826     }
827
828     public boolean isExportGroups()
829     {
830       return exportGroups;
831     }
832
833     public void setExportGroups(boolean exportGroups)
834     {
835       this.exportGroups = exportGroups;
836     }
837
838     public boolean isExportJalviewSettings()
839     {
840       return exportJalviewSettings;
841     }
842
843     public void setExportJalviewSettings(boolean exportJalviewSettings)
844     {
845       this.exportJalviewSettings = exportJalviewSettings;
846     }
847   }
848
849   /**
850    * Returns a descriptor for suitable feature display settings with
851    * <ul>
852    * <li>ResNums or insertions features visible</li>
853    * <li>insertions features coloured red</li>
854    * <li>ResNum features coloured by label</li>
855    * <li>Insertions displayed above (on top of) ResNums</li>
856    * </ul>
857    */
858   @Override
859   public FeatureSettingsModelI getFeatureColourScheme()
860   {
861     return new PDBFeatureSettings();
862   }
863 }