JAL-3829 insert title for structures from PDBe
[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     PDBFTSRestClient.setMock();
179     StructureChooserQuerySource scquery = StructureChooserQuerySource.getQuerySourceFor(new SequenceI[] { upSeq});
180     // gets the lightweight proxy rather than the ThreeDBStructureChooserQuerySource
181     AssertJUnit.assertTrue(scquery instanceof ThreeDBStructureChooserQuerySource);
182     String query = scquery.buildQuery(upSeq);
183     AssertJUnit.assertEquals("P38398", query);
184     
185     // query shouldn't change regardless of additional entries
186     // because 3DBeacons requires canonical entries.
187     upSeq.getAllPDBEntries().clear();
188     query = scquery.buildQuery(upSeq);
189     AssertJUnit.assertEquals("P38398", query);
190     upSeq.setDBRefs(null);
191     query = scquery.buildQuery(upSeq);
192     /*
193      * legacy projects/datasets will not have canonical flags set for uniprot dbrefs
194      * graceful behaviour would be to
195      *  - pick one ? not possible
196      *  - iterate through all until a 200 is obtained ?
197      *  ---> ideal but could be costly
198      *  ---> better to do a direct retrieval from uniprot to work out which is the canonical identifier..
199      *  ----> need a test to check that accessions can be promoted to canonical!
200      */
201     //FIXME - need to be able to use ID to query here ?
202     AssertJUnit.assertEquals(null, query);
203
204     
205     
206     // TODO: 
207     /**
208      * set of sequences:
209      * - no protein -> TDB not applicable, query PDBe only (consider RNA or DNA - specific query adapter ?)
210      * - protein but no uniprot -> first consider trying to get uniprot refs (need a mark to say none are available)
211      * - protein and uniprot - no canonicals -> resolve to uniprot automatically to get canonicals
212      * - query uniprot against 3DBeacons
213      * --> decorate experimental structures with additional data from PDBe
214      * - query remaining against PDBe
215      * Ranking
216      * - 3D Beacons
217      *  --> in memory ranking - no need to query twice
218      *  Rank by
219      *  - experimental > AlphaFold -> Model
220      *  - start > end
221      *  -> filters for 
222      *  -> experimental only
223      *  -> experimental plus best models for other regions
224      *  -> "best cover" 
225      *  -> need to be able to select correct reference (the longest one that covers all) for superposition
226      */
227 //    
228 //    DBRefEntry uniprotDBRef = new DBRefEntry();
229 //    uniprotDBRef.setAccessionId("P12345");
230 //    uniprotDBRef.setSource(DBRefSource.UNIPROT);
231 //    upSeq.addDBRef(uniprotDBRef);
232 //
233 //    DBRefEntry pdbDBRef = new DBRefEntry();
234 //    pdbDBRef.setAccessionId("1XYZ");
235 //    pdbDBRef.setSource(DBRefSource.PDB);
236 //    upSeq.addDBRef(pdbDBRef);
237 //
238 //    for (int x = 1; x < 5; x++)
239 //    {
240 //      DBRefEntry dbRef = new DBRefEntry();
241 //      dbRef.setAccessionId("XYZ_" + x);
242 //      seq.addDBRef(dbRef);
243 //    }
244 //    System.out.println("");
245 //    System.out.println(seq.getDBRefs());
246 //    System.out.println(query);
247 //    query = scquery.buildQuery(seq);
248 //    assertEquals(
249 //            "uniprot_accession:P12345 OR uniprot_id:P12345 OR pdb_id:1xyz",
250 //            query);
251   }
252   @Test(groups= {"Functional"})
253   public void cascadingThreeDBandPDBQuerys()
254   {
255     TDBeaconsFTSRestClientTest.setMock();
256     PDBFTSRestClient.setMock();
257     ThreeDBStructureChooserQuerySource tdbquery = new ThreeDBStructureChooserQuerySource();
258     PDBStructureChooserQuerySource pdbquery  = new PDBStructureChooserQuerySource();
259             
260
261     
262     FTSRestResponse upResponse = null;
263     FTSRestResponse pdbResponse = null;
264     
265     try {
266       upResponse = tdbquery.fetchStructuresMetaData(upSeq, tdbquery.getDocFieldPrefs().getStructureSummaryFields(),  null, false);
267       // test ranking without additional PDBe data
268       FTSRestResponse firstRanked = tdbquery.selectFirstRankedQuery(upSeq, upResponse.getSearchSummary(), tdbquery.getDocFieldPrefs().getStructureSummaryFields(), "", false);
269       assertTrue(firstRanked.getNumberOfItemsFound()<upResponse.getNumberOfItemsFound());
270       // NB Could have race condition here 
271       String pdb_Query = tdbquery.buildPDBFTSQueryFor(upResponse);
272       assertTrue(pdb_Query.trim().length()>0);
273       pdbResponse = tdbquery.fetchStructuresMetaDataFor(pdbquery, upResponse);
274       assertTrue(pdbResponse.getNumberOfItemsFound()>0);
275       FTSRestResponse joinedResp = tdbquery.joinResponses(upResponse, pdbResponse);
276       assertEquals(upResponse.getNumberOfItemsFound(),joinedResp.getNumberOfItemsFound());
277       
278       
279     } catch (Exception x)
280     {
281       x.printStackTrace();
282       Assert.fail("Unexpected Exception");
283     }
284     StructureChooserQuerySource scquery = StructureChooserQuerySource.getQuerySourceFor(new SequenceI[] { upSeq});
285
286   }
287   
288   @Test(groups = { "Functional" })
289   public void sanitizeSeqNameTest()
290   {
291     String name = "ab_cdEF|fwxyz012349";
292     AssertJUnit.assertEquals(name, PDBStructureChooserQuerySource.sanitizeSeqName(name));
293
294     // remove a [nn] substring
295     name = "abcde12[345]fg";
296     AssertJUnit.assertEquals("abcde12fg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
297
298     // remove characters other than a-zA-Z0-9 | or _
299     name = "ab[cd],.\t£$*!- \\\"@:e";
300     AssertJUnit.assertEquals("abcde", PDBStructureChooserQuerySource.sanitizeSeqName(name));
301
302     name = "abcde12[345a]fg";
303     AssertJUnit.assertEquals("abcde12345afg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
304   }
305 }