Merge branch 'develop' into trialMerge
[jalview.git] / test / jalview / structure / StructureSelectionManagerTest.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.structure;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import jalview.datamodel.AlignedCodonFrame;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.JvOptionPane;
31 import jalview.io.DataSourceType;
32 import jalview.io.StructureFile;
33 import jalview.util.MapList;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41
42 public class StructureSelectionManagerTest
43 {
44
45   @BeforeClass(alwaysRun = true)
46   public void setUpJvOptionPane()
47   {
48     JvOptionPane.setInteractiveMode(false);
49     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50   }
51
52   private StructureSelectionManager ssm;
53
54   @BeforeMethod(alwaysRun = true)
55   public void setUp()
56   {
57     StructureImportSettings.setShowSeqFeatures(true);
58     ssm = new StructureSelectionManager();
59   }
60
61   @Test(groups = { "Functional" })
62   public void testRegisterMapping()
63   {
64     AlignedCodonFrame acf1 = new AlignedCodonFrame();
65     acf1.addMap(new Sequence("s1", "ttt"), new Sequence("p1", "p"),
66             new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 1, 1));
67     AlignedCodonFrame acf2 = new AlignedCodonFrame();
68     acf2.addMap(new Sequence("s2", "ttt"), new Sequence("p2", "p"),
69             new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 1, 1));
70
71     ssm.registerMapping(acf1);
72     assertEquals(1, ssm.getSequenceMappings().size());
73     assertTrue(ssm.getSequenceMappings().contains(acf1));
74
75     ssm.registerMapping(acf2);
76     assertEquals(2, ssm.getSequenceMappings().size());
77     assertTrue(ssm.getSequenceMappings().contains(acf1));
78     assertTrue(ssm.getSequenceMappings().contains(acf2));
79
80     /*
81      * Re-adding the first mapping does nothing
82      */
83     ssm.registerMapping(acf1);
84     assertEquals(2, ssm.getSequenceMappings().size());
85     assertTrue(ssm.getSequenceMappings().contains(acf1));
86     assertTrue(ssm.getSequenceMappings().contains(acf2));
87   }
88
89   @Test(groups = { "Functional" })
90   public void testRegisterMappings()
91   {
92     AlignedCodonFrame acf1 = new AlignedCodonFrame();
93     acf1.addMap(new Sequence("s1", "ttt"), new Sequence("p1", "p"),
94             new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 1, 1));
95     AlignedCodonFrame acf2 = new AlignedCodonFrame();
96     acf2.addMap(new Sequence("s2", "ttt"), new Sequence("p2", "p"),
97             new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 1, 1));
98     AlignedCodonFrame acf3 = new AlignedCodonFrame();
99     acf3.addMap(new Sequence("s3", "ttt"), new Sequence("p3", "p"),
100             new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 1, 1));
101
102     List<AlignedCodonFrame> set1 = new ArrayList<AlignedCodonFrame>();
103     set1.add(acf1);
104     set1.add(acf2);
105     List<AlignedCodonFrame> set2 = new ArrayList<AlignedCodonFrame>();
106     set2.add(acf2);
107     set2.add(acf3);
108
109     /*
110      * Add both sets twice; each mapping should be added once only
111      */
112     ssm.registerMappings(set1);
113     ssm.registerMappings(set1);
114     ssm.registerMappings(set2);
115     ssm.registerMappings(set2);
116
117     assertEquals(3, ssm.getSequenceMappings().size());
118     assertTrue(ssm.getSequenceMappings().contains(acf1));
119     assertTrue(ssm.getSequenceMappings().contains(acf2));
120     assertTrue(ssm.getSequenceMappings().contains(acf3));
121   }
122
123   /**
124    * Verify that RESNUM sequence features are present after creating a PDB
125    * mapping
126    */
127   @Test(groups = { "Functional" })
128   public void testSetMapping_seqFeatures()
129   {
130     SequenceI seq = new Sequence(
131             "1GAQ|B",
132             "ATYNVKLITPEGEVELQVPDDVYILDQAEEDGIDLPYSCRAGSCSSCAGKVVSGSVDQSDQSYLDDGQIADGWVLTCHAYPTSDVVIETHKEEELTGA");
133     StructureSelectionManager sm = new StructureSelectionManager();
134     sm.setProcessSecondaryStructure(true);
135     sm.setAddTempFacAnnot(true);
136     StructureFile pmap = sm.setMapping(true, new SequenceI[] { seq },
137             new String[] { null }, "examples/1gaq.txt", DataSourceType.FILE);
138     assertTrue(pmap != null);
139
140     assertEquals(3, pmap.getSeqs().size());
141     assertEquals("1GAQ|A", pmap.getSeqs().get(0).getName());
142     assertEquals("1GAQ|B", pmap.getSeqs().get(1).getName());
143     assertEquals("1GAQ|C", pmap.getSeqs().get(2).getName());
144
145     /*
146      * Verify a RESNUM sequence feature in the PDBfile sequence
147      */
148     SequenceFeature sf = pmap.getSeqs().get(0).getSequenceFeatures()[0];
149     assertEquals("RESNUM", sf.getType());
150     assertEquals("1gaq", sf.getFeatureGroup());
151     assertEquals("GLU:  19  1gaqA", sf.getDescription());
152
153     /*
154      * Verify a RESNUM sequence feature in the StructureSelectionManager mapped
155      * sequence
156      */
157     StructureMapping map = sm.getMapping("examples/1gaq.txt")[0];
158     sf = map.sequence.getSequenceFeatures()[0];
159     assertEquals("RESNUM", sf.getType());
160     assertEquals("1gaq", sf.getFeatureGroup());
161     assertEquals("ALA:   1  1gaqB", sf.getDescription());
162   }
163 }