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 jalview.analysis.CrossRef;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.AlignedCodonFrame;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.AlignmentTest;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.CrossRefAction;
31 import jalview.gui.Desktop;
32 import jalview.gui.JvOptionPane;
33 import jalview.gui.SequenceFetcher;
34 import jalview.project.Jalview2XML;
35 import jalview.util.DBRefUtils;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.HashMap;
42 import java.util.List;
45 import org.testng.Assert;
46 import org.testng.annotations.BeforeClass;
47 import org.testng.annotations.DataProvider;
48 import org.testng.annotations.Test;
50 import junit.extensions.PA;
52 @Test(singleThreaded = true)
53 public class CrossRef2xmlTests extends Jalview2xmlBase
57 @BeforeClass(alwaysRun = true)
58 public void setUpJvOptionPane()
60 JvOptionPane.setInteractiveMode(false);
61 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
64 @DataProvider(name = "initialAccessions")
65 static Object[][] getAccessions()
67 return new String[][] { { "UNIPROT", "P00338" },
68 { "UNIPROT", "Q8Z9G6" },
69 { "ENSEMBLGENOMES", "CAD01290" } };
73 * test store and recovery of all reachable cross refs from all reachable
74 * crossrefs for one or more fetched db refs. Currently, this test has a known
82 dataProvider = "initialAccessions",
84 public void testRetrieveAndShowCrossref(String forSource,
85 String forAccession) throws Exception
88 List<String> failedDBRetr = new ArrayList<>();
89 List<String> failedXrefMenuItems = new ArrayList<>();
90 List<String> failedProjectRecoveries = new ArrayList<>();
91 // only search for ensembl or Uniprot crossrefs
92 List<String> limit = Arrays
94 { DBRefUtils.getCanonicalName("ENSEMBL"),
95 DBRefUtils.getCanonicalName("Uniprot") });
96 // for every set of db queries
98 // verify presence of expected xrefs
99 // show xrefs - verify expected type of frame is shown for each xref
101 // - verify original -> xref -> xref(original) recovers frame containing at
102 // least the first retrieved sequence
105 // 2. individual frames
106 // 3. load each one back and verify
107 // . aligned sequences (.toString() )
108 // . xrefs (.toString() )
112 Map<String, String> dbtoviewBit = new HashMap<>();
113 List<String> keyseq = new ArrayList<>();
114 Map<String, File> savedProjects = new HashMap<>();
116 // for (String[] did : new String[][] { { "UNIPROT", "P00338" } })
118 // pass counters - 0 - first pass, 1 means retrieve project rather than
120 int pass1 = 0, pass2 = 0, pass3 = 0;
121 // each do loop performs two iterations in the first outer loop pass, but
122 // only performs one iteration on the second outer loop
123 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
124 // { pass 2 = 0 { pass 3 = 0 } }
127 String first = forSource + " " + forAccession;// did[0] + " " + did[1];
128 AlignFrame af = null;
133 List<String> ptypes = null;
138 SequenceFetcher sf = new SequenceFetcher(Desktop.getInstance(),
139 forSource, forAccession);
141 AlignFrame[] afs = Desktop.getAlignFrames();
144 failedDBRetr.add("Didn't retrieve " + first);
150 // verify references for retrieved data
151 AlignmentTest.assertAlignmentDatasetRefs(
152 af.getViewport().getAlignment(), "Pass (" + pass1 + ","
153 + pass2 + "," + pass3 + "): Fetch " + first + ":");
154 assertDatasetIsNormalisedKnownDefect(
155 af.getViewport().getAlignment(), "Pass (" + pass1 + ","
156 + pass2 + "," + pass3 + "): Fetch " + first + ":");
157 dna = af.getViewport().getAlignment().isNucleotide();
158 retral = af.getViewport().getAlignment();
159 dataset = retral.getDataset();
160 seqs = retral.getSequencesArray();
165 Desktop.getInstance().closeAll_actionPerformed(null);
166 // recover stored project
167 af = new FileLoader(false).LoadFileWaitTillLoaded(
168 savedProjects.get(first).toString(), DataSourceType.FILE);
169 System.out.println("Recovered view for '" + first + "' from '"
170 + savedProjects.get(first).toString() + "'");
171 dna = af.getViewport().getAlignment().isNucleotide();
172 retral = af.getViewport().getAlignment();
173 dataset = retral.getDataset();
174 seqs = retral.getSequencesArray();
176 // verify references for recovered data
177 AlignmentTest.assertAlignmentDatasetRefs(
178 af.getViewport().getAlignment(),
179 "Pass (" + pass1 + "," + pass2 + "," + pass3 + "): Recover "
181 assertDatasetIsNormalisedKnownDefect(
182 af.getViewport().getAlignment(),
183 "Pass (" + pass1 + "," + pass2 + "," + pass3 + "): Recover "
188 // store project on first pass, compare next pass
189 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
191 ptypes = (seqs == null || seqs.length == 0) ? null
192 : new CrossRef(seqs, dataset)
193 .findXrefSourcesForSequences(dna);
194 filterDbRefs(ptypes, limit);
196 // start of pass2: retrieve each cross-ref for fetched or restored
198 do // first cross ref and recover crossref loop
201 for (String db : ptypes)
203 // counter for splitframe views retrieved via crossref
205 // build next key so we an retrieve all views
206 String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
207 // perform crossref action, or retrieve stored project
208 List<AlignmentViewPanel> cra_views = new ArrayList<>();
209 CrossRefAction cra = null;
212 { // retrieve and show cross-refs in this thread
213 cra = CrossRefAction.getHandlerFor(seqs, dna, db, af);
215 cra_views = (List<AlignmentViewPanel>) PA.getValue(cra,
217 if (cra_views.size() == 0)
219 failedXrefMenuItems.add(
220 "No crossrefs retrieved for " + first + " -> " + db);
223 assertNucleotide(cra_views.get(0),
224 "Nucleotide panel included proteins for " + first
226 assertProtein(cra_views.get(1),
227 "Protein panel included nucleotides for " + first
232 Desktop.getInstance().closeAll_actionPerformed(null);
234 // recover stored project
235 File storedProject = savedProjects.get(nextxref);
236 if (storedProject == null)
238 failedProjectRecoveries
239 .add("Failed to store a view for '" + nextxref + "'");
243 // recover stored project
244 AlignFrame af2 = new FileLoader(false).LoadFileWaitTillLoaded(
245 savedProjects.get(nextxref).toString(),
246 DataSourceType.FILE);
248 .println("Recovered view for '" + nextxref + "' from '"
249 + savedProjects.get(nextxref).toString() + "'");
250 // gymnastics to recover the alignPanel/Complementary alignPanel
251 if (af2.getViewport().isNucleotide())
253 // top view, then bottom
254 cra_views.add(af2.getViewport().getAlignPanel());
255 cra_views.add(((jalview.gui.AlignViewport) af2.getViewport()
256 .getCodingComplement()).getAlignPanel());
261 // bottom view, then top
262 cra_views.add(((jalview.gui.AlignViewport) af2.getViewport()
263 .getCodingComplement()).getAlignPanel());
264 cra_views.add(af2.getViewport().getAlignPanel());
268 HashMap<String, List<String>> xrptypes = new HashMap<>();
269 // first save/verify views.
270 for (AlignmentViewPanel avp : cra_views)
272 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
273 // verify references for this panel
274 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
275 "Pass (" + pass1 + "," + pass2 + "," + pass3
276 + "): before start of pass3: " + nextxref
278 assertDatasetIsNormalisedKnownDefect(avp.getAlignment(),
279 "Pass (" + pass1 + "," + pass2 + "," + pass3
280 + "): before start of pass3: " + nextxref
283 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
285 List<String> _xrptypes = (seqs == null || seqs.length == 0)
287 : new CrossRef(xrseqs, dataset)
288 .findXrefSourcesForSequences(
289 avp.getAlignViewport().isNucleotide());
291 stringify(dbtoviewBit, savedProjects, nextxref, avp);
292 xrptypes.put(nextxref, _xrptypes);
296 // now do the second xref pass starting from either saved or just
297 // recovered split pane, in sequence
298 do // retrieve second set of cross refs or recover and verify
301 for (AlignmentViewPanel avp : cra_views)
303 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
304 for (String xrefdb : xrptypes.get(nextxref))
306 List<AlignmentViewPanel> cra_views2 = new ArrayList<>();
308 String nextnextxref = nextxref + " -> " + xrefdb + "{" + q
313 SequenceI[] xrseqs = avp.getAlignment()
314 .getSequencesArray();
315 AlignFrame nextaf = Desktop
316 .getAlignFrameFor(avp.getAlignViewport());
318 cra = CrossRefAction.getHandlerFor(xrseqs,
319 avp.getAlignViewport().isNucleotide(), xrefdb,
322 cra_views2 = (List<AlignmentViewPanel>) PA.getValue(cra,
324 if (cra_views2.size() == 0)
326 failedXrefMenuItems.add("No crossrefs retrieved for '"
327 + nextxref + "' to " + xrefdb + " via '"
328 + nextaf.getTitle() + "'");
331 assertNucleotide(cra_views2.get(0),
332 "Nucleotide panel included proteins for '"
333 + nextxref + "' to " + xrefdb + " via '"
334 + nextaf.getTitle() + "'");
335 assertProtein(cra_views2.get(1),
336 "Protein panel included nucleotides for '"
337 + nextxref + "' to " + xrefdb + " via '"
338 + nextaf.getTitle() + "'");
343 Desktop.getInstance().closeAll_actionPerformed(null);
344 // recover stored project
345 File storedProject = savedProjects.get(nextnextxref);
346 if (storedProject == null)
348 failedProjectRecoveries
349 .add("Failed to store a view for '"
350 + nextnextxref + "'");
353 AlignFrame af2 = new FileLoader(false)
354 .LoadFileWaitTillLoaded(savedProjects
355 .get(nextnextxref).toString(),
356 DataSourceType.FILE);
358 .println("Recovered view for '" + nextnextxref
359 + "' from '" + savedProjects
360 .get(nextnextxref).toString()
362 // gymnastics to recover the alignPanel/Complementary
364 if (af2.getViewport().isNucleotide())
366 // top view, then bottom
367 cra_views2.add(af2.getViewport().getAlignPanel());
368 cra_views2.add(((jalview.gui.AlignViewport) af2
369 .getViewport().getCodingComplement())
375 // bottom view, then top
376 cra_views2.add(((jalview.gui.AlignViewport) af2
377 .getViewport().getCodingComplement())
379 cra_views2.add(af2.getViewport().getAlignPanel());
381 Assert.assertEquals(cra_views2.size(), 2);
382 Assert.assertNotNull(cra_views2.get(0));
383 Assert.assertNotNull(cra_views2.get(1));
386 for (AlignmentViewPanel nextavp : cra_views2)
388 nextnextxref = nextxref + " -> " + xrefdb + "{" + q++
391 // verify references for this panel
392 AlignmentTest.assertAlignmentDatasetRefs(
393 nextavp.getAlignment(),
394 "" + "Pass (" + pass1 + "," + pass2 + "): For "
395 + nextnextxref + ":");
396 assertDatasetIsNormalisedKnownDefect(
397 nextavp.getAlignment(),
398 "" + "Pass (" + pass1 + "," + pass2 + "): For "
399 + nextnextxref + ":");
401 stringify(dbtoviewBit, savedProjects, nextnextxref,
403 keyseq.add(nextnextxref);
405 } // end of loop around showing all xrefdb for crossrf2
407 } // end of loop around all viewpanels from crossrf1
408 } while (pass2 == 2 && pass3++ < 2);
409 // fetchdb->crossref1->crossref-2->verify for xrefs we
410 // either loop twice when pass2=0, or just once when pass2=1
411 // (recovered project from previous crossref)
413 } // end of loop over db-xrefs for crossref-2
415 // fetchdb-->crossref1
416 // for each xref we try to retrieve xref, store and verify when
417 // pass1=0, or just retrieve and verify when pass1=1
418 } while (pass1 == 1 && pass2++ < 2);
421 // loop twice: first, do the retrieve, second recover from saved project
423 // increment pass counters, so we repeat traversal starting from the
424 // oldest saved project first.
427 // verify stored projects for first set of cross references
429 // and verify cross-references retrieved from stored projects
439 if (failedXrefMenuItems.size() > 0)
441 for (String s : failedXrefMenuItems)
443 System.err.println(s);
445 Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
448 if (failedProjectRecoveries.size() > 0)
451 for (String s : failedProjectRecoveries)
453 System.err.println(s);
456 "Didn't recover projects for some retrievals (did they retrieve ?) ("
457 + failedProjectRecoveries.size() + " counts)");
459 if (failedDBRetr.size() > 0)
461 for (String s : failedProjectRecoveries)
463 System.err.println(s);
465 Assert.fail("Didn't retrieve some db refs for checking cross-refs ("
466 + failedDBRetr.size() + " counts)");
470 private void filterDbRefs(List<String> ptypes, List<String> limit)
475 while (ptypes.size() > p)
477 if (!limit.contains(ptypes.get(p)))
490 * wrapper to trap known defect for AH002001 testcase
495 private void assertDatasetIsNormalisedKnownDefect(AlignmentI al,
500 AlignmentTest.assertDatasetIsNormalised(al, message);
501 } catch (AssertionError ae)
503 if (!ae.getMessage().endsWith("EMBL|AH002001"))
509 System.out.println("Ignored exception for known defect: JAL-2179 : "
516 private void assertProtein(AlignmentViewPanel alignmentViewPanel,
519 assertType(true, alignmentViewPanel, message);
522 private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
525 assertType(false, alignmentViewPanel, message);
528 private void assertType(boolean expectProtein,
529 AlignmentViewPanel alignmentViewPanel, String message)
531 List<SequenceI> nonType = new ArrayList<>();
532 for (SequenceI sq : alignmentViewPanel.getAlignViewport().getAlignment()
535 if (sq.isProtein() != expectProtein)
540 if (nonType.size() > 0)
542 Assert.fail(message + " [ "
543 + (expectProtein ? "nucleotides were " : "proteins were ")
544 + nonType.toString() + " ]");
549 * first time called, record strings derived from alignment and
550 * alignedcodonframes, and save view to a project file. Second time called,
551 * compare strings to existing ones. org.testng.Assert.assertTrue on
555 * map between xrefpath and view string
556 * @param savedProjects
557 * - map from xrefpath to saved project filename (createTempFile)
559 * - xrefpath - unique ID for this context (composed of sequence of
560 * db-fetch/cross-ref actions preceeding state)
562 * - viewpanel to store (for viewpanels in splitframe, the same
563 * project should be written for both panels, only one needs
564 * recovering for comparison on the next stringify call, but each
565 * viewpanel needs to be called with a distinct xrefpath to ensure
566 * each one's strings are compared)
568 private void stringify(Map<String, String> dbtoviewBit,
569 Map<String, File> savedProjects, String xrefpath,
570 AlignmentViewPanel avp)
572 if (savedProjects != null)
574 if (savedProjects.get(xrefpath) == null)
576 // write a project file for this view. On the second pass, this will be
577 // recovered and cross-references verified
580 File prfile = File.createTempFile("crossRefTest", ".jvp");
581 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
582 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
584 System.out.println("Written view from '" + xrefpath + "' as '"
585 + prfile.getAbsolutePath() + "'");
586 savedProjects.put(xrefpath, prfile);
587 } catch (IOException q)
589 Assert.fail("Unexpected IO Exception", q);
594 System.out.println("Stringify check on view from '" + xrefpath
595 + "' [ possibly retrieved from '"
596 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
601 StringBuilder sbr = new StringBuilder();
602 sbr.append(avp.getAlignment().toString());
604 sbr.append("<End of alignment>");
606 sbr.append(avp.getAlignment().getDataset());
608 sbr.append("<End of dataset>");
611 if (avp.getAlignment().getCodonFrames() != null)
613 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
615 sbr.append("<AlignedCodonFrame " + p++ + ">");
617 sbr.append(ac.toString());
621 String dbt = dbtoviewBit.get(xrefpath);
624 dbtoviewBit.put(xrefpath, sbr.toString());
628 Assert.assertEquals(sbr.toString(), dbt,
629 "stringify mismatch for " + xrefpath);