From 9b44a36c39de64a90fee2c64a9d699c5291d45fa Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Tue, 16 Aug 2016 10:39:11 +0100 Subject: [PATCH] JAL-2154 xref test harness - test fails if there are differences between dataset/alignment/codonframes for views created from a sequence of cross-ref actions, and the same views after restoring from a jalview project. Currently fails ! --- test/jalview/io/CrossRef2xmlTests.java | 321 ++++++++++++++++++++++++++------ 1 file changed, 268 insertions(+), 53 deletions(-) diff --git a/test/jalview/io/CrossRef2xmlTests.java b/test/jalview/io/CrossRef2xmlTests.java index dd6aac6..f0d3240 100644 --- a/test/jalview/io/CrossRef2xmlTests.java +++ b/test/jalview/io/CrossRef2xmlTests.java @@ -24,12 +24,18 @@ import static org.testng.AssertJUnit.assertTrue; import jalview.analysis.CrossRef; import jalview.api.AlignmentViewPanel; +import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.AlignmentI; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.gui.CrossRefAction; import jalview.gui.Desktop; +import jalview.gui.Jalview2XML; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; import java.util.IdentityHashMap; import java.util.List; @@ -64,70 +70,279 @@ public class CrossRef2xmlTests extends Jalview2xmlBase // . codonframes // // + HashMap dbtoviewBit = new HashMap(); + List keyseq = new ArrayList(); + HashMap savedProjects = new HashMap(); + for (String[] did : new String[][] { { "UNIPROT", "P01731" } }) { - AlignFrame af = jalview.gui.SequenceFetcher.fetchAndShow(did[0], - did[1]).get(0); - assertTrue("Didn't read in the example file correctly.", af != null); - boolean dna = af.getViewport().getAlignment().isNucleotide(); - AlignmentI retral = af.getViewport().getAlignment(); - AlignmentI dataset = retral.getDataset(); - SequenceI[] seqs = retral.getSequencesArray(); - List ptypes = (seqs == null || seqs.length == 0) ? null - : new CrossRef(seqs, dataset) - .findXrefSourcesForSequences(dna); - /* - * map between a view, and views generated after retrieving xrefs - */ - IdentityHashMap> viewxrefview = new IdentityHashMap>(); - /* - * map between a particular view and it's originating dbref path - */ - IdentityHashMap viewsourcedb = new IdentityHashMap(); - - String first = did[0] + " " + did[1]; - viewsourcedb.put(af.alignPanel, first); - for (String db : ptypes) + // pass counters - 0 - first pass, 1 means retrieve project rather than + // perform action + int pass1 = 0, pass2 = 0, pass3 = 0; + // each do loop performs two iterations in the first outer loop pass, but + // only performs one iteration on the second outer loop + // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1 + // { pass 2 = 0 { pass 3 = 0 } } + do { - // retrieve and show cross-refs in this thread - CrossRefAction cra = new CrossRefAction(af, seqs, dna, db); - cra.run(); - Assert.assertTrue(cra.getXrefViews().size() > 0, - "No crossrefs retrieved for " + db); - viewxrefview.put(af.alignPanel, cra.getXrefViews()); - - for (AlignmentViewPanel avp : cra.getXrefViews()) + String first = did[0] + " " + did[1]; + AlignFrame af = null; + if (pass1 == 0) + { + // retrieve dbref + + keyseq.add(first); + + af = jalview.gui.SequenceFetcher.fetchAndShow(did[0], did[1]) + .get(0); + assertTrue("Didn't retrieve " + first, af != null); + // store project to recover on next pass + stringify(dbtoviewBit, savedProjects, first, af.alignPanel); + } + else { + Desktop.instance.closeAll_actionPerformed(null); + // recover stored project + af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects + .get(first).toString(), FormatAdapter.FILE); + } - SequenceI[] xrseqs = avp.getAlignment().getSequencesArray(); - String nextxref = first + " -> " + db; - viewsourcedb.put(avp, nextxref); - List xrptypes = (seqs == null || seqs.length == 0) ? null - : new CrossRef(xrseqs, dataset) - .findXrefSourcesForSequences(avp - .getAlignViewport().isNucleotide()); - for (String xrefdb : xrptypes) + boolean dna = af.getViewport().getAlignment().isNucleotide(); + AlignmentI retral = af.getViewport().getAlignment(); + AlignmentI dataset = retral.getDataset(); + SequenceI[] seqs = retral.getSequencesArray(); + List ptypes = (seqs == null || seqs.length == 0) ? null + : new CrossRef(seqs, dataset) + .findXrefSourcesForSequences(dna); + + /* + * map between a view, and views generated after retrieving xrefs + */ + IdentityHashMap> viewxrefview = new IdentityHashMap>(); + /* + * map between a particular view and it's originating dbref path + */ + IdentityHashMap viewsourcedb = new IdentityHashMap(); + + viewsourcedb.put(af.alignPanel, first); + for (String db : ptypes) + { + pass2 = 0; + do // second cross ref and recover crossref loop { - AlignFrame nextaf = Desktop.getAlignFrameFor(avp - .getAlignViewport()); - cra = new CrossRefAction(nextaf, xrseqs, avp.getAlignViewport() - .isNucleotide(), xrefdb); - cra.run(); - Assert.assertTrue(cra.getXrefViews().size() > 0, - "No crossrefs found for '" + nextxref + "' to " - + xrefdb + " via '" + nextaf.getTitle() + "'"); - // save views for analysis - viewxrefview.put(avp, cra.getXrefViews()); - for (AlignmentViewPanel nextavp : cra.getXrefViews()) + // counter for splitframe views retrieved via crossref + int p = 0; + // build next key so we an retrieve all views + String nextxref = first + " -> " + db + "{" + p + "}"; + // perform crossref action, or retrieve stored project + List cra_views = new ArrayList(); + CrossRefAction cra = null; + if (pass2 == 0) + { // retrieve and show cross-refs in this thread + cra = new CrossRefAction(af, seqs, dna, db); + cra.run(); + Assert.assertTrue(cra.getXrefViews().size() > 0, + "No crossrefs retrieved for " + db); + cra_views = cra.getXrefViews(); + viewxrefview.put(af.alignPanel, cra.getXrefViews()); + } + else { + Desktop.instance.closeAll_actionPerformed(null); + // recover stored project + AlignFrame af2 = new FileLoader(false) + .LoadFileWaitTillLoaded(savedProjects.get(first) + .toString(), FormatAdapter.FILE); + // gymnastics to recover the alignPanel/Complementary alignPanel + if (af2.getViewport().isNucleotide()) + { + // top view, then bottom + cra_views.add(af2.getViewport().getAlignPanel()); + cra_views.add(((jalview.gui.AlignViewport) af2 + .getViewport().getCodingComplement()) + .getAlignPanel()); + + } + else + { + // bottom view, then top + cra_views.add(((jalview.gui.AlignViewport) af2 + .getViewport().getCodingComplement()) + .getAlignPanel()); + cra_views.add(af2.getViewport().getAlignPanel()); - viewsourcedb.put(nextavp, nextxref + " -> " + xrefdb); + } } - } + for (AlignmentViewPanel avp : cra_views) + { + + SequenceI[] xrseqs = avp.getAlignment().getSequencesArray(); + nextxref = first + " -> " + db + "{" + p++ + "}"; + + viewsourcedb.put(avp, nextxref); + stringify(dbtoviewBit, savedProjects, nextxref, avp); + keyseq.add(nextxref); + + List xrptypes = (seqs == null || seqs.length == 0) ? null + : new CrossRef(xrseqs, dataset) + .findXrefSourcesForSequences(avp + .getAlignViewport().isNucleotide()); + for (String xrefdb : xrptypes) + { + pass3 = 0; + do // 3rd cross ref and recover crossref loop + { + List cra_views2 = new ArrayList(); + int q = 0; + String nextnextxref = "{" + p + "}" + nextxref + " -> " + + xrefdb + "{" + q + "}"; + + AlignFrame nextaf = Desktop.getAlignFrameFor(avp + .getAlignViewport()); + if (pass3 == 0) + { + + cra = new CrossRefAction(nextaf, xrseqs, avp + .getAlignViewport().isNucleotide(), xrefdb); + cra.run(); + Assert.assertTrue(cra.getXrefViews().size() > 0, + "No crossrefs found for '" + nextxref + "' to " + + xrefdb + " via '" + nextaf.getTitle() + + "'"); + cra_views2 = cra.getXrefViews(); + } + else + { + Desktop.instance.closeAll_actionPerformed(null); + // recover stored project + AlignFrame af2 = new FileLoader(false) + .LoadFileWaitTillLoaded( + savedProjects.get(nextnextxref) + .toString(), FormatAdapter.FILE); + // gymnastics to recover the alignPanel/Complementary + // alignPanel + if (af2.getViewport().isNucleotide()) + { + // top view, then bottom + cra_views2.add(af2.getViewport().getAlignPanel()); + cra_views2.add(((jalview.gui.AlignViewport) af2 + .getViewport().getCodingComplement()) + .getAlignPanel()); + + } + else + { + // bottom view, then top + cra_views2.add(((jalview.gui.AlignViewport) af2 + .getViewport().getCodingComplement()) + .getAlignPanel()); + cra_views2.add(af2.getViewport().getAlignPanel()); + } + Assert.assertEquals(cra_views2.size(), 2); + Assert.assertNotNull(cra_views2.get(0)); + Assert.assertNotNull(cra_views2.get(1)); + } + + for (AlignmentViewPanel nextavp : cra_views2) + { + nextnextxref = "{" + p + "}" + nextxref + " -> " + + xrefdb + "{" + q++ + "}"; + viewsourcedb.put(nextavp, nextnextxref); + stringify(dbtoviewBit, savedProjects, nextnextxref, + nextavp); + keyseq.add(nextnextxref); + } + } while (pass3++ < 2 && pass2 < 1); + } + } + } while (pass2++ < 2 && pass1 < 1); } - } + } while (++pass1 < 2); } - Thread.sleep(50000); } + /** + * first time called, record strings derived from alignment and + * alignedcodonframes, and save view to a project file. Second time called, + * compare strings to existing ones. org.testng.Assert.assertTrue on + * stringmatch + * + * @param dbtoviewBit + * map between xrefpath and view string + * @param savedProjects + * - map from xrefpath to saved project filename (createTempFile) + * @param xrefpath + * - xrefpath - unique ID for this context (composed of sequence of + * db-fetch/cross-ref actions preceeding state) + * @param avp + * - viewpanel to store (for viewpanels in splitframe, the same + * project should be written for both panels, only one needs + * recovering for comparison on the next stringify call, but each + * viewpanel needs to be called with a distinct xrefpath to ensure + * each one's strings are compared) + */ + private void stringify(HashMap dbtoviewBit, + HashMap savedProjects, String xrefpath, + AlignmentViewPanel avp) + { + if (savedProjects != null) + { + if (savedProjects.get(xrefpath) == null) + { + // write a project file for this view. On the second pass, this will be + // recovered and cross-references verified + try + { + File prfile = File.createTempFile("crossRefTest", ".jvp"); + AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport()); + new Jalview2XML(false).saveAlignment(af, prfile.toString(), + af.getTitle()); + System.out.println("Written view from '" + xrefpath + "' as '" + + prfile.getAbsolutePath() + "'"); + savedProjects.put(xrefpath, prfile); + } catch (IOException q) + { + Assert.fail("Unexpected IO Exception", q); + } + } + else + { + System.out.println("Stringify check on view from '" + xrefpath + + "' [ possibly retrieved from '" + + savedProjects.get(xrefpath).getAbsolutePath() + "' ]"); + + } + } + + StringBuilder sbr = new StringBuilder(); + sbr.append(avp.getAlignment().toString()); + sbr.append("\n"); + sbr.append(""); + sbr.append("\n"); + sbr.append(avp.getAlignment().getDataset()); + sbr.append("\n"); + sbr.append(""); + sbr.append("\n"); + int p = 0; + if (avp.getAlignment().getCodonFrames() != null) + { + for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames()) + { + sbr.append(""); + sbr.append("\n"); + sbr.append(ac.toString()); + sbr.append("\n"); + } + } + String dbt = dbtoviewBit.get(xrefpath); + if (dbt == null) + { + dbtoviewBit.put(xrefpath, sbr.toString()); + } + else + { + Assert.assertEquals(sbr.toString(), dbt, + "stringify mismatch for " + xrefpath); + } + } } -- 1.7.10.2