JAL-3438 spotless for 2.11.2.0
[jalview.git] / test / jalview / io / gff / GffTests.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.io.gff;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertSame;
25 import static org.testng.AssertJUnit.assertTrue;
26 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
27
28 import jalview.datamodel.AlignedCodonFrame;
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.Mapping;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceDummy;
34 import jalview.datamodel.SequenceI;
35 import jalview.gui.AlignFrame;
36 import jalview.gui.JvOptionPane;
37 import jalview.io.DataSourceType;
38 import jalview.io.FileLoader;
39
40 import java.util.List;
41
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
44
45 /**
46  * Tests of use cases that include parsing GFF (version 2 or 3) features that
47  * describe mappings between protein and cDNA. The format of the GFF varies
48  * depending on which tool generated it.
49  */
50 public class GffTests
51 {
52
53   @BeforeClass(alwaysRun = true)
54   public void setUpJvOptionPane()
55   {
56     JvOptionPane.setInteractiveMode(false);
57     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
58   }
59
60   /**
61    * Test the case where we load a protein ('query') sequence, then exonerateGff
62    * describing its mapping to cDNA, and then a DNA sequence including the
63    * mapped region
64    */
65   @Test(groups = "Functional")
66   public void testResolveExonerateGff()
67   {
68     String proteinSeq = ">prot1/10-16\nYCWRSGA";
69     AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(proteinSeq,
70             DataSourceType.PASTE);
71
72     /*
73      * exonerate GFF output mapping residues 11-15 (CWRSG) 
74      * to bases 24-10 in sequence 'dna1' (reverse strand)
75      */
76     String exonerateGff = "##gff-version 2\n"
77             + "prot1\tprotein2genome\tsimilarity\t11\t15\t99\t-\t.\talignment_id 0 ; Target dna1 ; Align 11 24 5";
78     af.loadJalviewDataFile(exonerateGff, DataSourceType.PASTE, null, null);
79
80     /*
81      * check we have a mapping from prot1 to SequenceDummy 'dna1'
82      */
83     AlignmentI dataset = af.getViewport().getAlignment().getDataset();
84     assertEquals(1, dataset.getSequences().size());
85     assertEquals("prot1", dataset.getSequenceAt(0).getName());
86     assertEquals("YCWRSGA", dataset.getSequenceAt(0).getSequenceAsString());
87     List<AlignedCodonFrame> mappings = dataset.getCodonFrames();
88     assertEquals(1, mappings.size());
89     AlignedCodonFrame mapping = mappings.iterator().next();
90     SequenceI mappedDna = mapping.getDnaForAaSeq(dataset.getSequenceAt(0));
91     assertTrue(mappedDna instanceof SequenceDummy);
92     assertEquals("dna1", mappedDna.getName());
93     Mapping[] mapList = mapping.getProtMappings();
94     assertEquals(1, mapList.length);
95     // 11 in protein should map to codon [24, 23, 22] in dna
96     int[] mappedRegion = mapList[0].getMap().locateInFrom(11, 11);
97     assertArrayEquals(new int[] { 24, 22 }, mappedRegion);
98     // 15 in protein should map to codon [12, 11, 10] in dna
99     mappedRegion = mapList[0].getMap().locateInFrom(15, 15);
100     assertArrayEquals(new int[] { 12, 10 }, mappedRegion);
101
102     SequenceI dna1 = new Sequence("dna1", "AAACCCGGGTTTAAACCCGGGTTT");
103     AlignmentI al = new Alignment(new SequenceI[] { dna1 });
104     al.setDataset(null);
105
106     /*
107      * Now 'realise' the virtual mapping to the real DNA sequence;
108      * interactively this could be by a drag or fetch of the sequence data
109      * on to the alignment
110      */
111     mapping.realiseWith(dna1);
112     // verify the mapping is now from the real, not the dummy sequence
113     assertSame(dna1.getDatasetSequence(),
114             mapping.getDnaForAaSeq(dataset.getSequenceAt(0)));
115   }
116 }