JAL-653 AlignedCodonFrame internal refactor, merge ranges with
[jalview.git] / test / jalview / io / ExonerateGffTest.java
1 package jalview.io;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertSame;
5 import static org.testng.AssertJUnit.assertTrue;
6 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
7
8 import jalview.datamodel.AlignedCodonFrame;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.Mapping;
12 import jalview.datamodel.Sequence;
13 import jalview.datamodel.SequenceDummy;
14 import jalview.datamodel.SequenceI;
15 import jalview.gui.AlignFrame;
16
17 import java.util.Set;
18
19 import org.testng.annotations.Test;
20
21 /**
22  * Tests of use cases that include parsing exonerate GFF 'similarity' features.
23  * These describe mappings between protein and cDNA
24  * 
25  * @author gmcarstairs
26  *
27  */
28 public class ExonerateGffTest
29 {
30
31   /**
32    * Test the case where we load a protein ('query') sequence, then exonerateGff
33    * describing its mapping to cDNA, and then a DNA sequence including the
34    * mapped region
35    */
36   @Test(groups = "Functional")
37   public void testLoadProteinGffCdna()
38   {
39     String proteinSeq = ">prot1/10-16\nYCWRSGA";
40     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
41             proteinSeq, FormatAdapter.PASTE);
42
43     /*
44      * exonerate GFF output mapping residues 11-15 (CWRSG) 
45      * to bases 24-10 in sequence 'dna1' (reverse strand)
46      */
47     String exonerateGff = "##gff-version 2\n"
48             + "prot1\tprotein2genome\tsimilarity\t11\t15\t99\t-\t.\talignment_id 0 ; Target dna1 ; Align 11 24 5";
49     af.loadJalviewDataFile(exonerateGff, FormatAdapter.PASTE, null, null);
50
51     /*
52      * check we have a mapping from prot1 to SequenceDummy 'dna1'
53      */
54     AlignmentI dataset = af.getViewport().getAlignment().getDataset();
55     assertEquals(1, dataset.getSequences().size());
56     assertEquals("prot1", dataset.getSequenceAt(0).getName());
57     assertEquals("YCWRSGA", dataset.getSequenceAt(0).getSequenceAsString());
58     Set<AlignedCodonFrame> mappings = dataset.getCodonFrames();
59     assertEquals(1, mappings.size());
60     AlignedCodonFrame mapping = mappings.iterator().next();
61     SequenceI mappedDna = mapping.getDnaForAaSeq(dataset.getSequenceAt(0));
62     assertTrue(mappedDna instanceof SequenceDummy);
63     assertEquals("dna1", mappedDna.getName());
64     Mapping[] mapList = mapping.getProtMappings();
65     assertEquals(1, mapList.length);
66     // 11 in protein should map to codon [24, 23, 22] in dna
67     int[] mappedRegion = mapList[0].getMap().locateInFrom(11, 11);
68     assertArrayEquals(new int[] { 24, 22 }, mappedRegion);
69     // 15 in protein should map to codon [12, 11, 10] in dna
70     mappedRegion = mapList[0].getMap().locateInFrom(15, 15);
71     assertArrayEquals(new int[] { 12, 10 }, mappedRegion);
72
73     // so far so good; TODO: programmatically add mapped sequences
74     // and verify the mappings are 'realised'
75     SequenceI dna1 = new Sequence("dna1", "AAACCCGGGTTTAAACCCGGGTTT");
76     AlignmentI al = new Alignment(new SequenceI[] { dna1 });
77     al.setDataset(null);
78
79     /*
80      * Now 'realise' the virtual mapping to the real DNA sequence;
81      * interactively this could be by a drag or fetch of the sequence data
82      */
83     mapping.realiseWith(dna1);
84     // verify the mapping is now from the real, not the dummy sequence
85     assertSame(dna1.getDatasetSequence(),
86             mapping.getDnaForAaSeq(dataset.getSequenceAt(0)));
87   }
88 }