Merge develop to Release_2_8_3_Branch
[jalview.git] / test / jalview / util / MappingUtilsTest.java
1 package jalview.util;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertSame;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
7 import jalview.api.AlignViewportI;
8 import jalview.datamodel.AlignedCodonFrame;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.ColumnSelection;
12 import jalview.datamodel.SearchResults;
13 import jalview.datamodel.SearchResults.Match;
14 import jalview.datamodel.Sequence;
15 import jalview.datamodel.SequenceGroup;
16 import jalview.gui.AlignViewport;
17 import jalview.io.AppletFormatAdapter;
18 import jalview.io.FormatAdapter;
19
20 import java.awt.Color;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.Set;
25
26 import org.junit.Test;
27
28 public class MappingUtilsTest
29 {
30   private AlignViewportI dnaView;
31   private AlignViewportI proteinView;
32
33   /**
34    * Simple test of mapping with no intron involved.
35    */
36   @Test
37   public void testBuildSearchResults()
38   {
39     final Sequence seq1 = new Sequence("Seq1", "C-G-TA-GC");
40     seq1.createDatasetSequence();
41
42     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
43     aseq1.createDatasetSequence();
44
45     /*
46      * Map dna bases 1-6 to protein residues 1-2
47      */
48     AlignedCodonFrame acf = new AlignedCodonFrame();
49     MapList map = new MapList(new int[]
50     { 1, 6 }, new int[]
51     { 1, 2 }, 3, 1);
52     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
53     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
54
55     /*
56      * Check protein residue 1 maps to codon 1-3, 2 to codon 4-6
57      */
58     SearchResults sr = MappingUtils.buildSearchResults(aseq1, 1, acfList);
59     assertEquals(1, sr.getResults().size());
60     Match m = sr.getResults().get(0);
61     assertEquals(seq1.getDatasetSequence(), m.getSequence());
62     assertEquals(1, m.getStart());
63     assertEquals(3, m.getEnd());
64     sr = MappingUtils.buildSearchResults(aseq1, 2, acfList);
65     assertEquals(1, sr.getResults().size());
66     m = sr.getResults().get(0);
67     assertEquals(seq1.getDatasetSequence(), m.getSequence());
68     assertEquals(4, m.getStart());
69     assertEquals(6, m.getEnd());
70
71     /*
72      * Check inverse mappings, from codons 1-3, 4-6 to protein 1, 2
73      */
74     for (int i = 1; i < 7; i++)
75     {
76       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
77       assertEquals(1, sr.getResults().size());
78       m = sr.getResults().get(0);
79       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
80       int residue = i > 3 ? 2 : 1;
81       assertEquals(residue, m.getStart());
82       assertEquals(residue, m.getEnd());
83     }
84   }
85
86   /**
87    * Simple test of mapping with introns involved.
88    */
89   @Test
90   public void testBuildSearchResults_withIntro()
91   {
92     final Sequence seq1 = new Sequence("Seq1", "C-G-TAGA-GCAGCTT");
93     seq1.createDatasetSequence();
94   
95     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
96     aseq1.createDatasetSequence();
97   
98     /*
99      * Map dna bases [2, 4, 5], [7, 9, 11] to protein residues 1 and 2
100      */
101     AlignedCodonFrame acf = new AlignedCodonFrame();
102     MapList map = new MapList(new int[]
103     { 2, 2, 4, 5, 7, 7, 9, 9, 11, 11 }, new int[]
104     { 1, 2 }, 3, 1);
105     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
106     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
107   
108     /*
109      * Check protein residue 1 maps to [2, 4, 5]
110      */
111     SearchResults sr = MappingUtils.buildSearchResults(aseq1, 1, acfList);
112     assertEquals(2, sr.getResults().size());
113     Match m = sr.getResults().get(0);
114     assertEquals(seq1.getDatasetSequence(), m.getSequence());
115     assertEquals(2, m.getStart());
116     assertEquals(2, m.getEnd());
117     m = sr.getResults().get(1);
118     assertEquals(seq1.getDatasetSequence(), m.getSequence());
119     assertEquals(4, m.getStart());
120     assertEquals(5, m.getEnd());
121
122     /*
123      * Check protein residue 2 maps to [7, 9, 11]
124      */
125     sr = MappingUtils.buildSearchResults(aseq1, 2, acfList);
126     assertEquals(3, sr.getResults().size());
127     m = sr.getResults().get(0);
128     assertEquals(seq1.getDatasetSequence(), m.getSequence());
129     assertEquals(7, m.getStart());
130     assertEquals(7, m.getEnd());
131     m = sr.getResults().get(1);
132     assertEquals(seq1.getDatasetSequence(), m.getSequence());
133     assertEquals(9, m.getStart());
134     assertEquals(9, m.getEnd());
135     m = sr.getResults().get(2);
136     assertEquals(seq1.getDatasetSequence(), m.getSequence());
137     assertEquals(11, m.getStart());
138     assertEquals(11, m.getEnd());
139   
140     /*
141      * Check inverse mappings, from codons to protein
142      */
143     for (int i = 1; i < 14; i++)
144     {
145       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
146       int residue = (i == 2 || i == 4 || i == 5) ? 1 : (i == 7 || i == 9
147               || i == 11 ? 2 : 0);
148       if (residue == 0)
149       {
150         assertEquals(0, sr.getResults().size());
151         continue;
152       }
153       assertEquals(1, sr.getResults().size());
154       m = sr.getResults().get(0);
155       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
156       assertEquals(residue, m.getStart());
157       assertEquals(residue, m.getEnd());
158     }
159   }
160
161   /**
162    * Test mapping a sequence group.
163    * 
164    * @throws IOException
165    */
166   @Test
167   public void testMapSequenceGroup() throws IOException
168   {
169     /*
170      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
171      * viewport).
172      */
173     AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
174             "FASTA");
175     cdna.setDataset(null);
176     AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
177             "FASTA");
178     protein.setDataset(null);
179     AlignedCodonFrame acf = new AlignedCodonFrame();
180     MapList map = new MapList(new int[]
181     { 1, 3 }, new int[]
182     { 1, 1 }, 3, 1);
183     for (int seq = 0; seq < 3; seq++)
184     {
185       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
186               .getSequenceAt(seq).getDatasetSequence(), map);
187     }
188     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
189
190     AlignViewportI dnaView = new AlignViewport(cdna);
191     AlignViewportI proteinView = new AlignViewport(protein);
192     protein.setCodonFrames(acfList);
193
194     /*
195      * Select Seq1 and Seq3 in the protein
196      */
197     SequenceGroup sg = new SequenceGroup();
198     sg.setColourText(true);
199     sg.setIdColour(Color.GREEN);
200     sg.setOutlineColour(Color.LIGHT_GRAY);
201     sg.addSequence(protein.getSequenceAt(0), false);
202     sg.addSequence(protein.getSequenceAt(2), false);
203
204     /*
205      * Verify the mapped sequence group in dna
206      */
207     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
208     assertTrue(mappedGroup.getColourText());
209     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
210     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
211     assertEquals(2, mappedGroup.getSequences().size());
212     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
213     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
214
215     /*
216      * Verify mapping sequence group from dna to protein
217      */
218     sg.clear();
219     sg.addSequence(cdna.getSequenceAt(1), false);
220     sg.addSequence(cdna.getSequenceAt(0), false);
221     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
222     assertTrue(mappedGroup.getColourText());
223     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
224     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
225     assertEquals(2, mappedGroup.getSequences().size());
226     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
227     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
228   }
229
230   /**
231    * Helper method to load an alignment and ensure dataset sequences are set up.
232    * 
233    * @param data
234    * @param format
235    *          TODO
236    * @return
237    * @throws IOException
238    */
239   protected AlignmentI loadAlignment(final String data, String format)
240           throws IOException
241   {
242     Alignment a = new FormatAdapter().readFile(data,
243             AppletFormatAdapter.PASTE, format);
244     a.setDataset(null);
245     return a;
246   }
247
248   /**
249    * Test mapping a column selection in protein to its dna equivalent
250    * 
251    * @throws IOException
252    */
253   @Test
254   public void testMapColumnSelection_proteinToDna() throws IOException
255   {
256     setupMappedAlignments();
257   
258     ColumnSelection colsel = new ColumnSelection();
259
260     /*
261      * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
262      * in dna respectively, overall 0-4
263      */
264     colsel.addElement(0);
265     ColumnSelection cs = MappingUtils.mapColumnSelection(colsel,
266             proteinView, dnaView);
267     assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
268
269     /*
270      * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
271      */
272     colsel.clear();
273     colsel.addElement(1);
274     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
275     assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
276
277     /*
278      * Column 2 in protein picks up gaps only - no mapping
279      */
280     colsel.clear();
281     colsel.addElement(2);
282     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
283     assertEquals("[]", cs.getSelected().toString());
284
285     /*
286      * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
287      * 6-9, 6-10, 5-8 respectively, overall to 5-10
288      */
289     colsel.clear();
290     colsel.addElement(3);
291     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
292     assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
293
294     /*
295      * Combine selection of columns 1 and 3 to get a discontiguous mapped
296      * selection
297      */
298     colsel.clear();
299     colsel.addElement(1);
300     colsel.addElement(3);
301     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
302     assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", cs.getSelected()
303             .toString());
304   }
305
306   /**
307    * @throws IOException
308    */
309   protected void setupMappedAlignments() throws IOException
310   {
311     /*
312      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
313      * viewport). Lower case for introns.
314      */
315     AlignmentI cdna = loadAlignment(">Seq1\nAC-GctGtC-T\n"
316             + ">Seq2\nTc-GA-G-T-Tc\n" + ">Seq3\nTtTT-AaCGg-\n",
317             "FASTA");
318     cdna.setDataset(null);
319     AlignmentI protein = loadAlignment(
320             ">Seq1\n-K-P\n>Seq2\nL--Q\n>Seq3\nG--S\n",
321             "FASTA");
322     protein.setDataset(null);
323     AlignedCodonFrame acf = new AlignedCodonFrame();
324     MapList map = new MapList(new int[]
325     { 1, 3, 6, 6, 8, 9 }, new int[]
326     { 1, 2 }, 3, 1);
327     acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein
328             .getSequenceAt(0).getDatasetSequence(), map);
329     map = new MapList(new int[]
330     { 1, 1, 3, 4, 5, 7 }, new int[]
331     { 1, 2 }, 3, 1);
332     acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein
333             .getSequenceAt(1).getDatasetSequence(), map);
334     map = new MapList(new int[]
335     { 1, 1, 3, 4, 5, 5, 7, 8 }, new int[]
336     { 1, 2 }, 3, 1);
337     acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
338             .getSequenceAt(2).getDatasetSequence(), map);
339     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
340   
341     dnaView = new AlignViewport(cdna);
342     proteinView = new AlignViewport(protein);
343     protein.setCodonFrames(acfList);
344   }
345
346   /**
347    * Test mapping a column selection in dna to its protein equivalent
348    * 
349    * @throws IOException
350    */
351   @Test
352   public void testMapColumnSelection_dnaToProtein() throws IOException
353   {
354     setupMappedAlignments();
355   
356     ColumnSelection colsel = new ColumnSelection();
357   
358     /*
359      * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
360      * in protein.
361      */
362     colsel.addElement(0);
363     ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, dnaView,
364             proteinView);
365     assertEquals("[0, 1]", cs.getSelected().toString());
366
367     /*
368      * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
369      * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
370      */
371     colsel.addElement(3);
372     colsel.addElement(4);
373     colsel.addElement(5);
374     cs = MappingUtils.mapColumnSelection(colsel, dnaView, proteinView);
375     assertEquals("[0, 1, 3]", cs.getSelected().toString());
376   }
377
378   /**
379    * Tests for the method that converts a series of [start, end] ranges to
380    * single positions
381    */
382   @Test
383   public void testFlattenRanges()
384   {
385     assertEquals("[1, 2, 3, 4]",
386             Arrays.toString(MappingUtils.flattenRanges(new int[]
387             { 1, 4 })));
388     assertEquals("[1, 2, 3, 4]",
389             Arrays.toString(MappingUtils.flattenRanges(new int[]
390             { 1, 2, 3, 4 })));
391     assertEquals("[1, 2, 3, 4]",
392             Arrays.toString(MappingUtils.flattenRanges(new int[]
393             { 1, 1, 2, 2, 3, 3, 4, 4 })));
394     assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
395             Arrays.toString(MappingUtils.flattenRanges(new int[]
396             { 1, 4, 7, 9, 12, 12 })));
397     // unpaired start position is ignored:
398     assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
399             Arrays.toString(MappingUtils.flattenRanges(new int[]
400             { 1, 4, 7, 9, 12, 12, 15 })));
401   }
402 }