Merge branch 'develop' into trialMerge
[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.DataSourceType;
29 import jalview.io.FileLoader;
30
31 import java.util.Arrays;
32
33 import org.testng.Assert;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 public class FeatureScoreModelTest
38 {
39
40   @BeforeClass(alwaysRun = true)
41   public void setUpJvOptionPane()
42   {
43     JvOptionPane.setInteractiveMode(false);
44     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
45   }
46
47   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";
48
49   int[] sf1 = new int[] { 74, 74, 73, 73, 23, 23, -1, -1 };
50
51   int[] sf2 = new int[] { -1, -1, 74, 75, -1, -1, 76, 77 };
52
53   int[] sf3 = new int[] { -1, -1, -1, -1, -1, -1, 76, 77 };
54
55   public AlignFrame getTestAlignmentFrame()
56   {
57     AlignFrame alf = new FileLoader(false).LoadFileWaitTillLoaded(
58             alntestFile, DataSourceType.PASTE);
59     AlignmentI al = alf.getViewport().getAlignment();
60     Assert.assertEquals(al.getHeight(), 4);
61     Assert.assertEquals(al.getWidth(), 5);
62     for (int i = 0; i < 4; i++)
63     {
64       SequenceI ds = al.getSequenceAt(i).getDatasetSequence();
65       if (sf1[i * 2] > 0)
66       {
67         ds.addSequenceFeature(new SequenceFeature("sf1", "sf1", "sf1",
68                 sf1[i * 2], sf1[i * 2 + 1], "sf1"));
69       }
70       if (sf2[i * 2] > 0)
71       {
72         ds.addSequenceFeature(new SequenceFeature("sf2", "sf2", "sf2",
73                 sf2[i * 2], sf2[i * 2 + 1], "sf2"));
74       }
75       if (sf3[i * 2] > 0)
76       {
77         ds.addSequenceFeature(new SequenceFeature("sf3", "sf3", "sf3",
78                 sf3[i * 2], sf3[i * 2 + 1], "sf3"));
79       }
80     }
81     alf.setShowSeqFeatures(true);
82     alf.getFeatureRenderer().setVisible("sf1");
83     alf.getFeatureRenderer().setVisible("sf2");
84     alf.getFeatureRenderer().setVisible("sf3");
85     alf.getFeatureRenderer().findAllFeatures(true);
86     Assert.assertEquals(alf.getFeatureRenderer().getDisplayedFeatureTypes()
87             .size(), 3, "Number of feature types");
88     Assert.assertTrue(alf.getCurrentView().areFeaturesDisplayed());
89     return alf;
90   }
91
92   @Test(groups = { "Functional" })
93   public void testFeatureScoreModel() throws Exception
94   {
95     AlignFrame alf = getTestAlignmentFrame();
96     FeatureScoreModel fsm = new FeatureScoreModel();
97     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
98             .getAlignPanel()));
99     alf.selectAllSequenceMenuItem_actionPerformed(null);
100     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
101             true));
102     Assert.assertTrue(dm[0][2] == 0f,
103             "FER1_MESCR (0) should be identical with RAPSA (2)");
104     Assert.assertTrue(dm[0][1] > dm[0][2],
105             "FER1_MESCR (0) should be further from SPIOL (1) than it is from RAPSA (2)");
106   }
107
108   @Test(groups = { "Functional" })
109   public void testFeatureScoreModel_hiddenFirstColumn() throws Exception
110   {
111     AlignFrame alf = getTestAlignmentFrame();
112     // hiding first two columns shouldn't affect the tree
113     alf.getViewport().hideColumns(0, 1);
114     FeatureScoreModel fsm = new FeatureScoreModel();
115     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
116             .getAlignPanel()));
117     alf.selectAllSequenceMenuItem_actionPerformed(null);
118     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
119             true));
120     Assert.assertTrue(dm[0][2] == 0f,
121             "FER1_MESCR (0) should be identical with RAPSA (2)");
122     Assert.assertTrue(dm[0][1] > dm[0][2],
123             "FER1_MESCR (0) should be further from SPIOL (1) than it is from RAPSA (2)");
124   }
125
126   @Test(groups = { "Functional" })
127   public void testFeatureScoreModel_HiddenColumns() throws Exception
128   {
129     AlignFrame alf = getTestAlignmentFrame();
130     // hide columns and check tree changes
131     alf.getViewport().hideColumns(3, 4);
132     alf.getViewport().hideColumns(0, 1);
133     FeatureScoreModel fsm = new FeatureScoreModel();
134     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
135             .getAlignPanel()));
136     alf.selectAllSequenceMenuItem_actionPerformed(null);
137     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
138             true));
139     Assert.assertTrue(
140             dm[0][2] == 0f,
141             "After hiding last two columns FER1_MESCR (0) should still be identical with RAPSA (2)");
142     Assert.assertTrue(
143             dm[0][1] == 0f,
144             "After hiding last two columns FER1_MESCR (0) should now also be identical with SPIOL (1)");
145     for (int s = 0; s < 3; s++)
146     {
147       Assert.assertTrue(dm[s][3] > 0f, "After hiding last two columns "
148               + alf.getViewport().getAlignment().getSequenceAt(s).getName()
149               + "(" + s + ") should still be distinct from FER1_MAIZE (3)");
150     }
151   }
152
153   /**
154    * Check findFeatureAt doesn't return contact features except at contact
155    * points TODO:move to under the FeatureRendererModel test suite
156    */
157   @Test(groups = { "Functional" })
158   public void testFindFeatureAt_PointFeature() throws Exception
159   {
160     String alignment = "a CCCCCCGGGGGGCCCCCC\n" + "b CCCCCCGGGGGGCCCCCC\n"
161             + "c CCCCCCGGGGGGCCCCCC\n";
162     AlignFrame af = new jalview.io.FileLoader(false)
163             .LoadFileWaitTillLoaded(alignment, DataSourceType.PASTE);
164     SequenceI aseq = af.getViewport().getAlignment().getSequenceAt(0);
165     SequenceFeature sf = null;
166     sf = new SequenceFeature("disulphide bond", "", 2, 5, Float.NaN, "");
167     aseq.addSequenceFeature(sf);
168     Assert.assertTrue(sf.isContactFeature());
169     af.refreshFeatureUI(true);
170     af.getFeatureRenderer().setAllVisible(Arrays.asList("disulphide bond"));
171     Assert.assertEquals(af.getFeatureRenderer().getDisplayedFeatureTypes()
172             .size(), 1, "Should be just one feature type displayed");
173     // step through and check for pointwise feature presence/absence
174     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 1)
175             .size(), 0);
176     // step through and check for pointwise feature presence/absence
177     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 2)
178             .size(), 1);
179     // step through and check for pointwise feature presence/absence
180     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 3)
181             .size(), 0);
182     // step through and check for pointwise feature presence/absence
183     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 4)
184             .size(), 0);
185     // step through and check for pointwise feature presence/absence
186     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 5)
187             .size(), 1);
188     // step through and check for pointwise feature presence/absence
189     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 6)
190             .size(), 0);
191   }
192
193 }