Merge branch 'Jalview-JS/develop' into merge_js_develop
[jalview.git] / test / jalview / hmmer / HMMERTest.java
index 7707dca..04cc3be 100644 (file)
@@ -1,20 +1,21 @@
 package jalview.hmmer;
 
 import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 import jalview.bin.Jalview;
-import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenMarkovModel;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
-import jalview.io.DataSourceType;
-import jalview.io.FastaFile;
-import jalview.io.FileParse;
+import jalview.io.HMMFile;
+import jalview.util.MessageManager;
 import jalview.ws.params.ArgumentI;
+import jalview.ws.params.simple.Option;
 
 import java.io.IOException;
 import java.net.MalformedURLException;
@@ -25,86 +26,109 @@ import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
-
 public class HMMERTest {
 
   AlignFrame frame;
 
   @BeforeClass(alwaysRun = true)
-  public static void setUpBeforeClass() throws Exception
+  public void setUpBeforeClass() throws Exception
   {
+    /*
+     * NB: check HMMER_PATH in testProps.jvprops is valid for
+     * the machine on which this runs
+     */
     Jalview.main(
             new String[]
     { "-noquestionnaire", "-nonews", "-props",
-        "test/jalview/hmmer/testProps.jvprops" });
+                "test/jalview/hmmer/testProps.jvprops", "-open",
+                "examples/uniref50.fa" });
+    frame = Desktop.getAlignFrames()[0];
   }
 
   @AfterClass(alwaysRun = true)
   public static void tearDownAfterClass() throws Exception
   {
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
   }
 
-  @Test(priority = 0)
-  public void testHMMBuild() throws MalformedURLException, IOException
+  /**
+   * Test with a dependency on locally installed hmmbuild binaries
+   * 
+   * @throws MalformedURLException
+   * @throws IOException
+   */
+  @Test(groups = "External")
+  public void testHMMBuildThenHMMAlign()
+          throws MalformedURLException, IOException
   {
-    FastaFile file = null;
-    try
-    {
-      file = new FastaFile(
-              new FileParse("examples/uniref50.fa", DataSourceType.FILE));
-    } catch (IOException e)
-    {
-      e.printStackTrace();
-      fail();
-    }
-    SequenceI[] seqs = file.getSeqsAsArray();
-    AlignmentI al = new Alignment(seqs);
-    frame = new AlignFrame(al, 150, 20);
+    /*
+     * run hmmbuild
+     */
+    testHMMBuild();
+    List<SequenceI> hmms = frame.getViewport().getAlignment()
+            .getHmmSequences();
+    assertFalse(hmms.isEmpty());
+
+    /*
+     * now run hmmalign - by default with respect to the added HMM profile
+     */
+    testHMMAlign();
+  }
 
-    HMMBuildThread thread = new HMMBuildThread(frame,
-            new ArrayList<ArgumentI>());
-    thread.hmmbuildWaitTillComplete();
+  public void testHMMBuild()
+  {
+    /*
+     * set up argument to run hmmbuild for the alignment
+     */
+    ArrayList<ArgumentI> params = new ArrayList<>();
+    String argName = MessageManager.getString("label.hmmbuild_for");
+    String argValue = MessageManager.getString("label.alignment");
+    params.add(
+            new Option(argName, null, false, null, argValue, null, null));
+
+    HMMBuild builder = new HMMBuild(frame, params);
+    builder.run();
 
     SequenceI seq = frame.getViewport().getAlignment().getSequenceAt(0);
     HiddenMarkovModel hmm = seq.getHMM();
-    if (hmm == null)
-    {
-      fail();
-    }
+    assertNotNull(hmm);
 
-    assertEquals(hmm.getLength().intValue(), 148);
+    assertEquals(hmm.getLength(), 148);
     assertEquals(hmm.getAlphabetType(), "amino");
-    assertEquals(hmm.getName(), "Alignment");
-    assertEquals(hmm.getEffectiveNumberOfSequences(), 0.648193, 0.0001);
-    assertEquals(hmm.getConsensusAtAlignColumn(15), 's');
+    assertEquals(hmm.getName(), "Alignment_HMM");
+    assertEquals(hmm.getProperty(HMMFile.EFF_NUMBER_OF_SEQUENCES),
+            "0.648193");
   }
 
-  @Test(priority = 1)
-  public void testHMMAlign() throws MalformedURLException, IOException
+  public void testHMMAlign()
   {
-    HMMAlignThread thread = new HMMAlignThread(frame, true,
+    HmmerCommand thread = new HMMAlign(frame,
             new ArrayList<ArgumentI>());
-    try
-    {
-      thread.hmmalignWaitTillComplete();
-    } catch (Exception e)
-    {
-      e.printStackTrace();
-      fail();
-    }
+    thread.run();
 
-    if (Desktop.getAlignFrames() == null)
+    AlignFrame[] alignFrames = Desktop.getAlignFrames();
+    if (alignFrames == null)
     {
-      fail();
+      fail("No align frame loaded");
     }
 
-    AlignFrame frame = Desktop.getAlignFrames()[0];
-    AlignmentI al = frame.getViewport().getAlignment();
-    assertNotEquals(al, null);
-    List<SequenceI> hmmSeqs = al.getHMMConsensusSequences(false);
-    assertNotEquals(hmmSeqs, null);
-
+    /*
+     * now have the original align frame, and another for realigned sequences
+     */
+    assertEquals(alignFrames.length, 2);
+    AlignmentI original = alignFrames[0].getViewport().getAlignment();
+    assertNotNull(original);
+    AlignmentI realigned = alignFrames[1].getViewport().getAlignment();
+    assertNotNull(realigned);
+    assertFalse(original.getHmmSequences().isEmpty());
+    assertFalse(realigned.getHmmSequences().isEmpty());
+
+    SequenceI ferCapan = original.findName("FER_CAPAN");
+    assertTrue(ferCapan.getSequenceAsString().startsWith("MA------SVSAT"));
+
+    SequenceI ferCapanRealigned = realigned.findName("FER_CAPAN");
+    assertTrue(ferCapanRealigned.getSequenceAsString()
+            .startsWith("-------m-A----SVSAT"));
   }
 }