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