JAL-1641 added support for exporting sequence features and non auto-generated annotations
[jalview.git] / src / jalview / io / JSONFile.java
index 8a30cf4..9f09c98 100644 (file)
@@ -1,15 +1,22 @@
 package jalview.io;
 
+import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Annotation;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
+import jalview.gui.AlignFrame;
+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.SequencePojo;
 import jalview.schemes.ColourSchemeI;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Iterator;
 
 import org.json.simple.JSONArray;
@@ -32,6 +39,8 @@ public class JSONFile extends AlignFile
 
   private AlignmentI al;
 
+  private AlignFrame af;
+
   private jalview.api.FeatureRenderer fr;
 
   public JSONFile(AlignmentI al)
@@ -75,6 +84,31 @@ public class JSONFile extends AlignFile
     // .getColourName(af.getViewport().getGlobalColourScheme()));
     jsonAlignmentPojo.setJalviewVersion(jalviewVersion);
     jsonAlignmentPojo.setWebStartUrl(webStartLaunchServletUrl);
+    for (AlignmentAnnotation annot : annotations)
+    {
+      AlignmentAnnotationPojo alignAnnotPojo = new AlignmentAnnotationPojo();
+      alignAnnotPojo.setDescription(annot.description);
+      alignAnnotPojo.setLabel(annot.label);
+
+      for (Annotation annotation : annot.annotations)
+      {
+        AnnotationPojo annotationPojo = new AnnotationPojo();
+        if (annotation != null)
+        {
+          annotationPojo.setDescription(annotation.description);
+          annotationPojo.setValue(annotation.value);
+          annotationPojo
+                  .setSecondaryStructure(annotation.secondaryStructure);
+          annotationPojo.setDisplayCharacter(annotation.displayCharacter);
+          alignAnnotPojo.getAnnotations().add(annotationPojo);
+        }
+        else
+        {
+          alignAnnotPojo.getAnnotations().add(annotationPojo);
+        }
+      }
+      jsonAlignmentPojo.getAlignmentAnnotation().add(alignAnnotPojo);
+    }
 
     int count = 0;
     for (SequenceI seq : seqs)
@@ -90,12 +124,25 @@ public class JSONFile extends AlignFile
       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())
+        {
+          FeaturePojo jsonFeature = new FeaturePojo();
+          jsonFeature.setXstart(seq.findIndex(sf.getBegin()) - 1);
+          jsonFeature.setXend(seq.findIndex(sf.getEnd()));
+          jsonFeature.setText(sf.getType());
+          seqFeaturesPojo.add(jsonFeature);
+        }
+        jsonSeqPojo.setFeatures(seqFeaturesPojo);
+      }
+
     }
 
-    /**
-     * TODO add logic to export Sequence features, non-auto-generated
-     * annotations, colour schemes
-     */
     return new com.json.JSONObject(jsonAlignmentPojo).toString()
             .replaceAll("xstart", "xStart").replaceAll("xend", "xEnd");
   }
@@ -108,6 +155,8 @@ public class JSONFile extends AlignFile
       JSONObject alignmentJsonObj = (JSONObject) jsonParser
               .parse(jsonAlignmentString);
       JSONArray seqJsonArray = (JSONArray) alignmentJsonObj.get("seqs");
+      JSONArray alAnnotJsonArray = (JSONArray) alignmentJsonObj.get("alignmentAnnotation");
+
       String bioJsColourScheme = (String) alignmentJsonObj
               .get("globalColorScheme");
       cs = getJalviewColorScheme(bioJsColourScheme);
@@ -131,6 +180,48 @@ public class JSONFile extends AlignFile
         }
         seqs.add(seq);
       }
+
+      for (Iterator<JSONObject> alAnnotIter = alAnnotJsonArray.iterator(); alAnnotIter
+              .hasNext();)
+      {
+        JSONObject alAnnot = alAnnotIter.next();
+        JSONArray annotJsonArray = (JSONArray) alAnnot
+                .get("annotations");
+        Annotation[] annotations = new Annotation[annotJsonArray.size()];
+        int count = 0;
+        for (Iterator<JSONObject> annotIter = annotJsonArray.iterator(); annotIter
+                .hasNext();)
+        {
+          JSONObject annot = annotIter.next();
+          if (annot == null)
+          {
+            annotations[count] = null;
+          }
+          else
+          {
+            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();
+
+            annotations[count] = new Annotation(displayChar, desc, ss, val);
+          }
+          ++count;
+        }
+
+        AlignmentAnnotation alignAnnot = new AlignmentAnnotation(alAnnot
+                .get("label").toString(), alAnnot.get("description")
+                .toString(), annotations);
+        this.annotations.add(alignAnnot);
+      }
+
     } catch (Exception e)
     {
       e.printStackTrace();
@@ -166,7 +257,6 @@ public class JSONFile extends AlignFile
     return seqFeatures;
   }
 
-
   private ColourSchemeI getJalviewColorScheme(String bioJsColourSchemeName)
   {
     ColourSchemeI jalviewColor = null;
@@ -182,6 +272,7 @@ public class JSONFile extends AlignFile
     return jalviewColor;
   }
 
+
   public String getGlobalColorScheme()
   {
     return globalColorScheme;