d9b1d90122c2248caa7266eb34eaef57276216ff
[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.util.DBRefUtils;
32 import jalview.ws.SequenceFetcher;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import org.testng.annotations.AfterClass;
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.Test;
40
41 /**
42  * @author jimp
43  * 
44  */
45 public class DbRefFetcherTest
46 {
47
48   /**
49    * @throws java.lang.Exception
50    */
51   @BeforeClass(alwaysRun = true)
52   public static void setUpBeforeClass() throws Exception
53   {
54     jalview.bin.Cache.initLogger();
55   }
56
57   /**
58    * @throws java.lang.Exception
59    */
60   @AfterClass
61   public static void tearDownAfterClass() throws Exception
62   {
63   }
64
65   @Test(groups ={ "Functional" })
66   public void testStandardProtDbs()
67   {
68     String[] defdb = DBRefSource.PROTEINDBS;
69     List<DbSourceProxy> srces = new ArrayList<DbSourceProxy>();
70     for (String ddb : defdb)
71     {
72       SequenceFetcher sfetcher = new SequenceFetcher();
73       List<DbSourceProxy> srcesfordb = sfetcher.getSourceProxy(ddb);
74
75       if (srcesfordb != null)
76       {
77         srces.addAll(srcesfordb);
78       }
79     }
80     DbSourceProxy uniprot = null;
81     int i = 0;
82     // append the selected sequence sources to the default dbs
83     for (DbSourceProxy s : srces)
84     {
85       if (s.getDbSource().equalsIgnoreCase(DBRefSource.UNIPROT))
86       {
87         i++;
88       }
89
90       if (s instanceof jalview.ws.dbsources.Uniprot)
91       {
92         uniprot = s;
93         break;
94       }
95     }
96
97     assertTrue("Failed to find Uniprot source as first source amongst "
98             + srces.size() + " sources (source was at position " + i + ")",
99             uniprot != null && i < 2);
100   }
101
102   @Test(groups =
103   { "External" })
104   public void testEmblUniprotProductRecovery() throws Exception
105   {
106     String retrievalId = "CAA23748"; // "V00488";
107     DbSourceProxy embl = new SequenceFetcher().getSourceProxy(DBRefSource.EMBL).get(0);
108     assertNotNull("Couldn't find the EMBL retrieval client", embl);
109     verifyProteinNucleotideXref(retrievalId, embl);
110   }
111
112   @Test(groups =
113   { "External" })
114   public void testEmblCDSUniprotProductRecovery() throws Exception
115   {
116     String retrievalId = "AAH29712";
117     DbSourceProxy embl = new SequenceFetcher().getSourceProxy(
118             DBRefSource.EMBLCDS).get(0);
119     assertNotNull("Couldn't find the EMBL retrieval client", embl);
120     verifyProteinNucleotideXref(retrievalId, embl);
121   }
122
123   private void verifyProteinNucleotideXref(String retrievalId,
124           DbSourceProxy embl) throws Exception
125   {
126     AlignmentI alsq = embl.getSequenceRecords(retrievalId);
127     assertNotNull("Couldn't find the EMBL record " + retrievalId, alsq);
128     assertEquals("Didn't retrieve right number of records", 1, alsq.getHeight());
129     DBRefEntry[] dr = DBRefUtils.selectRefs(alsq.getSequenceAt(0).getDBRef(), DBRefSource.PROTEINSEQ);
130     assertNotNull(dr);
131     assertEquals("Expected a single Uniprot cross reference", 1, dr.length);
132     assertEquals("Expected cross refernce map to be one amino acid", dr[0]
133             .getMap().getMappedWidth(), 1);
134     assertEquals("Expected local refernce map to be 3 nucleotides", dr[0]
135             .getMap().getWidth(), 3);
136     AlignmentI sprods = CrossRef.findXrefSequences(alsq.getSequencesArray(), true, dr[0].getSource(), alsq.getDataset());
137     assertNotNull(
138             "Couldn't recover cross reference sequence from dataset. Was it ever added ?",
139             sprods);
140     
141     
142   }
143 }