dcf0f5d885b0cdf622e4fed1514d4a727a42ae4b
[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.JalviewColourScheme;
51 import jalview.schemes.ResidueColourScheme;
52 import jalview.util.ColorUtils;
53 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
54
55 import java.awt.Color;
56 import java.io.IOException;
57 import java.io.Reader;
58 import java.util.ArrayList;
59 import java.util.Hashtable;
60 import java.util.Iterator;
61 import java.util.List;
62 import java.util.Vector;
63
64 import org.json.simple.JSONArray;
65 import org.json.simple.JSONObject;
66 import org.json.simple.parser.JSONParser;
67
68 public class JSONFile extends AlignFile implements ComplexAlignFile
69 {
70   private static String version = new BuildDetails().getVersion();
71
72   private String webstartUrl = "http://www.jalview.org/services/launchApp";
73
74   private String application = "Jalview";
75
76   private String globalColourScheme;
77
78   private boolean showSeqFeatures;
79
80   private Hashtable<String, Sequence> seqMap;
81
82   private FeaturesDisplayedI displayedFeatures;
83
84   private FeatureRenderer fr;
85
86   private List<int[]> hiddenColumnsList;
87
88   private HiddenColumns hiddenColumns;
89
90   private List<String> hiddenSeqRefs;
91
92   private ArrayList<SequenceI> hiddenSequences;
93
94   private final static String TCOFFEE_SCORE = "TCoffeeScore";
95
96   public JSONFile()
97   {
98     super();
99   }
100
101   public JSONFile(FileParse source) throws IOException
102   {
103     super(source);
104   }
105
106   public JSONFile(String inFile, DataSourceType sourceType)
107           throws IOException
108   {
109     super(inFile, sourceType);
110   }
111
112   @Override
113   public void parse() throws IOException
114   {
115     parse(getReader());
116
117   }
118
119   @Override
120   public String print(SequenceI[] sqs, boolean jvsuffix)
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 : sqs)
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(annotations));
215       }
216       else
217       {
218         // These color schemes require annotation, disable them if annotations
219         // are not exported
220         if (globalColourScheme
221                 .equalsIgnoreCase(JalviewColourScheme.RNAHelices.toString())
222                 || globalColourScheme
223                         .equalsIgnoreCase(JalviewColourScheme.TCoffee
224                                 .toString()))
225         {
226           jsonAlignmentPojo.setGlobalColorScheme(ResidueColourScheme.NONE);
227         }
228       }
229
230       if (exportSettings.isExportFeatures())
231       {
232         jsonAlignmentPojo
233                 .setSeqFeatures(sequenceFeatureToJsonPojo(sqs, fr));
234       }
235
236       if (exportSettings.isExportGroups() && seqGroups != null
237               && seqGroups.size() > 0)
238       {
239         for (SequenceGroup seqGrp : seqGroups)
240         {
241           SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
242           seqGrpPojo.setGroupName(seqGrp.getName());
243           seqGrpPojo.setColourScheme(seqGrp.getColourScheme()
244                   .getSchemeName());
245           seqGrpPojo.setColourText(seqGrp.getColourText());
246           seqGrpPojo.setDescription(seqGrp.getDescription());
247           seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
248           seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
249           seqGrpPojo.setEndRes(seqGrp.getEndRes());
250           seqGrpPojo.setStartRes(seqGrp.getStartRes());
251           seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
252           for (SequenceI seq : seqGrp.getSequences())
253           {
254             seqGrpPojo.getSequenceRefs()
255                     .add(String.valueOf(seq.hashCode()));
256           }
257           jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo);
258         }
259       }
260       org.json.JSONObject generatedJSon = new org.json.JSONObject(
261               jsonAlignmentPojo);
262       jsonOutput = generatedJSon.toString();
263       return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend",
264               "xEnd");
265     } catch (Exception e)
266     {
267       e.printStackTrace();
268     }
269     return jsonOutput;
270   }
271
272   public String[] getHiddenSections()
273   {
274     String[] hiddenSections = new String[2];
275     if (getViewport() == null)
276     {
277       return null;
278     }
279
280     // hidden column business
281     if (getViewport().hasHiddenColumns())
282     {
283       List<int[]> hiddenCols = getViewport().getAlignment()
284               .getHiddenColumns()
285               .getListOfCols();
286       StringBuilder hiddenColsBuilder = new StringBuilder();
287       for (int[] range : hiddenCols)
288       {
289         hiddenColsBuilder.append(";").append(range[0]).append("-")
290                 .append(range[1]);
291       }
292
293       hiddenColsBuilder.deleteCharAt(0);
294       hiddenSections[0] = hiddenColsBuilder.toString();
295     }
296
297     // hidden rows/seqs business
298     HiddenSequences hiddenSeqsObj = getViewport().getAlignment()
299             .getHiddenSequences();
300     if (hiddenSeqsObj == null || hiddenSeqsObj.hiddenSequences == null)
301     {
302       return hiddenSections;
303     }
304
305     SequenceI[] hiddenSeqs = hiddenSeqsObj.hiddenSequences;
306     StringBuilder hiddenSeqsBuilder = new StringBuilder();
307     for (SequenceI hiddenSeq : hiddenSeqs)
308     {
309       if (hiddenSeq != null)
310       {
311         hiddenSeqsBuilder.append(";").append(hiddenSeq.hashCode());
312       }
313     }
314     if (hiddenSeqsBuilder.length() > 0)
315     {
316       hiddenSeqsBuilder.deleteCharAt(0);
317     }
318     hiddenSections[1] = hiddenSeqsBuilder.toString();
319
320     return hiddenSections;
321   }
322
323   public List<SequenceFeaturesPojo> sequenceFeatureToJsonPojo(
324           SequenceI[] sqs, FeatureRenderer fr)
325   {
326     displayedFeatures = (fr == null) ? null : fr.getFeaturesDisplayed();
327     List<SequenceFeaturesPojo> sequenceFeaturesPojo = new ArrayList<SequenceFeaturesPojo>();
328     if (sqs == null)
329     {
330       return sequenceFeaturesPojo;
331     }
332
333     FeatureColourFinder finder = new FeatureColourFinder(fr);
334
335     for (SequenceI seq : sqs)
336     {
337       SequenceI dataSetSequence = seq.getDatasetSequence();
338       SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null
339               : seq.getDatasetSequence().getSequenceFeatures();
340
341       seqFeatures = (seqFeatures == null) ? seq.getSequenceFeatures()
342               : seqFeatures;
343       if (seqFeatures == null)
344       {
345         continue;
346       }
347
348       for (SequenceFeature sf : seqFeatures)
349       {
350         if (displayedFeatures != null
351                 && displayedFeatures.isVisible(sf.getType()))
352         {
353           SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
354                   String.valueOf(seq.hashCode()));
355
356           String featureColour = (fr == null) ? null : jalview.util.Format
357                   .getHexString(finder.findFeatureColour(Color.white, seq,
358                           seq.findIndex(sf.getBegin())));
359           jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
360           jsonFeature.setXend(seq.findIndex(sf.getEnd()));
361           jsonFeature.setType(sf.getType());
362           jsonFeature.setDescription(sf.getDescription());
363           jsonFeature.setLinks(sf.links);
364           jsonFeature.setOtherDetails(sf.otherDetails);
365           jsonFeature.setScore(sf.getScore());
366           jsonFeature.setFillColor(featureColour);
367           jsonFeature.setFeatureGroup(sf.getFeatureGroup());
368           sequenceFeaturesPojo.add(jsonFeature);
369         }
370       }
371     }
372     return sequenceFeaturesPojo;
373   }
374
375   public static List<AlignmentAnnotationPojo> annotationToJsonPojo(
376           Vector<AlignmentAnnotation> annotations)
377   {
378     List<AlignmentAnnotationPojo> jsonAnnotations = new ArrayList<AlignmentAnnotationPojo>();
379     if (annotations == null)
380     {
381       return jsonAnnotations;
382     }
383     for (AlignmentAnnotation annot : annotations)
384     {
385       AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
386       alignAnnotPojo.setDescription(annot.description);
387       alignAnnotPojo.setLabel(annot.label);
388       if (!Double.isNaN(annot.score))
389       {
390         alignAnnotPojo.setScore(annot.score);
391       }
392       alignAnnotPojo.setCalcId(annot.getCalcId());
393       alignAnnotPojo.setGraphType(annot.graph);
394
395       AnnotationDisplaySettingPojo annotSetting = new AnnotationDisplaySettingPojo();
396       annotSetting.setBelowAlignment(annot.belowAlignment);
397       annotSetting.setCentreColLabels(annot.centreColLabels);
398       annotSetting.setScaleColLabel(annot.scaleColLabel);
399       annotSetting.setShowAllColLabels(annot.showAllColLabels);
400       annotSetting.setVisible(annot.visible);
401       annotSetting.setHasIcon(annot.hasIcons);
402       alignAnnotPojo.setAnnotationSettings(annotSetting);
403       SequenceI refSeq = annot.sequenceRef;
404       if (refSeq != null)
405       {
406         alignAnnotPojo.setSequenceRef(String.valueOf(refSeq.hashCode()));
407       }
408       for (Annotation annotation : annot.annotations)
409       {
410         AnnotationPojo annotationPojo = new AnnotationPojo();
411         if (annotation != null)
412         {
413           annotationPojo.setDescription(annotation.description);
414           annotationPojo.setValue(annotation.value);
415           annotationPojo
416                   .setSecondaryStructure(annotation.secondaryStructure);
417           String displayChar = annotation.displayCharacter == null ? null
418                   : annotation.displayCharacter;
419           // System.out.println("--------------------->[" + displayChar + "]");
420           annotationPojo.setDisplayCharacter(displayChar);
421           if (annotation.colour != null)
422           {
423             annotationPojo.setColour(jalview.util.Format
424                     .getHexString(annotation.colour));
425           }
426           alignAnnotPojo.getAnnotations().add(annotationPojo);
427         }
428         else
429         {
430           if (annot.getCalcId() != null
431                   && annot.getCalcId().equalsIgnoreCase(TCOFFEE_SCORE))
432           {
433             // do nothing
434           }
435           else
436           {
437             alignAnnotPojo.getAnnotations().add(annotationPojo);
438           }
439         }
440       }
441       jsonAnnotations.add(alignAnnotPojo);
442     }
443     return jsonAnnotations;
444   }
445
446   @SuppressWarnings("unchecked")
447   public JSONFile parse(Reader jsonAlignmentString)
448   {
449     try
450     {
451       JSONParser jsonParser = new JSONParser();
452       JSONObject alignmentJsonObj = (JSONObject) jsonParser
453               .parse(jsonAlignmentString);
454       JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
455       JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj
456               .get("alignAnnotation");
457       JSONArray jsonSeqArray = (JSONArray) alignmentJsonObj
458               .get("seqFeatures");
459       JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
460               .get("seqGroups");
461       JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
462               .get("appSettings");
463
464       if (jvSettingsJsonObj != null)
465       {
466         globalColourScheme = (String) jvSettingsJsonObj
467                 .get("globalColorScheme");
468         Boolean showFeatures = Boolean.valueOf(jvSettingsJsonObj.get(
469                 "showSeqFeatures").toString());
470         setShowSeqFeatures(showFeatures);
471         parseHiddenSeqRefsAsList(jvSettingsJsonObj);
472         parseHiddenCols(jvSettingsJsonObj);
473       }
474
475       hiddenSequences = new ArrayList<SequenceI>();
476       seqMap = new Hashtable<String, Sequence>();
477       for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
478               .hasNext();)
479       {
480         JSONObject sequence = sequenceIter.next();
481         String sequcenceString = sequence.get("seq").toString();
482         String sequenceName = sequence.get("name").toString();
483         String seqUniqueId = sequence.get("id").toString();
484         int start = Integer.valueOf(sequence.get("start").toString());
485         int end = Integer.valueOf(sequence.get("end").toString());
486         Sequence seq = new Sequence(sequenceName, sequcenceString, start,
487                 end);
488         if (hiddenSeqRefs != null && hiddenSeqRefs.contains(seqUniqueId))
489         {
490           hiddenSequences.add(seq);
491         }
492         seqs.add(seq);
493         seqMap.put(seqUniqueId, seq);
494       }
495
496       parseFeatures(jsonSeqArray);
497
498       for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray.iterator(); seqGrpIter
499               .hasNext();)
500       {
501         JSONObject seqGrpObj = seqGrpIter.next();
502         String grpName = seqGrpObj.get("groupName").toString();
503         String colourScheme = seqGrpObj.get("colourScheme").toString();
504         String description = (seqGrpObj.get("description") == null) ? null
505                 : seqGrpObj.get("description").toString();
506         boolean displayBoxes = Boolean.valueOf(seqGrpObj
507                 .get("displayBoxes").toString());
508         boolean displayText = Boolean.valueOf(seqGrpObj.get("displayText")
509                 .toString());
510         boolean colourText = Boolean.valueOf(seqGrpObj.get("colourText")
511                 .toString());
512         boolean showNonconserved = Boolean.valueOf(seqGrpObj.get(
513                 "showNonconserved").toString());
514         int startRes = Integer
515                 .valueOf(seqGrpObj.get("startRes").toString());
516         int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString());
517         JSONArray sequenceRefs = (JSONArray) seqGrpObj.get("sequenceRefs");
518
519         ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
520         if (sequenceRefs.size() > 0)
521         {
522           Iterator<String> seqHashIter = sequenceRefs.iterator();
523           while (seqHashIter.hasNext())
524           {
525             String seqHash = seqHashIter.next();
526             Sequence sequence = seqMap.get(seqHash);
527             if (sequence != null)
528             {
529               grpSeqs.add(sequence);
530             }
531           }
532         }
533         SequenceGroup seqGrp = new SequenceGroup(grpSeqs, grpName, null,
534                 displayBoxes, displayText, colourText, startRes, endRes);
535         seqGrp.setColourScheme(ColourSchemeMapper.getJalviewColourScheme(
536                 colourScheme, seqGrp));
537         seqGrp.setShowNonconserved(showNonconserved);
538         seqGrp.setDescription(description);
539         this.seqGroups.add(seqGrp);
540
541       }
542
543       for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
544               .hasNext();)
545       {
546         JSONObject alAnnot = alAnnotIter.next();
547         JSONArray annotJsonArray = (JSONArray) alAnnot.get("annotations");
548         Annotation[] annotations = new Annotation[annotJsonArray.size()];
549         int count = 0;
550         for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
551                 .hasNext();)
552         {
553           JSONObject annot = annotIter.next();
554           if (annot == null)
555           {
556             annotations[count] = null;
557           }
558           else
559           {
560             float val = annot.get("value") == null ? null : Float
561                     .valueOf(annot.get("value").toString());
562             String desc = annot.get("description") == null ? null : annot
563                     .get("description").toString();
564             char ss = annot.get("secondaryStructure") == null
565                     || annot.get("secondaryStructure").toString()
566                             .equalsIgnoreCase("u0000") ? ' ' : annot
567                     .get("secondaryStructure").toString().charAt(0);
568             String displayChar = annot.get("displayCharacter") == null ? ""
569                     : annot.get("displayCharacter").toString();
570
571             annotations[count] = new Annotation(displayChar, desc, ss, val);
572             if (annot.get("colour") != null)
573             {
574               Color color = ColorUtils.parseColourString(annot.get(
575                       "colour").toString());
576               annotations[count].colour = color;
577             }
578           }
579           ++count;
580         }
581
582         AlignmentAnnotation alignAnnot = new AlignmentAnnotation(alAnnot
583                 .get("label").toString(), alAnnot.get("description")
584                 .toString(), annotations);
585         alignAnnot.graph = (alAnnot.get("graphType") == null) ? 0 : Integer
586                 .valueOf(alAnnot.get("graphType").toString());
587
588         JSONObject diplaySettings = (JSONObject) alAnnot
589                 .get("annotationSettings");
590         if (diplaySettings != null)
591         {
592
593           alignAnnot.scaleColLabel = (diplaySettings.get("scaleColLabel") == null) ? false
594                   : Boolean.valueOf(diplaySettings.get("scaleColLabel")
595                           .toString());
596           alignAnnot.showAllColLabels = (diplaySettings
597                   .get("showAllColLabels") == null) ? true : Boolean
598                   .valueOf(diplaySettings.get("showAllColLabels")
599                           .toString());
600           alignAnnot.centreColLabels = (diplaySettings
601                   .get("centreColLabels") == null) ? true
602                   : Boolean.valueOf(diplaySettings.get("centreColLabels")
603                           .toString());
604           alignAnnot.belowAlignment = (diplaySettings.get("belowAlignment") == null) ? false
605                   : Boolean.valueOf(diplaySettings.get("belowAlignment")
606                           .toString());
607           alignAnnot.visible = (diplaySettings.get("visible") == null) ? true
608                   : Boolean.valueOf(diplaySettings.get("visible")
609                           .toString());
610           alignAnnot.hasIcons = (diplaySettings.get("hasIcon") == null) ? true
611                   : Boolean.valueOf(diplaySettings.get("hasIcon")
612                           .toString());
613
614         }
615         if (alAnnot.get("score") != null)
616         {
617           alignAnnot.score = Double
618                   .valueOf(alAnnot.get("score").toString());
619         }
620
621         String calcId = (alAnnot.get("calcId") == null) ? "" : alAnnot.get(
622                 "calcId").toString();
623         alignAnnot.setCalcId(calcId);
624         String seqHash = (alAnnot.get("sequenceRef") != null) ? alAnnot
625                 .get("sequenceRef").toString() : null;
626
627         Sequence sequence = (seqHash != null) ? seqMap.get(seqHash) : null;
628         if (sequence != null)
629         {
630           alignAnnot.sequenceRef = sequence;
631           sequence.addAlignmentAnnotation(alignAnnot);
632           if (alignAnnot.label.equalsIgnoreCase("T-COFFEE"))
633           {
634             alignAnnot.createSequenceMapping(sequence, sequence.getStart(),
635                     false);
636             sequence.addAlignmentAnnotation(alignAnnot);
637             alignAnnot.adjustForAlignment();
638           }
639         }
640         alignAnnot.validateRangeAndDisplay();
641         this.annotations.add(alignAnnot);
642
643       }
644     } catch (Exception e)
645     {
646       e.printStackTrace();
647     }
648     return this;
649   }
650
651   public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson)
652   {
653     hiddenSeqRefs = new ArrayList<String>();
654     String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs");
655     if (hiddenSeqs != null && !hiddenSeqs.isEmpty())
656     {
657       String[] seqRefs = hiddenSeqs.split(";");
658       for (String seqRef : seqRefs)
659       {
660         hiddenSeqRefs.add(seqRef);
661       }
662     }
663   }
664
665   public void parseHiddenCols(JSONObject jvSettingsJson)
666   {
667     String hiddenCols = (String) jvSettingsJson.get("hiddenCols");
668     if (hiddenCols != null && !hiddenCols.isEmpty())
669     {
670       HiddenColumns hidden = new HiddenColumns();
671       String[] rangeStrings = hiddenCols.split(";");
672       for (String rangeString : rangeStrings)
673       {
674         String[] range = rangeString.split("-");
675         hidden.hideColumns(Integer.valueOf(range[0]),
676                 Integer.valueOf(range[1]));
677       }
678     }
679   }
680
681   @SuppressWarnings("unchecked")
682   private void parseFeatures(JSONArray jsonSeqFeatures)
683   {
684     if (jsonSeqFeatures != null)
685     {
686       displayedFeatures = new FeaturesDisplayed();
687       for (Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
688               .hasNext();)
689       {
690         JSONObject jsonFeature = seqFeatureItr.next();
691         Long begin = (Long) jsonFeature.get("xStart");
692         Long end = (Long) jsonFeature.get("xEnd");
693         String type = (String) jsonFeature.get("type");
694         String featureGrp = (String) jsonFeature.get("featureGroup");
695         String descripiton = (String) jsonFeature.get("description");
696         String seqRef = (String) jsonFeature.get("sequenceRef");
697         Float score = Float.valueOf(jsonFeature.get("score").toString());
698
699         Sequence seq = seqMap.get(seqRef);
700         SequenceFeature sequenceFeature = new SequenceFeature();
701         JSONArray linksJsonArray = (JSONArray) jsonFeature.get("links");
702         if (linksJsonArray != null && linksJsonArray.size() > 0)
703         {
704           Iterator<String> linkList = linksJsonArray.iterator();
705           while (linkList.hasNext())
706           {
707             String link = linkList.next();
708             sequenceFeature.addLink(link);
709           }
710         }
711         sequenceFeature.setFeatureGroup(featureGrp);
712         sequenceFeature.setScore(score);
713         sequenceFeature.setDescription(descripiton);
714         sequenceFeature.setType(type);
715         sequenceFeature.setBegin(seq.findPosition(begin.intValue()));
716         sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1);
717         seq.addSequenceFeature(sequenceFeature);
718         displayedFeatures.setVisible(type);
719       }
720     }
721   }
722
723   @Override
724   public String getGlobalColourScheme()
725   {
726     return globalColourScheme;
727   }
728
729   public void setGlobalColorScheme(String globalColourScheme)
730   {
731     this.globalColourScheme = globalColourScheme;
732   }
733
734   @Override
735   public FeaturesDisplayedI getDisplayedFeatures()
736   {
737     return displayedFeatures;
738   }
739
740   public void setDisplayedFeatures(FeaturesDisplayedI displayedFeatures)
741   {
742     this.displayedFeatures = displayedFeatures;
743   }
744
745   @Override
746   public void configureForView(AlignmentViewPanel avpanel)
747   {
748     if (avpanel == null)
749     {
750       return;
751     }
752     super.configureForView(avpanel);
753     AlignViewportI viewport = avpanel.getAlignViewport();
754     AlignmentI alignment = viewport.getAlignment();
755     AlignmentAnnotation[] annots = alignment.getAlignmentAnnotation();
756
757     seqGroups = alignment.getGroups();
758     fr = avpanel.cloneFeatureRenderer();
759
760     // Add non auto calculated annotation to AlignFile
761     if (annots != null)
762     {
763       for (AlignmentAnnotation annot : annots)
764       {
765         if (annot != null && !annot.autoCalculated)
766         {
767           annotations.add(annot);
768         }
769       }
770     }
771     globalColourScheme = viewport.getGlobalColourScheme().getSchemeName();
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 }