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.AssertJUnit.fail;
30 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
32 import java.awt.Color;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Iterator;
37 import java.util.List;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
42 import jalview.api.AlignViewportI;
43 import jalview.bin.Cache;
44 import jalview.commands.EditCommand;
45 import jalview.commands.EditCommand.Action;
46 import jalview.commands.EditCommand.Edit;
47 import jalview.datamodel.AlignedCodonFrame;
48 import jalview.datamodel.Alignment;
49 import jalview.datamodel.AlignmentI;
50 import jalview.datamodel.ColumnSelection;
51 import jalview.datamodel.HiddenColumns;
52 import jalview.datamodel.SearchResultMatchI;
53 import jalview.datamodel.SearchResultsI;
54 import jalview.datamodel.Sequence;
55 import jalview.datamodel.SequenceGroup;
56 import jalview.datamodel.SequenceI;
57 import jalview.gui.AlignViewport;
58 import jalview.gui.JvOptionPane;
59 import jalview.io.DataSourceType;
60 import jalview.io.FileFormat;
61 import jalview.io.FileFormatI;
62 import jalview.io.FormatAdapter;
64 public class MappingUtilsTest
66 @BeforeClass(alwaysRun = true)
72 @BeforeClass(alwaysRun = true)
73 public void setUpJvOptionPane()
75 JvOptionPane.setInteractiveMode(false);
76 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
79 private AlignViewportI dnaView;
81 private AlignViewportI proteinView;
84 * Simple test of mapping with no intron involved.
86 @Test(groups = { "Functional" })
87 public void testBuildSearchResults()
89 final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
90 seq1.createDatasetSequence();
92 final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
93 aseq1.createDatasetSequence();
96 * Map dna bases 5-10 to protein residues 12-13
98 AlignedCodonFrame acf = new AlignedCodonFrame();
99 MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
101 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
102 List<AlignedCodonFrame> acfList = Arrays
103 .asList(new AlignedCodonFrame[]
107 * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
109 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
110 assertEquals(1, sr.getResults().size());
111 SearchResultMatchI m = sr.getResults().get(0);
112 assertEquals(seq1.getDatasetSequence(), m.getSequence());
113 assertEquals(5, m.getStart());
114 assertEquals(7, m.getEnd());
115 sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
116 assertEquals(1, sr.getResults().size());
117 m = sr.getResults().get(0);
118 assertEquals(seq1.getDatasetSequence(), m.getSequence());
119 assertEquals(8, m.getStart());
120 assertEquals(10, m.getEnd());
123 * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
125 for (int i = 5; i < 11; i++)
127 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
128 assertEquals(1, sr.getResults().size());
129 m = sr.getResults().get(0);
130 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
131 int residue = i > 7 ? 13 : 12;
132 assertEquals(residue, m.getStart());
133 assertEquals(residue, m.getEnd());
138 * Simple test of mapping with introns involved.
140 @Test(groups = { "Functional" })
141 public void testBuildSearchResults_withIntron()
143 final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
144 seq1.createDatasetSequence();
146 final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
147 aseq1.createDatasetSequence();
150 * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
152 AlignedCodonFrame acf = new AlignedCodonFrame();
153 MapList map = new MapList(
155 { 6, 6, 8, 9, 11, 11, 13, 13, 15, 15 }, new int[] { 8, 9 }, 3,
157 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
158 List<AlignedCodonFrame> acfList = Arrays
159 .asList(new AlignedCodonFrame[]
163 * Check protein residue 8 maps to [6, 8, 9]
165 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
166 assertEquals(2, sr.getResults().size());
167 SearchResultMatchI m = sr.getResults().get(0);
168 assertEquals(seq1.getDatasetSequence(), m.getSequence());
169 assertEquals(6, m.getStart());
170 assertEquals(6, m.getEnd());
171 m = sr.getResults().get(1);
172 assertEquals(seq1.getDatasetSequence(), m.getSequence());
173 assertEquals(8, m.getStart());
174 assertEquals(9, m.getEnd());
177 * Check protein residue 9 maps to [11, 13, 15]
179 sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
180 assertEquals(3, sr.getResults().size());
181 m = sr.getResults().get(0);
182 assertEquals(seq1.getDatasetSequence(), m.getSequence());
183 assertEquals(11, m.getStart());
184 assertEquals(11, m.getEnd());
185 m = sr.getResults().get(1);
186 assertEquals(seq1.getDatasetSequence(), m.getSequence());
187 assertEquals(13, m.getStart());
188 assertEquals(13, m.getEnd());
189 m = sr.getResults().get(2);
190 assertEquals(seq1.getDatasetSequence(), m.getSequence());
191 assertEquals(15, m.getStart());
192 assertEquals(15, m.getEnd());
195 * Check inverse mappings, from codons to protein
197 for (int i = 5; i < 18; i++)
199 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
200 int residue = (i == 6 || i == 8 || i == 9) ? 8
201 : (i == 11 || i == 13 || i == 15 ? 9 : 0);
204 assertEquals(0, sr.getResults().size());
207 assertEquals(1, sr.getResults().size());
208 m = sr.getResults().get(0);
209 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
210 assertEquals(residue, m.getStart());
211 assertEquals(residue, m.getEnd());
216 * Test mapping a sequence group made of entire sequences.
218 * @throws IOException
220 @Test(groups = { "Functional" })
221 public void testMapSequenceGroup_sequences() throws IOException
224 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
227 AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
229 cdna.setDataset(null);
230 AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
232 protein.setDataset(null);
233 AlignedCodonFrame acf = new AlignedCodonFrame();
234 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
235 for (int seq = 0; seq < 3; seq++)
237 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
238 protein.getSequenceAt(seq).getDatasetSequence(), map);
240 List<AlignedCodonFrame> acfList = Arrays
241 .asList(new AlignedCodonFrame[]
244 AlignViewportI dnaView = new AlignViewport(cdna);
245 AlignViewportI proteinView = new AlignViewport(protein);
246 protein.setCodonFrames(acfList);
249 * Select Seq1 and Seq3 in the protein
251 SequenceGroup sg = new SequenceGroup();
252 sg.setColourText(true);
253 sg.setIdColour(Color.GREEN);
254 sg.setOutlineColour(Color.LIGHT_GRAY);
255 sg.addSequence(protein.getSequenceAt(0), false);
256 sg.addSequence(protein.getSequenceAt(2), false);
257 sg.setEndRes(protein.getWidth() - 1);
260 * Verify the mapped sequence group in dna
262 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
263 proteinView, dnaView);
264 assertTrue(mappedGroup.getColourText());
265 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
266 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
267 assertEquals(2, mappedGroup.getSequences().size());
268 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
269 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
270 assertEquals(0, mappedGroup.getStartRes());
271 assertEquals(2, mappedGroup.getEndRes()); // 3 columns (1 codon)
274 * Verify mapping sequence group from dna to protein
277 sg.addSequence(cdna.getSequenceAt(1), false);
278 sg.addSequence(cdna.getSequenceAt(0), false);
281 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
282 assertTrue(mappedGroup.getColourText());
283 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
284 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
285 assertEquals(2, mappedGroup.getSequences().size());
286 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
287 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
288 assertEquals(0, mappedGroup.getStartRes());
289 assertEquals(0, mappedGroup.getEndRes());
293 * Helper method to load an alignment and ensure dataset sequences are set up.
299 * @throws IOException
301 protected AlignmentI loadAlignment(final String data, FileFormatI format)
304 AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
311 * Test mapping a column selection in protein to its dna equivalent
313 * @throws IOException
315 @Test(groups = { "Functional" })
316 public void testMapColumnSelection_proteinToDna() throws IOException
318 setupMappedAlignments();
320 ColumnSelection colsel = new ColumnSelection();
321 HiddenColumns hidden = new HiddenColumns();
324 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
325 * in dna respectively, overall 0-4
327 colsel.addElement(0);
328 ColumnSelection cs = new ColumnSelection();
329 HiddenColumns hs = new HiddenColumns();
330 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
332 assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
335 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
339 colsel.addElement(1);
340 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
342 assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
345 * Column 2 in protein picks up gaps only - no mapping
349 colsel.addElement(2);
350 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
352 assertEquals("[]", cs.getSelected().toString());
355 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
356 * 6-9, 6-10, 5-8 respectively, overall to 5-10
360 colsel.addElement(3);
361 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
363 assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
366 * Combine selection of columns 1 and 3 to get a discontiguous mapped
371 colsel.addElement(1);
372 colsel.addElement(3);
373 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
375 assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]",
376 cs.getSelected().toString());
380 * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
381 * offset start positions for a more general test case.
383 * @throws IOException
385 protected void setupMappedAlignments() throws IOException
388 * Map (upper-case = coding):
389 * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
390 * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
391 * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
393 AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
394 + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
396 cdna.setDataset(null);
397 AlignmentI protein = loadAlignment(
398 ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
400 protein.setDataset(null);
402 // map first dna to first protein seq
403 AlignedCodonFrame acf = new AlignedCodonFrame();
404 MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
407 acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(),
408 protein.getSequenceAt(0).getDatasetSequence(), map);
410 // map second dna to second protein seq
411 map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 },
414 acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(),
415 protein.getSequenceAt(1).getDatasetSequence(), map);
417 // map third dna to third protein seq
418 map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 },
421 acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(),
422 protein.getSequenceAt(2).getDatasetSequence(), map);
423 List<AlignedCodonFrame> acfList = Arrays
424 .asList(new AlignedCodonFrame[]
427 dnaView = new AlignViewport(cdna);
428 proteinView = new AlignViewport(protein);
429 protein.setCodonFrames(acfList);
433 * Test mapping a column selection in dna to its protein equivalent
435 * @throws IOException
437 @Test(groups = { "Functional" })
438 public void testMapColumnSelection_dnaToProtein() throws IOException
440 setupMappedAlignments();
442 ColumnSelection colsel = new ColumnSelection();
443 HiddenColumns hidden = new HiddenColumns();
446 * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
449 ColumnSelection cs = new ColumnSelection();
450 HiddenColumns hs = new HiddenColumns();
451 colsel.addElement(0);
452 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
454 assertEquals("[0, 1]", cs.getSelected().toString());
457 * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
458 * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
460 colsel.addElement(3);
461 colsel.addElement(4);
462 colsel.addElement(5);
464 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
466 assertEquals("[0, 1, 3]", cs.getSelected().toString());
469 @Test(groups = { "Functional" })
470 public void testMapColumnSelection_null() throws IOException
472 setupMappedAlignments();
473 ColumnSelection cs = new ColumnSelection();
474 HiddenColumns hs = new HiddenColumns();
475 MappingUtils.mapColumnSelection(null, null, dnaView, proteinView, cs,
477 assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
481 * Tests for the method that converts a series of [start, end] ranges to
484 @Test(groups = { "Functional" })
485 public void testFlattenRanges()
487 assertEquals("[1, 2, 3, 4]",
488 Arrays.toString(MappingUtils.flattenRanges(new int[]
490 assertEquals("[1, 2, 3, 4]",
491 Arrays.toString(MappingUtils.flattenRanges(new int[]
493 assertEquals("[1, 2, 3, 4]",
494 Arrays.toString(MappingUtils.flattenRanges(new int[]
495 { 1, 1, 2, 2, 3, 3, 4, 4 })));
496 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
497 Arrays.toString(MappingUtils.flattenRanges(new int[]
498 { 1, 4, 7, 9, 12, 12 })));
499 // trailing unpaired start position is ignored:
500 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
501 Arrays.toString(MappingUtils.flattenRanges(new int[]
502 { 1, 4, 7, 9, 12, 12, 15 })));
506 * Test mapping a sequence group made of entire columns.
508 * @throws IOException
510 @Test(groups = { "Functional" })
511 public void testMapSequenceGroup_columns() throws IOException
514 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
517 AlignmentI cdna = loadAlignment(
518 ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
520 cdna.setDataset(null);
521 AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
523 protein.setDataset(null);
524 AlignedCodonFrame acf = new AlignedCodonFrame();
525 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
526 for (int seq = 0; seq < 3; seq++)
528 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
529 protein.getSequenceAt(seq).getDatasetSequence(), map);
531 List<AlignedCodonFrame> acfList = Arrays
532 .asList(new AlignedCodonFrame[]
535 AlignViewportI dnaView = new AlignViewport(cdna);
536 AlignViewportI proteinView = new AlignViewport(protein);
537 protein.setCodonFrames(acfList);
540 * Select all sequences, column 2 in the protein
542 SequenceGroup sg = new SequenceGroup();
543 sg.setColourText(true);
544 sg.setIdColour(Color.GREEN);
545 sg.setOutlineColour(Color.LIGHT_GRAY);
546 sg.addSequence(protein.getSequenceAt(0), false);
547 sg.addSequence(protein.getSequenceAt(1), false);
548 sg.addSequence(protein.getSequenceAt(2), false);
553 * Verify the mapped sequence group in dna
555 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
556 proteinView, dnaView);
557 assertTrue(mappedGroup.getColourText());
558 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
559 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
560 assertEquals(3, mappedGroup.getSequences().size());
561 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
562 assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
563 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
564 assertEquals(3, mappedGroup.getStartRes());
565 assertEquals(5, mappedGroup.getEndRes());
568 * Verify mapping sequence group from dna to protein
571 sg.addSequence(cdna.getSequenceAt(0), false);
572 sg.addSequence(cdna.getSequenceAt(1), false);
573 sg.addSequence(cdna.getSequenceAt(2), false);
574 // select columns 2 and 3 in DNA which span protein columns 0 and 1
577 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
578 assertTrue(mappedGroup.getColourText());
579 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
580 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
581 assertEquals(3, mappedGroup.getSequences().size());
582 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
583 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
584 assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
585 assertEquals(0, mappedGroup.getStartRes());
586 assertEquals(1, mappedGroup.getEndRes());
590 * Test mapping a sequence group made of a sequences/columns region.
592 * @throws IOException
594 @Test(groups = { "Functional" })
595 public void testMapSequenceGroup_region() throws IOException
598 * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
601 AlignmentI cdna = loadAlignment(
602 ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
604 cdna.setDataset(null);
605 AlignmentI protein = loadAlignment(
606 ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n",
608 protein.setDataset(null);
609 AlignedCodonFrame acf = new AlignedCodonFrame();
610 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
611 for (int seq = 0; seq < 3; seq++)
613 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
614 protein.getSequenceAt(seq).getDatasetSequence(), map);
616 List<AlignedCodonFrame> acfList = Arrays
617 .asList(new AlignedCodonFrame[]
620 AlignViewportI dnaView = new AlignViewport(cdna);
621 AlignViewportI proteinView = new AlignViewport(protein);
622 protein.setCodonFrames(acfList);
625 * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
626 * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
627 * only includes a gap in Seq2 there is no mappable selection region in the
630 SequenceGroup sg = new SequenceGroup();
631 sg.setColourText(true);
632 sg.setIdColour(Color.GREEN);
633 sg.setOutlineColour(Color.LIGHT_GRAY);
634 sg.addSequence(protein.getSequenceAt(0), false);
635 sg.addSequence(protein.getSequenceAt(1), false);
640 * Verify the mapped sequence group in dna
642 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
643 proteinView, dnaView);
644 assertTrue(mappedGroup.getColourText());
645 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
646 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
647 assertEquals(1, mappedGroup.getSequences().size());
648 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
649 // Seq2 in protein has a gap in column 1 - ignored
650 // Seq1 has K which should map to columns 0-3 in Seq1
651 assertEquals(0, mappedGroup.getStartRes());
652 assertEquals(3, mappedGroup.getEndRes());
655 * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
656 * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
660 mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
661 assertEquals(1, mappedGroup.getStartRes());
662 assertEquals(13, mappedGroup.getEndRes());
665 * Verify mapping sequence group from dna to protein
668 sg.addSequence(cdna.getSequenceAt(0), false);
670 // select columns 4,5 - includes Seq1:codon2 (A) only
673 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
674 assertEquals(2, mappedGroup.getStartRes());
675 assertEquals(2, mappedGroup.getEndRes());
677 // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
678 sg.addSequence(cdna.getSequenceAt(1), false);
679 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
680 assertEquals(2, mappedGroup.getStartRes());
681 assertEquals(4, mappedGroup.getEndRes());
683 // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
684 sg.addSequence(cdna.getSequenceAt(2), false);
685 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
686 assertEquals(0, mappedGroup.getStartRes());
687 assertEquals(4, mappedGroup.getEndRes());
690 @Test(groups = { "Functional" })
691 public void testFindMappingsForSequence()
693 SequenceI seq1 = new Sequence("Seq1", "ABC");
694 SequenceI seq2 = new Sequence("Seq2", "ABC");
695 SequenceI seq3 = new Sequence("Seq3", "ABC");
696 SequenceI seq4 = new Sequence("Seq4", "ABC");
697 seq1.createDatasetSequence();
698 seq2.createDatasetSequence();
699 seq3.createDatasetSequence();
700 seq4.createDatasetSequence();
703 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
705 AlignedCodonFrame acf1 = new AlignedCodonFrame();
706 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
707 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
708 AlignedCodonFrame acf2 = new AlignedCodonFrame();
709 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
710 AlignedCodonFrame acf3 = new AlignedCodonFrame();
711 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
713 List<AlignedCodonFrame> mappings = new ArrayList<>();
719 * Seq1 has three mappings
721 List<AlignedCodonFrame> result = MappingUtils
722 .findMappingsForSequence(seq1, mappings);
723 assertEquals(3, result.size());
724 assertTrue(result.contains(acf1));
725 assertTrue(result.contains(acf2));
726 assertTrue(result.contains(acf3));
729 * Seq2 has two mappings
731 result = MappingUtils.findMappingsForSequence(seq2, mappings);
732 assertEquals(2, result.size());
733 assertTrue(result.contains(acf1));
734 assertTrue(result.contains(acf2));
737 * Seq3 has one mapping
739 result = MappingUtils.findMappingsForSequence(seq3, mappings);
740 assertEquals(1, result.size());
741 assertTrue(result.contains(acf3));
744 * Seq4 has no mappings
746 result = MappingUtils.findMappingsForSequence(seq4, mappings);
747 assertEquals(0, result.size());
749 result = MappingUtils.findMappingsForSequence(null, mappings);
750 assertEquals(0, result.size());
752 result = MappingUtils.findMappingsForSequence(seq1, null);
753 assertEquals(0, result.size());
755 result = MappingUtils.findMappingsForSequence(null, null);
756 assertEquals(0, result.size());
760 * just like the one above, but this time, we provide a set of sequences to
761 * subselect the mapping search
763 @Test(groups = { "Functional" })
764 public void testFindMappingsForSequenceAndOthers()
766 SequenceI seq1 = new Sequence("Seq1", "ABC");
767 SequenceI seq2 = new Sequence("Seq2", "ABC");
768 SequenceI seq3 = new Sequence("Seq3", "ABC");
769 SequenceI seq4 = new Sequence("Seq4", "ABC");
770 seq1.createDatasetSequence();
771 seq2.createDatasetSequence();
772 seq3.createDatasetSequence();
773 seq4.createDatasetSequence();
776 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4
778 AlignedCodonFrame acf1 = new AlignedCodonFrame();
779 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
780 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
781 AlignedCodonFrame acf2 = new AlignedCodonFrame();
782 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
783 AlignedCodonFrame acf3 = new AlignedCodonFrame();
784 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
785 AlignedCodonFrame acf4 = new AlignedCodonFrame();
786 acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map);
788 List<AlignedCodonFrame> mappings = new ArrayList<>();
797 List<AlignedCodonFrame> result = MappingUtils
798 .findMappingsForSequenceAndOthers(null, mappings,
799 Arrays.asList(new SequenceI[]
801 assertTrue(result.isEmpty());
803 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null,
804 Arrays.asList(new SequenceI[]
806 assertTrue(result.isEmpty());
809 * Seq1 has three mappings, but filter argument will only accept
812 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
813 Arrays.asList(new SequenceI[]
814 { seq1, seq2, seq1.getDatasetSequence() }));
815 assertEquals(2, result.size());
816 assertTrue(result.contains(acf1));
817 assertTrue(result.contains(acf2));
818 assertFalse("Did not expect to find mapping acf3 - subselect failed",
819 result.contains(acf3));
821 "Did not expect to find mapping acf4 - doesn't involve sequence",
822 result.contains(acf4));
825 * and verify the no filter case
827 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
829 assertEquals(3, result.size());
830 assertTrue(result.contains(acf1));
831 assertTrue(result.contains(acf2));
832 assertTrue(result.contains(acf3));
835 @Test(groups = { "Functional" })
836 public void testMapEditCommand()
838 SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
839 SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
840 dna.createDatasetSequence();
841 protein.createDatasetSequence();
842 AlignedCodonFrame acf = new AlignedCodonFrame();
843 MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3,
845 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
846 List<AlignedCodonFrame> mappings = new ArrayList<>();
849 AlignmentI prot = new Alignment(new SequenceI[] { protein });
850 prot.setCodonFrames(mappings);
851 AlignmentI nuc = new Alignment(new SequenceI[] { dna });
854 * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
855 * i.e. insert two gaps at column 4
857 EditCommand ec = new EditCommand();
858 final Edit edit = ec.new Edit(Action.INSERT_GAP,
860 { protein }, 4, 2, '-');
861 ec.appendEdit(edit, prot, true, null);
864 * the mapped edit command should be to insert 6 gaps before base 4 in the
865 * nucleotide sequence, which corresponds to aligned column 12 in the dna
867 EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
869 assertEquals(1, mappedEdit.getEdits().size());
870 Edit e = mappedEdit.getEdits().get(0);
871 assertEquals(1, e.getSequences().length);
872 assertEquals(dna, e.getSequences()[0]);
873 assertEquals(12, e.getPosition());
874 assertEquals(6, e.getNumber());
878 * Tests for the method that converts a series of [start, end] ranges to
879 * single positions, where the mapping is to a reverse strand i.e. start is
880 * greater than end point mapped to
882 @Test(groups = { "Functional" })
883 public void testFlattenRanges_reverseStrand()
885 assertEquals("[4, 3, 2, 1]",
886 Arrays.toString(MappingUtils.flattenRanges(new int[]
888 assertEquals("[4, 3, 2, 1]",
889 Arrays.toString(MappingUtils.flattenRanges(new int[]
891 assertEquals("[4, 3, 2, 1]",
892 Arrays.toString(MappingUtils.flattenRanges(new int[]
893 { 4, 4, 3, 3, 2, 2, 1, 1 })));
894 assertEquals("[12, 9, 8, 7, 4, 3, 2, 1]",
895 Arrays.toString(MappingUtils.flattenRanges(new int[]
896 { 12, 12, 9, 7, 4, 1 })));
897 // forwards and backwards anyone?
898 assertEquals("[4, 5, 6, 3, 2, 1]",
899 Arrays.toString(MappingUtils.flattenRanges(new int[]
901 // backwards and forwards
902 assertEquals("[3, 2, 1, 4, 5, 6]",
903 Arrays.toString(MappingUtils.flattenRanges(new int[]
905 // trailing unpaired start position is ignored:
906 assertEquals("[12, 9, 8, 7, 4, 3, 2]",
907 Arrays.toString(MappingUtils.flattenRanges(new int[]
908 { 12, 12, 9, 7, 4, 2, 1 })));
912 * Test mapping a column selection including hidden columns
914 * @throws IOException
916 @Test(groups = { "Functional" })
917 public void testMapColumnSelection_hiddenColumns() throws IOException
919 setupMappedAlignments();
921 ColumnSelection proteinSelection = new ColumnSelection();
922 HiddenColumns hiddenCols = new HiddenColumns();
925 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
926 * in dna respectively, overall 0-4
928 proteinSelection.hideSelectedColumns(0, hiddenCols);
929 ColumnSelection dnaSelection = new ColumnSelection();
930 HiddenColumns dnaHidden = new HiddenColumns();
931 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
932 proteinView, dnaView, dnaSelection, dnaHidden);
933 assertEquals("[]", dnaSelection.getSelected().toString());
934 Iterator<int[]> regions = dnaHidden.iterator();
935 assertEquals(1, dnaHidden.getNumberOfRegions());
936 assertEquals("[0, 4]", Arrays.toString(regions.next()));
939 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
941 dnaSelection = new ColumnSelection();
942 dnaHidden = new HiddenColumns();
943 hiddenCols.revealAllHiddenColumns(proteinSelection);
944 // the unhidden columns are now marked selected!
945 assertEquals("[0]", proteinSelection.getSelected().toString());
946 // deselect these or hideColumns will be expanded to include 0
947 proteinSelection.clear();
948 proteinSelection.hideSelectedColumns(1, hiddenCols);
949 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
950 proteinView, dnaView, dnaSelection, dnaHidden);
951 regions = dnaHidden.iterator();
952 assertEquals(1, dnaHidden.getNumberOfRegions());
953 assertEquals("[0, 3]", Arrays.toString(regions.next()));
956 * Column 2 in protein picks up gaps only - no mapping
958 dnaSelection = new ColumnSelection();
959 dnaHidden = new HiddenColumns();
960 hiddenCols.revealAllHiddenColumns(proteinSelection);
961 proteinSelection.clear();
962 proteinSelection.hideSelectedColumns(2, hiddenCols);
963 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
964 proteinView, dnaView, dnaSelection, dnaHidden);
965 assertEquals(0, dnaHidden.getNumberOfRegions());
968 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
969 * 6-9, 6-10, 5-8 respectively, overall to 5-10
971 dnaSelection = new ColumnSelection();
972 dnaHidden = new HiddenColumns();
973 hiddenCols.revealAllHiddenColumns(proteinSelection);
974 proteinSelection.clear();
975 proteinSelection.hideSelectedColumns(3, hiddenCols); // 5-10 hidden in dna
976 proteinSelection.addElement(1); // 0-3 selected in dna
977 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
978 proteinView, dnaView, dnaSelection, dnaHidden);
979 assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
980 regions = dnaHidden.iterator();
981 assertEquals(1, dnaHidden.getNumberOfRegions());
982 assertEquals("[5, 10]", Arrays.toString(regions.next()));
985 * Combine hiding columns 1 and 3 to get discontiguous hidden columns
987 dnaSelection = new ColumnSelection();
988 dnaHidden = new HiddenColumns();
989 hiddenCols.revealAllHiddenColumns(proteinSelection);
990 proteinSelection.clear();
991 proteinSelection.hideSelectedColumns(1, hiddenCols);
992 proteinSelection.hideSelectedColumns(3, hiddenCols);
993 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
994 proteinView, dnaView, dnaSelection, dnaHidden);
995 regions = dnaHidden.iterator();
996 assertEquals(2, dnaHidden.getNumberOfRegions());
997 assertEquals("[0, 3]", Arrays.toString(regions.next()));
998 assertEquals("[5, 10]", Arrays.toString(regions.next()));
1001 @Test(groups = { "Functional" })
1002 public void testGetLength()
1004 assertEquals(0, MappingUtils.getLength(null));
1007 * [start, end] ranges
1009 List<int[]> ranges = new ArrayList<>();
1010 assertEquals(0, MappingUtils.getLength(ranges));
1011 ranges.add(new int[] { 1, 1 });
1012 assertEquals(1, MappingUtils.getLength(ranges));
1013 ranges.add(new int[] { 2, 10 });
1014 assertEquals(10, MappingUtils.getLength(ranges));
1015 ranges.add(new int[] { 20, 10 });
1016 assertEquals(21, MappingUtils.getLength(ranges));
1019 * [start, end, start, end...] ranges
1022 ranges.add(new int[] { 1, 5, 8, 4 });
1023 ranges.add(new int[] { 8, 2 });
1024 ranges.add(new int[] { 12, 12 });
1025 assertEquals(18, MappingUtils.getLength(ranges));
1028 @Test(groups = { "Functional" })
1029 public void testContains()
1031 assertFalse(MappingUtils.contains(null, 1));
1032 List<int[]> ranges = new ArrayList<>();
1033 assertFalse(MappingUtils.contains(ranges, 1));
1035 ranges.add(new int[] { 1, 4 });
1036 ranges.add(new int[] { 6, 6 });
1037 ranges.add(new int[] { 8, 10 });
1038 ranges.add(new int[] { 30, 20 });
1039 ranges.add(new int[] { -16, -44 });
1041 assertFalse(MappingUtils.contains(ranges, 0));
1042 assertTrue(MappingUtils.contains(ranges, 1));
1043 assertTrue(MappingUtils.contains(ranges, 2));
1044 assertTrue(MappingUtils.contains(ranges, 3));
1045 assertTrue(MappingUtils.contains(ranges, 4));
1046 assertFalse(MappingUtils.contains(ranges, 5));
1048 assertTrue(MappingUtils.contains(ranges, 6));
1049 assertFalse(MappingUtils.contains(ranges, 7));
1051 assertTrue(MappingUtils.contains(ranges, 8));
1052 assertTrue(MappingUtils.contains(ranges, 9));
1053 assertTrue(MappingUtils.contains(ranges, 10));
1055 assertFalse(MappingUtils.contains(ranges, 31));
1056 assertTrue(MappingUtils.contains(ranges, 30));
1057 assertTrue(MappingUtils.contains(ranges, 29));
1058 assertTrue(MappingUtils.contains(ranges, 20));
1059 assertFalse(MappingUtils.contains(ranges, 19));
1061 assertFalse(MappingUtils.contains(ranges, -15));
1062 assertTrue(MappingUtils.contains(ranges, -16));
1063 assertTrue(MappingUtils.contains(ranges, -44));
1064 assertFalse(MappingUtils.contains(ranges, -45));
1068 * Test the method that drops positions from the start of a mapped range
1070 @Test(groups = "Functional")
1071 public void testRemoveStartPositions()
1073 int[] ranges = new int[] { 1, 10 };
1074 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1075 assertEquals("[1, 10]", Arrays.toString(adjusted));
1077 adjusted = MappingUtils.removeStartPositions(1, ranges);
1078 assertEquals("[2, 10]", Arrays.toString(adjusted));
1079 assertEquals("[1, 10]", Arrays.toString(ranges));
1082 adjusted = MappingUtils.removeStartPositions(1, ranges);
1083 assertEquals("[3, 10]", Arrays.toString(adjusted));
1084 assertEquals("[2, 10]", Arrays.toString(ranges));
1086 ranges = new int[] { 2, 3, 10, 12 };
1087 adjusted = MappingUtils.removeStartPositions(1, ranges);
1088 assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted));
1089 assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges));
1091 ranges = new int[] { 2, 2, 8, 12 };
1092 adjusted = MappingUtils.removeStartPositions(1, ranges);
1093 assertEquals("[8, 12]", Arrays.toString(adjusted));
1094 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1096 ranges = new int[] { 2, 2, 8, 12 };
1097 adjusted = MappingUtils.removeStartPositions(2, ranges);
1098 assertEquals("[9, 12]", Arrays.toString(adjusted));
1099 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1101 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1102 adjusted = MappingUtils.removeStartPositions(1, ranges);
1103 assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted));
1104 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1106 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1107 adjusted = MappingUtils.removeStartPositions(2, ranges);
1108 assertEquals("[9, 12]", Arrays.toString(adjusted));
1109 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1111 ranges = new int[] { 2, 3, 9, 12 };
1112 adjusted = MappingUtils.removeStartPositions(3, ranges);
1113 assertEquals("[10, 12]", Arrays.toString(adjusted));
1114 assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges));
1118 * Test the method that drops positions from the start of a mapped range, on
1119 * the reverse strand
1121 @Test(groups = "Functional")
1122 public void testRemoveStartPositions_reverseStrand()
1124 int[] ranges = new int[] { 10, 1 };
1125 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1126 assertEquals("[10, 1]", Arrays.toString(adjusted));
1127 assertEquals("[10, 1]", Arrays.toString(ranges));
1130 adjusted = MappingUtils.removeStartPositions(1, ranges);
1131 assertEquals("[9, 1]", Arrays.toString(adjusted));
1132 assertEquals("[10, 1]", Arrays.toString(ranges));
1135 adjusted = MappingUtils.removeStartPositions(1, ranges);
1136 assertEquals("[8, 1]", Arrays.toString(adjusted));
1137 assertEquals("[9, 1]", Arrays.toString(ranges));
1139 ranges = new int[] { 12, 11, 9, 6 };
1140 adjusted = MappingUtils.removeStartPositions(1, ranges);
1141 assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted));
1142 assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges));
1144 ranges = new int[] { 12, 12, 8, 4 };
1145 adjusted = MappingUtils.removeStartPositions(1, ranges);
1146 assertEquals("[8, 4]", Arrays.toString(adjusted));
1147 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1149 ranges = new int[] { 12, 12, 8, 4 };
1150 adjusted = MappingUtils.removeStartPositions(2, ranges);
1151 assertEquals("[7, 4]", Arrays.toString(adjusted));
1152 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1154 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1155 adjusted = MappingUtils.removeStartPositions(1, ranges);
1156 assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted));
1157 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1159 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1160 adjusted = MappingUtils.removeStartPositions(2, ranges);
1161 assertEquals("[8, 4]", Arrays.toString(adjusted));
1162 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1164 ranges = new int[] { 12, 11, 8, 4 };
1165 adjusted = MappingUtils.removeStartPositions(3, ranges);
1166 assertEquals("[7, 4]", Arrays.toString(adjusted));
1167 assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges));
1170 @Test(groups = { "Functional" })
1171 public void testRangeContains()
1174 * both forward ranges
1177 MappingUtils.rangeContains(new int[]
1178 { 1, 10 }, new int[] { 1, 10 }));
1180 MappingUtils.rangeContains(new int[]
1181 { 1, 10 }, new int[] { 2, 10 }));
1183 MappingUtils.rangeContains(new int[]
1184 { 1, 10 }, new int[] { 1, 9 }));
1186 MappingUtils.rangeContains(new int[]
1187 { 1, 10 }, new int[] { 4, 5 }));
1189 MappingUtils.rangeContains(new int[]
1190 { 1, 10 }, new int[] { 0, 9 }));
1192 MappingUtils.rangeContains(new int[]
1193 { 1, 10 }, new int[] { -10, -9 }));
1195 MappingUtils.rangeContains(new int[]
1196 { 1, 10 }, new int[] { 1, 11 }));
1198 MappingUtils.rangeContains(new int[]
1199 { 1, 10 }, new int[] { 11, 12 }));
1202 * forward range, reverse query
1205 MappingUtils.rangeContains(new int[]
1206 { 1, 10 }, new int[] { 10, 1 }));
1208 MappingUtils.rangeContains(new int[]
1209 { 1, 10 }, new int[] { 9, 1 }));
1211 MappingUtils.rangeContains(new int[]
1212 { 1, 10 }, new int[] { 10, 2 }));
1214 MappingUtils.rangeContains(new int[]
1215 { 1, 10 }, new int[] { 5, 5 }));
1217 MappingUtils.rangeContains(new int[]
1218 { 1, 10 }, new int[] { 11, 1 }));
1220 MappingUtils.rangeContains(new int[]
1221 { 1, 10 }, new int[] { 10, 0 }));
1224 * reverse range, forward query
1227 MappingUtils.rangeContains(new int[]
1228 { 10, 1 }, new int[] { 1, 10 }));
1230 MappingUtils.rangeContains(new int[]
1231 { 10, 1 }, new int[] { 1, 9 }));
1233 MappingUtils.rangeContains(new int[]
1234 { 10, 1 }, new int[] { 2, 10 }));
1236 MappingUtils.rangeContains(new int[]
1237 { 10, 1 }, new int[] { 6, 6 }));
1239 MappingUtils.rangeContains(new int[]
1240 { 10, 1 }, new int[] { 6, 11 }));
1242 MappingUtils.rangeContains(new int[]
1243 { 10, 1 }, new int[] { 11, 20 }));
1245 MappingUtils.rangeContains(new int[]
1246 { 10, 1 }, new int[] { -3, -2 }));
1252 MappingUtils.rangeContains(new int[]
1253 { 10, 1 }, new int[] { 10, 1 }));
1255 MappingUtils.rangeContains(new int[]
1256 { 10, 1 }, new int[] { 9, 1 }));
1258 MappingUtils.rangeContains(new int[]
1259 { 10, 1 }, new int[] { 10, 2 }));
1261 MappingUtils.rangeContains(new int[]
1262 { 10, 1 }, new int[] { 3, 3 }));
1264 MappingUtils.rangeContains(new int[]
1265 { 10, 1 }, new int[] { 11, 1 }));
1267 MappingUtils.rangeContains(new int[]
1268 { 10, 1 }, new int[] { 10, 0 }));
1270 MappingUtils.rangeContains(new int[]
1271 { 10, 1 }, new int[] { 12, 11 }));
1273 MappingUtils.rangeContains(new int[]
1274 { 10, 1 }, new int[] { -5, -8 }));
1280 MappingUtils.rangeContains(new int[]
1281 { 1, 10, 12 }, new int[] { 1, 10 }));
1283 MappingUtils.rangeContains(new int[]
1284 { 1, 10 }, new int[] { 1 }));
1285 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, null));
1286 assertFalse(MappingUtils.rangeContains(null, new int[] { 1, 10 }));
1289 @Test(groups = "Functional")
1290 public void testRemoveEndPositions()
1292 List<int[]> ranges = new ArrayList<>();
1295 * case 1: truncate last range
1297 ranges.add(new int[] { 1, 10 });
1298 ranges.add(new int[] { 20, 30 });
1299 MappingUtils.removeEndPositions(5, ranges);
1300 assertEquals(2, ranges.size());
1301 assertEquals(25, ranges.get(1)[1]);
1304 * case 2: remove last range
1307 ranges.add(new int[] { 1, 10 });
1308 ranges.add(new int[] { 20, 22 });
1309 MappingUtils.removeEndPositions(3, ranges);
1310 assertEquals(1, ranges.size());
1311 assertEquals(10, ranges.get(0)[1]);
1314 * case 3: truncate penultimate range
1317 ranges.add(new int[] { 1, 10 });
1318 ranges.add(new int[] { 20, 21 });
1319 MappingUtils.removeEndPositions(3, ranges);
1320 assertEquals(1, ranges.size());
1321 assertEquals(9, ranges.get(0)[1]);
1324 * case 4: remove last two ranges
1327 ranges.add(new int[] { 1, 10 });
1328 ranges.add(new int[] { 20, 20 });
1329 ranges.add(new int[] { 30, 30 });
1330 MappingUtils.removeEndPositions(3, ranges);
1331 assertEquals(1, ranges.size());
1332 assertEquals(9, ranges.get(0)[1]);
1335 @Test(groups = "Functional")
1336 public void testListToArray()
1338 List<int[]> ranges = new ArrayList<>();
1340 int[] result = MappingUtils.listToArray(ranges);
1341 assertEquals(result.length, 0);
1342 ranges.add(new int[] {24, 12});
1343 result = MappingUtils.listToArray(ranges);
1344 assertEquals(result.length, 2);
1345 assertEquals(result[0], 24);
1346 assertEquals(result[1], 12);
1347 ranges.add(new int[] {-7, 30});
1348 result = MappingUtils.listToArray(ranges);
1349 assertEquals(result.length, 4);
1350 assertEquals(result[0], 24);
1351 assertEquals(result[1], 12);
1352 assertEquals(result[2], -7);
1353 assertEquals(result[3], 30);
1356 MappingUtils.listToArray(null);
1357 fail("Expected exception");
1358 } catch (NullPointerException e)
1365 * Test mapping a sequence group where sequences in and outside the group
1366 * share a dataset sequence (e.g. alternative CDS for the same gene)
1368 * This scenario doesn't arise after JAL-3763 changes, but test left as still valid
1369 * @throws IOException
1371 @Test(groups = { "Functional" })
1372 public void testMapSequenceGroup_sharedDataset() throws IOException
1375 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
1376 * viewport). CDS sequences share the same 'gene' dataset sequence.
1378 SequenceI dna = new Sequence("dna", "aaatttgggcccaaatttgggccc");
1379 SequenceI cds1 = new Sequence("cds1/1-6", "aaattt");
1380 SequenceI cds2 = new Sequence("cds1/4-9", "tttggg");
1381 SequenceI cds3 = new Sequence("cds1/19-24", "gggccc");
1383 cds1.setDatasetSequence(dna);
1384 cds2.setDatasetSequence(dna);
1385 cds3.setDatasetSequence(dna);
1387 SequenceI pep1 = new Sequence("pep1", "KF");
1388 SequenceI pep2 = new Sequence("pep2", "FG");
1389 SequenceI pep3 = new Sequence("pep3", "GP");
1390 pep1.createDatasetSequence();
1391 pep2.createDatasetSequence();
1392 pep3.createDatasetSequence();
1395 * add mappings from coding positions of dna to respective peptides
1397 AlignedCodonFrame acf = new AlignedCodonFrame();
1398 acf.addMap(dna, pep1,
1399 new MapList(new int[]
1400 { 1, 6 }, new int[] { 1, 2 }, 3, 1));
1401 acf.addMap(dna, pep2,
1402 new MapList(new int[]
1403 { 4, 9 }, new int[] { 1, 2 }, 3, 1));
1404 acf.addMap(dna, pep3,
1405 new MapList(new int[]
1406 { 19, 24 }, new int[] { 1, 2 }, 3, 1));
1408 List<AlignedCodonFrame> acfList = Arrays
1409 .asList(new AlignedCodonFrame[]
1412 AlignmentI cdna = new Alignment(new SequenceI[] { cds1, cds2, cds3 });
1413 AlignmentI protein = new Alignment(
1415 { pep1, pep2, pep3 });
1416 AlignViewportI cdnaView = new AlignViewport(cdna);
1417 AlignViewportI peptideView = new AlignViewport(protein);
1418 protein.setCodonFrames(acfList);
1421 * Select pep1 and pep3 in the protein alignment
1423 SequenceGroup sg = new SequenceGroup();
1424 sg.setColourText(true);
1425 sg.setIdColour(Color.GREEN);
1426 sg.setOutlineColour(Color.LIGHT_GRAY);
1427 sg.addSequence(pep1, false);
1428 sg.addSequence(pep3, false);
1429 sg.setEndRes(protein.getWidth() - 1);
1432 * Verify the mapped sequence group in dna is cds1 and cds3
1434 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
1435 peptideView, cdnaView);
1436 assertTrue(mappedGroup.getColourText());
1437 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
1438 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
1439 assertEquals(2, mappedGroup.getSequences().size());
1440 assertSame(cds1, mappedGroup.getSequences().get(0));
1441 assertSame(cds3, mappedGroup.getSequences().get(1));
1442 // columns 1-6 selected (0-5 base zero)
1443 assertEquals(0, mappedGroup.getStartRes());
1444 assertEquals(5, mappedGroup.getEndRes());
1447 * Select mapping sequence group from dna to protein
1450 sg.addSequence(cds2, false);
1451 sg.addSequence(cds1, false);
1453 sg.setEndRes(cdna.getWidth() - 1);
1454 mappedGroup = MappingUtils.mapSequenceGroup(sg, cdnaView, peptideView);
1455 assertTrue(mappedGroup.getColourText());
1456 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
1457 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
1458 assertEquals(2, mappedGroup.getSequences().size());
1459 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
1460 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
1461 assertEquals(0, mappedGroup.getStartRes());
1462 assertEquals(1, mappedGroup.getEndRes()); // two columns
1465 @Test(groups = "Functional")
1466 public void testFindOverlap()
1468 List<int[]> ranges = new ArrayList<>();
1469 ranges.add(new int[] {4, 8});
1470 ranges.add(new int[] {10, 12});
1471 ranges.add(new int[] {16, 19});
1473 int[] overlap = MappingUtils.findOverlap(ranges, 5, 13);
1474 assertArrayEquals(overlap, new int[] {5, 12});
1475 overlap = MappingUtils.findOverlap(ranges, -100, 100);
1476 assertArrayEquals(overlap, new int[] {4, 19});
1477 overlap = MappingUtils.findOverlap(ranges, 7, 17);
1478 assertArrayEquals(overlap, new int[] {7, 17});
1479 overlap = MappingUtils.findOverlap(ranges, 13, 15);
1480 assertNull(overlap);