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