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