414e8a72e0c351845680fe9b0cfaa685d85a575a
[jalview.git] / test / jalview / gui / structurechooser / StructureChooserQuerySourceTest.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.gui.structurechooser;
22
23
24 import static org.testng.Assert.assertEquals;
25 import static org.testng.Assert.assertTrue;
26
27 import java.util.Collection;
28 import java.util.Vector;
29
30 import org.junit.Assert;
31 import org.testng.AssertJUnit;
32 import org.testng.annotations.AfterMethod;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
36
37 import jalview.datamodel.DBRefEntry;
38 import jalview.datamodel.DBRefSource;
39 import jalview.datamodel.PDBEntry;
40 import jalview.datamodel.Sequence;
41 import jalview.datamodel.SequenceI;
42 import jalview.fts.api.FTSData;
43 import jalview.fts.core.FTSRestRequest;
44 import jalview.fts.core.FTSRestResponse;
45 import jalview.fts.service.pdb.PDBFTSRestClient;
46 import jalview.fts.threedbeacons.TDBeaconsFTSRestClientTest;
47 import jalview.gui.JvOptionPane;
48 import jalview.gui.StructureChooser;
49 import jalview.jbgui.FilterOption;
50
51 public class StructureChooserQuerySourceTest
52 {
53
54   @BeforeClass(alwaysRun = true)
55   public void setUpJvOptionPane()
56   {
57     JvOptionPane.setInteractiveMode(false);
58     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
59   }
60
61   Sequence seq,upSeq;
62
63   // same set up as for structurechooser test
64   
65 @BeforeMethod(alwaysRun = true)
66   public void setUp() throws Exception
67   {
68     seq = new Sequence("PDB|4kqy|4KQY|A", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1,
69             26);
70     seq.createDatasetSequence();
71     for (int x = 1; x < 5; x++)
72     {
73       DBRefEntry dbRef = new DBRefEntry();
74       dbRef.setAccessionId("XYZ_" + x);
75       seq.addDBRef(dbRef);
76     }
77
78     PDBEntry dbRef = new PDBEntry();
79     dbRef.setId("1tim");
80
81     Vector<PDBEntry> pdbIds = new Vector<>();
82     pdbIds.add(dbRef);
83
84     seq.setPDBId(pdbIds);
85     
86     // Uniprot sequence for 3D-Beacons mocks
87     upSeq = new Sequence("P38398", 
88             "MDLSALRVEEVQNVINAMQKILECPICLELIKEPVSTKCDHIFCKFCMLKLLNQKKGPSQCPLCKNDITKRS\n"
89             + "LQESTRFSQLVEELLKIICAFQLDTGLEYANSYNFAKKENNSPEHLKDEVSIIQSMGYRNRAKRLLQSEPEN\n"
90             + "PSLQETSLSVQLSNLGTVRTLRTKQRIQPQKTSVYIELGSDSSEDTVNKATYCSVGDQELLQITPQGTRDEI\n"
91             + "SLDSAKKAACEFSETDVTNTEHHQPSNNDLNTTEKRAAERHPEKYQGSSVSNLHVEPCGTNTHASSLQHENS\n"
92             + "SLLLTKDRMNVEKAEFCNKSKQPGLARSQHNRWAGSKETCNDRRTPSTEKKVDLNADPLCERKEWNKQKLPC\n"
93             + "SENPRDTEDVPWITLNSSIQKVNEWFSRSDELLGSDDSHDGESESNAKVADVLDVLNEVDEYSGSSEKIDLL\n"
94             + "ASDPHEALICKSERVHSKSVESNIEDKIFGKTYRKKASLPNLSHVTENLIIGAFVTEPQIIQERPLTNKLKR\n"
95             + "KRRPTSGLHPEDFIKKADLAVQKTPEMINQGTNQTEQNGQVMNITNSGHENKTKGDSIQNEKNPNPIESLEK\n"
96             + "ESAFKTKAEPISSSISNMELELNIHNSKAPKKNRLRRKSSTRHIHALELVVSRNLSPPNCTELQIDSCSSSE\n"
97             + "EIKKKKYNQMPVRHSRNLQLMEGKEPATGAKKSNKPNEQTSKRHDSDTFPELKLTNAPGSFTKCSNTSELKE\n"
98             + "FVNPSLPREEKEEKLETVKVSNNAEDPKDLMLSGERVLQTERSVESSSISLVPGTDYGTQESISLLEVSTLG\n"
99             + "KAKTEPNKCVSQCAAFENPKGLIHGCSKDNRNDTEGFKYPLGHEVNHSRETSIEMEESELDAQYLQNTFKVS\n"
100             + "KRQSFAPFSNPGNAEEECATFSAHSGSLKKQSPKVTFECEQKEENQGKNESNIKPVQTVNITAGFPVVGQKD\n"
101             + "KPVDNAKCSIKGGSRFCLSSQFRGNETGLITPNKHGLLQNPYRIPPLFPIKSFVKTKCKKNLLEENFEEHSM\n"
102             + "SPEREMGNENIPSTVSTISRNNIRENVFKEASSSNINEVGSSTNEVGSSINEIGSSDENIQAELGRNRGPKL\n"
103             + "NAMLRLGVLQPEVYKQSLPGSNCKHPEIKKQEYEEVVQTVNTDFSPYLISDNLEQPMGSSHASQVCSETPDD\n"
104             + "LLDDGEIKEDTSFAENDIKESSAVFSKSVQKGELSRSPSPFTHTHLAQGYRRGAKKLESSEENLSSEDEELP\n"
105             + "CFQHLLFGKVNNIPSQSTRHSTVATECLSKNTEENLLSLKNSLNDCSNQVILAKASQEHHLSEETKCSASLF\n"
106             + "SSQCSELEDLTANTNTQDPFLIGSSKQMRHQSESQGVGLSDKELVSDDEERGTGLEENNQEEQSMDSNLGEA\n"
107             + "ASGCESETSVSEDCSGLSSQSDILTTQQRDTMQHNLIKLQQEMAELEAVLEQHGSQPSNSYPSIISDSSALE\n"
108             + "DLRNPEQSTSEKAVLTSQKSSEYPISQNPEGLSADKFEVSADSSTSKNKEPGVERSSPSKCPSLDDRWYMHS\n"
109             + "CSGSLQNRNYPSQEELIKVVDVEEQQLEESGPHDLTETSYLPRQDLEGTPYLESGISLFSDDPESDPSEDRA\n"
110             + "PESARVGNIPSSTSALKVPQLKVAESAQSPAAAHTTDTAGYNAMEESVSREKPELTASTERVNKRMSMVVSG\n"
111             + "LTPEEFMLVYKFARKHHITLTNLITEETTHVVMKTDAEFVCERTLKYFLGIAGGKWVVSYFWVTQSIKERKM\n"
112             + "LNEHDFEVRGDVVNGRNHQGPKRARESQDRKIFRGLEICCYGPFTNMPTDQLEWMVQLCGASVVKELSSFTL\n"
113             + "GTGVHPIVVVQPDAWTEDNGFHAIGQMCEAPVVTREWVLDSVALYQCQELDTYLIPQIPHSHY\n"
114             + "", 1,
115 1863);
116     upSeq.createDatasetSequence();
117     upSeq.setDescription("Breast cancer type 1 susceptibility protein");
118     upSeq.addDBRef(new DBRefEntry("UNIPROT","0","P38398",null,true));
119   }
120
121 @AfterMethod(alwaysRun = true)
122   public void tearDown() throws Exception
123   {
124     seq = null;
125     upSeq=null;
126   }
127
128   @SuppressWarnings("deprecation")
129   @Test(groups = { "Functional" })
130   public void buildPDBQueryTest()
131   {
132     System.out.println("seq >>>> " + seq);
133     
134     StructureChooserQuerySource scquery = StructureChooserQuerySource.getQuerySourceFor(new SequenceI[] { seq});
135     AssertJUnit.assertTrue(scquery instanceof PDBStructureChooserQuerySource);
136     String query = scquery.buildQuery(seq);
137     AssertJUnit.assertEquals("pdb_id:1tim", query);
138     seq.getAllPDBEntries().clear();
139     query = scquery.buildQuery(seq);
140     AssertJUnit.assertEquals(
141             "text:XYZ_1 OR text:XYZ_2 OR text:XYZ_3 OR text:XYZ_4 OR text:4kqy",
142             query);
143     seq.setDBRefs(null);
144     query = scquery.buildQuery(seq);
145     System.out.println(query);
146     AssertJUnit.assertEquals("text:4kqy", query);
147
148     DBRefEntry uniprotDBRef = new DBRefEntry();
149     uniprotDBRef.setAccessionId("P12345");
150     uniprotDBRef.setSource(DBRefSource.UNIPROT);
151     seq.addDBRef(uniprotDBRef);
152
153     DBRefEntry pdbDBRef = new DBRefEntry();
154     pdbDBRef.setAccessionId("1XYZ");
155     pdbDBRef.setSource(DBRefSource.PDB);
156     seq.addDBRef(pdbDBRef);
157
158     for (int x = 1; x < 5; x++)
159     {
160       DBRefEntry dbRef = new DBRefEntry();
161       dbRef.setAccessionId("XYZ_" + x);
162       seq.addDBRef(dbRef);
163     }
164     System.out.println("");
165     System.out.println(seq.getDBRefs());
166     System.out.println(query);
167     query = scquery.buildQuery(seq);
168     AssertJUnit.assertEquals(
169             "uniprot_accession:P12345 OR uniprot_id:P12345 OR pdb_id:1xyz",
170             query);
171   }
172
173   @SuppressWarnings("deprecation")
174   @Test(groups = { "Functional" })
175   public void buildThreeDBQueryTest()
176   {
177     System.out.println("seq >>>> " + upSeq);
178     TDBeaconsFTSRestClientTest.setMock();
179     PDBFTSRestClient.setMock();
180     StructureChooserQuerySource scquery = StructureChooserQuerySource.getQuerySourceFor(new SequenceI[] { upSeq});
181     // gets the lightweight proxy rather than the ThreeDBStructureChooserQuerySource
182     AssertJUnit.assertTrue(scquery instanceof ThreeDBStructureChooserQuerySource);
183     String query = scquery.buildQuery(upSeq);
184     AssertJUnit.assertEquals("P38398", query);
185     
186     // query shouldn't change regardless of additional entries
187     // because 3DBeacons requires canonical entries.
188     upSeq.getAllPDBEntries().clear();
189     query = scquery.buildQuery(upSeq);
190     AssertJUnit.assertEquals("P38398", query);
191     upSeq.setDBRefs(null);
192     query = scquery.buildQuery(upSeq);
193     /*
194      * legacy projects/datasets will not have canonical flags set for uniprot dbrefs
195      * graceful behaviour would be to
196      *  - pick one ? not possible
197      *  - iterate through all until a 200 is obtained ?
198      *  ---> ideal but could be costly
199      *  ---> better to do a direct retrieval from uniprot to work out which is the canonical identifier..
200      *  ----> need a test to check that accessions can be promoted to canonical!
201      */
202     //FIXME - need to be able to use ID to query here ?
203     AssertJUnit.assertEquals(null, query);
204
205     
206     
207     // TODO: 
208     /**
209      * set of sequences:
210      * - no protein -> TDB not applicable, query PDBe only (consider RNA or DNA - specific query adapter ?)
211      * - protein but no uniprot -> first consider trying to get uniprot refs (need a mark to say none are available)
212      * - protein and uniprot - no canonicals -> resolve to uniprot automatically to get canonicals
213      * - query uniprot against 3DBeacons
214      * --> decorate experimental structures with additional data from PDBe
215      * - query remaining against PDBe
216      * Ranking
217      * - 3D Beacons
218      *  --> in memory ranking - no need to query twice
219      *  Rank by
220      *  - experimental > AlphaFold -> Model
221      *  - start > end
222      *  -> filters for 
223      *  -> experimental only
224      *  -> experimental plus best models for other regions
225      *  -> "best cover" 
226      *  -> need to be able to select correct reference (the longest one that covers all) for superposition
227      */
228 //    
229 //    DBRefEntry uniprotDBRef = new DBRefEntry();
230 //    uniprotDBRef.setAccessionId("P12345");
231 //    uniprotDBRef.setSource(DBRefSource.UNIPROT);
232 //    upSeq.addDBRef(uniprotDBRef);
233 //
234 //    DBRefEntry pdbDBRef = new DBRefEntry();
235 //    pdbDBRef.setAccessionId("1XYZ");
236 //    pdbDBRef.setSource(DBRefSource.PDB);
237 //    upSeq.addDBRef(pdbDBRef);
238 //
239 //    for (int x = 1; x < 5; x++)
240 //    {
241 //      DBRefEntry dbRef = new DBRefEntry();
242 //      dbRef.setAccessionId("XYZ_" + x);
243 //      seq.addDBRef(dbRef);
244 //    }
245 //    System.out.println("");
246 //    System.out.println(seq.getDBRefs());
247 //    System.out.println(query);
248 //    query = scquery.buildQuery(seq);
249 //    assertEquals(
250 //            "uniprot_accession:P12345 OR uniprot_id:P12345 OR pdb_id:1xyz",
251 //            query);
252   }
253   @Test(groups= {"Functional"})
254   public void cascadingThreeDBandPDBQuerys()
255   {
256     TDBeaconsFTSRestClientTest.setMock();
257     PDBFTSRestClient.setMock();
258     ThreeDBStructureChooserQuerySource tdbquery = new ThreeDBStructureChooserQuerySource();
259     PDBStructureChooserQuerySource pdbquery  = new PDBStructureChooserQuerySource();
260             
261
262     
263     FTSRestResponse upResponse = null;
264     FTSRestResponse pdbResponse = null;
265     // TODO test available options
266     // Best coverage
267     // Best Alphafold Model
268     // Best model (by confidence score)
269     // Will also need to develop a more sophisticated filtering system
270     FilterOption opt = tdbquery.getAvailableFilterOptions(StructureChooser.VIEWS_FILTER).get(0);
271     try {
272       upResponse = tdbquery.fetchStructuresMetaData(upSeq, tdbquery.getDocFieldPrefs().getStructureSummaryFields(),  opt, false);
273       // test ranking without additional PDBe data
274       FTSRestResponse firstRanked = tdbquery.selectFirstRankedQuery(upSeq, upResponse.getSearchSummary(), tdbquery.getDocFieldPrefs().getStructureSummaryFields(), opt.getValue(), false);
275       assertTrue(firstRanked.getNumberOfItemsFound()<upResponse.getNumberOfItemsFound());
276       // NB Could have race condition here 
277       String pdb_Query = tdbquery.buildPDBFTSQueryFor(upResponse);
278       assertTrue(pdb_Query.trim().length()>0);
279       pdbResponse = tdbquery.fetchStructuresMetaDataFor(pdbquery, upResponse);
280       assertTrue(pdbResponse.getNumberOfItemsFound()>0);
281       FTSRestResponse joinedResp = tdbquery.joinResponses(upResponse, pdbResponse);
282       assertEquals(upResponse.getNumberOfItemsFound(),joinedResp.getNumberOfItemsFound());
283       
284       
285     } catch (Exception x)
286     {
287       x.printStackTrace();
288       Assert.fail("Unexpected Exception");
289     }
290     StructureChooserQuerySource scquery = StructureChooserQuerySource.getQuerySourceFor(new SequenceI[] { upSeq});
291
292   }
293   
294   @Test(groups = { "Functional" })
295   public void sanitizeSeqNameTest()
296   {
297     String name = "ab_cdEF|fwxyz012349";
298     AssertJUnit.assertEquals(name, PDBStructureChooserQuerySource.sanitizeSeqName(name));
299
300     // remove a [nn] substring
301     name = "abcde12[345]fg";
302     AssertJUnit.assertEquals("abcde12fg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
303
304     // remove characters other than a-zA-Z0-9 | or _
305     name = "ab[cd],.\t£$*!- \\\"@:e";
306     AssertJUnit.assertEquals("abcde", PDBStructureChooserQuerySource.sanitizeSeqName(name));
307
308     name = "abcde12[345a]fg";
309     AssertJUnit.assertEquals("abcde12345afg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
310   }
311 }