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