2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.renderer;
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNotEquals;
26 import static org.testng.Assert.assertNotNull;
27 import static org.testng.Assert.assertTrue;
29 import org.testng.annotations.Test;
31 import jalview.datamodel.AlignmentAnnotation;
32 import jalview.datamodel.ColumnSelection;
33 import jalview.datamodel.ContactListI;
34 import jalview.datamodel.ContactRange;
35 import jalview.datamodel.Mapping;
36 import jalview.datamodel.SeqDistanceContactMatrix;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceI;
39 import jalview.util.MapList;
40 import jalview.util.StringUtils;
41 import jalview.ws.datamodel.MappableContactMatrixI;
43 public class ContactGeometryTest
45 @Test(groups = "Functional")
46 public void testCoverageofRange()
48 // a really dumb test to make sure we really cover the requested pixel and
49 // contactList range for any dimension of each
50 for (int range = 12; range < 2000; range += 35)
52 StringBuilder sb = new StringBuilder();
53 while (sb.length() < range)
57 SequenceI sq = new Sequence("a", sb.toString());
58 MappableContactMatrixI cm = new SeqDistanceContactMatrix(range);
59 AlignmentAnnotation cm_aan = sq.addContactList(cm);
60 ContactListI cl = sq.getContactListFor(cm_aan, 10);
62 for (int ht = range / 2; ht < range * 3; ht++)
64 ContactGeometry clgeom = new ContactGeometry(cl, ht);
65 assertNotNull(clgeom.allSteps());
70 @Test(groups = "Functional")
71 public void testContactGeometry()
73 SequenceI sq = new Sequence("a", "SSSQ");
74 MappableContactMatrixI cm = new SeqDistanceContactMatrix(4);
75 AlignmentAnnotation cm_aan = sq.addContactList(cm);
76 checkConsistencyFor(sq, cm_aan);
77 // Also check all is good when there's a sequence mapping involved
78 MappableContactMatrixI newcm = cm.liftOver(sq,
79 new Mapping(sq, new MapList(new int[]
80 { 1, 4 }, new int[] { 1, 4 }, 1, 1)));
81 AlignmentAnnotation mapped_cm = sq.addContactList(newcm);
82 checkConsistencyFor(sq, mapped_cm);
85 // Do some asserts for a sequence and a contact matrix
86 private void checkConsistencyFor(SequenceI sq, AlignmentAnnotation cm_aan)
89 ContactListI cl = sq.getContactListFor(cm_aan, col);
91 assertEquals(cl.getContactHeight(), 4);
93 // Map contacts 0 to 3 to a tiny range and check
94 ContactGeometry testee = new ContactGeometry(cl, 2);
95 assertEquals(testee.contacts_per_pixel, 2d);
96 ContactGeometry.contactInterval lastInterval = testee.mapFor(1);
97 assertEquals(lastInterval.cStart, 2);
98 assertEquals(lastInterval.cEnd, 3);
99 assertEquals(lastInterval.pStart, 1);
100 assertEquals(lastInterval.pEnd, 1);
101 ContactGeometry.contactInterval another = testee.mapFor(1, 2);
102 assertEquals(lastInterval, another);
103 // Also check for a big pixel range
104 testee = new ContactGeometry(cl, 395);
105 lastInterval = testee.mapFor(390, 395); // 395 is one over limit.
106 assertNotNull(lastInterval);
107 assertEquals(lastInterval.cEnd, 3);
108 assertEquals(lastInterval.pEnd, 394);
109 // Map contacts 0 to 3 to Pixels 0-9, 10-19, 20-29, 30-39
110 testee = new ContactGeometry(cl, 40);
112 // verify mapping from pixel to contacts
114 // renderer thinks base 0 for pixel coordinates
115 // contact coordinates are base 1
116 for (int p = 0; p < 40; p++)
118 int expectC = (p / 10);
119 int expectP = (expectC) * 10;
120 ContactGeometry.contactInterval ci_at = testee.mapFor(p),
121 ci_from = testee.mapFor(p, p);
122 assertNotNull(ci_at);
123 // mapFor and map should locate the same pixel window
124 assertEquals(ci_at.cStart, expectC,
125 "Different cStart at position " + p);
126 assertEquals(ci_at.cEnd, expectC, "Different cEnd at position " + p);
127 assertEquals(ci_at.pStart, expectP,
128 "Different pStart at position " + p);
129 assertEquals(ci_at.pEnd, expectP + 9,
130 "Different pEnd at position " + p);
132 assertEquals(ci_from, ci_at,
133 "Different contactIntervals at position " + p);
134 // also test getRangeFor
135 ContactRange cr = cl.getRangeFor(ci_at.cStart, ci_at.cEnd);
136 assertEquals(cr.getFrom_column(), cr.getTo_column());
137 assertEquals((double) cr.getMean(),
138 (double) Math.abs(col - cr.getFrom_column()),
139 "Didn't resolve expected value at position " + p);
142 ContactGeometry.contactInterval ci_at0 = testee.mapFor(0);
143 ContactGeometry.contactInterval ci_at9 = testee.mapFor(9);
144 assertNotNull(ci_at9);
146 assertEquals(ci_at0, ci_at9);
149 ContactGeometry.contactInterval ci_at10 = testee.mapFor(10);
150 assertNotNull(ci_at10);
151 ContactGeometry.contactInterval ci_at11 = testee.mapFor(11);
152 assertNotNull(ci_at11);
154 assertEquals(ci_at11, ci_at10,
155 "Off-by-one in ContactGeometry mapping.");
157 assertNotEquals(ci_at0, ci_at10,
158 "Expected adjacent cells to be not equal.");
160 // verify adjacent window is mapped
161 assertEquals(ci_at11.cStart, ci_at9.cStart + 1);
163 assertEquals(ci_at9.cEnd + 1, ci_at11.cStart);
164 assertEquals(ci_at9.cEnd + 1, ci_at11.cEnd);
166 // verify interval/intersection
167 // column selection is base 0
168 ColumnSelection cs = new ColumnSelection();
171 boolean mask = false;
174 assertFalse(testee.intersects(ci_at0, cs, null, mask));
175 assertFalse(testee.intersects(ci_at11, cs, null, mask));
176 assertTrue(testee.intersects(testee.mapFor(21), cs, null, mask));
177 assertFalse(testee.intersects(testee.mapFor(31), cs, null, mask));
179 assertTrue(testee.intersects(testee.mapFor(31), cs, null, mask));
181 assertFalse(testee.intersects(testee.mapFor(21), cs, null, mask));