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;
62 public class MappingUtilsTest
64 @BeforeClass(alwaysRun = true)
70 @BeforeClass(alwaysRun = true)
71 public void setUpJvOptionPane()
73 JvOptionPane.setInteractiveMode(false);
74 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
77 private AlignViewportI dnaView;
79 private AlignViewportI proteinView;
82 * Simple test of mapping with no intron involved.
84 @Test(groups = { "Functional" })
85 public void testBuildSearchResults()
87 final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
88 seq1.createDatasetSequence();
90 final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
91 aseq1.createDatasetSequence();
94 * Map dna bases 5-10 to protein residues 12-13
96 AlignedCodonFrame acf = new AlignedCodonFrame();
97 MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
99 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
100 List<AlignedCodonFrame> acfList = Arrays
101 .asList(new AlignedCodonFrame[]
105 * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
107 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
108 assertEquals(1, sr.getResults().size());
109 SearchResultMatchI m = sr.getResults().get(0);
110 assertEquals(seq1.getDatasetSequence(), m.getSequence());
111 assertEquals(5, m.getStart());
112 assertEquals(7, m.getEnd());
113 sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
114 assertEquals(1, sr.getResults().size());
115 m = sr.getResults().get(0);
116 assertEquals(seq1.getDatasetSequence(), m.getSequence());
117 assertEquals(8, m.getStart());
118 assertEquals(10, m.getEnd());
121 * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
123 for (int i = 5; i < 11; i++)
125 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
126 assertEquals(1, sr.getResults().size());
127 m = sr.getResults().get(0);
128 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
129 int residue = i > 7 ? 13 : 12;
130 assertEquals(residue, m.getStart());
131 assertEquals(residue, m.getEnd());
136 * Simple test of mapping with introns involved.
138 @Test(groups = { "Functional" })
139 public void testBuildSearchResults_withIntron()
141 final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
142 seq1.createDatasetSequence();
144 final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
145 aseq1.createDatasetSequence();
148 * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
150 AlignedCodonFrame acf = new AlignedCodonFrame();
151 MapList map = new MapList(
153 { 6, 6, 8, 9, 11, 11, 13, 13, 15, 15 }, new int[] { 8, 9 }, 3,
155 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
156 List<AlignedCodonFrame> acfList = Arrays
157 .asList(new AlignedCodonFrame[]
161 * Check protein residue 8 maps to [6, 8, 9]
163 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
164 assertEquals(2, sr.getResults().size());
165 SearchResultMatchI m = sr.getResults().get(0);
166 assertEquals(seq1.getDatasetSequence(), m.getSequence());
167 assertEquals(6, m.getStart());
168 assertEquals(6, m.getEnd());
169 m = sr.getResults().get(1);
170 assertEquals(seq1.getDatasetSequence(), m.getSequence());
171 assertEquals(8, m.getStart());
172 assertEquals(9, m.getEnd());
175 * Check protein residue 9 maps to [11, 13, 15]
177 sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
178 assertEquals(3, sr.getResults().size());
179 m = sr.getResults().get(0);
180 assertEquals(seq1.getDatasetSequence(), m.getSequence());
181 assertEquals(11, m.getStart());
182 assertEquals(11, m.getEnd());
183 m = sr.getResults().get(1);
184 assertEquals(seq1.getDatasetSequence(), m.getSequence());
185 assertEquals(13, m.getStart());
186 assertEquals(13, m.getEnd());
187 m = sr.getResults().get(2);
188 assertEquals(seq1.getDatasetSequence(), m.getSequence());
189 assertEquals(15, m.getStart());
190 assertEquals(15, m.getEnd());
193 * Check inverse mappings, from codons to protein
195 for (int i = 5; i < 18; i++)
197 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
198 int residue = (i == 6 || i == 8 || i == 9) ? 8
199 : (i == 11 || i == 13 || i == 15 ? 9 : 0);
202 assertEquals(0, sr.getResults().size());
205 assertEquals(1, sr.getResults().size());
206 m = sr.getResults().get(0);
207 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
208 assertEquals(residue, m.getStart());
209 assertEquals(residue, m.getEnd());
214 * Test mapping a sequence group made of entire sequences.
216 * @throws IOException
218 @Test(groups = { "Functional" })
219 public void testMapSequenceGroup_sequences() throws IOException
222 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
225 AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
227 cdna.setDataset(null);
228 AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
230 protein.setDataset(null);
231 AlignedCodonFrame acf = new AlignedCodonFrame();
232 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
233 for (int seq = 0; seq < 3; seq++)
235 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
236 protein.getSequenceAt(seq).getDatasetSequence(), map);
238 List<AlignedCodonFrame> acfList = Arrays
239 .asList(new AlignedCodonFrame[]
242 AlignViewportI dnaView = new AlignViewport(cdna);
243 AlignViewportI proteinView = new AlignViewport(protein);
244 protein.setCodonFrames(acfList);
247 * Select Seq1 and Seq3 in the protein
249 SequenceGroup sg = new SequenceGroup();
250 sg.setColourText(true);
251 sg.setIdColour(Color.GREEN);
252 sg.setOutlineColour(Color.LIGHT_GRAY);
253 sg.addSequence(protein.getSequenceAt(0), false);
254 sg.addSequence(protein.getSequenceAt(2), false);
255 sg.setEndRes(protein.getWidth() - 1);
258 * Verify the mapped sequence group in dna
260 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
261 proteinView, dnaView);
262 assertTrue(mappedGroup.getColourText());
263 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
264 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
265 assertEquals(2, mappedGroup.getSequences().size());
266 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
267 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
268 assertEquals(0, mappedGroup.getStartRes());
269 assertEquals(2, mappedGroup.getEndRes()); // 3 columns (1 codon)
272 * Verify mapping sequence group from dna to protein
275 sg.addSequence(cdna.getSequenceAt(1), false);
276 sg.addSequence(cdna.getSequenceAt(0), false);
279 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
280 assertTrue(mappedGroup.getColourText());
281 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
282 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
283 assertEquals(2, mappedGroup.getSequences().size());
284 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
285 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
286 assertEquals(0, mappedGroup.getStartRes());
287 assertEquals(0, mappedGroup.getEndRes());
291 * Helper method to load an alignment and ensure dataset sequences are set up.
297 * @throws IOException
299 protected AlignmentI loadAlignment(final String data, FileFormatI format)
302 AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
309 * Test mapping a column selection in protein to its dna equivalent
311 * @throws IOException
313 @Test(groups = { "Functional" })
314 public void testMapColumnSelection_proteinToDna() throws IOException
316 setupMappedAlignments();
318 ColumnSelection colsel = new ColumnSelection();
319 HiddenColumns hidden = new HiddenColumns();
322 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
323 * in dna respectively, overall 0-4
325 colsel.addElement(0);
326 ColumnSelection cs = new ColumnSelection();
327 HiddenColumns hs = new HiddenColumns();
328 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
330 assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
333 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
337 colsel.addElement(1);
338 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
340 assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
343 * Column 2 in protein picks up gaps only - no mapping
347 colsel.addElement(2);
348 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
350 assertEquals("[]", cs.getSelected().toString());
353 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
354 * 6-9, 6-10, 5-8 respectively, overall to 5-10
358 colsel.addElement(3);
359 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
361 assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
364 * Combine selection of columns 1 and 3 to get a discontiguous mapped
369 colsel.addElement(1);
370 colsel.addElement(3);
371 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
373 assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]",
374 cs.getSelected().toString());
378 * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
379 * offset start positions for a more general test case.
381 * @throws IOException
383 protected void setupMappedAlignments() throws IOException
386 * Map (upper-case = coding):
387 * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
388 * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
389 * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
391 AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
392 + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
394 cdna.setDataset(null);
395 AlignmentI protein = loadAlignment(
396 ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
398 protein.setDataset(null);
400 // map first dna to first protein seq
401 AlignedCodonFrame acf = new AlignedCodonFrame();
402 MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
405 acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(),
406 protein.getSequenceAt(0).getDatasetSequence(), map);
408 // map second dna to second protein seq
409 map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 },
412 acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(),
413 protein.getSequenceAt(1).getDatasetSequence(), map);
415 // map third dna to third protein seq
416 map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 },
419 acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(),
420 protein.getSequenceAt(2).getDatasetSequence(), map);
421 List<AlignedCodonFrame> acfList = Arrays
422 .asList(new AlignedCodonFrame[]
425 dnaView = new AlignViewport(cdna);
426 proteinView = new AlignViewport(protein);
427 protein.setCodonFrames(acfList);
431 * Test mapping a column selection in dna to its protein equivalent
433 * @throws IOException
435 @Test(groups = { "Functional" })
436 public void testMapColumnSelection_dnaToProtein() throws IOException
438 setupMappedAlignments();
440 ColumnSelection colsel = new ColumnSelection();
441 HiddenColumns hidden = new HiddenColumns();
444 * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
447 ColumnSelection cs = new ColumnSelection();
448 HiddenColumns hs = new HiddenColumns();
449 colsel.addElement(0);
450 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
452 assertEquals("[0, 1]", cs.getSelected().toString());
455 * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
456 * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
458 colsel.addElement(3);
459 colsel.addElement(4);
460 colsel.addElement(5);
462 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
464 assertEquals("[0, 1, 3]", cs.getSelected().toString());
467 @Test(groups = { "Functional" })
468 public void testMapColumnSelection_null() throws IOException
470 setupMappedAlignments();
471 ColumnSelection cs = new ColumnSelection();
472 HiddenColumns hs = new HiddenColumns();
473 MappingUtils.mapColumnSelection(null, null, dnaView, proteinView, cs,
475 assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
479 * Tests for the method that converts a series of [start, end] ranges to
482 @Test(groups = { "Functional" })
483 public void testFlattenRanges()
485 assertEquals("[1, 2, 3, 4]",
486 Arrays.toString(MappingUtils.flattenRanges(new int[]
488 assertEquals("[1, 2, 3, 4]",
489 Arrays.toString(MappingUtils.flattenRanges(new int[]
491 assertEquals("[1, 2, 3, 4]",
492 Arrays.toString(MappingUtils.flattenRanges(new int[]
493 { 1, 1, 2, 2, 3, 3, 4, 4 })));
494 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
495 Arrays.toString(MappingUtils.flattenRanges(new int[]
496 { 1, 4, 7, 9, 12, 12 })));
497 // trailing unpaired start position is ignored:
498 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
499 Arrays.toString(MappingUtils.flattenRanges(new int[]
500 { 1, 4, 7, 9, 12, 12, 15 })));
504 * Test mapping a sequence group made of entire columns.
506 * @throws IOException
508 @Test(groups = { "Functional" })
509 public void testMapSequenceGroup_columns() throws IOException
512 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
515 AlignmentI cdna = loadAlignment(
516 ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
518 cdna.setDataset(null);
519 AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
521 protein.setDataset(null);
522 AlignedCodonFrame acf = new AlignedCodonFrame();
523 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
524 for (int seq = 0; seq < 3; seq++)
526 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
527 protein.getSequenceAt(seq).getDatasetSequence(), map);
529 List<AlignedCodonFrame> acfList = Arrays
530 .asList(new AlignedCodonFrame[]
533 AlignViewportI dnaView = new AlignViewport(cdna);
534 AlignViewportI proteinView = new AlignViewport(protein);
535 protein.setCodonFrames(acfList);
538 * Select all sequences, column 2 in the protein
540 SequenceGroup sg = new SequenceGroup();
541 sg.setColourText(true);
542 sg.setIdColour(Color.GREEN);
543 sg.setOutlineColour(Color.LIGHT_GRAY);
544 sg.addSequence(protein.getSequenceAt(0), false);
545 sg.addSequence(protein.getSequenceAt(1), false);
546 sg.addSequence(protein.getSequenceAt(2), false);
551 * Verify the mapped sequence group in dna
553 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
554 proteinView, dnaView);
555 assertTrue(mappedGroup.getColourText());
556 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
557 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
558 assertEquals(3, mappedGroup.getSequences().size());
559 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
560 assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
561 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
562 assertEquals(3, mappedGroup.getStartRes());
563 assertEquals(5, mappedGroup.getEndRes());
566 * Verify mapping sequence group from dna to protein
569 sg.addSequence(cdna.getSequenceAt(0), false);
570 sg.addSequence(cdna.getSequenceAt(1), false);
571 sg.addSequence(cdna.getSequenceAt(2), false);
572 // select columns 2 and 3 in DNA which span protein columns 0 and 1
575 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
576 assertTrue(mappedGroup.getColourText());
577 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
578 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
579 assertEquals(3, mappedGroup.getSequences().size());
580 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
581 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
582 assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
583 assertEquals(0, mappedGroup.getStartRes());
584 assertEquals(1, mappedGroup.getEndRes());
588 * Test mapping a sequence group made of a sequences/columns region.
590 * @throws IOException
592 @Test(groups = { "Functional" })
593 public void testMapSequenceGroup_region() throws IOException
596 * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
599 AlignmentI cdna = loadAlignment(
600 ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
602 cdna.setDataset(null);
603 AlignmentI protein = loadAlignment(
604 ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n",
606 protein.setDataset(null);
607 AlignedCodonFrame acf = new AlignedCodonFrame();
608 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
609 for (int seq = 0; seq < 3; seq++)
611 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
612 protein.getSequenceAt(seq).getDatasetSequence(), map);
614 List<AlignedCodonFrame> acfList = Arrays
615 .asList(new AlignedCodonFrame[]
618 AlignViewportI dnaView = new AlignViewport(cdna);
619 AlignViewportI proteinView = new AlignViewport(protein);
620 protein.setCodonFrames(acfList);
623 * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
624 * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
625 * only includes a gap in Seq2 there is no mappable selection region in the
628 SequenceGroup sg = new SequenceGroup();
629 sg.setColourText(true);
630 sg.setIdColour(Color.GREEN);
631 sg.setOutlineColour(Color.LIGHT_GRAY);
632 sg.addSequence(protein.getSequenceAt(0), false);
633 sg.addSequence(protein.getSequenceAt(1), false);
638 * Verify the mapped sequence group in dna
640 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
641 proteinView, dnaView);
642 assertTrue(mappedGroup.getColourText());
643 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
644 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
645 assertEquals(1, mappedGroup.getSequences().size());
646 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
647 // Seq2 in protein has a gap in column 1 - ignored
648 // Seq1 has K which should map to columns 0-3 in Seq1
649 assertEquals(0, mappedGroup.getStartRes());
650 assertEquals(3, mappedGroup.getEndRes());
653 * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
654 * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
658 mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
659 assertEquals(1, mappedGroup.getStartRes());
660 assertEquals(13, mappedGroup.getEndRes());
663 * Verify mapping sequence group from dna to protein
666 sg.addSequence(cdna.getSequenceAt(0), false);
668 // select columns 4,5 - includes Seq1:codon2 (A) only
671 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
672 assertEquals(2, mappedGroup.getStartRes());
673 assertEquals(2, mappedGroup.getEndRes());
675 // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
676 sg.addSequence(cdna.getSequenceAt(1), false);
677 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
678 assertEquals(2, mappedGroup.getStartRes());
679 assertEquals(4, mappedGroup.getEndRes());
681 // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
682 sg.addSequence(cdna.getSequenceAt(2), false);
683 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
684 assertEquals(0, mappedGroup.getStartRes());
685 assertEquals(4, mappedGroup.getEndRes());
688 @Test(groups = { "Functional" })
689 public void testFindMappingsForSequence()
691 SequenceI seq1 = new Sequence("Seq1", "ABC");
692 SequenceI seq2 = new Sequence("Seq2", "ABC");
693 SequenceI seq3 = new Sequence("Seq3", "ABC");
694 SequenceI seq4 = new Sequence("Seq4", "ABC");
695 seq1.createDatasetSequence();
696 seq2.createDatasetSequence();
697 seq3.createDatasetSequence();
698 seq4.createDatasetSequence();
701 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
703 AlignedCodonFrame acf1 = new AlignedCodonFrame();
704 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
705 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
706 AlignedCodonFrame acf2 = new AlignedCodonFrame();
707 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
708 AlignedCodonFrame acf3 = new AlignedCodonFrame();
709 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
711 List<AlignedCodonFrame> mappings = new ArrayList<>();
717 * Seq1 has three mappings
719 List<AlignedCodonFrame> result = MappingUtils
720 .findMappingsForSequence(seq1, mappings);
721 assertEquals(3, result.size());
722 assertTrue(result.contains(acf1));
723 assertTrue(result.contains(acf2));
724 assertTrue(result.contains(acf3));
727 * Seq2 has two mappings
729 result = MappingUtils.findMappingsForSequence(seq2, mappings);
730 assertEquals(2, result.size());
731 assertTrue(result.contains(acf1));
732 assertTrue(result.contains(acf2));
735 * Seq3 has one mapping
737 result = MappingUtils.findMappingsForSequence(seq3, mappings);
738 assertEquals(1, result.size());
739 assertTrue(result.contains(acf3));
742 * Seq4 has no mappings
744 result = MappingUtils.findMappingsForSequence(seq4, mappings);
745 assertEquals(0, result.size());
747 result = MappingUtils.findMappingsForSequence(null, mappings);
748 assertEquals(0, result.size());
750 result = MappingUtils.findMappingsForSequence(seq1, null);
751 assertEquals(0, result.size());
753 result = MappingUtils.findMappingsForSequence(null, null);
754 assertEquals(0, result.size());
758 * just like the one above, but this time, we provide a set of sequences to
759 * subselect the mapping search
761 @Test(groups = { "Functional" })
762 public void testFindMappingsForSequenceAndOthers()
764 SequenceI seq1 = new Sequence("Seq1", "ABC");
765 SequenceI seq2 = new Sequence("Seq2", "ABC");
766 SequenceI seq3 = new Sequence("Seq3", "ABC");
767 SequenceI seq4 = new Sequence("Seq4", "ABC");
768 seq1.createDatasetSequence();
769 seq2.createDatasetSequence();
770 seq3.createDatasetSequence();
771 seq4.createDatasetSequence();
774 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4
776 AlignedCodonFrame acf1 = new AlignedCodonFrame();
777 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
778 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
779 AlignedCodonFrame acf2 = new AlignedCodonFrame();
780 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
781 AlignedCodonFrame acf3 = new AlignedCodonFrame();
782 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
783 AlignedCodonFrame acf4 = new AlignedCodonFrame();
784 acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map);
786 List<AlignedCodonFrame> mappings = new ArrayList<>();
795 List<AlignedCodonFrame> result = MappingUtils
796 .findMappingsForSequenceAndOthers(null, mappings,
797 Arrays.asList(new SequenceI[]
799 assertTrue(result.isEmpty());
801 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null,
802 Arrays.asList(new SequenceI[]
804 assertTrue(result.isEmpty());
807 * Seq1 has three mappings, but filter argument will only accept
810 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
811 Arrays.asList(new SequenceI[]
812 { seq1, seq2, seq1.getDatasetSequence() }));
813 assertEquals(2, result.size());
814 assertTrue(result.contains(acf1));
815 assertTrue(result.contains(acf2));
816 assertFalse("Did not expect to find mapping acf3 - subselect failed",
817 result.contains(acf3));
819 "Did not expect to find mapping acf4 - doesn't involve sequence",
820 result.contains(acf4));
823 * and verify the no filter case
825 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
827 assertEquals(3, result.size());
828 assertTrue(result.contains(acf1));
829 assertTrue(result.contains(acf2));
830 assertTrue(result.contains(acf3));
833 @Test(groups = { "Functional" })
834 public void testMapEditCommand()
836 SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
837 SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
838 dna.createDatasetSequence();
839 protein.createDatasetSequence();
840 AlignedCodonFrame acf = new AlignedCodonFrame();
841 MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3,
843 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
844 List<AlignedCodonFrame> mappings = new ArrayList<>();
847 AlignmentI prot = new Alignment(new SequenceI[] { protein });
848 prot.setCodonFrames(mappings);
849 AlignmentI nuc = new Alignment(new SequenceI[] { dna });
852 * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
853 * i.e. insert two gaps at column 4
855 EditCommand ec = new EditCommand();
856 final Edit edit = ec.new Edit(Action.INSERT_GAP,
858 { protein }, 4, 2, '-');
859 ec.appendEdit(edit, prot, true, null);
862 * the mapped edit command should be to insert 6 gaps before base 4 in the
863 * nucleotide sequence, which corresponds to aligned column 12 in the dna
865 EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
867 assertEquals(1, mappedEdit.getEdits().size());
868 Edit e = mappedEdit.getEdits().get(0);
869 assertEquals(1, e.getSequences().length);
870 assertEquals(dna, e.getSequences()[0]);
871 assertEquals(12, e.getPosition());
872 assertEquals(6, e.getNumber());
876 * Tests for the method that converts a series of [start, end] ranges to
877 * single positions, where the mapping is to a reverse strand i.e. start is
878 * greater than end point mapped to
880 @Test(groups = { "Functional" })
881 public void testFlattenRanges_reverseStrand()
883 assertEquals("[4, 3, 2, 1]",
884 Arrays.toString(MappingUtils.flattenRanges(new int[]
886 assertEquals("[4, 3, 2, 1]",
887 Arrays.toString(MappingUtils.flattenRanges(new int[]
889 assertEquals("[4, 3, 2, 1]",
890 Arrays.toString(MappingUtils.flattenRanges(new int[]
891 { 4, 4, 3, 3, 2, 2, 1, 1 })));
892 assertEquals("[12, 9, 8, 7, 4, 3, 2, 1]",
893 Arrays.toString(MappingUtils.flattenRanges(new int[]
894 { 12, 12, 9, 7, 4, 1 })));
895 // forwards and backwards anyone?
896 assertEquals("[4, 5, 6, 3, 2, 1]",
897 Arrays.toString(MappingUtils.flattenRanges(new int[]
899 // backwards and forwards
900 assertEquals("[3, 2, 1, 4, 5, 6]",
901 Arrays.toString(MappingUtils.flattenRanges(new int[]
903 // trailing unpaired start position is ignored:
904 assertEquals("[12, 9, 8, 7, 4, 3, 2]",
905 Arrays.toString(MappingUtils.flattenRanges(new int[]
906 { 12, 12, 9, 7, 4, 2, 1 })));
910 * Test mapping a column selection including hidden columns
912 * @throws IOException
914 @Test(groups = { "Functional" })
915 public void testMapColumnSelection_hiddenColumns() throws IOException
917 setupMappedAlignments();
919 ColumnSelection proteinSelection = new ColumnSelection();
920 HiddenColumns hiddenCols = new HiddenColumns();
923 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
924 * in dna respectively, overall 0-4
926 proteinSelection.hideSelectedColumns(0, hiddenCols);
927 ColumnSelection dnaSelection = new ColumnSelection();
928 HiddenColumns dnaHidden = new HiddenColumns();
929 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
930 proteinView, dnaView, dnaSelection, dnaHidden);
931 assertEquals("[]", dnaSelection.getSelected().toString());
932 Iterator<int[]> regions = dnaHidden.iterator();
933 assertEquals(1, dnaHidden.getNumberOfRegions());
934 assertEquals("[0, 4]", Arrays.toString(regions.next()));
937 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
939 dnaSelection = new ColumnSelection();
940 dnaHidden = new HiddenColumns();
941 hiddenCols.revealAllHiddenColumns(proteinSelection);
942 // the unhidden columns are now marked selected!
943 assertEquals("[0]", proteinSelection.getSelected().toString());
944 // deselect these or hideColumns will be expanded to include 0
945 proteinSelection.clear();
946 proteinSelection.hideSelectedColumns(1, hiddenCols);
947 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
948 proteinView, dnaView, dnaSelection, dnaHidden);
949 regions = dnaHidden.iterator();
950 assertEquals(1, dnaHidden.getNumberOfRegions());
951 assertEquals("[0, 3]", Arrays.toString(regions.next()));
954 * Column 2 in protein picks up gaps only - no mapping
956 dnaSelection = new ColumnSelection();
957 dnaHidden = new HiddenColumns();
958 hiddenCols.revealAllHiddenColumns(proteinSelection);
959 proteinSelection.clear();
960 proteinSelection.hideSelectedColumns(2, hiddenCols);
961 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
962 proteinView, dnaView, dnaSelection, dnaHidden);
963 assertEquals(0, dnaHidden.getNumberOfRegions());
966 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
967 * 6-9, 6-10, 5-8 respectively, overall to 5-10
969 dnaSelection = new ColumnSelection();
970 dnaHidden = new HiddenColumns();
971 hiddenCols.revealAllHiddenColumns(proteinSelection);
972 proteinSelection.clear();
973 proteinSelection.hideSelectedColumns(3, hiddenCols); // 5-10 hidden in dna
974 proteinSelection.addElement(1); // 0-3 selected in dna
975 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
976 proteinView, dnaView, dnaSelection, dnaHidden);
977 assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
978 regions = dnaHidden.iterator();
979 assertEquals(1, dnaHidden.getNumberOfRegions());
980 assertEquals("[5, 10]", Arrays.toString(regions.next()));
983 * Combine hiding columns 1 and 3 to get discontiguous hidden columns
985 dnaSelection = new ColumnSelection();
986 dnaHidden = new HiddenColumns();
987 hiddenCols.revealAllHiddenColumns(proteinSelection);
988 proteinSelection.clear();
989 proteinSelection.hideSelectedColumns(1, hiddenCols);
990 proteinSelection.hideSelectedColumns(3, hiddenCols);
991 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
992 proteinView, dnaView, dnaSelection, dnaHidden);
993 regions = dnaHidden.iterator();
994 assertEquals(2, dnaHidden.getNumberOfRegions());
995 assertEquals("[0, 3]", Arrays.toString(regions.next()));
996 assertEquals("[5, 10]", Arrays.toString(regions.next()));
999 @Test(groups = { "Functional" })
1000 public void testGetLength()
1002 assertEquals(0, MappingUtils.getLength(null));
1005 * [start, end] ranges
1007 List<int[]> ranges = new ArrayList<>();
1008 assertEquals(0, MappingUtils.getLength(ranges));
1009 ranges.add(new int[] { 1, 1 });
1010 assertEquals(1, MappingUtils.getLength(ranges));
1011 ranges.add(new int[] { 2, 10 });
1012 assertEquals(10, MappingUtils.getLength(ranges));
1013 ranges.add(new int[] { 20, 10 });
1014 assertEquals(21, MappingUtils.getLength(ranges));
1017 * [start, end, start, end...] ranges
1020 ranges.add(new int[] { 1, 5, 8, 4 });
1021 ranges.add(new int[] { 8, 2 });
1022 ranges.add(new int[] { 12, 12 });
1023 assertEquals(18, MappingUtils.getLength(ranges));
1026 @Test(groups = { "Functional" })
1027 public void testContains()
1029 assertFalse(MappingUtils.contains(null, 1));
1030 List<int[]> ranges = new ArrayList<>();
1031 assertFalse(MappingUtils.contains(ranges, 1));
1033 ranges.add(new int[] { 1, 4 });
1034 ranges.add(new int[] { 6, 6 });
1035 ranges.add(new int[] { 8, 10 });
1036 ranges.add(new int[] { 30, 20 });
1037 ranges.add(new int[] { -16, -44 });
1039 assertFalse(MappingUtils.contains(ranges, 0));
1040 assertTrue(MappingUtils.contains(ranges, 1));
1041 assertTrue(MappingUtils.contains(ranges, 2));
1042 assertTrue(MappingUtils.contains(ranges, 3));
1043 assertTrue(MappingUtils.contains(ranges, 4));
1044 assertFalse(MappingUtils.contains(ranges, 5));
1046 assertTrue(MappingUtils.contains(ranges, 6));
1047 assertFalse(MappingUtils.contains(ranges, 7));
1049 assertTrue(MappingUtils.contains(ranges, 8));
1050 assertTrue(MappingUtils.contains(ranges, 9));
1051 assertTrue(MappingUtils.contains(ranges, 10));
1053 assertFalse(MappingUtils.contains(ranges, 31));
1054 assertTrue(MappingUtils.contains(ranges, 30));
1055 assertTrue(MappingUtils.contains(ranges, 29));
1056 assertTrue(MappingUtils.contains(ranges, 20));
1057 assertFalse(MappingUtils.contains(ranges, 19));
1059 assertFalse(MappingUtils.contains(ranges, -15));
1060 assertTrue(MappingUtils.contains(ranges, -16));
1061 assertTrue(MappingUtils.contains(ranges, -44));
1062 assertFalse(MappingUtils.contains(ranges, -45));
1066 * Test the method that drops positions from the start of a mapped range
1068 @Test(groups = "Functional")
1069 public void testRemoveStartPositions()
1071 int[] ranges = new int[] { 1, 10 };
1072 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1073 assertEquals("[1, 10]", Arrays.toString(adjusted));
1075 adjusted = MappingUtils.removeStartPositions(1, ranges);
1076 assertEquals("[2, 10]", Arrays.toString(adjusted));
1077 assertEquals("[1, 10]", Arrays.toString(ranges));
1080 adjusted = MappingUtils.removeStartPositions(1, ranges);
1081 assertEquals("[3, 10]", Arrays.toString(adjusted));
1082 assertEquals("[2, 10]", Arrays.toString(ranges));
1084 ranges = new int[] { 2, 3, 10, 12 };
1085 adjusted = MappingUtils.removeStartPositions(1, ranges);
1086 assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted));
1087 assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges));
1089 ranges = new int[] { 2, 2, 8, 12 };
1090 adjusted = MappingUtils.removeStartPositions(1, ranges);
1091 assertEquals("[8, 12]", Arrays.toString(adjusted));
1092 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1094 ranges = new int[] { 2, 2, 8, 12 };
1095 adjusted = MappingUtils.removeStartPositions(2, ranges);
1096 assertEquals("[9, 12]", Arrays.toString(adjusted));
1097 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1099 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1100 adjusted = MappingUtils.removeStartPositions(1, ranges);
1101 assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted));
1102 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1104 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1105 adjusted = MappingUtils.removeStartPositions(2, ranges);
1106 assertEquals("[9, 12]", Arrays.toString(adjusted));
1107 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1109 ranges = new int[] { 2, 3, 9, 12 };
1110 adjusted = MappingUtils.removeStartPositions(3, ranges);
1111 assertEquals("[10, 12]", Arrays.toString(adjusted));
1112 assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges));
1116 * Test the method that drops positions from the start of a mapped range, on
1117 * the reverse strand
1119 @Test(groups = "Functional")
1120 public void testRemoveStartPositions_reverseStrand()
1122 int[] ranges = new int[] { 10, 1 };
1123 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1124 assertEquals("[10, 1]", Arrays.toString(adjusted));
1125 assertEquals("[10, 1]", Arrays.toString(ranges));
1128 adjusted = MappingUtils.removeStartPositions(1, ranges);
1129 assertEquals("[9, 1]", Arrays.toString(adjusted));
1130 assertEquals("[10, 1]", Arrays.toString(ranges));
1133 adjusted = MappingUtils.removeStartPositions(1, ranges);
1134 assertEquals("[8, 1]", Arrays.toString(adjusted));
1135 assertEquals("[9, 1]", Arrays.toString(ranges));
1137 ranges = new int[] { 12, 11, 9, 6 };
1138 adjusted = MappingUtils.removeStartPositions(1, ranges);
1139 assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted));
1140 assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges));
1142 ranges = new int[] { 12, 12, 8, 4 };
1143 adjusted = MappingUtils.removeStartPositions(1, ranges);
1144 assertEquals("[8, 4]", Arrays.toString(adjusted));
1145 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1147 ranges = new int[] { 12, 12, 8, 4 };
1148 adjusted = MappingUtils.removeStartPositions(2, ranges);
1149 assertEquals("[7, 4]", Arrays.toString(adjusted));
1150 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1152 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1153 adjusted = MappingUtils.removeStartPositions(1, ranges);
1154 assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted));
1155 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1157 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1158 adjusted = MappingUtils.removeStartPositions(2, ranges);
1159 assertEquals("[8, 4]", Arrays.toString(adjusted));
1160 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1162 ranges = new int[] { 12, 11, 8, 4 };
1163 adjusted = MappingUtils.removeStartPositions(3, ranges);
1164 assertEquals("[7, 4]", Arrays.toString(adjusted));
1165 assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges));
1168 @Test(groups = { "Functional" })
1169 public void testRangeContains()
1172 * both forward ranges
1175 MappingUtils.rangeContains(new int[]
1176 { 1, 10 }, new int[] { 1, 10 }));
1178 MappingUtils.rangeContains(new int[]
1179 { 1, 10 }, new int[] { 2, 10 }));
1181 MappingUtils.rangeContains(new int[]
1182 { 1, 10 }, new int[] { 1, 9 }));
1184 MappingUtils.rangeContains(new int[]
1185 { 1, 10 }, new int[] { 4, 5 }));
1187 MappingUtils.rangeContains(new int[]
1188 { 1, 10 }, new int[] { 0, 9 }));
1190 MappingUtils.rangeContains(new int[]
1191 { 1, 10 }, new int[] { -10, -9 }));
1193 MappingUtils.rangeContains(new int[]
1194 { 1, 10 }, new int[] { 1, 11 }));
1196 MappingUtils.rangeContains(new int[]
1197 { 1, 10 }, new int[] { 11, 12 }));
1200 * forward range, reverse query
1203 MappingUtils.rangeContains(new int[]
1204 { 1, 10 }, new int[] { 10, 1 }));
1206 MappingUtils.rangeContains(new int[]
1207 { 1, 10 }, new int[] { 9, 1 }));
1209 MappingUtils.rangeContains(new int[]
1210 { 1, 10 }, new int[] { 10, 2 }));
1212 MappingUtils.rangeContains(new int[]
1213 { 1, 10 }, new int[] { 5, 5 }));
1215 MappingUtils.rangeContains(new int[]
1216 { 1, 10 }, new int[] { 11, 1 }));
1218 MappingUtils.rangeContains(new int[]
1219 { 1, 10 }, new int[] { 10, 0 }));
1222 * reverse range, forward query
1225 MappingUtils.rangeContains(new int[]
1226 { 10, 1 }, new int[] { 1, 10 }));
1228 MappingUtils.rangeContains(new int[]
1229 { 10, 1 }, new int[] { 1, 9 }));
1231 MappingUtils.rangeContains(new int[]
1232 { 10, 1 }, new int[] { 2, 10 }));
1234 MappingUtils.rangeContains(new int[]
1235 { 10, 1 }, new int[] { 6, 6 }));
1237 MappingUtils.rangeContains(new int[]
1238 { 10, 1 }, new int[] { 6, 11 }));
1240 MappingUtils.rangeContains(new int[]
1241 { 10, 1 }, new int[] { 11, 20 }));
1243 MappingUtils.rangeContains(new int[]
1244 { 10, 1 }, new int[] { -3, -2 }));
1250 MappingUtils.rangeContains(new int[]
1251 { 10, 1 }, new int[] { 10, 1 }));
1253 MappingUtils.rangeContains(new int[]
1254 { 10, 1 }, new int[] { 9, 1 }));
1256 MappingUtils.rangeContains(new int[]
1257 { 10, 1 }, new int[] { 10, 2 }));
1259 MappingUtils.rangeContains(new int[]
1260 { 10, 1 }, new int[] { 3, 3 }));
1262 MappingUtils.rangeContains(new int[]
1263 { 10, 1 }, new int[] { 11, 1 }));
1265 MappingUtils.rangeContains(new int[]
1266 { 10, 1 }, new int[] { 10, 0 }));
1268 MappingUtils.rangeContains(new int[]
1269 { 10, 1 }, new int[] { 12, 11 }));
1271 MappingUtils.rangeContains(new int[]
1272 { 10, 1 }, new int[] { -5, -8 }));
1278 MappingUtils.rangeContains(new int[]
1279 { 1, 10, 12 }, new int[] { 1, 10 }));
1281 MappingUtils.rangeContains(new int[]
1282 { 1, 10 }, new int[] { 1 }));
1283 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, null));
1284 assertFalse(MappingUtils.rangeContains(null, new int[] { 1, 10 }));
1287 @Test(groups = "Functional")
1288 public void testRemoveEndPositions()
1290 List<int[]> ranges = new ArrayList<>();
1293 * case 1: truncate last range
1295 ranges.add(new int[] { 1, 10 });
1296 ranges.add(new int[] { 20, 30 });
1297 MappingUtils.removeEndPositions(5, ranges);
1298 assertEquals(2, ranges.size());
1299 assertEquals(25, ranges.get(1)[1]);
1302 * case 2: remove last range
1305 ranges.add(new int[] { 1, 10 });
1306 ranges.add(new int[] { 20, 22 });
1307 MappingUtils.removeEndPositions(3, ranges);
1308 assertEquals(1, ranges.size());
1309 assertEquals(10, ranges.get(0)[1]);
1312 * case 3: truncate penultimate range
1315 ranges.add(new int[] { 1, 10 });
1316 ranges.add(new int[] { 20, 21 });
1317 MappingUtils.removeEndPositions(3, ranges);
1318 assertEquals(1, ranges.size());
1319 assertEquals(9, ranges.get(0)[1]);
1322 * case 4: remove last two ranges
1325 ranges.add(new int[] { 1, 10 });
1326 ranges.add(new int[] { 20, 20 });
1327 ranges.add(new int[] { 30, 30 });
1328 MappingUtils.removeEndPositions(3, ranges);
1329 assertEquals(1, ranges.size());
1330 assertEquals(9, ranges.get(0)[1]);
1333 @Test(groups = "Functional")
1334 public void testFindOverlap()
1336 List<int[]> ranges = new ArrayList<>();
1337 ranges.add(new int[] { 4, 8 });
1338 ranges.add(new int[] { 10, 12 });
1339 ranges.add(new int[] { 16, 19 });
1341 int[] overlap = MappingUtils.findOverlap(ranges, 5, 13);
1342 assertArrayEquals(overlap, new int[] { 5, 12 });
1343 overlap = MappingUtils.findOverlap(ranges, -100, 100);
1344 assertArrayEquals(overlap, new int[] { 4, 19 });
1345 overlap = MappingUtils.findOverlap(ranges, 7, 17);
1346 assertArrayEquals(overlap, new int[] { 7, 17 });
1347 overlap = MappingUtils.findOverlap(ranges, 13, 15);
1348 assertNull(overlap);
1352 * Test mapping a sequence group where sequences in and outside the group
1353 * share a dataset sequence (e.g. alternative CDS for the same gene)
1355 * This scenario doesn't arise after JAL-3763 changes, but test left as still
1358 * @throws IOException
1360 @Test(groups = { "Functional" })
1361 public void testMapSequenceGroup_sharedDataset() throws IOException
1364 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
1365 * viewport). CDS sequences share the same 'gene' dataset sequence.
1367 SequenceI dna = new Sequence("dna", "aaatttgggcccaaatttgggccc");
1368 SequenceI cds1 = new Sequence("cds1/1-6", "aaattt");
1369 SequenceI cds2 = new Sequence("cds1/4-9", "tttggg");
1370 SequenceI cds3 = new Sequence("cds1/19-24", "gggccc");
1372 cds1.setDatasetSequence(dna);
1373 cds2.setDatasetSequence(dna);
1374 cds3.setDatasetSequence(dna);
1376 SequenceI pep1 = new Sequence("pep1", "KF");
1377 SequenceI pep2 = new Sequence("pep2", "FG");
1378 SequenceI pep3 = new Sequence("pep3", "GP");
1379 pep1.createDatasetSequence();
1380 pep2.createDatasetSequence();
1381 pep3.createDatasetSequence();
1384 * add mappings from coding positions of dna to respective peptides
1386 AlignedCodonFrame acf = new AlignedCodonFrame();
1387 acf.addMap(dna, pep1,
1388 new MapList(new int[]
1389 { 1, 6 }, new int[] { 1, 2 }, 3, 1));
1390 acf.addMap(dna, pep2,
1391 new MapList(new int[]
1392 { 4, 9 }, new int[] { 1, 2 }, 3, 1));
1393 acf.addMap(dna, pep3,
1394 new MapList(new int[]
1395 { 19, 24 }, new int[] { 1, 2 }, 3, 1));
1397 List<AlignedCodonFrame> acfList = Arrays
1398 .asList(new AlignedCodonFrame[]
1401 AlignmentI cdna = new Alignment(new SequenceI[] { cds1, cds2, cds3 });
1402 AlignmentI protein = new Alignment(
1404 { pep1, pep2, pep3 });
1405 AlignViewportI cdnaView = new AlignViewport(cdna);
1406 AlignViewportI peptideView = new AlignViewport(protein);
1407 protein.setCodonFrames(acfList);
1410 * Select pep1 and pep3 in the protein alignment
1412 SequenceGroup sg = new SequenceGroup();
1413 sg.setColourText(true);
1414 sg.setIdColour(Color.GREEN);
1415 sg.setOutlineColour(Color.LIGHT_GRAY);
1416 sg.addSequence(pep1, false);
1417 sg.addSequence(pep3, false);
1418 sg.setEndRes(protein.getWidth() - 1);
1421 * Verify the mapped sequence group in dna is cds1 and cds3
1423 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
1424 peptideView, cdnaView);
1425 assertTrue(mappedGroup.getColourText());
1426 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
1427 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
1428 assertEquals(2, mappedGroup.getSequences().size());
1429 assertSame(cds1, mappedGroup.getSequences().get(0));
1430 assertSame(cds3, mappedGroup.getSequences().get(1));
1431 // columns 1-6 selected (0-5 base zero)
1432 assertEquals(0, mappedGroup.getStartRes());
1433 assertEquals(5, mappedGroup.getEndRes());
1436 * Select mapping sequence group from dna to protein
1439 sg.addSequence(cds2, false);
1440 sg.addSequence(cds1, false);
1442 sg.setEndRes(cdna.getWidth() - 1);
1443 mappedGroup = MappingUtils.mapSequenceGroup(sg, cdnaView, peptideView);
1444 assertTrue(mappedGroup.getColourText());
1445 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
1446 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
1447 assertEquals(2, mappedGroup.getSequences().size());
1448 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
1449 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
1450 assertEquals(0, mappedGroup.getStartRes());
1451 assertEquals(1, mappedGroup.getEndRes()); // two columns