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