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