JAL-2416 replaced dependency on ResidueProperties with loaded score
[jalview.git] / test / jalview / analysis / scoremodels / FeatureDistanceModelTest.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 static org.testng.Assert.assertEquals;
24
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.gui.AlignFrame;
29 import jalview.gui.JvOptionPane;
30 import jalview.io.DataSourceType;
31 import jalview.io.FileLoader;
32
33 import java.util.Arrays;
34
35 import org.testng.Assert;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
38
39 public class FeatureDistanceModelTest
40 {
41
42   @BeforeClass(alwaysRun = true)
43   public void setUpJvOptionPane()
44   {
45     JvOptionPane.setInteractiveMode(false);
46     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
47   }
48
49   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";
50
51   int[] sf1 = new int[] { 74, 74, 73, 73, 23, 23, -1, -1 };
52
53   int[] sf2 = new int[] { -1, -1, 74, 75, -1, -1, 76, 77 };
54
55   int[] sf3 = new int[] { -1, -1, -1, -1, -1, -1, 76, 77 };
56
57   /**
58    * <pre>
59    * Load test alignment and add features to sequences: 
60    *      FER1_MESCR FER1_SPIOL FER3_RAPSA FER1_MAIZE 
61    *  sf1     X          X          X  
62    *  sf2                X                     X 
63    *  sf3                                      X
64    * </pre>
65    * 
66    * @return
67    */
68   public AlignFrame getTestAlignmentFrame()
69   {
70     AlignFrame alf = new FileLoader(false).LoadFileWaitTillLoaded(
71             alntestFile, DataSourceType.PASTE);
72     AlignmentI al = alf.getViewport().getAlignment();
73     Assert.assertEquals(al.getHeight(), 4);
74     Assert.assertEquals(al.getWidth(), 5);
75     for (int i = 0; i < 4; i++)
76     {
77       SequenceI ds = al.getSequenceAt(i).getDatasetSequence();
78       if (sf1[i * 2] > 0)
79       {
80         ds.addSequenceFeature(new SequenceFeature("sf1", "sf1", "sf1",
81                 sf1[i * 2], sf1[i * 2 + 1], "sf1"));
82       }
83       if (sf2[i * 2] > 0)
84       {
85         ds.addSequenceFeature(new SequenceFeature("sf2", "sf2", "sf2",
86                 sf2[i * 2], sf2[i * 2 + 1], "sf2"));
87       }
88       if (sf3[i * 2] > 0)
89       {
90         ds.addSequenceFeature(new SequenceFeature("sf3", "sf3", "sf3",
91                 sf3[i * 2], sf3[i * 2 + 1], "sf3"));
92       }
93     }
94     alf.setShowSeqFeatures(true);
95     alf.getFeatureRenderer().setVisible("sf1");
96     alf.getFeatureRenderer().setVisible("sf2");
97     alf.getFeatureRenderer().setVisible("sf3");
98     alf.getFeatureRenderer().findAllFeatures(true);
99     Assert.assertEquals(alf.getFeatureRenderer().getDisplayedFeatureTypes()
100             .size(), 3, "Number of feature types");
101     Assert.assertTrue(alf.getCurrentView().areFeaturesDisplayed());
102     return alf;
103   }
104
105   @Test(groups = { "Functional" })
106   public void testFeatureScoreModel() throws Exception
107   {
108     AlignFrame alf = getTestAlignmentFrame();
109     FeatureDistanceModel fsm = new FeatureDistanceModel();
110     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
111             .getAlignPanel()));
112     alf.selectAllSequenceMenuItem_actionPerformed(null);
113
114     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
115             true));
116     Assert.assertTrue(dm[0][2] == 0f,
117             "FER1_MESCR (0) should be identical with RAPSA (2)");
118     Assert.assertTrue(dm[0][1] > dm[0][2],
119             "FER1_MESCR (0) should be further from SPIOL (1) than it is from RAPSA (2)");
120   }
121
122   @Test(groups = { "Functional" })
123   public void testFeatureScoreModel_hiddenFirstColumn() throws Exception
124   {
125     AlignFrame alf = getTestAlignmentFrame();
126     // hiding first two columns shouldn't affect the tree
127     alf.getViewport().hideColumns(0, 1);
128     FeatureDistanceModel fsm = new FeatureDistanceModel();
129     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
130             .getAlignPanel()));
131     alf.selectAllSequenceMenuItem_actionPerformed(null);
132     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
133             true));
134     Assert.assertTrue(dm[0][2] == 0f,
135             "FER1_MESCR (0) should be identical with RAPSA (2)");
136     Assert.assertTrue(dm[0][1] > dm[0][2],
137             "FER1_MESCR (0) should be further from SPIOL (1) than it is from RAPSA (2)");
138   }
139
140   @Test(groups = { "Functional" })
141   public void testFeatureScoreModel_HiddenColumns() throws Exception
142   {
143     AlignFrame alf = getTestAlignmentFrame();
144     // hide columns and check tree changes
145     alf.getViewport().hideColumns(3, 4);
146     alf.getViewport().hideColumns(0, 1);
147     FeatureDistanceModel fsm = new FeatureDistanceModel();
148     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
149             .getAlignPanel()));
150     alf.selectAllSequenceMenuItem_actionPerformed(null);
151     float[][] dm = fsm.findDistances(alf.getViewport().getAlignmentView(
152             true));
153     Assert.assertTrue(
154             dm[0][2] == 0f,
155             "After hiding last two columns FER1_MESCR (0) should still be identical with RAPSA (2)");
156     Assert.assertTrue(
157             dm[0][1] == 0f,
158             "After hiding last two columns FER1_MESCR (0) should now also be identical with SPIOL (1)");
159     for (int s = 0; s < 3; s++)
160     {
161       Assert.assertTrue(dm[s][3] > 0f, "After hiding last two columns "
162               + alf.getViewport().getAlignment().getSequenceAt(s).getName()
163               + "(" + s + ") should still be distinct from FER1_MAIZE (3)");
164     }
165   }
166
167   /**
168    * Check findFeatureAt doesn't return contact features except at contact
169    * points TODO:move to under the FeatureRendererModel test suite
170    */
171   @Test(groups = { "Functional" })
172   public void testFindFeatureAt_PointFeature() throws Exception
173   {
174     String alignment = "a CCCCCCGGGGGGCCCCCC\n" + "b CCCCCCGGGGGGCCCCCC\n"
175             + "c CCCCCCGGGGGGCCCCCC\n";
176     AlignFrame af = new jalview.io.FileLoader(false)
177             .LoadFileWaitTillLoaded(alignment, DataSourceType.PASTE);
178     SequenceI aseq = af.getViewport().getAlignment().getSequenceAt(0);
179     SequenceFeature sf = null;
180     sf = new SequenceFeature("disulphide bond", "", 2, 5, Float.NaN, "");
181     aseq.addSequenceFeature(sf);
182     Assert.assertTrue(sf.isContactFeature());
183     af.refreshFeatureUI(true);
184     af.getFeatureRenderer().setAllVisible(Arrays.asList("disulphide bond"));
185     Assert.assertEquals(af.getFeatureRenderer().getDisplayedFeatureTypes()
186             .size(), 1, "Should be just one feature type displayed");
187     // step through and check for pointwise feature presence/absence
188     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 1)
189             .size(), 0);
190     // step through and check for pointwise feature presence/absence
191     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 2)
192             .size(), 1);
193     // step through and check for pointwise feature presence/absence
194     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 3)
195             .size(), 0);
196     // step through and check for pointwise feature presence/absence
197     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 4)
198             .size(), 0);
199     // step through and check for pointwise feature presence/absence
200     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 5)
201             .size(), 1);
202     // step through and check for pointwise feature presence/absence
203     Assert.assertEquals(af.getFeatureRenderer().findFeaturesAtRes(aseq, 6)
204             .size(), 0);
205   }
206
207   @Test(groups = { "Functional" })
208   public void testFindDistances() throws Exception
209   {
210     String seqs = ">s1\nABCDE\n>seq2\nABCDE\n";
211     AlignFrame alf = new FileLoader().LoadFileWaitTillLoaded(seqs,
212             DataSourceType.PASTE);
213     SequenceI s1 = alf.getViewport().getAlignment().getSequenceAt(0);
214     SequenceI s2 = alf.getViewport().getAlignment().getSequenceAt(1);
215
216     /*
217      * set domain and variant features thus:
218      *     ----5
219      *  s1 ddd..
220      *  s1 .vvv.
221      *  s1 ..vvv    
222      *  s2 .ddd. 
223      *  s2 vv..v
224      *  The number of unshared feature types per column is
225      *     20120 (two features of the same type doesn't affect score)
226      *  giving an average (pairwise distance) of 5/5 or 1.0 
227      */
228     s1.addSequenceFeature(new SequenceFeature("domain", null, 1, 3, 0f,
229             null));
230     s1.addSequenceFeature(new SequenceFeature("variant", null, 2, 4, 0f,
231             null));
232     s1.addSequenceFeature(new SequenceFeature("variant", null, 3, 5, 0f,
233             null));
234     s2.addSequenceFeature(new SequenceFeature("domain", null, 2, 4, 0f,
235             null));
236     s2.addSequenceFeature(new SequenceFeature("variant", null, 1, 2, 0f,
237             null));
238     s2.addSequenceFeature(new SequenceFeature("variant", null, 5, 5, 0f,
239             null));
240     alf.setShowSeqFeatures(true);
241     alf.getFeatureRenderer().findAllFeatures(true);
242
243     FeatureDistanceModel fsm = new FeatureDistanceModel();
244     Assert.assertTrue(fsm.configureFromAlignmentView(alf.getCurrentView()
245             .getAlignPanel()));
246     alf.selectAllSequenceMenuItem_actionPerformed(null);
247
248     float[][] distances = fsm.findDistances(alf.getViewport()
249             .getAlignmentView(true));
250     assertEquals(distances.length, 2);
251     assertEquals(distances[0][0], 0f);
252     assertEquals(distances[1][1], 0f);
253     // these left to fail pending resolution of
254     // JAL-2424 (dividing score by 6, not 5)
255     assertEquals(distances[0][1], 1f);
256     assertEquals(distances[1][0], 1f);
257   }
258
259 }