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.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
30 import java.awt.Color;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Iterator;
35 import java.util.List;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
40 import jalview.api.AlignViewportI;
41 import jalview.bin.Console;
42 import jalview.commands.EditCommand;
43 import jalview.commands.EditCommand.Action;
44 import jalview.commands.EditCommand.Edit;
45 import jalview.datamodel.AlignedCodonFrame;
46 import jalview.datamodel.Alignment;
47 import jalview.datamodel.AlignmentI;
48 import jalview.datamodel.ColumnSelection;
49 import jalview.datamodel.HiddenColumns;
50 import jalview.datamodel.SearchResultMatchI;
51 import jalview.datamodel.SearchResultsI;
52 import jalview.datamodel.Sequence;
53 import jalview.datamodel.SequenceGroup;
54 import jalview.datamodel.SequenceI;
55 import jalview.gui.AlignViewport;
56 import jalview.gui.JvOptionPane;
57 import jalview.io.DataSourceType;
58 import jalview.io.FileFormat;
59 import jalview.io.FileFormatI;
60 import jalview.io.FormatAdapter;
63 public class MappingUtilsTest
65 @BeforeClass(alwaysRun = true)
71 @BeforeClass(alwaysRun = true)
72 public void setUpJvOptionPane()
74 JvOptionPane.setInteractiveMode(false);
75 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
78 private AlignViewportI dnaView;
80 private AlignViewportI proteinView;
83 * Simple test of mapping with no intron involved.
85 @Test(groups = { "Functional" })
86 public void testBuildSearchResults()
88 final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
89 seq1.createDatasetSequence();
91 final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
92 aseq1.createDatasetSequence();
95 * Map dna bases 5-10 to protein residues 12-13
97 AlignedCodonFrame acf = new AlignedCodonFrame();
98 MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
100 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
101 List<AlignedCodonFrame> acfList = Arrays
102 .asList(new AlignedCodonFrame[]
106 * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
108 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
109 assertEquals(1, sr.getResults().size());
110 SearchResultMatchI m = sr.getResults().get(0);
111 assertEquals(seq1.getDatasetSequence(), m.getSequence());
112 assertEquals(5, m.getStart());
113 assertEquals(7, m.getEnd());
114 sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
115 assertEquals(1, sr.getResults().size());
116 m = sr.getResults().get(0);
117 assertEquals(seq1.getDatasetSequence(), m.getSequence());
118 assertEquals(8, m.getStart());
119 assertEquals(10, m.getEnd());
122 * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
124 for (int i = 5; i < 11; i++)
126 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
127 assertEquals(1, sr.getResults().size());
128 m = sr.getResults().get(0);
129 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
130 int residue = i > 7 ? 13 : 12;
131 assertEquals(residue, m.getStart());
132 assertEquals(residue, m.getEnd());
137 * Simple test of mapping with introns involved.
139 @Test(groups = { "Functional" })
140 public void testBuildSearchResults_withIntron()
142 final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
143 seq1.createDatasetSequence();
145 final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
146 aseq1.createDatasetSequence();
149 * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
151 AlignedCodonFrame acf = new AlignedCodonFrame();
152 MapList map = new MapList(
154 { 6, 6, 8, 9, 11, 11, 13, 13, 15, 15 }, new int[] { 8, 9 }, 3,
156 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
157 List<AlignedCodonFrame> acfList = Arrays
158 .asList(new AlignedCodonFrame[]
162 * Check protein residue 8 maps to [6, 8, 9]
164 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
165 assertEquals(2, sr.getResults().size());
166 SearchResultMatchI m = sr.getResults().get(0);
167 assertEquals(seq1.getDatasetSequence(), m.getSequence());
168 assertEquals(6, m.getStart());
169 assertEquals(6, m.getEnd());
170 m = sr.getResults().get(1);
171 assertEquals(seq1.getDatasetSequence(), m.getSequence());
172 assertEquals(8, m.getStart());
173 assertEquals(9, m.getEnd());
176 * Check protein residue 9 maps to [11, 13, 15]
178 sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
179 assertEquals(3, sr.getResults().size());
180 m = sr.getResults().get(0);
181 assertEquals(seq1.getDatasetSequence(), m.getSequence());
182 assertEquals(11, m.getStart());
183 assertEquals(11, m.getEnd());
184 m = sr.getResults().get(1);
185 assertEquals(seq1.getDatasetSequence(), m.getSequence());
186 assertEquals(13, m.getStart());
187 assertEquals(13, m.getEnd());
188 m = sr.getResults().get(2);
189 assertEquals(seq1.getDatasetSequence(), m.getSequence());
190 assertEquals(15, m.getStart());
191 assertEquals(15, m.getEnd());
194 * Check inverse mappings, from codons to protein
196 for (int i = 5; i < 18; i++)
198 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
199 int residue = (i == 6 || i == 8 || i == 9) ? 8
200 : (i == 11 || i == 13 || i == 15 ? 9 : 0);
203 assertEquals(0, sr.getResults().size());
206 assertEquals(1, sr.getResults().size());
207 m = sr.getResults().get(0);
208 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
209 assertEquals(residue, m.getStart());
210 assertEquals(residue, m.getEnd());
215 * Test mapping a sequence group made of entire sequences.
217 * @throws IOException
219 @Test(groups = { "Functional" })
220 public void testMapSequenceGroup_sequences() throws IOException
223 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
226 AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
228 cdna.setDataset(null);
229 AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
231 protein.setDataset(null);
232 AlignedCodonFrame acf = new AlignedCodonFrame();
233 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
234 for (int seq = 0; seq < 3; seq++)
236 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
237 protein.getSequenceAt(seq).getDatasetSequence(), map);
239 List<AlignedCodonFrame> acfList = Arrays
240 .asList(new AlignedCodonFrame[]
243 AlignViewportI dnaView = new AlignViewport(cdna);
244 AlignViewportI proteinView = new AlignViewport(protein);
245 protein.setCodonFrames(acfList);
248 * Select Seq1 and Seq3 in the protein
250 SequenceGroup sg = new SequenceGroup();
251 sg.setColourText(true);
252 sg.setIdColour(Color.GREEN);
253 sg.setOutlineColour(Color.LIGHT_GRAY);
254 sg.addSequence(protein.getSequenceAt(0), false);
255 sg.addSequence(protein.getSequenceAt(2), false);
256 sg.setEndRes(protein.getWidth() - 1);
259 * Verify the mapped sequence group in dna
261 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
262 proteinView, dnaView);
263 assertTrue(mappedGroup.getColourText());
264 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
265 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
266 assertEquals(2, mappedGroup.getSequences().size());
267 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
268 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
269 assertEquals(0, mappedGroup.getStartRes());
270 assertEquals(2, mappedGroup.getEndRes()); // 3 columns (1 codon)
273 * Verify mapping sequence group from dna to protein
276 sg.addSequence(cdna.getSequenceAt(1), false);
277 sg.addSequence(cdna.getSequenceAt(0), false);
280 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
281 assertTrue(mappedGroup.getColourText());
282 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
283 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
284 assertEquals(2, mappedGroup.getSequences().size());
285 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
286 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
287 assertEquals(0, mappedGroup.getStartRes());
288 assertEquals(0, mappedGroup.getEndRes());
292 * Helper method to load an alignment and ensure dataset sequences are set up.
298 * @throws IOException
300 protected AlignmentI loadAlignment(final String data, FileFormatI format)
303 AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
310 * Test mapping a column selection in protein to its dna equivalent
312 * @throws IOException
314 @Test(groups = { "Functional" })
315 public void testMapColumnSelection_proteinToDna() throws IOException
317 setupMappedAlignments();
319 ColumnSelection colsel = new ColumnSelection();
320 HiddenColumns hidden = new HiddenColumns();
323 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
324 * in dna respectively, overall 0-4
326 colsel.addElement(0);
327 ColumnSelection cs = new ColumnSelection();
328 HiddenColumns hs = new HiddenColumns();
329 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
331 assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
334 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
338 colsel.addElement(1);
339 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
341 assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
344 * Column 2 in protein picks up gaps only - no mapping
348 colsel.addElement(2);
349 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
351 assertEquals("[]", cs.getSelected().toString());
354 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
355 * 6-9, 6-10, 5-8 respectively, overall to 5-10
359 colsel.addElement(3);
360 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
362 assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
365 * Combine selection of columns 1 and 3 to get a discontiguous mapped
370 colsel.addElement(1);
371 colsel.addElement(3);
372 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
374 assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]",
375 cs.getSelected().toString());
379 * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
380 * offset start positions for a more general test case.
382 * @throws IOException
384 protected void setupMappedAlignments() throws IOException
387 * Map (upper-case = coding):
388 * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
389 * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
390 * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
392 AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
393 + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
395 cdna.setDataset(null);
396 AlignmentI protein = loadAlignment(
397 ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
399 protein.setDataset(null);
401 // map first dna to first protein seq
402 AlignedCodonFrame acf = new AlignedCodonFrame();
403 MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
406 acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(),
407 protein.getSequenceAt(0).getDatasetSequence(), map);
409 // map second dna to second protein seq
410 map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 },
413 acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(),
414 protein.getSequenceAt(1).getDatasetSequence(), map);
416 // map third dna to third protein seq
417 map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 },
420 acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(),
421 protein.getSequenceAt(2).getDatasetSequence(), map);
422 List<AlignedCodonFrame> acfList = Arrays
423 .asList(new AlignedCodonFrame[]
426 dnaView = new AlignViewport(cdna);
427 proteinView = new AlignViewport(protein);
428 protein.setCodonFrames(acfList);
432 * Test mapping a column selection in dna to its protein equivalent
434 * @throws IOException
436 @Test(groups = { "Functional" })
437 public void testMapColumnSelection_dnaToProtein() throws IOException
439 setupMappedAlignments();
441 ColumnSelection colsel = new ColumnSelection();
442 HiddenColumns hidden = new HiddenColumns();
445 * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
448 ColumnSelection cs = new ColumnSelection();
449 HiddenColumns hs = new HiddenColumns();
450 colsel.addElement(0);
451 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
453 assertEquals("[0, 1]", cs.getSelected().toString());
456 * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
457 * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
459 colsel.addElement(3);
460 colsel.addElement(4);
461 colsel.addElement(5);
463 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
465 assertEquals("[0, 1, 3]", cs.getSelected().toString());
468 @Test(groups = { "Functional" })
469 public void testMapColumnSelection_null() throws IOException
471 setupMappedAlignments();
472 ColumnSelection cs = new ColumnSelection();
473 HiddenColumns hs = new HiddenColumns();
474 MappingUtils.mapColumnSelection(null, null, dnaView, proteinView, cs,
476 assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
480 * Tests for the method that converts a series of [start, end] ranges to
483 @Test(groups = { "Functional" })
484 public void testFlattenRanges()
486 assertEquals("[1, 2, 3, 4]",
487 Arrays.toString(MappingUtils.flattenRanges(new int[]
489 assertEquals("[1, 2, 3, 4]",
490 Arrays.toString(MappingUtils.flattenRanges(new int[]
492 assertEquals("[1, 2, 3, 4]",
493 Arrays.toString(MappingUtils.flattenRanges(new int[]
494 { 1, 1, 2, 2, 3, 3, 4, 4 })));
495 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
496 Arrays.toString(MappingUtils.flattenRanges(new int[]
497 { 1, 4, 7, 9, 12, 12 })));
498 // trailing unpaired start position is ignored:
499 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
500 Arrays.toString(MappingUtils.flattenRanges(new int[]
501 { 1, 4, 7, 9, 12, 12, 15 })));
505 * Test mapping a sequence group made of entire columns.
507 * @throws IOException
509 @Test(groups = { "Functional" })
510 public void testMapSequenceGroup_columns() throws IOException
513 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
516 AlignmentI cdna = loadAlignment(
517 ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
519 cdna.setDataset(null);
520 AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
522 protein.setDataset(null);
523 AlignedCodonFrame acf = new AlignedCodonFrame();
524 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
525 for (int seq = 0; seq < 3; seq++)
527 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
528 protein.getSequenceAt(seq).getDatasetSequence(), map);
530 List<AlignedCodonFrame> acfList = Arrays
531 .asList(new AlignedCodonFrame[]
534 AlignViewportI dnaView = new AlignViewport(cdna);
535 AlignViewportI proteinView = new AlignViewport(protein);
536 protein.setCodonFrames(acfList);
539 * Select all sequences, column 2 in the protein
541 SequenceGroup sg = new SequenceGroup();
542 sg.setColourText(true);
543 sg.setIdColour(Color.GREEN);
544 sg.setOutlineColour(Color.LIGHT_GRAY);
545 sg.addSequence(protein.getSequenceAt(0), false);
546 sg.addSequence(protein.getSequenceAt(1), false);
547 sg.addSequence(protein.getSequenceAt(2), false);
552 * Verify the mapped sequence group in dna
554 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
555 proteinView, dnaView);
556 assertTrue(mappedGroup.getColourText());
557 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
558 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
559 assertEquals(3, mappedGroup.getSequences().size());
560 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
561 assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
562 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
563 assertEquals(3, mappedGroup.getStartRes());
564 assertEquals(5, mappedGroup.getEndRes());
567 * Verify mapping sequence group from dna to protein
570 sg.addSequence(cdna.getSequenceAt(0), false);
571 sg.addSequence(cdna.getSequenceAt(1), false);
572 sg.addSequence(cdna.getSequenceAt(2), false);
573 // select columns 2 and 3 in DNA which span protein columns 0 and 1
576 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
577 assertTrue(mappedGroup.getColourText());
578 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
579 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
580 assertEquals(3, mappedGroup.getSequences().size());
581 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
582 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
583 assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
584 assertEquals(0, mappedGroup.getStartRes());
585 assertEquals(1, mappedGroup.getEndRes());
589 * Test mapping a sequence group made of a sequences/columns region.
591 * @throws IOException
593 @Test(groups = { "Functional" })
594 public void testMapSequenceGroup_region() throws IOException
597 * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
600 AlignmentI cdna = loadAlignment(
601 ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
603 cdna.setDataset(null);
604 AlignmentI protein = loadAlignment(
605 ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n",
607 protein.setDataset(null);
608 AlignedCodonFrame acf = new AlignedCodonFrame();
609 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
610 for (int seq = 0; seq < 3; seq++)
612 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
613 protein.getSequenceAt(seq).getDatasetSequence(), map);
615 List<AlignedCodonFrame> acfList = Arrays
616 .asList(new AlignedCodonFrame[]
619 AlignViewportI dnaView = new AlignViewport(cdna);
620 AlignViewportI proteinView = new AlignViewport(protein);
621 protein.setCodonFrames(acfList);
624 * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
625 * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
626 * only includes a gap in Seq2 there is no mappable selection region in the
629 SequenceGroup sg = new SequenceGroup();
630 sg.setColourText(true);
631 sg.setIdColour(Color.GREEN);
632 sg.setOutlineColour(Color.LIGHT_GRAY);
633 sg.addSequence(protein.getSequenceAt(0), false);
634 sg.addSequence(protein.getSequenceAt(1), false);
639 * Verify the mapped sequence group in dna
641 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
642 proteinView, dnaView);
643 assertTrue(mappedGroup.getColourText());
644 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
645 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
646 assertEquals(1, mappedGroup.getSequences().size());
647 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
648 // Seq2 in protein has a gap in column 1 - ignored
649 // Seq1 has K which should map to columns 0-3 in Seq1
650 assertEquals(0, mappedGroup.getStartRes());
651 assertEquals(3, mappedGroup.getEndRes());
654 * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
655 * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
659 mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
660 assertEquals(1, mappedGroup.getStartRes());
661 assertEquals(13, mappedGroup.getEndRes());
664 * Verify mapping sequence group from dna to protein
667 sg.addSequence(cdna.getSequenceAt(0), false);
669 // select columns 4,5 - includes Seq1:codon2 (A) only
672 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
673 assertEquals(2, mappedGroup.getStartRes());
674 assertEquals(2, mappedGroup.getEndRes());
676 // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
677 sg.addSequence(cdna.getSequenceAt(1), false);
678 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
679 assertEquals(2, mappedGroup.getStartRes());
680 assertEquals(4, mappedGroup.getEndRes());
682 // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
683 sg.addSequence(cdna.getSequenceAt(2), false);
684 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
685 assertEquals(0, mappedGroup.getStartRes());
686 assertEquals(4, mappedGroup.getEndRes());
689 @Test(groups = { "Functional" })
690 public void testFindMappingsForSequence()
692 SequenceI seq1 = new Sequence("Seq1", "ABC");
693 SequenceI seq2 = new Sequence("Seq2", "ABC");
694 SequenceI seq3 = new Sequence("Seq3", "ABC");
695 SequenceI seq4 = new Sequence("Seq4", "ABC");
696 seq1.createDatasetSequence();
697 seq2.createDatasetSequence();
698 seq3.createDatasetSequence();
699 seq4.createDatasetSequence();
702 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
704 AlignedCodonFrame acf1 = new AlignedCodonFrame();
705 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
706 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
707 AlignedCodonFrame acf2 = new AlignedCodonFrame();
708 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
709 AlignedCodonFrame acf3 = new AlignedCodonFrame();
710 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
712 List<AlignedCodonFrame> mappings = new ArrayList<>();
718 * Seq1 has three mappings
720 List<AlignedCodonFrame> result = MappingUtils
721 .findMappingsForSequence(seq1, mappings);
722 assertEquals(3, result.size());
723 assertTrue(result.contains(acf1));
724 assertTrue(result.contains(acf2));
725 assertTrue(result.contains(acf3));
728 * Seq2 has two mappings
730 result = MappingUtils.findMappingsForSequence(seq2, mappings);
731 assertEquals(2, result.size());
732 assertTrue(result.contains(acf1));
733 assertTrue(result.contains(acf2));
736 * Seq3 has one mapping
738 result = MappingUtils.findMappingsForSequence(seq3, mappings);
739 assertEquals(1, result.size());
740 assertTrue(result.contains(acf3));
743 * Seq4 has no mappings
745 result = MappingUtils.findMappingsForSequence(seq4, mappings);
746 assertEquals(0, result.size());
748 result = MappingUtils.findMappingsForSequence(null, mappings);
749 assertEquals(0, result.size());
751 result = MappingUtils.findMappingsForSequence(seq1, null);
752 assertEquals(0, result.size());
754 result = MappingUtils.findMappingsForSequence(null, null);
755 assertEquals(0, result.size());
759 * just like the one above, but this time, we provide a set of sequences to
760 * subselect the mapping search
762 @Test(groups = { "Functional" })
763 public void testFindMappingsForSequenceAndOthers()
765 SequenceI seq1 = new Sequence("Seq1", "ABC");
766 SequenceI seq2 = new Sequence("Seq2", "ABC");
767 SequenceI seq3 = new Sequence("Seq3", "ABC");
768 SequenceI seq4 = new Sequence("Seq4", "ABC");
769 seq1.createDatasetSequence();
770 seq2.createDatasetSequence();
771 seq3.createDatasetSequence();
772 seq4.createDatasetSequence();
775 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4
777 AlignedCodonFrame acf1 = new AlignedCodonFrame();
778 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
779 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
780 AlignedCodonFrame acf2 = new AlignedCodonFrame();
781 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
782 AlignedCodonFrame acf3 = new AlignedCodonFrame();
783 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
784 AlignedCodonFrame acf4 = new AlignedCodonFrame();
785 acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map);
787 List<AlignedCodonFrame> mappings = new ArrayList<>();
796 List<AlignedCodonFrame> result = MappingUtils
797 .findMappingsForSequenceAndOthers(null, mappings,
798 Arrays.asList(new SequenceI[]
800 assertTrue(result.isEmpty());
802 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null,
803 Arrays.asList(new SequenceI[]
805 assertTrue(result.isEmpty());
808 * Seq1 has three mappings, but filter argument will only accept
811 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
812 Arrays.asList(new SequenceI[]
813 { seq1, seq2, seq1.getDatasetSequence() }));
814 assertEquals(2, result.size());
815 assertTrue(result.contains(acf1));
816 assertTrue(result.contains(acf2));
817 assertFalse("Did not expect to find mapping acf3 - subselect failed",
818 result.contains(acf3));
820 "Did not expect to find mapping acf4 - doesn't involve sequence",
821 result.contains(acf4));
824 * and verify the no filter case
826 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
828 assertEquals(3, result.size());
829 assertTrue(result.contains(acf1));
830 assertTrue(result.contains(acf2));
831 assertTrue(result.contains(acf3));
834 @Test(groups = { "Functional" })
835 public void testMapEditCommand()
837 SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
838 SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
839 dna.createDatasetSequence();
840 protein.createDatasetSequence();
841 AlignedCodonFrame acf = new AlignedCodonFrame();
842 MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3,
844 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
845 List<AlignedCodonFrame> mappings = new ArrayList<>();
848 AlignmentI prot = new Alignment(new SequenceI[] { protein });
849 prot.setCodonFrames(mappings);
850 AlignmentI nuc = new Alignment(new SequenceI[] { dna });
853 * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
854 * i.e. insert two gaps at column 4
856 EditCommand ec = new EditCommand();
857 final Edit edit = ec.new Edit(Action.INSERT_GAP,
859 { protein }, 4, 2, '-');
860 ec.appendEdit(edit, prot, true, null);
863 * the mapped edit command should be to insert 6 gaps before base 4 in the
864 * nucleotide sequence, which corresponds to aligned column 12 in the dna
866 EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
868 assertEquals(1, mappedEdit.getEdits().size());
869 Edit e = mappedEdit.getEdits().get(0);
870 assertEquals(1, e.getSequences().length);
871 assertEquals(dna, e.getSequences()[0]);
872 assertEquals(12, e.getPosition());
873 assertEquals(6, e.getNumber());
877 * Tests for the method that converts a series of [start, end] ranges to
878 * single positions, where the mapping is to a reverse strand i.e. start is
879 * greater than end point mapped to
881 @Test(groups = { "Functional" })
882 public void testFlattenRanges_reverseStrand()
884 assertEquals("[4, 3, 2, 1]",
885 Arrays.toString(MappingUtils.flattenRanges(new int[]
887 assertEquals("[4, 3, 2, 1]",
888 Arrays.toString(MappingUtils.flattenRanges(new int[]
890 assertEquals("[4, 3, 2, 1]",
891 Arrays.toString(MappingUtils.flattenRanges(new int[]
892 { 4, 4, 3, 3, 2, 2, 1, 1 })));
893 assertEquals("[12, 9, 8, 7, 4, 3, 2, 1]",
894 Arrays.toString(MappingUtils.flattenRanges(new int[]
895 { 12, 12, 9, 7, 4, 1 })));
896 // forwards and backwards anyone?
897 assertEquals("[4, 5, 6, 3, 2, 1]",
898 Arrays.toString(MappingUtils.flattenRanges(new int[]
900 // backwards and forwards
901 assertEquals("[3, 2, 1, 4, 5, 6]",
902 Arrays.toString(MappingUtils.flattenRanges(new int[]
904 // trailing unpaired start position is ignored:
905 assertEquals("[12, 9, 8, 7, 4, 3, 2]",
906 Arrays.toString(MappingUtils.flattenRanges(new int[]
907 { 12, 12, 9, 7, 4, 2, 1 })));
911 * Test mapping a column selection including hidden columns
913 * @throws IOException
915 @Test(groups = { "Functional" })
916 public void testMapColumnSelection_hiddenColumns() throws IOException
918 setupMappedAlignments();
920 ColumnSelection proteinSelection = new ColumnSelection();
921 HiddenColumns hiddenCols = new HiddenColumns();
924 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
925 * in dna respectively, overall 0-4
927 proteinSelection.hideSelectedColumns(0, hiddenCols);
928 ColumnSelection dnaSelection = new ColumnSelection();
929 HiddenColumns dnaHidden = new HiddenColumns();
930 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
931 proteinView, dnaView, dnaSelection, dnaHidden);
932 assertEquals("[]", dnaSelection.getSelected().toString());
933 Iterator<int[]> regions = dnaHidden.iterator();
934 assertEquals(1, dnaHidden.getNumberOfRegions());
935 assertEquals("[0, 4]", Arrays.toString(regions.next()));
938 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
940 dnaSelection = new ColumnSelection();
941 dnaHidden = new HiddenColumns();
942 hiddenCols.revealAllHiddenColumns(proteinSelection);
943 // the unhidden columns are now marked selected!
944 assertEquals("[0]", proteinSelection.getSelected().toString());
945 // deselect these or hideColumns will be expanded to include 0
946 proteinSelection.clear();
947 proteinSelection.hideSelectedColumns(1, hiddenCols);
948 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
949 proteinView, dnaView, dnaSelection, dnaHidden);
950 regions = dnaHidden.iterator();
951 assertEquals(1, dnaHidden.getNumberOfRegions());
952 assertEquals("[0, 3]", Arrays.toString(regions.next()));
955 * Column 2 in protein picks up gaps only - no mapping
957 dnaSelection = new ColumnSelection();
958 dnaHidden = new HiddenColumns();
959 hiddenCols.revealAllHiddenColumns(proteinSelection);
960 proteinSelection.clear();
961 proteinSelection.hideSelectedColumns(2, hiddenCols);
962 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
963 proteinView, dnaView, dnaSelection, dnaHidden);
964 assertEquals(0, dnaHidden.getNumberOfRegions());
967 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
968 * 6-9, 6-10, 5-8 respectively, overall to 5-10
970 dnaSelection = new ColumnSelection();
971 dnaHidden = new HiddenColumns();
972 hiddenCols.revealAllHiddenColumns(proteinSelection);
973 proteinSelection.clear();
974 proteinSelection.hideSelectedColumns(3, hiddenCols); // 5-10 hidden in dna
975 proteinSelection.addElement(1); // 0-3 selected in dna
976 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
977 proteinView, dnaView, dnaSelection, dnaHidden);
978 assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
979 regions = dnaHidden.iterator();
980 assertEquals(1, dnaHidden.getNumberOfRegions());
981 assertEquals("[5, 10]", Arrays.toString(regions.next()));
984 * Combine hiding columns 1 and 3 to get discontiguous hidden columns
986 dnaSelection = new ColumnSelection();
987 dnaHidden = new HiddenColumns();
988 hiddenCols.revealAllHiddenColumns(proteinSelection);
989 proteinSelection.clear();
990 proteinSelection.hideSelectedColumns(1, hiddenCols);
991 proteinSelection.hideSelectedColumns(3, hiddenCols);
992 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
993 proteinView, dnaView, dnaSelection, dnaHidden);
994 regions = dnaHidden.iterator();
995 assertEquals(2, dnaHidden.getNumberOfRegions());
996 assertEquals("[0, 3]", Arrays.toString(regions.next()));
997 assertEquals("[5, 10]", Arrays.toString(regions.next()));
1000 @Test(groups = { "Functional" })
1001 public void testGetLength()
1003 assertEquals(0, MappingUtils.getLength(null));
1006 * [start, end] ranges
1008 List<int[]> ranges = new ArrayList<>();
1009 assertEquals(0, MappingUtils.getLength(ranges));
1010 ranges.add(new int[] { 1, 1 });
1011 assertEquals(1, MappingUtils.getLength(ranges));
1012 ranges.add(new int[] { 2, 10 });
1013 assertEquals(10, MappingUtils.getLength(ranges));
1014 ranges.add(new int[] { 20, 10 });
1015 assertEquals(21, MappingUtils.getLength(ranges));
1018 * [start, end, start, end...] ranges
1021 ranges.add(new int[] { 1, 5, 8, 4 });
1022 ranges.add(new int[] { 8, 2 });
1023 ranges.add(new int[] { 12, 12 });
1024 assertEquals(18, MappingUtils.getLength(ranges));
1027 @Test(groups = { "Functional" })
1028 public void testContains()
1030 assertFalse(MappingUtils.contains(null, 1));
1031 List<int[]> ranges = new ArrayList<>();
1032 assertFalse(MappingUtils.contains(ranges, 1));
1034 ranges.add(new int[] { 1, 4 });
1035 ranges.add(new int[] { 6, 6 });
1036 ranges.add(new int[] { 8, 10 });
1037 ranges.add(new int[] { 30, 20 });
1038 ranges.add(new int[] { -16, -44 });
1040 assertFalse(MappingUtils.contains(ranges, 0));
1041 assertTrue(MappingUtils.contains(ranges, 1));
1042 assertTrue(MappingUtils.contains(ranges, 2));
1043 assertTrue(MappingUtils.contains(ranges, 3));
1044 assertTrue(MappingUtils.contains(ranges, 4));
1045 assertFalse(MappingUtils.contains(ranges, 5));
1047 assertTrue(MappingUtils.contains(ranges, 6));
1048 assertFalse(MappingUtils.contains(ranges, 7));
1050 assertTrue(MappingUtils.contains(ranges, 8));
1051 assertTrue(MappingUtils.contains(ranges, 9));
1052 assertTrue(MappingUtils.contains(ranges, 10));
1054 assertFalse(MappingUtils.contains(ranges, 31));
1055 assertTrue(MappingUtils.contains(ranges, 30));
1056 assertTrue(MappingUtils.contains(ranges, 29));
1057 assertTrue(MappingUtils.contains(ranges, 20));
1058 assertFalse(MappingUtils.contains(ranges, 19));
1060 assertFalse(MappingUtils.contains(ranges, -15));
1061 assertTrue(MappingUtils.contains(ranges, -16));
1062 assertTrue(MappingUtils.contains(ranges, -44));
1063 assertFalse(MappingUtils.contains(ranges, -45));
1067 * Test the method that drops positions from the start of a mapped range
1069 @Test(groups = "Functional")
1070 public void testRemoveStartPositions()
1072 int[] ranges = new int[] { 1, 10 };
1073 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1074 assertEquals("[1, 10]", Arrays.toString(adjusted));
1076 adjusted = MappingUtils.removeStartPositions(1, ranges);
1077 assertEquals("[2, 10]", Arrays.toString(adjusted));
1078 assertEquals("[1, 10]", Arrays.toString(ranges));
1081 adjusted = MappingUtils.removeStartPositions(1, ranges);
1082 assertEquals("[3, 10]", Arrays.toString(adjusted));
1083 assertEquals("[2, 10]", Arrays.toString(ranges));
1085 ranges = new int[] { 2, 3, 10, 12 };
1086 adjusted = MappingUtils.removeStartPositions(1, ranges);
1087 assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted));
1088 assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges));
1090 ranges = new int[] { 2, 2, 8, 12 };
1091 adjusted = MappingUtils.removeStartPositions(1, ranges);
1092 assertEquals("[8, 12]", Arrays.toString(adjusted));
1093 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1095 ranges = new int[] { 2, 2, 8, 12 };
1096 adjusted = MappingUtils.removeStartPositions(2, ranges);
1097 assertEquals("[9, 12]", Arrays.toString(adjusted));
1098 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1100 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1101 adjusted = MappingUtils.removeStartPositions(1, ranges);
1102 assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted));
1103 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1105 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1106 adjusted = MappingUtils.removeStartPositions(2, ranges);
1107 assertEquals("[9, 12]", Arrays.toString(adjusted));
1108 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1110 ranges = new int[] { 2, 3, 9, 12 };
1111 adjusted = MappingUtils.removeStartPositions(3, ranges);
1112 assertEquals("[10, 12]", Arrays.toString(adjusted));
1113 assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges));
1117 * Test the method that drops positions from the start of a mapped range, on
1118 * the reverse strand
1120 @Test(groups = "Functional")
1121 public void testRemoveStartPositions_reverseStrand()
1123 int[] ranges = new int[] { 10, 1 };
1124 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1125 assertEquals("[10, 1]", Arrays.toString(adjusted));
1126 assertEquals("[10, 1]", Arrays.toString(ranges));
1129 adjusted = MappingUtils.removeStartPositions(1, ranges);
1130 assertEquals("[9, 1]", Arrays.toString(adjusted));
1131 assertEquals("[10, 1]", Arrays.toString(ranges));
1134 adjusted = MappingUtils.removeStartPositions(1, ranges);
1135 assertEquals("[8, 1]", Arrays.toString(adjusted));
1136 assertEquals("[9, 1]", Arrays.toString(ranges));
1138 ranges = new int[] { 12, 11, 9, 6 };
1139 adjusted = MappingUtils.removeStartPositions(1, ranges);
1140 assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted));
1141 assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges));
1143 ranges = new int[] { 12, 12, 8, 4 };
1144 adjusted = MappingUtils.removeStartPositions(1, ranges);
1145 assertEquals("[8, 4]", Arrays.toString(adjusted));
1146 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1148 ranges = new int[] { 12, 12, 8, 4 };
1149 adjusted = MappingUtils.removeStartPositions(2, ranges);
1150 assertEquals("[7, 4]", Arrays.toString(adjusted));
1151 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1153 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1154 adjusted = MappingUtils.removeStartPositions(1, ranges);
1155 assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted));
1156 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1158 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1159 adjusted = MappingUtils.removeStartPositions(2, ranges);
1160 assertEquals("[8, 4]", Arrays.toString(adjusted));
1161 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1163 ranges = new int[] { 12, 11, 8, 4 };
1164 adjusted = MappingUtils.removeStartPositions(3, ranges);
1165 assertEquals("[7, 4]", Arrays.toString(adjusted));
1166 assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges));
1169 @Test(groups = { "Functional" })
1170 public void testRangeContains()
1173 * both forward ranges
1176 MappingUtils.rangeContains(new int[]
1177 { 1, 10 }, new int[] { 1, 10 }));
1179 MappingUtils.rangeContains(new int[]
1180 { 1, 10 }, new int[] { 2, 10 }));
1182 MappingUtils.rangeContains(new int[]
1183 { 1, 10 }, new int[] { 1, 9 }));
1185 MappingUtils.rangeContains(new int[]
1186 { 1, 10 }, new int[] { 4, 5 }));
1188 MappingUtils.rangeContains(new int[]
1189 { 1, 10 }, new int[] { 0, 9 }));
1191 MappingUtils.rangeContains(new int[]
1192 { 1, 10 }, new int[] { -10, -9 }));
1194 MappingUtils.rangeContains(new int[]
1195 { 1, 10 }, new int[] { 1, 11 }));
1197 MappingUtils.rangeContains(new int[]
1198 { 1, 10 }, new int[] { 11, 12 }));
1201 * forward range, reverse query
1204 MappingUtils.rangeContains(new int[]
1205 { 1, 10 }, new int[] { 10, 1 }));
1207 MappingUtils.rangeContains(new int[]
1208 { 1, 10 }, new int[] { 9, 1 }));
1210 MappingUtils.rangeContains(new int[]
1211 { 1, 10 }, new int[] { 10, 2 }));
1213 MappingUtils.rangeContains(new int[]
1214 { 1, 10 }, new int[] { 5, 5 }));
1216 MappingUtils.rangeContains(new int[]
1217 { 1, 10 }, new int[] { 11, 1 }));
1219 MappingUtils.rangeContains(new int[]
1220 { 1, 10 }, new int[] { 10, 0 }));
1223 * reverse range, forward query
1226 MappingUtils.rangeContains(new int[]
1227 { 10, 1 }, new int[] { 1, 10 }));
1229 MappingUtils.rangeContains(new int[]
1230 { 10, 1 }, new int[] { 1, 9 }));
1232 MappingUtils.rangeContains(new int[]
1233 { 10, 1 }, new int[] { 2, 10 }));
1235 MappingUtils.rangeContains(new int[]
1236 { 10, 1 }, new int[] { 6, 6 }));
1238 MappingUtils.rangeContains(new int[]
1239 { 10, 1 }, new int[] { 6, 11 }));
1241 MappingUtils.rangeContains(new int[]
1242 { 10, 1 }, new int[] { 11, 20 }));
1244 MappingUtils.rangeContains(new int[]
1245 { 10, 1 }, new int[] { -3, -2 }));
1251 MappingUtils.rangeContains(new int[]
1252 { 10, 1 }, new int[] { 10, 1 }));
1254 MappingUtils.rangeContains(new int[]
1255 { 10, 1 }, new int[] { 9, 1 }));
1257 MappingUtils.rangeContains(new int[]
1258 { 10, 1 }, new int[] { 10, 2 }));
1260 MappingUtils.rangeContains(new int[]
1261 { 10, 1 }, new int[] { 3, 3 }));
1263 MappingUtils.rangeContains(new int[]
1264 { 10, 1 }, new int[] { 11, 1 }));
1266 MappingUtils.rangeContains(new int[]
1267 { 10, 1 }, new int[] { 10, 0 }));
1269 MappingUtils.rangeContains(new int[]
1270 { 10, 1 }, new int[] { 12, 11 }));
1272 MappingUtils.rangeContains(new int[]
1273 { 10, 1 }, new int[] { -5, -8 }));
1279 MappingUtils.rangeContains(new int[]
1280 { 1, 10, 12 }, new int[] { 1, 10 }));
1282 MappingUtils.rangeContains(new int[]
1283 { 1, 10 }, new int[] { 1 }));
1284 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, null));
1285 assertFalse(MappingUtils.rangeContains(null, new int[] { 1, 10 }));
1288 @Test(groups = "Functional")
1289 public void testRemoveEndPositions()
1291 List<int[]> ranges = new ArrayList<>();
1294 * case 1: truncate last range
1296 ranges.add(new int[] { 1, 10 });
1297 ranges.add(new int[] { 20, 30 });
1298 MappingUtils.removeEndPositions(5, ranges);
1299 assertEquals(2, ranges.size());
1300 assertEquals(25, ranges.get(1)[1]);
1303 * case 2: remove last range
1306 ranges.add(new int[] { 1, 10 });
1307 ranges.add(new int[] { 20, 22 });
1308 MappingUtils.removeEndPositions(3, ranges);
1309 assertEquals(1, ranges.size());
1310 assertEquals(10, ranges.get(0)[1]);
1313 * case 3: truncate penultimate range
1316 ranges.add(new int[] { 1, 10 });
1317 ranges.add(new int[] { 20, 21 });
1318 MappingUtils.removeEndPositions(3, ranges);
1319 assertEquals(1, ranges.size());
1320 assertEquals(9, ranges.get(0)[1]);
1323 * case 4: remove last two ranges
1326 ranges.add(new int[] { 1, 10 });
1327 ranges.add(new int[] { 20, 20 });
1328 ranges.add(new int[] { 30, 30 });
1329 MappingUtils.removeEndPositions(3, ranges);
1330 assertEquals(1, ranges.size());
1331 assertEquals(9, ranges.get(0)[1]);
1334 @Test(groups = "Functional")
1335 public void testFindOverlap()
1337 List<int[]> ranges = new ArrayList<>();
1338 ranges.add(new int[] { 4, 8 });
1339 ranges.add(new int[] { 10, 12 });
1340 ranges.add(new int[] { 16, 19 });
1342 int[] overlap = MappingUtils.findOverlap(ranges, 5, 13);
1343 assertArrayEquals(overlap, new int[] { 5, 12 });
1344 overlap = MappingUtils.findOverlap(ranges, -100, 100);
1345 assertArrayEquals(overlap, new int[] { 4, 19 });
1346 overlap = MappingUtils.findOverlap(ranges, 7, 17);
1347 assertArrayEquals(overlap, new int[] { 7, 17 });
1348 overlap = MappingUtils.findOverlap(ranges, 13, 15);
1349 assertNull(overlap);
1353 * Test mapping a sequence group where sequences in and outside the group
1354 * share a dataset sequence (e.g. alternative CDS for the same gene)
1356 * This scenario doesn't arise after JAL-3763 changes, but test left as still
1359 * @throws IOException
1361 @Test(groups = { "Functional" })
1362 public void testMapSequenceGroup_sharedDataset() throws IOException
1365 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
1366 * viewport). CDS sequences share the same 'gene' dataset sequence.
1368 SequenceI dna = new Sequence("dna", "aaatttgggcccaaatttgggccc");
1369 SequenceI cds1 = new Sequence("cds1/1-6", "aaattt");
1370 SequenceI cds2 = new Sequence("cds1/4-9", "tttggg");
1371 SequenceI cds3 = new Sequence("cds1/19-24", "gggccc");
1373 cds1.setDatasetSequence(dna);
1374 cds2.setDatasetSequence(dna);
1375 cds3.setDatasetSequence(dna);
1377 SequenceI pep1 = new Sequence("pep1", "KF");
1378 SequenceI pep2 = new Sequence("pep2", "FG");
1379 SequenceI pep3 = new Sequence("pep3", "GP");
1380 pep1.createDatasetSequence();
1381 pep2.createDatasetSequence();
1382 pep3.createDatasetSequence();
1385 * add mappings from coding positions of dna to respective peptides
1387 AlignedCodonFrame acf = new AlignedCodonFrame();
1388 acf.addMap(dna, pep1,
1389 new MapList(new int[]
1390 { 1, 6 }, new int[] { 1, 2 }, 3, 1));
1391 acf.addMap(dna, pep2,
1392 new MapList(new int[]
1393 { 4, 9 }, new int[] { 1, 2 }, 3, 1));
1394 acf.addMap(dna, pep3,
1395 new MapList(new int[]
1396 { 19, 24 }, new int[] { 1, 2 }, 3, 1));
1398 List<AlignedCodonFrame> acfList = Arrays
1399 .asList(new AlignedCodonFrame[]
1402 AlignmentI cdna = new Alignment(new SequenceI[] { cds1, cds2, cds3 });
1403 AlignmentI protein = new Alignment(
1405 { pep1, pep2, pep3 });
1406 AlignViewportI cdnaView = new AlignViewport(cdna);
1407 AlignViewportI peptideView = new AlignViewport(protein);
1408 protein.setCodonFrames(acfList);
1411 * Select pep1 and pep3 in the protein alignment
1413 SequenceGroup sg = new SequenceGroup();
1414 sg.setColourText(true);
1415 sg.setIdColour(Color.GREEN);
1416 sg.setOutlineColour(Color.LIGHT_GRAY);
1417 sg.addSequence(pep1, false);
1418 sg.addSequence(pep3, false);
1419 sg.setEndRes(protein.getWidth() - 1);
1422 * Verify the mapped sequence group in dna is cds1 and cds3
1424 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
1425 peptideView, cdnaView);
1426 assertTrue(mappedGroup.getColourText());
1427 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
1428 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
1429 assertEquals(2, mappedGroup.getSequences().size());
1430 assertSame(cds1, mappedGroup.getSequences().get(0));
1431 assertSame(cds3, mappedGroup.getSequences().get(1));
1432 // columns 1-6 selected (0-5 base zero)
1433 assertEquals(0, mappedGroup.getStartRes());
1434 assertEquals(5, mappedGroup.getEndRes());
1437 * Select mapping sequence group from dna to protein
1440 sg.addSequence(cds2, false);
1441 sg.addSequence(cds1, false);
1443 sg.setEndRes(cdna.getWidth() - 1);
1444 mappedGroup = MappingUtils.mapSequenceGroup(sg, cdnaView, peptideView);
1445 assertTrue(mappedGroup.getColourText());
1446 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
1447 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
1448 assertEquals(2, mappedGroup.getSequences().size());
1449 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
1450 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
1451 assertEquals(0, mappedGroup.getStartRes());
1452 assertEquals(1, mappedGroup.getEndRes()); // two columns
1456 @Test(groups = "Functional")
1457 public void testAddRange()
1459 int[] range = { 1, 5 };
1460 List<int[]> ranges = new ArrayList<>();
1462 // add to empty list:
1463 MappingUtils.addRange(range, ranges);
1464 assertEquals(1, ranges.size());
1465 assertSame(range, ranges.get(0));
1467 // extend contiguous (same position):
1468 MappingUtils.addRange(new int[] { 5, 10 }, ranges);
1469 assertEquals(1, ranges.size());
1470 assertEquals(1, ranges.get(0)[0]);
1471 assertEquals(10, ranges.get(0)[1]);
1473 // extend contiguous (next position):
1474 MappingUtils.addRange(new int[] { 11, 15 }, ranges);
1475 assertEquals(1, ranges.size());
1476 assertEquals(1, ranges.get(0)[0]);
1477 assertEquals(15, ranges.get(0)[1]);
1479 // change direction: range is not merged:
1480 MappingUtils.addRange(new int[] { 16, 10 }, ranges);
1481 assertEquals(2, ranges.size());
1482 assertEquals(16, ranges.get(1)[0]);
1483 assertEquals(10, ranges.get(1)[1]);
1485 // extend reverse contiguous (same position):
1486 MappingUtils.addRange(new int[] { 10, 8 }, ranges);
1487 assertEquals(2, ranges.size());
1488 assertEquals(16, ranges.get(1)[0]);
1489 assertEquals(8, ranges.get(1)[1]);
1491 // extend reverse contiguous (next position):
1492 MappingUtils.addRange(new int[] { 7, 6 }, ranges);
1493 assertEquals(2, ranges.size());
1494 assertEquals(16, ranges.get(1)[0]);
1495 assertEquals(6, ranges.get(1)[1]);
1497 // change direction: range is not merged:
1498 MappingUtils.addRange(new int[] { 6, 9 }, ranges);
1499 assertEquals(3, ranges.size());
1500 assertEquals(6, ranges.get(2)[0]);
1501 assertEquals(9, ranges.get(2)[1]);
1503 // not contiguous: not merged
1504 MappingUtils.addRange(new int[] { 11, 12 }, ranges);
1505 assertEquals(4, ranges.size());
1506 assertEquals(11, ranges.get(3)[0]);
1507 assertEquals(12, ranges.get(3)[1]);