24074d0b371779579967e34aee075ad70a09435e
[jalview.git] / test / jalview / io / CrossRef2xmlTests.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io;
22
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;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
42
43 @Test(singleThreaded = true)
44 public class CrossRef2xmlTests extends Jalview2xmlBase
45 {
46
47   /**
48    * test store and recovery of expanded views
49    * 
50    * @throws Exception
51    */
52   @Test(groups = { "Operational" }, enabled = true)
53   public void testRetrieveAndShowCrossref() throws Exception
54   {
55     // for every set of db queries
56     // retrieve db query
57     // verify presence of expected xrefs
58     // show xrefs - verify expected type of frame is shown for each xref
59     // show xrefs again
60     // - verify original -> xref -> xref(original) recovers frame containing at
61     // least the first retrieved sequence
62     // store
63     // 1. whole project
64     // 2. individual frames
65     // 3. load each one back and verify
66     // . aligned sequences (.toString() )
67     // . xrefs (.toString() )
68     // . codonframes
69     //
70     //
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>();
74
75     for (String[] did : new String[][] { { "UNIPROT", "P01731" } })
76     {
77       // pass counters - 0 - first pass, 1 means retrieve project rather than
78       // perform action
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 } }
84       do
85       {
86         String first = did[0] + " " + did[1];
87         AlignFrame af = null;
88         boolean dna;
89         AlignmentI retral;
90         AlignmentI dataset;
91         SequenceI[] seqs;
92         List<String> ptypes = null;
93         if (pass1 == 0)
94         {
95           // retrieve dbref
96
97           keyseq.add(first);
98
99           af = jalview.gui.SequenceFetcher.fetchAndShow(did[0], did[1])
100                   .get(0);
101           Assert.assertTrue(af != null, "Didn't retrieve " + first);
102
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();
111
112         }
113         else
114         {
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();
125
126           // verify references for recovered data
127           AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
128                   .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
129                   + pass3 + "): Recover " + first + ":");
130
131         }
132
133         // store project on first pass, compare next pass
134         stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
135
136         ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
137                 seqs, dataset).findXrefSourcesForSequences(dna);
138
139         // start of pass2: retrieve each cross-ref for fetched or restored
140         // project.
141         do // first cross ref and recover crossref loop
142         {
143
144           for (String db : ptypes)
145           {
146             // counter for splitframe views retrieved via crossref
147             int firstcr_ap = 0;
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;
153             
154             if (pass2 == 0)
155             { // retrieve and show cross-refs in this thread
156               cra = new CrossRefAction(af, seqs, dna, db);
157               cra.run();
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
163                               + " -> " + db);
164               assertProtein(cra_views.get(1),
165                       "Protein panel included nucleotides for " + first
166                               + " -> " + db);
167             }
168             else
169             {
170               Desktop.instance.closeAll_actionPerformed(null);
171               pass3 = 0;
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()
178                       + "'");
179               // gymnastics to recover the alignPanel/Complementary alignPanel
180               if (af2.getViewport().isNucleotide())
181               {
182                 // top view, then bottom
183                 cra_views.add(af2.getViewport().getAlignPanel());
184                 cra_views.add(((jalview.gui.AlignViewport) af2
185                         .getViewport().getCodingComplement())
186                         .getAlignPanel());
187
188               }
189               else
190               {
191                 // bottom view, then top
192                 cra_views.add(((jalview.gui.AlignViewport) af2
193                         .getViewport().getCodingComplement())
194                         .getAlignPanel());
195                 cra_views.add(af2.getViewport().getAlignPanel());
196
197               }
198             }
199             HashMap<String, List<String>> xrptypes = new HashMap<String, List<String>>();
200             // first save/verify views.
201             for (AlignmentViewPanel avp : cra_views)
202             {
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
208                               + ":");
209
210               SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
211
212               List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
213                       : new CrossRef(xrseqs, dataset)
214                               .findXrefSourcesForSequences(avp
215                                       .getAlignViewport().isNucleotide());
216               
217               stringify(dbtoviewBit, savedProjects, nextxref, avp);
218               xrptypes.put(nextxref, _xrptypes);
219
220             }
221
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
225             {
226               firstcr_ap = 0;
227               for (AlignmentViewPanel avp : cra_views)
228               {
229                 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
230                 for (String xrefdb : xrptypes.get(nextxref))
231                 {
232                   List<AlignmentViewPanel> cra_views2 = new ArrayList<AlignmentViewPanel>();
233                   int q = 0;
234                   String nextnextxref = nextxref
235                           + " -> " + xrefdb + "{" + q + "}";
236
237                   if (pass3 == 0)
238                   {
239
240                     SequenceI[] xrseqs = avp.getAlignment()
241                             .getSequencesArray();
242                     AlignFrame nextaf = Desktop.getAlignFrameFor(avp
243                             .getAlignViewport());
244
245                     cra = new CrossRefAction(nextaf, xrseqs, avp
246                             .getAlignViewport().isNucleotide(), xrefdb);
247                     cra.run();
248                     Assert.assertTrue(
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() + "'");
262
263                   }
264                   else
265                   {
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()
275                             + "'");
276                     // gymnastics to recover the alignPanel/Complementary
277                     // alignPanel
278                     if (af2.getViewport().isNucleotide())
279                     {
280                       // top view, then bottom
281                       cra_views2.add(af2.getViewport().getAlignPanel());
282                       cra_views2.add(((jalview.gui.AlignViewport) af2
283                               .getViewport().getCodingComplement())
284                               .getAlignPanel());
285
286                     }
287                     else
288                     {
289                       // bottom view, then top
290                       cra_views2.add(((jalview.gui.AlignViewport) af2
291                               .getViewport().getCodingComplement())
292                               .getAlignPanel());
293                       cra_views2.add(af2.getViewport().getAlignPanel());
294                     }
295                     Assert.assertEquals(cra_views2.size(), 2);
296                     Assert.assertNotNull(cra_views2.get(0));
297                     Assert.assertNotNull(cra_views2.get(1));
298                   }
299
300                   for (AlignmentViewPanel nextavp : cra_views2)
301                   {
302                     nextnextxref = nextxref
303                             + " -> " + xrefdb + "{" + q++ + "}";
304
305                     // verify references for this panel
306                     AlignmentTest.assertAlignmentDatasetRefs(
307                             nextavp.getAlignment(), "" + "Pass (" + pass1
308                                     + "," + pass2 + "): For "
309                                     + nextnextxref + ":");
310
311                     stringify(dbtoviewBit, savedProjects, nextnextxref,
312                             nextavp);
313                     keyseq.add(nextnextxref);
314                   }
315                 } // end of loop around showing all xrefdb for crossrf2
316
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)
322
323           } // end of loop over db-xrefs for crossref-2
324
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);
329         // fetchdb
330         // for each ref we
331         // loop twice: first, do the retrieve, second recover from saved project
332
333         // increment pass counters, so we repeat traversal starting from the
334         // oldest saved project first.
335         if (pass1 == 0)
336         {
337           // verify stored projects for first set of cross references
338           pass1 = 1;
339           // and verify cross-references retrieved from stored projects
340           pass2 = 0;
341           pass3 = 0;
342         }
343         else
344         {
345           // verify stored projects for second set of cross references
346           pass2 = 1;
347           // and verify cross-references retrievable from those stored projects.
348           pass3 = 0;
349         }
350       } while (pass3 < 2);
351     }
352   }
353
354   private void assertProtein(AlignmentViewPanel alignmentViewPanel,
355           String message)
356   {
357     assertType(true, alignmentViewPanel, message);
358   }
359
360   private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
361           String message)
362   {
363     assertType(false, alignmentViewPanel, message);
364   }
365
366   private void assertType(boolean expectProtein,
367           AlignmentViewPanel alignmentViewPanel, String message)
368   {
369     List<SequenceI> nonType = new ArrayList<SequenceI>();
370     for (SequenceI sq : alignmentViewPanel.getAlignViewport()
371             .getAlignment()
372             .getSequences())
373     {
374       if (sq.isProtein() != expectProtein)
375       {
376         nonType.add(sq);
377       }
378     }
379     if (nonType.size() > 0)
380     {
381       Assert.fail(message + " [ "
382               + (expectProtein ? "nucleotides were " : "proteins were ")
383               + nonType.toString()
384               + " ]");
385     }
386   }
387
388   /**
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
392    * stringmatch
393    * 
394    * @param dbtoviewBit
395    *          map between xrefpath and view string
396    * @param savedProjects
397    *          - map from xrefpath to saved project filename (createTempFile)
398    * @param xrefpath
399    *          - xrefpath - unique ID for this context (composed of sequence of
400    *          db-fetch/cross-ref actions preceeding state)
401    * @param avp
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)
407    */
408   private void stringify(HashMap<String, String> dbtoviewBit,
409           HashMap<String, File> savedProjects, String xrefpath,
410           AlignmentViewPanel avp)
411   {
412     if (savedProjects != null)
413     {
414       if (savedProjects.get(xrefpath) == null)
415       {
416         // write a project file for this view. On the second pass, this will be
417         // recovered and cross-references verified
418         try
419         {
420           File prfile = File.createTempFile("crossRefTest", ".jvp");
421           AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
422           new Jalview2XML(false).saveAlignment(af, prfile.toString(),
423                   af.getTitle());
424           System.out.println("Written view from '" + xrefpath + "' as '"
425                   + prfile.getAbsolutePath() + "'");
426           savedProjects.put(xrefpath, prfile);
427         } catch (IOException q)
428         {
429           Assert.fail("Unexpected IO Exception", q);
430         }
431       }
432       else
433       {
434         System.out.println("Stringify check on view from '" + xrefpath
435                 + "' [ possibly retrieved from '"
436                 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
437
438       }
439     }
440
441     StringBuilder sbr = new StringBuilder();
442     sbr.append(avp.getAlignment().toString());
443     sbr.append("\n");
444     sbr.append("<End of alignment>");
445     sbr.append("\n");
446     sbr.append(avp.getAlignment().getDataset());
447     sbr.append("\n");
448     sbr.append("<End of dataset>");
449     sbr.append("\n");
450     int p = 0;
451     if (avp.getAlignment().getCodonFrames() != null)
452     {
453       for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
454       {
455         sbr.append("<AlignedCodonFrame " + p++ + ">");
456         sbr.append("\n");
457         sbr.append(ac.toString());
458         sbr.append("\n");
459       }
460     }
461     String dbt = dbtoviewBit.get(xrefpath);
462     if (dbt == null)
463     {
464       dbtoviewBit.put(xrefpath, sbr.toString());
465     }
466     else
467     {
468       Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "
469               + xrefpath);
470     }
471   }
472 }