1 package jalview.datamodel;
3 import static org.testng.Assert.*;
5 import org.testng.Assert;
6 import org.testng.annotations.BeforeClass;
7 import org.testng.annotations.Test;
9 import jalview.analysis.AlignmentUtils;
10 import jalview.analysis.SeqsetUtils;
11 import jalview.gui.JvOptionPane;
12 import jalview.util.MapList;
13 import jalview.ws.datamodel.MappableContactMatrixI;
14 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
16 public class PAEContactMatrixTest
18 @BeforeClass(alwaysRun = true)
19 public void setUpJvOptionPane()
21 JvOptionPane.setInteractiveMode(false);
22 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
25 static float[][] PAEdata = { { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f },
26 { 2.0f, 1.0f, 2.0f, 3.0f, 4.0f },
27 { 3.0f, 2.0f, 1.0f, 2.0f, 3.0f },
28 { 4.0f, 3.0f, 2.0f, 1.0f, 2.0f },
29 { 5.0f, 4.0f, 3.0f, 2.0f, 1.0f } };
32 * test associations for a PAE matrix
34 @Test(groups = { "Functional" })
35 public void testSeqAssociatedPAEMatrix()
37 Sequence seq = new Sequence("Seq", "ASDQE");
38 AlignmentAnnotation aa = seq
39 .addContactList(new PAEContactMatrix(seq, PAEdata));
40 assertNotNull(seq.getContactListFor(aa, 0));
41 assertEquals(seq.getContactListFor(aa, 0).getContactAt(0), 1.0);
42 assertNotNull(seq.getContactListFor(aa, 1));
43 assertEquals(seq.getContactListFor(aa, 1).getContactAt(1), 1.0);
44 assertNotNull(seq.getContactListFor(aa, 2));
45 assertEquals(seq.getContactListFor(aa, 2).getContactAt(2), 1.0);
46 assertNotNull(seq.getContactListFor(aa, 3));
47 assertEquals(seq.getContactListFor(aa, 3).getContactAt(3), 1.0);
48 assertNotNull(seq.getContactListFor(aa, 4));
49 assertEquals(seq.getContactListFor(aa, 4).getContactAt(4), 1.0);
51 assertNotNull(seq.getContactListFor(aa, seq.getEnd() - 1));
52 assertNull(seq.getContactListFor(aa, seq.getEnd()));
54 ContactListI cm = seq.getContactListFor(aa, seq.getStart());
55 assertEquals(cm.getContactAt(seq.getStart()), 1d);
56 verifyPAEmatrix(seq, aa, 0, 0, 4);
58 // Now associated with sequence not starting at 1
59 seq = new Sequence("Seq/5-9", "ASDQE");
60 ContactMatrixI paematrix = new PAEContactMatrix(seq, PAEdata);
61 aa = seq.addContactList(paematrix);
63 // individual annotation elements need to be distinct for Matrix associated
65 Annotation ae5 = aa.getAnnotationForPosition(5);
66 Annotation ae6 = aa.getAnnotationForPosition(6);
69 assertTrue(ae5 != ae6);
71 cm = seq.getContactListFor(aa, 0);
72 assertEquals(cm.getContactAt(0), 1d);
73 verifyPAEmatrix(seq, aa, 0, 0, 4);
76 paematrix.setGroupSet(
77 GroupSet.makeGroups(paematrix, false, 0.1f, false));
79 // remap - test the MappableContactMatrix.liftOver method
80 SequenceI newseq = new Sequence("Seq", "ASDQEASDQEASDQE");
81 Mapping sqmap = new Mapping(seq,
83 { 5, 8, 10, 10 }, new int[] { 5, 9 }, 1, 1));
84 assertTrue(paematrix instanceof MappableContactMatrixI);
86 MappableContactMatrixI remapped = ((MappableContactMatrixI) paematrix)
87 .liftOver(newseq, sqmap);
88 assertTrue(remapped instanceof PAEContactMatrix);
90 AlignmentAnnotation newaa = newseq.addContactList(remapped);
91 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(1)));
92 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(4)));
94 newseq.getContactListFor(newaa, -1 + newseq.findIndex(5)));
96 newseq.getContactListFor(newaa, -1 + newseq.findIndex(6)));
98 newseq.getContactListFor(newaa, -1 + newseq.findIndex(7)));
100 newseq.getContactListFor(newaa, -1 + newseq.findIndex(8)));
101 // no mapping for position 9
102 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(9)));
105 newseq.getContactListFor(newaa, -1 + newseq.findIndex(10)));
107 // verify MappedPositions includes discontinuity
108 int[] mappedCl = newseq.getContactListFor(newaa, 5)
109 .getMappedPositionsFor(0, 4);
110 assertEquals(4, mappedCl.length,
111 "getMappedPositionsFor doesn't support discontinuous mappings to contactList");
115 SequenceI alseq = newseq.getSubSequence(6, 10);
116 alseq.insertCharAt(2, 2, '-');
117 AlignmentI alForSeq = new Alignment(new SequenceI[] { alseq });
118 newaa = AlignmentUtils.addReferenceAnnotationTo(alForSeq, alseq, newaa,
120 // check for null on out of bounds
121 ContactListI alcl = alForSeq.getContactListFor(newaa,
122 newaa.annotations.length);
123 assertNull(alcl, "Should've gotten null!");
124 // now check for mapping
125 alcl = alForSeq.getContactListFor(newaa, 1);
127 mappedCl = alcl.getMappedPositionsFor(0, 4);
128 assertNotNull(mappedCl);
129 assertEquals(4, mappedCl.length,
130 "getMappedPositionsFor doesn't support discontinuous mappings to contactList");
132 // remap2 - test with original matrix map from 1-5 remapped to 5-9
134 seq = new Sequence("Seq/1-5", "ASDQE");
135 paematrix = new PAEContactMatrix(seq, PAEdata);
136 assertTrue(paematrix instanceof MappableContactMatrixI);
137 aa = seq.addContactList(paematrix);
139 newseq = new Sequence("Seq", "ASDQEASDQEASDQE");
140 sqmap = new Mapping(seq,
141 new MapList(new int[]
142 { 5, 9 }, new int[] { 1, 5 }, 1, 1));
144 remapped = ((MappableContactMatrixI) paematrix).liftOver(newseq, sqmap);
145 assertTrue(remapped instanceof PAEContactMatrix);
147 newaa = newseq.addContactList(remapped);
148 verify_mapping(newseq, newaa);
150 // remap3 - remap2 but mapping sense in liftover is reversed
152 seq = new Sequence("Seq/1-5", "ASDQE");
153 paematrix = new PAEContactMatrix(seq, PAEdata);
154 assertTrue(paematrix instanceof MappableContactMatrixI);
155 aa = seq.addContactList(paematrix);
157 newseq = new Sequence("Seq", "ASDQEASDQEASDQE");
158 sqmap = new Mapping(newseq,
159 new MapList(new int[]
160 { 1, 5 }, new int[] { 5, 9 }, 1, 1));
162 remapped = ((MappableContactMatrixI) paematrix).liftOver(newseq, sqmap);
163 assertTrue(remapped instanceof PAEContactMatrix);
165 newaa = newseq.addContactList(remapped);
166 verify_mapping(newseq, newaa);
170 * checks that the PAE matrix is located at positions 1-9 in newseq, and
171 * columns are not truncated.
176 private void verify_mapping(SequenceI newseq, AlignmentAnnotation newaa)
178 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(1)));
179 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(4)));
181 newseq.getContactListFor(newaa, -1 + newseq.findIndex(5)));
183 newseq.getContactListFor(newaa, -1 + newseq.findIndex(6)));
185 newseq.getContactListFor(newaa, -1 + newseq.findIndex(7)));
187 newseq.getContactListFor(newaa, -1 + newseq.findIndex(8)));
189 newseq.getContactListFor(newaa, -1 + newseq.findIndex(9)));
190 // last column should be null this time
191 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(10)));
193 verifyPAEmatrix(newseq, newaa, 4, 4, 8);
196 private void verifyPAEmatrix(SequenceI seq, AlignmentAnnotation aa,
197 int topl, int rowl, int rowr)
200 for (int f = rowl; f <= rowr; f++)
202 ContactListI clist = seq.getContactListFor(aa, f);
203 assertNotNull(clist, "No ContactListI for position " + (f));
204 assertEquals(clist.getContactAt(0), (double) f - topl + 1,
205 "for column " + f + " relative to " + topl);
206 mappedCl = clist.getMappedPositionsFor(0, 0);
207 assertNotNull(mappedCl);
208 assertEquals(mappedCl[0], mappedCl[1]);
209 assertEquals(mappedCl[0], seq.findIndex(seq.getStart() + topl));
210 assertEquals(clist.getContactAt(f - topl), 1d,
211 "for column and row " + f + " relative to " + topl);
216 * check mapping and resolution methods work
218 @Test(groups = { "Functional" })
219 public void testMappableContactMatrix()
221 SequenceI newseq = new Sequence("Seq", "ASDQEASDQEASDQE");
222 MapList map = new MapList(new int[] { 5, 9 }, new int[] { 1, 5 }, 1, 1);
223 AlignmentAnnotation aa = newseq.addContactList(
224 new PAEContactMatrix(newseq, map, PAEdata, null));
225 ContactListI clist = newseq.getContactListFor(aa, 4);
226 assertNotNull(clist);
227 clist = newseq.getContactListFor(aa, 3);
230 ContactMatrixI cm = newseq.getContactMatrixFor(aa);
231 MappableContactMatrixI mcm = (MappableContactMatrixI) cm;
232 int[] pos = mcm.getMappedPositionsFor(newseq, 0);
235 pos = mcm.getMappedPositionsFor(newseq, 1);
237 assertEquals(pos[0], 4 + newseq.getStart());
239 pos = mcm.getMappedPositionsFor(newseq, 6); // after end of matrix
241 pos = mcm.getMappedPositionsFor(newseq, 5); // at end of matrix
243 assertEquals(pos[0], 8 + newseq.getStart());
244 SequenceI alseq = newseq.deriveSequence();
245 alseq.insertCharAt(5, '-');
246 pos = mcm.getMappedPositionsFor(alseq, 5); // at end of matrix
248 assertEquals(pos[0], 8 + newseq.getStart());