Merge branch 'Jalview-JS/develop' into merge_js_develop
[jalview.git] / test / jalview / hmmer / HMMERTest.java
1 package jalview.hmmer;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertNotNull;
6 import static org.testng.Assert.assertTrue;
7 import static org.testng.Assert.fail;
8
9 import jalview.bin.Jalview;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.HiddenMarkovModel;
12 import jalview.datamodel.SequenceI;
13 import jalview.gui.AlignFrame;
14 import jalview.gui.Desktop;
15 import jalview.io.HMMFile;
16 import jalview.util.MessageManager;
17 import jalview.ws.params.ArgumentI;
18 import jalview.ws.params.simple.Option;
19
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.testng.annotations.AfterClass;
26 import org.testng.annotations.BeforeClass;
27 import org.testng.annotations.Test;
28
29 public class HMMERTest {
30
31   AlignFrame frame;
32
33   @BeforeClass(alwaysRun = true)
34   public void setUpBeforeClass() throws Exception
35   {
36     /*
37      * NB: check HMMER_PATH in testProps.jvprops is valid for
38      * the machine on which this runs
39      */
40     Jalview.main(
41             new String[]
42     { "-noquestionnaire", "-nonews", "-props",
43                 "test/jalview/hmmer/testProps.jvprops", "-open",
44                 "examples/uniref50.fa" });
45     frame = Desktop.getAlignFrames()[0];
46   }
47
48   @AfterClass(alwaysRun = true)
49   public static void tearDownAfterClass() throws Exception
50   {
51     Desktop.getInstance().closeAll_actionPerformed(null);
52   }
53
54   /**
55    * Test with a dependency on locally installed hmmbuild binaries
56    * 
57    * @throws MalformedURLException
58    * @throws IOException
59    */
60   @Test(groups = "External")
61   public void testHMMBuildThenHMMAlign()
62           throws MalformedURLException, IOException
63   {
64     /*
65      * run hmmbuild
66      */
67     testHMMBuild();
68     List<SequenceI> hmms = frame.getViewport().getAlignment()
69             .getHmmSequences();
70     assertFalse(hmms.isEmpty());
71
72     /*
73      * now run hmmalign - by default with respect to the added HMM profile
74      */
75     testHMMAlign();
76   }
77
78   public void testHMMBuild()
79   {
80     /*
81      * set up argument to run hmmbuild for the alignment
82      */
83     ArrayList<ArgumentI> params = new ArrayList<>();
84     String argName = MessageManager.getString("label.hmmbuild_for");
85     String argValue = MessageManager.getString("label.alignment");
86     params.add(
87             new Option(argName, null, false, null, argValue, null, null));
88
89     HMMBuild builder = new HMMBuild(frame, params);
90     builder.run();
91
92     SequenceI seq = frame.getViewport().getAlignment().getSequenceAt(0);
93     HiddenMarkovModel hmm = seq.getHMM();
94     assertNotNull(hmm);
95
96     assertEquals(hmm.getLength(), 148);
97     assertEquals(hmm.getAlphabetType(), "amino");
98     assertEquals(hmm.getName(), "Alignment_HMM");
99     assertEquals(hmm.getProperty(HMMFile.EFF_NUMBER_OF_SEQUENCES),
100             "0.648193");
101   }
102
103   public void testHMMAlign()
104   {
105     HmmerCommand thread = new HMMAlign(frame,
106             new ArrayList<ArgumentI>());
107     thread.run();
108
109     AlignFrame[] alignFrames = Desktop.getAlignFrames();
110     if (alignFrames == null)
111     {
112       fail("No align frame loaded");
113     }
114
115     /*
116      * now have the original align frame, and another for realigned sequences
117      */
118     assertEquals(alignFrames.length, 2);
119     AlignmentI original = alignFrames[0].getViewport().getAlignment();
120     assertNotNull(original);
121     AlignmentI realigned = alignFrames[1].getViewport().getAlignment();
122     assertNotNull(realigned);
123     assertFalse(original.getHmmSequences().isEmpty());
124     assertFalse(realigned.getHmmSequences().isEmpty());
125
126     SequenceI ferCapan = original.findName("FER_CAPAN");
127     assertTrue(ferCapan.getSequenceAsString().startsWith("MA------SVSAT"));
128
129     SequenceI ferCapanRealigned = realigned.findName("FER_CAPAN");
130     assertTrue(ferCapanRealigned.getSequenceAsString()
131             .startsWith("-------m-A----SVSAT"));
132   }
133 }
134