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