Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io

File JSONFile.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

136
312
35
2
862
722
119
0.38
8.91
17.5
3.4

Classes

Class Line # Actions
JSONFile 71 302 109 78
0.831533583.2%
JSONFile.JSONExportSettings 785 10 10 20
0.00%
 

Contributing tests

This file is covered by 7 tests. .

Source view

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