JAL-2326 added setup method for JvOptionPane in all Jalveiw test classes to enable...
[jalview.git] / test / jalview / analysis / scoremodels / FeatureScoreModelTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis.scoremodels;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.datamodel.SequenceI;
26 import jalview.gui.AlignFrame;
27 import jalview.gui.JvOptionPane;
28 import jalview.io.FileLoader;
29 import jalview.io.FormatAdapter;
30
31 import org.testng.Assert;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
34
35 public class FeatureScoreModelTest
36 {
37
38   @BeforeClass(alwaysRun = true)
39   public void setUpJvOptionPane()
40   {
41     JvOptionPane.setInteractiveMode(false);
42     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
43   }
44
45   public static String alntestFile = "FER1_MESCR/72-76 DVYIL\nFER1_SPIOL/71-75 DVYIL\nFER3_RAPSA/21-25 DVYVL\nFER1_MAIZE/73-77 DVYIL\n";
46
47   int[] sf1 = new int[] { 74, 74, 73, 73, 23, 23, -1, -1 };
48
49   int[] sf2 = new int[] { -1, -1, 74, 75, -1, -1, 76, 77 };
50
51   int[] sf3 = new int[] { -1, -1, -1, -1, -1, -1, 76, 77 };
52
53   public AlignFrame getTestAlignmentFrame()
54   {
55     AlignFrame alf = new FileLoader(false).LoadFileWaitTillLoaded(
56             alntestFile, FormatAdapter.PASTE);
57     AlignmentI al = alf.getViewport().getAlignment();
58     Assert.assertEquals(al.getHeight(), 4);
59     Assert.assertEquals(al.getWidth(), 5);
60     for (int i = 0; i < 4; i++)
61     {
62       SequenceI ds = al.getSequenceAt(i).getDatasetSequence();
63       if (sf1[i * 2] > 0)
64       {
65         ds.addSequenceFeature(new SequenceFeature("sf1", "sf1", "sf1",
66                 sf1[i * 2], sf1[i * 2 + 1], "sf1"));
67       }
68       if (sf2[i * 2] > 0)
69       {
70         ds.addSequenceFeature(new SequenceFeature("sf2", "sf2", "sf2",
71                 sf2[i * 2], sf2[i * 2 + 1], "sf2"));
72       }
73       if (sf3[i * 2] > 0)
74       {
75         ds.addSequenceFeature(new SequenceFeature("sf3", "sf3", "sf3",
76                 sf3[i * 2], sf3[i * 2 + 1], "sf3"));
77       }
78     }
79     alf.setShowSeqFeatures(true);
80     alf.getFeatureRenderer().setVisible("sf1");
81     alf.getFeatureRenderer().setVisible("sf2");
82     alf.getFeatureRenderer().setVisible("sf3");
83     alf.getFeatureRenderer().findAllFeatures(true);
84     Assert.assertEquals(alf.getFeatureRenderer().getDisplayedFeatureTypes()
85             .size(), 3, "Number of feature types");
86     Assert.assertTrue(alf.getCurrentView().areFeaturesDisplayed());
87     return alf;
88   }
89
90   @Test(groups = { "Functional" })
91   public void testFeatureScoreModel() throws Exception
92   {
93     AlignFrame alf = getTestAlignmentFrame();
94     FeatureScoreModel fsm = new FeatureScoreModel();
95     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
96             .getAlignPanel()));
97     alf.selectAllSequenceMenuItem_actionPerformed(null);
98     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
99             true));
100     Assert.assertTrue(dm[0][2] == 0f,
101             "FER1_MESCR (0) should be identical with RAPSA (2)");
102     Assert.assertTrue(dm[0][1] > dm[0][2],
103             "FER1_MESCR (0) should be further from SPIOL (1) than it is from RAPSA (2)");
104   }
105
106   @Test(groups = { "Functional" })
107   public void testFeatureScoreModel_hiddenFirstColumn() throws Exception
108   {
109     AlignFrame alf = getTestAlignmentFrame();
110     // hiding first two columns shouldn't affect the tree
111     alf.getViewport().hideColumns(0, 1);
112     FeatureScoreModel fsm = new FeatureScoreModel();
113     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
114             .getAlignPanel()));
115     alf.selectAllSequenceMenuItem_actionPerformed(null);
116     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
117             true));
118     Assert.assertTrue(dm[0][2] == 0f,
119             "FER1_MESCR (0) should be identical with RAPSA (2)");
120     Assert.assertTrue(dm[0][1] > dm[0][2],
121             "FER1_MESCR (0) should be further from SPIOL (1) than it is from RAPSA (2)");
122   }
123
124   @Test(groups = { "Functional" })
125   public void testFeatureScoreModel_HiddenColumns() throws Exception
126   {
127     AlignFrame alf = getTestAlignmentFrame();
128     // hide columns and check tree changes
129     alf.getViewport().hideColumns(3, 4);
130     alf.getViewport().hideColumns(0, 1);
131     FeatureScoreModel fsm = new FeatureScoreModel();
132     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
133             .getAlignPanel()));
134     alf.selectAllSequenceMenuItem_actionPerformed(null);
135     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
136             true));
137     Assert.assertTrue(
138             dm[0][2] == 0f,
139             "After hiding last two columns FER1_MESCR (0) should still be identical with RAPSA (2)");
140     Assert.assertTrue(
141             dm[0][1] == 0f,
142             "After hiding last two columns FER1_MESCR (0) should now also be identical with SPIOL (1)");
143     for (int s = 0; s < 3; s++)
144     {
145       Assert.assertTrue(dm[s][3] > 0f, "After hiding last two columns "
146               + alf.getViewport().getAlignment().getSequenceAt(s).getName()
147               + "(" + s + ") should still be distinct from FER1_MAIZE (3)");
148     }
149   }
150 }