JAL-2629 tidy tests, refactor hmmer node mapping slightly
[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.assertNotNull;
5 import static org.testng.Assert.assertTrue;
6 import static org.testng.Assert.fail;
7
8 import jalview.bin.Jalview;
9 import jalview.datamodel.AlignmentI;
10 import jalview.datamodel.HiddenMarkovModel;
11 import jalview.datamodel.SequenceI;
12 import jalview.gui.AlignFrame;
13 import jalview.gui.Desktop;
14 import jalview.ws.params.ArgumentI;
15
16 import java.io.IOException;
17 import java.net.MalformedURLException;
18 import java.util.ArrayList;
19
20 import org.testng.annotations.AfterClass;
21 import org.testng.annotations.BeforeClass;
22 import org.testng.annotations.Test;
23
24 public class HMMERTest {
25
26   AlignFrame frame;
27
28   @BeforeClass(alwaysRun = true)
29   public void setUpBeforeClass() throws Exception
30   {
31     Jalview.main(
32             new String[]
33     { "-noquestionnaire", "-nonews", "-props",
34                 "test/jalview/hmmer/testProps.jvprops", "-open",
35                 "examples/uniref50.fa" });
36     // FastaFile file = null;
37     // file = new FastaFile(
38     // new FileParse("examples/uniref50.fa", DataSourceType.FILE));
39     // SequenceI[] seqs = file.getSeqsAsArray();
40     // AlignmentI al = new Alignment(seqs);
41     // frame = new AlignFrame(al, 150, 20);
42     frame = Desktop.getAlignFrames()[0];
43   }
44
45   @AfterClass(alwaysRun = true)
46   public static void tearDownAfterClass() throws Exception
47   {
48     Desktop.instance.closeAll_actionPerformed(null);
49   }
50
51   /**
52    * Test with a dependency on locally installed hmmbuild binaries
53    * 
54    * @throws MalformedURLException
55    * @throws IOException
56    */
57   @Test(groups = "External")
58   public void testHMMBuildThenHMMAlign()
59           throws MalformedURLException, IOException
60   {
61     /*
62      * run hmmbuild - not the side-effect of selecting the HMM
63      * sequence that gets added to the alignment
64      */
65     testHMMBuild();
66     HiddenMarkovModel hmm = frame.getSelectedHMM();
67
68     /*
69      * now run hmmalign - with respect to the select HMM profile
70      */
71     testHMMAlign();
72   }
73
74   public void testHMMBuild()
75   {
76     HMMBuildThread thread = new HMMBuildThread(frame,
77             new ArrayList<ArgumentI>());
78     thread.hmmbuildWaitTillComplete();
79
80     SequenceI seq = frame.getViewport().getAlignment().getSequenceAt(0);
81     HiddenMarkovModel hmm = seq.getHMM();
82     assertNotNull(hmm);
83
84     assertEquals(hmm.getLength().intValue(), 148);
85     assertEquals(hmm.getAlphabetType(), "amino");
86     assertEquals(hmm.getName(), "Alignment");
87     assertEquals(hmm.getEffectiveNumberOfSequences(), 0.648193, 0.0001);
88     assertEquals(hmm.getConsensusAtAlignColumn(15), 's');
89   }
90
91   public void testHMMAlign()
92   {
93     HMMAlignThread thread = new HMMAlignThread(frame,
94             new ArrayList<ArgumentI>());
95     thread.hmmalignWaitTillComplete();
96
97     AlignFrame[] alignFrames = Desktop.getAlignFrames();
98     if (alignFrames == null)
99     {
100       fail("No align frame loaded");
101     }
102
103     /*
104      * now have the original align frame, and another for realigned sequences
105      */
106     assertEquals(alignFrames.length, 2);
107     AlignmentI original = alignFrames[0].getViewport().getAlignment();
108     assertNotNull(original);
109     AlignmentI realigned = alignFrames[1].getViewport().getAlignment();
110     assertNotNull(realigned);
111     assertNotNull(original.getHMMConsensusSequences());
112     assertNotNull(realigned.getHMMConsensusSequences());
113
114     SequenceI ferCapan = original.findName("FER_CAPAN");
115     assertTrue(ferCapan.getSequenceAsString().startsWith("MA------SVSAT"));
116
117     SequenceI ferCapanRealigned = realigned.findName("FER_CAPAN");
118     assertTrue(ferCapanRealigned.getSequenceAsString()
119             .startsWith("-------m-A----SVSAT"));
120   }
121 }
122