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 static org.testng.AssertJUnit.assertTrue;
25 import jalview.analysis.CrossRef;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.datamodel.AlignedCodonFrame;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.AlignmentTest;
30 import jalview.datamodel.SequenceI;
31 import jalview.gui.AlignFrame;
32 import jalview.gui.CrossRefAction;
33 import jalview.gui.Desktop;
34 import jalview.gui.Jalview2XML;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.IdentityHashMap;
41 import java.util.List;
43 import org.testng.Assert;
44 import org.testng.annotations.Test;
46 @Test(singleThreaded = true)
47 public class CrossRef2xmlTests extends Jalview2xmlBase
51 * test store and recovery of expanded views
55 @Test(groups = { "Operational" }, enabled = true)
56 public void testRetrieveAndShowCrossref() throws Exception
58 // for every set of db queries
60 // verify presence of expected xrefs
61 // show xrefs - verify expected type of frame is shown for each xref
63 // - verify original -> xref -> xref(original) recovers frame containing at
64 // least the first retrieved sequence
67 // 2. individual frames
68 // 3. load each one back and verify
69 // . aligned sequences (.toString() )
70 // . xrefs (.toString() )
74 HashMap<String, String> dbtoviewBit = new HashMap<String, String>();
75 List<String> keyseq = new ArrayList<String>();
76 HashMap<String, File> savedProjects = new HashMap<String, File>();
78 for (String[] did : new String[][] { { "UNIPROT", "P01731" } })
80 // pass counters - 0 - first pass, 1 means retrieve project rather than
82 int pass1 = 0, pass2 = 0, pass3 = 0;
83 // each do loop performs two iterations in the first outer loop pass, but
84 // only performs one iteration on the second outer loop
85 // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
86 // { pass 2 = 0 { pass 3 = 0 } }
89 String first = did[0] + " " + did[1];
97 af = jalview.gui.SequenceFetcher.fetchAndShow(did[0], did[1])
99 assertTrue("Didn't retrieve " + first, af != null);
101 // verify references for retrieved data
102 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
103 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
104 + pass3 + "): Fetch " + first + ":");
106 // store project to recover on next pass
107 stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
111 Desktop.instance.closeAll_actionPerformed(null);
112 // recover stored project
113 af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
114 .get(first).toString(), FormatAdapter.FILE);
116 // verify references for recovered data
117 AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
118 .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
119 + pass3 + "): Recover " + first + ":");
123 boolean dna = af.getViewport().getAlignment().isNucleotide();
124 AlignmentI retral = af.getViewport().getAlignment();
125 AlignmentI dataset = retral.getDataset();
126 SequenceI[] seqs = retral.getSequencesArray();
127 List<String> ptypes = (seqs == null || seqs.length == 0) ? null
128 : new CrossRef(seqs, dataset)
129 .findXrefSourcesForSequences(dna);
132 * map between a view, and views generated after retrieving xrefs
134 IdentityHashMap<AlignmentViewPanel, List<AlignmentViewPanel>> viewxrefview = new IdentityHashMap<AlignmentViewPanel, List<AlignmentViewPanel>>();
136 * map between a particular view and it's originating dbref path
138 IdentityHashMap<AlignmentViewPanel, String> viewsourcedb = new IdentityHashMap<AlignmentViewPanel, String>();
140 viewsourcedb.put(af.alignPanel, first);
141 for (String db : ptypes)
144 do // second cross ref and recover crossref loop
146 // counter for splitframe views retrieved via crossref
148 // build next key so we an retrieve all views
149 String nextxref = first + " -> " + db + "{" + p + "}";
150 // perform crossref action, or retrieve stored project
151 List<AlignmentViewPanel> cra_views = new ArrayList<AlignmentViewPanel>();
152 CrossRefAction cra = null;
154 { // retrieve and show cross-refs in this thread
155 cra = new CrossRefAction(af, seqs, dna, db);
157 Assert.assertTrue(cra.getXrefViews().size() > 0,
158 "No crossrefs retrieved for " + db);
159 cra_views = cra.getXrefViews();
160 viewxrefview.put(af.alignPanel, cra.getXrefViews());
164 Desktop.instance.closeAll_actionPerformed(null);
165 // recover stored project
166 AlignFrame af2 = new FileLoader(false)
167 .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
168 .toString(), FormatAdapter.FILE);
169 // gymnastics to recover the alignPanel/Complementary alignPanel
170 if (af2.getViewport().isNucleotide())
172 // top view, then bottom
173 cra_views.add(af2.getViewport().getAlignPanel());
174 cra_views.add(((jalview.gui.AlignViewport) af2
175 .getViewport().getCodingComplement())
181 // bottom view, then top
182 cra_views.add(((jalview.gui.AlignViewport) af2
183 .getViewport().getCodingComplement())
185 cra_views.add(af2.getViewport().getAlignPanel());
189 for (AlignmentViewPanel avp : cra_views)
191 nextxref = first + " -> " + db + "{" + p++ + "}";
193 // verify references for this panel
194 AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
195 "" + "Pass (" + pass1 + "," + pass2 + "): For "
198 SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
200 viewsourcedb.put(avp, nextxref);
201 stringify(dbtoviewBit, savedProjects, nextxref, avp);
202 keyseq.add(nextxref);
204 List<String> xrptypes = (seqs == null || seqs.length == 0) ? null
205 : new CrossRef(xrseqs, dataset)
206 .findXrefSourcesForSequences(avp
207 .getAlignViewport().isNucleotide());
208 for (String xrefdb : xrptypes)
211 do // 3rd cross ref and recover crossref loop
213 List<AlignmentViewPanel> cra_views2 = new ArrayList<AlignmentViewPanel>();
215 String nextnextxref = "{" + p + "}" + nextxref + " -> "
216 + xrefdb + "{" + q + "}";
218 AlignFrame nextaf = Desktop.getAlignFrameFor(avp
219 .getAlignViewport());
223 cra = new CrossRefAction(nextaf, xrseqs, avp
224 .getAlignViewport().isNucleotide(), xrefdb);
226 Assert.assertTrue(cra.getXrefViews().size() > 0,
227 "No crossrefs found for '" + nextxref + "' to "
228 + xrefdb + " via '" + nextaf.getTitle()
230 cra_views2 = cra.getXrefViews();
234 Desktop.instance.closeAll_actionPerformed(null);
235 // recover stored project
236 AlignFrame af2 = new FileLoader(false)
237 .LoadFileWaitTillLoaded(
238 savedProjects.get(nextnextxref)
239 .toString(), FormatAdapter.FILE);
240 // gymnastics to recover the alignPanel/Complementary
242 if (af2.getViewport().isNucleotide())
244 // top view, then bottom
245 cra_views2.add(af2.getViewport().getAlignPanel());
246 cra_views2.add(((jalview.gui.AlignViewport) af2
247 .getViewport().getCodingComplement())
253 // bottom view, then top
254 cra_views2.add(((jalview.gui.AlignViewport) af2
255 .getViewport().getCodingComplement())
257 cra_views2.add(af2.getViewport().getAlignPanel());
259 Assert.assertEquals(cra_views2.size(), 2);
260 Assert.assertNotNull(cra_views2.get(0));
261 Assert.assertNotNull(cra_views2.get(1));
264 for (AlignmentViewPanel nextavp : cra_views2)
266 nextnextxref = "{" + p + "}" + nextxref + " -> "
267 + xrefdb + "{" + q++ + "}";
269 // verify references for this panel
270 AlignmentTest.assertAlignmentDatasetRefs(
271 nextavp.getAlignment(), "" + "Pass (" + pass1
272 + "," + pass2 + "): For "
273 + nextnextxref + ":");
275 viewsourcedb.put(nextavp, nextnextxref);
276 stringify(dbtoviewBit, savedProjects, nextnextxref,
278 keyseq.add(nextnextxref);
280 } while (pass3++ < 2 && pass2 < 1);
283 } while (pass2++ < 2 && pass1 < 1);
285 } while (++pass1 < 2);
290 * first time called, record strings derived from alignment and
291 * alignedcodonframes, and save view to a project file. Second time called,
292 * compare strings to existing ones. org.testng.Assert.assertTrue on
296 * map between xrefpath and view string
297 * @param savedProjects
298 * - map from xrefpath to saved project filename (createTempFile)
300 * - xrefpath - unique ID for this context (composed of sequence of
301 * db-fetch/cross-ref actions preceeding state)
303 * - viewpanel to store (for viewpanels in splitframe, the same
304 * project should be written for both panels, only one needs
305 * recovering for comparison on the next stringify call, but each
306 * viewpanel needs to be called with a distinct xrefpath to ensure
307 * each one's strings are compared)
309 private void stringify(HashMap<String, String> dbtoviewBit,
310 HashMap<String, File> savedProjects, String xrefpath,
311 AlignmentViewPanel avp)
313 if (savedProjects != null)
315 if (savedProjects.get(xrefpath) == null)
317 // write a project file for this view. On the second pass, this will be
318 // recovered and cross-references verified
321 File prfile = File.createTempFile("crossRefTest", ".jvp");
322 AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
323 new Jalview2XML(false).saveAlignment(af, prfile.toString(),
325 System.out.println("Written view from '" + xrefpath + "' as '"
326 + prfile.getAbsolutePath() + "'");
327 savedProjects.put(xrefpath, prfile);
328 } catch (IOException q)
330 Assert.fail("Unexpected IO Exception", q);
335 System.out.println("Stringify check on view from '" + xrefpath
336 + "' [ possibly retrieved from '"
337 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
342 StringBuilder sbr = new StringBuilder();
343 sbr.append(avp.getAlignment().toString());
345 sbr.append("<End of alignment>");
347 sbr.append(avp.getAlignment().getDataset());
349 sbr.append("<End of dataset>");
352 if (avp.getAlignment().getCodonFrames() != null)
354 for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
356 sbr.append("<AlignedCodonFrame " + p++ + ">");
358 sbr.append(ac.toString());
362 String dbt = dbtoviewBit.get(xrefpath);
365 dbtoviewBit.put(xrefpath, sbr.toString());
369 Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "