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