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