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