JAL-2154 refactor CrossRef UI code to its own class
[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 static org.testng.AssertJUnit.assertTrue;
24
25 import jalview.analysis.CrossRef;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.CrossRefAction;
31 import jalview.gui.Desktop;
32
33 import java.util.IdentityHashMap;
34 import java.util.List;
35
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
38
39 @Test(singleThreaded = true)
40 public class CrossRef2xmlTests extends Jalview2xmlBase
41 {
42
43   /**
44    * test store and recovery of expanded views
45    * 
46    * @throws Exception
47    */
48   @Test(groups = { "Operational" }, enabled = true)
49   public void testRetrieveAndShowCrossref() throws Exception
50   {
51     // for every set of db queries
52     // retrieve db query
53     // verify presence of expected xrefs
54     // show xrefs - verify expected type of frame is shown for each xref
55     // show xrefs again
56     // - verify original -> xref -> xref(original) recovers frame containing at
57     // least the first retrieved sequence
58     // store
59     // 1. whole project
60     // 2. individual frames
61     // 3. load each one back and verify
62     // . aligned sequences (.toString() )
63     // . xrefs (.toString() )
64     // . codonframes
65     //
66     //
67     for (String[] did : new String[][] { { "UNIPROT", "P01731" } })
68     {
69       AlignFrame af = jalview.gui.SequenceFetcher.fetchAndShow(did[0],
70               did[1]).get(0);
71       assertTrue("Didn't read in the example file correctly.", af != null);
72       boolean dna = af.getViewport().getAlignment().isNucleotide();
73       AlignmentI retral = af.getViewport().getAlignment();
74       AlignmentI dataset = retral.getDataset();
75       SequenceI[] seqs = retral.getSequencesArray();
76       List<String> ptypes = (seqs == null || seqs.length == 0) ? null
77               : new CrossRef(seqs, dataset)
78                       .findXrefSourcesForSequences(dna);
79       /*
80        * map between a view, and views generated after retrieving xrefs
81        */
82       IdentityHashMap<AlignmentViewPanel, List<AlignmentViewPanel>> viewxrefview = new IdentityHashMap<AlignmentViewPanel, List<AlignmentViewPanel>>();
83       /*
84        * map between a particular view and it's originating dbref path
85        */
86       IdentityHashMap<AlignmentViewPanel, String> viewsourcedb = new IdentityHashMap<AlignmentViewPanel, String>();
87
88       String first = did[0] + " " + did[1];
89       viewsourcedb.put(af.alignPanel, first);
90       for (String db : ptypes)
91       {
92         // retrieve and show cross-refs in this thread
93         CrossRefAction cra = new CrossRefAction(af, seqs, dna, db);
94         cra.run();
95         Assert.assertTrue(cra.getXrefViews().size() > 0,
96                 "No crossrefs retrieved for " + db);
97         viewxrefview.put(af.alignPanel, cra.getXrefViews());
98
99         for (AlignmentViewPanel avp : cra.getXrefViews())
100         {
101
102           SequenceI[] xrseqs = avp.getAlignment().getSequencesArray();
103           String nextxref = first + " -> " + db;
104           viewsourcedb.put(avp, nextxref);
105           List<String> xrptypes = (seqs == null || seqs.length == 0) ? null
106                   : new CrossRef(xrseqs, dataset)
107                           .findXrefSourcesForSequences(avp
108                                   .getAlignViewport().isNucleotide());
109           for (String xrefdb : xrptypes)
110           {
111             AlignFrame nextaf = Desktop.getAlignFrameFor(avp
112                     .getAlignViewport());
113             cra = new CrossRefAction(nextaf, xrseqs, avp.getAlignViewport()
114                     .isNucleotide(), xrefdb);
115             cra.run();
116             Assert.assertTrue(cra.getXrefViews().size() > 0,
117                     "No crossrefs found for '" + nextxref + "' to "
118                             + xrefdb + " via '" + nextaf.getTitle() + "'");
119             // save views for analysis
120             viewxrefview.put(avp, cra.getXrefViews());
121             for (AlignmentViewPanel nextavp : cra.getXrefViews())
122             {
123
124               viewsourcedb.put(nextavp, nextxref + " -> " + xrefdb);
125             }
126           }
127         }
128       }
129     }
130     Thread.sleep(50000);
131   }
132
133 }