JAL-2154 catch and report a failures when xrefed project didn’t seem to have associat...
[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     List<String> failedXrefMenuItems = new ArrayList<String>();
56     List<String> failedProjectRecoveries = new ArrayList<String>();
57
58     // for every set of db queries
59     // retrieve db query
60     // verify presence of expected xrefs
61     // show xrefs - verify expected type of frame is shown for each xref
62     // show xrefs again
63     // - verify original -> xref -> xref(original) recovers frame containing at
64     // least the first retrieved sequence
65     // store
66     // 1. whole project
67     // 2. individual frames
68     // 3. load each one back and verify
69     // . aligned sequences (.toString() )
70     // . xrefs (.toString() )
71     // . codonframes
72     //
73     //
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>();
77
78     for (String[] did : new String[][] { { "UNIPROT", "P01731" } })
79     {
80       // pass counters - 0 - first pass, 1 means retrieve project rather than
81       // perform action
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 } }
87       do
88       {
89         String first = did[0] + " " + did[1];
90         AlignFrame af = null;
91         boolean dna;
92         AlignmentI retral;
93         AlignmentI dataset;
94         SequenceI[] seqs;
95         List<String> ptypes = null;
96         if (pass1 == 0)
97         {
98           // retrieve dbref
99
100           keyseq.add(first);
101
102           af = jalview.gui.SequenceFetcher.fetchAndShow(did[0], did[1])
103                   .get(0);
104           Assert.assertTrue(af != null, "Didn't retrieve " + first);
105
106           // verify references for retrieved data
107           AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
108                   .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
109                   + pass3 + "): Fetch " + first + ":");
110           dna = af.getViewport().getAlignment().isNucleotide();
111           retral = af.getViewport().getAlignment();
112           dataset = retral.getDataset();
113           seqs = retral.getSequencesArray();
114
115         }
116         else
117         {
118           Desktop.instance.closeAll_actionPerformed(null);
119           // recover stored project
120           af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
121                   .get(first).toString(), FormatAdapter.FILE);
122           System.out.println("Recovered view for '" + first + "' from '"
123                   + savedProjects.get(first).toString() + "'");
124           dna = af.getViewport().getAlignment().isNucleotide();
125           retral = af.getViewport().getAlignment();
126           dataset = retral.getDataset();
127           seqs = retral.getSequencesArray();
128
129           // verify references for recovered data
130           AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
131                   .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
132                   + pass3 + "): Recover " + first + ":");
133
134         }
135
136         // store project on first pass, compare next pass
137         stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
138
139         ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
140                 seqs, dataset).findXrefSourcesForSequences(dna);
141
142         // start of pass2: retrieve each cross-ref for fetched or restored
143         // project.
144         do // first cross ref and recover crossref loop
145         {
146
147           for (String db : ptypes)
148           {
149             // counter for splitframe views retrieved via crossref
150             int firstcr_ap = 0;
151             // build next key so we an retrieve all views
152             String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
153             // perform crossref action, or retrieve stored project
154             List<AlignmentViewPanel> cra_views = new ArrayList<AlignmentViewPanel>();
155             CrossRefAction cra = null;
156             
157             if (pass2 == 0)
158             { // retrieve and show cross-refs in this thread
159               cra = new CrossRefAction(af, seqs, dna, db);
160               cra.run();
161               if (cra.getXrefViews().size() == 0)
162               {
163                 failedXrefMenuItems.add("No crossrefs retrieved for "
164                         + first + " -> " + db);
165                 continue;
166               }
167               cra_views = cra.getXrefViews();
168               assertNucleotide(cra_views.get(0),
169                       "Nucleotide panel included proteins for " + first
170                               + " -> " + db);
171               assertProtein(cra_views.get(1),
172                       "Protein panel included nucleotides for " + first
173                               + " -> " + db);
174             }
175             else
176             {
177               Desktop.instance.closeAll_actionPerformed(null);
178               pass3 = 0;
179               // recover stored project
180               File storedProject = savedProjects.get(nextxref);
181               if (storedProject == null)
182               {
183                 failedProjectRecoveries.add("Failed to store a view for '"
184                         + nextxref + "'");
185                 continue;
186               }
187
188               // recover stored project
189               AlignFrame af2 = new FileLoader(false)
190                       .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
191                               .toString(), FormatAdapter.FILE);
192               System.out.println("Recovered view for '" + nextxref
193                       + "' from '" + savedProjects.get(nextxref).toString()
194                       + "'");
195               // gymnastics to recover the alignPanel/Complementary alignPanel
196               if (af2.getViewport().isNucleotide())
197               {
198                 // top view, then bottom
199                 cra_views.add(af2.getViewport().getAlignPanel());
200                 cra_views.add(((jalview.gui.AlignViewport) af2
201                         .getViewport().getCodingComplement())
202                         .getAlignPanel());
203
204               }
205               else
206               {
207                 // bottom view, then top
208                 cra_views.add(((jalview.gui.AlignViewport) af2
209                         .getViewport().getCodingComplement())
210                         .getAlignPanel());
211                 cra_views.add(af2.getViewport().getAlignPanel());
212
213               }
214             }
215             HashMap<String, List<String>> xrptypes = new HashMap<String, List<String>>();
216             // first save/verify views.
217             for (AlignmentViewPanel avp : cra_views)
218             {
219               nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
220               // verify references for this panel
221               AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
222                       "Pass (" + pass1 + "," + pass2 + "," + pass3
223                               + "): before start of pass3: " + nextxref
224                               + ":");
225
226               SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
227
228               List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
229                       : new CrossRef(xrseqs, dataset)
230                               .findXrefSourcesForSequences(avp
231                                       .getAlignViewport().isNucleotide());
232               
233               stringify(dbtoviewBit, savedProjects, nextxref, avp);
234               xrptypes.put(nextxref, _xrptypes);
235
236             }
237
238             // now do the second xref pass starting from either saved or just
239             // recovered split pane, in sequence
240             do // retrieve second set of cross refs or recover and verify
241             {
242               firstcr_ap = 0;
243               for (AlignmentViewPanel avp : cra_views)
244               {
245                 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
246                 for (String xrefdb : xrptypes.get(nextxref))
247                 {
248                   List<AlignmentViewPanel> cra_views2 = new ArrayList<AlignmentViewPanel>();
249                   int q = 0;
250                   String nextnextxref = nextxref
251                           + " -> " + xrefdb + "{" + q + "}";
252
253                   if (pass3 == 0)
254                   {
255
256                     SequenceI[] xrseqs = avp.getAlignment()
257                             .getSequencesArray();
258                     AlignFrame nextaf = Desktop.getAlignFrameFor(avp
259                             .getAlignViewport());
260
261                     cra = new CrossRefAction(nextaf, xrseqs, avp
262                             .getAlignViewport().isNucleotide(), xrefdb);
263                     cra.run();
264                     if (cra.getXrefViews().size() == 0)
265                     {
266                       failedXrefMenuItems
267                               .add("No crossrefs retrieved for '"
268                               + nextxref + "' to " + xrefdb + " via '"
269                               + nextaf.getTitle() + "'");
270                       continue;
271                     }
272                     cra_views2 = cra.getXrefViews();
273                     assertNucleotide(cra_views2.get(0),
274                             "Nucleotide panel included proteins for '"
275                                     + nextxref + "' to " + xrefdb
276                                     + " via '" + nextaf.getTitle() + "'");
277                     assertProtein(cra_views2.get(1),
278                             "Protein panel included nucleotides for '"
279                                     + nextxref + "' to " + xrefdb
280                                     + " via '" + nextaf.getTitle() + "'");
281
282                   }
283                   else
284                   {
285                     Desktop.instance.closeAll_actionPerformed(null);
286                     // recover stored project
287                     File storedProject = savedProjects.get(nextnextxref);
288                     if (storedProject == null)
289                     {
290                       failedProjectRecoveries
291                               .add("Failed to store a view for '"
292                                       + nextnextxref + "'");
293                       continue;
294                     }
295                     AlignFrame af2 = new FileLoader(false)
296                             .LoadFileWaitTillLoaded(
297                                     savedProjects.get(nextnextxref)
298                                             .toString(), FormatAdapter.FILE);
299                     System.out.println("Recovered view for '"
300                             + nextnextxref + "' from '"
301                             + savedProjects.get(nextnextxref).toString()
302                             + "'");
303                     // gymnastics to recover the alignPanel/Complementary
304                     // alignPanel
305                     if (af2.getViewport().isNucleotide())
306                     {
307                       // top view, then bottom
308                       cra_views2.add(af2.getViewport().getAlignPanel());
309                       cra_views2.add(((jalview.gui.AlignViewport) af2
310                               .getViewport().getCodingComplement())
311                               .getAlignPanel());
312
313                     }
314                     else
315                     {
316                       // bottom view, then top
317                       cra_views2.add(((jalview.gui.AlignViewport) af2
318                               .getViewport().getCodingComplement())
319                               .getAlignPanel());
320                       cra_views2.add(af2.getViewport().getAlignPanel());
321                     }
322                     Assert.assertEquals(cra_views2.size(), 2);
323                     Assert.assertNotNull(cra_views2.get(0));
324                     Assert.assertNotNull(cra_views2.get(1));
325                   }
326
327                   for (AlignmentViewPanel nextavp : cra_views2)
328                   {
329                     nextnextxref = nextxref
330                             + " -> " + xrefdb + "{" + q++ + "}";
331
332                     // verify references for this panel
333                     AlignmentTest.assertAlignmentDatasetRefs(
334                             nextavp.getAlignment(), "" + "Pass (" + pass1
335                                     + "," + pass2 + "): For "
336                                     + nextnextxref + ":");
337
338                     stringify(dbtoviewBit, savedProjects, nextnextxref,
339                             nextavp);
340                     keyseq.add(nextnextxref);
341                   }
342                 } // end of loop around showing all xrefdb for crossrf2
343
344               } // end of loop around all viewpanels from crossrf1
345             } while (pass2 == 2 && pass3++ < 2);
346             // fetchdb->crossref1->crossref-2->verify for xrefs we
347             // either loop twice when pass2=0, or just once when pass2=1
348             // (recovered project from previous crossref)
349
350           } // end of loop over db-xrefs for crossref-2
351
352           // fetchdb-->crossref1
353           // for each xref we try to retrieve xref, store and verify when
354           // pass1=0, or just retrieve and verify when pass1=1
355         } while (pass1 == 1 && pass2++ < 2);
356         // fetchdb
357         // for each ref we
358         // loop twice: first, do the retrieve, second recover from saved project
359
360         // increment pass counters, so we repeat traversal starting from the
361         // oldest saved project first.
362         if (pass1 == 0)
363         {
364           // verify stored projects for first set of cross references
365           pass1 = 1;
366           // and verify cross-references retrieved from stored projects
367           pass2 = 0;
368           pass3 = 0;
369         }
370         else
371         {
372           // verify stored projects for second set of cross references
373           pass2 = 1;
374           // and verify cross-references retrievable from those stored projects.
375           pass3 = 0;
376         }
377       } while (pass3 < 2);
378     }
379     if (failedXrefMenuItems.size() > 0)
380     {
381       for (String s : failedXrefMenuItems)
382       {
383         System.err.println(s);
384       }
385       Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
386               + " counts)");
387     }
388     if (failedProjectRecoveries.size() > 0)
389     {
390
391       for (String s : failedProjectRecoveries)
392       {
393         System.err.println(s);
394       }
395       Assert.fail("Didn't recover projects for some retrievals (did they retrieve ?) ("
396               + failedProjectRecoveries.size() + " counts)");
397     }
398   }
399
400   private void assertProtein(AlignmentViewPanel alignmentViewPanel,
401           String message)
402   {
403     assertType(true, alignmentViewPanel, message);
404   }
405
406   private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
407           String message)
408   {
409     assertType(false, alignmentViewPanel, message);
410   }
411
412   private void assertType(boolean expectProtein,
413           AlignmentViewPanel alignmentViewPanel, String message)
414   {
415     List<SequenceI> nonType = new ArrayList<SequenceI>();
416     for (SequenceI sq : alignmentViewPanel.getAlignViewport()
417             .getAlignment()
418             .getSequences())
419     {
420       if (sq.isProtein() != expectProtein)
421       {
422         nonType.add(sq);
423       }
424     }
425     if (nonType.size() > 0)
426     {
427       Assert.fail(message + " [ "
428               + (expectProtein ? "nucleotides were " : "proteins were ")
429               + nonType.toString()
430               + " ]");
431     }
432   }
433
434   /**
435    * first time called, record strings derived from alignment and
436    * alignedcodonframes, and save view to a project file. Second time called,
437    * compare strings to existing ones. org.testng.Assert.assertTrue on
438    * stringmatch
439    * 
440    * @param dbtoviewBit
441    *          map between xrefpath and view string
442    * @param savedProjects
443    *          - map from xrefpath to saved project filename (createTempFile)
444    * @param xrefpath
445    *          - xrefpath - unique ID for this context (composed of sequence of
446    *          db-fetch/cross-ref actions preceeding state)
447    * @param avp
448    *          - viewpanel to store (for viewpanels in splitframe, the same
449    *          project should be written for both panels, only one needs
450    *          recovering for comparison on the next stringify call, but each
451    *          viewpanel needs to be called with a distinct xrefpath to ensure
452    *          each one's strings are compared)
453    */
454   private void stringify(HashMap<String, String> dbtoviewBit,
455           HashMap<String, File> savedProjects, String xrefpath,
456           AlignmentViewPanel avp)
457   {
458     if (savedProjects != null)
459     {
460       if (savedProjects.get(xrefpath) == null)
461       {
462         // write a project file for this view. On the second pass, this will be
463         // recovered and cross-references verified
464         try
465         {
466           File prfile = File.createTempFile("crossRefTest", ".jvp");
467           AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
468           new Jalview2XML(false).saveAlignment(af, prfile.toString(),
469                   af.getTitle());
470           System.out.println("Written view from '" + xrefpath + "' as '"
471                   + prfile.getAbsolutePath() + "'");
472           savedProjects.put(xrefpath, prfile);
473         } catch (IOException q)
474         {
475           Assert.fail("Unexpected IO Exception", q);
476         }
477       }
478       else
479       {
480         System.out.println("Stringify check on view from '" + xrefpath
481                 + "' [ possibly retrieved from '"
482                 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
483
484       }
485     }
486
487     StringBuilder sbr = new StringBuilder();
488     sbr.append(avp.getAlignment().toString());
489     sbr.append("\n");
490     sbr.append("<End of alignment>");
491     sbr.append("\n");
492     sbr.append(avp.getAlignment().getDataset());
493     sbr.append("\n");
494     sbr.append("<End of dataset>");
495     sbr.append("\n");
496     int p = 0;
497     if (avp.getAlignment().getCodonFrames() != null)
498     {
499       for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
500       {
501         sbr.append("<AlignedCodonFrame " + p++ + ">");
502         sbr.append("\n");
503         sbr.append(ac.toString());
504         sbr.append("\n");
505       }
506     }
507     String dbt = dbtoviewBit.get(xrefpath);
508     if (dbt == null)
509     {
510       dbtoviewBit.put(xrefpath, sbr.toString());
511     }
512     else
513     {
514       Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "
515               + xrefpath);
516     }
517   }
518 }