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.DataSourceType;
37 import jalview.io.FileFormat;
38 import jalview.io.FormatAdapter;
39 import jalview.util.MessageManager;
41 import java.awt.Component;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.List;
46 import javax.swing.JMenu;
47 import javax.swing.JMenuItem;
48 import javax.swing.JPopupMenu;
49 import javax.swing.JSeparator;
51 import org.testng.annotations.BeforeClass;
52 import org.testng.annotations.BeforeMethod;
53 import org.testng.annotations.Test;
55 public class PopupMenuTest
58 @BeforeClass(alwaysRun = true)
59 public void setUpJvOptionPane()
61 JvOptionPane.setInteractiveMode(false);
62 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
65 // 4 sequences x 13 positions
66 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
68 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
70 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
71 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
76 AlignmentPanel parentPanel;
78 PopupMenu testee = null;
80 @BeforeMethod(alwaysRun = true)
81 public void setUp() throws IOException
83 alignment = new FormatAdapter().readFile(TEST_DATA,
84 DataSourceType.PASTE, FileFormat.Fasta);
85 AlignFrame af = new AlignFrame(alignment, 700, 500);
86 parentPanel = new AlignmentPanel(af, af.getViewport());
87 testee = new PopupMenu(parentPanel, null, null);
89 for (SequenceI seq : alignment.getSequences())
91 final AlignmentAnnotation annotation = new AlignmentAnnotation(
92 "label" + i, "desc" + i, i);
93 annotation.setCalcId("calcId" + i);
94 seq.addAlignmentAnnotation(annotation);
95 annotation.setSequenceRef(seq);
99 @Test(groups = { "Functional" })
100 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
102 JMenuItem menu = new JMenuItem();
103 List<SequenceI> seqs = new ArrayList<SequenceI>();
104 testee.configureReferenceAnnotationsMenu(menu, seqs);
105 assertFalse(menu.isEnabled());
107 menu.setEnabled(true);
108 testee.configureReferenceAnnotationsMenu(menu, null);
109 assertFalse(menu.isEnabled());
113 * Test building the 'add reference annotations' menu for the case where there
114 * are no reference annotations to add to the alignment. The menu item should
117 @Test(groups = { "Functional" })
118 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
120 JMenuItem menu = new JMenuItem();
123 * Initial state is that sequences have annotations, and have dataset
124 * sequences, but the dataset sequences have no annotations. Hence nothing
127 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
129 testee.configureReferenceAnnotationsMenu(menu, seqs);
130 assertFalse(menu.isEnabled());
134 * Test building the 'add reference annotations' menu for the case where all
135 * reference annotations are already on the alignment. The menu item should be
138 @Test(groups = { "Functional" })
139 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
141 JMenuItem menu = new JMenuItem();
142 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
144 // make up new annotations and add to dataset sequences, sequences and
146 attachReferenceAnnotations(seqs, true, true);
148 testee.configureReferenceAnnotationsMenu(menu, seqs);
149 assertFalse(menu.isEnabled());
153 * Test building the 'add reference annotations' menu for the case where
154 * several reference annotations are on the dataset but not on the sequences.
155 * The menu item should be enabled, and acquire a tooltip which lists the
156 * annotation sources (calcIds) and type (labels).
158 @Test(groups = { "Functional" })
159 public void testConfigureReferenceAnnotationsMenu()
161 JMenuItem menu = new JMenuItem();
162 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
164 // make up new annotations and add to dataset sequences
165 attachReferenceAnnotations(seqs, false, false);
167 testee.configureReferenceAnnotationsMenu(menu, seqs);
168 assertTrue(menu.isEnabled());
169 String s = MessageManager.getString("label.add_annotations_for");
170 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
171 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
172 assertEquals(expected, menu.getToolTipText());
176 * Test building the 'add reference annotations' menu for the case where
177 * several reference annotations are on the dataset and the sequences but not
178 * on the alignment. The menu item should be enabled, and acquire a tooltip
179 * which lists the annotation sources (calcIds) and type (labels).
181 @Test(groups = { "Functional" })
182 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
184 JMenuItem menu = new JMenuItem();
185 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
187 // make up new annotations and add to dataset sequences and sequences
188 attachReferenceAnnotations(seqs, true, false);
190 testee.configureReferenceAnnotationsMenu(menu, seqs);
191 assertTrue(menu.isEnabled());
192 String s = MessageManager.getString("label.add_annotations_for");
193 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
194 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
195 assertEquals(expected, menu.getToolTipText());
199 * Generate annotations and add to dataset sequences and (optionally)
200 * sequences and/or alignment
203 * @param addToSequence
204 * @param addToAlignment
206 private void attachReferenceAnnotations(List<SequenceI> seqs,
207 boolean addToSequence, boolean addToAlignment)
209 // PDB.secondary structure on Sequence0
210 AlignmentAnnotation annotation = new AlignmentAnnotation(
211 "secondary structure", "", 0);
212 annotation.setCalcId("PDB");
213 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
216 seqs.get(0).addAlignmentAnnotation(annotation);
220 this.alignment.addAnnotation(annotation);
223 // PDB.Temp on Sequence1
224 annotation = new AlignmentAnnotation("Temp", "", 0);
225 annotation.setCalcId("PDB");
226 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
229 seqs.get(1).addAlignmentAnnotation(annotation);
233 this.alignment.addAnnotation(annotation);
236 // JMOL.secondary structure on Sequence0
237 annotation = new AlignmentAnnotation("secondary structure", "", 0);
238 annotation.setCalcId("Jmol");
239 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
242 seqs.get(0).addAlignmentAnnotation(annotation);
246 this.alignment.addAnnotation(annotation);
251 * Test building the 'add reference annotations' menu for the case where there
252 * are two alignment views:
254 * <li>in one view, reference annotations have been added (are on the
255 * datasets, sequences and alignment)</li>
256 * <li>in the current view, reference annotations are on the dataset and
257 * sequence, but not the alignment</li>
259 * The menu item should be enabled, and acquire a tooltip which lists the
260 * annotation sources (calcIds) and type (labels).
262 @Test(groups = { "Functional" })
263 public void testConfigureReferenceAnnotationsMenu_twoViews()
268 * Test for building menu options including 'show' and 'hide' annotation
271 @Test(groups = { "Functional" })
272 public void testBuildAnnotationTypesMenus()
274 JMenu showMenu = new JMenu();
275 JMenu hideMenu = new JMenu();
276 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
278 // make up new annotations and add to sequences and to the alignment
280 // PDB.secondary structure on Sequence0
281 AlignmentAnnotation annotation = new AlignmentAnnotation(
282 "secondary structure", "", new Annotation[] {});
283 annotation.setCalcId("PDB");
284 annotation.visible = true;
285 seqs.get(0).addAlignmentAnnotation(annotation);
286 parentPanel.getAlignment().addAnnotation(annotation);
288 // JMOL.secondary structure on Sequence0 - hidden
289 annotation = new AlignmentAnnotation("secondary structure", "",
290 new Annotation[] {});
291 annotation.setCalcId("JMOL");
292 annotation.visible = false;
293 seqs.get(0).addAlignmentAnnotation(annotation);
294 parentPanel.getAlignment().addAnnotation(annotation);
296 // Jpred.SSP on Sequence0 - hidden
297 annotation = new AlignmentAnnotation("SSP", "", new Annotation[] {});
298 annotation.setCalcId("JPred");
299 annotation.visible = false;
300 seqs.get(0).addAlignmentAnnotation(annotation);
301 parentPanel.getAlignment().addAnnotation(annotation);
303 // PDB.Temp on Sequence1
304 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
305 annotation.setCalcId("PDB");
306 annotation.visible = true;
307 seqs.get(1).addAlignmentAnnotation(annotation);
308 parentPanel.getAlignment().addAnnotation(annotation);
311 * Expect menu options to show "secondary structure" and "SSP", and to hide
312 * "secondary structure" and "Temp". Tooltip should be calcId.
314 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
316 assertTrue(showMenu.isEnabled());
317 assertTrue(hideMenu.isEnabled());
319 Component[] showOptions = showMenu.getMenuComponents();
320 Component[] hideOptions = hideMenu.getMenuComponents();
322 assertEquals(4, showOptions.length); // includes 'All' and separator
323 assertEquals(4, hideOptions.length);
324 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
325 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
326 assertEquals(JSeparator.HORIZONTAL,
327 ((JSeparator) showOptions[1]).getOrientation());
328 assertEquals("secondary structure",
329 ((JMenuItem) showOptions[2]).getText());
330 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
331 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
332 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
334 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
335 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
336 assertEquals(JSeparator.HORIZONTAL,
337 ((JSeparator) hideOptions[1]).getOrientation());
338 assertEquals("secondary structure",
339 ((JMenuItem) hideOptions[2]).getText());
340 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
341 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
342 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
346 * Test for building menu options with only 'hide' annotation types enabled.
348 @Test(groups = { "Functional" })
349 public void testBuildAnnotationTypesMenus_showDisabled()
351 JMenu showMenu = new JMenu();
352 JMenu hideMenu = new JMenu();
353 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
355 // make up new annotations and add to sequences and to the alignment
357 // PDB.secondary structure on Sequence0
358 AlignmentAnnotation annotation = new AlignmentAnnotation(
359 "secondary structure", "", new Annotation[] {});
360 annotation.setCalcId("PDB");
361 annotation.visible = true;
362 seqs.get(0).addAlignmentAnnotation(annotation);
363 parentPanel.getAlignment().addAnnotation(annotation);
365 // PDB.Temp on Sequence1
366 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
367 annotation.setCalcId("PDB");
368 annotation.visible = true;
369 seqs.get(1).addAlignmentAnnotation(annotation);
370 parentPanel.getAlignment().addAnnotation(annotation);
373 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
374 * should be calcId. 'Show' menu should be disabled.
376 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
378 assertFalse(showMenu.isEnabled());
379 assertTrue(hideMenu.isEnabled());
381 Component[] showOptions = showMenu.getMenuComponents();
382 Component[] hideOptions = hideMenu.getMenuComponents();
384 assertEquals(2, showOptions.length); // includes 'All' and separator
385 assertEquals(4, hideOptions.length);
386 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
387 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
388 assertEquals(JSeparator.HORIZONTAL,
389 ((JSeparator) showOptions[1]).getOrientation());
391 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
392 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
393 assertEquals(JSeparator.HORIZONTAL,
394 ((JSeparator) hideOptions[1]).getOrientation());
395 assertEquals("secondary structure",
396 ((JMenuItem) hideOptions[2]).getText());
397 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
398 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
399 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
403 * Test for building menu options with only 'show' annotation types enabled.
405 @Test(groups = { "Functional" })
406 public void testBuildAnnotationTypesMenus_hideDisabled()
408 JMenu showMenu = new JMenu();
409 JMenu hideMenu = new JMenu();
410 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
412 // make up new annotations and add to sequences and to the alignment
414 // PDB.secondary structure on Sequence0
415 AlignmentAnnotation annotation = new AlignmentAnnotation(
416 "secondary structure", "", new Annotation[] {});
417 annotation.setCalcId("PDB");
418 annotation.visible = false;
419 seqs.get(0).addAlignmentAnnotation(annotation);
420 parentPanel.getAlignment().addAnnotation(annotation);
422 // PDB.Temp on Sequence1
423 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
424 annotation.setCalcId("PDB2");
425 annotation.visible = false;
426 seqs.get(1).addAlignmentAnnotation(annotation);
427 parentPanel.getAlignment().addAnnotation(annotation);
430 * Expect menu options to show "secondary structure" and "Temp". Tooltip
431 * should be calcId. 'hide' menu should be disabled.
433 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
435 assertTrue(showMenu.isEnabled());
436 assertFalse(hideMenu.isEnabled());
438 Component[] showOptions = showMenu.getMenuComponents();
439 Component[] hideOptions = hideMenu.getMenuComponents();
441 assertEquals(4, showOptions.length); // includes 'All' and separator
442 assertEquals(2, hideOptions.length);
443 assertEquals("All", ((JMenuItem) showOptions[0]).getText());
444 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
445 assertEquals(JSeparator.HORIZONTAL,
446 ((JSeparator) showOptions[1]).getOrientation());
447 assertEquals("secondary structure",
448 ((JMenuItem) showOptions[2]).getText());
449 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
450 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
451 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
453 assertEquals("All", ((JMenuItem) hideOptions[0]).getText());
454 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
455 assertEquals(JSeparator.HORIZONTAL,
456 ((JSeparator) hideOptions[1]).getOrientation());
460 * Test for adding feature links
462 @Test(groups = { "Functional" })
463 public void testAddFeatureLinks()
465 // sequences from the alignment
466 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
468 // create list of links and list of DBRefs
469 List<String> links = new ArrayList<String>();
470 List<DBRefEntry> refs = new ArrayList<DBRefEntry>();
472 // links as might be added into Preferences | Connections dialog
473 links.add("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$"
474 + SEQUENCE_ID + "$");
475 links.add("UNIPROT | http://www.uniprot.org/uniprot/$" + DB_ACCESSION
477 links.add("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$"
478 + DB_ACCESSION + "$");
479 // Gene3D entry tests for case (in)sensitivity
480 links.add("Gene3D | http://gene3d.biochem.ucl.ac.uk/Gene3D/search?sterm=$"
481 + DB_ACCESSION + "$&mode=protein");
484 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "P83527"));
485 refs.add(new DBRefEntry("INTERPRO", "1", "IPR001041"));
486 refs.add(new DBRefEntry("INTERPRO", "1", "IPR006058"));
487 refs.add(new DBRefEntry("INTERPRO", "1", "IPR012675"));
490 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "Q9ZTS2"));
491 refs.add(new DBRefEntry("GENE3D", "1", "3.10.20.30"));
493 // add all the dbrefs to the sequences: Uniprot 1 each, Interpro all 3 to
494 // seq0, Gene3D to seq1
495 seqs.get(0).addDBRef(refs.get(0));
497 seqs.get(0).addDBRef(refs.get(1));
498 seqs.get(0).addDBRef(refs.get(2));
499 seqs.get(0).addDBRef(refs.get(3));
501 seqs.get(1).addDBRef(refs.get(4));
502 seqs.get(1).addDBRef(refs.get(5));
504 // get the Popup Menu for first sequence
505 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0), links);
506 Component[] seqItems = testee.sequenceMenu.getMenuComponents();
507 JMenu linkMenu = (JMenu) seqItems[6];
508 Component[] linkItems = linkMenu.getMenuComponents();
510 // check the number of links are the expected number
511 assertEquals(5, linkItems.length);
513 // first entry is EMBL-EBI which just uses sequence id not accession id?
514 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
516 // sequence id for each link should match corresponding DB accession id
517 for (int i = 1; i < 4; i++)
519 assertEquals(refs.get(i - 1).getSource(), ((JMenuItem) linkItems[i])
520 .getText().split("\\|")[0]);
521 assertEquals(refs.get(i - 1).getAccessionId(),
522 ((JMenuItem) linkItems[i])
523 .getText().split("\\|")[1]);
526 // get the Popup Menu for second sequence
527 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(1), links);
528 seqItems = testee.sequenceMenu.getMenuComponents();
529 linkMenu = (JMenu) seqItems[6];
530 linkItems = linkMenu.getMenuComponents();
532 // check the number of links are the expected number
533 assertEquals(3, linkItems.length);
535 // first entry is EMBL-EBI which just uses sequence id not accession id?
536 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
538 // sequence id for each link should match corresponding DB accession id
539 for (int i = 1; i < 3; i++)
541 assertEquals(refs.get(i + 3).getSource(), ((JMenuItem) linkItems[i])
542 .getText().split("\\|")[0].toUpperCase());
543 assertEquals(refs.get(i + 3).getAccessionId(),
544 ((JMenuItem) linkItems[i]).getText().split("\\|")[1]);
547 // if there are no valid links the Links submenu is disabled
548 List<String> nomatchlinks = new ArrayList<String>();
549 nomatchlinks.add("NOMATCH | http://www.uniprot.org/uniprot/$"
550 + DB_ACCESSION + "$");
552 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0),
554 seqItems = testee.sequenceMenu.getMenuComponents();
555 linkMenu = (JMenu) seqItems[6];
556 assertFalse(linkMenu.isEnabled());