JAL-1780 JAL-653 Format/AppletFormat import and export pipeline regularised, uses...
[jalview.git] / test / jalview / io / JSONFileTest.java
index d2960d9..bce9795 100644 (file)
@@ -1,15 +1,22 @@
 package jalview.io;
 
+
+import static org.junit.Assert.assertNotNull;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.gui.AlignFrame;
+import jalview.gui.AlignmentPanel;
 import jalview.schemes.ColourSchemeI;
 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
 
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 
 import org.junit.After;
 import org.junit.Assert;
@@ -18,7 +25,6 @@ import org.junit.Test;
 
 public class JSONFileTest
 {
-  private JSONFile jsonFile;
 
   private int TEST_SEQ_HEIGHT = 0;
 
@@ -26,11 +32,19 @@ public class JSONFileTest
 
   private int TEST_ANOT_HEIGHT = 0;
 
+  private AlignFrame af;
+
+  AlignmentI alignment;
+
+  AlignmentPanel alignPanel;
+
+  HashMap<String, SequenceI> testSeqs = new HashMap<String, SequenceI>();
+  HashMap<String, AlignmentAnnotation> testAnnots = new HashMap<String, AlignmentAnnotation>();
+  HashMap<String, SequenceGroup> testGrps = new HashMap<String, SequenceGroup>();
+
   @Before
-  public void setUp() throws Exception
+  public void setup() throws Exception
   {
-    jsonFile = new JSONFile();
-
     // create and add sequences
     Sequence[] seqs = new Sequence[5];
     seqs[0] = new Sequence("FER_CAPAN",
@@ -46,11 +60,11 @@ public class JSONFileTest
 
     // create and add sequence features
     SequenceFeature seqFeature2 = new SequenceFeature("feature_x",
-            "desciption", "status", 22, 29, "jalview");
+            "desciption", "status", 6, 15, "Jalview");
     SequenceFeature seqFeature3 = new SequenceFeature("feature_x",
-            "desciption", "status", 25, 32, "jalview");
+            "desciption", "status", 9, 18, "Jalview");
     SequenceFeature seqFeature4 = new SequenceFeature("feature_x",
-            "desciption", "status", 25, 32, "jalview");
+            "desciption", "status", 9, 18, "Jalview");
     seqs[2].addSequenceFeature(seqFeature2);
     seqs[3].addSequenceFeature(seqFeature3);
     seqs[4].addSequenceFeature(seqFeature4);
@@ -58,26 +72,29 @@ public class JSONFileTest
     // add created features to features displayed
     FeaturesDisplayed fDis = new FeaturesDisplayed();
     fDis.setVisible("feature_x");
-    jsonFile.setDisplayedFeatures(fDis);
-    JSONFile.setSeqFeaturesEnabled(true);
+    // jsonFile.setDisplayedFeatures(fDis);
+    // jsonFile.setShowSeqFeatures(true);
 
     for (Sequence seq : seqs)
     {
       seq.setDatasetSequence(seq);
-      jsonFile.seqs.add(seq);
+      testSeqs.put(seq.getName(), seq);
+      // jsonFile.seqs.add(seq);
     }
 
     // create and add sequence groups
     ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
-    grpSeqs.add(seqs[0]);
     grpSeqs.add(seqs[1]);
     grpSeqs.add(seqs[2]);
-    ColourSchemeI scheme = jsonFile.getJalviewColorScheme("zappo");
-    SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1114606272",
-            scheme, true, true, false, 2, 9);
+    grpSeqs.add(seqs[3]);
+    grpSeqs.add(seqs[4]);
+    ColourSchemeI scheme = JSONFile.getJalviewColorScheme("zappo");
+    SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1883305585",
+            scheme, true, true, false, 21, 29);
     seqGrp.setShowNonconserved(false);
     seqGrp.setDescription(null);
-    jsonFile.seqGroups.add(seqGrp);
+    // jsonFile.seqGroups.add(seqGrp);
+    testGrps.put(seqGrp.getName(), seqGrp);
 
     // create and add annotation
     Annotation[] annot = new Annotation[35];
@@ -86,46 +103,46 @@ public class JSONFileTest
     annot[2] = new Annotation("α", "", 'H', 0);
     annot[3] = new Annotation("α", "", 'H', 0);
     annot[4] = new Annotation("α", "", 'H', 0);
-    annot[5] = new Annotation("α", "", 'H', 0);
+    annot[5] = new Annotation("", "", '\u0000', 0);
     annot[6] = new Annotation("", "", '\u0000', 0);
     annot[7] = new Annotation("", "", '\u0000', 0);
-    annot[8] = new Annotation("", "", '\u0000', 0);
-    annot[9] = new Annotation("", "", '\u0000', 0);
+    annot[8] = new Annotation("β", "", 'E', 0);
+    annot[9] = new Annotation("β", "", 'E', 0);
     annot[10] = new Annotation("β", "", 'E', 0);
     annot[11] = new Annotation("β", "", 'E', 0);
-    annot[12] = new Annotation("", "", '\u0000', 0);
-    annot[13] = new Annotation("", "", '\u0000', 0);
-    annot[14] = new Annotation("", "", '\u0000', 0);
-    annot[15] = new Annotation("", "", '\u0000', 0);
-    annot[16] = new Annotation("α", "", 'H', 0);
-    annot[17] = new Annotation("α", "", 'H', 0);
-    annot[18] = new Annotation("α", "", 'H', 0);
-    annot[19] = new Annotation("α", "", 'H', 0);
-    annot[20] = new Annotation("α", "", 'H', 0);
-
+    annot[12] = new Annotation("β", "", 'E', 0);
+    annot[13] = new Annotation("β", "", 'E', 0);
+    annot[14] = new Annotation("β", "", 'E', 0);
+    annot[15] = new Annotation("β", "", 'E', 0);
+    annot[16] = new Annotation("", "", '\u0000', 0);
+    annot[17] = new Annotation("", "", '\u0000', 0);
+    annot[18] = new Annotation("", "", '\u0000', 0);
+    annot[19] = new Annotation("", "", '\u0000', 0);
+    annot[20] = new Annotation("", "", '\u0000', 0);
     annot[21] = new Annotation("", "", '\u0000', 0);
     annot[22] = new Annotation("", "", '\u0000', 0);
     annot[23] = new Annotation("", "", '\u0000', 0);
     annot[24] = new Annotation("", "", '\u0000', 0);
     annot[25] = new Annotation("", "", '\u0000', 0);
-    annot[26] = new Annotation("", "", '\u0000', 0);
-    annot[27] = new Annotation("", "", '\u0000', 0);
-    annot[28] = new Annotation("", "", '\u0000', 0);
-    annot[29] = new Annotation("", "", '\u0000', 0);
-    annot[30] = new Annotation("", "", '\u0000', 0);
+    annot[26] = new Annotation("α", "", 'H', 0);
+    annot[27] = new Annotation("α", "", 'H', 0);
+    annot[28] = new Annotation("α", "", 'H', 0);
+    annot[29] = new Annotation("α", "", 'H', 0);
+    annot[30] = new Annotation("α", "", 'H', 0);
     annot[31] = new Annotation("", "", '\u0000', 0);
-    annot[32] = new Annotation("β", "", 'E', 0);
-    annot[33] = new Annotation("β", "", 'E', 0);
-    annot[34] = new Annotation("β", "", 'E', 0);
+    annot[32] = new Annotation("", "", '\u0000', 0);
+    annot[33] = new Annotation("", "", '\u0000', 0);
+    annot[34] = new Annotation("", "", '\u0000', 0);
 
     AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
             "Secondary Structure", "New description", annot);
-    jsonFile.annotations.add(alignAnnot);
+    // jsonFile.annotations.add(alignAnnot);
+    testAnnots.put(alignAnnot.label, alignAnnot);
 
     // Alignment al = new Alignment(seqs);
-    TEST_SEQ_HEIGHT = jsonFile.seqs.size();
-    TEST_GRP_HEIGHT = jsonFile.seqGroups.size();
-    TEST_ANOT_HEIGHT = jsonFile.annotations.size();
+    TEST_SEQ_HEIGHT = testSeqs.size();
+    TEST_GRP_HEIGHT = testGrps.size();
+    TEST_ANOT_HEIGHT = testAnnots.size();
   }
 
   @After
@@ -134,77 +151,134 @@ public class JSONFileTest
   }
 
   @Test
