JAL-1622 code tidy only, no functional change
[jalview.git] / src / jalview / io / JSONFile.java
index e38812c..5e2139e 100644 (file)
@@ -1,26 +1,48 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+
 package jalview.io;
 
+import jalview.api.AlignViewControllerGuiI;
+import jalview.api.AlignViewportI;
+import jalview.api.FeatureRenderer;
 import jalview.api.FeaturesDisplayedI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
+import jalview.datamodel.HiddenSequences;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
-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.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;
@@ -34,13 +56,13 @@ import org.json.simple.parser.JSONParser;
 
 public class JSONFile extends AlignFile
 {
-  private static ColourSchemeI colourScheme;
+  private ColourSchemeI colourScheme;
 
-  private static boolean seqFeaturesEnabled;
+  private String version = "2.9";
 
-  private String jalviewVersion;
+  private String webstartUrl = "www.jalview.org/services/launchApp";
 
-  private String webStartLaunchServletUrl = "http://www.jalview.org/services/launchApp";
+  private String application = "Jalview";
 
   public static final String FILE_EXT = "json";
 
@@ -54,7 +76,7 @@ public class JSONFile extends AlignFile
 
   private FeaturesDisplayedI displayedFeatures;
 
-  private AlignmentViewport av;
+  private FeatureRenderer fr;
 
   private JSONExportSettings jsonExportSettings;
 
@@ -89,6 +111,7 @@ public class JSONFile extends AlignFile
   @Override
   public String print()
   {
+    String jsonOutput = null;
     try
     {
       if (getJsonExportSettings() == null)
@@ -101,15 +124,14 @@ public class JSONFile extends AlignFile
       }
 
       AlignmentPojo jsonAlignmentPojo = new AlignmentPojo();
-      if (Desktop.getCurrentAlignFrame() != null)
+      if (getViewport() != null)
       {
-        globalColorScheme = ColourSchemeProperty.getColourName(Desktop
-                .getCurrentAlignFrame().getViewport()
+        globalColorScheme = ColourSchemeProperty
+                .getColourName(getViewport()
                 .getGlobalColourScheme());
-        this.av = Desktop.getCurrentAlignFrame().getCurrentView();
-        setDisplayedFeatures(av.getFeaturesDisplayed());
-        showSeqFeatures = Desktop.getCurrentAlignFrame().showSeqFeatures
-                .isSelected();
+        setDisplayedFeatures(getViewport().getFeaturesDisplayed());
+        showSeqFeatures = getViewport().isShowSequenceFeatures();
+        fr = getViewport().getFeatureRenderer();
       }
 
       int count = 0;
@@ -130,12 +152,21 @@ public class JSONFile extends AlignFile
 
       if (jsonExportSettings.isExportJalviewSettings())
       {
-        JalviewSettingsPojo jvSettings = new JalviewSettingsPojo();
-        jvSettings.setGlobalColorScheme(globalColorScheme);
-        jvSettings.setJalviewVersion(jalviewVersion);
-        jvSettings.setWebStartUrl(webStartLaunchServletUrl);
-        jvSettings.setShowSeqFeatures(showSeqFeatures);
-        jsonAlignmentPojo.setJalviewSettings(jvSettings);
+        jsonAlignmentPojo.setGlobalColorScheme(globalColorScheme);
+        jsonAlignmentPojo.getAppSettings().put("application", application);
+        jsonAlignmentPojo.getAppSettings().put("version", version);
+        jsonAlignmentPojo.getAppSettings().put("webStartUrl", webstartUrl);
+        jsonAlignmentPojo.getAppSettings().put("showSeqFeatures",
+                String.valueOf(showSeqFeatures));
+
+        String[] hiddenSections = exportHiddenSections();
+        if (hiddenSections != null)
+        {
+          jsonAlignmentPojo.getAppSettings().put("hiddenCols",
+                  String.valueOf(hiddenSections[0]));
+          jsonAlignmentPojo.getAppSettings().put("hiddenSeqs",
+                  String.valueOf(hiddenSections[1]));
+        }
       }
 
       if (jsonExportSettings.isExportAnnotations())
@@ -146,8 +177,8 @@ public class JSONFile extends AlignFile
 
       if (jsonExportSettings.isExportSequenceFeatures())
       {
-        jsonAlignmentPojo.setSeqFeatures(sequenceFeatureToJsonPojo(seqs,
-                getDisplayedFeatures()));
+        jsonAlignmentPojo
+                .setSeqFeatures(sequenceFeatureToJsonPojo(seqs, fr));
       }
 
       if (jsonExportSettings.isExportGroups() && seqGroups != null
@@ -174,21 +205,77 @@ public class JSONFile extends AlignFile
           jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo);
         }
       }
-      com.json.JSONObject generatedJSon = new com.json.JSONObject(
+      org.json.JSONObject generatedJSon = new org.json.JSONObject(
               jsonAlignmentPojo);
-      String jsonOutput = generatedJSon.toString();
+      jsonOutput = generatedJSon.toString();
       return jsonOutput.replaceAll("xstart", "xStart").replaceAll("xend",
               "xEnd");
     } catch (Exception e)
     {
       e.printStackTrace();
-      throw e;
     }
+    return jsonOutput;
+  }
+
+  public String[] exportHiddenSections()
+  {
+    String[] hiddenSections = new String[2];
+    if (getViewport() == null)
+    {
+      return null;
+    }
+
+    System.out.println("--- Hidden Sections ---");
+    // hidden column business
+    if (getViewport().hasHiddenColumns())
+    {
+      System.out.print("Hidden Cols : ");
+      List<int[]> hiddenCols = getViewport().getColumnSelection()
+              .getHiddenColumns();
+      StringBuilder hiddenColsBuilder = new StringBuilder();
+      for (int[] range : hiddenCols)
+      {
+        hiddenColsBuilder.append(";").append(range[0]).append("-")
+                .append(range[1]);
+      }
+
+      hiddenColsBuilder.deleteCharAt(0);
+      hiddenSections[0] = hiddenColsBuilder.toString();
+      System.out.println(hiddenSections[0]);
+    }
+
+    // hidden rows/seqs business
+    HiddenSequences hiddenSeqsObj = getViewport().getAlignment()
+            .getHiddenSequences();
+    if (hiddenSeqsObj == null)
+    {
+      return hiddenSections;
+    }
+
+    SequenceI[] hiddenSeqs = hiddenSeqsObj.hiddenSequences;
+    System.out.print("Hidden Seqs : ");
+    StringBuilder hiddenSeqsBuilder = new StringBuilder();
+    for (SequenceI hiddenSeq : hiddenSeqs)
+    {
+      if (hiddenSeq != null)
+      {
+        hiddenSeqsBuilder.append(";").append(hiddenSeq.hashCode());
+      }
+    }
+    if (hiddenSeqsBuilder.length() > 0)
+    {
+      hiddenSeqsBuilder.deleteCharAt(0);
+    }
+    hiddenSections[1] = hiddenSeqsBuilder.toString();
+    System.out.println(hiddenSections[1]);
+    return hiddenSections;
   }
 
   public static List<SequenceFeaturesPojo> sequenceFeatureToJsonPojo(
-          List<SequenceI> seqs, FeaturesDisplayedI displayedFeatures)
+          List<SequenceI> seqs, FeatureRenderer fr)
   {
+    FeaturesDisplayedI displayedFeatures = (fr == null) ? null : fr
+            .getFeaturesDisplayed();
     List<SequenceFeaturesPojo> sequenceFeaturesPojo = new ArrayList<SequenceFeaturesPojo>();
     for (SequenceI seq : seqs)
     {
@@ -208,6 +295,10 @@ public class JSONFile extends AlignFile
         {
           SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
                   seq.getName() + "_" + seq.hashCode());
+          String featureColour = (fr == null) ? null : jalview.util.Format
+                  .getHexString(fr
+                  .findFeatureColour(Color.white, seq,
+                          seq.findIndex(sf.getBegin())));
           jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
           jsonFeature.setXend(seq.findIndex(sf.getEnd()));
           jsonFeature.setType(sf.getType());
@@ -215,6 +306,7 @@ public class JSONFile extends AlignFile
           jsonFeature.setLinks(sf.links);
           jsonFeature.setOtherDetails(sf.otherDetails);
           jsonFeature.setScore(sf.getScore());
+          jsonFeature.setFillColor(featureColour);
           jsonFeature.setFeatureGroup(sf.getFeatureGroup());
           sequenceFeaturesPojo.add(jsonFeature);
         }
@@ -274,7 +366,7 @@ public class JSONFile extends AlignFile
       JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
               .get("seqGroups");
       JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
-              .get("jalviewSettings");
+              .get("appSettings");
 
       if (jvSettingsJsonObj != null)
       {
@@ -283,7 +375,7 @@ public class JSONFile extends AlignFile
         Boolean showFeatures = Boolean.valueOf(jvSettingsJsonObj.get(
                 "showSeqFeatures").toString());
         setColourScheme(getJalviewColorScheme(jsColourScheme));
-        JSONFile.setSeqFeaturesEnabled(showFeatures);
+        setShowSeqFeatures(showFeatures);
       }
 
       seqMap = new Hashtable<String, Sequence>();
@@ -369,7 +461,7 @@ public class JSONFile extends AlignFile
             String desc = annot.get("description") == null ? null : annot
                     .get("description").toString();
 
-            char ss = annot.get("secondaryStructure") == null ? null
+            char ss = annot.get("secondaryStructure") == null ? ' '
                     : annot.get("secondaryStructure").toString().charAt(0);
             String displayChar = annot.get("displayCharacter").toString();
 
@@ -435,7 +527,8 @@ public class JSONFile extends AlignFile
     }
   }
 
