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;
44 import junit.extensions.PA;
46 import org.testng.Assert;
47 import org.testng.annotations.BeforeClass;
48 import org.testng.annotations.Test;
50 @Test(singleThreaded = true)
51 public class CrossRef2xmlTests extends Jalview2xmlBase
55 @BeforeClass(alwaysRun = true)
56 public void setUpJvOptionPane()
58 JvOptionPane.setInteractiveMode(false);
59 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
63 * test store and recovery of all reachable cross refs from all reachable
64 * crossrefs for one or more fetched db refs. Currently, this test has a known
69 @Test(groups = { "Operational" }, enabled = true)
70 public void testRetrieveAndShowCrossref() throws Exception
73 List<String> failedDBRetr = new ArrayList<>();
74 List<String> failedXrefMenuItems = new ArrayList<>();
75 List<String> failedProjectRecoveries = new ArrayList<>();
76 // only search for ensembl or Uniprot crossrefs
77 List<String> limit=Arrays.asList(new String[] {
78 DBRefUtils.getCanonicalName("ENSEMBL"),
79 DBRefUtils.getCanonicalName("Uniprot")});
80 // for every set of db queries
82 // verify presence of expected xrefs
83 // show xrefs - verify expected type of frame is shown for each xref
85 // - verify original -> xref -> xref(original) recovers frame containing at
86 // least the first retrieved sequence
89 // 2. individual frames
90 // 3. load each one back and verify
91 // . aligned sequences (.toString() )
92 // . xrefs (.toString() )
96 Map<String, String> dbtoviewBit = new HashMap<>();
97 List<String> keyseq = new ArrayList<>();
98 Map<String, File> savedProjects = new HashMap<>();
100 for (String[] did : new String[][] { { "UNIPROT", "P00338" } })
102 // pass counters - 0 - first pass, 1 means retrieve project rather than
104 int pass1 = 0, pass2 = 0, pass3 = 0;
105 // each do loop performs two iterations in the first outer loop pass, but
106 // only performs one iteration on the second outer loop
107 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
108 // { pass 2 = 0 { pass 3 = 0 } }
111 String first = did[0] + " " + did[1];
112 AlignFrame af = null;
117 List<String> ptypes = null;
122 List<AlignFrame> afs = jalview.gui.SequenceFetcher.fetchAndShow(
126 failedDBRetr.add("Didn't retrieve " + first);
132 // verify references for retrieved data
133 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
134 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
135 + pass3 + "): Fetch " + first + ":");
136 assertDatasetIsNormalisedKnownDefect(af.getViewport()
137 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
138 + pass3 + "): Fetch " + first + ":");
139 dna = af.getViewport().getAlignment().isNucleotide();
140 retral = af.getViewport().getAlignment();
141 dataset = retral.getDataset();
142 seqs = retral.getSequencesArray();
147 Desktop.instance.closeAll_actionPerformed(null);
148 // recover stored project
149 af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
150 .get(first).toString(), DataSourceType.FILE);
151 System.out.println("Recovered view for '" + first + "' from '"
152 + savedProjects.get(first).toString() + "'");
153 dna = af.getViewport().getAlignment().isNucleotide();
154 retral = af.getViewport().getAlignment();
155 dataset = retral.getDataset();
156 seqs = retral.getSequencesArray();
158 // verify references for recovered data
159 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
160 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
161 + pass3 + "): Recover " + first + ":");
162 assertDatasetIsNormalisedKnownDefect(af.getViewport()
163 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
164 + pass3 + "): Recover " + first + ":");
168 // store project on first pass, compare next pass
169 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
171 ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
172 seqs, dataset).findXrefSourcesForSequences(dna);
173 filterDbRefs(ptypes, limit);
175 // start of pass2: retrieve each cross-ref for fetched or restored
177 do // first cross ref and recover crossref loop
180 for (String db : ptypes)
182 // counter for splitframe views retrieved via crossref
184 // build next key so we an retrieve all views
185 String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
186 // perform crossref action, or retrieve stored project
187 List<AlignmentViewPanel> cra_views = new ArrayList<>();
188 CrossRefAction cra = null;
191 { // retrieve and show cross-refs in this thread
192 cra = CrossRefAction.getHandlerFor(seqs, dna, db, af);
194 cra_views = (List<AlignmentViewPanel>) PA.getValue(cra,
196 if (cra_views.size() == 0)
198 failedXrefMenuItems.add("No crossrefs retrieved for "
199 + first + " -> " + db);
202 assertNucleotide(cra_views.get(0),
203 "Nucleotide panel included proteins for " + first
205 assertProtein(cra_views.get(1),
206 "Protein panel included nucleotides for " + first
211 Desktop.instance.closeAll_actionPerformed(null);
213 // recover stored project
214 File storedProject = savedProjects.get(nextxref);
215 if (storedProject == null)
217 failedProjectRecoveries.add("Failed to store a view for '"
222 // recover stored project
223 AlignFrame af2 = new FileLoader(false)
224 .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
225 .toString(), DataSourceType.FILE);
226 System.out.println("Recovered view for '" + nextxref
227 + "' from '" + savedProjects.get(nextxref).toString()
229 // gymnastics to recover the alignPanel/Complementary alignPanel
230 if (af2.getViewport().isNucleotide())
232 // top view, then bottom
233 cra_views.add(af2.getViewport().getAlignPanel());
234 cra_views.add(((jalview.gui.AlignViewport) af2
235 .getViewport().getCodingComplement())
241 // bottom view, then top
242 cra_views.add(((jalview.gui.AlignViewport) af2
243 .getViewport().getCodingComplement())
245 cra_views.add(af2.getViewport().getAlignPanel());
249 HashMap<String, List<String>> xrptypes = new HashMap<>();
250 // first save/verify views.
251 for (AlignmentViewPanel avp : cra_views)
253 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
254 // verify references for this panel
255 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
256 "Pass (" + pass1 + "," + pass2 + "," + pass3
257 + "): before start of pass3: " + nextxref
259 assertDatasetIsNormalisedKnownDefect(avp.getAlignment(),
260 "Pass (" + pass1 + "," + pass2 + "," + pass3
261 + "): before start of pass3: " + nextxref
264 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
266 List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
267 : new CrossRef(xrseqs, dataset)
268 .findXrefSourcesForSequences(avp
269 .getAlignViewport().isNucleotide());
271 stringify(dbtoviewBit, savedProjects, nextxref, avp);
272 xrptypes.put(nextxref, _xrptypes);
276 // now do the second xref pass starting from either saved or just
277 // recovered split pane, in sequence
278 do // retrieve second set of cross refs or recover and verify
281 for (AlignmentViewPanel avp : cra_views)
283 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
284 for (String xrefdb : xrptypes.get(nextxref))
286 List<AlignmentViewPanel> cra_views2 = new ArrayList<>();
288 String nextnextxref = nextxref + " -> " + xrefdb + "{"
293 SequenceI[] xrseqs = avp.getAlignment()
294 .getSequencesArray();
295 AlignFrame nextaf = Desktop.getAlignFrameFor(avp
296 .getAlignViewport());
298 cra = CrossRefAction.getHandlerFor(xrseqs, avp
299 .getAlignViewport().isNucleotide(), xrefdb,
302 cra_views2 = (List<AlignmentViewPanel>) PA.getValue(
304 if (cra_views2.size() == 0)
307 .add("No crossrefs retrieved for '"
308 + nextxref + "' to " + xrefdb
309 + " via '" + nextaf.getTitle() + "'");
312 assertNucleotide(cra_views2.get(0),
313 "Nucleotide panel included proteins for '"
314 + nextxref + "' to " + xrefdb
315 + " via '" + nextaf.getTitle() + "'");
316 assertProtein(cra_views2.get(1),
317 "Protein panel included nucleotides for '"
318 + nextxref + "' to " + xrefdb
319 + " via '" + nextaf.getTitle() + "'");
324 Desktop.instance.closeAll_actionPerformed(null);
325 // recover stored project
326 File storedProject = savedProjects.get(nextnextxref);
327 if (storedProject == null)
329 failedProjectRecoveries
330 .add("Failed to store a view for '"
331 + nextnextxref + "'");
334 AlignFrame af2 = new FileLoader(false)
335 .LoadFileWaitTillLoaded(
336 savedProjects.get(nextnextxref)
338 DataSourceType.FILE);
339 System.out.println("Recovered view for '"
340 + nextnextxref + "' from '"
341 + savedProjects.get(nextnextxref).toString()
343 // gymnastics to recover the alignPanel/Complementary
345 if (af2.getViewport().isNucleotide())
347 // top view, then bottom
348 cra_views2.add(af2.getViewport().getAlignPanel());
349 cra_views2.add(((jalview.gui.AlignViewport) af2
350 .getViewport().getCodingComplement())
356 // bottom view, then top
357 cra_views2.add(((jalview.gui.AlignViewport) af2
358 .getViewport().getCodingComplement())
360 cra_views2.add(af2.getViewport().getAlignPanel());
362 Assert.assertEquals(cra_views2.size(), 2);
363 Assert.assertNotNull(cra_views2.get(0));
364 Assert.assertNotNull(cra_views2.get(1));
367 for (AlignmentViewPanel nextavp : cra_views2)
369 nextnextxref = nextxref + " -> " + xrefdb + "{" + q++
372 // verify references for this panel
373 AlignmentTest.assertAlignmentDatasetRefs(
374 nextavp.getAlignment(), "" + "Pass (" + pass1
375 + "," + pass2 + "): For "
376 + nextnextxref + ":");
377 assertDatasetIsNormalisedKnownDefect(
378 nextavp.getAlignment(), "" + "Pass (" + pass1
379 + "," + pass2 + "): For "
380 + nextnextxref + ":");
382 stringify(dbtoviewBit, savedProjects, nextnextxref,
384 keyseq.add(nextnextxref);
386 } // end of loop around showing all xrefdb for crossrf2
388 } // end of loop around all viewpanels from crossrf1
389 } while (pass2 == 2 && pass3++ < 2);
390 // fetchdb->crossref1->crossref-2->verify for xrefs we
391 // either loop twice when pass2=0, or just once when pass2=1
392 // (recovered project from previous crossref)
394 } // end of loop over db-xrefs for crossref-2
396 // fetchdb-->crossref1
397 // for each xref we try to retrieve xref, store and verify when
398 // pass1=0, or just retrieve and verify when pass1=1
399 } while (pass1 == 1 && pass2++ < 2);
402 // loop twice: first, do the retrieve, second recover from saved project
404 // increment pass counters, so we repeat traversal starting from the
405 // oldest saved project first.
408 // verify stored projects for first set of cross references
410 // and verify cross-references retrieved from stored projects
420 if (failedXrefMenuItems.size() > 0)
422 for (String s : failedXrefMenuItems)
424 System.err.println(s);
426 Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
429 if (failedProjectRecoveries.size() > 0)
432 for (String s : failedProjectRecoveries)
434 System.err.println(s);
436 Assert.fail("Didn't recover projects for some retrievals (did they retrieve ?) ("
437 + failedProjectRecoveries.size() + " counts)");
439 if (failedDBRetr.size() > 0)
441 for (String s : failedProjectRecoveries)
443 System.err.println(s);
445 Assert.fail("Didn't retrieve some db refs for checking cross-refs ("
446 + failedDBRetr.size() + " counts)");
450 private void filterDbRefs(List<String> ptypes, List<String> limit)
455 while (ptypes.size() > p)
457 if (!limit.contains(ptypes.get(p)))
470 * wrapper to trap known defect for AH002001 testcase
475 private void assertDatasetIsNormalisedKnownDefect(AlignmentI al,
480 AlignmentTest.assertDatasetIsNormalised(al, message);
481 } catch (AssertionError ae)
483 if (!ae.getMessage().endsWith("EMBL|AH002001"))
490 .println("Ignored exception for known defect: JAL-2179 : "
497 private void assertProtein(AlignmentViewPanel alignmentViewPanel,
500 assertType(true, alignmentViewPanel, message);
503 private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
506 assertType(false, alignmentViewPanel, message);
509 private void assertType(boolean expectProtein,
510 AlignmentViewPanel alignmentViewPanel, String message)
512 List<SequenceI> nonType = new ArrayList<>();
513 for (SequenceI sq : alignmentViewPanel.getAlignViewport()
514 .getAlignment().getSequences())
516 if (sq.isProtein() != expectProtein)
521 if (nonType.size() > 0)
523 Assert.fail(message + " [ "
524 + (expectProtein ? "nucleotides were " : "proteins were ")
525 + nonType.toString() + " ]");
530 * first time called, record strings derived from alignment and
531 * alignedcodonframes, and save view to a project file. Second time called,
532 * compare strings to existing ones. org.testng.Assert.assertTrue on
536 * map between xrefpath and view string
537 * @param savedProjects
538 * - map from xrefpath to saved project filename (createTempFile)
540 * - xrefpath - unique ID for this context (composed of sequence of
541 * db-fetch/cross-ref actions preceeding state)
543 * - viewpanel to store (for viewpanels in splitframe, the same
544 * project should be written for both panels, only one needs
545 * recovering for comparison on the next stringify call, but each
546 * viewpanel needs to be called with a distinct xrefpath to ensure
547 * each one's strings are compared)
549 private void stringify(Map<String, String> dbtoviewBit,
550 Map<String, File> savedProjects, String xrefpath,
551 AlignmentViewPanel avp)
553 if (savedProjects != null)
555 if (savedProjects.get(xrefpath) == null)
557 // write a project file for this view. On the second pass, this will be
558 // recovered and cross-references verified
561 File prfile = File.createTempFile("crossRefTest", ".jvp");
562 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
563 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
565 System.out.println("Written view from '" + xrefpath + "' as '"
566 + prfile.getAbsolutePath() + "'");
567 savedProjects.put(xrefpath, prfile);
568 } catch (IOException q)
570 Assert.fail("Unexpected IO Exception", q);
575 System.out.println("Stringify check on view from '" + xrefpath
576 + "' [ possibly retrieved from '"
577 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
582 StringBuilder sbr = new StringBuilder();
583 sbr.append(avp.getAlignment().toString());
585 sbr.append("<End of alignment>");
587 sbr.append(avp.getAlignment().getDataset());
589 sbr.append("<End of dataset>");
592 if (avp.getAlignment().getCodonFrames() != null)
594 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
596 sbr.append("<AlignedCodonFrame " + p++ + ">");
598 sbr.append(ac.toString());
602 String dbt = dbtoviewBit.get(xrefpath);
605 dbtoviewBit.put(xrefpath, sbr.toString());
609 Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "