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