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;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
43 @Test(singleThreaded = true)
44 public class CrossRef2xmlTests extends Jalview2xmlBase
48 * test store and recovery of all reachable cross refs from all reachable
49 * crossrefs for one or more fetched db refs. Currently, this test has a known
54 @Test(groups = { "Operational" }, enabled = true)
55 public void testRetrieveAndShowCrossref() throws Exception
58 List<String> failedDBRetr = new ArrayList<String>();
59 List<String> failedXrefMenuItems = new ArrayList<String>();
60 List<String> failedProjectRecoveries = new ArrayList<String>();
62 // for every set of db queries
64 // verify presence of expected xrefs
65 // show xrefs - verify expected type of frame is shown for each xref
67 // - verify original -> xref -> xref(original) recovers frame containing at
68 // least the first retrieved sequence
71 // 2. individual frames
72 // 3. load each one back and verify
73 // . aligned sequences (.toString() )
74 // . xrefs (.toString() )
78 HashMap<String, String> dbtoviewBit = new HashMap<String, String>();
79 List<String> keyseq = new ArrayList<String>();
80 HashMap<String, File> savedProjects = new HashMap<String, File>();
82 for (String[] did : new String[][] { { "ENSEMBL", "ENSG00000157764" },
83 { "UNIPROT", "P01731" } })
85 // pass counters - 0 - first pass, 1 means retrieve project rather than
87 int pass1 = 0, pass2 = 0, pass3 = 0;
88 // each do loop performs two iterations in the first outer loop pass, but
89 // only performs one iteration on the second outer loop
90 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
91 // { pass 2 = 0 { pass 3 = 0 } }
94 String first = did[0] + " " + did[1];
100 List<String> ptypes = null;
105 List<AlignFrame> afs = jalview.gui.SequenceFetcher.fetchAndShow(
109 failedDBRetr.add("Didn't retrieve " + first);
115 // verify references for retrieved data
116 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
117 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
118 + pass3 + "): Fetch " + first + ":");
119 assertDatasetIsNormalisedKnownDefect(af.getViewport()
120 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
121 + pass3 + "): Fetch " + first + ":");
122 dna = af.getViewport().getAlignment().isNucleotide();
123 retral = af.getViewport().getAlignment();
124 dataset = retral.getDataset();
125 seqs = retral.getSequencesArray();
130 Desktop.instance.closeAll_actionPerformed(null);
131 // recover stored project
132 af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
133 .get(first).toString(), FormatAdapter.FILE);
134 System.out.println("Recovered view for '" + first + "' from '"
135 + savedProjects.get(first).toString() + "'");
136 dna = af.getViewport().getAlignment().isNucleotide();
137 retral = af.getViewport().getAlignment();
138 dataset = retral.getDataset();
139 seqs = retral.getSequencesArray();
141 // verify references for recovered data
142 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
143 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
144 + pass3 + "): Recover " + first + ":");
145 assertDatasetIsNormalisedKnownDefect(af.getViewport()
146 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
147 + pass3 + "): Recover " + first + ":");
151 // store project on first pass, compare next pass
152 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
154 ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
155 seqs, dataset).findXrefSourcesForSequences(dna);
157 // start of pass2: retrieve each cross-ref for fetched or restored
159 do // first cross ref and recover crossref loop
162 for (String db : ptypes)
164 // counter for splitframe views retrieved via crossref
166 // build next key so we an retrieve all views
167 String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
168 // perform crossref action, or retrieve stored project
169 List<AlignmentViewPanel> cra_views = new ArrayList<AlignmentViewPanel>();
170 CrossRefAction cra = null;
173 { // retrieve and show cross-refs in this thread
174 cra = new CrossRefAction(af, seqs, dna, db);
176 if (cra.getXrefViews().size() == 0)
178 failedXrefMenuItems.add("No crossrefs retrieved for "
179 + first + " -> " + db);
182 cra_views = cra.getXrefViews();
183 assertNucleotide(cra_views.get(0),
184 "Nucleotide panel included proteins for " + first
186 assertProtein(cra_views.get(1),
187 "Protein panel included nucleotides for " + first
192 Desktop.instance.closeAll_actionPerformed(null);
194 // recover stored project
195 File storedProject = savedProjects.get(nextxref);
196 if (storedProject == null)
198 failedProjectRecoveries.add("Failed to store a view for '"
203 // recover stored project
204 AlignFrame af2 = new FileLoader(false)
205 .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
206 .toString(), FormatAdapter.FILE);
207 System.out.println("Recovered view for '" + nextxref
208 + "' from '" + savedProjects.get(nextxref).toString()
210 // gymnastics to recover the alignPanel/Complementary alignPanel
211 if (af2.getViewport().isNucleotide())
213 // top view, then bottom
214 cra_views.add(af2.getViewport().getAlignPanel());
215 cra_views.add(((jalview.gui.AlignViewport) af2
216 .getViewport().getCodingComplement())
222 // bottom view, then top
223 cra_views.add(((jalview.gui.AlignViewport) af2
224 .getViewport().getCodingComplement())
226 cra_views.add(af2.getViewport().getAlignPanel());
230 HashMap<String, List<String>> xrptypes = new HashMap<String, List<String>>();
231 // first save/verify views.
232 for (AlignmentViewPanel avp : cra_views)
234 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
235 // verify references for this panel
236 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
237 "Pass (" + pass1 + "," + pass2 + "," + pass3
238 + "): before start of pass3: " + nextxref
240 assertDatasetIsNormalisedKnownDefect(avp.getAlignment(),
241 "Pass (" + pass1 + "," + pass2 + "," + pass3
242 + "): before start of pass3: " + nextxref
245 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
247 List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
248 : new CrossRef(xrseqs, dataset)
249 .findXrefSourcesForSequences(avp
250 .getAlignViewport().isNucleotide());
252 stringify(dbtoviewBit, savedProjects, nextxref, avp);
253 xrptypes.put(nextxref, _xrptypes);
257 // now do the second xref pass starting from either saved or just
258 // recovered split pane, in sequence
259 do // retrieve second set of cross refs or recover and verify
262 for (AlignmentViewPanel avp : cra_views)
264 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
265 for (String xrefdb : xrptypes.get(nextxref))
267 List<AlignmentViewPanel> cra_views2 = new ArrayList<AlignmentViewPanel>();
269 String nextnextxref = nextxref
270 + " -> " + xrefdb + "{" + q + "}";
275 SequenceI[] xrseqs = avp.getAlignment()
276 .getSequencesArray();
277 AlignFrame nextaf = Desktop.getAlignFrameFor(avp
278 .getAlignViewport());
280 cra = new CrossRefAction(nextaf, xrseqs, avp
281 .getAlignViewport().isNucleotide(), xrefdb);
283 if (cra.getXrefViews().size() == 0)
286 .add("No crossrefs retrieved for '"
287 + nextxref + "' to " + xrefdb + " via '"
288 + nextaf.getTitle() + "'");
291 cra_views2 = cra.getXrefViews();
292 assertNucleotide(cra_views2.get(0),
293 "Nucleotide panel included proteins for '"
294 + nextxref + "' to " + xrefdb
295 + " via '" + nextaf.getTitle() + "'");
296 assertProtein(cra_views2.get(1),
297 "Protein panel included nucleotides for '"
298 + nextxref + "' to " + xrefdb
299 + " via '" + nextaf.getTitle() + "'");
304 Desktop.instance.closeAll_actionPerformed(null);
305 // recover stored project
306 File storedProject = savedProjects.get(nextnextxref);
307 if (storedProject == null)
309 failedProjectRecoveries
310 .add("Failed to store a view for '"
311 + nextnextxref + "'");
314 AlignFrame af2 = new FileLoader(false)
315 .LoadFileWaitTillLoaded(
316 savedProjects.get(nextnextxref)
317 .toString(), FormatAdapter.FILE);
318 System.out.println("Recovered view for '"
319 + nextnextxref + "' from '"
320 + savedProjects.get(nextnextxref).toString()
322 // gymnastics to recover the alignPanel/Complementary
324 if (af2.getViewport().isNucleotide())
326 // top view, then bottom
327 cra_views2.add(af2.getViewport().getAlignPanel());
328 cra_views2.add(((jalview.gui.AlignViewport) af2
329 .getViewport().getCodingComplement())
335 // bottom view, then top
336 cra_views2.add(((jalview.gui.AlignViewport) af2
337 .getViewport().getCodingComplement())
339 cra_views2.add(af2.getViewport().getAlignPanel());
341 Assert.assertEquals(cra_views2.size(), 2);
342 Assert.assertNotNull(cra_views2.get(0));
343 Assert.assertNotNull(cra_views2.get(1));
346 for (AlignmentViewPanel nextavp : cra_views2)
348 nextnextxref = nextxref
349 + " -> " + xrefdb + "{" + q++ + "}";
351 // verify references for this panel
352 AlignmentTest.assertAlignmentDatasetRefs(
353 nextavp.getAlignment(), "" + "Pass (" + pass1
354 + "," + pass2 + "): For "
355 + nextnextxref + ":");
356 assertDatasetIsNormalisedKnownDefect(
357 nextavp.getAlignment(), "" + "Pass (" + pass1
358 + "," + pass2 + "): For "
359 + nextnextxref + ":");
361 stringify(dbtoviewBit, savedProjects, nextnextxref,
363 keyseq.add(nextnextxref);
365 } // end of loop around showing all xrefdb for crossrf2
367 } // end of loop around all viewpanels from crossrf1
368 } while (pass2 == 2 && pass3++ < 2);
369 // fetchdb->crossref1->crossref-2->verify for xrefs we
370 // either loop twice when pass2=0, or just once when pass2=1
371 // (recovered project from previous crossref)
373 } // end of loop over db-xrefs for crossref-2
375 // fetchdb-->crossref1
376 // for each xref we try to retrieve xref, store and verify when
377 // pass1=0, or just retrieve and verify when pass1=1
378 } while (pass1 == 1 && pass2++ < 2);
381 // loop twice: first, do the retrieve, second recover from saved project
383 // increment pass counters, so we repeat traversal starting from the
384 // oldest saved project first.
387 // verify stored projects for first set of cross references
389 // and verify cross-references retrieved from stored projects
399 if (failedXrefMenuItems.size() > 0)
401 for (String s : failedXrefMenuItems)
403 System.err.println(s);
405 Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
408 if (failedProjectRecoveries.size() > 0)
411 for (String s : failedProjectRecoveries)
413 System.err.println(s);
415 Assert.fail("Didn't recover projects for some retrievals (did they retrieve ?) ("
416 + failedProjectRecoveries.size() + " counts)");
418 if (failedDBRetr.size() > 0)
420 for (String s : failedProjectRecoveries)
422 System.err.println(s);
424 Assert.fail("Didn't retrieve some db refs for checking cross-refs ("
425 + failedDBRetr.size() + " counts)");
430 * wrapper to trap known defect for AH002001 testcase
435 private void assertDatasetIsNormalisedKnownDefect(AlignmentI al,
440 AlignmentTest.assertDatasetIsNormalised(al, message);
441 } catch (AssertionError ae)
443 if (!ae.getMessage().endsWith("EMBL|AH002001"))
450 .println("Ignored exception for known defect: JAL-2179 : "
457 private void assertProtein(AlignmentViewPanel alignmentViewPanel,
460 assertType(true, alignmentViewPanel, message);
463 private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
466 assertType(false, alignmentViewPanel, message);
469 private void assertType(boolean expectProtein,
470 AlignmentViewPanel alignmentViewPanel, String message)
472 List<SequenceI> nonType = new ArrayList<SequenceI>();
473 for (SequenceI sq : alignmentViewPanel.getAlignViewport()
477 if (sq.isProtein() != expectProtein)
482 if (nonType.size() > 0)
484 Assert.fail(message + " [ "
485 + (expectProtein ? "nucleotides were " : "proteins were ")
492 * first time called, record strings derived from alignment and
493 * alignedcodonframes, and save view to a project file. Second time called,
494 * compare strings to existing ones. org.testng.Assert.assertTrue on
498 * map between xrefpath and view string
499 * @param savedProjects
500 * - map from xrefpath to saved project filename (createTempFile)
502 * - xrefpath - unique ID for this context (composed of sequence of
503 * db-fetch/cross-ref actions preceeding state)
505 * - viewpanel to store (for viewpanels in splitframe, the same
506 * project should be written for both panels, only one needs
507 * recovering for comparison on the next stringify call, but each
508 * viewpanel needs to be called with a distinct xrefpath to ensure
509 * each one's strings are compared)
511 private void stringify(HashMap<String, String> dbtoviewBit,
512 HashMap<String, File> savedProjects, String xrefpath,
513 AlignmentViewPanel avp)
515 if (savedProjects != null)
517 if (savedProjects.get(xrefpath) == null)
519 // write a project file for this view. On the second pass, this will be
520 // recovered and cross-references verified
523 File prfile = File.createTempFile("crossRefTest", ".jvp");
524 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
525 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
527 System.out.println("Written view from '" + xrefpath + "' as '"
528 + prfile.getAbsolutePath() + "'");
529 savedProjects.put(xrefpath, prfile);
530 } catch (IOException q)
532 Assert.fail("Unexpected IO Exception", q);
537 System.out.println("Stringify check on view from '" + xrefpath
538 + "' [ possibly retrieved from '"
539 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
544 StringBuilder sbr = new StringBuilder();
545 sbr.append(avp.getAlignment().toString());
547 sbr.append("<End of alignment>");
549 sbr.append(avp.getAlignment().getDataset());
551 sbr.append("<End of dataset>");
554 if (avp.getAlignment().getCodonFrames() != null)
556 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
558 sbr.append("<AlignedCodonFrame " + p++ + ">");
560 sbr.append(ac.toString());
564 String dbt = dbtoviewBit.get(xrefpath);
567 dbtoviewBit.put(xrefpath, sbr.toString());
571 Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "