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