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