Merge branch 'develop' into trialMerge
[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(), DataSourceType.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(), DataSourceType.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(),
318                                     DataSourceType.FILE);
319                     System.out.println("Recovered view for '"
320                             + nextnextxref + "' from '"
321                             + savedProjects.get(nextnextxref).toString()
322                             + "'");
323                     // gymnastics to recover the alignPanel/Complementary
324                     // alignPanel
325                     if (af2.getViewport().isNucleotide())
326                     {
327                       // top view, then bottom
328                       cra_views2.add(af2.getViewport().getAlignPanel());
329                       cra_views2.add(((jalview.gui.AlignViewport) af2
330                               .getViewport().getCodingComplement())
331                               .getAlignPanel());
332
333                     }
334                     else
335                     {
336                       // bottom view, then top
337                       cra_views2.add(((jalview.gui.AlignViewport) af2
338                               .getViewport().getCodingComplement())
339                               .getAlignPanel());
340                       cra_views2.add(af2.getViewport().getAlignPanel());
341                     }
342                     Assert.assertEquals(cra_views2.size(), 2);
343                     Assert.assertNotNull(cra_views2.get(0));
344                     Assert.assertNotNull(cra_views2.get(1));
345                   }
346
347                   for (AlignmentViewPanel nextavp : cra_views2)
348                   {
349                     nextnextxref = nextxref + " -> " + xrefdb + "{" + q++
350                             + "}";
351
352                     // verify references for this panel
353                     AlignmentTest.assertAlignmentDatasetRefs(
354                             nextavp.getAlignment(), "" + "Pass (" + pass1
355                                     + "," + pass2 + "): For "
356                                     + nextnextxref + ":");
357                     assertDatasetIsNormalisedKnownDefect(
358                             nextavp.getAlignment(), "" + "Pass (" + pass1
359                                     + "," + pass2 + "): For "
360                                     + nextnextxref + ":");
361
362                     stringify(dbtoviewBit, savedProjects, nextnextxref,
363                             nextavp);
364                     keyseq.add(nextnextxref);
365                   }
366                 } // end of loop around showing all xrefdb for crossrf2
367
368               } // end of loop around all viewpanels from crossrf1
369             } while (pass2 == 2 && pass3++ < 2);
370             // fetchdb->crossref1->crossref-2->verify for xrefs we
371             // either loop twice when pass2=0, or just once when pass2=1
372             // (recovered project from previous crossref)
373
374           } // end of loop over db-xrefs for crossref-2
375
376           // fetchdb-->crossref1
377           // for each xref we try to retrieve xref, store and verify when
378           // pass1=0, or just retrieve and verify when pass1=1
379         } while (pass1 == 1 && pass2++ < 2);
380         // fetchdb
381         // for each ref we
382         // loop twice: first, do the retrieve, second recover from saved project
383
384         // increment pass counters, so we repeat traversal starting from the
385         // oldest saved project first.
386         if (pass1 == 0)
387         {
388           // verify stored projects for first set of cross references
389           pass1 = 1;
390           // and verify cross-references retrieved from stored projects
391           pass2 = 0;
392           pass3 = 0;
393         }
394         else
395         {
396           pass1++;
397         }
398       } while (pass1 < 3);
399     }
400     if (failedXrefMenuItems.size() > 0)
401     {
402       for (String s : failedXrefMenuItems)
403       {
404         System.err.println(s);
405       }
406       Assert.fail("Faulty xref menu (" + failedXrefMenuItems.size()
407               + " counts)");
408     }
409     if (failedProjectRecoveries.size() > 0)
410     {
411
412       for (String s : failedProjectRecoveries)
413       {
414         System.err.println(s);
415       }
416       Assert.fail("Didn't recover projects for some retrievals (did they retrieve ?) ("
417               + failedProjectRecoveries.size() + " counts)");
418     }
419     if (failedDBRetr.size() > 0)
420     {
421       for (String s : failedProjectRecoveries)
422       {
423         System.err.println(s);
424       }
425       Assert.fail("Didn't retrieve some db refs for checking cross-refs ("
426               + failedDBRetr.size() + " counts)");
427     }
428   }
429
430   /**
431    * wrapper to trap known defect for AH002001 testcase
432    * 
433    * @param alignment
434    * @param string
435    */
436   private void assertDatasetIsNormalisedKnownDefect(AlignmentI al,
437           String message)
438   {
439     try
440     {
441       AlignmentTest.assertDatasetIsNormalised(al, message);
442     } catch (AssertionError ae)
443     {
444       if (!ae.getMessage().endsWith("EMBL|AH002001"))
445       {
446         throw ae;
447       }
448       else
449       {
450         System.out
451                 .println("Ignored exception for known defect: JAL-2179 : "
452                         + message);
453       }
454
455     }
456   }
457
458   private void assertProtein(AlignmentViewPanel alignmentViewPanel,
459           String message)
460   {
461     assertType(true, alignmentViewPanel, message);
462   }
463
464   private void assertNucleotide(AlignmentViewPanel alignmentViewPanel,
465           String message)
466   {
467     assertType(false, alignmentViewPanel, message);
468   }
469
470   private void assertType(boolean expectProtein,
471           AlignmentViewPanel alignmentViewPanel, String message)
472   {
473     List<SequenceI> nonType = new ArrayList<SequenceI>();
474     for (SequenceI sq : alignmentViewPanel.getAlignViewport()
475             .getAlignment().getSequences())
476     {
477       if (sq.isProtein() != expectProtein)
478       {
479         nonType.add(sq);
480       }
481     }
482     if (nonType.size() > 0)
483     {
484       Assert.fail(message + " [ "
485               + (expectProtein ? "nucleotides were " : "proteins were ")
486               + nonType.toString() + " ]");
487     }
488   }
489
490   /**
491    * first time called, record strings derived from alignment and
492    * alignedcodonframes, and save view to a project file. Second time called,
493    * compare strings to existing ones. org.testng.Assert.assertTrue on
494    * stringmatch
495    * 
496    * @param dbtoviewBit
497    *          map between xrefpath and view string
498    * @param savedProjects
499    *          - map from xrefpath to saved project filename (createTempFile)
500    * @param xrefpath
501    *          - xrefpath - unique ID for this context (composed of sequence of
502    *          db-fetch/cross-ref actions preceeding state)
503    * @param avp
504    *          - viewpanel to store (for viewpanels in splitframe, the same
505    *          project should be written for both panels, only one needs
506    *          recovering for comparison on the next stringify call, but each
507    *          viewpanel needs to be called with a distinct xrefpath to ensure
508    *          each one's strings are compared)
509    */
510   private void stringify(HashMap<String, String> dbtoviewBit,
511           HashMap<String, File> savedProjects, String xrefpath,
512           AlignmentViewPanel avp)
513   {
514     if (savedProjects != null)
515     {
516       if (savedProjects.get(xrefpath) == null)
517       {
518         // write a project file for this view. On the second pass, this will be
519         // recovered and cross-references verified
520         try
521         {
522           File prfile = File.createTempFile("crossRefTest", ".jvp");
523           AlignFrame af = Desktop.getAlignFrameFor(avp.getAlignViewport());
524           new Jalview2XML(false).saveAlignment(af, prfile.toString(),
525                   af.getTitle());
526           System.out.println("Written view from '" + xrefpath + "' as '"
527                   + prfile.getAbsolutePath() + "'");
528           savedProjects.put(xrefpath, prfile);
529         } catch (IOException q)
530         {
531           Assert.fail("Unexpected IO Exception", q);
532         }
533       }
534       else
535       {
536         System.out.println("Stringify check on view from '" + xrefpath
537                 + "' [ possibly retrieved from '"
538                 + savedProjects.get(xrefpath).getAbsolutePath() + "' ]");
539
540       }
541     }
542
543     StringBuilder sbr = new StringBuilder();
544     sbr.append(avp.getAlignment().toString());
545     sbr.append("\n");
546     sbr.append("<End of alignment>");
547     sbr.append("\n");
548     sbr.append(avp.getAlignment().getDataset());
549     sbr.append("\n");
550     sbr.append("<End of dataset>");
551     sbr.append("\n");
552     int p = 0;
553     if (avp.getAlignment().getCodonFrames() != null)
554     {
555       for (AlignedCodonFrame ac : avp.getAlignment().getCodonFrames())
556       {
557         sbr.append("<AlignedCodonFrame " + p++ + ">");
558         sbr.append("\n");
559         sbr.append(ac.toString());
560         sbr.append("\n");
561       }
562     }
563     String dbt = dbtoviewBit.get(xrefpath);
564     if (dbt == null)
565     {
566       dbtoviewBit.put(xrefpath, sbr.toString());
567     }
568     else
569     {
570       Assert.assertEquals(sbr.toString(), dbt, "stringify mismatch for "
571               + xrefpath);
572     }
573   }
574 }