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
55 List<String> failedXrefMenuItems = new ArrayList<String>();
56 // for every set of db queries
58 // verify presence of expected xrefs
59 // show xrefs - verify expected type of frame is shown for each xref
61 // - verify original -> xref -> xref(original) recovers frame containing at
62 // least the first retrieved sequence
65 // 2. individual frames
66 // 3. load each one back and verify
67 // . aligned sequences (.toString() )
68 // . xrefs (.toString() )
72 HashMap<String, String> dbtoviewBit = new HashMap<String, String>();
73 List<String> keyseq = new ArrayList<String>();
74 HashMap<String, File> savedProjects = new HashMap<String, File>();
76 for (String[] did : new String[][] { { "UNIPROT", "P01731" } })
78 // pass counters - 0 - first pass, 1 means retrieve project rather than
80 int pass1 = 0, pass2 = 0, pass3 = 0;
81 // each do loop performs two iterations in the first outer loop pass, but
82 // only performs one iteration on the second outer loop
83 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
84 // { pass 2 = 0 { pass 3 = 0 } }
87 String first = did[0] + " " + did[1];
93 List<String> ptypes = null;
100 af = jalview.gui.SequenceFetcher.fetchAndShow(did[0], did[1])
102 Assert.assertTrue(af != null, "Didn't retrieve " + first);
104 // verify references for retrieved data
105 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
106 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
107 + pass3 + "): Fetch " + first + ":");
108 dna = af.getViewport().getAlignment().isNucleotide();
109 retral = af.getViewport().getAlignment();
110 dataset = retral.getDataset();
111 seqs = retral.getSequencesArray();
116 Desktop.instance.closeAll_actionPerformed(null);
117 // recover stored project
118 af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
119 .get(first).toString(), FormatAdapter.FILE);
120 System.out.println("Recovered view for '" + first + "' from '"
121 + savedProjects.get(first).toString() + "'");
122 dna = af.getViewport().getAlignment().isNucleotide();
123 retral = af.getViewport().getAlignment();
124 dataset = retral.getDataset();
125 seqs = retral.getSequencesArray();
127 // verify references for recovered data
128 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
129 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
130 + pass3 + "): Recover " + first + ":");
134 // store project on first pass, compare next pass
135 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
137 ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
138 seqs, dataset).findXrefSourcesForSequences(dna);
140 // start of pass2: retrieve each cross-ref for fetched or restored
142 do // first cross ref and recover crossref loop
145 for (String db : ptypes)
147 // counter for splitframe views retrieved via crossref
149 // build next key so we an retrieve all views
150 String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
151 // perform crossref action, or retrieve stored project
152 List<AlignmentViewPanel> cra_views = new ArrayList<AlignmentViewPanel>();
153 CrossRefAction cra = null;
156 { // retrieve and show cross-refs in this thread
157 cra = new CrossRefAction(af, seqs, dna, db);
159 if (cra.getXrefViews().size() == 0)
161 failedXrefMenuItems.add("No crossrefs retrieved for "
162 + first + " -> " + db);
165 cra_views = cra.getXrefViews();
166 assertNucleotide(cra_views.get(0),
167 "Nucleotide panel included proteins for " + first
169 assertProtein(cra_views.get(1),
170 "Protein panel included nucleotides for " + first
175 Desktop.instance.closeAll_actionPerformed(null);
177 // recover stored project
178 AlignFrame af2 = new FileLoader(false)
179 .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
180 .toString(), FormatAdapter.FILE);
181 System.out.println("Recovered view for '" + nextxref
182 + "' from '" + savedProjects.get(nextxref).toString()
184 // gymnastics to recover the alignPanel/Complementary alignPanel
185 if (af2.getViewport().isNucleotide())
187 // top view, then bottom
188 cra_views.add(af2.getViewport().getAlignPanel());
189 cra_views.add(((jalview.gui.AlignViewport) af2
190 .getViewport().getCodingComplement())
196 // bottom view, then top
197 cra_views.add(((jalview.gui.AlignViewport) af2
198 .getViewport().getCodingComplement())
200 cra_views.add(af2.getViewport().getAlignPanel());
204 HashMap<String, List<String>> xrptypes = new HashMap<String, List<String>>();
205 // first save/verify views.
206 for (AlignmentViewPanel avp : cra_views)
208 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
209 // verify references for this panel
210 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
211 "Pass (" + pass1 + "," + pass2 + "," + pass3
212 + "): before start of pass3: " + nextxref
215 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
217 List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
218 : new CrossRef(xrseqs, dataset)
219 .findXrefSourcesForSequences(avp
220 .getAlignViewport().isNucleotide());
222 stringify(dbtoviewBit, savedProjects, nextxref, avp);
223 xrptypes.put(nextxref, _xrptypes);
227 // now do the second xref pass starting from either saved or just
228 // recovered split pane, in sequence
229 do // retrieve second set of cross refs or recover and verify
232 for (AlignmentViewPanel avp : cra_views)
234 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
235 for (String xrefdb : xrptypes.get(nextxref))
237 List<AlignmentViewPanel> cra_views2 = new ArrayList<AlignmentViewPanel>();
239 String nextnextxref = nextxref
240 + " -> " + xrefdb + "{" + q + "}";
245 SequenceI[] xrseqs = avp.getAlignment()
246 .getSequencesArray();
247 AlignFrame nextaf = Desktop.getAlignFrameFor(avp
248 .getAlignViewport());
250 cra = new CrossRefAction(nextaf, xrseqs, avp
251 .getAlignViewport().isNucleotide(), xrefdb);
253 if (cra.getXrefViews().size() == 0)
256 .add("No crossrefs retrieved for '"
257 + nextxref + "' to " + xrefdb + " via '"
258 + nextaf.getTitle() + "'");
261 cra_views2 = cra.getXrefViews();
262 assertNucleotide(cra_views2.get(0),
263 "Nucleotide panel included proteins for '"
264 + nextxref + "' to " + xrefdb
265 + " via '" + nextaf.getTitle() + "'");
266 assertProtein(cra_views2.get(1),
267 "Protein panel included nucleotides for '"
268 + nextxref + "' to " + xrefdb
269 + " via '" + nextaf.getTitle() + "'");
274 Desktop.instance.closeAll_actionPerformed(null);
275 // recover stored project
276 AlignFrame af2 = new FileLoader(false)
277 .LoadFileWaitTillLoaded(
278 savedProjects.get(nextnextxref)
279 .toString(), FormatAdapter.FILE);
280 System.out.println("Recovered view for '"
281 + nextnextxref + "' from '"
282 + savedProjects.get(nextnextxref).toString()
284 // gymnastics to recover the alignPanel/Complementary
286 if (af2.getViewport().isNucleotide())
288 // top view, then bottom
289 cra_views2.add(af2.getViewport().getAlignPanel());
290 cra_views2.add(((jalview.gui.AlignViewport) af2
291 .getViewport().getCodingComplement())
297 // bottom view, then top
298 cra_views2.add(((jalview.gui.AlignViewport) af2
299 .getViewport().getCodingComplement())
301 cra_views2.add(af2.getViewport().getAlignPanel());
303 Assert.assertEquals(cra_views2.size(), 2);
304 Assert.assertNotNull(cra_views2.get(0));
305 Assert.assertNotNull(cra_views2.get(1));
308 for (AlignmentViewPanel nextavp : cra_views2)
310 nextnextxref = nextxref
311 + " -> " + xrefdb + "{" + q++ + "}";
313 // verify references for this panel
314 AlignmentTest.assertAlignmentDatasetRefs(
315 nextavp.getAlignment(), "" + "Pass (" + pass1
316 + "," + pass2 + "): For "
317 + nextnextxref + ":");
319 stringify(dbtoviewBit, savedProjects, nextnextxref,
321 keyseq.add(nextnextxref);
323 } // end of loop around showing all xrefdb for crossrf2
325 } // end of loop around all viewpanels from crossrf1
326 } while (pass2 == 2 && pass3++ < 2);
327 // fetchdb->crossref1->crossref-2->verify for xrefs we
328 // either loop twice when pass2=0, or just once when pass2=1
329 // (recovered project from previous crossref)
331 } // end of loop over db-xrefs for crossref-2
333 // fetchdb-->crossref1
334 // for each xref we try to retrieve xref, store and verify when
335 // pass1=0, or just retrieve and verify when pass1=1
336 } while (pass1 == 1 && pass2++ < 2);
339 // loop twice: first, do the retrieve, second recover from saved project
341 // increment pass counters, so we repeat traversal starting from the
342 // oldest saved project first.
345 // verify stored projects for first set of cross references
347 // and verify cross-references retrieved from stored projects
353 // verify stored projects for second set of cross references
355 // and verify cross-references retrievable from those stored projects.
360 if (failedXrefMenuItems.size() > 0)
362 StringBuilder sb = new StringBuilder();
363 for (String s : failedXrefMenuItems)
367 Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
368 + " counts)\n" + sb.toString());
372 private void assertProtein(AlignmentViewPanel alignmentViewPanel,
375 assertType(true, alignmentViewPanel, message);
378 private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
381 assertType(false, alignmentViewPanel, message);
384 private void assertType(boolean expectProtein,
385 AlignmentViewPanel alignmentViewPanel, String message)
387 List<SequenceI> nonType = new ArrayList<SequenceI>();
388 for (SequenceI sq : alignmentViewPanel.getAlignViewport()
392 if (sq.isProtein() != expectProtein)
397 if (nonType.size() > 0)
399 Assert.fail(message + " [ "
400 + (expectProtein ? "nucleotides were " : "proteins were ")
407 * first time called, record strings derived from alignment and
408 * alignedcodonframes, and save view to a project file. Second time called,
409 * compare strings to existing ones. org.testng.Assert.assertTrue on
413 * map between xrefpath and view string
414 * @param savedProjects
415 * - map from xrefpath to saved project filename (createTempFile)
417 * - xrefpath - unique ID for this context (composed of sequence of
418 * db-fetch/cross-ref actions preceeding state)
420 * - viewpanel to store (for viewpanels in splitframe, the same
421 * project should be written for both panels, only one needs
422 * recovering for comparison on the next stringify call, but each
423 * viewpanel needs to be called with a distinct xrefpath to ensure
424 * each one's strings are compared)
426 private void stringify(HashMap<String, String> dbtoviewBit,
427 HashMap<String, File> savedProjects, String xrefpath,
428 AlignmentViewPanel avp)
430 if (savedProjects != null)
432 if (savedProjects.get(xrefpath) == null)
434 // write a project file for this view. On the second pass, this will be
435 // recovered and cross-references verified
438 File prfile = File.createTempFile("crossRefTest", ".jvp");
439 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
440 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
442 System.out.println("Written view from '" + xrefpath + "' as '"
443 + prfile.getAbsolutePath() + "'");
444 savedProjects.put(xrefpath, prfile);
445 } catch (IOException q)
447 Assert.fail("Unexpected IO Exception", q);
452 System.out.println("Stringify check on view from '" + xrefpath
453 + "' [ possibly retrieved from '"
454 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
459 StringBuilder sbr = new StringBuilder();
460 sbr.append(avp.getAlignment().toString());
462 sbr.append("<End of alignment>");
464 sbr.append(avp.getAlignment().getDataset());
466 sbr.append("<End of dataset>");
469 if (avp.getAlignment().getCodonFrames() != null)
471 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
473 sbr.append("<AlignedCodonFrame " + p++ + ">");
475 sbr.append(ac.toString());
479 String dbt = dbtoviewBit.get(xrefpath);
482 dbtoviewBit.put(xrefpath, sbr.toString());
486 Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "