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