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