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