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