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.Assert.assertEquals;
24 import static org.testng.Assert.assertNotNull;
25 import static org.testng.Assert.assertTrue;
27 import jalview.analysis.CrossRef;
28 import jalview.api.AlignmentViewPanel;
29 import jalview.datamodel.AlignedCodonFrame;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.AlignmentTest;
32 import jalview.datamodel.SequenceI;
33 import jalview.gui.AlignFrame;
34 import jalview.gui.CrossRefAction;
35 import jalview.gui.Desktop;
36 import jalview.gui.JvOptionPane;
37 import jalview.project.Jalview2XML;
38 import jalview.util.DBRefUtils;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
48 import org.testng.Assert;
49 import org.testng.annotations.BeforeClass;
50 import org.testng.annotations.DataProvider;
51 import org.testng.annotations.Test;
53 import junit.extensions.PA;
55 @Test(singleThreaded = true)
56 public class CrossRef2xmlTests extends Jalview2xmlBase
60 @BeforeClass(alwaysRun = true)
61 public void setUpJvOptionPane()
63 JvOptionPane.setInteractiveMode(false);
64 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
67 @Test(groups = { "Functional" }, enabled = true)
68 public void openCrossrefsForEnsemblTwice()
70 AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
71 "examples/testdata/CantShowEnsemblCrossrefsTwice.jvp",
73 assertNotNull(af, "Couldn't load test's project.");
74 AlignmentI origAlig = af.getViewport().getAlignment();
75 List<String> source = new CrossRef(origAlig.getSequencesArray(),
76 origAlig.getDataset()).findXrefSourcesForSequences(true);
77 assertEquals(source.size(), 1, "Expected just one crossref to show.");
78 List<AlignmentViewPanel> views;
80 // try to show once - in a code block so handler is forgotten about
81 CrossRefAction xref1 = CrossRefAction.getHandlerFor(
82 origAlig.getSequencesArray(), true, source.get(0), af);
86 views = (List<AlignmentViewPanel>) PA.getValue(xref1, "xrefViews");
87 assertTrue(views.size() > 0,
88 "Couldn't get cross ref on first attempt (SERIOUS FAIL).");
89 } catch (Exception ex)
91 Assert.fail("Unexpected Exception for first xref action", ex);
96 // now just try it again
97 CrossRefAction xref2 = CrossRefAction.getHandlerFor(
98 origAlig.getSequencesArray(), true, source.get(0), af);
102 views = (List<AlignmentViewPanel>) PA.getValue(xref2, "xrefViews");
103 assertTrue(views.size() > 0,
104 "Couldn't get cross ref on second attempt (SERIOUS FAIL).");
105 } catch (Exception ex)
107 Assert.fail("Unexpected Exception for second xref action", ex);
109 // TODO : check that both views contain the same data
112 @DataProvider(name = "initialAccessions")
113 static Object[][] getAccessions()
115 return new String[][] { { "UNIPROT", "P00338" },
116 { "UNIPROT", "Q8Z9G6" },
117 { "ENSEMBLGENOMES", "CAD01290" } };
121 * test store and recovery of all reachable cross refs from all reachable
122 * crossrefs for one or more fetched db refs. Currently, this test has a known
130 dataProvider = "initialAccessions",
132 public void testRetrieveAndShowCrossref(String forSource,
133 String forAccession) throws Exception
136 List<String> failedDBRetr = new ArrayList<>();
137 List<String> failedXrefMenuItems = new ArrayList<>();
138 List<String> failedProjectRecoveries = new ArrayList<>();
139 // only search for ensembl or Uniprot crossrefs
140 List<String> limit=Arrays.asList(new String[] {
141 DBRefUtils.getCanonicalName("ENSEMBL"),
142 DBRefUtils.getCanonicalName("Uniprot")});
143 // for every set of db queries
145 // verify presence of expected xrefs
146 // show xrefs - verify expected type of frame is shown for each xref
148 // - verify original -> xref -> xref(original) recovers frame containing at
149 // least the first retrieved sequence
152 // 2. individual frames
153 // 3. load each one back and verify
154 // . aligned sequences (.toString() )
155 // . xrefs (.toString() )
159 Map<String, String> dbtoviewBit = new HashMap<>();
160 List<String> keyseq = new ArrayList<>();
161 Map<String, File> savedProjects = new HashMap<>();
163 // for (String[] did : new String[][] { { "UNIPROT", "P00338" } })
165 // pass counters - 0 - first pass, 1 means retrieve project rather than
167 int pass1 = 0, pass2 = 0, pass3 = 0;
168 // each do loop performs two iterations in the first outer loop pass, but
169 // only performs one iteration on the second outer loop
170 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
171 // { pass 2 = 0 { pass 3 = 0 } }
174 String first = forSource + " " + forAccession;//did[0] + " " + did[1];
175 AlignFrame af = null;
180 List<String> ptypes = null;
185 List<AlignFrame> afs = jalview.gui.SequenceFetcher.fetchAndShow(
186 forSource, forAccession);
190 failedDBRetr.add("Didn't retrieve " + first);
196 // verify references for retrieved data
197 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
198 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
199 + pass3 + "): Fetch " + first + ":");
200 assertDatasetIsNormalisedKnownDefect(af.getViewport()
201 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
202 + pass3 + "): Fetch " + first + ":");
203 dna = af.getViewport().getAlignment().isNucleotide();
204 retral = af.getViewport().getAlignment();
205 dataset = retral.getDataset();
206 seqs = retral.getSequencesArray();
211 Desktop.instance.closeAll_actionPerformed(null);
212 // recover stored project
213 af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
214 .get(first).toString(), DataSourceType.FILE);
215 System.out.println("Recovered view for '" + first + "' from '"
216 + savedProjects.get(first).toString() + "'");
217 dna = af.getViewport().getAlignment().isNucleotide();
218 retral = af.getViewport().getAlignment();
219 dataset = retral.getDataset();
220 seqs = retral.getSequencesArray();
222 // verify references for recovered data
223 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
224 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
225 + pass3 + "): Recover " + first + ":");
226 assertDatasetIsNormalisedKnownDefect(af.getViewport()
227 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
228 + pass3 + "): Recover " + first + ":");
232 // store project on first pass, compare next pass
233 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
235 ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
236 seqs, dataset).findXrefSourcesForSequences(dna);
237 filterDbRefs(ptypes, limit);
239 // start of pass2: retrieve each cross-ref for fetched or restored
241 do // first cross ref and recover crossref loop
244 for (String db : ptypes)
246 // counter for splitframe views retrieved via crossref
248 // build next key so we an retrieve all views
249 String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
250 // perform crossref action, or retrieve stored project
251 List<AlignmentViewPanel> cra_views = new ArrayList<>();
252 CrossRefAction cra = null;
255 { // retrieve and show cross-refs in this thread
256 cra = CrossRefAction.getHandlerFor(seqs, dna, db, af);
258 cra_views = (List<AlignmentViewPanel>) PA.getValue(cra,
260 if (cra_views.size() == 0)
262 failedXrefMenuItems.add("No crossrefs retrieved for "
263 + first + " -> " + db);
266 assertNucleotide(cra_views.get(0),
267 "Nucleotide panel included proteins for " + first
269 assertProtein(cra_views.get(1),
270 "Protein panel included nucleotides for " + first
275 Desktop.instance.closeAll_actionPerformed(null);
277 // recover stored project
278 File storedProject = savedProjects.get(nextxref);
279 if (storedProject == null)
281 failedProjectRecoveries.add("Failed to store a view for '"
286 // recover stored project
287 AlignFrame af2 = new FileLoader(false)
288 .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
289 .toString(), DataSourceType.FILE);
290 System.out.println("Recovered view for '" + nextxref
291 + "' from '" + savedProjects.get(nextxref).toString()
293 // gymnastics to recover the alignPanel/Complementary alignPanel
294 if (af2.getViewport().isNucleotide())
296 // top view, then bottom
297 cra_views.add(af2.getViewport().getAlignPanel());
298 cra_views.add(((jalview.gui.AlignViewport) af2
299 .getViewport().getCodingComplement())
305 // bottom view, then top
306 cra_views.add(((jalview.gui.AlignViewport) af2
307 .getViewport().getCodingComplement())
309 cra_views.add(af2.getViewport().getAlignPanel());
313 HashMap<String, List<String>> xrptypes = new HashMap<>();
314 // first save/verify views.
315 for (AlignmentViewPanel avp : cra_views)
317 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
318 // verify references for this panel
319 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
320 "Pass (" + pass1 + "," + pass2 + "," + pass3
321 + "): before start of pass3: " + nextxref
323 assertDatasetIsNormalisedKnownDefect(avp.getAlignment(),
324 "Pass (" + pass1 + "," + pass2 + "," + pass3
325 + "): before start of pass3: " + nextxref
328 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
330 List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
331 : new CrossRef(xrseqs, dataset)
332 .findXrefSourcesForSequences(avp
333 .getAlignViewport().isNucleotide());
335 stringify(dbtoviewBit, savedProjects, nextxref, avp);
336 xrptypes.put(nextxref, _xrptypes);
340 // now do the second xref pass starting from either saved or just
341 // recovered split pane, in sequence
342 do // retrieve second set of cross refs or recover and verify
345 for (AlignmentViewPanel avp : cra_views)
347 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
348 for (String xrefdb : xrptypes.get(nextxref))
350 List<AlignmentViewPanel> cra_views2 = new ArrayList<>();
352 String nextnextxref = nextxref + " -> " + xrefdb + "{"
357 SequenceI[] xrseqs = avp.getAlignment()
358 .getSequencesArray();
359 AlignFrame nextaf = Desktop.getAlignFrameFor(avp
360 .getAlignViewport());
362 cra = CrossRefAction.getHandlerFor(xrseqs, avp
363 .getAlignViewport().isNucleotide(), xrefdb,
366 cra_views2 = (List<AlignmentViewPanel>) PA.getValue(
368 if (cra_views2.size() == 0)
371 .add("No crossrefs retrieved for '"
372 + nextxref + "' to " + xrefdb
373 + " via '" + nextaf.getTitle() + "'");
376 assertNucleotide(cra_views2.get(0),
377 "Nucleotide panel included proteins for '"
378 + nextxref + "' to " + xrefdb
379 + " via '" + nextaf.getTitle() + "'");
380 assertProtein(cra_views2.get(1),
381 "Protein panel included nucleotides for '"
382 + nextxref + "' to " + xrefdb
383 + " via '" + nextaf.getTitle() + "'");
388 Desktop.instance.closeAll_actionPerformed(null);
389 // recover stored project
390 File storedProject = savedProjects.get(nextnextxref);
391 if (storedProject == null)
393 failedProjectRecoveries
394 .add("Failed to store a view for '"
395 + nextnextxref + "'");
398 AlignFrame af2 = new FileLoader(false)
399 .LoadFileWaitTillLoaded(
400 savedProjects.get(nextnextxref)
402 DataSourceType.FILE);
403 System.out.println("Recovered view for '"
404 + nextnextxref + "' from '"
405 + savedProjects.get(nextnextxref).toString()
407 // gymnastics to recover the alignPanel/Complementary
409 if (af2.getViewport().isNucleotide())
411 // top view, then bottom
412 cra_views2.add(af2.getViewport().getAlignPanel());
413 cra_views2.add(((jalview.gui.AlignViewport) af2
414 .getViewport().getCodingComplement())
420 // bottom view, then top
421 cra_views2.add(((jalview.gui.AlignViewport) af2
422 .getViewport().getCodingComplement())
424 cra_views2.add(af2.getViewport().getAlignPanel());
426 Assert.assertEquals(cra_views2.size(), 2);
427 Assert.assertNotNull(cra_views2.get(0));
428 Assert.assertNotNull(cra_views2.get(1));
431 for (AlignmentViewPanel nextavp : cra_views2)
433 nextnextxref = nextxref + " -> " + xrefdb + "{" + q++
436 // verify references for this panel
437 AlignmentTest.assertAlignmentDatasetRefs(
438 nextavp.getAlignment(), "" + "Pass (" + pass1
439 + "," + pass2 + "): For "
440 + nextnextxref + ":");
441 assertDatasetIsNormalisedKnownDefect(
442 nextavp.getAlignment(), "" + "Pass (" + pass1
443 + "," + pass2 + "): For "
444 + nextnextxref + ":");
446 stringify(dbtoviewBit, savedProjects, nextnextxref,
448 keyseq.add(nextnextxref);
450 } // end of loop around showing all xrefdb for crossrf2
452 } // end of loop around all viewpanels from crossrf1
453 } while (pass2 == 2 && pass3++ < 2);
454 // fetchdb->crossref1->crossref-2->verify for xrefs we
455 // either loop twice when pass2=0, or just once when pass2=1
456 // (recovered project from previous crossref)
458 } // end of loop over db-xrefs for crossref-2
460 // fetchdb-->crossref1
461 // for each xref we try to retrieve xref, store and verify when
462 // pass1=0, or just retrieve and verify when pass1=1
463 } while (pass1 == 1 && pass2++ < 2);
466 // loop twice: first, do the retrieve, second recover from saved project
468 // increment pass counters, so we repeat traversal starting from the
469 // oldest saved project first.
472 // verify stored projects for first set of cross references
474 // and verify cross-references retrieved from stored projects
484 if (failedXrefMenuItems.size() > 0)
486 for (String s : failedXrefMenuItems)
488 System.err.println(s);
490 Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
493 if (failedProjectRecoveries.size() > 0)
496 for (String s : failedProjectRecoveries)
498 System.err.println(s);
500 Assert.fail("Didn't recover projects for some retrievals (did they retrieve ?) ("
501 + failedProjectRecoveries.size() + " counts)");
503 if (failedDBRetr.size() > 0)
505 for (String s : failedProjectRecoveries)
507 System.err.println(s);
509 Assert.fail("Didn't retrieve some db refs for checking cross-refs ("
510 + failedDBRetr.size() + " counts)");
514 private void filterDbRefs(List<String> ptypes, List<String> limit)
519 while (ptypes.size() > p)
521 if (!limit.contains(ptypes.get(p)))
534 * wrapper to trap known defect for AH002001 testcase
539 private void assertDatasetIsNormalisedKnownDefect(AlignmentI al,
544 AlignmentTest.assertDatasetIsNormalised(al, message);
545 } catch (AssertionError ae)
547 if (!ae.getMessage().endsWith("EMBL|AH002001"))
554 .println("Ignored exception for known defect: JAL-2179 : "
561 private void assertProtein(AlignmentViewPanel alignmentViewPanel,
564 assertType(true, alignmentViewPanel, message);
567 private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
570 assertType(false, alignmentViewPanel, message);
573 private void assertType(boolean expectProtein,
574 AlignmentViewPanel alignmentViewPanel, String message)
576 List<SequenceI> nonType = new ArrayList<>();
577 for (SequenceI sq : alignmentViewPanel.getAlignViewport()
578 .getAlignment().getSequences())
580 if (sq.isProtein() != expectProtein)
585 if (nonType.size() > 0)
587 Assert.fail(message + " [ "
588 + (expectProtein ? "nucleotides were " : "proteins were ")
589 + nonType.toString() + " ]");
594 * first time called, record strings derived from alignment and
595 * alignedcodonframes, and save view to a project file. Second time called,
596 * compare strings to existing ones. org.testng.Assert.assertTrue on
600 * map between xrefpath and view string
601 * @param savedProjects
602 * - map from xrefpath to saved project filename (createTempFile)
604 * - xrefpath - unique ID for this context (composed of sequence of
605 * db-fetch/cross-ref actions preceeding state)
607 * - viewpanel to store (for viewpanels in splitframe, the same
608 * project should be written for both panels, only one needs
609 * recovering for comparison on the next stringify call, but each
610 * viewpanel needs to be called with a distinct xrefpath to ensure
611 * each one's strings are compared)
613 private void stringify(Map<String, String> dbtoviewBit,
614 Map<String, File> savedProjects, String xrefpath,
615 AlignmentViewPanel avp)
617 if (savedProjects != null)
619 if (savedProjects.get(xrefpath) == null)
621 // write a project file for this view. On the second pass, this will be
622 // recovered and cross-references verified
625 File prfile = File.createTempFile("crossRefTest", ".jvp");
626 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
627 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
629 System.out.println("Written view from '" + xrefpath + "' as '"
630 + prfile.getAbsolutePath() + "'");
631 savedProjects.put(xrefpath, prfile);
632 } catch (IOException q)
634 Assert.fail("Unexpected IO Exception", q);
639 System.out.println("Stringify check on view from '" + xrefpath
640 + "' [ possibly retrieved from '"
641 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
646 StringBuilder sbr = new StringBuilder();
647 sbr.append(avp.getAlignment().toString());
649 sbr.append("<End of alignment>");
651 sbr.append(avp.getAlignment().getDataset());
653 sbr.append("<End of dataset>");
656 if (avp.getAlignment().getCodonFrames() != null)
658 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
660 sbr.append("<AlignedCodonFrame " + p++ + ">");
662 sbr.append(ac.toString());
666 String dbt = dbtoviewBit.get(xrefpath);
669 dbtoviewBit.put(xrefpath, sbr.toString());
673 Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "