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