-  public ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName)
+  public static ColourSchemeI getJalviewColorScheme(
+          String bioJsColourSchemeName)
   {
     ColourSchemeI jalviewColor = null;
     for (JalviewBioJsColorSchemeMapper cs : JalviewBioJsColorSchemeMapper
@@ -450,9 +543,9 @@ public class JSONFile extends AlignFile
     return jalviewColor;
   }
 
-  public void LoadAlignmentFeatures(AlignFrame af)
+  public void applySettingsToAlignFrame(AlignViewControllerGuiI af)
   {
-    af.setShowSeqFeatures(JSONFile.isSeqFeaturesEnabled());
+    af.setShowSeqFeatures(isShowSeqFeatures());
     af.changeColour(getColourScheme());
     af.setMenusForViewport();
   }
@@ -467,24 +560,14 @@ public class JSONFile extends AlignFile
     this.globalColorScheme = globalColorScheme;
   }
 
-  public static ColourSchemeI getColourScheme()
+  public ColourSchemeI getColourScheme()
   {
     return colourScheme;
   }
 
-  public static void setColourScheme(ColourSchemeI colourScheme)
-  {
-    JSONFile.colourScheme = colourScheme;
-  }
-
-  public static boolean isSeqFeaturesEnabled()
+  public void setColourScheme(ColourSchemeI colourScheme)
   {
-    return seqFeaturesEnabled;
-  }
-
-  public static void setSeqFeaturesEnabled(boolean seqFeaturesEnabled)
-  {
-    JSONFile.seqFeaturesEnabled = seqFeaturesEnabled;
+    this.colourScheme = colourScheme;
   }
 
   public FeaturesDisplayedI getDisplayedFeatures()
