Merge branch 'features/r2_11_2_alphafold/JAL-2349_JAL-3855' into 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.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     {
102       assertEquals(DBRefSource.UNIPROT, ref.getCanonicalSourceName());
103       canonicalUp |= ref.isCanonical();
104     }
105     assertTrue("No Canonical Uniprot reference detected", canonicalUp);
106   }
107
108   /**
109    * Tests that standard protein database sources include Uniprot (as the first)
110    * and also PDB. (Additional sources are dependent on availability of DAS
111    * services.)
112    */
113   @Test(groups = { "Functional" })
114   public void testStandardProtDbs()
115   {
116     List<String> defdb = new ArrayList<String>();
117     defdb.addAll(Arrays.asList(DBRefSource.PROTEINDBS));
118     defdb.add(DBRefSource.PDB);
119     List<DbSourceProxy> srces = new ArrayList<DbSourceProxy>();
120     SequenceFetcher sfetcher = new SequenceFetcher();
121     boolean pdbFound = false;
122
123     for (String ddb : defdb)
124     {
125       List<DbSourceProxy> srcesfordb = sfetcher.getSourceProxy(ddb);
126
127       if (srcesfordb != null)
128       {
129         // TODO is this right? get duplicate entries
130         srces.addAll(srcesfordb);
131       }
132     }
133
134     int i = 0;
135     int uniprotPos = -1;
136     for (DbSourceProxy s : srces)
137     {
138       if (s instanceof Uniprot && uniprotPos == -1)
139       {
140         uniprotPos = i;
141       }
142       if (s instanceof Pdb)
143       {
144         pdbFound = true;
145       }
146       i++;
147     }
148
149     assertTrue("Failed to find Uniprot source as first source amongst "
150             + srces.size() + " sources (source was at position "
151             + uniprotPos + ")", uniprotPos == 0);
152     assertTrue("Failed to find PDB source amongst " + srces.size()
153             + " sources", pdbFound);
154   }
155
156   /**
157    * Tests retrieval of one entry from EMBL. Test is dependent on availability
158    * of network and the EMBL service.
159    * 
160    * @throws Exception
161    */
162   @Test(groups = { "External" })
163   public void testEmblUniprotProductRecovery() throws Exception
164   {
165     String retrievalId = "V00488";
166     DbSourceProxy embl = new SequenceFetcher()
167             .getSourceProxy(DBRefSource.EMBL).get(0);
168     assertNotNull("Couldn't find the EMBL retrieval client", embl);
169     verifyProteinNucleotideXref(retrievalId, embl);
170   }
171
172   /**
173    * Tests retrieval of one entry from EMBLCDS. Test is dependent on
174    * availability of network and the EMBLCDS service.
175    * 
176    * @throws Exception
177    */
178   @Test(groups = { "External" })
179   public void testEmblCDSUniprotProductRecovery() throws Exception
180   {
181     String retrievalId = "AAH29712";
182     DbSourceProxy embl = new SequenceFetcher()
183             .getSourceProxy(DBRefSource.EMBLCDS).get(0);
184     assertNotNull("Couldn't find the EMBL retrieval client", embl);
185     verifyProteinNucleotideXref(retrievalId, embl);
186   }
187
188   /**
189    * Helper method to perform database retrieval and verification of results.
190    * 
191    * @param retrievalId
192    * @param embl
193    * @throws Exception
194    */
195   private void verifyProteinNucleotideXref(String retrievalId,
196           DbSourceProxy embl) throws Exception
197   {
198     AlignmentI alsq = embl.getSequenceRecords(retrievalId);
199     assertNotNull("Couldn't find the EMBL record " + retrievalId, alsq);
200     assertEquals("Didn't retrieve right number of records", 1,
201             alsq.getHeight());
202     SequenceI seq = alsq.getSequenceAt(0);
203     assertEquals("Wrong sequence name",
204             embl.getDbSource() + "|" + retrievalId, seq.getName());
205     List<SequenceFeature> sfs = seq.getSequenceFeatures();
206     assertFalse("Sequence features missing", sfs.isEmpty());
207     assertTrue("Feature not CDS", FeatureProperties
208             .isCodingFeature(embl.getDbSource(), sfs.get(0).getType()));
209     assertEquals(embl.getDbSource(), sfs.get(0).getFeatureGroup());
210     List<DBRefEntry> dr = DBRefUtils.selectRefs(seq.getDBRefs(),
211             new String[]
212             { DBRefSource.UNIPROT });
213     assertNotNull(dr);
214     assertEquals("Expected a single Uniprot cross reference", 1, dr.size());
215     assertEquals("Expected cross reference map to be one amino acid",
216             dr.get(0).getMap().getMappedWidth(), 1);
217     assertEquals("Expected local reference map to be 3 nucleotides",
218             dr.get(0).getMap().getWidth(), 3);
219     AlignmentI sprods = new CrossRef(alsq.getSequencesArray(), alsq)
220             .findXrefSequences(dr.get(0).getSource(), true);
221     assertNotNull(
222             "Couldn't recover cross reference sequence from dataset. Was it ever added ?",
223             sprods);
224     assertEquals("Didn't xref right number of records", 1,
225             sprods.getHeight());
226     SequenceI proteinSeq = sprods.getSequenceAt(0);
227     assertEquals(proteinSeq.getSequenceAsString(),
228             dr.get(0).getMap().getTo().getSequenceAsString());
229     assertEquals(dr.get(0).getSource() + "|" + dr.get(0).getAccessionId(),
230             proteinSeq.getName());
231   }
232
233   /**
234    * Tests retrieval of one entry from EMBLCDS. Test is dependent on
235    * availability of network and the EMBLCDS service.
236    * 
237    * @throws Exception
238    */
239   @Test(groups = { "External" })
240   public void testAlphaFoldClien() throws Exception
241   {
242     DbSourceProxy alphafold = new EBIAlfaFold();
243     AlignmentI resp = alphafold.getSequenceRecords(alphafold.getTestQuery());
244     assertNotNull(resp);
245     assertEquals("One sequence only",resp.getHeight(), 1);
246     for (AlignmentAnnotation aa:resp.getAlignmentAnnotation()) {
247       if (aa.graph == AlignmentAnnotation.CUSTOMRENDERER)
248       {
249         assertTrue("Contact map didn't provide valid contact",resp.getContactListFor(aa, 1).getContactAt(1)!=-1d);
250         // test passes
251         return;
252       }
253     }
254     Assert.fail();
255   }
256
257 }