JAL-2089 patch broken merge to master for Release 2.10.0b1
[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.io.FileLoader;
28 import jalview.io.FormatAdapter;
29
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
32
33 public class FeatureScoreModelTest
34 {
35   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";
36
37   int[] sf1 = new int[] { 74, 74, 73, 73, 23, 23, -1, -1 };
38
39   int[] sf2 = new int[] { -1, -1, 74, 75, -1, -1, 76, 77 };
40
41   int[] sf3 = new int[] { -1, -1, -1, -1, -1, -1, 76, 77 };
42
43   public AlignFrame getTestAlignmentFrame()
44   {
45     AlignFrame alf = new FileLoader(false).LoadFileWaitTillLoaded(
46             alntestFile, FormatAdapter.PASTE);
47     AlignmentI al = alf.getViewport().getAlignment();
48     Assert.assertEquals(al.getHeight(), 4);
49     Assert.assertEquals(al.getWidth(), 5);
50     for (int i = 0; i < 4; i++)
51     {
52       SequenceI ds = al.getSequenceAt(i).getDatasetSequence();
53       if (sf1[i * 2] > 0)
54       {
55         ds.addSequenceFeature(new SequenceFeature("sf1", "sf1", "sf1",
56                 sf1[i * 2], sf1[i * 2 + 1], "sf1"));
57       }
58       if (sf2[i * 2] > 0)
59       {
60         ds.addSequenceFeature(new SequenceFeature("sf2", "sf2", "sf2",
61                 sf2[i * 2], sf2[i * 2 + 1], "sf2"));
62       }
63       if (sf3[i * 2] > 0)
64       {
65         ds.addSequenceFeature(new SequenceFeature("sf3", "sf3", "sf3",
66                 sf3[i * 2], sf3[i * 2 + 1], "sf3"));
67       }
68     }
69     alf.setShowSeqFeatures(true);
70     alf.getFeatureRenderer().setVisible("sf1");
71     alf.getFeatureRenderer().setVisible("sf2");
72     alf.getFeatureRenderer().setVisible("sf3");
73     alf.getFeatureRenderer().findAllFeatures(true);
74     Assert.assertEquals(alf.getFeatureRenderer().getDisplayedFeatureTypes()
75             .size(), 3, "Number of feature types");
76     Assert.assertTrue(alf.getCurrentView().areFeaturesDisplayed());
77     return alf;
78   }
79
80   @Test(groups = { "Functional" })
81   public void testFeatureScoreModel() throws Exception
82   {
83     AlignFrame alf = getTestAlignmentFrame();
84     FeatureScoreModel fsm = new FeatureScoreModel();
85     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
86             .getAlignPanel()));
87     alf.selectAllSequenceMenuItem_actionPerformed(null);
88     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
89             true));
90     Assert.assertTrue(dm[0][2] == 0f,
91             "FER1_MESCR (0) should be identical with RAPSA (2)");
92     Assert.assertTrue(dm[0][1] > dm[0][2],
93             "FER1_MESCR (0) should be further from SPIOL (1) than it is from RAPSA (2)");
94   }
95
96   @Test(groups = { "Functional" })
97   public void testFeatureScoreModel_hiddenFirstColumn() throws Exception
98   {
99     AlignFrame alf = getTestAlignmentFrame();
100     // hiding first two columns shouldn't affect the tree
101     alf.getViewport().hideColumns(0, 1);
102     FeatureScoreModel fsm = new FeatureScoreModel();
103     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
104             .getAlignPanel()));
105     alf.selectAllSequenceMenuItem_actionPerformed(null);
106     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
107             true));
108     Assert.assertTrue(dm[0][2] == 0f,
109             "FER1_MESCR (0) should be identical with RAPSA (2)");
110     Assert.assertTrue(dm[0][1] > dm[0][2],
111             "FER1_MESCR (0) should be further from SPIOL (1) than it is from RAPSA (2)");
112   }
113
114   @Test(groups = { "Functional" })
115   public void testFeatureScoreModel_HiddenColumns() throws Exception
116   {
117     AlignFrame alf = getTestAlignmentFrame();
118     // hide columns and check tree changes
119     alf.getViewport().hideColumns(3, 4);
120     alf.getViewport().hideColumns(0, 1);
121     FeatureScoreModel fsm = new FeatureScoreModel();
122     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
123             .getAlignPanel()));
124     alf.selectAllSequenceMenuItem_actionPerformed(null);
125     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
126             true));
127     Assert.assertTrue(
128             dm[0][2] == 0f,
129             "After hiding last two columns FER1_MESCR (0) should still be identical with RAPSA (2)");
130     Assert.assertTrue(
131             dm[0][1] == 0f,
132             "After hiding last two columns FER1_MESCR (0) should now also be identical with SPIOL (1)");
133     for (int s = 0; s < 3; s++)
134     {
135       Assert.assertTrue(dm[s][3] > 0f, "After hiding last two columns "
136               + alf.getViewport().getAlignment().getSequenceAt(s).getName()
137               + "(" + s + ") should still be distinct from FER1_MAIZE (3)");
138     }
139   }
140 }