JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / test / jalview / datamodel / DBRefEntryTest.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.datamodel;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertSame;
26 import static org.testng.AssertJUnit.assertTrue;
27
28 import jalview.util.MapList;
29
30 import org.testng.annotations.Test;
31
32 public class DBRefEntryTest
33 {
34
35   /**
36    * Tests for the method that compares equality of reference (but not mapping)
37    */
38   @Test(groups = { "Functional" })
39   public void testEqualRef()
40   {
41     DBRefEntry ref1 = new DBRefEntry("UNIPROT", "1", "V71633");
42     assertTrue(ref1.equalRef(ref1));
43     assertFalse(ref1.equalRef(null));
44
45     // comparison is not case sensitive
46     DBRefEntry ref2 = new DBRefEntry("uniprot", "1", "v71633");
47     assertTrue(ref1.equalRef(ref2));
48     assertTrue(ref2.equalRef(ref1));
49
50     // source, version and accessionid must match
51     assertFalse(ref1.equalRef(new DBRefEntry("UNIPRO", "1", "V71633")));
52     assertFalse(ref1.equalRef(new DBRefEntry("UNIPROT", "2", "V71633")));
53     assertFalse(ref1.equalRef(new DBRefEntry("UNIPROT", "1", "V71632")));
54
55     // presence of or differences in mappings are ignored
56     ref1.setMap(new Mapping(new MapList(new int[] { 1, 3 }, new int[] { 1,
57         1 }, 3, 1)));
58     assertTrue(ref1.equalRef(ref2));
59     assertTrue(ref2.equalRef(ref1));
60     ref1.setMap(new Mapping(new MapList(new int[] { 1, 6 }, new int[] { 1,
61         2 }, 3, 1)));
62     assertTrue(ref1.equalRef(ref2));
63     assertTrue(ref2.equalRef(ref1));
64   }
65
66   /**
67    * Tests for the method that may update a DBRefEntry from another with a
68    * mapping or 'real' version
69    */
70   @Test(groups = { "Functional" })
71   public void testUpdateFrom()
72   {
73     DBRefEntry ref1 = new DBRefEntry("UNIPROT", "1", "V71633");
74
75     assertFalse(ref1.updateFrom(null));
76
77     /*
78      * equivalent other dbref
79      */
80     DBRefEntry ref2 = new DBRefEntry("uniprot", "1", "v71633");
81     assertTrue(ref1.updateFrom(ref2));
82     assertEquals("UNIPROT", ref1.getSource()); // unchanged
83     assertEquals("V71633", ref1.getAccessionId()); // unchanged
84
85     /*
86      * ref1 has no mapping, acquires mapping from ref2
87      */
88     Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
89         1, 1 }, 3, 1));
90     ref2.setMap(map);
91     assertTrue(ref1.updateFrom(ref2));
92     assertSame(map, ref1.getMap()); // null mapping updated
93
94     /*
95      * ref1 has a mapping, does not acquire mapping from ref2
96      */
97     ref2.setMap(new Mapping(map));
98     assertTrue(ref1.updateFrom(ref2));
99     assertSame(map, ref1.getMap()); // non-null mapping not updated
100
101     /*
102      * ref2 has a different source, accession or version
103      */
104     ref2.setSource("pdb");
105     assertFalse(ref1.updateFrom(ref2));
106     ref2.setSource(ref1.getSource());
107     ref2.setAccessionId("P12345");
108     assertFalse(ref1.updateFrom(ref2));
109     ref2.setAccessionId(ref1.getAccessionId());
110     ref1.setVersion("2");
111     assertFalse(ref1.updateFrom(ref2));
112
113     /*
114      * a non-null version supersedes "0" or "source:0"
115      */
116     ref2.setVersion(null);
117     assertFalse(ref1.updateFrom(ref2));
118     assertEquals("2", ref1.getVersion());
119     ref2.setVersion("3");
120     ref1.setVersion("0");
121     assertTrue(ref1.updateFrom(ref2));
122     assertEquals("3", ref1.getVersion());
123     ref1.setVersion("UNIPROT:0");
124     assertTrue(ref1.updateFrom(ref2));
125     assertEquals("3", ref1.getVersion());
126
127     /*
128      * version "source:n" with n>0 is not superseded
129      */
130     ref1.setVersion("UNIPROT:1");
131     assertFalse(ref1.updateFrom(ref2));
132     assertEquals("UNIPROT:1", ref1.getVersion());
133
134     /*
135      * version "10" is not superseded
136      */
137     ref1.setVersion("10");
138     assertFalse(ref1.updateFrom(ref2));
139     assertEquals("10", ref1.getVersion());
140   }
141
142   @Test(groups = { "Functional" })
143   public void testIsPrimaryCandidate()
144   {
145     DBRefEntry dbr = new DBRefEntry(DBRefSource.UNIPROT, "", "Q12345");
146     assertTrue(dbr.isPrimaryCandidate());
147
148     /*
149      *  1:1 mapping - ok
150      */
151     dbr.setMap(new Mapping(null, new int[] { 1, 3 }, new int[] { 1, 3 }, 1,
152             1));
153     assertTrue(dbr.isPrimaryCandidate());
154
155     /*
156      *  1:1 mapping of identical split ranges - not ok
157      */
158     dbr.setMap(new Mapping(null, new int[] { 1, 3, 6, 9 }, new int[] { 1,
159         3, 6, 9 }, 1, 1));
160     assertFalse(dbr.isPrimaryCandidate());
161
162     /*
163      *  1:1 mapping of different ranges - not ok
164      */
165     dbr.setMap(new Mapping(null, new int[] { 1, 4 }, new int[] { 2, 5 }, 1,
166             1));
167     assertFalse(dbr.isPrimaryCandidate());
168
169     /*
170      *  1:1 mapping of 'isoform' ranges - not ok
171      */
172     dbr.setMap(new Mapping(null, new int[] { 1, 2, 6, 9 }, new int[] { 1,
173         3, 7, 9 }, 1, 1));
174     assertFalse(dbr.isPrimaryCandidate());
175     dbr.setMap(null);
176     assertTrue(dbr.isPrimaryCandidate());
177
178     /*
179      * Version string is prefixed with another dbref source string (fail)
180      */
181     dbr.setVersion(DBRefSource.EMBL + ":0");
182     assertFalse(dbr.isPrimaryCandidate());
183
184     /*
185      * Version string is alphanumeric
186      */
187     dbr.setVersion("0.1.b");
188     assertTrue(dbr.isPrimaryCandidate());
189
190     /*
191      * null version string can't be primary ref
192      */
193     dbr.setVersion(null);
194     assertFalse(dbr.isPrimaryCandidate());
195     dbr.setVersion("");
196     assertTrue(dbr.isPrimaryCandidate());
197
198     /*
199      *  1:1 mapping and sequenceRef (fail)
200      */
201     dbr.setMap(new Mapping(new Sequence("foo", "ASDF"), new int[] { 1, 3 },
202             new int[] { 1, 3 }, 1, 1));
203     assertFalse(dbr.isPrimaryCandidate());
204
205     /*
206      * 1:3 mapping (fail)
207      */
208     dbr.setMap(new Mapping(null, new int[] { 1, 3 }, new int[] { 1, 3 }, 1,
209             3));
210     assertFalse(dbr.isPrimaryCandidate());
211
212     /*
213      * 2:2 mapping with shift (expected fail, but maybe use case for a pass)
214      */
215     dbr.setMap(new Mapping(null, new int[] { 1, 4 }, new int[] { 1, 4 }, 2,
216             2));
217     assertFalse(dbr.isPrimaryCandidate());
218   }
219 }