JAL-1641 added support for exporting sequence features and non auto-generated annotations features/JAL-1641_JSON-File-format
authorCharles Ofoegbu <tcnofoegbu@dundee.ac.uk>
Mon, 9 Feb 2015 08:50:46 +0000 (08:50 +0000)
committerCharles Ofoegbu <tcnofoegbu@dundee.ac.uk>
Mon, 9 Feb 2015 08:50:46 +0000 (08:50 +0000)
src/jalview/io/AlignFile.java
src/jalview/io/AppletFormatAdapter.java
src/jalview/io/IdentifyFile.java
src/jalview/io/JSONFile.java
src/jalview/json/binding/v1/AlignmentAnnotationPojo.java [new file with mode: 0644]
src/jalview/json/binding/v1/AlignmentPojo.java
src/jalview/json/binding/v1/AnnotationPojo.java [new file with mode: 0644]

index af835ca..19cdade 100755 (executable)
@@ -174,7 +174,7 @@ public abstract class AlignFile extends FileParse
 
     for (int i = 0; i < seqs.size(); i++)
     {
-      s[i] = (SequenceI) seqs.elementAt(i);
+      s[i] = seqs.elementAt(i);
     }
 
     return s;
@@ -198,7 +198,7 @@ public abstract class AlignFile extends FileParse
        * Rna.GetBasePairsFromAlignmentAnnotation(annotations.elementAt(i));
        * Rna.HelixMap(pairArray);
        */
-      AlignmentAnnotation an = (AlignmentAnnotation) annotations
+      AlignmentAnnotation an = annotations
               .elementAt(i);
       an.validateRangeAndDisplay();
       al.addAnnotation(an);
index bbd32be..98e366b 100755 (executable)
@@ -498,7 +498,6 @@ public class AppletFormatAdapter
     try
     {
       AlignFile afile = null;
-
       if (format.equalsIgnoreCase("FASTA"))
       {
         afile = new FastaFile();
@@ -557,6 +556,16 @@ public class AppletFormatAdapter
 
       afile.setSeqs(alignment.getSequencesArray());
 
+      // Add non auto calculated annotation to AlignFile
+      for (AlignmentAnnotation annot : alignment.getAlignmentAnnotation())
+      {
+        if (!annot.autoCalculated)
+        {
+          afile.annotations.add(annot);
+        }
+
+      }
+
       String afileresp = afile.print();
       if (afile.hasWarningMessage())
       {
index 3b4fd1a..94e6ebe 100755 (executable)
@@ -136,7 +136,7 @@ public class IdentifyFile
 
           break;
         }
-        if (data.indexOf("{\"SEQS\":[{\"FEATURES") > -1)
+        if (data.indexOf("{\"") > -1)
         {
           reply = JSONFile.FILE_DESC;
           break;
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;
diff --git a/src/jalview/json/binding/v1/AlignmentAnnotationPojo.java b/src/jalview/json/binding/v1/AlignmentAnnotationPojo.java
new file mode 100644 (file)
index 0000000..4cdc3d1
--- /dev/null
@@ -0,0 +1,43 @@
+package jalview.json.binding.v1;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AlignmentAnnotationPojo
+{
+  private String label;
+
+  private String description;
+
+  private List<AnnotationPojo> annotations = new ArrayList();
+
+  public String getLabel()
+  {
+    return label;
+  }
+
+  public void setLabel(String label)
+  {
+    this.label = label;
+  }
+
+  public String getDescription()
+  {
+    return description;
+  }
+
+  public void setDescription(String annotationId)
+  {
+    this.description = annotationId;
+  }
+
+  public List<AnnotationPojo> getAnnotations()
+  {
+    return annotations;
+  }
+
+  public void setAnnotations(List<AnnotationPojo> annotations)
+  {
+    this.annotations = annotations;
+  }
+}
index 4391d80..482da51 100644 (file)
@@ -15,6 +15,7 @@ import jalview.schemes.TurnColourScheme;
 import jalview.schemes.ZappoColourScheme;
 
 import java.util.ArrayList;
+import java.util.List;
 
 public class AlignmentPojo
 {
@@ -23,13 +24,17 @@ public class AlignmentPojo
   private String jalviewVersion;
 
   private String webStartUrl;
-  private ArrayList<SequencePojo> seqs = new ArrayList<SequencePojo>();
+
+  private List<SequencePojo> seqs = new ArrayList<SequencePojo>();
+
+  private List<AlignmentAnnotationPojo> alignmentAnnotation = new ArrayList<AlignmentAnnotationPojo>();
 
   public AlignmentPojo()
   {
 
   }
-  public ArrayList<SequencePojo> getSeqs()
+
+  public List<SequencePojo> getSeqs()
   {
     return seqs;
   }
@@ -89,6 +94,16 @@ public class AlignmentPojo
     this.webStartUrl = webStartUrl;
   }
 
+  public List<AlignmentAnnotationPojo> getAlignmentAnnotation()
+  {
+    return alignmentAnnotation;
+  }
+
+  public void setAlignmentAnnotation(List<AlignmentAnnotationPojo> alignmentAnnotation)
+  {
+    this.alignmentAnnotation = alignmentAnnotation;
+  }
+
   public enum JalviewBioJsColorSchemeMapper
   {
     USER_DEFINED("User Defined", "user defined", null), NONE("None", "foo",
diff --git a/src/jalview/json/binding/v1/AnnotationPojo.java b/src/jalview/json/binding/v1/AnnotationPojo.java
new file mode 100644 (file)
index 0000000..2e5aac4
--- /dev/null
@@ -0,0 +1,54 @@
+package jalview.json.binding.v1;
+
+
+public class AnnotationPojo
+{
+  private String displayCharacter = "";
+
+  private String description;
+
+  private char secondaryStructure;
+
+  private float value;
+
+
+  public String getDisplayCharacter()
+  {
+    return displayCharacter;
+  }
+
+  public void setDisplayCharacter(String displayCharacter)
+  {
+    this.displayCharacter = displayCharacter;
+  }
+
+  public String getDescription()
+  {
+    return description;
+  }
+
+  public void setDescription(String description)
+  {
+    this.description = description;
+  }
+
+  public char getSecondaryStructure()
+  {
+    return secondaryStructure;
+  }
+
+  public void setSecondaryStructure(char secondaryStructure)
+  {
+    this.secondaryStructure = secondaryStructure;
+  }
+
+  public float getValue()
+  {
+    return value;
+  }
+
+  public void setValue(float value)
+  {
+    this.value = value;
+  }
+}