JAL-3127 set ‘updateStructures’ flag for viewports associated with each tree to true...
[jalview.git] / test / jalview / analysis / scoremodels / PIDModelTest.java
1 package jalview.analysis.scoremodels;
2
3 import static org.testng.Assert.assertEquals;
4
5 import jalview.api.analysis.SimilarityParamsI;
6 import jalview.util.Comparison;
7
8 import org.testng.annotations.Test;
9
10 public class PIDModelTest
11 {
12   private static final double DELTA = 0.00001D;
13
14   @Test(groups = "Functional")
15   public void testGetPairwiseScore()
16   {
17     PIDModel sm = new PIDModel();
18     assertEquals(sm.getPairwiseScore('A', 'A'), 1f);
19     assertEquals(sm.getPairwiseScore('A', 'a'), 1f);
20     assertEquals(sm.getPairwiseScore('a', 'A'), 1f);
21     assertEquals(sm.getPairwiseScore('A', 'B'), 0f);
22     assertEquals(sm.getPairwiseScore('A', ' '), 0f);
23     assertEquals(sm.getPairwiseScore(' ', ' '), 0f);
24     assertEquals(sm.getPairwiseScore('.', '.'), 0f);
25     assertEquals(sm.getPairwiseScore('-', '-'), 0f);
26   }
27
28   /**
29    * Regression test to verify that a (suitably configured) PIDModel computes
30    * the same percentage identities as the Comparison.PID method
31    */
32   @Test(groups = "Functional")
33   public void testComputePID_matchesComparisonPID()
34   {
35     SimilarityParamsI params = new SimilarityParams(true, true, true, true);
36
37     /*
38      * same length, no gaps
39      */
40     String s1 = "ARFNQDWSGI";
41     String s2 = "ARKNQDQSGI";
42
43     new PIDModel();
44     double newScore = PIDModel.computePID(s1, s2, params);
45     double oldScore = Comparison.PID(s1, s2);
46     assertEquals(newScore, oldScore, DELTA);
47
48     /*
49      * same length, with gaps
50      */
51     s1 = "-RFNQDWSGI";
52     s2 = "ARKNQ-QSGI";
53     new PIDModel();
54     newScore = PIDModel.computePID(s1, s2, params);
55     oldScore = Comparison.PID(s1, s2);
56     assertEquals(newScore, oldScore, DELTA);
57
58     /*
59      * s2 longer than s1, with gaps
60      */
61     s1 = "ARK-";
62     s2 = "-RFNQ";
63     new PIDModel();
64     newScore = PIDModel.computePID(s1, s2, params);
65     oldScore = Comparison.PID(s1, s2);
66     assertEquals(newScore, oldScore, DELTA);
67
68     /*
69      * s1 longer than s2, with gaps
70      */
71     s1 = "-RFNQ";
72     s2 = "ARK-";
73     new PIDModel();
74     newScore = PIDModel.computePID(s1, s2, params);
75     oldScore = Comparison.PID(s1, s2);
76     assertEquals(newScore, oldScore, DELTA);
77
78     /*
79      * same but now also with gapped columns
80      */
81     s1 = "-R-F-NQ";
82     s2 = "AR-K--";
83     new PIDModel();
84     newScore = PIDModel.computePID(s1, s2, params);
85     oldScore = Comparison.PID(s1, s2);
86     assertEquals(newScore, oldScore, DELTA);
87   }
88
89   /**
90    * Tests for percentage identity variants where only the shorter length of two
91    * sequences is used
92    */
93   @Test(groups = "Functional")
94   public void testComputePID_matchShortestSequence()
95   {
96     String s1 = "FR-K-S";
97     String s2 = "FS--L";
98
99     /*
100      * match gap-gap and gap-char
101      * PID = 4/5 = 80%
102      */
103     SimilarityParamsI params = new SimilarityParams(true, true, true, true);
104     assertEquals(PIDModel.computePID(s1, s2, params), 80d);
105
106     /*
107      * match gap-char but not gap-gap
108      * PID = 3/4 = 75%
109      */
110     params = new SimilarityParams(false, true, true, true);
111     assertEquals(PIDModel.computePID(s1, s2, params), 75d);
112
113     /*
114      * include gaps but don't match them
115      * include gap-gap, counted as identity
116      * PID = 2/5 = 40%
117      */
118     params = new SimilarityParams(true, false, true, true);
119     assertEquals(PIDModel.computePID(s1, s2, params), 40d);
120
121     /*
122      * include gaps but don't match them
123      * exclude gap-gap
124      * PID = 1/4 = 25%
125      */
126     params = new SimilarityParams(false, false, true, true);
127     assertEquals(PIDModel.computePID(s1, s2, params), 25d);
128   }
129
130   /**
131    * Tests for percentage identity variants where the longer length of two
132    * sequences is used
133    */
134   @Test(groups = "Functional")
135   public void testComputePID_matchLongestSequence()
136   {
137     String s1 = "FR-K-S";
138     String s2 = "FS--L";
139   
140     /*
141      * match gap-gap and gap-char
142      * shorter sequence treated as if with trailing gaps
143      * PID = 5/6 = 83.333...%
144      */
145     SimilarityParamsI params = new SimilarityParams(true, true, true, false);
146     assertEquals(PIDModel.computePID(s1, s2, params), 500d / 6);
147   
148     /*
149      * match gap-char but not gap-gap
150      * PID = 4/5 = 80%
151      */
152     params = new SimilarityParams(false, true, true, false);
153     assertEquals(PIDModel.computePID(s1, s2, params), 80d);
154   
155     /*
156      * include gaps but don't match them
157      * include gap-gap, counted as identity
158      * PID = 2/6 = 33.333...%
159      */
160     params = new SimilarityParams(true, false, true, false);
161     assertEquals(PIDModel.computePID(s1, s2, params), 100d / 3);
162   
163     /*
164      * include gaps but don't match them
165      * exclude gap-gap
166      * PID = 1/5 = 25%
167      */
168     params = new SimilarityParams(false, false, true, false);
169     assertEquals(PIDModel.computePID(s1, s2, params), 20d);
170
171     /*
172      * no tests for matchGaps=true, includeGaps=false
173      * as it don't make sense
174      */
175   }
176 }