9e24262c89a38b53a5467e253b0ec4c85106913c
[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<>();
69     List<String> failedXrefMenuItems = new ArrayList<>();
70     List<String> failedProjectRecoveries = new ArrayList<>();
71     // for every set of db queries
72     // retrieve db query
73     // verify presence of expected xrefs
74     // show xrefs - verify expected type of frame is shown for each xref
75     // show xrefs again
76     // - verify original -> xref -> xref(original) recovers frame containing at
77     // least the first retrieved sequence
78     // store
79     // 1. whole project
80     // 2. individual frames
81     // 3. load each one back and verify
82     // . aligned sequences (.toString() )
83     // . xrefs (.toString() )
84     // . codonframes
85     //
86     //
87     HashMap<String, String> dbtoviewBit = new HashMap<>();
88     List<String> keyseq = new ArrayList<>();
89     HashMap<String, File> savedProjects = new HashMap<>();
90
91     for (String[] did : new String[][] { { "ENSEMBL", "ENSG00000157764" },
92     { "UNIPROT", "P01731" } })
93     {
94       // pass counters - 0 - first pass, 1 means retrieve project rather than
95       // perform action
96       int pass1 = 0, pass2 = 0, pass3 = 0;
97       // each do loop performs two iterations in the first outer loop pass, but
98       // only performs one iteration on the second outer loop
99       // ie. pass 1 = 0 {pass 2= 0 { pass 3 = 0,1 }, pass 2=1 { pass 3 = 0 }}, 1
100       // { pass 2 = 0 { pass 3 = 0 } }
101       do
102       {
103         String first = did[0] + " " + did[1];
104         AlignFrame af = null;
105         boolean dna;
106         AlignmentI retral;
107         AlignmentI dataset;
108         SequenceI[] seqs;
109         List<String> ptypes = null;
110         if (pass1 == 0)
111         {
112           // retrieve dbref
113
114           List<AlignFrame> afs = jalview.gui.SequenceFetcher.fetchAndShow(
115                   did[0], did[1]);
116           if (afs.size() == 0)
117           {
118             failedDBRetr.add("Didn't retrieve " + first);
119             break;
120           }
121           keyseq.add(first);
122           af = afs.get(0);
123
124           // verify references for retrieved data
125           AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
126                   .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
127                   + pass3 + "): Fetch " + first + ":");
128           assertDatasetIsNormalisedKnownDefect(af.getViewport()
129                   .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
130                   + pass3 + "): Fetch " + first + ":");
131           dna = af.getViewport().getAlignment().isNucleotide();
132           retral = af.getViewport().getAlignment();
133           dataset = retral.getDataset();
134           seqs = retral.getSequencesArray();
135
136         }
137         else
138         {
139           Desktop.instance.closeAll_actionPerformed(null);
140           // recover stored project
141           af = new FileLoader(false).LoadFileWaitTillLoaded(savedProjects
142                   .get(first).toString(), DataSourceType.FILE);
143           System.out.println("Recovered view for '" + first + "' from '"
144                   + savedProjects.get(first).toString() + "'");
145           dna = af.getViewport().getAlignment().isNucleotide();
146           retral = af.getViewport().getAlignment();
147           dataset = retral.getDataset();
148           seqs = retral.getSequencesArray();
149
150           // verify references for recovered data
151           AlignmentTest.assertAlignmentDatasetRefs(af.getViewport()
152                   .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
153                   + pass3 + "): Recover " + first + ":");
154           assertDatasetIsNormalisedKnownDefect(af.getViewport()
155                   .getAlignment(), "Pass (" + pass1 + "," + pass2 + ","
156                   + pass3 + "): Recover " + first + ":");
157
158         }
159
160         // store project on first pass, compare next pass
161         stringify(dbtoviewBit, savedProjects, first, af.alignPanel);
162
163         ptypes = (seqs == null || seqs.length == 0) ? null : new CrossRef(
164                 seqs, dataset).findXrefSourcesForSequences(dna);
165
166         // start of pass2: retrieve each cross-ref for fetched or restored
167         // project.
168         do // first cross ref and recover crossref loop
169         {
170
171           for (String db : ptypes)
172           {
173             // counter for splitframe views retrieved via crossref
174             int firstcr_ap = 0;
175             // build next key so we an retrieve all views
176             String nextxref = first + " -> " + db + "{" + firstcr_ap + "}";
177             // perform crossref action, or retrieve stored project
178             List<AlignmentViewPanel> cra_views = new ArrayList<>();
179             CrossRefAction cra = null;
180
181             if (pass2 == 0)
182             { // retrieve and show cross-refs in this thread
183               cra = new CrossRefAction(af, seqs, dna, db);
184               cra.run();
185               if (cra.getXrefViews().size() == 0)
186               {
187                 failedXrefMenuItems.add("No crossrefs retrieved for "
188                         + first + " -> " + db);
189                 continue;
190               }
191               cra_views = cra.getXrefViews();
192               assertNucleotide(cra_views.get(0),
193                       "Nucleotide panel included proteins for " + first
194                               + " -> " + db);
195               assertProtein(cra_views.get(1),
196                       "Protein panel included nucleotides for " + first
197                               + " -> " + db);
198             }
199             else
200             {
201               Desktop.instance.closeAll_actionPerformed(null);
202               pass3 = 0;
203               // recover stored project
204               File storedProject = savedProjects.get(nextxref);
205               if (storedProject == null)
206               {
207                 failedProjectRecoveries.add("Failed to store a view for '"
208                         + nextxref + "'");
209                 continue;
210               }
211
212               // recover stored project
213               AlignFrame af2 = new FileLoader(false)
214                       .LoadFileWaitTillLoaded(savedProjects.get(nextxref)
215                               .toString(), DataSourceType.FILE);
216               System.out.println("Recovered view for '" + nextxref
217                       + "' from '" + savedProjects.get(nextxref).toString()
218                       + "'");
219               // gymnastics to recover the alignPanel/Complementary alignPanel
220               if (af2.getViewport().isNucleotide())
221               {
222                 // top view, then bottom
223                 cra_views.add(af2.getViewport().getAlignPanel());
224                 cra_views.add(((jalview.gui.AlignViewport) af2
225                         .getViewport().getCodingComplement())
226                         .getAlignPanel());
227
228               }
229               else
230               {
231                 // bottom view, then top
232                 cra_views.add(((jalview.gui.AlignViewport) af2
233                         .getViewport().getCodingComplement())
234                         .getAlignPanel());
235                 cra_views.add(af2.getViewport().getAlignPanel());
236
237               }
238             }
239             HashMap<String, List<String>> xrptypes = new HashMap<>();
240             // first save/verify views.
241             for (AlignmentViewPanel avp : cra_views)
242             {
243               nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
244               // verify references for this panel
245               AlignmentTest.assertAlignmentDatasetRefs(avp.getAlignment(),
246                       "Pass (" + pass1 + "," + pass2 + "," + pass3
247                               + "): before start of pass3: " + nextxref
248                               + ":");
249               assertDatasetIsNormalisedKnownDefect(avp.getAlignment(),
250                       "Pass (" + pass1 + "," + pass2 + "," + pass3
251                               + "): before start of pass3: " + nextxref
252                               + ":");
253
254               SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
255
256               List<String> _xrptypes = (seqs == null || seqs.length == 0) ? null
257                       : new CrossRef(xrseqs, dataset)
258                               .findXrefSourcesForSequences(avp
259                                       .getAlignViewport().isNucleotide());
260
261               stringify(dbtoviewBit, savedProjects, nextxref, avp);
262               xrptypes.put(nextxref, _xrptypes);
263
264             }
265
266             // now do the second xref pass starting from either saved or just
267             // recovered split pane, in sequence
268             do // retrieve second set of cross refs or recover and verify
269             {
270               firstcr_ap = 0;
271               for (AlignmentViewPanel avp : cra_views)
272               {
273                 nextxref = first + " -> " + db + "{" + firstcr_ap++ + "}";
274                 for (String xrefdb : xrptypes.get(nextxref))
275                 {
276                   List<AlignmentViewPanel> cra_views2 = new ArrayList<>();
277                   int q = 0;
278                   String nextnextxref = nextxref + " -> " + xrefdb + "{"
279                           + q + "}";
280
281                   if (pass3 == 0)
282                   {
283
284                     SequenceI[] xrseqs = avp.getAlignment()
285                             .getSequencesArray();
286                     AlignFrame nextaf = Desktop.getAlignFrameFor(avp
287                             .getAlignViewport());
288
289                     cra = new CrossRefAction(nextaf, xrseqs, avp
290                             .getAlignViewport().isNucleotide(), xrefdb);
291                     cra.run();
292                     if (cra.getXrefViews().size() == 0)
293                     {
294                       failedXrefMenuItems
295                               .add("No crossrefs retrieved for '"
296                                       + nextxref + "' to " + xrefdb
297                                       + " via '" + nextaf.getTitle() + "'");
298                       continue;
299                     }
300                     cra_views2 = cra.getXrefViews();
301                     assertNucleotide(cra_views2.get(0),
302                             "Nucleotide panel included proteins for '"
303                                     + nextxref + "' to " + xrefdb
304                                     + " via '" + nextaf.getTitle() + "'");
305                     assertProtein(cra_views2.get(1),
306                             "Protein panel included nucleotides for '"
307                                     + nextxref + "' to " + xrefdb
308                                     + " via '" + nextaf.getTitle() + "'");
309
310                   }
311                   else
312                   {
313                     Desktop.instance.closeAll_actionPerformed(null);
314                     // recover stored project
315                     File storedProject = savedProjects.get(nextnextxref);
316                     if (storedProject == null)
317                     {
318                       failedProjectRecoveries
319                               .add("Failed to store a view for '"
320                                       + nextnextxref + "'");
321                       continue;
322                     }
323                     AlignFrame af2 = new FileLoader(false)
324                             .LoadFileWaitTillLoaded(
325                                     savedProjects.get(nextnextxref)
326                                             .toString(),
327                                     DataSourceType.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<>();
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 }