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