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