-  public void test()
+  public void testParse()
   {
-    String jsonOuput = jsonFile.print();
-    // System.out.println(">>>>>>>>>>>>>> " + jsonOuput);
-    JSONFile output = new JSONFile().parse(jsonOuput);
+    String jsonFile = "examples/example.json";
+    AppletFormatAdapter rf = new AppletFormatAdapter();
+    AlignmentI al = null;
+    try
+    {
+      al = rf.readFile(jsonFile, AppletFormatAdapter.FILE,
+              JSONFile.FILE_DESC);
+    } catch (IOException e)
+    {
+      e.printStackTrace();
+    }
+    assertNotNull("Couldn't read supplied alignment data.", al);
 
-    int matchedCounter = 0;
-    for (SequenceI in : jsonFile.getSeqs())
+    int passedCount = 0;
+    for (SequenceI seq : al.getSequences())
     {
-      for (SequenceI out : output.getSeqs())
-      {
-        if (in.getName().equals(out.getName())
-                && in.getSequenceAsString().equals(
-                        out.getSequenceAsString())
-                && in.getStart() == out.getStart()
-                && in.getEnd() == out.getEnd() && featuresMatched(in, out))
-        {
-          // System.out.println(">>>> Seq Match Detected");
-          ++matchedCounter;
-        }
-      }
+      SequenceI expectedSeq = testSeqs.get(seq.getName());
+      Assert.assertTrue("Failed Sequence Test  for >>> " + seq.getName(),
+              isSeqMatched(expectedSeq, seq));
+      passedCount++;
     }
-    Assert.assertTrue(matchedCounter == TEST_SEQ_HEIGHT);
+    Assert.assertEquals("Some Sequences did not pass the test",
+            TEST_SEQ_HEIGHT, passedCount);
 
-    matchedCounter = 0;
-    for (SequenceGroup in : jsonFile.getSeqGroups())
+    passedCount = 0;
+    for (SequenceGroup seqGrp : al.getGroups())
     {
-      for (SequenceGroup out : output.getSeqGroups())
-      {
-        if (in.getName().equals(out.getName())
-                && in.getColourText() == out.getColourText()
-                && in.getDisplayBoxes() == out.getDisplayBoxes()
-                && in.getIgnoreGapsConsensus() == out
-                        .getIgnoreGapsConsensus() && in.cs.equals(out.cs)
-                && in.getSequences().size() == out.getSequences().size())
-        {
-          // System.out.println(">>>> Grp Match Detected");
-          ++matchedCounter;
-        }
-      }
+      SequenceGroup expectedGrp = testGrps.get(seqGrp.getName());
+      Assert.assertTrue(
+              "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
+              isGroupMatched(expectedGrp, seqGrp));
+      passedCount++;
     }
-    Assert.assertTrue(matchedCounter == TEST_GRP_HEIGHT);
+    Assert.assertEquals("Some SequenceGroups did not pass the test",
+            TEST_GRP_HEIGHT, passedCount);
 
-    matchedCounter = 0;
-    for (AlignmentAnnotation in : jsonFile.annotations)
+    passedCount = 0;
+    for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
     {
-      for (AlignmentAnnotation out : output.annotations)
+      AlignmentAnnotation expectedAnnot = testAnnots.get(annot.label);
+      Assert.assertTrue("Failed AlignmentAnnotation Test for >>> "
+              + annot.label, isAnnotationMatched(expectedAnnot, annot));
+      passedCount++;
+    }
+    Assert.assertEquals("Some Sequences did not pass the test",
+            TEST_ANOT_HEIGHT, passedCount);
+
+    // af = new AlignFrame(al, 700, 500);
+    // AlignViewport viewport = af.getViewport();
+    // alignPanel = new AlignmentPanel(af, viewport);
+  }
+
+  public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
+          AlignmentAnnotation annot)
+  {
+    if (!eAnnot.label.equals(annot.label)
+            || !eAnnot.description.equals(annot.description)
+            || eAnnot.annotations.length != annot.annotations.length)
+    {
+      return false;
+    }
+
+    for (int x = 0; x < annot.annotations.length; x++)
+    {
+      Annotation y = annot.annotations[x];
+      Annotation z = annot.annotations[x];
+
+      if (!y.displayCharacter.equals(z.displayCharacter)
+              || y.value != z.value
+              || y.secondaryStructure != z.secondaryStructure)
       {
-        try
-        {
-          // System.out.println("label >>>>> " + in.label + " | " + out.label);
-          // System.out.println("label >>>>> " + in.description + " | "
-          // + out.description);
-          // System.out.println("label >>>>> " + in.annotations.length + " | "
-          // + out.annotations.length);
-          if (in.label.equals(out.label)
-                  && in.description.equals(out.description)
-                && in.annotations.length == out.annotations.length)
-        {
-          ++matchedCounter;
-        }
-        } catch (Exception e)
-        {
-          e.printStackTrace();
-        }
+        return false;
       }
     }
-    // System.out.println("matched >>>>> " + matchedCounter + " | "
-    // + TEST_ANOT_HEIGHT);
-    Assert.assertTrue(matchedCounter == TEST_ANOT_HEIGHT);
+    return true;
+  }
+
+  public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
+  {
+    System.out.println("Testing >>> " + actualSeq.getName());
+
+    if (expectedSeq.getName().equals(actualSeq.getName())
+            && expectedSeq.getSequenceAsString().equals(
+                    actualSeq.getSequenceAsString())
+            && expectedSeq.getStart() == actualSeq.getStart()
+            && expectedSeq.getEnd() == actualSeq.getEnd()
+            && featuresMatched(expectedSeq, actualSeq))
+    {
+      return true;
+    }
+    return false;
+  }
 
