JAL-1672 additional assertions added
[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.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 =
120   { "External" })
121   public void testEmblUniprotProductRecovery() throws Exception
122   {
123     String retrievalId = "V00488";
124     DbSourceProxy embl = new SequenceFetcher().getSourceProxy(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 =
136   { "External" })
137   public void testEmblCDSUniprotProductRecovery() throws Exception
138   {
139     String retrievalId = "AAH29712";
140     DbSourceProxy embl = new SequenceFetcher().getSourceProxy(
141             DBRefSource.EMBLCDS).get(0);
142     assertNotNull("Couldn't find the EMBL retrieval client", embl);
143     verifyProteinNucleotideXref(retrievalId, embl);
144   }
145
146   /**
147    * Helper method to perform database retrieval and verification of results.
148    * 
149    * @param retrievalId
150    * @param embl
151    * @throws Exception
152    */
153   private void verifyProteinNucleotideXref(String retrievalId,
154           DbSourceProxy embl) throws Exception
155   {
156     AlignmentI alsq = embl.getSequenceRecords(retrievalId);
157     assertNotNull("Couldn't find the EMBL record " + retrievalId, alsq);
158     assertEquals("Didn't retrieve right number of records", 1, 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(), DBRefSource.PROTEINSEQ);
170     assertNotNull(dr);
171     assertEquals("Expected a single Uniprot cross reference", 1, dr.length);
172     assertEquals("Expected cross reference map to be one amino acid", dr[0]
173             .getMap().getMappedWidth(), 1);
174     assertEquals("Expected local reference map to be 3 nucleotides", dr[0]
175             .getMap().getWidth(), 3);
176     AlignmentI sprods = CrossRef.findXrefSequences(alsq.getSequencesArray(), true, dr[0].getSource(), alsq.getDataset());
177     assertNotNull(
178             "Couldn't recover cross reference sequence from dataset. Was it ever added ?",
179             sprods);
180     assertEquals("Didn't xref right number of records", 1,
181             sprods.getHeight());
182     SequenceI proteinSeq = sprods.getSequenceAt(0);
183     assertEquals(proteinSeq.getSequenceAsString(), dr[0].getMap().getTo()
184             .getSequenceAsString());
185     assertEquals(dr[0].getSource() + "|" + dr[0].getAccessionId(),
186             proteinSeq.getName());
187   }
188 }