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