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