JAL-1645 source formatting and organise imports
[jalview.git] / test / jalview / gui / AlignViewportTest.java
1 package jalview.gui;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertNotNull;
6 import static org.testng.AssertJUnit.assertSame;
7 import static org.testng.AssertJUnit.assertTrue;
8
9 import jalview.datamodel.AlignedCodonFrame;
10 import jalview.datamodel.Alignment;
11 import jalview.datamodel.AlignmentI;
12 import jalview.datamodel.PDBEntry;
13 import jalview.datamodel.PDBEntry.Type;
14 import jalview.datamodel.Sequence;
15 import jalview.datamodel.SequenceI;
16 import jalview.io.FileLoader;
17 import jalview.io.FormatAdapter;
18 import jalview.structure.StructureSelectionManager;
19
20 import java.util.LinkedHashSet;
21 import java.util.Set;
22
23 import org.testng.annotations.BeforeClass;
24 import org.testng.annotations.BeforeMethod;
25 import org.testng.annotations.Test;
26
27 public class AlignViewportTest
28 {
29
30   AlignmentI al;
31
32   AlignViewport testee;
33
34   @BeforeClass(alwaysRun = true)
35   public static void setUpBeforeClass() throws Exception
36   {
37     jalview.bin.Jalview.main(new String[] { "-props",
38         "test/jalview/testProps.jvprops" });
39   }
40
41   @BeforeMethod(alwaysRun = true)
42   public void setUp()
43   {
44     SequenceI seq1 = new Sequence("Seq1", "ABC");
45     SequenceI seq2 = new Sequence("Seq2", "ABC");
46     SequenceI seq3 = new Sequence("Seq3", "ABC");
47     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3 };
48     al = new Alignment(seqs);
49     al.setDataset(null);
50     testee = new AlignViewport(al);
51   }
52
53   @Test(groups = { "Functional" })
54   public void testCollateForPdb()
55   {
56     /*
57      * Set up sequence pdb ids
58      */
59     PDBEntry pdb1 = new PDBEntry("1ABC", "A", Type.PDB, "1ABC.pdb");
60     PDBEntry pdb2 = new PDBEntry("2ABC", "A", Type.PDB, "2ABC.pdb");
61     PDBEntry pdb3 = new PDBEntry("3ABC", "A", Type.PDB, "3ABC.pdb");
62
63     /*
64      * seq1 and seq3 refer to 1ABC, seq2 to 2ABC, none to 3ABC
65      */
66     al.getSequenceAt(0).getDatasetSequence()
67             .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
68     al.getSequenceAt(2).getDatasetSequence()
69             .addPDBId(new PDBEntry("1ABC", "B", Type.PDB, "1ABC.pdb"));
70     al.getSequenceAt(1).getDatasetSequence()
71             .addPDBId(new PDBEntry("2ABC", "C", Type.PDB, "2ABC.pdb"));
72     /*
73      * Add a second chain PDB xref to Seq2 - should not result in a duplicate in
74      * the results
75      */
76     al.getSequenceAt(1).getDatasetSequence()
77             .addPDBId(new PDBEntry("2ABC", "D", Type.PDB, "2ABC.pdb"));
78     /*
79      * Seq3 refers to 3abc - this does not match 3ABC (as the code stands)
80      */
81     al.getSequenceAt(2).getDatasetSequence()
82             .addPDBId(new PDBEntry("3abc", "D", Type.PDB, "3ABC.pdb"));
83
84     /*
85      * run method under test
86      */
87     SequenceI[][] seqs = testee.collateForPDB(new PDBEntry[] { pdb1, pdb2,
88         pdb3 });
89
90     // seq1 and seq3 refer to PDBEntry[0]
91     assertEquals(2, seqs[0].length);
92     assertSame(al.getSequenceAt(0), seqs[0][0]);
93     assertSame(al.getSequenceAt(2), seqs[0][1]);
94
95     // seq2 refers to PDBEntry[1]
96     assertEquals(1, seqs[1].length);
97     assertSame(al.getSequenceAt(1), seqs[1][0]);
98
99     // no sequence refers to PDBEntry[2]
100     assertEquals(0, seqs[2].length);
101   }
102
103   /**
104    * Test that a mapping is not deregistered when a second view is closed but
105    * the first still holds a reference to the mapping
106    */
107   @Test(groups = { "Functional" })
108   public void testDeregisterMapping_onCloseView()
109   {
110     /*
111      * alignment with reference to mappings
112      */
113     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
114             ">Seq1\nCAGT\n", FormatAdapter.PASTE);
115
116     AlignedCodonFrame acf1 = new AlignedCodonFrame();
117     AlignedCodonFrame acf2 = new AlignedCodonFrame();
118
119     Set<AlignedCodonFrame> mappings = new LinkedHashSet<AlignedCodonFrame>();
120     mappings.add(acf1);
121     mappings.add(acf2);
122     af1.getViewport().getAlignment().setCodonFrames(mappings);
123     af1.newView_actionPerformed(null);
124
125     /*
126      * Verify that creating the alignment for the new View has registered the
127      * mappings
128      */
129     StructureSelectionManager ssm = StructureSelectionManager
130             .getStructureSelectionManager(Desktop.instance);
131     assertEquals(2, ssm.seqmappings.size());
132     assertTrue(ssm.seqmappings.contains(acf1));
133     assertTrue(ssm.seqmappings.contains(acf2));
134
135     /*
136      * Close the second view. Verify that mappings are not removed as the first
137      * view still holds a reference to them.
138      */
139     af1.closeMenuItem_actionPerformed(false);
140     assertEquals(2, ssm.seqmappings.size());
141     assertTrue(ssm.seqmappings.contains(acf1));
142     assertTrue(ssm.seqmappings.contains(acf2));
143   }
144
145   /**
146    * Test that a mapping is deregistered if no alignment holds a reference to it
147    */
148   @Test(groups = { "Functional" })
149   public void testDeregisterMapping_withNoReference()
150   {
151     Desktop d = Desktop.instance;
152     assertNotNull(d);
153     StructureSelectionManager ssm = StructureSelectionManager
154             .getStructureSelectionManager(Desktop.instance);
155     ssm.resetAll();
156
157     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
158             ">Seq1\nRSVQ\n", FormatAdapter.PASTE);
159     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
160             ">Seq2\nDGEL\n", FormatAdapter.PASTE);
161
162     AlignedCodonFrame acf1 = new AlignedCodonFrame();
163     AlignedCodonFrame acf2 = new AlignedCodonFrame();
164     AlignedCodonFrame acf3 = new AlignedCodonFrame();
165
166     Set<AlignedCodonFrame> mappings1 = new LinkedHashSet<AlignedCodonFrame>();
167     mappings1.add(acf1);
168     af1.getViewport().getAlignment().setCodonFrames(mappings1);
169
170     Set<AlignedCodonFrame> mappings2 = new LinkedHashSet<AlignedCodonFrame>();
171     mappings2.add(acf2);
172     mappings2.add(acf3);
173     af2.getViewport().getAlignment().setCodonFrames(mappings2);
174
175     /*
176      * AlignFrame1 has mapping acf1, AlignFrame2 has acf2 and acf3
177      */
178
179     Set<AlignedCodonFrame> ssmMappings = ssm.seqmappings;
180     assertEquals(0, ssmMappings.size());
181     ssm.registerMapping(acf1);
182     assertEquals(1, ssmMappings.size());
183     ssm.registerMapping(acf2);
184     assertEquals(2, ssmMappings.size());
185     ssm.registerMapping(acf3);
186     assertEquals(3, ssmMappings.size());
187
188     /*
189      * Closing AlignFrame2 should remove its mappings from
190      * StructureSelectionManager, since AlignFrame1 has no reference to them
191      */
192     af2.closeMenuItem_actionPerformed(true);
193     assertEquals(1, ssmMappings.size());
194     assertTrue(ssmMappings.contains(acf1));
195   }
196
197   /**
198    * Test that a mapping is not deregistered if another alignment holds a
199    * reference to it
200    */
201   @Test(groups = { "Functional" })
202   public void testDeregisterMapping_withReference()
203   {
204     Desktop d = Desktop.instance;
205     assertNotNull(d);
206     StructureSelectionManager ssm = StructureSelectionManager
207             .getStructureSelectionManager(Desktop.instance);
208     ssm.resetAll();
209
210     AlignFrame af1 = new FileLoader().LoadFileWaitTillLoaded(
211             ">Seq1\nRSVQ\n", FormatAdapter.PASTE);
212     AlignFrame af2 = new FileLoader().LoadFileWaitTillLoaded(
213             ">Seq2\nDGEL\n", FormatAdapter.PASTE);
214
215     AlignedCodonFrame acf1 = new AlignedCodonFrame();
216     AlignedCodonFrame acf2 = new AlignedCodonFrame();
217     AlignedCodonFrame acf3 = new AlignedCodonFrame();
218
219     Set<AlignedCodonFrame> mappings1 = new LinkedHashSet<AlignedCodonFrame>();
220     mappings1.add(acf1);
221     mappings1.add(acf2);
222     af1.getViewport().getAlignment().setCodonFrames(mappings1);
223
224     Set<AlignedCodonFrame> mappings2 = new LinkedHashSet<AlignedCodonFrame>();
225     mappings2.add(acf2);
226     mappings2.add(acf3);
227     af2.getViewport().getAlignment().setCodonFrames(mappings2);
228
229     /*
230      * AlignFrame1 has mappings acf1 and acf2, AlignFrame2 has acf2 and acf3
231      */
232
233     Set<AlignedCodonFrame> ssmMappings = ssm.seqmappings;
234     assertEquals(0, ssmMappings.size());
235     ssm.registerMapping(acf1);
236     assertEquals(1, ssmMappings.size());
237     ssm.registerMapping(acf2);
238     assertEquals(2, ssmMappings.size());
239     ssm.registerMapping(acf3);
240     assertEquals(3, ssmMappings.size());
241
242     /*
243      * Closing AlignFrame2 should remove mapping acf3 from
244      * StructureSelectionManager, but not acf2, since AlignFrame1 still has a
245      * reference to it
246      */
247     af2.closeMenuItem_actionPerformed(true);
248     assertEquals(2, ssmMappings.size());
249     assertTrue(ssmMappings.contains(acf1));
250     assertTrue(ssmMappings.contains(acf2));
251     assertFalse(ssmMappings.contains(acf3));
252   }
253 }