2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.DBRefEntry;
31 import jalview.datamodel.DBRefSource;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.io.AppletFormatAdapter;
35 import jalview.io.FormatAdapter;
36 import jalview.util.MessageManager;
38 import java.awt.Component;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.List;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuItem;
45 import javax.swing.JPopupMenu;
46 import javax.swing.JSeparator;
48 import org.testng.annotations.BeforeMethod;
49 import org.testng.annotations.Test;
51 public class PopupMenuTest
53 // 4 sequences x 13 positions
54 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
56 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
58 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
59 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
64 AlignmentPanel parentPanel;
66 PopupMenu testee = null;
68 @BeforeMethod(alwaysRun = true)
69 public void setUp() throws IOException
71 alignment = new FormatAdapter().readFile(TEST_DATA,
72 AppletFormatAdapter.PASTE, "FASTA");
73 AlignFrame af = new AlignFrame(alignment, 700, 500);
74 parentPanel = new AlignmentPanel(af, af.getViewport());
75 testee = new PopupMenu(parentPanel, null, null);
77 for (SequenceI seq : alignment.getSequences())
79 final AlignmentAnnotation annotation = new AlignmentAnnotation(
80 "label" + i, "desc" + i, i);
81 annotation.setCalcId("calcId" + i);
82 seq.addAlignmentAnnotation(annotation);
83 annotation.setSequenceRef(seq);
87 @Test(groups = { "Functional" })
88 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
90 JMenuItem menu = new JMenuItem();
91 List<SequenceI> seqs = new ArrayList<SequenceI>();
92 testee.configureReferenceAnnotationsMenu(menu, seqs);
93 assertFalse(menu.isEnabled());
95 menu.setEnabled(true);
96 testee.configureReferenceAnnotationsMenu(menu, null);
97 assertFalse(menu.isEnabled());
101 * Test building the 'add reference annotations' menu for the case where there
102 * are no reference annotations to add to the alignment. The menu item should
105 @Test(groups = { "Functional" })
106 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
108 JMenuItem menu = new JMenuItem();
111 * Initial state is that sequences have annotations, and have dataset
112 * sequences, but the dataset sequences have no annotations. Hence nothing
115 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
117 testee.configureReferenceAnnotationsMenu(menu, seqs);
118 assertFalse(menu.isEnabled());
122 * Test building the 'add reference annotations' menu for the case where all
123 * reference annotations are already on the alignment. The menu item should be
126 @Test(groups = { "Functional" })
127 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
129 JMenuItem menu = new JMenuItem();
130 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
132 // make up new annotations and add to dataset sequences, sequences and
134 attachReferenceAnnotations(seqs, true, true);
136 testee.configureReferenceAnnotationsMenu(menu, seqs);
137 assertFalse(menu.isEnabled());
141 * Test building the 'add reference annotations' menu for the case where
142 * several reference annotations are on the dataset but not on the sequences.
143 * The menu item should be enabled, and acquire a tooltip which lists the
144 * annotation sources (calcIds) and type (labels).
146 @Test(groups = { "Functional" })
147 public void testConfigureReferenceAnnotationsMenu()
149 JMenuItem menu = new JMenuItem();
150 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
152 // make up new annotations and add to dataset sequences
153 attachReferenceAnnotations(seqs, false, false);
155 testee.configureReferenceAnnotationsMenu(menu, seqs);
156 assertTrue(menu.isEnabled());
157 String s = MessageManager.getString("label.add_annotations_for");
158 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
159 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
160 assertEquals(expected, menu.getToolTipText());
164 * Test building the 'add reference annotations' menu for the case where
165 * several reference annotations are on the dataset and the sequences but not
166 * on the alignment. The menu item should be enabled, and acquire a tooltip
167 * which lists the annotation sources (calcIds) and type (labels).
169 @Test(groups = { "Functional" })
170 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
172 JMenuItem menu = new JMenuItem();
173 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
175 // make up new annotations and add to dataset sequences and sequences
176 attachReferenceAnnotations(seqs, true, false);
178 testee.configureReferenceAnnotationsMenu(menu, seqs);
179 assertTrue(menu.isEnabled());
180 String s = MessageManager.getString("label.add_annotations_for");
181 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
182 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
183 assertEquals(expected, menu.getToolTipText());
187 * Generate annotations and add to dataset sequences and (optionally)
188 * sequences and/or alignment
191 * @param addToSequence
192 * @param addToAlignment
194 private void attachReferenceAnnotations(List<SequenceI> seqs,
195 boolean addToSequence, boolean addToAlignment)
197 // PDB.secondary structure on Sequence0
198 AlignmentAnnotation annotation = new AlignmentAnnotation(
199 "secondary structure", "", 0);
200 annotation.setCalcId("PDB");
201 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
204 seqs.get(0).addAlignmentAnnotation(annotation);
208 this.alignment.addAnnotation(annotation);
211 // PDB.Temp on Sequence1
212 annotation = new AlignmentAnnotation("Temp", "", 0);
213 annotation.setCalcId("PDB");
214 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
217 seqs.get(1).addAlignmentAnnotation(annotation);
221 this.alignment.addAnnotation(annotation);
224 // JMOL.secondary structure on Sequence0
225 annotation = new AlignmentAnnotation("secondary structure", "", 0);
226 annotation.setCalcId("Jmol");
227 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
230 seqs.get(0).addAlignmentAnnotation(annotation);
234 this.alignment.addAnnotation(annotation);
239 * Test building the 'add reference annotations' menu for the case where there
240 * are two alignment views:
242 * <li>in one view, reference annotations have been added (are on the
243 * datasets, sequences and alignment)</li>
244 * <li>in the current view, reference annotations are on the dataset and
245 * sequence, but not the alignment</li>
247 * The menu item should be enabled, and acquire a tooltip which lists the
248 * annotation sources (calcIds) and type (labels).
250 @Test(groups = { "Functional" })
251 public void testConfigureReferenceAnnotationsMenu_twoViews()
256 * Test for building menu options including 'show' and 'hide' annotation
259 @Test(groups = { "Functional" })
260 public void testBuildAnnotationTypesMenus()
262 JMenu showMenu = new JMenu();
263 JMenu hideMenu = new JMenu();
264 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
266 // make up new annotations and add to sequences and to the alignment
268 // PDB.secondary structure on Sequence0
269 AlignmentAnnotation annotation = new AlignmentAnnotation(
270 "secondary structure", "", new Annotation[] {});
271 annotation.setCalcId("PDB");
272 annotation.visible = true;
273 seqs.get(0).addAlignmentAnnotation(annotation);
274 parentPanel.getAlignment().addAnnotation(annotation);
276 // JMOL.secondary structure on Sequence0 - hidden
277 annotation = new AlignmentAnnotation("secondary structure", "",
278 new Annotation[] {});
279 annotation.setCalcId("JMOL");
280 annotation.visible = false;
281 seqs.get(0).addAlignmentAnnotation(annotation);
282 parentPanel.getAlignment().addAnnotation(annotation);
284 // Jpred.SSP on Sequence0 - hidden
285 annotation = new AlignmentAnnotation("SSP", "", new Annotation[] {});
286 annotation.setCalcId("JPred");
287 annotation.visible = false;
288 seqs.get(0).addAlignmentAnnotation(annotation);
289 parentPanel.getAlignment().addAnnotation(annotation);
291 // PDB.Temp on Sequence1
292 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
293 annotation.setCalcId("PDB");
294 annotation.visible = true;
295 seqs.get(1).addAlignmentAnnotation(annotation);
296 parentPanel.getAlignment().addAnnotation(annotation);
299 * Expect menu options to show "secondary structure" and "SSP", and to hide
300 * "secondary structure" and "Temp". Tooltip should be calcId.
302 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
304 assertTrue(showMenu.isEnabled());
305 assertTrue(hideMenu.isEnabled());
307 Component[] showOptions = showMenu.getMenuComponents();
308 Component[] hideOptions = hideMenu.getMenuComponents();
310 assertEquals(4, showOptions.length); // includes 'All' and separator
311 assertEquals(4, hideOptions.length);
312 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
313 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
314 assertEquals(JSeparator.HORIZONTAL,
315 ((JSeparator) showOptions[1]).getOrientation());
316 assertEquals("secondary structure",
317 ((JMenuItem) showOptions[2]).getText());
318 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
319 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
320 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
322 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
323 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
324 assertEquals(JSeparator.HORIZONTAL,
325 ((JSeparator) hideOptions[1]).getOrientation());
326 assertEquals("secondary structure",
327 ((JMenuItem) hideOptions[2]).getText());
328 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
329 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
330 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
334 * Test for building menu options with only 'hide' annotation types enabled.
336 @Test(groups = { "Functional" })
337 public void testBuildAnnotationTypesMenus_showDisabled()
339 JMenu showMenu = new JMenu();
340 JMenu hideMenu = new JMenu();
341 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
343 // make up new annotations and add to sequences and to the alignment
345 // PDB.secondary structure on Sequence0
346 AlignmentAnnotation annotation = new AlignmentAnnotation(
347 "secondary structure", "", new Annotation[] {});
348 annotation.setCalcId("PDB");
349 annotation.visible = true;
350 seqs.get(0).addAlignmentAnnotation(annotation);
351 parentPanel.getAlignment().addAnnotation(annotation);
353 // PDB.Temp on Sequence1
354 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
355 annotation.setCalcId("PDB");
356 annotation.visible = true;
357 seqs.get(1).addAlignmentAnnotation(annotation);
358 parentPanel.getAlignment().addAnnotation(annotation);
361 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
362 * should be calcId. 'Show' menu should be disabled.
364 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
366 assertFalse(showMenu.isEnabled());
367 assertTrue(hideMenu.isEnabled());
369 Component[] showOptions = showMenu.getMenuComponents();
370 Component[] hideOptions = hideMenu.getMenuComponents();
372 assertEquals(2, showOptions.length); // includes 'All' and separator
373 assertEquals(4, hideOptions.length);
374 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
375 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
376 assertEquals(JSeparator.HORIZONTAL,
377 ((JSeparator) showOptions[1]).getOrientation());
379 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
380 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
381 assertEquals(JSeparator.HORIZONTAL,
382 ((JSeparator) hideOptions[1]).getOrientation());
383 assertEquals("secondary structure",
384 ((JMenuItem) hideOptions[2]).getText());
385 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
386 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
387 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
391 * Test for building menu options with only 'show' annotation types enabled.
393 @Test(groups = { "Functional" })
394 public void testBuildAnnotationTypesMenus_hideDisabled()
396 JMenu showMenu = new JMenu();
397 JMenu hideMenu = new JMenu();
398 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
400 // make up new annotations and add to sequences and to the alignment
402 // PDB.secondary structure on Sequence0
403 AlignmentAnnotation annotation = new AlignmentAnnotation(
404 "secondary structure", "", new Annotation[] {});
405 annotation.setCalcId("PDB");
406 annotation.visible = false;
407 seqs.get(0).addAlignmentAnnotation(annotation);
408 parentPanel.getAlignment().addAnnotation(annotation);
410 // PDB.Temp on Sequence1
411 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
412 annotation.setCalcId("PDB2");
413 annotation.visible = false;
414 seqs.get(1).addAlignmentAnnotation(annotation);
415 parentPanel.getAlignment().addAnnotation(annotation);
418 * Expect menu options to show "secondary structure" and "Temp". Tooltip
419 * should be calcId. 'hide' menu should be disabled.
421 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
423 assertTrue(showMenu.isEnabled());
424 assertFalse(hideMenu.isEnabled());
426 Component[] showOptions = showMenu.getMenuComponents();
427 Component[] hideOptions = hideMenu.getMenuComponents();
429 assertEquals(4, showOptions.length); // includes 'All' and separator
430 assertEquals(2, hideOptions.length);
431 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
432 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
433 assertEquals(JSeparator.HORIZONTAL,
434 ((JSeparator) showOptions[1]).getOrientation());
435 assertEquals("secondary structure",
436 ((JMenuItem) showOptions[2]).getText());
437 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
438 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
439 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
441 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
442 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
443 assertEquals(JSeparator.HORIZONTAL,
444 ((JSeparator) hideOptions[1]).getOrientation());
448 * Test for adding feature links
450 @Test(groups = { "Functional" })
451 public void testAddFeatureLinks()
453 // sequences from the alignment
454 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
456 // create list of links and list of DBRefs
457 List<String> links = new ArrayList<String>();
458 List<DBRefEntry> refs = new ArrayList<DBRefEntry>();
460 // links as might be added into Preferences | Connections dialog
461 links.add("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_NAME$");
462 links.add("UNIPROT | http://www.uniprot.org/uniprot/$SEQUENCE_ID$");
463 links.add("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$SEQUENCE_ID$");
464 // Gene3D entry tests for case (in)sensitivity
465 links.add("Gene3D | http://gene3d.biochem.ucl.ac.uk/Gene3D/search?sterm=$SEQUENCE_ID$&mode=protein");
468 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "P83527"));
469 refs.add(new DBRefEntry("INTERPRO", "1", "IPR001041"));
470 refs.add(new DBRefEntry("INTERPRO", "1", "IPR006058"));
471 refs.add(new DBRefEntry("INTERPRO", "1", "IPR012675"));
474 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "Q9ZTS2"));
475 refs.add(new DBRefEntry("GENE3D", "1", "3.10.20.30"));
477 // add all the dbrefs to the sequences: Uniprot 1 each, Interpro all 3 to
478 // seq0, Gene3D to seq1
479 seqs.get(0).addDBRef(refs.get(0));
481 seqs.get(0).addDBRef(refs.get(1));
482 seqs.get(0).addDBRef(refs.get(2));
483 seqs.get(0).addDBRef(refs.get(3));
485 seqs.get(1).addDBRef(refs.get(4));
486 seqs.get(1).addDBRef(refs.get(5));
488 // get the Popup Menu for first sequence
489 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0), links);
490 Component[] seqItems = testee.sequenceMenu.getMenuComponents();
491 JMenu linkMenu = (JMenu) seqItems[6];
492 Component[] linkItems = linkMenu.getMenuComponents();
494 // check the number of links are the expected number
495 assertEquals(5, linkItems.length);
497 // first entry is EMBL-EBI which just uses sequence id not accession id?
498 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
500 // sequence id for each link should match corresponding DB accession id
501 for (int i = 1; i < 4; i++)
503 assertEquals(refs.get(i - 1).getSource(), ((JMenuItem) linkItems[i])
504 .getText().split("\\|")[0]);
505 assertEquals(refs.get(i - 1).getAccessionId(),
506 ((JMenuItem) linkItems[i])
507 .getText().split("\\|")[1]);
510 // get the Popup Menu for second sequence
511 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(1), links);
512 seqItems = testee.sequenceMenu.getMenuComponents();
513 linkMenu = (JMenu) seqItems[6];
514 linkItems = linkMenu.getMenuComponents();
516 // check the number of links are the expected number
517 assertEquals(3, linkItems.length);
519 // first entry is EMBL-EBI which just uses sequence id not accession id?
520 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
522 // sequence id for each link should match corresponding DB accession id
523 for (int i = 1; i < 3; i++)
525 assertEquals(refs.get(i + 3).getSource(), ((JMenuItem) linkItems[i])
526 .getText().split("\\|")[0].toUpperCase());
527 assertEquals(refs.get(i + 3).getAccessionId(),
528 ((JMenuItem) linkItems[i]).getText().split("\\|")[1]);