JAL-1925 update source version in license
[jalview.git] / test / jalview / util / MappingUtilsTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3  * Copyright (C) 2015 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.util;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertSame;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.api.AlignViewportI;
28 import jalview.commands.EditCommand;
29 import jalview.commands.EditCommand.Action;
30 import jalview.commands.EditCommand.Edit;
31 import jalview.datamodel.AlignedCodonFrame;
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.ColumnSelection;
35 import jalview.datamodel.SearchResults;
36 import jalview.datamodel.SearchResults.Match;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.datamodel.SequenceI;
40 import jalview.gui.AlignViewport;
41 import jalview.io.AppletFormatAdapter;
42 import jalview.io.FormatAdapter;
43
44 import java.awt.Color;
45 import java.io.IOException;
46 import java.util.Arrays;
47 import java.util.Collections;
48 import java.util.HashSet;
49 import java.util.LinkedHashSet;
50 import java.util.List;
51 import java.util.Set;
52
53 import org.testng.annotations.Test;
54
55 public class MappingUtilsTest
56 {
57   private AlignViewportI dnaView;
58
59   private AlignViewportI proteinView;
60
61   /**
62    * Simple test of mapping with no intron involved.
63    */
64   @Test(groups = { "Functional" })
65   public void testBuildSearchResults()
66   {
67     final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
68     seq1.createDatasetSequence();
69
70     final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
71     aseq1.createDatasetSequence();
72
73     /*
74      * Map dna bases 5-10 to protein residues 12-13
75      */
76     AlignedCodonFrame acf = new AlignedCodonFrame();
77     MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
78             1);
79     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
80     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
81
82     /*
83      * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
84      */
85     SearchResults sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
86     assertEquals(1, sr.getResults().size());
87     Match m = sr.getResults().get(0);
88     assertEquals(seq1.getDatasetSequence(), m.getSequence());
89     assertEquals(5, m.getStart());
90     assertEquals(7, m.getEnd());
91     sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
92     assertEquals(1, sr.getResults().size());
93     m = sr.getResults().get(0);
94     assertEquals(seq1.getDatasetSequence(), m.getSequence());
95     assertEquals(8, m.getStart());
96     assertEquals(10, m.getEnd());
97
98     /*
99      * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
100      */
101     for (int i = 5; i < 11; i++)
102     {
103       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
104       assertEquals(1, sr.getResults().size());
105       m = sr.getResults().get(0);
106       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
107       int residue = i > 7 ? 13 : 12;
108       assertEquals(residue, m.getStart());
109       assertEquals(residue, m.getEnd());
110     }
111   }
112
113   /**
114    * Simple test of mapping with introns involved.
115    */
116   @Test(groups = { "Functional" })
117   public void testBuildSearchResults_withIntron()
118   {
119     final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
120     seq1.createDatasetSequence();
121
122     final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
123     aseq1.createDatasetSequence();
124
125     /*
126      * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
127      */
128     AlignedCodonFrame acf = new AlignedCodonFrame();
129     MapList map = new MapList(new int[] { 6, 6, 8, 9, 11, 11, 13, 13, 15,
130         15 }, new int[] { 8, 9 }, 3, 1);
131     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
132     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
133
134     /*
135      * Check protein residue 8 maps to [6, 8, 9]
136      */
137     SearchResults sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
138     assertEquals(2, sr.getResults().size());
139     Match m = sr.getResults().get(0);
140     assertEquals(seq1.getDatasetSequence(), m.getSequence());
141     assertEquals(6, m.getStart());
142     assertEquals(6, m.getEnd());
143     m = sr.getResults().get(1);
144     assertEquals(seq1.getDatasetSequence(), m.getSequence());
145     assertEquals(8, m.getStart());
146     assertEquals(9, m.getEnd());
147
148     /*
149      * Check protein residue 9 maps to [11, 13, 15]
150      */
151     sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
152     assertEquals(3, sr.getResults().size());
153     m = sr.getResults().get(0);
154     assertEquals(seq1.getDatasetSequence(), m.getSequence());
155     assertEquals(11, m.getStart());
156     assertEquals(11, m.getEnd());
157     m = sr.getResults().get(1);
158     assertEquals(seq1.getDatasetSequence(), m.getSequence());
159     assertEquals(13, m.getStart());
160     assertEquals(13, m.getEnd());
161     m = sr.getResults().get(2);
162     assertEquals(seq1.getDatasetSequence(), m.getSequence());
163     assertEquals(15, m.getStart());
164     assertEquals(15, m.getEnd());
165
166     /*
167      * Check inverse mappings, from codons to protein
168      */
169     for (int i = 5; i < 18; i++)
170     {
171       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
172       int residue = (i == 6 || i == 8 || i == 9) ? 8 : (i == 11 || i == 13
173               || i == 15 ? 9 : 0);
174       if (residue == 0)
175       {
176         assertEquals(0, sr.getResults().size());
177         continue;
178       }
179       assertEquals(1, sr.getResults().size());
180       m = sr.getResults().get(0);
181       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
182       assertEquals(residue, m.getStart());
183       assertEquals(residue, m.getEnd());
184     }
185   }
186
187   /**
188    * Test mapping a sequence group made of entire sequences.
189    * 
190    * @throws IOException
191    */
192   @Test(groups = { "Functional" })
193   public void testMapSequenceGroup_sequences() throws IOException
194   {
195     /*
196      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
197      * viewport).
198      */
199     AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
200             "FASTA");
201     cdna.setDataset(null);
202     AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
203             "FASTA");
204     protein.setDataset(null);
205     AlignedCodonFrame acf = new AlignedCodonFrame();
206     MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
207     for (int seq = 0; seq < 3; seq++)
208     {
209       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
210               .getSequenceAt(seq).getDatasetSequence(), map);
211     }
212     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
213
214     AlignViewportI dnaView = new AlignViewport(cdna);
215     AlignViewportI proteinView = new AlignViewport(protein);
216     protein.setCodonFrames(acfList);
217
218     /*
219      * Select Seq1 and Seq3 in the protein (startRes=endRes=0)
220      */
221     SequenceGroup sg = new SequenceGroup();
222     sg.setColourText(true);
223     sg.setIdColour(Color.GREEN);
224     sg.setOutlineColour(Color.LIGHT_GRAY);
225     sg.addSequence(protein.getSequenceAt(0), false);
226     sg.addSequence(protein.getSequenceAt(2), false);
227
228     /*
229      * Verify the mapped sequence group in dna
230      */
231     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
232             proteinView, dnaView);
233     assertTrue(mappedGroup.getColourText());
234     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
235     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
236     assertEquals(2, mappedGroup.getSequences().size());
237     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
238     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
239     assertEquals(0, mappedGroup.getStartRes());
240     assertEquals(2, mappedGroup.getEndRes());
241
242     /*
243      * Verify mapping sequence group from dna to protein
244      */
245     sg.clear();
246     sg.addSequence(cdna.getSequenceAt(1), false);
247     sg.addSequence(cdna.getSequenceAt(0), false);
248     sg.setStartRes(0);
249     sg.setEndRes(2);
250     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
251     assertTrue(mappedGroup.getColourText());
252     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
253     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
254     assertEquals(2, mappedGroup.getSequences().size());
255     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
256     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
257     assertEquals(0, mappedGroup.getStartRes());
258     assertEquals(0, mappedGroup.getEndRes());
259   }
260
261   /**
262    * Helper method to load an alignment and ensure dataset sequences are set up.
263    * 
264    * @param data
265    * @param format
266    *          TODO
267    * @return
268    * @throws IOException
269    */
270   protected AlignmentI loadAlignment(final String data, String format)
271           throws IOException
272   {
273     AlignmentI a = new FormatAdapter().readFile(data,
274             AppletFormatAdapter.PASTE, format);
275     a.setDataset(null);
276     return a;
277   }
278
279   /**
280    * Test mapping a column selection in protein to its dna equivalent
281    * 
282    * @throws IOException
283    */
284   @Test(groups = { "Functional" })
285   public void testMapColumnSelection_proteinToDna() throws IOException
286   {
287     setupMappedAlignments();
288
289     ColumnSelection colsel = new ColumnSelection();
290
291     /*
292      * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
293      * in dna respectively, overall 0-4
294      */
295     colsel.addElement(0);
296     ColumnSelection cs = MappingUtils.mapColumnSelection(colsel,
297             proteinView, dnaView);
298     assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
299
300     /*
301      * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
302      */
303     colsel.clear();
304     colsel.addElement(1);
305     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
306     assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
307
308     /*
309      * Column 2 in protein picks up gaps only - no mapping
310      */
311     colsel.clear();
312     colsel.addElement(2);
313     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
314     assertEquals("[]", cs.getSelected().toString());
315
316     /*
317      * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
318      * 6-9, 6-10, 5-8 respectively, overall to 5-10
319      */
320     colsel.clear();
321     colsel.addElement(3);
322     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
323     assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
324
325     /*
326      * Combine selection of columns 1 and 3 to get a discontiguous mapped
327      * selection
328      */
329     colsel.clear();
330     colsel.addElement(1);
331     colsel.addElement(3);
332     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
333     assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", cs.getSelected()
334             .toString());
335   }
336
337   /**
338    * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
339    * offset start positions for a more general test case.
340    * 
341    * @throws IOException
342    */
343   protected void setupMappedAlignments() throws IOException
344   {
345     /*
346      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
347      * viewport). Lower case for introns.
348      */
349     AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
350             + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
351             "FASTA");
352     cdna.setDataset(null);
353     AlignmentI protein = loadAlignment(
354             ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
355             "FASTA");
356     protein.setDataset(null);
357
358     // map first dna to first protein seq
359     AlignedCodonFrame acf = new AlignedCodonFrame();
360     MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
361             new int[] { 40, 41 }, 3, 1);
362     acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein
363             .getSequenceAt(0).getDatasetSequence(), map);
364
365     // map second dna to second protein seq
366     map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 }, new int[] { 50,
367         51 }, 3, 1);
368     acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein
369             .getSequenceAt(1).getDatasetSequence(), map);
370
371     // map third dna to third protein seq
372     map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 }, new int[] { 60,
373         61 }, 3, 1);
374     acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
375             .getSequenceAt(2).getDatasetSequence(), map);
376     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
377
378     dnaView = new AlignViewport(cdna);
379     proteinView = new AlignViewport(protein);
380     protein.setCodonFrames(acfList);
381   }
382
383   /**
384    * Test mapping a column selection in dna to its protein equivalent
385    * 
386    * @throws IOException
387    */
388   @Test(groups = { "Functional" })
389   public void testMapColumnSelection_dnaToProtein() throws IOException
390   {
391     setupMappedAlignments();
392
393     ColumnSelection colsel = new ColumnSelection();
394
395     /*
396      * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
397      * in protein.
398      */
399     colsel.addElement(0);
400     ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, dnaView,
401             proteinView);
402     assertEquals("[0, 1]", cs.getSelected().toString());
403
404     /*
405      * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
406      * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
407      */
408     colsel.addElement(3);
409     colsel.addElement(4);
410     colsel.addElement(5);
411     cs = MappingUtils.mapColumnSelection(colsel, dnaView, proteinView);
412     assertEquals("[0, 1, 3]", cs.getSelected().toString());
413   }
414
415   @Test(groups = { "Functional" })
416   public void testMapColumnSelection_null() throws IOException
417   {
418     setupMappedAlignments();
419     ColumnSelection cs = MappingUtils.mapColumnSelection(null, dnaView,
420             proteinView);
421     assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
422   }
423
424   /**
425    * Tests for the method that converts a series of [start, end] ranges to
426    * single positions
427    */
428   @Test(groups = { "Functional" })
429   public void testFlattenRanges()
430   {
431     assertEquals("[1, 2, 3, 4]",
432             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4 })));
433     assertEquals(
434             "[1, 2, 3, 4]",
435             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 2, 3,
436                 4 })));
437     assertEquals(
438             "[1, 2, 3, 4]",
439             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 1, 2,
440                 2, 3, 3, 4, 4 })));
441     assertEquals(
442             "[1, 2, 3, 4, 7, 8, 9, 12]",
443             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
444                 9, 12, 12 })));
445     // unpaired start position is ignored:
446     assertEquals(
447             "[1, 2, 3, 4, 7, 8, 9, 12]",
448             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
449                 9, 12, 12, 15 })));
450   }
451
452   /**
453    * Test mapping a sequence group made of entire columns.
454    * 
455    * @throws IOException
456    */
457   @Test(groups = { "Functional" })
458   public void testMapSequenceGroup_columns() throws IOException
459   {
460     /*
461      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
462      * viewport).
463      */
464     AlignmentI cdna = loadAlignment(
465             ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n", "FASTA");
466     cdna.setDataset(null);
467     AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
468             "FASTA");
469     protein.setDataset(null);
470     AlignedCodonFrame acf = new AlignedCodonFrame();
471     MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
472     for (int seq = 0; seq < 3; seq++)
473     {
474       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
475               .getSequenceAt(seq).getDatasetSequence(), map);
476     }
477     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
478
479     AlignViewportI dnaView = new AlignViewport(cdna);
480     AlignViewportI proteinView = new AlignViewport(protein);
481     protein.setCodonFrames(acfList);
482
483     /*
484      * Select all sequences, column 2 in the protein
485      */
486     SequenceGroup sg = new SequenceGroup();
487     sg.setColourText(true);
488     sg.setIdColour(Color.GREEN);
489     sg.setOutlineColour(Color.LIGHT_GRAY);
490     sg.addSequence(protein.getSequenceAt(0), false);
491     sg.addSequence(protein.getSequenceAt(1), false);
492     sg.addSequence(protein.getSequenceAt(2), false);
493     sg.setStartRes(1);
494     sg.setEndRes(1);
495
496     /*
497      * Verify the mapped sequence group in dna
498      */
499     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
500             proteinView, dnaView);
501     assertTrue(mappedGroup.getColourText());
502     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
503     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
504     assertEquals(3, mappedGroup.getSequences().size());
505     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
506     assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
507     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
508     assertEquals(3, mappedGroup.getStartRes());
509     assertEquals(5, mappedGroup.getEndRes());
510
511     /*
512      * Verify mapping sequence group from dna to protein
513      */
514     sg.clear();
515     sg.addSequence(cdna.getSequenceAt(0), false);
516     sg.addSequence(cdna.getSequenceAt(1), false);
517     sg.addSequence(cdna.getSequenceAt(2), false);
518     // select columns 2 and 3 in DNA which span protein columns 0 and 1
519     sg.setStartRes(2);
520     sg.setEndRes(3);
521     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
522     assertTrue(mappedGroup.getColourText());
523     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
524     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
525     assertEquals(3, mappedGroup.getSequences().size());
526     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
527     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
528     assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
529     assertEquals(0, mappedGroup.getStartRes());
530     assertEquals(1, mappedGroup.getEndRes());
531   }
532
533   /**
534    * Test mapping a sequence group made of a sequences/columns region.
535    * 
536    * @throws IOException
537    */
538   @Test(groups = { "Functional" })
539   public void testMapSequenceGroup_region() throws IOException
540   {
541     /*
542      * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
543      * viewport).
544      */
545     AlignmentI cdna = loadAlignment(
546             ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
547             "FASTA");
548     cdna.setDataset(null);
549     AlignmentI protein = loadAlignment(
550             ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", "FASTA");
551     protein.setDataset(null);
552     AlignedCodonFrame acf = new AlignedCodonFrame();
553     MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
554     for (int seq = 0; seq < 3; seq++)
555     {
556       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
557               .getSequenceAt(seq).getDatasetSequence(), map);
558     }
559     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
560
561     AlignViewportI dnaView = new AlignViewport(cdna);
562     AlignViewportI proteinView = new AlignViewport(protein);
563     protein.setCodonFrames(acfList);
564
565     /*
566      * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
567      * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
568      * only includes a gap in Seq2 there is no mappable selection region in the
569      * corresponding DNA.
570      */
571     SequenceGroup sg = new SequenceGroup();
572     sg.setColourText(true);
573     sg.setIdColour(Color.GREEN);
574     sg.setOutlineColour(Color.LIGHT_GRAY);
575     sg.addSequence(protein.getSequenceAt(0), false);
576     sg.addSequence(protein.getSequenceAt(1), false);
577     sg.setStartRes(1);
578     sg.setEndRes(1);
579
580     /*
581      * Verify the mapped sequence group in dna
582      */
583     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
584             proteinView, dnaView);
585     assertTrue(mappedGroup.getColourText());
586     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
587     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
588     assertEquals(1, mappedGroup.getSequences().size());
589     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
590     // Seq2 in protein has a gap in column 1 - ignored
591     // Seq1 has K which should map to columns 0-3 in Seq1
592     assertEquals(0, mappedGroup.getStartRes());
593     assertEquals(3, mappedGroup.getEndRes());
594
595     /*
596      * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
597      * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
598      */
599     sg.setStartRes(2);
600     sg.setEndRes(4);
601     mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
602     assertEquals(1, mappedGroup.getStartRes());
603     assertEquals(13, mappedGroup.getEndRes());
604
605     /*
606      * Verify mapping sequence group from dna to protein
607      */
608     sg.clear();
609     sg.addSequence(cdna.getSequenceAt(0), false);
610
611     // select columns 4,5 - includes Seq1:codon2 (A) only
612     sg.setStartRes(4);
613     sg.setEndRes(5);
614     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
615     assertEquals(2, mappedGroup.getStartRes());
616     assertEquals(2, mappedGroup.getEndRes());
617
618     // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
619     sg.addSequence(cdna.getSequenceAt(1), false);
620     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
621     assertEquals(2, mappedGroup.getStartRes());
622     assertEquals(4, mappedGroup.getEndRes());
623
624     // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
625     sg.addSequence(cdna.getSequenceAt(2), false);
626     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
627     assertEquals(0, mappedGroup.getStartRes());
628     assertEquals(4, mappedGroup.getEndRes());
629   }
630
631   @Test(groups = { "Functional" })
632   public void testFindMappingsForSequence()
633   {
634     SequenceI seq1 = new Sequence("Seq1", "ABC");
635     SequenceI seq2 = new Sequence("Seq2", "ABC");
636     SequenceI seq3 = new Sequence("Seq3", "ABC");
637     SequenceI seq4 = new Sequence("Seq4", "ABC");
638     seq1.createDatasetSequence();
639     seq2.createDatasetSequence();
640     seq3.createDatasetSequence();
641     seq4.createDatasetSequence();
642
643     /*
644      * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
645      */
646     AlignedCodonFrame acf1 = new AlignedCodonFrame();
647     MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
648     acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
649     AlignedCodonFrame acf2 = new AlignedCodonFrame();
650     acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
651     AlignedCodonFrame acf3 = new AlignedCodonFrame();
652     acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
653
654     Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
655     mappings.add(acf1);
656     mappings.add(acf2);
657     mappings.add(acf3);
658
659     /*
660      * Seq1 has three mappings
661      */
662     List<AlignedCodonFrame> result = MappingUtils.findMappingsForSequence(
663             seq1, mappings);
664     assertEquals(3, result.size());
665     assertTrue(result.contains(acf1));
666     assertTrue(result.contains(acf2));
667     assertTrue(result.contains(acf3));
668
669     /*
670      * Seq2 has two mappings
671      */
672     result = MappingUtils.findMappingsForSequence(seq2, mappings);
673     assertEquals(2, result.size());
674     assertTrue(result.contains(acf1));
675     assertTrue(result.contains(acf2));
676
677     /*
678      * Seq3 has one mapping
679      */
680     result = MappingUtils.findMappingsForSequence(seq3, mappings);
681     assertEquals(1, result.size());
682     assertTrue(result.contains(acf3));
683
684     /*
685      * Seq4 has no mappings
686      */
687     result = MappingUtils.findMappingsForSequence(seq4, mappings);
688     assertEquals(0, result.size());
689
690     result = MappingUtils.findMappingsForSequence(null, mappings);
691     assertEquals(0, result.size());
692
693     result = MappingUtils.findMappingsForSequence(seq1, null);
694     assertEquals(0, result.size());
695
696     result = MappingUtils.findMappingsForSequence(null, null);
697     assertEquals(0, result.size());
698   }
699
700   @Test(groups = { "Functional" })
701   public void testMapEditCommand()
702   {
703     SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
704     SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
705     dna.createDatasetSequence();
706     protein.createDatasetSequence();
707     AlignedCodonFrame acf = new AlignedCodonFrame();
708     MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3, 1);
709     acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
710     Set<AlignedCodonFrame> mappings = new LinkedHashSet<AlignedCodonFrame>();
711     mappings.add(acf);
712
713     AlignmentI prot = new Alignment(new SequenceI[] { protein });
714     prot.setCodonFrames(mappings);
715     AlignmentI nuc = new Alignment(new SequenceI[] { dna });
716
717     /*
718      * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
719      * i.e. insert two gaps at column 4
720      */
721     EditCommand ec = new EditCommand();
722     final Edit edit = ec.new Edit(Action.INSERT_GAP,
723             new SequenceI[] { protein }, 4, 2, '-');
724     ec.appendEdit(edit, prot, true, null);
725
726     /*
727      * the mapped edit command should be to insert 6 gaps before base 4 in the
728      * nucleotide sequence, which corresponds to aligned column 12 in the dna
729      */
730     EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
731             '-', mappings);
732     assertEquals(1, mappedEdit.getEdits().size());
733     Edit e = mappedEdit.getEdits().get(0);
734     assertEquals(1, e.getSequences().length);
735     assertEquals(dna, e.getSequences()[0]);
736     assertEquals(12, e.getPosition());
737     assertEquals(6, e.getNumber());
738   }
739 }