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(GroupSet.makeGroups(paematrix, false,0.1f, false));
78 // remap - test the MappableContactMatrix.liftOver method
79 SequenceI newseq = new Sequence("Seq", "ASDQEASDQEASDQE");
80 Mapping sqmap = new Mapping(seq,
82 { 5, 8, 10, 10 }, new int[] { 5, 9 }, 1, 1));
83 assertTrue(paematrix instanceof MappableContactMatrixI);
85 MappableContactMatrixI remapped = ((MappableContactMatrixI) paematrix)
86 .liftOver(newseq, sqmap);
87 assertTrue(remapped instanceof PAEContactMatrix);
89 AlignmentAnnotation newaa = newseq.addContactList(remapped);
90 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(1)));
91 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(4)));
93 newseq.getContactListFor(newaa, -1 + newseq.findIndex(5)));
95 newseq.getContactListFor(newaa, -1 + newseq.findIndex(6)));
97 newseq.getContactListFor(newaa, -1 + newseq.findIndex(7)));
99 newseq.getContactListFor(newaa, -1 + newseq.findIndex(8)));
100 // no mapping for position 9
101 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(9)));
104 newseq.getContactListFor(newaa, -1 + newseq.findIndex(10)));
106 // verify MappedPositions includes discontinuity
107 int[] mappedCl = newseq.getContactListFor(newaa, 5)
108 .getMappedPositionsFor(0, 4);
109 assertEquals(4, mappedCl.length,
110 "getMappedPositionsFor doesn't support discontinuous mappings to contactList");
114 SequenceI alseq = newseq.getSubSequence(6, 10);
115 alseq.insertCharAt(2, 2, '-');
116 AlignmentI alForSeq = new Alignment(new SequenceI[] { alseq });
117 newaa = AlignmentUtils.addReferenceAnnotationTo(alForSeq, alseq, newaa,
119 // check for null on out of bounds
120 ContactListI alcl = alForSeq.getContactListFor(newaa, newaa.annotations.length);
121 assertNull(alcl,"Should've gotten null!");
122 // now check for mapping
123 alcl = alForSeq.getContactListFor(newaa, 1);
125 mappedCl = alcl.getMappedPositionsFor(0, 4);
126 assertNotNull(mappedCl);
127 assertEquals(4, mappedCl.length,
128 "getMappedPositionsFor doesn't support discontinuous mappings to contactList");
130 // remap2 - test with original matrix map from 1-5 remapped to 5-9
132 seq = new Sequence("Seq/1-5", "ASDQE");
133 paematrix = new PAEContactMatrix(seq, PAEdata);
134 assertTrue(paematrix instanceof MappableContactMatrixI);
135 aa = seq.addContactList(paematrix);
137 newseq = new Sequence("Seq", "ASDQEASDQEASDQE");
138 sqmap = new Mapping(seq,
139 new MapList(new int[]
140 { 5, 9 }, new int[] { 1, 5 }, 1, 1));
142 remapped = ((MappableContactMatrixI) paematrix).liftOver(newseq, sqmap);
143 assertTrue(remapped instanceof PAEContactMatrix);
145 newaa = newseq.addContactList(remapped);
146 verify_mapping(newseq, newaa);
148 // remap3 - remap2 but mapping sense in liftover is reversed
150 seq = new Sequence("Seq/1-5", "ASDQE");
151 paematrix = new PAEContactMatrix(seq, PAEdata);
152 assertTrue(paematrix instanceof MappableContactMatrixI);
153 aa = seq.addContactList(paematrix);
155 newseq = new Sequence("Seq", "ASDQEASDQEASDQE");
156 sqmap = new Mapping(newseq,
157 new MapList(new int[]
158 { 1, 5 }, new int[] { 5, 9 }, 1, 1));
160 remapped = ((MappableContactMatrixI) paematrix).liftOver(newseq, sqmap);
161 assertTrue(remapped instanceof PAEContactMatrix);
163 newaa = newseq.addContactList(remapped);
164 verify_mapping(newseq, newaa);
168 * checks that the PAE matrix is located at positions 1-9 in newseq, and
169 * columns are not truncated.
174 private void verify_mapping(SequenceI newseq, AlignmentAnnotation newaa)
176 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(1)));
177 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(4)));
179 newseq.getContactListFor(newaa, -1 + newseq.findIndex(5)));
181 newseq.getContactListFor(newaa, -1 + newseq.findIndex(6)));
183 newseq.getContactListFor(newaa, -1 + newseq.findIndex(7)));
185 newseq.getContactListFor(newaa, -1 + newseq.findIndex(8)));
187 newseq.getContactListFor(newaa, -1 + newseq.findIndex(9)));
188 // last column should be null this time
189 assertNull(newseq.getContactListFor(newaa, -1 + newseq.findIndex(10)));
191 verifyPAEmatrix(newseq, newaa, 4, 4, 8);
194 private void verifyPAEmatrix(SequenceI seq, AlignmentAnnotation aa,
195 int topl, int rowl, int rowr)
198 for (int f = rowl; f <= rowr; f++)
200 ContactListI clist = seq.getContactListFor(aa, f);
201 assertNotNull(clist, "No ContactListI for position " + (f));
202 assertEquals(clist.getContactAt(0), (double) f - topl + 1,
203 "for column " + f + " relative to " + topl);
204 mappedCl = clist.getMappedPositionsFor(0, 0);
205 assertNotNull(mappedCl);
206 assertEquals(mappedCl[0], mappedCl[1]);
207 assertEquals(mappedCl[0], seq.findIndex(seq.getStart() + topl));
208 assertEquals(clist.getContactAt(f - topl), 1d,
209 "for column and row " + f + " relative to " + topl);
214 * check mapping and resolution methods work
216 @Test(groups= {"Functional"})
217 public void testMappableContactMatrix() {
218 SequenceI newseq = new Sequence("Seq", "ASDQEASDQEASDQE");
219 MapList map = new MapList(new int[]
220 { 5, 9 }, new int[] { 1, 5 }, 1, 1);
221 AlignmentAnnotation aa = newseq
222 .addContactList(new PAEContactMatrix(newseq,map, PAEdata,null));
223 ContactListI clist = newseq.getContactListFor(aa, 4);
224 assertNotNull(clist);
225 clist = newseq.getContactListFor(aa, 3);
228 ContactMatrixI cm = newseq.getContactMatrixFor(aa);
229 MappableContactMatrixI mcm = (MappableContactMatrixI) cm;
230 int[] pos = mcm.getMappedPositionsFor(newseq, 0);
233 pos = mcm.getMappedPositionsFor(newseq, 1);
235 assertEquals(pos[0],4+newseq.getStart());
237 pos = mcm.getMappedPositionsFor(newseq, 6); // after end of matrix
239 pos = mcm.getMappedPositionsFor(newseq, 5); // at end of matrix
241 assertEquals(pos[0],8+newseq.getStart());
242 SequenceI alseq = newseq.deriveSequence();
243 alseq.insertCharAt(5,'-');
244 pos = mcm.getMappedPositionsFor(alseq, 5); // at end of matrix
246 assertEquals(pos[0],8+newseq.getStart());