JAL-2906 apply GPLv3 license source
[jalview.git] / test / jalview / structure / StructureMappingTest.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.structure;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertTrue;
26
27 import jalview.datamodel.Mapping;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceI;
30 import jalview.util.MapList;
31
32 import java.util.HashMap;
33 import java.util.List;
34
35 import org.testng.annotations.Test;
36
37 public class StructureMappingTest
38 {
39   @Test(groups = "Functional")
40   public void testGetPDBResNumRanges()
41   {
42     HashMap<Integer, int[]> map = new HashMap<>();
43
44     StructureMapping mapping = new StructureMapping(null, null, null, null,
45             map, null);
46
47     List<int[]> ranges = mapping.getPDBResNumRanges(1, 2);
48     assertTrue(ranges.isEmpty());
49
50     map.put(1, new int[] { 12, 20 }); // 1 maps to 12
51     ranges = mapping.getPDBResNumRanges(2, 3);
52     assertTrue(ranges.isEmpty());
53     ranges = mapping.getPDBResNumRanges(1, 2);
54     assertEquals(ranges.size(), 1);
55     assertEquals(ranges.get(0)[0], 12);
56     assertEquals(ranges.get(0)[1], 12);
57
58     map.put(2, new int[] { 13, 20 }); // 2 maps to 13
59     ranges = mapping.getPDBResNumRanges(1, 2);
60     assertEquals(ranges.size(), 1);
61     assertEquals(ranges.get(0)[0], 12);
62     assertEquals(ranges.get(0)[1], 13);
63
64     map.put(3, new int[] { 15, 20 }); // 3 maps to 15 - break
65     ranges = mapping.getPDBResNumRanges(1, 5);
66     assertEquals(ranges.size(), 2);
67     assertEquals(ranges.get(0)[0], 12);
68     assertEquals(ranges.get(0)[1], 13);
69     assertEquals(ranges.get(1)[0], 15);
70     assertEquals(ranges.get(1)[1], 15);
71   }
72
73   @Test(groups = "Functional")
74   public void testEquals()
75   {
76     SequenceI seq1 = new Sequence("seq1", "ABCDE");
77     SequenceI seq2 = new Sequence("seq1", "ABCDE");
78     String pdbFile = "a/b/file1.pdb";
79     String pdbId = "1a70";
80     String chain = "A";
81     String mappingDetails = "these are the mapping details, honest";
82     HashMap<Integer, int[]> map = new HashMap<>();
83
84     Mapping seqToPdbMapping = new Mapping(seq1,
85             new MapList(new int[]
86             { 1, 5 }, new int[] { 2, 6 }, 1, 1));
87     StructureMapping sm1 = new StructureMapping(seq1, pdbFile, pdbId, chain,
88             map, mappingDetails, seqToPdbMapping);
89     assertFalse(sm1.equals(null));
90     assertFalse(sm1.equals("x"));
91
92     StructureMapping sm2 = new StructureMapping(seq1, pdbFile, pdbId, chain,
93             map, mappingDetails, seqToPdbMapping);
94     assertTrue(sm1.equals(sm2));
95     assertTrue(sm2.equals(sm1));
96     assertEquals(sm1.hashCode(), sm2.hashCode());
97
98     // with different sequence
99     sm2 = new StructureMapping(seq2, pdbFile, pdbId, chain, map,
100             mappingDetails, seqToPdbMapping);
101     assertFalse(sm1.equals(sm2));
102     assertFalse(sm2.equals(sm1));
103
104     // with different file
105     sm2 = new StructureMapping(seq1, "a/b/file2.pdb", pdbId, chain, map,
106             mappingDetails, seqToPdbMapping);
107     assertFalse(sm1.equals(sm2));
108     assertFalse(sm2.equals(sm1));
109
110     // with different pdbid (case sensitive)
111     sm2 = new StructureMapping(seq1, pdbFile, "1A70", chain, map,
112             mappingDetails, seqToPdbMapping);
113     assertFalse(sm1.equals(sm2));
114     assertFalse(sm2.equals(sm1));
115
116     // with different chain
117     sm2 = new StructureMapping(seq1, pdbFile, pdbId, "B", map,
118             mappingDetails, seqToPdbMapping);
119     assertFalse(sm1.equals(sm2));
120     assertFalse(sm2.equals(sm1));
121
122     // map is ignore for this test
123     sm2 = new StructureMapping(seq1, pdbFile, pdbId, chain, null,
124             mappingDetails, seqToPdbMapping);
125     assertTrue(sm1.equals(sm2));
126     assertTrue(sm2.equals(sm1));
127
128     // with different mapping details
129     sm2 = new StructureMapping(seq1, pdbFile, pdbId, chain, map,
130             "different details!", seqToPdbMapping);
131     assertFalse(sm1.equals(sm2));
132     assertFalse(sm2.equals(sm1));
133
134     // with different seq to pdb mapping
135     Mapping map2 = new Mapping(seq1,
136             new MapList(new int[]
137             { 1, 5 }, new int[] { 3, 7 }, 1, 1));
138     sm2 = new StructureMapping(seq1, pdbFile, pdbId, chain, map,
139             mappingDetails, map2);
140     assertFalse(sm1.equals(sm2));
141     assertFalse(sm2.equals(sm1));
142   }
143 }