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 expanded views
52 @Test(groups = { "Operational" }, enabled = true)
53 public void testRetrieveAndShowCrossref() throws Exception
56 List<String> failedDBRetr = new ArrayList<String>();
57 List<String> failedXrefMenuItems = new ArrayList<String>();
58 List<String> failedProjectRecoveries = new ArrayList<String>();
60 // for every set of db queries
62 // verify presence of expected xrefs
63 // show xrefs - verify expected type of frame is shown for each xref
65 // - verify original -> xref -> xref(original) recovers frame containing at
66 // least the first retrieved sequence
69 // 2. individual frames
70 // 3. load each one back and verify
71 // . aligned sequences (.toString() )
72 // . xrefs (.toString() )
76 HashMap<String, String> dbtoviewBit = new HashMap<String, String>();
77 List<String> keyseq = new ArrayList<String>();
78 HashMap<String, File> savedProjects = new HashMap<String, File>();
80 for (String[] did : new String[][] { { "ENSEMBL", "ENSG00000157764" },
81 { "UNIPROT", "P01731" } })
83 // pass counters - 0 - first pass, 1 means retrieve project rather than
85 int pass1 = 0, pass2 = 0, pass3 = 0;
86 // each do loop performs two iterations in the first outer loop pass, but
87 // only performs one iteration on the second outer loop
88 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
89 // { pass 2 = 0 { pass 3 = 0 } }
92 String first = did[0] + " " + did[1];
98 List<String> ptypes = null;
103 List<AlignFrame> afs = jalview.gui.SequenceFetcher.fetchAndShow(
107 failedDBRetr.add("Didn't retrieve " + first);
113 // verify references for retrieved data
114 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
115 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
116 + pass3 + "): Fetch " + first + ":");
117 dna = af.getViewport().getAlignment().isNucleotide();
118 retral = af.getViewport().getAlignment();
119 dataset = retral.getDataset();
120 seqs = retral.getSequencesArray();
125 Desktop.instance.closeAll_actionPerformed(null);
126 // recover stored project
127 af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
128 .get(first).toString(), FormatAdapter.FILE);
129 System.out.println("Recovered view for '" + first + "' from '"
130 + savedProjects.get(first).toString() + "'");
131 dna = af.getViewport().getAlignment().isNucleotide();
132 retral = af.getViewport().getAlignment();
133 dataset = retral.getDataset();
134 seqs = retral.getSequencesArray();
136 // verify references for recovered data
137 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
138 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
139 + pass3 + "): Recover " + first + ":");
143 // store project on first pass, compare next pass
144 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
146 ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
147 seqs, dataset).findXrefSourcesForSequences(dna);
149 // start of pass2: retrieve each cross-ref for fetched or restored
151 do // first cross ref and recover crossref loop
154 for (String db : ptypes)
156 // counter for splitframe views retrieved via crossref
158 // build next key so we an retrieve all views
159 String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
160 // perform crossref action, or retrieve stored project
161 List<AlignmentViewPanel> cra_views = new ArrayList<AlignmentViewPanel>();
162 CrossRefAction cra = null;
165 { // retrieve and show cross-refs in this thread
166 cra = new CrossRefAction(af, seqs, dna, db);
168 if (cra.getXrefViews().size() == 0)
170 failedXrefMenuItems.add("No crossrefs retrieved for "
171 + first + " -> " + db);
174 cra_views = cra.getXrefViews();
175 assertNucleotide(cra_views.get(0),
176 "Nucleotide panel included proteins for " + first
178 assertProtein(cra_views.get(1),
179 "Protein panel included nucleotides for " + first
184 Desktop.instance.closeAll_actionPerformed(null);
186 // recover stored project
187 File storedProject = savedProjects.get(nextxref);
188 if (storedProject == null)
190 failedProjectRecoveries.add("Failed to store a view for '"
195 // recover stored project
196 AlignFrame af2 = new FileLoader(false)
197 .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
198 .toString(), FormatAdapter.FILE);
199 System.out.println("Recovered view for '" + nextxref
200 + "' from '" + savedProjects.get(nextxref).toString()
202 // gymnastics to recover the alignPanel/Complementary alignPanel
203 if (af2.getViewport().isNucleotide())
205 // top view, then bottom
206 cra_views.add(af2.getViewport().getAlignPanel());
207 cra_views.add(((jalview.gui.AlignViewport) af2
208 .getViewport().getCodingComplement())
214 // bottom view, then top
215 cra_views.add(((jalview.gui.AlignViewport) af2
216 .getViewport().getCodingComplement())
218 cra_views.add(af2.getViewport().getAlignPanel());
222 HashMap<String, List<String>> xrptypes = new HashMap<String, List<String>>();
223 // first save/verify views.
224 for (AlignmentViewPanel avp : cra_views)
226 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
227 // verify references for this panel
228 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
229 "Pass (" + pass1 + "," + pass2 + "," + pass3
230 + "): before start of pass3: " + nextxref
233 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
235 List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
236 : new CrossRef(xrseqs, dataset)
237 .findXrefSourcesForSequences(avp
238 .getAlignViewport().isNucleotide());
240 stringify(dbtoviewBit, savedProjects, nextxref, avp);
241 xrptypes.put(nextxref, _xrptypes);
245 // now do the second xref pass starting from either saved or just
246 // recovered split pane, in sequence
247 do // retrieve second set of cross refs or recover and verify
250 for (AlignmentViewPanel avp : cra_views)
252 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
253 for (String xrefdb : xrptypes.get(nextxref))
255 List<AlignmentViewPanel> cra_views2 = new ArrayList<AlignmentViewPanel>();
257 String nextnextxref = nextxref
258 + " -> " + xrefdb + "{" + q + "}";
263 SequenceI[] xrseqs = avp.getAlignment()
264 .getSequencesArray();
265 AlignFrame nextaf = Desktop.getAlignFrameFor(avp
266 .getAlignViewport());
268 cra = new CrossRefAction(nextaf, xrseqs, avp
269 .getAlignViewport().isNucleotide(), xrefdb);
271 if (cra.getXrefViews().size() == 0)
274 .add("No crossrefs retrieved for '"
275 + nextxref + "' to " + xrefdb + " via '"
276 + nextaf.getTitle() + "'");
279 cra_views2 = cra.getXrefViews();
280 assertNucleotide(cra_views2.get(0),
281 "Nucleotide panel included proteins for '"
282 + nextxref + "' to " + xrefdb
283 + " via '" + nextaf.getTitle() + "'");
284 assertProtein(cra_views2.get(1),
285 "Protein panel included nucleotides for '"
286 + nextxref + "' to " + xrefdb
287 + " via '" + nextaf.getTitle() + "'");
292 Desktop.instance.closeAll_actionPerformed(null);
293 // recover stored project
294 File storedProject = savedProjects.get(nextnextxref);
295 if (storedProject == null)
297 failedProjectRecoveries
298 .add("Failed to store a view for '"
299 + nextnextxref + "'");
302 AlignFrame af2 = new FileLoader(false)
303 .LoadFileWaitTillLoaded(
304 savedProjects.get(nextnextxref)
305 .toString(), FormatAdapter.FILE);
306 System.out.println("Recovered view for '"
307 + nextnextxref + "' from '"
308 + savedProjects.get(nextnextxref).toString()
310 // gymnastics to recover the alignPanel/Complementary
312 if (af2.getViewport().isNucleotide())
314 // top view, then bottom
315 cra_views2.add(af2.getViewport().getAlignPanel());
316 cra_views2.add(((jalview.gui.AlignViewport) af2
317 .getViewport().getCodingComplement())
323 // bottom view, then top
324 cra_views2.add(((jalview.gui.AlignViewport) af2
325 .getViewport().getCodingComplement())
327 cra_views2.add(af2.getViewport().getAlignPanel());
329 Assert.assertEquals(cra_views2.size(), 2);
330 Assert.assertNotNull(cra_views2.get(0));
331 Assert.assertNotNull(cra_views2.get(1));
334 for (AlignmentViewPanel nextavp : cra_views2)
336 nextnextxref = nextxref
337 + " -> " + xrefdb + "{" + q++ + "}";
339 // verify references for this panel
340 AlignmentTest.assertAlignmentDatasetRefs(
341 nextavp.getAlignment(), "" + "Pass (" + pass1
342 + "," + pass2 + "): For "
343 + nextnextxref + ":");
345 stringify(dbtoviewBit, savedProjects, nextnextxref,
347 keyseq.add(nextnextxref);
349 } // end of loop around showing all xrefdb for crossrf2
351 } // end of loop around all viewpanels from crossrf1
352 } while (pass2 == 2 && pass3++ < 2);
353 // fetchdb->crossref1->crossref-2->verify for xrefs we
354 // either loop twice when pass2=0, or just once when pass2=1
355 // (recovered project from previous crossref)
357 } // end of loop over db-xrefs for crossref-2
359 // fetchdb-->crossref1
360 // for each xref we try to retrieve xref, store and verify when
361 // pass1=0, or just retrieve and verify when pass1=1
362 } while (pass1 == 1 && pass2++ < 2);
365 // loop twice: first, do the retrieve, second recover from saved project
367 // increment pass counters, so we repeat traversal starting from the
368 // oldest saved project first.
371 // verify stored projects for first set of cross references
373 // and verify cross-references retrieved from stored projects
382 // verify stored projects for second set of cross references
384 // and verify cross-references retrievable from those stored projects.
390 if (failedXrefMenuItems.size() > 0)
392 for (String s : failedXrefMenuItems)
394 System.err.println(s);
396 Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
399 if (failedProjectRecoveries.size() > 0)
402 for (String s : failedProjectRecoveries)
404 System.err.println(s);
406 Assert.fail("Didn't recover projects for some retrievals (did they retrieve ?) ("
407 + failedProjectRecoveries.size() + " counts)");
409 if (failedDBRetr.size() > 0)
411 for (String s : failedProjectRecoveries)
413 System.err.println(s);
415 Assert.fail("Didn't retrieve some db refs for checking cross-refs ("
416 + failedDBRetr.size() + " counts)");
420 private void assertProtein(AlignmentViewPanel alignmentViewPanel,
423 assertType(true, alignmentViewPanel, message);
426 private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
429 assertType(false, alignmentViewPanel, message);
432 private void assertType(boolean expectProtein,
433 AlignmentViewPanel alignmentViewPanel, String message)
435 List<SequenceI> nonType = new ArrayList<SequenceI>();
436 for (SequenceI sq : alignmentViewPanel.getAlignViewport()
440 if (sq.isProtein() != expectProtein)
445 if (nonType.size() > 0)
447 Assert.fail(message + " [ "
448 + (expectProtein ? "nucleotides were " : "proteins were ")
455 * first time called, record strings derived from alignment and
456 * alignedcodonframes, and save view to a project file. Second time called,
457 * compare strings to existing ones. org.testng.Assert.assertTrue on
461 * map between xrefpath and view string
462 * @param savedProjects
463 * - map from xrefpath to saved project filename (createTempFile)
465 * - xrefpath - unique ID for this context (composed of sequence of
466 * db-fetch/cross-ref actions preceeding state)
468 * - viewpanel to store (for viewpanels in splitframe, the same
469 * project should be written for both panels, only one needs
470 * recovering for comparison on the next stringify call, but each
471 * viewpanel needs to be called with a distinct xrefpath to ensure
472 * each one's strings are compared)
474 private void stringify(HashMap<String, String> dbtoviewBit,
475 HashMap<String, File> savedProjects, String xrefpath,
476 AlignmentViewPanel avp)
478 if (savedProjects != null)
480 if (savedProjects.get(xrefpath) == null)
482 // write a project file for this view. On the second pass, this will be
483 // recovered and cross-references verified
486 File prfile = File.createTempFile("crossRefTest", ".jvp");
487 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
488 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
490 System.out.println("Written view from '" + xrefpath + "' as '"
491 + prfile.getAbsolutePath() + "'");
492 savedProjects.put(xrefpath, prfile);
493 } catch (IOException q)
495 Assert.fail("Unexpected IO Exception", q);
500 System.out.println("Stringify check on view from '" + xrefpath
501 + "' [ possibly retrieved from '"
502 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
507 StringBuilder sbr = new StringBuilder();
508 sbr.append(avp.getAlignment().toString());
510 sbr.append("<End of alignment>");
512 sbr.append(avp.getAlignment().getDataset());
514 sbr.append("<End of dataset>");
517 if (avp.getAlignment().getCodonFrames() != null)
519 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
521 sbr.append("<AlignedCodonFrame " + p++ + ">");
523 sbr.append(ac.toString());
527 String dbt = dbtoviewBit.get(xrefpath);
530 dbtoviewBit.put(xrefpath, sbr.toString());
534 Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "