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