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