JAL-2629 storage and lifecycle of alignment/group HMM annotations revised
[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 - note the side-effect of selecting the HMM
64      * sequence that gets added to the alignment
65      */
66     testHMMBuild();
67     HiddenMarkovModel hmm = frame.getSelectedHMM();
68     assertNotNull(hmm);
69
70     /*
71      * now run hmmalign - with respect to the select HMM profile
72      */
73     testHMMAlign();
74   }
75
76   public void testHMMBuild()
77   {
78     HMMBuild builder = new HMMBuild(frame,
79             new ArrayList<ArgumentI>());
80     builder.run();
81
82     SequenceI seq = frame.getViewport().getAlignment().getSequenceAt(0);
83     HiddenMarkovModel hmm = seq.getHMM();
84     assertNotNull(hmm);
85
86     assertEquals(hmm.getLength(), 148);
87     assertEquals(hmm.getAlphabetType(), "amino");
88     assertEquals(hmm.getName(), "Alignment_HMM");
89     assertEquals(hmm.getProperty(HMMFile.EFF_NUMBER_OF_SEQUENCES),
90             "0.648193");
91     assertEquals(hmm.getConsensusAtAlignColumn(15), 's');
92   }
93
94   public void testHMMAlign()
95   {
96     HMMAlign thread = new HMMAlign(frame,
97             new ArrayList<ArgumentI>());
98     thread.run();
99
100     AlignFrame[] alignFrames = Desktop.getAlignFrames();
101     if (alignFrames == null)
102     {
103       fail("No align frame loaded");
104     }
105
106     /*
107      * now have the original align frame, and another for realigned sequences
108      */
109     assertEquals(alignFrames.length, 2);
110     AlignmentI original = alignFrames[0].getViewport().getAlignment();
111     assertNotNull(original);
112     AlignmentI realigned = alignFrames[1].getViewport().getAlignment();
113     assertNotNull(realigned);
114     assertNotNull(original.getHmmConsensus());
115     assertNotNull(realigned.getHmmConsensus());
116
117     SequenceI ferCapan = original.findName("FER_CAPAN");
118     assertTrue(ferCapan.getSequenceAsString().startsWith("MA------SVSAT"));
119
120     SequenceI ferCapanRealigned = realigned.findName("FER_CAPAN");
121     assertTrue(ferCapanRealigned.getSequenceAsString()
122             .startsWith("-------m-A----SVSAT"));
123   }
124 }
125