@@ -507,6 +590,50 @@ public class JSONFile extends AlignFile
     this.jsonExportSettings = jsonExportSettings;
   }
 
+  public static String getJSONData(AlignViewportI av)
+  {
+    JSONFile jsonFile = new JSONFile();
+    jsonFile.setViewport(av);
+    jsonFile.seqGroups = av.getAlignment().getGroups();
+    jsonFile.setDisplayedFeatures(av.getFeaturesDisplayed());
+
+    for (SequenceI seq : av.getAlignment().getSequences())
+    {
+      jsonFile.seqs.add(seq);
+    }
+  
+    // Add non auto calculated annotation to AlignFile
+    for (AlignmentAnnotation annot : av.getAlignment()
+            .getAlignmentAnnotation())
+    {
+      if (annot != null && !annot.autoCalculated)
+      {
+        if (annot.label.equals("PDB.CATempFactor"))
+        {
+          continue;
+        }
+        jsonFile.annotations.add(annot);
+      }
+    }
+    String jsonString = jsonFile.print();
+    return jsonString;
+  }
+
+  public boolean isShowSeqFeatures()
+  {
+    return showSeqFeatures;
+  }
+
+  public void setShowSeqFeatures(boolean showSeqFeatures)
+  {
+    this.showSeqFeatures = showSeqFeatures;
+  }
+
+  public Vector<AlignmentAnnotation> getAnnotations()
+  {
+    return annotations;
+  }
+
   public class JSONExportSettings
   {
     private boolean exportSequence;