Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / test / jalview / ws / ebi / HmmerJSONProcessTest.java
diff --git a/test/jalview/ws/ebi/HmmerJSONProcessTest.java b/test/jalview/ws/ebi/HmmerJSONProcessTest.java
new file mode 100644 (file)
index 0000000..bf68906
--- /dev/null
@@ -0,0 +1,122 @@
+package jalview.ws.ebi;
+
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceI;
+import jalview.io.DataSourceType;
+import jalview.io.FileFormat;
+import jalview.io.FileParse;
+import jalview.io.FormatAdapter;
+
+import java.io.File;
+
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class HmmerJSONProcessTest {
+  public static File alignmentFragFile = new File(
+          "examples/testdata/hmmer3/alignment_frag.fa.gz");
+
+  private AlignmentI getSearchResultFragmentAlignment() throws Exception
+  {
+    AlignmentI alf = new FormatAdapter().readFile(
+            alignmentFragFile.getAbsolutePath(), DataSourceType.FILE,
+            FileFormat.Fasta);
+
+    return alf;
+  }
+
+  public static File alignmentResultFile = new File(
+          "examples/testdata/hmmer3/alignment_res.fa.gz");
+
+  private AlignmentI getSearchResultAlignment() throws Exception
+  {
+    AlignmentI alf = new FormatAdapter().readFile(
+            alignmentResultFile.getAbsolutePath(), DataSourceType.FILE,
+            FileFormat.Fasta);
+
+    return alf;
+  }
+
+  public static String hitTestFile = "examples/testdata/hmmer3/hit_fragment.json.gz",
+          hmmerResultFile = "examples/testdata/hmmer3/hmmeresult.json.gz";
+
+
+  @Test(groups = { "Functional" })
+  public void parseHitTest() throws Exception
+  {
+
+    Assert.assertTrue(new File(hitTestFile).exists(),
+            "Can't find test data.\n"
+            + hitTestFile);
+    JSONParser jp = new JSONParser();
+    // read JSON in same way - via fileparse
+    Object hitfragment = jp.parse(new FileParse(hitTestFile,
+            DataSourceType.FILE).getReader());
+    Assert.assertTrue((hitfragment instanceof JSONObject),
+            "Didn't find a JSON object map in " + hitTestFile);
+    AlignmentI searchResult = getSearchResultFragmentAlignment();
+
+    Assert.assertTrue(searchResult != null && searchResult.getHeight() > 0,
+            "Didn't read search result alignment from " + alignmentFragFile);
+
+    HmmerJSONProcessor hjsp = new HmmerJSONProcessor(searchResult);
+    hjsp.addHit((JSONObject) hitfragment, 1);
+    // check that
+    // scores, posterior probabilities and stuff exist.
+  }
+
+  @Test(groups = { "Functional" })
+  public void parseJsonResultTest() throws Exception
+  {
+
+    Assert.assertTrue(new File(hmmerResultFile).exists(),
+            "Can't find test data.\n" + hmmerResultFile);
+
+    AlignmentI searchResult = getSearchResultAlignment();
+
+    Assert.assertTrue(searchResult != null && searchResult.getHeight() > 0,
+            "Didn't read search result alignment from " + alignmentFragFile);
+
+    HmmerJSONProcessor hjsp = new HmmerJSONProcessor(searchResult);
+    hjsp.parseFrom(new FileParse(hmmerResultFile, DataSourceType.FILE));
+    AlignmentAnnotation[] aa = searchResult.getSequenceAt(5)
+            .getAnnotation();
+    Assert.assertNotNull(aa);
+    Assert.assertEquals(aa.length, 3,
+            "didn't get expected set of annotation.\n");
+    // DPTSERWFHGHLSGKEAEKLLTeKGKHGSFLVRESQSHPGDFVLSVRTgddkgesndgKSKVTHVMIR-CQELKYDVGGGERFDSLTDLVEHYKKNPmvet
+    // LGTVLQLKQP
+    // 5789*****************9799***********************999998888888********.99**************************9999
+    // 899999*999
+    // AlignmentAnnotation
+    // 101 == 8
+    String seq = "tLGT";
+    SequenceI s5 = searchResult.getSequenceAt(5);
+    Assert.assertEquals(
+            s5.getSubSequence(s5.findIndex(225), s5.findIndex(229))
+                    .getSequenceAsString(),
+            seq);
+    int pos = s5.findIndex(226);
+    for (AlignmentAnnotation an : aa)
+    {
+      if (an.label.startsWith("Posterior"))
+      {
+        Assert.assertEquals(an.annotations[pos].value, 8f);
+
+      }
+    }
+    ;
+    // check that
+    // scores, posterior probabilities and stuff exist.
+  }
+  // Groovy test
+  // def al = Jalview.getAlignFrames()[0].getViewport().getAlignment()
+  // def jproc = new jalview.ws.ebi.HmmerJSONProcessor(al)
+  // jproc.parseFrom(new
+  // jalview.io.FileParse("examples/testdata/hmmer3/hmmeresult.json.gz","File"))
+  // jproc.updateView(Jalview.getAlignFrames()[0].getViewport())
+
+}