JAL-1641 refactored out view settings from Desktop.java
[jalview.git] / src / jalview / io / JSONFile.java
index d3d2ef7..26ffb7c 100644 (file)
@@ -11,20 +11,22 @@ import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
 import jalview.json.binding.v1.AlignmentAnnotationPojo;
 import jalview.json.binding.v1.AlignmentPojo;
-import jalview.json.binding.v1.AlignmentPojo.JalviewBioJsColorSchemeMapper;
 import jalview.json.binding.v1.AnnotationPojo;
-import jalview.json.binding.v1.FeaturePojo;
+import jalview.json.binding.v1.JalviewSettingsPojo;
+import jalview.json.binding.v1.JalviewSettingsPojo.JalviewBioJsColorSchemeMapper;
+import jalview.json.binding.v1.SequenceFeaturesPojo;
 import jalview.json.binding.v1.SequenceGrpPojo;
 import jalview.json.binding.v1.SequencePojo;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemeProperty;
 import jalview.viewmodel.AlignmentViewport;
 
-import java.awt.Color;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
 
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
@@ -32,7 +34,9 @@ import org.json.simple.parser.JSONParser;
 
 public class JSONFile extends AlignFile
 {
-  private ColourSchemeI cs;
+  private static ColourSchemeI colourScheme;
+
+  private static boolean seqFeaturesEnabled;
 
   private String jalviewVersion;
 
@@ -44,6 +48,8 @@ public class JSONFile extends AlignFile
 
   private String globalColorScheme;
 
+  private boolean showSeqFeatures;
+
   private Hashtable<String, Sequence> seqMap;
 
   private FeaturesDisplayedI displayedFeatures;
@@ -67,7 +73,6 @@ public class JSONFile extends AlignFile
     super(inFile, type);
   }
 
-
   @Override
   public void parse() throws IOException
   {
@@ -84,46 +89,141 @@ public class JSONFile extends AlignFile
   @Override
   public String print()
   {
-    AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
-    if (Desktop.getCurrentAlignFrame() != null)
+    try
     {
-      jsonAlignmentPojo.setGlobalColorScheme(ColourSchemeProperty
-              .getColourName(Desktop.getCurrentAlignFrame().getViewport()
-                      .getGlobalColourScheme()));
-      this.av = Desktop.getCurrentAlignFrame().getCurrentView();
-      this.fr = Desktop.getCurrentAlignFrame().alignPanel
-              .cloneFeatureRenderer();
-      displayedFeatures = av.getFeaturesDisplayed();
-      jsonAlignmentPojo
-              .setShowSeqFeatures(Desktop.getCurrentAlignFrame().showSeqFeatures
-                      .isSelected());
+      JSONExportSettings exportSettings = new JSONExportSettings();
+      exportSettings.setExportAnnotations(true);
+      exportSettings.setExportGroups(true);
+      exportSettings.setExportJalviewSettings(true);
+      exportSettings.setExportSequenceFeatures(true);
+
+      AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
+      if (Desktop.getCurrentAlignFrame() != null)
+      {
+        globalColorScheme = ColourSchemeProperty.getColourName(Desktop
+                .getCurrentAlignFrame().getViewport()
+                .getGlobalColourScheme());
+        this.av = Desktop.getCurrentAlignFrame().getCurrentView();
+        this.fr = Desktop.getCurrentAlignFrame().alignPanel
+                .cloneFeatureRenderer();
+        displayedFeatures = av.getFeaturesDisplayed();
+        showSeqFeatures = Desktop.getCurrentAlignFrame().showSeqFeatures
+                .isSelected();
+      }
+
+      int count = 0;
+      for (SequenceI seq : seqs)
+      {
+        StringBuilder name = new StringBuilder();
+        name.append(seq.getName()).append("/").append(seq.getStart())
+                .append("-").append(seq.getEnd());
+        SequencePojo jsonSeqPojo = new SequencePojo();
+        jsonSeqPojo.setId(seq.getName() + "_" + seq.hashCode());
+        jsonSeqPojo.setOrder(++count);
+        jsonSeqPojo.setEnd(seq.getEnd());
+        jsonSeqPojo.setStart(seq.getStart());
+        jsonSeqPojo.setName(name.toString());
+        jsonSeqPojo.setSeq(seq.getSequenceAsString());
+        jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
+      }
+
+      if (exportSettings.isExportJalviewSettings())
+      {
+        JalviewSettingsPojo jvSettings = new JalviewSettingsPojo();
+        jvSettings.setGlobalColorScheme(globalColorScheme);
+        jvSettings.setJalviewVersion(jalviewVersion);
+        jvSettings.setWebStartUrl(webStartLaunchServletUrl);
+        jvSettings.setShowSeqFeatures(showSeqFeatures);
+        jsonAlignmentPojo.setJalviewSettings(jvSettings);
+      }
+
+      if (exportSettings.isExportAnnotations())
+      {
+        jsonAlignmentPojo
+                .setAlignAnnotation(annotationToJsonPojo(annotations));
+      }
+
+      if (exportSettings.isExportSequenceFeatures())
+      {
+        jsonAlignmentPojo.setSeqFeatures(sequenceFeatureToJsonPojo(seqs,
+                displayedFeatures));
+      }
+
+      if (exportSettings.isExportGroups() && seqGroups.size() > 0)
+      {
+        ArrayList<SequenceGrpPojo> sequenceGroupsPojo = new ArrayList<SequenceGrpPojo>();
+        for (SequenceGroup seqGrp : seqGroups)
+        {
+          SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
+          seqGrpPojo.setGroupName(seqGrp.getName());
+          seqGrpPojo.setColourScheme(ColourSchemeProperty
+                  .getColourName(seqGrp.cs));
+          seqGrpPojo.setColourText(seqGrp.getColourText());
+          seqGrpPojo.setDescription(seqGrp.getDescription());
+          seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
+          seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
+          seqGrpPojo.setEndRes(seqGrp.getEndRes());
+          seqGrpPojo.setStartRes(seqGrp.getStartRes());
+          seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
+          for (SequenceI seq : seqGrp.getSequences())
+          {
+            seqGrpPojo.getSeqsHash().add(
+                    seq.getName() + "_" + seq.hashCode());
+          }
+          jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo);
+        }
+      }
+      com.json.JSONObject generatedJSon = new com.json.JSONObject(
+              jsonAlignmentPojo);
+      String jsonOutput = generatedJSon.toString();
+      return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend",
+              "xEnd");
+    } catch (Exception e)
+    {
+      e.printStackTrace();
+      throw e;
     }
-    jsonAlignmentPojo.setJalviewVersion(jalviewVersion);
-    jsonAlignmentPojo.setWebStartUrl(webStartLaunchServletUrl);
+  }
 
-    if (seqGroups.size() > 0)
+  public static List<SequenceFeaturesPojo> sequenceFeatureToJsonPojo(
+          List<SequenceI> seqs, FeaturesDisplayedI displayedFeatures)
+  {
+    List<SequenceFeaturesPojo> sequenceFeaturesPojo = new ArrayList<SequenceFeaturesPojo>();
+    for (SequenceI seq : seqs)
     {
-      ArrayList<SequenceGrpPojo> sequenceGroupsPojo = new ArrayList<SequenceGrpPojo>();
-      for (SequenceGroup seqGrp : seqGroups)
+      SequenceI dataSetSequence = seq.getDatasetSequence();
+      SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null
+              : seq.getDatasetSequence().getSequenceFeatures();
+      for (SequenceFeature sf : seqFeatures)
       {
-        SequenceGrpPojo seqGrpPojo = new SequenceGrpPojo();
-        seqGrpPojo.setGroupName(seqGrp.getName());
-        seqGrpPojo.setColourScheme(ColourSchemeProperty
-                .getColourName(seqGrp.cs));
-        seqGrpPojo.setColourText(seqGrp.getColourText());
-        seqGrpPojo.setDescription(seqGrp.getDescription());
-        seqGrpPojo.setDisplayBoxes(seqGrp.getDisplayBoxes());
-        seqGrpPojo.setDisplayText(seqGrp.getDisplayText());
-        seqGrpPojo.setEndRes(seqGrp.getEndRes());
-        seqGrpPojo.setStartRes(seqGrp.getStartRes());
-        seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
-        for(SequenceI seq : seqGrp.getSequences()){
-          seqGrpPojo.getSeqsHash().add(seq.getName() + "_" + seq.hashCode());
+        if (displayedFeatures != null
+                && displayedFeatures.isVisible(sf.getType()))
+        {
+          // String fillColor = ((fr != null) ? jalview.util.Format
+          // .getHexString(fr.findFeatureColour(Color.white, seq,
+          // seq.findIndex(sf.getBegin()))) : null);
+          SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
+                  seq.getName() + "_" + seq.hashCode());
+          jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
+          jsonFeature.setXend(seq.findIndex(sf.getEnd()));
+          jsonFeature.setType(sf.getType());
+          jsonFeature.setDescription(sf.getDescription());
+          jsonFeature.setLinks(sf.links);
+          jsonFeature.setOtherDetails(sf.otherDetails);
+          jsonFeature.setScore(sf.getScore());
+          // jsonFeature.setFillColor(fillColor);
+          jsonFeature.setFeatureGroup(sf.getFeatureGroup());
+          sequenceFeaturesPojo.add(jsonFeature);
         }
-        jsonAlignmentPojo.getSequenceGroups().add(seqGrpPojo);
       }
     }
+    return sequenceFeaturesPojo;
+  }
 
+  public static List<AlignmentAnnotationPojo> annotationToJsonPojo(
+          Vector<AlignmentAnnotation> annotations)
+  {
+    List<AlignmentAnnotationPojo> jsonAnnotations = new ArrayList<AlignmentAnnotationPojo>();
     for (AlignmentAnnotation annot : annotations)
     {
       AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
@@ -146,59 +246,9 @@ public class JSONFile extends AlignFile
           alignAnnotPojo.getAnnotations().add(annotationPojo);
         }
       }
-      jsonAlignmentPojo.getAlignmentAnnotation().add(alignAnnotPojo);
+      jsonAnnotations.add(alignAnnotPojo);
     }
-
-    int count = 0;
-    for (SequenceI seq : seqs)
-    {
-      StringBuilder name = new StringBuilder();
-      name.append(seq.getName()).append("/").append(seq.getStart())
-              .append("-").append(seq.getEnd());
-
-      SequencePojo jsonSeqPojo = new SequencePojo();
-
-      jsonSeqPojo.setId(seq.getName() + "_" + seq.hashCode());
-      jsonSeqPojo.setOrder(++count);
-      jsonSeqPojo.setEnd(seq.getEnd());
-      jsonSeqPojo.setStart(seq.getStart());
-      jsonSeqPojo.setName(name.toString());
-      jsonSeqPojo.setSeq(seq.getSequenceAsString());
-      jsonAlignmentPojo.getSeqs().add(jsonSeqPojo);
-
-      if (seq.getDatasetSequence() != null
-              && seq.getDatasetSequence().getSequenceFeatures() != null)
-      {
-        ArrayList<FeaturePojo> seqFeaturesPojo = new ArrayList<FeaturePojo>();
-        for (SequenceFeature sf : seq.getDatasetSequence()
-                .getSequenceFeatures())
-        {
-
-          if (displayedFeatures != null
-                  && displayedFeatures.isVisible(sf.getType()))
-          {
-
-            String fillColor = ((fr != null) ? jalview.util.Format
-                    .getHexString(fr.findFeatureColour(Color.white, seq,
-                            seq.findIndex(sf.getBegin()))) : null);
-            FeaturePojo jsonFeature = new FeaturePojo();
-            jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
-            jsonFeature.setXend(seq.findIndex(sf.getEnd()));
-            jsonFeature.setType(sf.getType());
-            jsonFeature.setDescription(sf.getDescription());
-            jsonFeature.setLinks(sf.links);
-            jsonFeature.setOtherDetails(sf.otherDetails);
-            jsonFeature.setScore(sf.getScore());
-            jsonFeature.setFillColor(fillColor);
-            jsonFeature.setFeatureGroup(sf.getFeatureGroup());
-            seqFeaturesPojo.add(jsonFeature);
-          }
-        }
-        jsonSeqPojo.setFeatures(seqFeaturesPojo);
-      }
-    }
-    return new com.json.JSONObject(jsonAlignmentPojo).toString()
-            .replaceAll("xstart", "xStart").replaceAll("xend", "xEnd");
+    return jsonAnnotations;
   }
 
   public void parse(String jsonAlignmentString)
@@ -209,16 +259,28 @@ public class JSONFile extends AlignFile
       JSONObject alignmentJsonObj = (JSONObject) jsonParser
               .parse(jsonAlignmentString);
       JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
-      JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj.get("alignmentAnnotation");
-      JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj.get("sequenceGroups");
-      String jsColourScheme = (String) alignmentJsonObj
-              .get("globalColorScheme");
-      Boolean showFeatures = Boolean.valueOf(alignmentJsonObj.get(
-              "showSeqFeatures").toString());
-      cs = getJalviewColorScheme(jsColourScheme);
+      JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj
+              .get("alignAnnotation");
+      JSONArray jsonSeqArray = (JSONArray) alignmentJsonObj
+              .get("seqFeatures");
+      JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
+              .get("seqGroups");
+      JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
+              .get("jalviewSettings");
+
+      if (jvSettingsJsonObj != null)
+      {
+        String jsColourScheme = (String) jvSettingsJsonObj
+                .get("globalColorScheme");
+        Boolean showFeatures = Boolean.valueOf(jvSettingsJsonObj.get(
+                "showSeqFeatures").toString());
+        setColourScheme(getJalviewColorScheme(jsColourScheme));
+        JSONFile.setSeqFeaturesEnabled(showFeatures);
+        // Desktop.setCurrentSeqFeaturesVisible(showFeatures);
+      }
+
       seqMap = new Hashtable<String, Sequence>();
-      Desktop.setCurrentGlobalColourScheme(cs);
-      Desktop.setCurrentSeqFeaturesVisible(showFeatures);
+      // Desktop.setCurrentGlobalColourScheme(cs);
       for (Iterator<JSONObject> sequenceIter = seqJsonArray.iterator(); sequenceIter
               .hasNext();)
       {
@@ -230,17 +292,11 @@ public class JSONFile extends AlignFile
         int end = Integer.valueOf(sequence.get("end").toString());
         Sequence seq = new Sequence(sequenceName, sequcenceString, start,
                 end);
-        JSONArray jsonSeqArray = (JSONArray) sequence.get("features");
-        SequenceFeature[] retrievedSeqFeatures = getJalviewSequenceFeatures(
-                jsonSeqArray, seq);
-        if (retrievedSeqFeatures != null)
-        {
-          seq.setSequenceFeatures(retrievedSeqFeatures);
-        }
         seqs.add(seq);
         seqMap.put(seqUniqueId, seq);
       }
-      
+      parseFeatures(jsonSeqArray);
+
       for (Iterator<JSONObject> seqGrpIter = seqGrpJsonArray.iterator(); seqGrpIter
               .hasNext();)
       {
@@ -285,13 +341,11 @@ public class JSONFile extends AlignFile
 
       }
 
-
       for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
               .hasNext();)
       {
         JSONObject alAnnot = alAnnotIter.next();
-        JSONArray annotJsonArray = (JSONArray) alAnnot
-                .get("annotations");
+        JSONArray annotJsonArray = (JSONArray) alAnnot.get("annotations");
         Annotation[] annotations = new Annotation[annotJsonArray.size()];
         int count = 0;
         for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
@@ -304,17 +358,14 @@ public class JSONFile extends AlignFile
           }
           else
           {
-            float val = annot.get("value") == null ? null
-                    : Float.valueOf(annot.get("value")
-                    .toString());
+            float val = annot.get("value") == null ? null : Float
+                    .valueOf(annot.get("value").toString());
             String desc = annot.get("description") == null ? null : annot
                     .get("description").toString();
 
             char ss = annot.get("secondaryStructure") == null ? null
-                    : annot
-                    .get("secondaryStructure").toString().charAt(0);
-            String displayChar = annot.get(
-                    "displayCharacter").toString();
+                    : annot.get("secondaryStructure").toString().charAt(0);
+            String displayChar = annot.get("displayCharacter").toString();
 
             annotations[count] = new Annotation(displayChar, desc, ss, val);
           }
@@ -333,20 +384,15 @@ public class JSONFile extends AlignFile
     }
   }
 
-  public SequenceFeature[] getJalviewSequenceFeatures(
-          JSONArray jsonSeqFeatures, Sequence seq)
+
+  private void parseFeatures(JSONArray jsonSeqFeatures)
   {
-    SequenceFeature[] seqFeatures = null;
-    int count = 0;
     if (jsonSeqFeatures != null)
     {
-      seqFeatures = new SequenceFeature[jsonSeqFeatures.size()];
       for (@SuppressWarnings("unchecked")
       Iterator<JSONObject> seqFeatureItr = jsonSeqFeatures.iterator(); seqFeatureItr
               .hasNext();)
       {
-
-        SequenceFeature sequenceFeature = new SequenceFeature();
         JSONObject jsonFeature = seqFeatureItr.next();
         Long begin = (Long) jsonFeature.get("xStart");
         Long end = (Long) jsonFeature.get("xEnd");
@@ -354,23 +400,33 @@ public class JSONFile extends AlignFile
         String color = (String) jsonFeature.get("fillColor");
         String featureGrp = (String) jsonFeature.get("featureGroup");
         String descripiton = (String) jsonFeature.get("description");
+        String seqRef = (String) jsonFeature.get("sequenceRef");
         Float score = Float.valueOf(jsonFeature.get("score").toString());
         // Hashtable otherDetails = (Hashtable) jsonFeature
         // .get("otherDetails");
-        // Vector<String> links = (Vector<String>) jsonFeature.get("links");
-
-        // sequenceFeature.links = links;
         // sequenceFeature.otherDetails = otherDetails;
+
+        Sequence seq = seqMap.get(seqRef);
+        SequenceFeature sequenceFeature = new SequenceFeature();
+        JSONArray linksJsonArray = (JSONArray) jsonFeature.get("links");
+        if (linksJsonArray != null && linksJsonArray.size() > 0)
+        {
+          Iterator<String> linkList = linksJsonArray.iterator();
+          while (linkList.hasNext())
+          {
+            String link = linkList.next();
+            sequenceFeature.addLink(link);
+          }
+        }
+        sequenceFeature.setFeatureGroup(featureGrp);
         sequenceFeature.setScore(score);
         sequenceFeature.setDescription(descripiton);
-
+        sequenceFeature.setType(type);
         sequenceFeature.setBegin(seq.findPosition(begin.intValue()));
         sequenceFeature.setEnd(seq.findPosition(end.intValue()) - 1);
-        sequenceFeature.setType(type);
-        seqFeatures[count++] = sequenceFeature;
+        seq.addSequenceFeature(sequenceFeature);
       }
     }
-    return seqFeatures;
   }
 
   private ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName)
@@ -390,8 +446,8 @@ public class JSONFile extends AlignFile
 
   public void LoadAlignmentFeatures(AlignFrame af)
   {
-    af.setShowSeqFeatures(Desktop.isCurrentSeqFeaturesVisible());
-    af.changeColour(Desktop.getCurrentGlobalColourScheme());
+    af.setShowSeqFeatures(JSONFile.isSeqFeaturesEnabled());
+    af.changeColour(getColourScheme());
     af.setMenusForViewport();
   }
 
@@ -404,4 +460,87 @@ public class JSONFile extends AlignFile
   {
     this.globalColorScheme = globalColorScheme;
   }
+
+  public static ColourSchemeI getColourScheme()
+  {
+    return colourScheme;
+  }
+
+  public static void setColourScheme(ColourSchemeI colourScheme)
+  {
+    JSONFile.colourScheme = colourScheme;
+  }
+
+  public static boolean isSeqFeaturesEnabled()
+  {
+    return seqFeaturesEnabled;
+  }
+
+  public static void setSeqFeaturesEnabled(boolean seqFeaturesEnabled)
+  {
+    JSONFile.seqFeaturesEnabled = seqFeaturesEnabled;
+  }
+
+  public class JSONExportSettings
+  {
+    private boolean exportSequence;
+
+    private boolean exportSequenceFeatures;
+
+    private boolean exportAnnotations;
+
+    private boolean exportGroups;
+
+    private boolean exportJalviewSettings;
+
+    public boolean isExportSequence()
+    {
+      return exportSequence;
+    }
+
+    public void setExportSequence(boolean exportSequence)
+    {
+      this.exportSequence = exportSequence;
+    }
+
+    public boolean isExportSequenceFeatures()
+    {
+      return exportSequenceFeatures;
+    }
+
+    public void setExportSequenceFeatures(boolean exportSequenceFeatures)
+    {
+      this.exportSequenceFeatures = exportSequenceFeatures;
+    }
+
+    public boolean isExportAnnotations()
+    {
+      return exportAnnotations;
+    }
+
+    public void setExportAnnotations(boolean exportAnnotations)
+    {
+      this.exportAnnotations = exportAnnotations;
+    }
+
+    public boolean isExportGroups()
+    {
+      return exportGroups;
+    }
+
+    public void setExportGroups(boolean exportGroups)
+    {
+      this.exportGroups = exportGroups;
+    }
+
+    public boolean isExportJalviewSettings()
+    {
+      return exportJalviewSettings;
+    }
+
+    public void setExportJalviewSettings(boolean exportJalviewSettings)
+    {
+      this.exportJalviewSettings = exportJalviewSettings;
+    }
+  }
 }