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 // for every set of db queries
57 // verify presence of expected xrefs
58 // show xrefs - verify expected type of frame is shown for each xref
60 // - verify original -> xref -> xref(original) recovers frame containing at
61 // least the first retrieved sequence
64 // 2. individual frames
65 // 3. load each one back and verify
66 // . aligned sequences (.toString() )
67 // . xrefs (.toString() )
71 HashMap<String, String> dbtoviewBit = new HashMap<String, String>();
72 List<String> keyseq = new ArrayList<String>();
73 HashMap<String, File> savedProjects = new HashMap<String, File>();
75 for (String[] did : new String[][] { { "UNIPROT", "P01731" } })
77 // pass counters - 0 - first pass, 1 means retrieve project rather than
79 int pass1 = 0, pass2 = 0, pass3 = 0;
80 // each do loop performs two iterations in the first outer loop pass, but
81 // only performs one iteration on the second outer loop
82 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
83 // { pass 2 = 0 { pass 3 = 0 } }
86 String first = did[0] + " " + did[1];
92 List<String> ptypes = null;
99 af = jalview.gui.SequenceFetcher.fetchAndShow(did[0], did[1])
101 Assert.assertTrue(af != null, "Didn't retrieve " + first);
103 // verify references for retrieved data
104 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
105 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
106 + pass3 + "): Fetch " + first + ":");
107 dna = af.getViewport().getAlignment().isNucleotide();
108 retral = af.getViewport().getAlignment();
109 dataset = retral.getDataset();
110 seqs = retral.getSequencesArray();
115 Desktop.instance.closeAll_actionPerformed(null);
116 // recover stored project
117 af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
118 .get(first).toString(), FormatAdapter.FILE);
119 System.out.println("Recovered view for '" + first + "' from '"
120 + savedProjects.get(first).toString() + "'");
121 dna = af.getViewport().getAlignment().isNucleotide();
122 retral = af.getViewport().getAlignment();
123 dataset = retral.getDataset();
124 seqs = retral.getSequencesArray();
126 // verify references for recovered data
127 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
128 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
129 + pass3 + "): Recover " + first + ":");
133 // store project on first pass, compare next pass
134 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
136 ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
137 seqs, dataset).findXrefSourcesForSequences(dna);
139 // start of pass2: retrieve each cross-ref for fetched or restored
141 do // first cross ref and recover crossref loop
144 for (String db : ptypes)
146 // counter for splitframe views retrieved via crossref
148 // build next key so we an retrieve all views
149 String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
150 // perform crossref action, or retrieve stored project
151 List<AlignmentViewPanel> cra_views = new ArrayList<AlignmentViewPanel>();
152 CrossRefAction cra = null;
155 { // retrieve and show cross-refs in this thread
156 cra = new CrossRefAction(af, seqs, dna, db);
158 Assert.assertTrue(cra.getXrefViews().size() > 0,
159 "No crossrefs retrieved for " + db);
160 cra_views = cra.getXrefViews();
161 assertNucleotide(cra_views.get(0),
162 "Nucleotide panel included proteins for " + first
164 assertProtein(cra_views.get(1),
165 "Protein panel included nucleotides for " + first
170 Desktop.instance.closeAll_actionPerformed(null);
172 // recover stored project
173 AlignFrame af2 = new FileLoader(false)
174 .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
175 .toString(), FormatAdapter.FILE);
176 System.out.println("Recovered view for '" + nextxref
177 + "' from '" + savedProjects.get(nextxref).toString()
179 // gymnastics to recover the alignPanel/Complementary alignPanel
180 if (af2.getViewport().isNucleotide())
182 // top view, then bottom
183 cra_views.add(af2.getViewport().getAlignPanel());
184 cra_views.add(((jalview.gui.AlignViewport) af2
185 .getViewport().getCodingComplement())
191 // bottom view, then top
192 cra_views.add(((jalview.gui.AlignViewport) af2
193 .getViewport().getCodingComplement())
195 cra_views.add(af2.getViewport().getAlignPanel());
199 HashMap<String, List<String>> xrptypes = new HashMap<String, List<String>>();
200 // first save/verify views.
201 for (AlignmentViewPanel avp : cra_views)
203 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
204 // verify references for this panel
205 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
206 "Pass (" + pass1 + "," + pass2 + "," + pass3
207 + "): before start of pass3: " + nextxref
210 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
212 List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
213 : new CrossRef(xrseqs, dataset)
214 .findXrefSourcesForSequences(avp
215 .getAlignViewport().isNucleotide());
217 stringify(dbtoviewBit, savedProjects, nextxref, avp);
218 xrptypes.put(nextxref, _xrptypes);
222 // now do the second xref pass starting from either saved or just
223 // recovered split pane, in sequence
224 do // retrieve second set of cross refs or recover and verify
227 for (AlignmentViewPanel avp : cra_views)
229 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
230 for (String xrefdb : xrptypes.get(nextxref))
232 List<AlignmentViewPanel> cra_views2 = new ArrayList<AlignmentViewPanel>();
234 String nextnextxref = nextxref
235 + " -> " + xrefdb + "{" + q + "}";
240 SequenceI[] xrseqs = avp.getAlignment()
241 .getSequencesArray();
242 AlignFrame nextaf = Desktop.getAlignFrameFor(avp
243 .getAlignViewport());
245 cra = new CrossRefAction(nextaf, xrseqs, avp
246 .getAlignViewport().isNucleotide(), xrefdb);
249 cra.getXrefViews().size() > 0,
250 "No crossrefs found for '" + nextnextxref
251 + "' to " + xrefdb + " via '"
252 + nextaf.getTitle() + "'");
253 cra_views2 = cra.getXrefViews();
254 assertNucleotide(cra_views2.get(0),
255 "Nucleotide panel included proteins for '"
256 + nextxref + "' to " + xrefdb
257 + " via '" + nextaf.getTitle() + "'");
258 assertProtein(cra_views2.get(1),
259 "Protein panel included nucleotides for '"
260 + nextxref + "' to " + xrefdb
261 + " via '" + nextaf.getTitle() + "'");
266 Desktop.instance.closeAll_actionPerformed(null);
267 // recover stored project
268 AlignFrame af2 = new FileLoader(false)
269 .LoadFileWaitTillLoaded(
270 savedProjects.get(nextnextxref)
271 .toString(), FormatAdapter.FILE);
272 System.out.println("Recovered view for '"
273 + nextnextxref + "' from '"
274 + savedProjects.get(nextnextxref).toString()
276 // gymnastics to recover the alignPanel/Complementary
278 if (af2.getViewport().isNucleotide())
280 // top view, then bottom
281 cra_views2.add(af2.getViewport().getAlignPanel());
282 cra_views2.add(((jalview.gui.AlignViewport) af2
283 .getViewport().getCodingComplement())
289 // bottom view, then top
290 cra_views2.add(((jalview.gui.AlignViewport) af2
291 .getViewport().getCodingComplement())
293 cra_views2.add(af2.getViewport().getAlignPanel());
295 Assert.assertEquals(cra_views2.size(), 2);
296 Assert.assertNotNull(cra_views2.get(0));
297 Assert.assertNotNull(cra_views2.get(1));
300 for (AlignmentViewPanel nextavp : cra_views2)
302 nextnextxref = nextxref
303 + " -> " + xrefdb + "{" + q++ + "}";
305 // verify references for this panel
306 AlignmentTest.assertAlignmentDatasetRefs(
307 nextavp.getAlignment(), "" + "Pass (" + pass1
308 + "," + pass2 + "): For "
309 + nextnextxref + ":");
311 stringify(dbtoviewBit, savedProjects, nextnextxref,
313 keyseq.add(nextnextxref);
315 } // end of loop around showing all xrefdb for crossrf2
317 } // end of loop around all viewpanels from crossrf1
318 } while (pass2 == 2 && pass3++ < 2);
319 // fetchdb->crossref1->crossref-2->verify for xrefs we
320 // either loop twice when pass2=0, or just once when pass2=1
321 // (recovered project from previous crossref)
323 } // end of loop over db-xrefs for crossref-2
325 // fetchdb-->crossref1
326 // for each xref we try to retrieve xref, store and verify when
327 // pass1=0, or just retrieve and verify when pass1=1
328 } while (pass1 == 1 && pass2++ < 2);
331 // loop twice: first, do the retrieve, second recover from saved project
333 // increment pass counters, so we repeat traversal starting from the
334 // oldest saved project first.
337 // verify stored projects for first set of cross references
339 // and verify cross-references retrieved from stored projects
345 // verify stored projects for second set of cross references
347 // and verify cross-references retrievable from those stored projects.
354 private void assertProtein(AlignmentViewPanel alignmentViewPanel,
357 assertType(true, alignmentViewPanel, message);
360 private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
363 assertType(false, alignmentViewPanel, message);
366 private void assertType(boolean expectProtein,
367 AlignmentViewPanel alignmentViewPanel, String message)
369 List<SequenceI> nonType = new ArrayList<SequenceI>();
370 for (SequenceI sq : alignmentViewPanel.getAlignViewport()
374 if (sq.isProtein() != expectProtein)
379 if (nonType.size() > 0)
381 Assert.fail(message + " [ "
382 + (expectProtein ? "nucleotides were " : "proteins were ")
389 * first time called, record strings derived from alignment and
390 * alignedcodonframes, and save view to a project file. Second time called,
391 * compare strings to existing ones. org.testng.Assert.assertTrue on
395 * map between xrefpath and view string
396 * @param savedProjects
397 * - map from xrefpath to saved project filename (createTempFile)
399 * - xrefpath - unique ID for this context (composed of sequence of
400 * db-fetch/cross-ref actions preceeding state)
402 * - viewpanel to store (for viewpanels in splitframe, the same
403 * project should be written for both panels, only one needs
404 * recovering for comparison on the next stringify call, but each
405 * viewpanel needs to be called with a distinct xrefpath to ensure
406 * each one's strings are compared)
408 private void stringify(HashMap<String, String> dbtoviewBit,
409 HashMap<String, File> savedProjects, String xrefpath,
410 AlignmentViewPanel avp)
412 if (savedProjects != null)
414 if (savedProjects.get(xrefpath) == null)
416 // write a project file for this view. On the second pass, this will be
417 // recovered and cross-references verified
420 File prfile = File.createTempFile("crossRefTest", ".jvp");
421 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
422 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
424 System.out.println("Written view from '" + xrefpath + "' as '"
425 + prfile.getAbsolutePath() + "'");
426 savedProjects.put(xrefpath, prfile);
427 } catch (IOException q)
429 Assert.fail("Unexpected IO Exception", q);
434 System.out.println("Stringify check on view from '" + xrefpath
435 + "' [ possibly retrieved from '"
436 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
441 StringBuilder sbr = new StringBuilder();
442 sbr.append(avp.getAlignment().toString());
444 sbr.append("<End of alignment>");
446 sbr.append(avp.getAlignment().getDataset());
448 sbr.append("<End of dataset>");
451 if (avp.getAlignment().getCodonFrames() != null)
453 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
455 sbr.append("<AlignedCodonFrame " + p++ + ">");
457 sbr.append(ac.toString());
461 String dbt = dbtoviewBit.get(xrefpath);
464 dbtoviewBit.put(xrefpath, sbr.toString());
468 Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "