updated build.xml
[jalview.git] / src / jalview / io / JSONFile.java
index 30383f3..621604c 100644 (file)
 
 package jalview.io;
 
+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;
+import org.json.simple.parser.JSONParser;
+
 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.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 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;
-import org.json.simple.parser.JSONParser;
-
 public class JSONFile extends AlignFile
 {
   private ColourSchemeI colourScheme;
 
-  private String jalviewVersion;
+  private String version = "2.9";
+
+  private String webstartUrl = "www.jalview.org/services/launchApp";
 
-  private String webStartLaunchServletUrl;
+  private String application = "Jalview";
 
   public static final String FILE_EXT = "json";
 
@@ -78,6 +80,10 @@ public class JSONFile extends AlignFile
 
   private JSONExportSettings jsonExportSettings;
 
+  private List<int[]> hiddenColumns;
+
+  private List<String> hiddenSeqRefs;
+
   public JSONFile()
   {
     super();
@@ -139,7 +145,7 @@ public class JSONFile extends AlignFile
         name.append(seq.getName()).append("/").append(seq.getStart())
                 .append("-").append(seq.getEnd());
         SequencePojo jsonSeqPojo = new SequencePojo();
-        jsonSeqPojo.setId(seq.getName() + "_" + seq.hashCode());
+        jsonSeqPojo.setId(String.valueOf(seq.hashCode()));
         jsonSeqPojo.setOrder(++count);
         jsonSeqPojo.setEnd(seq.getEnd());
         jsonSeqPojo.setStart(seq.getStart());
@@ -150,15 +156,27 @@ public class JSONFile extends AlignFile
 
       if (jsonExportSettings.isExportJalviewSettings())
       {
-        jalviewVersion = "";
-        webStartLaunchServletUrl = "www.jalview.org/services/launchApp";
-
-        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 && getViewport().isIncludeHiddenRegion())
+        {
+          if (hiddenSections[0] != null)
+          {
+            jsonAlignmentPojo.getAppSettings().put("hiddenCols",
+                    String.valueOf(hiddenSections[0]));
+          }
+          if (hiddenSections[1] != null)
+          {
+            jsonAlignmentPojo.getAppSettings().put("hiddenSeqs",
+                    String.valueOf(hiddenSections[1]));
+          }
+        }
       }
 
       if (jsonExportSettings.isExportAnnotations())
@@ -191,8 +209,7 @@ public class JSONFile extends AlignFile
           seqGrpPojo.setShowNonconserved(seqGrp.getShowNonconserved());
           for (SequenceI seq : seqGrp.getSequences())
           {
-            seqGrpPojo.getSeqsHash().add(
-                    seq.getName() + "_" + seq.hashCode());
+            seqGrpPojo.getSeqsHash().add(String.valueOf(seq.hashCode()));
           }
           jsonAlignmentPojo.getSeqGroups().add(seqGrpPojo);
         }
@@ -209,6 +226,59 @@ public class JSONFile extends AlignFile
     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 || hiddenSeqsObj.hiddenSequences == null)
+    {
+      return hiddenSections;
+    }
+
+    SequenceI[] hiddenSeqs = hiddenSeqsObj.hiddenSequences;
+    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();
+
+    return hiddenSections;
+  }
+
   public static List<SequenceFeaturesPojo> sequenceFeatureToJsonPojo(
           List<SequenceI> seqs, FeatureRenderer fr)
   {
@@ -221,6 +291,8 @@ public class JSONFile extends AlignFile
       SequenceFeature[] seqFeatures = (dataSetSequence == null) ? null
               : seq.getDatasetSequence().getSequenceFeatures();
 
+      seqFeatures = (seqFeatures == null) ? seq.getSequenceFeatures()
+              : seqFeatures;
       if (seqFeatures == null)
       {
         continue;
@@ -232,7 +304,7 @@ public class JSONFile extends AlignFile
                 && displayedFeatures.isVisible(sf.getType()))
         {
           SequenceFeaturesPojo jsonFeature = new SequenceFeaturesPojo(
-                  seq.getName() + "_" + seq.hashCode());
+                  String.valueOf(seq.hashCode()));
           String featureColour = (fr == null) ? null : jalview.util.Format
                   .getHexString(fr
                   .findFeatureColour(Color.white, seq,
@@ -304,7 +376,7 @@ public class JSONFile extends AlignFile
       JSONArray seqGrpJsonArray = (JSONArray) alignmentJsonObj
               .get("seqGroups");
       JSONObject jvSettingsJsonObj = (JSONObject) alignmentJsonObj
-              .get("jalviewSettings");
+              .get("appSettings");
 
       if (jvSettingsJsonObj != null)
       {
@@ -314,6 +386,8 @@ public class JSONFile extends AlignFile
                 "showSeqFeatures").toString());
         setColourScheme(getJalviewColorScheme(jsColourScheme));
         setShowSeqFeatures(showFeatures);
+        parseHiddenSeqRefsAsList(jvSettingsJsonObj);
+        parseHiddenCols(jvSettingsJsonObj);
       }
 
       seqMap = new Hashtable<String, Sequence>();
@@ -328,6 +402,10 @@ public class JSONFile extends AlignFile
         int end = Integer.valueOf(sequence.get("end").toString());
         Sequence seq = new Sequence(sequenceName, sequcenceString, start,
                 end);
+        if (hiddenSeqRefs != null && hiddenSeqRefs.contains(seqUniqueId))
+        {
+          seq.setHidden(true);
+        }
         seqs.add(seq);
         seqMap.put(seqUniqueId, seq);
       }
@@ -421,6 +499,31 @@ public class JSONFile extends AlignFile
     return this;
   }
 
+  public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson)
+  {
+    hiddenSeqRefs = new ArrayList<String>();
+    String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs");
+    if(hiddenSeqs != null && !hiddenSeqs.isEmpty()){
+      String[] seqRefs = hiddenSeqs.split(";");
+      for(String seqRef : seqRefs){
+        hiddenSeqRefs.add(seqRef);
+      }
+    }
+  }
+
+  public void parseHiddenCols(JSONObject jvSettingsJson)
+  {
+    hiddenColumns = new ArrayList<int[]>();
+    String hiddenCols = (String) jvSettingsJson.get("hiddenCols");
+    if(hiddenCols != null && !hiddenCols.isEmpty()){
+      String[] rangeStrings = hiddenCols.split(";");
+      for(String rangeString : rangeStrings){
+        String[] range = rangeString.split("-");
+        hiddenColumns.add(new int[]
+        { Integer.valueOf(range[0]), Integer.valueOf(range[1]) });
+      }
+    }
+  }
 
   @SuppressWarnings("unchecked")
   private void parseFeatures(JSONArray jsonSeqFeatures)
@@ -481,11 +584,13 @@ public class JSONFile extends AlignFile
     return jalviewColor;
   }
 
-  public void applySettingsToAlignFrame(AlignViewControllerGuiI af)
+  public void applySettingsToAlignmentView(AlignViewControllerGuiI avc)
   {
-    af.setShowSeqFeatures(isShowSeqFeatures());
-    af.changeColour(getColourScheme());
-    af.setMenusForViewport();
+    avc.setShowSeqFeatures(isShowSeqFeatures());
+    avc.changeColour(getColourScheme());
+    avc.setMenusForViewport();
+    avc.hideColumns(hiddenColumns);
+    avc.syncHiddenSequences();
   }
 
   public String getGlobalColorScheme()
@@ -572,6 +677,11 @@ public class JSONFile extends AlignFile
     return annotations;
   }
 
+  public List<int[]> getHiddenColumns()
+  {
+    return hiddenColumns;
+  }
+
   public class JSONExportSettings
   {
     private boolean exportSequence;