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.Jalview2XML;
33 import jalview.gui.JvOptionPane;
34 import jalview.util.DBRefUtils;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashMap;
41 import java.util.List;
43 import org.testng.Assert;
44 import org.testng.annotations.BeforeClass;
45 import org.testng.annotations.Test;
47 @Test(singleThreaded = true)
48 public class CrossRef2xmlTests extends Jalview2xmlBase
52 @BeforeClass(alwaysRun = true)
53 public void setUpJvOptionPane()
55 JvOptionPane.setInteractiveMode(false);
56 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
60 * test store and recovery of all reachable cross refs from all reachable
61 * crossrefs for one or more fetched db refs. Currently, this test has a known
66 @Test(groups = { "Operational" }, enabled = true)
67 public void testRetrieveAndShowCrossref() throws Exception
70 List<String> failedDBRetr = new ArrayList<>();
71 List<String> failedXrefMenuItems = new ArrayList<>();
72 List<String> failedProjectRecoveries = new ArrayList<>();
73 // only search for ensembl or Uniprot crossrefs
74 List<String> limit=Arrays.asList(new String[] {
75 DBRefUtils.getCanonicalName("ENSEMBL"),
76 DBRefUtils.getCanonicalName("Uniprot")});
77 // for every set of db queries
79 // verify presence of expected xrefs
80 // show xrefs - verify expected type of frame is shown for each xref
82 // - verify original -> xref -> xref(original) recovers frame containing at
83 // least the first retrieved sequence
86 // 2. individual frames
87 // 3. load each one back and verify
88 // . aligned sequences (.toString() )
89 // . xrefs (.toString() )
93 HashMap<String, String> dbtoviewBit = new HashMap<>();
94 List<String> keyseq = new ArrayList<>();
95 HashMap<String, File> savedProjects = new HashMap<>();
97 for (String[] did : new String[][] { { "UNIPROT", "P00338" } })
99 // pass counters - 0 - first pass, 1 means retrieve project rather than
101 int pass1 = 0, pass2 = 0, pass3 = 0;
102 // each do loop performs two iterations in the first outer loop pass, but
103 // only performs one iteration on the second outer loop
104 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
105 // { pass 2 = 0 { pass 3 = 0 } }
108 String first = did[0] + " " + did[1];
109 AlignFrame af = null;
114 List<String> ptypes = null;
119 List<AlignFrame> afs = jalview.gui.SequenceFetcher.fetchAndShow(
123 failedDBRetr.add("Didn't retrieve " + first);
129 // verify references for retrieved data
130 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
131 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
132 + pass3 + "): Fetch " + first + ":");
133 assertDatasetIsNormalisedKnownDefect(af.getViewport()
134 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
135 + pass3 + "): Fetch " + first + ":");
136 dna = af.getViewport().getAlignment().isNucleotide();
137 retral = af.getViewport().getAlignment();
138 dataset = retral.getDataset();
139 seqs = retral.getSequencesArray();
144 Desktop.instance.closeAll_actionPerformed(null);
145 // recover stored project
146 af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
147 .get(first).toString(), DataSourceType.FILE);
148 System.out.println("Recovered view for '" + first + "' from '"
149 + savedProjects.get(first).toString() + "'");
150 dna = af.getViewport().getAlignment().isNucleotide();
151 retral = af.getViewport().getAlignment();
152 dataset = retral.getDataset();
153 seqs = retral.getSequencesArray();
155 // verify references for recovered data
156 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
157 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
158 + pass3 + "): Recover " + first + ":");
159 assertDatasetIsNormalisedKnownDefect(af.getViewport()
160 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
161 + pass3 + "): Recover " + first + ":");
165 // store project on first pass, compare next pass
166 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
168 ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
169 seqs, dataset).findXrefSourcesForSequences(dna);
170 filterDbRefs(ptypes, limit);
172 // start of pass2: retrieve each cross-ref for fetched or restored
174 do // first cross ref and recover crossref loop
177 for (String db : ptypes)
179 // counter for splitframe views retrieved via crossref
181 // build next key so we an retrieve all views
182 String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
183 // perform crossref action, or retrieve stored project
184 List<AlignmentViewPanel> cra_views = new ArrayList<>();
185 CrossRefAction cra = null;
188 { // retrieve and show cross-refs in this thread
189 cra = new CrossRefAction(af, seqs, dna, db);
191 if (cra.getXrefViews().size() == 0)
193 failedXrefMenuItems.add("No crossrefs retrieved for "
194 + first + " -> " + db);
197 cra_views = cra.getXrefViews();
198 assertNucleotide(cra_views.get(0),
199 "Nucleotide panel included proteins for " + first
201 assertProtein(cra_views.get(1),
202 "Protein panel included nucleotides for " + first
207 Desktop.instance.closeAll_actionPerformed(null);
209 // recover stored project
210 File storedProject = savedProjects.get(nextxref);
211 if (storedProject == null)
213 failedProjectRecoveries.add("Failed to store a view for '"
218 // recover stored project
219 AlignFrame af2 = new FileLoader(false)
220 .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
221 .toString(), DataSourceType.FILE);
222 System.out.println("Recovered view for '" + nextxref
223 + "' from '" + savedProjects.get(nextxref).toString()
225 // gymnastics to recover the alignPanel/Complementary alignPanel
226 if (af2.getViewport().isNucleotide())
228 // top view, then bottom
229 cra_views.add(af2.getViewport().getAlignPanel());
230 cra_views.add(((jalview.gui.AlignViewport) af2
231 .getViewport().getCodingComplement())
237 // bottom view, then top
238 cra_views.add(((jalview.gui.AlignViewport) af2
239 .getViewport().getCodingComplement())
241 cra_views.add(af2.getViewport().getAlignPanel());
245 HashMap<String, List<String>> xrptypes = new HashMap<>();
246 // first save/verify views.
247 for (AlignmentViewPanel avp : cra_views)
249 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
250 // verify references for this panel
251 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
252 "Pass (" + pass1 + "," + pass2 + "," + pass3
253 + "): before start of pass3: " + nextxref
255 assertDatasetIsNormalisedKnownDefect(avp.getAlignment(),
256 "Pass (" + pass1 + "," + pass2 + "," + pass3
257 + "): before start of pass3: " + nextxref
260 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
262 List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
263 : new CrossRef(xrseqs, dataset)
264 .findXrefSourcesForSequences(avp
265 .getAlignViewport().isNucleotide());
267 stringify(dbtoviewBit, savedProjects, nextxref, avp);
268 xrptypes.put(nextxref, _xrptypes);
272 // now do the second xref pass starting from either saved or just
273 // recovered split pane, in sequence
274 do // retrieve second set of cross refs or recover and verify
277 for (AlignmentViewPanel avp : cra_views)
279 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
280 for (String xrefdb : xrptypes.get(nextxref))
282 List<AlignmentViewPanel> cra_views2 = new ArrayList<>();
284 String nextnextxref = nextxref + " -> " + xrefdb + "{"
290 SequenceI[] xrseqs = avp.getAlignment()
291 .getSequencesArray();
292 AlignFrame nextaf = Desktop.getAlignFrameFor(avp
293 .getAlignViewport());
295 cra = new CrossRefAction(nextaf, xrseqs, avp
296 .getAlignViewport().isNucleotide(), xrefdb);
298 if (cra.getXrefViews().size() == 0)
301 .add("No crossrefs retrieved for '"
302 + nextxref + "' to " + xrefdb
303 + " via '" + nextaf.getTitle() + "'");
306 cra_views2 = cra.getXrefViews();
307 assertNucleotide(cra_views2.get(0),
308 "Nucleotide panel included proteins for '"
309 + nextxref + "' to " + xrefdb
310 + " via '" + nextaf.getTitle() + "'");
311 assertProtein(cra_views2.get(1),
312 "Protein panel included nucleotides for '"
313 + nextxref + "' to " + xrefdb
314 + " via '" + nextaf.getTitle() + "'");
319 Desktop.instance.closeAll_actionPerformed(null);
320 // recover stored project
321 File storedProject = savedProjects.get(nextnextxref);
322 if (storedProject == null)
324 failedProjectRecoveries
325 .add("Failed to store a view for '"
326 + nextnextxref + "'");
329 AlignFrame af2 = new FileLoader(false)
330 .LoadFileWaitTillLoaded(
331 savedProjects.get(nextnextxref)
333 DataSourceType.FILE);
334 System.out.println("Recovered view for '"
335 + nextnextxref + "' from '"
336 + savedProjects.get(nextnextxref).toString()
338 // gymnastics to recover the alignPanel/Complementary
340 if (af2.getViewport().isNucleotide())
342 // top view, then bottom
343 cra_views2.add(af2.getViewport().getAlignPanel());
344 cra_views2.add(((jalview.gui.AlignViewport) af2
345 .getViewport().getCodingComplement())
351 // bottom view, then top
352 cra_views2.add(((jalview.gui.AlignViewport) af2
353 .getViewport().getCodingComplement())
355 cra_views2.add(af2.getViewport().getAlignPanel());
357 Assert.assertEquals(cra_views2.size(), 2);
358 Assert.assertNotNull(cra_views2.get(0));
359 Assert.assertNotNull(cra_views2.get(1));
362 for (AlignmentViewPanel nextavp : cra_views2)
364 nextnextxref = nextxref + " -> " + xrefdb + "{" + q++
367 // verify references for this panel
368 AlignmentTest.assertAlignmentDatasetRefs(
369 nextavp.getAlignment(), "" + "Pass (" + pass1
370 + "," + pass2 + "): For "
371 + nextnextxref + ":");
372 assertDatasetIsNormalisedKnownDefect(
373 nextavp.getAlignment(), "" + "Pass (" + pass1
374 + "," + pass2 + "): For "
375 + nextnextxref + ":");
377 stringify(dbtoviewBit, savedProjects, nextnextxref,
379 keyseq.add(nextnextxref);
381 } // end of loop around showing all xrefdb for crossrf2
383 } // end of loop around all viewpanels from crossrf1
384 } while (pass2 == 2 && pass3++ < 2);
385 // fetchdb->crossref1->crossref-2->verify for xrefs we
386 // either loop twice when pass2=0, or just once when pass2=1
387 // (recovered project from previous crossref)
389 } // end of loop over db-xrefs for crossref-2
391 // fetchdb-->crossref1
392 // for each xref we try to retrieve xref, store and verify when
393 // pass1=0, or just retrieve and verify when pass1=1
394 } while (pass1 == 1 && pass2++ < 2);
397 // loop twice: first, do the retrieve, second recover from saved project
399 // increment pass counters, so we repeat traversal starting from the
400 // oldest saved project first.
403 // verify stored projects for first set of cross references
405 // and verify cross-references retrieved from stored projects
415 if (failedXrefMenuItems.size() > 0)
417 for (String s : failedXrefMenuItems)
419 System.err.println(s);
421 Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
424 if (failedProjectRecoveries.size() > 0)
427 for (String s : failedProjectRecoveries)
429 System.err.println(s);
431 Assert.fail("Didn't recover projects for some retrievals (did they retrieve ?) ("
432 + failedProjectRecoveries.size() + " counts)");
434 if (failedDBRetr.size() > 0)
436 for (String s : failedProjectRecoveries)
438 System.err.println(s);
440 Assert.fail("Didn't retrieve some db refs for checking cross-refs ("
441 + failedDBRetr.size() + " counts)");
445 private void filterDbRefs(List<String> ptypes, List<String> limit)
450 while (ptypes.size() > p)
452 if (!limit.contains(ptypes.get(p)))
465 * wrapper to trap known defect for AH002001 testcase
470 private void assertDatasetIsNormalisedKnownDefect(AlignmentI al,
475 AlignmentTest.assertDatasetIsNormalised(al, message);
476 } catch (AssertionError ae)
478 if (!ae.getMessage().endsWith("EMBL|AH002001"))
485 .println("Ignored exception for known defect: JAL-2179 : "
492 private void assertProtein(AlignmentViewPanel alignmentViewPanel,
495 assertType(true, alignmentViewPanel, message);
498 private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
501 assertType(false, alignmentViewPanel, message);
504 private void assertType(boolean expectProtein,
505 AlignmentViewPanel alignmentViewPanel, String message)
507 List<SequenceI> nonType = new ArrayList<>();
508 for (SequenceI sq : alignmentViewPanel.getAlignViewport()
509 .getAlignment().getSequences())
511 if (sq.isProtein() != expectProtein)
516 if (nonType.size() > 0)
518 Assert.fail(message + " [ "
519 + (expectProtein ? "nucleotides were " : "proteins were ")
520 + nonType.toString() + " ]");
525 * first time called, record strings derived from alignment and
526 * alignedcodonframes, and save view to a project file. Second time called,
527 * compare strings to existing ones. org.testng.Assert.assertTrue on
531 * map between xrefpath and view string
532 * @param savedProjects
533 * - map from xrefpath to saved project filename (createTempFile)
535 * - xrefpath - unique ID for this context (composed of sequence of
536 * db-fetch/cross-ref actions preceeding state)
538 * - viewpanel to store (for viewpanels in splitframe, the same
539 * project should be written for both panels, only one needs
540 * recovering for comparison on the next stringify call, but each
541 * viewpanel needs to be called with a distinct xrefpath to ensure
542 * each one's strings are compared)
544 private void stringify(HashMap<String, String> dbtoviewBit,
545 HashMap<String, File> savedProjects, String xrefpath,
546 AlignmentViewPanel avp)
548 if (savedProjects != null)
550 if (savedProjects.get(xrefpath) == null)
552 // write a project file for this view. On the second pass, this will be
553 // recovered and cross-references verified
556 File prfile = File.createTempFile("crossRefTest", ".jvp");
557 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
558 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
560 System.out.println("Written view from '" + xrefpath + "' as '"
561 + prfile.getAbsolutePath() + "'");
562 savedProjects.put(xrefpath, prfile);
563 } catch (IOException q)
565 Assert.fail("Unexpected IO Exception", q);
570 System.out.println("Stringify check on view from '" + xrefpath
571 + "' [ possibly retrieved from '"
572 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
577 StringBuilder sbr = new StringBuilder();
578 sbr.append(avp.getAlignment().toString());
580 sbr.append("<End of alignment>");
582 sbr.append(avp.getAlignment().getDataset());
584 sbr.append("<End of dataset>");
587 if (avp.getAlignment().getCodonFrames() != null)
589 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
591 sbr.append("<AlignedCodonFrame " + p++ + ">");
593 sbr.append(ac.toString());
597 String dbt = dbtoviewBit.get(xrefpath);
600 dbtoviewBit.put(xrefpath, sbr.toString());
604 Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "