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.bin.Cache;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.Annotation;
33 import jalview.datamodel.DBRefEntry;
34 import jalview.datamodel.DBRefSource;
35 import jalview.datamodel.SequenceFeature;
36 import jalview.datamodel.SequenceI;
37 import jalview.io.DataSourceType;
38 import jalview.io.FileFormat;
39 import jalview.io.FormatAdapter;
40 import jalview.urls.api.UrlProviderFactoryI;
41 import jalview.urls.desktop.DesktopUrlProviderFactory;
42 import jalview.util.MessageManager;
43 import jalview.util.UrlConstants;
45 import java.awt.Component;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.List;
51 import javax.swing.JMenu;
52 import javax.swing.JMenuItem;
53 import javax.swing.JPopupMenu;
54 import javax.swing.JSeparator;
56 import org.testng.annotations.BeforeClass;
57 import org.testng.annotations.BeforeMethod;
58 import org.testng.annotations.Test;
60 public class PopupMenuTest
63 @BeforeClass(alwaysRun = true)
64 public void setUpJvOptionPane()
66 JvOptionPane.setInteractiveMode(false);
67 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
70 // 4 sequences x 13 positions
71 final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
73 + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
75 + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
76 + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
81 AlignmentPanel parentPanel;
83 PopupMenu testee = null;
85 @BeforeMethod(alwaysRun = true)
86 public void setUp() throws IOException
88 Cache.loadProperties("test/jalview/io/testProps.jvprops");
89 String inMenuString = ("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$"
93 + "UNIPROT | http://www.uniprot.org/uniprot/$" + DB_ACCESSION + "$")
95 + ("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$"
99 // Gene3D entry tests for case (in)sensitivity
100 ("Gene3D | http://gene3d.biochem.ucl.ac.uk/Gene3D/search?sterm=$"
101 + DB_ACCESSION + "$&mode=protein");
103 UrlProviderFactoryI factory = new DesktopUrlProviderFactory(
104 UrlConstants.DEFAULT_LABEL, inMenuString, "");
105 Preferences.sequenceUrlLinks = factory.createUrlProvider();
107 alignment = new FormatAdapter().readFile(TEST_DATA,
108 DataSourceType.PASTE, FileFormat.Fasta);
109 AlignFrame af = new AlignFrame(alignment, 700, 500);
110 parentPanel = new AlignmentPanel(af, af.getViewport());
111 testee = new PopupMenu(parentPanel, null, null);
113 for (SequenceI seq : alignment.getSequences())
115 final AlignmentAnnotation annotation = new AlignmentAnnotation(
116 "label" + i, "desc" + i, i);
117 annotation.setCalcId("calcId" + i);
118 seq.addAlignmentAnnotation(annotation);
119 annotation.setSequenceRef(seq);
123 @Test(groups = { "Functional" })
124 public void testConfigureReferenceAnnotationsMenu_noSequenceSelected()
126 JMenuItem menu = new JMenuItem();
127 List<SequenceI> seqs = new ArrayList<SequenceI>();
128 testee.configureReferenceAnnotationsMenu(menu, seqs);
129 assertFalse(menu.isEnabled());
131 menu.setEnabled(true);
132 testee.configureReferenceAnnotationsMenu(menu, null);
133 assertFalse(menu.isEnabled());
137 * Test building the 'add reference annotations' menu for the case where there
138 * are no reference annotations to add to the alignment. The menu item should
141 @Test(groups = { "Functional" })
142 public void testConfigureReferenceAnnotationsMenu_noReferenceAnnotations()
144 JMenuItem menu = new JMenuItem();
147 * Initial state is that sequences have annotations, and have dataset
148 * sequences, but the dataset sequences have no annotations. Hence nothing
151 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
153 testee.configureReferenceAnnotationsMenu(menu, seqs);
154 assertFalse(menu.isEnabled());
158 * Test building the 'add reference annotations' menu for the case where all
159 * reference annotations are already on the alignment. The menu item should be
162 @Test(groups = { "Functional" })
163 public void testConfigureReferenceAnnotationsMenu_alreadyAdded()
165 JMenuItem menu = new JMenuItem();
166 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
168 // make up new annotations and add to dataset sequences, sequences and
170 attachReferenceAnnotations(seqs, true, true);
172 testee.configureReferenceAnnotationsMenu(menu, seqs);
173 assertFalse(menu.isEnabled());
177 * Test building the 'add reference annotations' menu for the case where
178 * several reference annotations are on the dataset but not on the sequences.
179 * The menu item should be enabled, and acquire a tooltip which lists the
180 * annotation sources (calcIds) and type (labels).
182 @Test(groups = { "Functional" })
183 public void testConfigureReferenceAnnotationsMenu()
185 JMenuItem menu = new JMenuItem();
186 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
188 // make up new annotations and add to dataset sequences
189 attachReferenceAnnotations(seqs, false, false);
191 testee.configureReferenceAnnotationsMenu(menu, seqs);
192 assertTrue(menu.isEnabled());
193 String s = MessageManager.getString("label.add_annotations_for");
194 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
195 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
196 assertEquals(expected, menu.getToolTipText());
200 * Test building the 'add reference annotations' menu for the case where
201 * several reference annotations are on the dataset and the sequences but not
202 * on the alignment. The menu item should be enabled, and acquire a tooltip
203 * which lists the annotation sources (calcIds) and type (labels).
205 @Test(groups = { "Functional" })
206 public void testConfigureReferenceAnnotationsMenu_notOnAlignment()
208 JMenuItem menu = new JMenuItem();
209 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
211 // make up new annotations and add to dataset sequences and sequences
212 attachReferenceAnnotations(seqs, true, false);
214 testee.configureReferenceAnnotationsMenu(menu, seqs);
215 assertTrue(menu.isEnabled());
216 String s = MessageManager.getString("label.add_annotations_for");
217 String expected = "<html><style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
218 + s + "<br/>Jmol/secondary structure<br/>PDB/Temp</p></html>";
219 assertEquals(expected, menu.getToolTipText());
223 * Generate annotations and add to dataset sequences and (optionally)
224 * sequences and/or alignment
227 * @param addToSequence
228 * @param addToAlignment
230 private void attachReferenceAnnotations(List<SequenceI> seqs,
231 boolean addToSequence, boolean addToAlignment)
233 // PDB.secondary structure on Sequence0
234 AlignmentAnnotation annotation = new AlignmentAnnotation(
235 "secondary structure", "", 0);
236 annotation.setCalcId("PDB");
237 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
240 seqs.get(0).addAlignmentAnnotation(annotation);
244 this.alignment.addAnnotation(annotation);
247 // PDB.Temp on Sequence1
248 annotation = new AlignmentAnnotation("Temp", "", 0);
249 annotation.setCalcId("PDB");
250 seqs.get(1).getDatasetSequence().addAlignmentAnnotation(annotation);
253 seqs.get(1).addAlignmentAnnotation(annotation);
257 this.alignment.addAnnotation(annotation);
260 // JMOL.secondary structure on Sequence0
261 annotation = new AlignmentAnnotation("secondary structure", "", 0);
262 annotation.setCalcId("Jmol");
263 seqs.get(0).getDatasetSequence().addAlignmentAnnotation(annotation);
266 seqs.get(0).addAlignmentAnnotation(annotation);
270 this.alignment.addAnnotation(annotation);
275 * Test building the 'add reference annotations' menu for the case where there
276 * are two alignment views:
278 * <li>in one view, reference annotations have been added (are on the
279 * datasets, sequences and alignment)</li>
280 * <li>in the current view, reference annotations are on the dataset and
281 * sequence, but not the alignment</li>
283 * The menu item should be enabled, and acquire a tooltip which lists the
284 * annotation sources (calcIds) and type (labels).
286 @Test(groups = { "Functional" })
287 public void testConfigureReferenceAnnotationsMenu_twoViews()
292 * Test for building menu options including 'show' and 'hide' annotation
295 @Test(groups = { "Functional" })
296 public void testBuildAnnotationTypesMenus()
298 JMenu showMenu = new JMenu();
299 JMenu hideMenu = new JMenu();
300 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
302 // make up new annotations and add to sequences and to the alignment
304 // PDB.secondary structure on Sequence0
305 AlignmentAnnotation annotation = new AlignmentAnnotation(
306 "secondary structure", "", new Annotation[] {});
307 annotation.setCalcId("PDB");
308 annotation.visible = true;
309 seqs.get(0).addAlignmentAnnotation(annotation);
310 parentPanel.getAlignment().addAnnotation(annotation);
312 // JMOL.secondary structure on Sequence0 - hidden
313 annotation = new AlignmentAnnotation("secondary structure", "",
314 new Annotation[] {});
315 annotation.setCalcId("JMOL");
316 annotation.visible = false;
317 seqs.get(0).addAlignmentAnnotation(annotation);
318 parentPanel.getAlignment().addAnnotation(annotation);
320 // Jpred.SSP on Sequence0 - hidden
321 annotation = new AlignmentAnnotation("SSP", "", new Annotation[] {});
322 annotation.setCalcId("JPred");
323 annotation.visible = false;
324 seqs.get(0).addAlignmentAnnotation(annotation);
325 parentPanel.getAlignment().addAnnotation(annotation);
327 // PDB.Temp on Sequence1
328 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
329 annotation.setCalcId("PDB");
330 annotation.visible = true;
331 seqs.get(1).addAlignmentAnnotation(annotation);
332 parentPanel.getAlignment().addAnnotation(annotation);
335 * Expect menu options to show "secondary structure" and "SSP", and to hide
336 * "secondary structure" and "Temp". Tooltip should be calcId.
338 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
340 assertTrue(showMenu.isEnabled());
341 assertTrue(hideMenu.isEnabled());
343 Component[] showOptions = showMenu.getMenuComponents();
344 Component[] hideOptions = hideMenu.getMenuComponents();
346 assertEquals(4, showOptions.length); // includes 'All' and separator
347 assertEquals(4, hideOptions.length);
348 String all = MessageManager.getString("label.all");
349 assertEquals(all, ((JMenuItem) showOptions[0]).getText());
350 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
351 assertEquals(JSeparator.HORIZONTAL,
352 ((JSeparator) showOptions[1]).getOrientation());
353 assertEquals("secondary structure",
354 ((JMenuItem) showOptions[2]).getText());
355 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
356 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
357 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
359 assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
360 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
361 assertEquals(JSeparator.HORIZONTAL,
362 ((JSeparator) hideOptions[1]).getOrientation());
363 assertEquals("secondary structure",
364 ((JMenuItem) hideOptions[2]).getText());
365 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
366 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
367 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
371 * Test for building menu options with only 'hide' annotation types enabled.
373 @Test(groups = { "Functional" })
374 public void testBuildAnnotationTypesMenus_showDisabled()
376 JMenu showMenu = new JMenu();
377 JMenu hideMenu = new JMenu();
378 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
380 // make up new annotations and add to sequences and to the alignment
382 // PDB.secondary structure on Sequence0
383 AlignmentAnnotation annotation = new AlignmentAnnotation(
384 "secondary structure", "", new Annotation[] {});
385 annotation.setCalcId("PDB");
386 annotation.visible = true;
387 seqs.get(0).addAlignmentAnnotation(annotation);
388 parentPanel.getAlignment().addAnnotation(annotation);
390 // PDB.Temp on Sequence1
391 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
392 annotation.setCalcId("PDB");
393 annotation.visible = true;
394 seqs.get(1).addAlignmentAnnotation(annotation);
395 parentPanel.getAlignment().addAnnotation(annotation);
398 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
399 * should be calcId. 'Show' menu should be disabled.
401 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
403 assertFalse(showMenu.isEnabled());
404 assertTrue(hideMenu.isEnabled());
406 Component[] showOptions = showMenu.getMenuComponents();
407 Component[] hideOptions = hideMenu.getMenuComponents();
409 assertEquals(2, showOptions.length); // includes 'All' and separator
410 assertEquals(4, hideOptions.length);
411 String all = MessageManager.getString("label.all");
412 assertEquals(all, ((JMenuItem) showOptions[0]).getText());
413 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
414 assertEquals(JSeparator.HORIZONTAL,
415 ((JSeparator) showOptions[1]).getOrientation());
417 assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
418 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
419 assertEquals(JSeparator.HORIZONTAL,
420 ((JSeparator) hideOptions[1]).getOrientation());
421 assertEquals("secondary structure",
422 ((JMenuItem) hideOptions[2]).getText());
423 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
424 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
425 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
429 * Test for building menu options with only 'show' annotation types enabled.
431 @Test(groups = { "Functional" })
432 public void testBuildAnnotationTypesMenus_hideDisabled()
434 JMenu showMenu = new JMenu();
435 JMenu hideMenu = new JMenu();
436 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
438 // make up new annotations and add to sequences and to the alignment
440 // PDB.secondary structure on Sequence0
441 AlignmentAnnotation annotation = new AlignmentAnnotation(
442 "secondary structure", "", new Annotation[] {});
443 annotation.setCalcId("PDB");
444 annotation.visible = false;
445 seqs.get(0).addAlignmentAnnotation(annotation);
446 parentPanel.getAlignment().addAnnotation(annotation);
448 // PDB.Temp on Sequence1
449 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
450 annotation.setCalcId("PDB2");
451 annotation.visible = false;
452 seqs.get(1).addAlignmentAnnotation(annotation);
453 parentPanel.getAlignment().addAnnotation(annotation);
456 * Expect menu options to show "secondary structure" and "Temp". Tooltip
457 * should be calcId. 'hide' menu should be disabled.
459 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
461 assertTrue(showMenu.isEnabled());
462 assertFalse(hideMenu.isEnabled());
464 Component[] showOptions = showMenu.getMenuComponents();
465 Component[] hideOptions = hideMenu.getMenuComponents();
467 assertEquals(4, showOptions.length); // includes 'All' and separator
468 assertEquals(2, hideOptions.length);
469 String all = MessageManager.getString("label.all");
470 assertEquals(all, ((JMenuItem) showOptions[0]).getText());
471 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
472 assertEquals(JSeparator.HORIZONTAL,
473 ((JSeparator) showOptions[1]).getOrientation());
474 assertEquals("secondary structure",
475 ((JMenuItem) showOptions[2]).getText());
476 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
477 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
478 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
480 assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
481 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
482 assertEquals(JSeparator.HORIZONTAL,
483 ((JSeparator) hideOptions[1]).getOrientation());
487 * Test for adding feature links
489 @Test(groups = { "Functional" })
490 public void testAddFeatureLinks()
492 // sequences from the alignment
493 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
495 // create list of links and list of DBRefs
496 List<String> links = new ArrayList<String>();
497 List<DBRefEntry> refs = new ArrayList<DBRefEntry>();
499 // links as might be added into Preferences | Connections dialog
500 links.add("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$"
501 + SEQUENCE_ID + "$");
502 links.add("UNIPROT | http://www.uniprot.org/uniprot/$" + DB_ACCESSION
504 links.add("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$"
505 + DB_ACCESSION + "$");
506 // Gene3D entry tests for case (in)sensitivity
507 links.add("Gene3D | http://gene3d.biochem.ucl.ac.uk/Gene3D/search?sterm=$"
508 + DB_ACCESSION + "$&mode=protein");
511 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "P83527"));
512 refs.add(new DBRefEntry("INTERPRO", "1", "IPR001041"));
513 refs.add(new DBRefEntry("INTERPRO", "1", "IPR006058"));
514 refs.add(new DBRefEntry("INTERPRO", "1", "IPR012675"));
517 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "Q9ZTS2"));
518 refs.add(new DBRefEntry("GENE3D", "1", "3.10.20.30"));
520 // add all the dbrefs to the sequences: Uniprot 1 each, Interpro all 3 to
521 // seq0, Gene3D to seq1
522 SequenceI seq = seqs.get(0);
523 seq.addDBRef(refs.get(0));
525 seq.addDBRef(refs.get(1));
526 seq.addDBRef(refs.get(2));
527 seq.addDBRef(refs.get(3));
529 seqs.get(1).addDBRef(refs.get(4));
530 seqs.get(1).addDBRef(refs.get(5));
532 // get the Popup Menu for first sequence
533 List<SequenceFeature> noFeatures = Collections.<SequenceFeature> emptyList();
534 testee = new PopupMenu(parentPanel, seq, noFeatures);
535 Component[] seqItems = testee.sequenceMenu.getMenuComponents();
536 JMenu linkMenu = (JMenu) seqItems[6];
537 Component[] linkItems = linkMenu.getMenuComponents();
539 // check the number of links are the expected number
540 assertEquals(5, linkItems.length);
542 // first entry is EMBL-EBI which just uses sequence id not accession id?
543 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
545 // sequence id for each link should match corresponding DB accession id
546 for (int i = 1; i < 4; i++)
548 String msg = seq.getName() + " link[" + i + "]";
549 assertEquals(msg, refs.get(i - 1).getSource(),
550 ((JMenuItem) linkItems[i])
551 .getText().split("\\|")[0]);
552 assertEquals(msg, refs.get(i - 1).getAccessionId(),
553 ((JMenuItem) linkItems[i])
554 .getText().split("\\|")[1]);
557 // get the Popup Menu for second sequence
559 testee = new PopupMenu(parentPanel, seq, noFeatures);
560 seqItems = testee.sequenceMenu.getMenuComponents();
561 linkMenu = (JMenu) seqItems[6];
562 linkItems = linkMenu.getMenuComponents();
564 // check the number of links are the expected number
565 assertEquals(3, linkItems.length);
567 // first entry is EMBL-EBI which just uses sequence id not accession id?
568 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
570 // sequence id for each link should match corresponding DB accession id
571 for (int i = 1; i < 3; i++)
573 String msg = seq.getName() + " link[" + i + "]";
574 assertEquals(msg, refs.get(i + 3).getSource(),
575 ((JMenuItem) linkItems[i])
576 .getText().split("\\|")[0].toUpperCase());
577 assertEquals(msg, refs.get(i + 3).getAccessionId(),
578 ((JMenuItem) linkItems[i]).getText().split("\\|")[1]);
581 // if there are no valid links the Links submenu is disabled
582 List<String> nomatchlinks = new ArrayList<String>();
583 nomatchlinks.add("NOMATCH | http://www.uniprot.org/uniprot/$"
584 + DB_ACCESSION + "$");
586 testee = new PopupMenu(parentPanel, seq, noFeatures);
587 seqItems = testee.sequenceMenu.getMenuComponents();
588 linkMenu = (JMenu) seqItems[6];
589 assertFalse(linkMenu.isEnabled());