3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertSame;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7 import jalview.api.AlignViewportI;
8 import jalview.datamodel.AlignedCodonFrame;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.ColumnSelection;
12 import jalview.datamodel.SearchResults;
13 import jalview.datamodel.SearchResults.Match;
14 import jalview.datamodel.Sequence;
15 import jalview.datamodel.SequenceGroup;
16 import jalview.gui.AlignViewport;
17 import jalview.io.AppletFormatAdapter;
18 import jalview.io.FormatAdapter;
20 import java.awt.Color;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.Collections;
26 import org.junit.Test;
28 public class MappingUtilsTest
30 private AlignViewportI dnaView;
31 private AlignViewportI proteinView;
34 * Simple test of mapping with no intron involved.
37 public void testBuildSearchResults()
39 final Sequence seq1 = new Sequence("Seq1", "C-G-TA-GC");
40 seq1.createDatasetSequence();
42 final Sequence aseq1 = new Sequence("Seq1", "-P-R");
43 aseq1.createDatasetSequence();
46 * Map dna bases 1-6 to protein residues 1-2
48 AlignedCodonFrame acf = new AlignedCodonFrame();
49 MapList map = new MapList(new int[]
52 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
53 Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
56 * Check protein residue 1 maps to codon 1-3, 2 to codon 4-6
58 SearchResults sr = MappingUtils.buildSearchResults(aseq1, 1, acfList);
59 assertEquals(1, sr.getResults().size());
60 Match m = sr.getResults().get(0);
61 assertEquals(seq1.getDatasetSequence(), m.getSequence());
62 assertEquals(1, m.getStart());
63 assertEquals(3, m.getEnd());
64 sr = MappingUtils.buildSearchResults(aseq1, 2, acfList);
65 assertEquals(1, sr.getResults().size());
66 m = sr.getResults().get(0);
67 assertEquals(seq1.getDatasetSequence(), m.getSequence());
68 assertEquals(4, m.getStart());
69 assertEquals(6, m.getEnd());
72 * Check inverse mappings, from codons 1-3, 4-6 to protein 1, 2
74 for (int i = 1; i < 7; i++)
76 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
77 assertEquals(1, sr.getResults().size());
78 m = sr.getResults().get(0);
79 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
80 int residue = i > 3 ? 2 : 1;
81 assertEquals(residue, m.getStart());
82 assertEquals(residue, m.getEnd());
87 * Simple test of mapping with introns involved.
90 public void testBuildSearchResults_withIntro()
92 final Sequence seq1 = new Sequence("Seq1", "C-G-TAGA-GCAGCTT");
93 seq1.createDatasetSequence();
95 final Sequence aseq1 = new Sequence("Seq1", "-P-R");
96 aseq1.createDatasetSequence();
99 * Map dna bases [2, 4, 5], [7, 9, 11] to protein residues 1 and 2
101 AlignedCodonFrame acf = new AlignedCodonFrame();
102 MapList map = new MapList(new int[]
103 { 2, 2, 4, 5, 7, 7, 9, 9, 11, 11 }, new int[]
105 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
106 Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
109 * Check protein residue 1 maps to [2, 4, 5]
111 SearchResults sr = MappingUtils.buildSearchResults(aseq1, 1, acfList);
112 assertEquals(2, sr.getResults().size());
113 Match m = sr.getResults().get(0);
114 assertEquals(seq1.getDatasetSequence(), m.getSequence());
115 assertEquals(2, m.getStart());
116 assertEquals(2, m.getEnd());
117 m = sr.getResults().get(1);
118 assertEquals(seq1.getDatasetSequence(), m.getSequence());
119 assertEquals(4, m.getStart());
120 assertEquals(5, m.getEnd());
123 * Check protein residue 2 maps to [7, 9, 11]
125 sr = MappingUtils.buildSearchResults(aseq1, 2, acfList);
126 assertEquals(3, sr.getResults().size());
127 m = sr.getResults().get(0);
128 assertEquals(seq1.getDatasetSequence(), m.getSequence());
129 assertEquals(7, m.getStart());
130 assertEquals(7, m.getEnd());
131 m = sr.getResults().get(1);
132 assertEquals(seq1.getDatasetSequence(), m.getSequence());
133 assertEquals(9, m.getStart());
134 assertEquals(9, m.getEnd());
135 m = sr.getResults().get(2);
136 assertEquals(seq1.getDatasetSequence(), m.getSequence());
137 assertEquals(11, m.getStart());
138 assertEquals(11, m.getEnd());
141 * Check inverse mappings, from codons to protein
143 for (int i = 1; i < 14; i++)
145 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
146 int residue = (i == 2 || i == 4 || i == 5) ? 1 : (i == 7 || i == 9
150 assertEquals(0, sr.getResults().size());
153 assertEquals(1, sr.getResults().size());
154 m = sr.getResults().get(0);
155 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
156 assertEquals(residue, m.getStart());
157 assertEquals(residue, m.getEnd());
162 * Test mapping a sequence group.
164 * @throws IOException
167 public void testMapSequenceGroup() throws IOException
170 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
173 AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
175 cdna.setDataset(null);
176 AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
178 protein.setDataset(null);
179 AlignedCodonFrame acf = new AlignedCodonFrame();
180 MapList map = new MapList(new int[]
183 for (int seq = 0; seq < 3; seq++)
185 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
186 .getSequenceAt(seq).getDatasetSequence(), map);
188 Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
190 AlignViewportI dnaView = new AlignViewport(cdna);
191 AlignViewportI proteinView = new AlignViewport(protein);
192 protein.setCodonFrames(acfList);
195 * Select Seq1 and Seq3 in the protein
197 SequenceGroup sg = new SequenceGroup();
198 sg.setColourText(true);
199 sg.setIdColour(Color.GREEN);
200 sg.setOutlineColour(Color.LIGHT_GRAY);
201 sg.addSequence(protein.getSequenceAt(0), false);
202 sg.addSequence(protein.getSequenceAt(2), false);
205 * Verify the mapped sequence group in dna
207 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
208 assertTrue(mappedGroup.getColourText());
209 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
210 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
211 assertEquals(2, mappedGroup.getSequences().size());
212 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
213 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
216 * Verify mapping sequence group from dna to protein
219 sg.addSequence(cdna.getSequenceAt(1), false);
220 sg.addSequence(cdna.getSequenceAt(0), false);
221 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
222 assertTrue(mappedGroup.getColourText());
223 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
224 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
225 assertEquals(2, mappedGroup.getSequences().size());
226 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
227 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
231 * Helper method to load an alignment and ensure dataset sequences are set up.
237 * @throws IOException
239 protected AlignmentI loadAlignment(final String data, String format)
242 Alignment a = new FormatAdapter().readFile(data,
243 AppletFormatAdapter.PASTE, format);
249 * Test mapping a column selection in protein to its dna equivalent
251 * @throws IOException
254 public void testMapColumnSelection_proteinToDna() throws IOException
256 setupMappedAlignments();
258 ColumnSelection colsel = new ColumnSelection();
261 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
262 * in dna respectively, overall 0-4
264 colsel.addElement(0);
265 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel,
266 proteinView, dnaView);
267 assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
270 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
273 colsel.addElement(1);
274 cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
275 assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
278 * Column 2 in protein picks up gaps only - no mapping
281 colsel.addElement(2);
282 cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
283 assertEquals("[]", cs.getSelected().toString());
286 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
287 * 6-9, 6-10, 5-8 respectively, overall to 5-10
290 colsel.addElement(3);
291 cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
292 assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
295 * Combine selection of columns 1 and 3 to get a discontiguous mapped
299 colsel.addElement(1);
300 colsel.addElement(3);
301 cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
302 assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", cs.getSelected()
307 * @throws IOException
309 protected void setupMappedAlignments() throws IOException
312 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
313 * viewport). Lower case for introns.
315 AlignmentI cdna = loadAlignment(">Seq1\nAC-GctGtC-T\n"
316 + ">Seq2\nTc-GA-G-T-Tc\n" + ">Seq3\nTtTT-AaCGg-\n",
318 cdna.setDataset(null);
319 AlignmentI protein = loadAlignment(
320 ">Seq1\n-K-P\n>Seq2\nL--Q\n>Seq3\nG--S\n",
322 protein.setDataset(null);
323 AlignedCodonFrame acf = new AlignedCodonFrame();
324 MapList map = new MapList(new int[]
325 { 1, 3, 6, 6, 8, 9 }, new int[]
327 acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein
328 .getSequenceAt(0).getDatasetSequence(), map);
329 map = new MapList(new int[]
330 { 1, 1, 3, 4, 5, 7 }, new int[]
332 acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein
333 .getSequenceAt(1).getDatasetSequence(), map);
334 map = new MapList(new int[]
335 { 1, 1, 3, 4, 5, 5, 7, 8 }, new int[]
337 acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
338 .getSequenceAt(2).getDatasetSequence(), map);
339 Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
341 dnaView = new AlignViewport(cdna);
342 proteinView = new AlignViewport(protein);
343 protein.setCodonFrames(acfList);
347 * Test mapping a column selection in dna to its protein equivalent
349 * @throws IOException
352 public void testMapColumnSelection_dnaToProtein() throws IOException
354 setupMappedAlignments();
356 ColumnSelection colsel = new ColumnSelection();
359 * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
362 colsel.addElement(0);
363 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, dnaView,
365 assertEquals("[0, 1]", cs.getSelected().toString());
368 * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
369 * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
371 colsel.addElement(3);
372 colsel.addElement(4);
373 colsel.addElement(5);
374 cs = MappingUtils.mapColumnSelection(colsel, dnaView, proteinView);
375 assertEquals("[0, 1, 3]", cs.getSelected().toString());
379 * Tests for the method that converts a series of [start, end] ranges to
383 public void testFlattenRanges()
385 assertEquals("[1, 2, 3, 4]",
386 Arrays.toString(MappingUtils.flattenRanges(new int[]
388 assertEquals("[1, 2, 3, 4]",
389 Arrays.toString(MappingUtils.flattenRanges(new int[]
391 assertEquals("[1, 2, 3, 4]",
392 Arrays.toString(MappingUtils.flattenRanges(new int[]
393 { 1, 1, 2, 2, 3, 3, 4, 4 })));
394 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
395 Arrays.toString(MappingUtils.flattenRanges(new int[]
396 { 1, 4, 7, 9, 12, 12 })));
397 // unpaired start position is ignored:
398 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
399 Arrays.toString(MappingUtils.flattenRanges(new int[]
400 { 1, 4, 7, 9, 12, 12, 15 })));