JAL-1912 added support of tcoffee annotation and other per sequence annotation for...
[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.FeaturesDisplayedI;
30 import jalview.bin.BuildDetails;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.AlignmentI;
33 import jalview.datamodel.Annotation;
34 import jalview.datamodel.ColumnSelection;
35 import jalview.datamodel.HiddenSequences;
36 import jalview.datamodel.Sequence;
37 import jalview.datamodel.SequenceFeature;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.datamodel.SequenceI;
40 import jalview.json.binding.biojson.v1.AlignmentAnnotationPojo;
41 import jalview.json.binding.biojson.v1.AlignmentPojo;
42 import jalview.json.binding.biojson.v1.AnnotationDisplaySettingPojo;
43 import jalview.json.binding.biojson.v1.AnnotationPojo;
44 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
45 import jalview.json.binding.biojson.v1.SequenceFeaturesPojo;
46 import jalview.json.binding.biojson.v1.SequenceGrpPojo;
47 import jalview.json.binding.biojson.v1.SequencePojo;
48 import jalview.schemes.ColourSchemeProperty;
49 import jalview.schemes.UserColourScheme;
50 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
51
52 import java.awt.Color;
53 import java.io.IOException;
54 import java.io.Reader;
55 import java.util.ArrayList;
56 import java.util.Hashtable;
57 import java.util.Iterator;
58 import java.util.List;
59 import java.util.Vector;
60
61 import org.json.simple.JSONArray;
62 import org.json.simple.JSONObject;
63 import org.json.simple.parser.JSONParser;
64
65 public class JSONFile extends AlignFile implements ComplexAlignFile
66 {
67   private static String version = new BuildDetails().getVersion();
68
69   private String webstartUrl = "http://www.jalview.org/services/launchApp";
70
71   private String application = "Jalview";
72
73   public static final String FILE_EXT = "json";
74
75   public static final String FILE_DESC = "JSON";
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 List<int[]> hiddenColumns;
88
89   private ColumnSelection columnSelection;
90
91   private List<String> hiddenSeqRefs;
92
93   private ArrayList<SequenceI> hiddenSequences;
94
95   private final static String TCOFFEE_SCORE = "TCoffeeScore";
96
97   public JSONFile()
98   {
99     super();
100   }
101
102   public JSONFile(FileParse source) throws IOException
103   {
104     super(source);
105   }
106
107   public JSONFile(String inFile, String type) throws IOException
108   {
109     super(inFile, type);
110   }
111
112   @Override
113   public void parse() throws IOException
114   {
115     parse(getReader());
116
117   }
118
119   @Override
120   public String print()
121   {
122     String jsonOutput = null;
123     try
124     {
125       AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
126       AlignExportSettingI exportSettings = getExportSettings();
127
128       // if no export settings were supplied use the following with all values
129       // defaulting to true
130       if (exportSettings == null)
131       {
132         exportSettings = new AlignExportSettingI()
133         {
134           @Override
135           public boolean isExportHiddenSequences()
136           {
137             return true;
138           }
139
140           @Override
141           public boolean isExportHiddenColumns()
142           {
143             return true;
144           }
145
146           @Override
147           public boolean isExportGroups()
148           {
149             return true;
150           }
151
152           @Override
153           public boolean isExportFeatures()
154           {
155             return true;
156           }
157
158           @Override
159           public boolean isExportAnnotations()
160           {
161             return true;
162           }
163
164           @Override
165           public boolean isCancelled()
166           {
167             return false;
168           }
169         };
170       }
171
172       int count = 0;
173       for (SequenceI seq : seqs)
174       {
175         StringBuilder name = new StringBuilder();
176         name.append(seq.getName()).append("/").append(seq.getStart())
177                 .append("-").append(seq.getEnd());
178         SequencePojo jsonSeqPojo = new SequencePojo();
179         jsonSeqPojo.setId(String.valueOf(seq.hashCode()));
180         jsonSeqPojo.setOrder(++count);
181         jsonSeqPojo.setEnd(seq.getEnd());
182         jsonSeqPojo.setStart(seq.getStart());
183         jsonSeqPojo.setName(name.toString());
184         jsonSeqPojo.setSeq(seq.getSequenceAsString());
185         jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
186       }
187       jsonAlignmentPojo.setGlobalColorScheme(globalColourScheme);
188       jsonAlignmentPojo.getAppSettings().put("application", application);
189       jsonAlignmentPojo.getAppSettings().put("version", version);
190       jsonAlignmentPojo.getAppSettings().put("webStartUrl", webstartUrl);
191       jsonAlignmentPojo.getAppSettings().put("showSeqFeatures",
192               String.valueOf(showSeqFeatures));
193
194       String[] hiddenSections = getHiddenSections();
195       if (hiddenSections != null)
196       {
197         if (hiddenSections[0] != null
198                 && exportSettings.isExportHiddenColumns())
199         {
200           jsonAlignmentPojo.getAppSettings().put("hiddenCols",
201                   String.valueOf(hiddenSections[0]));
202         }
203         if (hiddenSections[1] != null
204                 && exportSettings.isExportHiddenSequences())
205         {
206           jsonAlignmentPojo.getAppSettings().put("hiddenSeqs",
207                   String.valueOf(hiddenSections[1]));
208         }
209       }
210
211       if (exportSettings.isExportAnnotations())
212       {
213         jsonAlignmentPojo
214 .setAlignAnnotation(annotationToJsonPojo(
215                 annotations, seqs));
216       }
217       else
218       {
219         if (globalColourScheme.equalsIgnoreCase("RNA Helices"))
220         {
221           jsonAlignmentPojo.setGlobalColorScheme("None");
222         }
223       }
224
225       if (exportSettings.isExportFeatures())
226       {
227         jsonAlignmentPojo
228                 .setSeqFeatures(sequenceFeatureToJsonPojo(seqs, 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           List<SequenceI> seqs, FeatureRenderer fr)
319   {
320     displayedFeatures = (fr == null) ? null : fr.getFeaturesDisplayed();
321     List<SequenceFeaturesPojo> sequenceFeaturesPojo = new ArrayList<SequenceFeaturesPojo>();
322     for (SequenceI seq : seqs)
323     {
324       SequenceI dataSetSequence = seq.getDatasetSequence();
325       SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null
326               : seq.getDatasetSequence().getSequenceFeatures();
327
328       seqFeatures = (seqFeatures == null) ? seq.getSequenceFeatures()
329               : seqFeatures;
330       if (seqFeatures == null)
331       {
332         continue;
333       }
334
335       for (SequenceFeature sf : seqFeatures)
336       {
337         if (displayedFeatures != null
338                 && displayedFeatures.isVisible(sf.getType()))
339         {
340           SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
341                   String.valueOf(seq.hashCode()));
342
343           String featureColour = (fr == null) ? null : jalview.util.Format
344                   .getHexString(fr.findFeatureColour(Color.white, seq,
345                           seq.findIndex(sf.getBegin())));
346           jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
347           jsonFeature.setXend(seq.findIndex(sf.getEnd()));
348           jsonFeature.setType(sf.getType());
349           jsonFeature.setDescription(sf.getDescription());
350           jsonFeature.setLinks(sf.links);
351           jsonFeature.setOtherDetails(sf.otherDetails);
352           jsonFeature.setScore(sf.getScore());
353           jsonFeature.setFillColor(featureColour);
354           jsonFeature.setFeatureGroup(sf.getFeatureGroup());
355           sequenceFeaturesPojo.add(jsonFeature);
356         }
357       }
358     }
359     return sequenceFeaturesPojo;
360   }
361
362   public static List<AlignmentAnnotationPojo> annotationToJsonPojo(
363           Vector<AlignmentAnnotation> annotations, Vector<SequenceI> seqs)
364   {
365     List<AlignmentAnnotationPojo> jsonAnnotations = new ArrayList<AlignmentAnnotationPojo>();
366     if (annotations == null)
367     {
368       return jsonAnnotations;
369     }
370     for (AlignmentAnnotation annot : annotations)
371     {
372       AnnotationDisplaySettingPojo annotSetting = new AnnotationDisplaySettingPojo();
373       annotSetting.setBelowAlignment(annot.belowAlignment);
374       annotSetting.setCentreColLabels(annot.centreColLabels);
375       annotSetting.setScaleColLabel(annot.centreColLabels);
376       annotSetting.setShowAllColLabels(annot.showAllColLabels);
377       annotSetting.setVisible(annot.visible);
378
379
380       AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
381       alignAnnotPojo.setScore(annot.score);
382       alignAnnotPojo.setCalcId(annot.getCalcId());
383       alignAnnotPojo.setDescription(annot.description);
384       alignAnnotPojo.setLabel(annot.label);
385       alignAnnotPojo.setGraphType(annot.graph);
386       alignAnnotPojo.setAnnotationSettings(annotSetting);
387       int seqHash = setAnnotationRefSeq(annot, seqs);
388       alignAnnotPojo.setSequenceRef(seqHash == 0 ? null : String
389               .valueOf(seqHash));
390       for (Annotation annotation : annot.annotations)
391       {
392         AnnotationPojo annotationPojo = new AnnotationPojo();
393         if (annotation != null)
394         {
395           annotationPojo.setDescription(annotation.description);
396           annotationPojo.setValue(annotation.value);
397           annotationPojo
398                   .setSecondaryStructure(annotation.secondaryStructure);
399           annotationPojo.setDisplayCharacter(annotation.displayCharacter);
400           if (annotation.colour != null)
401           {
402             annotationPojo.setColour(jalview.util.Format
403                     .getHexString(annotation.colour));
404           }
405           alignAnnotPojo.getAnnotations().add(annotationPojo);
406         }
407         else
408         {
409           if (annot.getCalcId() != null
410                   && annot.getCalcId().equalsIgnoreCase(TCOFFEE_SCORE))
411           {
412             // do nothing
413           }
414           else
415           {
416             alignAnnotPojo.getAnnotations().add(null);
417           }
418         }
419       }
420       jsonAnnotations.add(alignAnnotPojo);
421     }
422     return jsonAnnotations;
423   }
424
425   private static int setAnnotationRefSeq(AlignmentAnnotation annot,
426           Vector<SequenceI> seqs)
427   {
428     if (annot == null || seqs == null || seqs.size() == 0)
429     {
430       return 0;
431     }
432     for (SequenceI seq : seqs)
433     {
434       if (seq == null || seq.getAnnotation() == null)
435       {
436         continue;
437       }
438       for (AlignmentAnnotation seqAnnot : seq.getAnnotation())
439       {
440         if (seqAnnot == annot)
441         {
442           return seq.hashCode();
443         }
444       }
445     }
446     return 0;
447   }
448   @SuppressWarnings("unchecked")
449   public JSONFile parse(Reader jsonAlignmentString)
450   {
451     try
452     {
453       JSONParser jsonParser = new JSONParser();
454       JSONObject alignmentJsonObj = (JSONObject) jsonParser
455               .parse(jsonAlignmentString);
456       JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
457       JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj
458               .get("alignAnnotation");
459       JSONArray jsonSeqArray = (JSONArray) alignmentJsonObj
460               .get("seqFeatures");
461       JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
462               .get("seqGroups");
463       JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
464               .get("appSettings");
465
466       if (jvSettingsJsonObj != null)
467       {
468         globalColourScheme = (String) jvSettingsJsonObj
469                 .get("globalColorScheme");
470         Boolean showFeatures = Boolean.valueOf(jvSettingsJsonObj.get(
471                 "showSeqFeatures").toString());
472         setShowSeqFeatures(showFeatures);
473         parseHiddenSeqRefsAsList(jvSettingsJsonObj);
474         parseHiddenCols(jvSettingsJsonObj);
475       }
476
477       hiddenSequences = new ArrayList<SequenceI>();
478       seqMap = new Hashtable<String, Sequence>();
479       for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
480               .hasNext();)
481       {
482         JSONObject sequence = sequenceIter.next();
483         String sequcenceString = sequence.get("seq").toString();
484         String sequenceName = sequence.get("name").toString();
485         String seqUniqueId = sequence.get("id").toString();
486         int start = Integer.valueOf(sequence.get("start").toString());
487         int end = Integer.valueOf(sequence.get("end").toString());
488         Sequence seq = new Sequence(sequenceName, sequcenceString, start,
489                 end);
490         if (hiddenSeqRefs != null && hiddenSeqRefs.contains(seqUniqueId))
491         {
492           hiddenSequences.add(seq);
493         }
494         seqs.add(seq);
495         seqMap.put(seqUniqueId, seq);
496       }
497
498
499       parseFeatures(jsonSeqArray);
500
501       for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray.iterator(); seqGrpIter
502               .hasNext();)
503       {
504         JSONObject seqGrpObj = seqGrpIter.next();
505         String grpName = seqGrpObj.get("groupName").toString();
506         String colourScheme = seqGrpObj.get("colourScheme").toString();
507         String description = (seqGrpObj.get("description") == null) ? null
508                 : seqGrpObj.get("description").toString();
509         boolean displayBoxes = Boolean.valueOf(seqGrpObj
510                 .get("displayBoxes").toString());
511         boolean displayText = Boolean.valueOf(seqGrpObj.get("displayText")
512                 .toString());
513         boolean colourText = Boolean.valueOf(seqGrpObj.get("colourText")
514                 .toString());
515         boolean showNonconserved = Boolean.valueOf(seqGrpObj.get(
516                 "showNonconserved").toString());
517         int startRes = Integer
518                 .valueOf(seqGrpObj.get("startRes").toString());
519         int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
520         JSONArray sequenceRefs = (JSONArray) seqGrpObj.get("sequenceRefs");
521
522         ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
523         if (sequenceRefs.size() > 0)
524         {
525           Iterator<String> seqHashIter = sequenceRefs.iterator();
526           while (seqHashIter.hasNext())
527           {
528             String seqHash = seqHashIter.next();
529             Sequence sequence = seqMap.get(seqHash);
530             if (sequence != null)
531             {
532               grpSeqs.add(sequence);
533             }
534           }
535         }
536         SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, null,
537                 displayBoxes, displayText, colourText,
538                 startRes, endRes);
539         seqGrp.cs = ColourSchemeMapper.getJalviewColourScheme(colourScheme,
540                 seqGrp);
541         seqGrp.setShowNonconserved(showNonconserved);
542         seqGrp.setDescription(description);
543         this.seqGroups.add(seqGrp);
544
545       }
546
547       for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
548               .hasNext();)
549       {
550         JSONObject alAnnot = alAnnotIter.next();
551         if (alAnnot == null)
552         {
553           continue;
554         }
555         JSONObject diplaySettings = (JSONObject) alAnnot
556                 .get("annotationSettings");
557
558         JSONArray annotJsonArray = (JSONArray) alAnnot.get("annotations");
559         Annotation[] annotations = new Annotation[annotJsonArray.size()];
560         int count = 0;
561         String calcId = alAnnot.get("calcId") == null ? "" : alAnnot.get(
562                 "calcId").toString();
563         for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
564                 .hasNext();)
565         {
566           JSONObject annot = annotIter.next();
567           if (annot == null)
568           {
569             annotations[count] = null;
570           }
571           else
572           {
573             float val = annot.get("value") == null ? null : Float
574                     .valueOf(annot.get("value").toString());
575             String desc = annot.get("description") == null ? null : annot
576                     .get("description").toString();
577             char ss = annot.get("secondaryStructure") == null
578                     || annot.get("secondaryStructure").toString()
579                             .equalsIgnoreCase("u0000") ? ' ' : annot
580                     .get("secondaryStructure").toString().charAt(0);
581             String displayChar = annot.get("displayCharacter") == null ? ""
582                     : annot.get("displayCharacter").toString();
583             Color color = annot.get("colour") == null ? Color.white
584                     : UserColourScheme.getColourFromString(annot.get(
585                     "colour").toString());
586             annotations[count] = new Annotation(displayChar, desc, ss, val,
587                     color);
588           }
589           ++count;
590         }
591
592         AlignmentAnnotation alignAnnot = new AlignmentAnnotation(alAnnot
593                 .get("label").toString(), alAnnot.get("description")
594                 .toString(), annotations);
595         alignAnnot.graph = (alAnnot.get("graphType") == null) ? 0 : Integer
596                 .valueOf(alAnnot.get("graphType")
597                         .toString());
598         alignAnnot.scaleColLabel = (diplaySettings.get("scaleColLabel") == null) ? false
599                 : Boolean.valueOf(diplaySettings.get("scaleColLabel")
600                         .toString());
601         alignAnnot.showAllColLabels = (diplaySettings
602                 .get("showAllColLabels") == null) ? true : Boolean
603                 .valueOf(diplaySettings.get("showAllColLabels").toString());
604         alignAnnot.centreColLabels = (diplaySettings.get("centreColLabels") == null) ? true
605                 : Boolean.valueOf(diplaySettings.get("centreColLabels")
606                         .toString());
607         alignAnnot.belowAlignment = (diplaySettings.get("belowAlignment") == null) ? false
608                 : Boolean.valueOf(diplaySettings.get("belowAlignment")
609                         .toString());
610         alignAnnot.visible = (diplaySettings.get("visible") == null) ? true
611                 : Boolean.valueOf(diplaySettings.get("visible").toString());
612
613
614         alignAnnot.score = alAnnot.get("score") == null ? null : Double
615                 .valueOf(alAnnot.get("score").toString());
616         alignAnnot.setCalcId(calcId);
617         String seqHash = (alAnnot.get("sequenceRef") != null) ? alAnnot
618                 .get("sequenceRef").toString() : null;
619
620         Sequence sequence = (seqHash != null) ? seqMap.get(seqHash) : null;
621         if (sequence != null)
622         {
623           alignAnnot.sequenceRef = sequence;
624           sequence.addAlignmentAnnotation(alignAnnot);
625           if (alignAnnot.label.equalsIgnoreCase("T-COFFEE"))
626           {
627             alignAnnot.createSequenceMapping(sequence, sequence.getStart(),
628                     false);
629             sequence.addAlignmentAnnotation(alignAnnot);
630             alignAnnot.adjustForAlignment();
631           }
632         }
633         this.annotations.add(alignAnnot);
634         alignAnnot.validateRangeAndDisplay();
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   public String getGlobalColourScheme()
716   {
717     return globalColourScheme;
718   }
719
720   public void setGlobalColorScheme(String globalColourScheme)
721   {
722     this.globalColourScheme = globalColourScheme;
723   }
724
725   @Override
726   public FeaturesDisplayedI getDisplayedFeatures()
727   {
728     return displayedFeatures;
729   }
730
731   public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures)
732   {
733     this.displayedFeatures = displayedFeatures;
734   }
735
736   public void configureForView(AlignmentViewPanel avpanel)
737   {
738     super.configureForView(avpanel);
739     AlignViewportI viewport = avpanel.getAlignViewport();
740     AlignmentI alignment = viewport.getAlignment();
741     AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
742
743     seqGroups = alignment.getGroups();
744     fr = avpanel.cloneFeatureRenderer();
745
746     // Add non auto calculated annotation to AlignFile
747     for (AlignmentAnnotation annot : annots)
748     {
749       if (annot != null && !annot.autoCalculated)
750       {
751         annotations.add(annot);
752       }
753     }
754     globalColourScheme = ColourSchemeProperty.getColourName(viewport
755             .getGlobalColourScheme());
756     setDisplayedFeatures(viewport.getFeaturesDisplayed());
757     showSeqFeatures = viewport.isShowSequenceFeatures();
758
759   }
760
761   public boolean isShowSeqFeatures()
762   {
763     return showSeqFeatures;
764   }
765
766   public void setShowSeqFeatures(boolean showSeqFeatures)
767   {
768     this.showSeqFeatures = showSeqFeatures;
769   }
770
771   public Vector<AlignmentAnnotation> getAnnotations()
772   {
773     return annotations;
774   }
775
776   public List<int[]> getHiddenColumns()
777   {
778     return hiddenColumns;
779   }
780
781   public ColumnSelection getColumnSelection()
782   {
783     return columnSelection;
784   }
785
786   public void setColumnSelection(ColumnSelection columnSelection)
787   {
788     this.columnSelection = columnSelection;
789   }
790
791   public SequenceI[] getHiddenSequences()
792   {
793     if (hiddenSequences == null || hiddenSequences.isEmpty())
794     {
795       return new SequenceI[] {};
796     }
797     synchronized (hiddenSequences)
798     {
799       return hiddenSequences.toArray(new SequenceI[hiddenSequences.size()]);
800     }
801   }
802
803   public void setHiddenSequences(ArrayList<SequenceI> hiddenSequences)
804   {
805     this.hiddenSequences = hiddenSequences;
806   }
807
808   public class JSONExportSettings
809   {
810     private boolean exportSequence;
811
812     private boolean exportSequenceFeatures;
813
814     private boolean exportAnnotations;
815
816     private boolean exportGroups;
817
818     private boolean exportJalviewSettings;
819
820     public boolean isExportSequence()
821     {
822       return exportSequence;
823     }
824
825     public void setExportSequence(boolean exportSequence)
826     {
827       this.exportSequence = exportSequence;
828     }
829
830     public boolean isExportSequenceFeatures()
831     {
832       return exportSequenceFeatures;
833     }
834
835     public void setExportSequenceFeatures(boolean exportSequenceFeatures)
836     {
837       this.exportSequenceFeatures = exportSequenceFeatures;
838     }
839
840     public boolean isExportAnnotations()
841     {
842       return exportAnnotations;
843     }
844
845     public void setExportAnnotations(boolean exportAnnotations)
846     {
847       this.exportAnnotations = exportAnnotations;
848     }
849
850     public boolean isExportGroups()
851     {
852       return exportGroups;
853     }
854
855     public void setExportGroups(boolean exportGroups)
856     {
857       this.exportGroups = exportGroups;
858     }
859
860     public boolean isExportJalviewSettings()
861     {
862       return exportJalviewSettings;
863     }
864
865     public void setExportJalviewSettings(boolean exportJalviewSettings)
866     {
867       this.exportJalviewSettings = exportJalviewSettings;
868     }
869   }
870 }