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 String all = MessageManager.getString("label.all");
325 assertEquals(all, ((JMenuItem) showOptions[0]).getText());
326 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
327 assertEquals(JSeparator.HORIZONTAL,
328 ((JSeparator) showOptions[1]).getOrientation());
329 assertEquals("secondary structure",
330 ((JMenuItem) showOptions[2]).getText());
331 assertEquals("JMOL", ((JMenuItem) showOptions[2]).getToolTipText());
332 assertEquals("SSP", ((JMenuItem) showOptions[3]).getText());
333 assertEquals("JPred", ((JMenuItem) showOptions[3]).getToolTipText());
335 assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
336 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
337 assertEquals(JSeparator.HORIZONTAL,
338 ((JSeparator) hideOptions[1]).getOrientation());
339 assertEquals("secondary structure",
340 ((JMenuItem) hideOptions[2]).getText());
341 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
342 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
343 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
347 * Test for building menu options with only 'hide' annotation types enabled.
349 @Test(groups = { "Functional" })
350 public void testBuildAnnotationTypesMenus_showDisabled()
352 JMenu showMenu = new JMenu();
353 JMenu hideMenu = new JMenu();
354 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
356 // make up new annotations and add to sequences and to the alignment
358 // PDB.secondary structure on Sequence0
359 AlignmentAnnotation annotation = new AlignmentAnnotation(
360 "secondary structure", "", new Annotation[] {});
361 annotation.setCalcId("PDB");
362 annotation.visible = true;
363 seqs.get(0).addAlignmentAnnotation(annotation);
364 parentPanel.getAlignment().addAnnotation(annotation);
366 // PDB.Temp on Sequence1
367 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
368 annotation.setCalcId("PDB");
369 annotation.visible = true;
370 seqs.get(1).addAlignmentAnnotation(annotation);
371 parentPanel.getAlignment().addAnnotation(annotation);
374 * Expect menu options to hide "secondary structure" and "Temp". Tooltip
375 * should be calcId. 'Show' menu should be disabled.
377 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
379 assertFalse(showMenu.isEnabled());
380 assertTrue(hideMenu.isEnabled());
382 Component[] showOptions = showMenu.getMenuComponents();
383 Component[] hideOptions = hideMenu.getMenuComponents();
385 assertEquals(2, showOptions.length); // includes 'All' and separator
386 assertEquals(4, hideOptions.length);
387 String all = MessageManager.getString("label.all");
388 assertEquals(all, ((JMenuItem) showOptions[0]).getText());
389 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
390 assertEquals(JSeparator.HORIZONTAL,
391 ((JSeparator) showOptions[1]).getOrientation());
393 assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
394 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
395 assertEquals(JSeparator.HORIZONTAL,
396 ((JSeparator) hideOptions[1]).getOrientation());
397 assertEquals("secondary structure",
398 ((JMenuItem) hideOptions[2]).getText());
399 assertEquals("PDB", ((JMenuItem) hideOptions[2]).getToolTipText());
400 assertEquals("Temp", ((JMenuItem) hideOptions[3]).getText());
401 assertEquals("PDB", ((JMenuItem) hideOptions[3]).getToolTipText());
405 * Test for building menu options with only 'show' annotation types enabled.
407 @Test(groups = { "Functional" })
408 public void testBuildAnnotationTypesMenus_hideDisabled()
410 JMenu showMenu = new JMenu();
411 JMenu hideMenu = new JMenu();
412 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
414 // make up new annotations and add to sequences and to the alignment
416 // PDB.secondary structure on Sequence0
417 AlignmentAnnotation annotation = new AlignmentAnnotation(
418 "secondary structure", "", new Annotation[] {});
419 annotation.setCalcId("PDB");
420 annotation.visible = false;
421 seqs.get(0).addAlignmentAnnotation(annotation);
422 parentPanel.getAlignment().addAnnotation(annotation);
424 // PDB.Temp on Sequence1
425 annotation = new AlignmentAnnotation("Temp", "", new Annotation[] {});
426 annotation.setCalcId("PDB2");
427 annotation.visible = false;
428 seqs.get(1).addAlignmentAnnotation(annotation);
429 parentPanel.getAlignment().addAnnotation(annotation);
432 * Expect menu options to show "secondary structure" and "Temp". Tooltip
433 * should be calcId. 'hide' menu should be disabled.
435 testee.buildAnnotationTypesMenus(showMenu, hideMenu, seqs);
437 assertTrue(showMenu.isEnabled());
438 assertFalse(hideMenu.isEnabled());
440 Component[] showOptions = showMenu.getMenuComponents();
441 Component[] hideOptions = hideMenu.getMenuComponents();
443 assertEquals(4, showOptions.length); // includes 'All' and separator
444 assertEquals(2, hideOptions.length);
445 String all = MessageManager.getString("label.all");
446 assertEquals(all, ((JMenuItem) showOptions[0]).getText());
447 assertTrue(showOptions[1] instanceof JPopupMenu.Separator);
448 assertEquals(JSeparator.HORIZONTAL,
449 ((JSeparator) showOptions[1]).getOrientation());
450 assertEquals("secondary structure",
451 ((JMenuItem) showOptions[2]).getText());
452 assertEquals("PDB", ((JMenuItem) showOptions[2]).getToolTipText());
453 assertEquals("Temp", ((JMenuItem) showOptions[3]).getText());
454 assertEquals("PDB2", ((JMenuItem) showOptions[3]).getToolTipText());
456 assertEquals(all, ((JMenuItem) hideOptions[0]).getText());
457 assertTrue(hideOptions[1] instanceof JPopupMenu.Separator);
458 assertEquals(JSeparator.HORIZONTAL,
459 ((JSeparator) hideOptions[1]).getOrientation());
463 * Test for adding feature links
465 @Test(groups = { "Functional" })
466 public void testAddFeatureLinks()
468 // sequences from the alignment
469 List<SequenceI> seqs = parentPanel.getAlignment().getSequences();
471 // create list of links and list of DBRefs
472 List<String> links = new ArrayList<String>();
473 List<DBRefEntry> refs = new ArrayList<DBRefEntry>();
475 // links as might be added into Preferences | Connections dialog
476 links.add("EMBL-EBI Search | http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$"
477 + SEQUENCE_ID + "$");
478 links.add("UNIPROT | http://www.uniprot.org/uniprot/$" + DB_ACCESSION
480 links.add("INTERPRO | http://www.ebi.ac.uk/interpro/entry/$"
481 + DB_ACCESSION + "$");
482 // Gene3D entry tests for case (in)sensitivity
483 links.add("Gene3D | http://gene3d.biochem.ucl.ac.uk/Gene3D/search?sterm=$"
484 + DB_ACCESSION + "$&mode=protein");
487 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "P83527"));
488 refs.add(new DBRefEntry("INTERPRO", "1", "IPR001041"));
489 refs.add(new DBRefEntry("INTERPRO", "1", "IPR006058"));
490 refs.add(new DBRefEntry("INTERPRO", "1", "IPR012675"));
493 refs.add(new DBRefEntry(DBRefSource.UNIPROT, "1", "Q9ZTS2"));
494 refs.add(new DBRefEntry("GENE3D", "1", "3.10.20.30"));
496 // add all the dbrefs to the sequences: Uniprot 1 each, Interpro all 3 to
497 // seq0, Gene3D to seq1
498 seqs.get(0).addDBRef(refs.get(0));
500 seqs.get(0).addDBRef(refs.get(1));
501 seqs.get(0).addDBRef(refs.get(2));
502 seqs.get(0).addDBRef(refs.get(3));
504 seqs.get(1).addDBRef(refs.get(4));
505 seqs.get(1).addDBRef(refs.get(5));
507 // get the Popup Menu for first sequence
508 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0), links);
509 Component[] seqItems = testee.sequenceMenu.getMenuComponents();
510 JMenu linkMenu = (JMenu) seqItems[6];
511 Component[] linkItems = linkMenu.getMenuComponents();
513 // check the number of links are the expected number
514 assertEquals(5, linkItems.length);
516 // first entry is EMBL-EBI which just uses sequence id not accession id?
517 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
519 // sequence id for each link should match corresponding DB accession id
520 for (int i = 1; i < 4; i++)
522 assertEquals(refs.get(i - 1).getSource(), ((JMenuItem) linkItems[i])
523 .getText().split("\\|")[0]);
524 assertEquals(refs.get(i - 1).getAccessionId(),
525 ((JMenuItem) linkItems[i])
526 .getText().split("\\|")[1]);
529 // get the Popup Menu for second sequence
530 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(1), links);
531 seqItems = testee.sequenceMenu.getMenuComponents();
532 linkMenu = (JMenu) seqItems[6];
533 linkItems = linkMenu.getMenuComponents();
535 // check the number of links are the expected number
536 assertEquals(3, linkItems.length);
538 // first entry is EMBL-EBI which just uses sequence id not accession id?
539 assertEquals("EMBL-EBI Search", ((JMenuItem) linkItems[0]).getText());
541 // sequence id for each link should match corresponding DB accession id
542 for (int i = 1; i < 3; i++)
544 assertEquals(refs.get(i + 3).getSource(), ((JMenuItem) linkItems[i])
545 .getText().split("\\|")[0].toUpperCase());
546 assertEquals(refs.get(i + 3).getAccessionId(),
547 ((JMenuItem) linkItems[i]).getText().split("\\|")[1]);
550 // if there are no valid links the Links submenu is disabled
551 List<String> nomatchlinks = new ArrayList<String>();
552 nomatchlinks.add("NOMATCH | http://www.uniprot.org/uniprot/$"
553 + DB_ACCESSION + "$");
555 testee = new PopupMenu(parentPanel, (Sequence) seqs.get(0),
557 seqItems = testee.sequenceMenu.getMenuComponents();
558 linkMenu = (JMenu) seqItems[6];
559 assertFalse(linkMenu.isEnabled());