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