09d9df1f0c2661d24c398507254d1a2ee3b947eb
[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 testIsPrimary()
144   {
145     DBRefEntry dbr = new DBRefEntry(DBRefSource.UNIPROT, "", "Q12345");
146     assertTrue(dbr.isPrimary());
147     /*
148      *  1:1 mapping 
149      */
150     dbr.setMap(new Mapping(null, new int[] { 1, 3 }, new int[] { 1, 3 }, 1,
151             1));
152     assertTrue(dbr.isPrimary());
153     /*
154      * Version string is prefixed with another dbref source string (fail)
155      */
156     dbr.setVersion(DBRefSource.EMBL + ":0");
157     assertFalse(dbr.isPrimary());
158
159     /*
160      * Version string is alphanumeric
161      */
162     dbr.setVersion("0.1.b");
163     assertTrue(dbr.isPrimary());
164
165     /*
166      *  1:1 mapping with shift (fail)
167      */
168     dbr.setMap(new Mapping(null, new int[] { 1, 3 }, new int[] { 2, 4 }, 1,
169             1));
170     assertFalse(dbr.isPrimary());
171
172     /*
173      *  1:1 mapping and sequenceRef (fail)
174      */
175     dbr.setMap(new Mapping(new Sequence("foo", "ASDF"), new int[] { 1, 3 },
176             new int[] { 1, 3 }, 1, 1));
177     assertFalse(dbr.isPrimary());
178
179     /*
180      * 1:3 mapping (fail)
181      */
182     dbr.setMap(new Mapping(null, new int[] { 1, 3 }, new int[] { 1, 3 }, 1,
183             3));
184     assertFalse(dbr.isPrimary());
185     /*
186      * 2:2 mapping with shift (expected fail, but maybe use case for a pass)
187      */
188     dbr.setMap(new Mapping(null, new int[] { 1, 3 }, new int[] { 1, 3 }, 2,
189             2));
190     assertFalse(dbr.isPrimary());
191
192     /*
193      * Version string is prefixed with another dbref source string
194      */
195     dbr.setVersion(DBRefSource.EMBL + ":0");
196     assertFalse(dbr.isPrimary());
197
198   }
199 }