+  public boolean isGroupMatched(SequenceGroup expectedGrp,
+          SequenceGroup actualGrp)
+  {
+
+    System.out.println("Testing >>> " + actualGrp.getName());
+    System.out.println(expectedGrp.getName() + " | " + actualGrp.getName());
+    System.out.println(expectedGrp.getColourText() + " | "
+            + actualGrp.getColourText());
+    System.out.println(expectedGrp.getDisplayBoxes() + " | "
+            + actualGrp.getDisplayBoxes());
+    System.out.println(expectedGrp.getIgnoreGapsConsensus() + " | "
+            + actualGrp.getIgnoreGapsConsensus());
+    System.out.println(expectedGrp.getSequences().size() + " | "
+            + actualGrp.getSequences().size());
+    System.out.println(expectedGrp.getStartRes() + " | "
+            + actualGrp.getStartRes());
+    System.out.println(expectedGrp.getEndRes() + " | "
+            + actualGrp.getEndRes());
+
+    if (expectedGrp.getName().equals(actualGrp.getName())
+            && expectedGrp.getColourText() == actualGrp.getColourText()
+            && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes()
+            && expectedGrp.getIgnoreGapsConsensus() == actualGrp
+                    .getIgnoreGapsConsensus()
+            && expectedGrp.cs.equals(actualGrp.cs)
+            && expectedGrp.getSequences().size() == actualGrp
+                    .getSequences().size()
+            && expectedGrp.getStartRes() == actualGrp.getStartRes()
+            && expectedGrp.getEndRes() == actualGrp.getEndRes())
+    {
+      return true;
+    }
+    return false;
   }
 
   private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
@@ -232,22 +306,27 @@ public class JSONFileTest
 
       int testSize = inFeature.length;
       int matchedCount = 0;
-      // System.out.println(">>>>>>>>>>>>> 1");
       for (SequenceFeature in : inFeature)
       {
-        for (SequenceFeature out : inFeature)
+        for (SequenceFeature out : outFeature)
         {
+          System.out.println(out.getType() + " | " + in.getType());
+          System.out.println(out.getBegin() + " | " + in.getBegin());
+          System.out.println(out.getEnd() + " | " + in.getEnd());
+
           if (inFeature.length == outFeature.length
                   && in.getBegin() == out.getBegin()
                   && in.getEnd() == out.getEnd()
                   && in.getScore() == out.getScore()
-                  && in.getFeatureGroup().equals(out.getFeatureGroup()))
+                  && in.getFeatureGroup().equals(out.getFeatureGroup())
+                  && in.getType().equals(out.getType()))
           {
 
             ++matchedCount;
           }
         }
       }
+      System.out.println("matched count >>>>>> " + matchedCount);
       if (testSize == matchedCount)
       {
         matched = true;