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 jalview.util.UrlConstants.DB_ACCESSION;
24 import static jalview.util.UrlConstants.SEQUENCE_ID;
25 import static org.testng.AssertJUnit.assertEquals;
26 import static org.testng.AssertJUnit.assertFalse;
27 import static org.testng.AssertJUnit.assertTrue;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.DBRefEntry;
33 import jalview.datamodel.DBRefSource;
34 import jalview.datamodel.Sequence;
35 import jalview.datamodel.SequenceI;
36 import jalview.io.AppletFormatAdapter;
37 import jalview.io.FormatAdapter;
38 import jalview.util.MessageManager;
40 import java.awt.Component;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.List;
45 import javax.swing.JMenu;
46 import javax.swing.JMenuItem;
47 import javax.swing.JPopupMenu;
48 import javax.swing.JSeparator;
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.BeforeMethod;
52 import org.testng.annotations.Test;
54 public class PopupMenuTest
57 @BeforeClass(alwaysRun = true)
58 public void setUpJvOptionPane()
60 JvOptionPane.setInteractiveMode(false);
61 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
64 // 4 sequences x 13 positions
65 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
67 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
69 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
70 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
75 AlignmentPanel parentPanel;
77 PopupMenu testee = null;
79 @BeforeMethod(alwaysRun = true)
80 public void setUp() throws IOException
82 alignment = new FormatAdapter().readFile(TEST_DATA,
83 AppletFormatAdapter.PASTE, "FASTA");
84 AlignFrame af = new AlignFrame(alignment, 700, 500);
85 parentPanel = new AlignmentPanel(af, af.getViewport());
86 testee = new PopupMenu(parentPanel, null, null);
88 for (SequenceI seq : alignment.getSequences())
90 final AlignmentAnnotation annotation = new AlignmentAnnotation(
91 "label" + i, "desc" + i, i);
92 annotation.setCalcId("calcId" + i);
93 seq.addAlignmentAnnotation(annotation);
94 annotation.setSequenceRef(seq);
98 @Test(groups = { "Functional" })
99 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
101 JMenuItem menu = new JMenuItem();
102 List<SequenceI> seqs = new ArrayList<SequenceI>();
103 testee.configureReferenceAnnotationsMenu(menu, seqs);
104 assertFalse(menu.isEnabled());
106 menu.setEnabled(true);
107 testee.configureReferenceAnnotationsMenu(menu, null);
108 assertFalse(menu.isEnabled());
112 * Test building the 'add reference annotations' menu for the case where there
113 * are no reference annotations to add to the alignment. The menu item should
116 @Test(groups = { "Functional" })
117 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
119 JMenuItem menu = new JMenuItem();
122 * Initial state is that sequences have annotations, and have dataset
123 * sequences, but the dataset sequences have no annotations. Hence nothing
126 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
128 testee.configureReferenceAnnotationsMenu(menu, seqs);
129 assertFalse(menu.isEnabled());
133 * Test building the 'add reference annotations' menu for the case where all
134 * reference annotations are already on the alignment. The menu item should be
137 @Test(groups = { "Functional" })
138 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
140 JMenuItem menu = new JMenuItem();
141 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
143 // make up new annotations and add to dataset sequences, sequences and
145 attachReferenceAnnotations(seqs, true, true);
147 testee.configureReferenceAnnotationsMenu(menu, seqs);
148 assertFalse(menu.isEnabled());
152 * Test building the 'add reference annotations' menu for the case where
153 * several reference annotations are on the dataset but not on the sequences.
154 * The menu item should be enabled, and acquire a tooltip which lists the
155 * annotation sources (calcIds) and type (labels).
157 @Test(groups = { "Functional" })
158 public void testConfigureReferenceAnnotationsMenu()
160 JMenuItem menu = new JMenuItem();
161 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
163 // make up new annotations and add to dataset sequences
164 attachReferenceAnnotations(seqs, false, false);
166 testee.configureReferenceAnnotationsMenu(menu, seqs);
167 assertTrue(menu.isEnabled());
168 String s = MessageManager.getString("label.add_annotations_for");
169 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
170 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
171 assertEquals(expected, menu.getToolTipText());
175 * Test building the 'add reference annotations' menu for the case where
176 * several reference annotations are on the dataset and the sequences but not
177 * on the alignment. The menu item should be enabled, and acquire a tooltip
178 * which lists the annotation sources (calcIds) and type (labels).
180 @Test(groups = { "Functional" })
181 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
183 JMenuItem menu = new JMenuItem();
184 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
186 // make up new annotations and add to dataset sequences and sequences
187 attachReferenceAnnotations(seqs, true, false);
189 testee.configureReferenceAnnotationsMenu(menu, seqs);
190 assertTrue(menu.isEnabled());
191 String s = MessageManager.getString("label.add_annotations_for");
192 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
193 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
194 assertEquals(expected, menu.getToolTipText());
198 * Generate annotations and add to dataset sequences and (optionally)
199 * sequences and/or alignment
202 * @param addToSequence
203 * @param addToAlignment
205 private void attachReferenceAnnotations(List<SequenceI> seqs,
206 boolean addToSequence, boolean addToAlignment)
208 // PDB.secondary structure on Sequence0
209 AlignmentAnnotation annotation = new AlignmentAnnotation(
210 "secondary structure", "", 0);
211 annotation.setCalcId("PDB");
212 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
215 seqs.get(0).addAlignmentAnnotation(annotation);
219 this.alignment.addAnnotation(annotation);
222 // PDB.Temp on Sequence1
223 annotation = new AlignmentAnnotation("Temp", "", 0);
224 annotation.setCalcId("PDB");
225 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
228 seqs.get(1).addAlignmentAnnotation(annotation);
232 this.alignment.addAnnotation(annotation);
235 // JMOL.secondary structure on Sequence0
236 annotation = new AlignmentAnnotation("secondary structure", "", 0);
237 annotation.setCalcId("Jmol");
238 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
241 seqs.get(0).addAlignmentAnnotation(annotation);
245 this.alignment.addAnnotation(annotation);
250 * Test building the 'add reference annotations' menu for the case where there
251 * are two alignment views:
253 * <li>in one view, reference annotations have been added (are on the
254 * datasets, sequences and alignment)</li>
255 * <li>in the current view, reference annotations are on the dataset and
256 * sequence, but not the alignment</li>
258 * The menu item should be enabled, and acquire a tooltip which lists the
259 * annotation sources (calcIds) and type (labels).
261 @Test(groups = { "Functional" })
262 public void testConfigureReferenceAnnotationsMenu_twoViews()
267 * Test for building menu options including 'show' and 'hide' annotation
270 @Test(groups = { "Functional" })
271 public void testBuildAnnotationTypesMenus()
273 JMenu showMenu = new JMenu();
274 JMenu hideMenu = new JMenu();
275 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
277 // make up new annotations and add to sequences and to the alignment
279 // PDB.secondary structure on Sequence0
280 AlignmentAnnotation annotation = new AlignmentAnnotation(
281 "secondary structure", "", new Annotation[] {});
282 annotation.setCalcId("PDB");
283 annotation.visible = true;
284 seqs.get(0).addAlignmentAnnotation(annotation);
285 parentPanel.getAlignment().addAnnotation(annotation);
287 // JMOL.secondary structure on Sequence0 - hidden
288 annotation = new AlignmentAnnotation("secondary structure", "",
289 new Annotation[] {});
290 annotation.setCalcId("JMOL");
291 annotation.visible = false;
292 seqs.get(0).addAlignmentAnnotation(annotation);
293 parentPanel.getAlignment().addAnnotation(annotation);
295 // Jpred.SSP on Sequence0 - hidden
296 annotation = new AlignmentAnnotation("SSP", "", new Annotation[] {});
297 annotation.setCalcId("JPred");
298 annotation.visible = false;
299 seqs.get(0).addAlignmentAnnotation(annotation);
300 parentPanel.getAlignment().addAnnotation(annotation);
302 // PDB.Temp on Sequence1
303 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
304 annotation.setCalcId("PDB");
305 annotation.visible = true;
306 seqs.get(1).addAlignmentAnnotation(annotation);
307 parentPanel.getAlignment().addAnnotation(annotation);
310 * Expect menu options to show "secondary structure" and "SSP", and to hide
311 * "secondary structure" and "Temp". Tooltip should be calcId.
313 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
315 assertTrue(showMenu.isEnabled());
316 assertTrue(hideMenu.isEnabled());
318 Component[] showOptions = showMenu.getMenuComponents();
319 Component[] hideOptions = hideMenu.getMenuComponents();
321 assertEquals(4, showOptions.length); // includes 'All' and separator
322 assertEquals(4, hideOptions.length);
323 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
324 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
325 assertEquals(JSeparator.HORIZONTAL,
326 ((JSeparator) showOptions[1]).getOrientation());
327 assertEquals("secondary structure",
328 ((JMenuItem) showOptions[2]).getText());
329 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
330 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
331 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
333 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
334 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
335 assertEquals(JSeparator.HORIZONTAL,
336 ((JSeparator) hideOptions[1]).getOrientation());
337 assertEquals("secondary structure",
338 ((JMenuItem) hideOptions[2]).getText());
339 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
340 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
341 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
345 * Test for building menu options with only 'hide' annotation types enabled.
347 @Test(groups = { "Functional" })
348 public void testBuildAnnotationTypesMenus_showDisabled()
350 JMenu showMenu = new JMenu();
351 JMenu hideMenu = new JMenu();
352 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
354 // make up new annotations and add to sequences and to the alignment
356 // PDB.secondary structure on Sequence0
357 AlignmentAnnotation annotation = new AlignmentAnnotation(
358 "secondary structure", "", new Annotation[] {});
359 annotation.setCalcId("PDB");
360 annotation.visible = true;
361 seqs.get(0).addAlignmentAnnotation(annotation);
362 parentPanel.getAlignment().addAnnotation(annotation);
364 // PDB.Temp on Sequence1
365 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
366 annotation.setCalcId("PDB");
367 annotation.visible = true;
368 seqs.get(1).addAlignmentAnnotation(annotation);
369 parentPanel.getAlignment().addAnnotation(annotation);
372 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
373 * should be calcId. 'Show' menu should be disabled.
375 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
377 assertFalse(showMenu.isEnabled());
378 assertTrue(hideMenu.isEnabled());
380 Component[] showOptions = showMenu.getMenuComponents();
381 Component[] hideOptions = hideMenu.getMenuComponents();
383 assertEquals(2, showOptions.length); // includes 'All' and separator
384 assertEquals(4, hideOptions.length);
385 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
386 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
387 assertEquals(JSeparator.HORIZONTAL,
388 ((JSeparator) showOptions[1]).getOrientation());
390 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
391 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
392 assertEquals(JSeparator.HORIZONTAL,
393 ((JSeparator) hideOptions[1]).getOrientation());
394 assertEquals("secondary structure",
395 ((JMenuItem) hideOptions[2]).getText());
396 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
397 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
398 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
402 * Test for building menu options with only 'show' annotation types enabled.
404 @Test(groups = { "Functional" })
405 public void testBuildAnnotationTypesMenus_hideDisabled()
407 JMenu showMenu = new JMenu();
408 JMenu hideMenu = new JMenu();
409 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
411 // make up new annotations and add to sequences and to the alignment
413 // PDB.secondary structure on Sequence0
414 AlignmentAnnotation annotation = new AlignmentAnnotation(
415 "secondary structure", "", new Annotation[] {});
416 annotation.setCalcId("PDB");
417 annotation.visible = false;
418 seqs.get(0).addAlignmentAnnotation(annotation);
419 parentPanel.getAlignment().addAnnotation(annotation);
421 // PDB.Temp on Sequence1
422 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
423 annotation.setCalcId("PDB2");
424 annotation.visible = false;
425 seqs.get(1).addAlignmentAnnotation(annotation);
426 parentPanel.getAlignment().addAnnotation(annotation);
429 * Expect menu options to show "secondary structure" and "Temp". Tooltip
430 * should be calcId. 'hide' menu should be disabled.
432 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
434 assertTrue(showMenu.isEnabled());
435 assertFalse(hideMenu.isEnabled());
437 Component[] showOptions = showMenu.getMenuComponents();
438 Component[] hideOptions = hideMenu.getMenuComponents();
440 assertEquals(4, showOptions.length); // includes 'All' and separator
441 assertEquals(2, hideOptions.length);
442 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
443 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
444 assertEquals(JSeparator.HORIZONTAL,
445 ((JSeparator) showOptions[1]).getOrientation());
446 assertEquals("secondary structure",
447 ((JMenuItem) showOptions[2]).getText());
448 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
449 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
450 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
452 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
453 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
454 assertEquals(JSeparator.HORIZONTAL,
455 ((JSeparator) hideOptions[1]).getOrientation());
459 * Test for adding feature links
461 @Test(groups = { "Functional" })
462 public void testAddFeatureLinks()
464 // sequences from the alignment
465 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
467 // create list of links and list of DBRefs
468 List<String> links = new ArrayList<String>();
469 List<DBRefEntry> refs = new ArrayList<DBRefEntry>();
471 // links as might be added into Preferences | Connections dialog
472 links.add("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$"
473 + SEQUENCE_ID + "$");
474 links.add("UNIPROT | http://www.uniprot.org/uniprot/$" + DB_ACCESSION
476 links.add("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$"
477 + DB_ACCESSION + "$");
478 // Gene3D entry tests for case (in)sensitivity
479 links.add("Gene3D | http://gene3d.biochem.ucl.ac.uk/Gene3D/search?sterm=$"
480 + DB_ACCESSION + "$&mode=protein");
483 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "P83527"));
484 refs.add(new DBRefEntry("INTERPRO", "1", "IPR001041"));
485 refs.add(new DBRefEntry("INTERPRO", "1", "IPR006058"));
486 refs.add(new DBRefEntry("INTERPRO", "1", "IPR012675"));
489 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "Q9ZTS2"));
490 refs.add(new DBRefEntry("GENE3D", "1", "3.10.20.30"));
492 // add all the dbrefs to the sequences: Uniprot 1 each, Interpro all 3 to
493 // seq0, Gene3D to seq1
494 seqs.get(0).addDBRef(refs.get(0));
496 seqs.get(0).addDBRef(refs.get(1));
497 seqs.get(0).addDBRef(refs.get(2));
498 seqs.get(0).addDBRef(refs.get(3));
500 seqs.get(1).addDBRef(refs.get(4));
501 seqs.get(1).addDBRef(refs.get(5));
503 // get the Popup Menu for first sequence
504 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0), links);
505 Component[] seqItems = testee.sequenceMenu.getMenuComponents();
506 JMenu linkMenu = (JMenu) seqItems[6];
507 Component[] linkItems = linkMenu.getMenuComponents();
509 // check the number of links are the expected number
510 assertEquals(5, linkItems.length);
512 // first entry is EMBL-EBI which just uses sequence id not accession id?
513 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
515 // sequence id for each link should match corresponding DB accession id
516 for (int i = 1; i < 4; i++)
518 assertEquals(refs.get(i - 1).getSource(), ((JMenuItem) linkItems[i])
519 .getText().split("\\|")[0]);
520 assertEquals(refs.get(i - 1).getAccessionId(),
521 ((JMenuItem) linkItems[i])
522 .getText().split("\\|")[1]);
525 // get the Popup Menu for second sequence
526 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(1), links);
527 seqItems = testee.sequenceMenu.getMenuComponents();
528 linkMenu = (JMenu) seqItems[6];
529 linkItems = linkMenu.getMenuComponents();
531 // check the number of links are the expected number
532 assertEquals(3, linkItems.length);
534 // first entry is EMBL-EBI which just uses sequence id not accession id?
535 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
537 // sequence id for each link should match corresponding DB accession id
538 for (int i = 1; i < 3; i++)
540 assertEquals(refs.get(i + 3).getSource(), ((JMenuItem) linkItems[i])
541 .getText().split("\\|")[0].toUpperCase());
542 assertEquals(refs.get(i + 3).getAccessionId(),
543 ((JMenuItem) linkItems[i]).getText().split("\\|")[1]);
546 // if there are no valid links the Links submenu is disabled
547 List<String> nomatchlinks = new ArrayList<String>();
548 nomatchlinks.add("NOMATCH | http://www.uniprot.org/uniprot/$"
549 + DB_ACCESSION + "$");
551 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0),
553 seqItems = testee.sequenceMenu.getMenuComponents();
554 linkMenu = (JMenu) seqItems[6];
555 assertFalse(linkMenu.isEnabled());