JAL-2326 added setup method for JvOptionPane in all Jalveiw test classes to enable...
[jalview.git] / test / jalview / ws / seqfetcher / DbRefFetcherTest.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.ws.seqfetcher;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.analysis.CrossRef;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.DBRefEntry;
30 import jalview.datamodel.DBRefSource;
31 import jalview.datamodel.FeatureProperties;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceI;
34 import jalview.gui.JvOptionPane;
35 import jalview.util.DBRefUtils;
36 import jalview.ws.SequenceFetcher;
37 import jalview.ws.dbsources.Pdb;
38 import jalview.ws.dbsources.Uniprot;
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
43
44 import org.testng.annotations.AfterClass;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.Test;
47
48 /**
49  * @author jimp
50  * 
51  */
52 public class DbRefFetcherTest
53 {
54
55   @BeforeClass(alwaysRun = true)
56   public void setUpJvOptionPane()
57   {
58     JvOptionPane.setInteractiveMode(false);
59     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
60   }
61
62   /**
63    * @throws java.lang.Exception
64    */
65   @BeforeClass(alwaysRun = true)
66   public static void setUpBeforeClass() throws Exception
67   {
68     jalview.bin.Cache.initLogger();
69   }
70
71   /**
72    * @throws java.lang.Exception
73    */
74   @AfterClass(alwaysRun = true)
75   public static void tearDownAfterClass() throws Exception
76   {
77   }
78
79   /**
80    * Tests that standard protein database sources include Uniprot (as the first)
81    * and also PDB. (Additional sources are dependent on availability of DAS
82    * services.)
83    */
84   @Test(groups = { "Functional" })
85   public void testStandardProtDbs()
86   {
87     List<String> defdb = new ArrayList<String>();
88     defdb.addAll(Arrays.asList(DBRefSource.PROTEINDBS));
89     defdb.add(DBRefSource.PDB);
90     List<DbSourceProxy> srces = new ArrayList<DbSourceProxy>();
91     SequenceFetcher sfetcher = new SequenceFetcher();
92     boolean pdbFound = false;
93
94     for (String ddb : defdb)
95     {
96       List<DbSourceProxy> srcesfordb = sfetcher.getSourceProxy(ddb);
97
98       if (srcesfordb != null)
99       {
100         // TODO is this right? get duplicate entries
101         srces.addAll(srcesfordb);
102       }
103     }
104
105     int i = 0;
106     int uniprotPos = -1;
107     for (DbSourceProxy s : srces)
108     {
109       if (s instanceof Uniprot && uniprotPos == -1)
110       {
111         uniprotPos = i;
112       }
113       if (s instanceof Pdb)
114       {
115         pdbFound = true;
116       }
117       i++;
118     }
119
120     assertTrue("Failed to find Uniprot source as first source amongst "
121             + srces.size() + " sources (source was at position "
122             + uniprotPos + ")", uniprotPos == 0);
123     assertTrue("Failed to find PDB source amongst " + srces.size()
124             + " sources", pdbFound);
125   }
126
127   /**
128    * Tests retrieval of one entry from EMBL. Test is dependent on availability
129    * of network and the EMBL service.
130    * 
131    * @throws Exception
132    */
133   @Test(groups = { "External" })
134   public void testEmblUniprotProductRecovery() throws Exception
135   {
136     String retrievalId = "V00488";
137     DbSourceProxy embl = new SequenceFetcher().getSourceProxy(
138             DBRefSource.EMBL).get(0);
139     assertNotNull("Couldn't find the EMBL retrieval client", embl);
140     verifyProteinNucleotideXref(retrievalId, embl);
141   }
142
143   /**
144    * Tests retrieval of one entry from EMBLCDS. Test is dependent on
145    * availability of network and the EMBLCDS service.
146    * 
147    * @throws Exception
148    */
149   @Test(groups = { "External" })
150   public void testEmblCDSUniprotProductRecovery() throws Exception
151   {
152     String retrievalId = "AAH29712";
153     DbSourceProxy embl = new SequenceFetcher().getSourceProxy(
154             DBRefSource.EMBLCDS).get(0);
155     assertNotNull("Couldn't find the EMBL retrieval client", embl);
156     verifyProteinNucleotideXref(retrievalId, embl);
157   }
158
159   /**
160    * Helper method to perform database retrieval and verification of results.
161    * 
162    * @param retrievalId
163    * @param embl
164    * @throws Exception
165    */
166   private void verifyProteinNucleotideXref(String retrievalId,
167           DbSourceProxy embl) throws Exception
168   {
169     AlignmentI alsq = embl.getSequenceRecords(retrievalId);
170     assertNotNull("Couldn't find the EMBL record " + retrievalId, alsq);
171     assertEquals("Didn't retrieve right number of records", 1,
172             alsq.getHeight());
173     SequenceI seq = alsq.getSequenceAt(0);
174     assertEquals("Wrong sequence name", embl.getDbSource() + "|"
175             + retrievalId, seq.getName());
176     SequenceFeature[] sfs = seq.getSequenceFeatures();
177     assertNotNull("Sequence features missing", sfs);
178     assertTrue(
179             "Feature not CDS",
180             FeatureProperties.isCodingFeature(embl.getDbSource(),
181                     sfs[0].getType()));
182     assertEquals(embl.getDbSource(), sfs[0].getFeatureGroup());
183     DBRefEntry[] dr = DBRefUtils.selectRefs(seq.getDBRefs(),
184             new String[] { DBRefSource.UNIPROT });
185     assertNotNull(dr);
186     assertEquals("Expected a single Uniprot cross reference", 1, dr.length);
187     assertEquals("Expected cross reference map to be one amino acid", dr[0]
188             .getMap().getMappedWidth(), 1);
189     assertEquals("Expected local reference map to be 3 nucleotides", dr[0]
190             .getMap().getWidth(), 3);
191     AlignmentI sprods = new CrossRef(alsq.getSequencesArray(), alsq)
192             .findXrefSequences(dr[0].getSource(), true);
193     assertNotNull(
194             "Couldn't recover cross reference sequence from dataset. Was it ever added ?",
195             sprods);
196     assertEquals("Didn't xref right number of records", 1,
197             sprods.getHeight());
198     SequenceI proteinSeq = sprods.getSequenceAt(0);
199     assertEquals(proteinSeq.getSequenceAsString(), dr[0].getMap().getTo()
200             .getSequenceAsString());
201     assertEquals(dr[0].getSource() + "|" + dr[0].getAccessionId(),
202             proteinSeq.getName());
203   }
204 }