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