JAL-3829 JAL-3865 test structure chooser queries to mocked 3D Beacons FTS
[jalview.git] / test / jalview / gui / StructureChooserTest.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;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.datamodel.DBRefEntry;
28 import jalview.datamodel.DBRefSource;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceI;
32 import jalview.fts.api.FTSData;
33 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
34 import jalview.fts.threedbeacons.TDBeaconsFTSRestClientTest;
35 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
36 import jalview.gui.structurechooser.StructureChooserQuerySource;
37 import jalview.gui.structurechooser.ThreeDBStructureChooserQuerySource;
38 import jalview.jbgui.FilterOption;
39 import jalview.ws.params.InvalidArgumentException;
40
41 import java.util.Collection;
42 import java.util.Vector;
43
44 import org.testng.annotations.AfterMethod;
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
48
49 import junit.extensions.PA;
50
51 public class StructureChooserTest
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   @BeforeMethod(alwaysRun = true)
64   public void setUp() throws Exception
65   {
66     seq = new Sequence("PDB|4kqy|4KQY|A", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1,
67             26);
68     seq.createDatasetSequence();
69     for (int x = 1; x < 5; x++)
70     {
71       DBRefEntry dbRef = new DBRefEntry();
72       dbRef.setAccessionId("XYZ_" + x);
73       seq.addDBRef(dbRef);
74     }
75
76     PDBEntry dbRef = new PDBEntry();
77     dbRef.setId("1tim");
78
79     Vector<PDBEntry> pdbIds = new Vector<>();
80     pdbIds.add(dbRef);
81
82     seq.setPDBId(pdbIds);
83     
84     // Uniprot sequence for 3D-Beacons mocks
85     upSeq = new Sequence("P38398", 
86             "MDLSALRVEEVQNVINAMQKILECPICLELIKEPVSTKCDHIFCKFCMLKLLNQKKGPSQCPLCKNDITKRS\n"
87             + "LQESTRFSQLVEELLKIICAFQLDTGLEYANSYNFAKKENNSPEHLKDEVSIIQSMGYRNRAKRLLQSEPEN\n"
88             + "PSLQETSLSVQLSNLGTVRTLRTKQRIQPQKTSVYIELGSDSSEDTVNKATYCSVGDQELLQITPQGTRDEI\n"
89             + "SLDSAKKAACEFSETDVTNTEHHQPSNNDLNTTEKRAAERHPEKYQGSSVSNLHVEPCGTNTHASSLQHENS\n"
90             + "SLLLTKDRMNVEKAEFCNKSKQPGLARSQHNRWAGSKETCNDRRTPSTEKKVDLNADPLCERKEWNKQKLPC\n"
91             + "SENPRDTEDVPWITLNSSIQKVNEWFSRSDELLGSDDSHDGESESNAKVADVLDVLNEVDEYSGSSEKIDLL\n"
92             + "ASDPHEALICKSERVHSKSVESNIEDKIFGKTYRKKASLPNLSHVTENLIIGAFVTEPQIIQERPLTNKLKR\n"
93             + "KRRPTSGLHPEDFIKKADLAVQKTPEMINQGTNQTEQNGQVMNITNSGHENKTKGDSIQNEKNPNPIESLEK\n"
94             + "ESAFKTKAEPISSSISNMELELNIHNSKAPKKNRLRRKSSTRHIHALELVVSRNLSPPNCTELQIDSCSSSE\n"
95             + "EIKKKKYNQMPVRHSRNLQLMEGKEPATGAKKSNKPNEQTSKRHDSDTFPELKLTNAPGSFTKCSNTSELKE\n"
96             + "FVNPSLPREEKEEKLETVKVSNNAEDPKDLMLSGERVLQTERSVESSSISLVPGTDYGTQESISLLEVSTLG\n"
97             + "KAKTEPNKCVSQCAAFENPKGLIHGCSKDNRNDTEGFKYPLGHEVNHSRETSIEMEESELDAQYLQNTFKVS\n"
98             + "KRQSFAPFSNPGNAEEECATFSAHSGSLKKQSPKVTFECEQKEENQGKNESNIKPVQTVNITAGFPVVGQKD\n"
99             + "KPVDNAKCSIKGGSRFCLSSQFRGNETGLITPNKHGLLQNPYRIPPLFPIKSFVKTKCKKNLLEENFEEHSM\n"
100             + "SPEREMGNENIPSTVSTISRNNIRENVFKEASSSNINEVGSSTNEVGSSINEIGSSDENIQAELGRNRGPKL\n"
101             + "NAMLRLGVLQPEVYKQSLPGSNCKHPEIKKQEYEEVVQTVNTDFSPYLISDNLEQPMGSSHASQVCSETPDD\n"
102             + "LLDDGEIKEDTSFAENDIKESSAVFSKSVQKGELSRSPSPFTHTHLAQGYRRGAKKLESSEENLSSEDEELP\n"
103             + "CFQHLLFGKVNNIPSQSTRHSTVATECLSKNTEENLLSLKNSLNDCSNQVILAKASQEHHLSEETKCSASLF\n"
104             + "SSQCSELEDLTANTNTQDPFLIGSSKQMRHQSESQGVGLSDKELVSDDEERGTGLEENNQEEQSMDSNLGEA\n"
105             + "ASGCESETSVSEDCSGLSSQSDILTTQQRDTMQHNLIKLQQEMAELEAVLEQHGSQPSNSYPSIISDSSALE\n"
106             + "DLRNPEQSTSEKAVLTSQKSSEYPISQNPEGLSADKFEVSADSSTSKNKEPGVERSSPSKCPSLDDRWYMHS\n"
107             + "CSGSLQNRNYPSQEELIKVVDVEEQQLEESGPHDLTETSYLPRQDLEGTPYLESGISLFSDDPESDPSEDRA\n"
108             + "PESARVGNIPSSTSALKVPQLKVAESAQSPAAAHTTDTAGYNAMEESVSREKPELTASTERVNKRMSMVVSG\n"
109             + "LTPEEFMLVYKFARKHHITLTNLITEETTHVVMKTDAEFVCERTLKYFLGIAGGKWVVSYFWVTQSIKERKM\n"
110             + "LNEHDFEVRGDVVNGRNHQGPKRARESQDRKIFRGLEICCYGPFTNMPTDQLEWMVQLCGASVVKELSSFTL\n"
111             + "GTGVHPIVVVQPDAWTEDNGFHAIGQMCEAPVVTREWVLDSVALYQCQELDTYLIPQIPHSHY\n"
112             + "", 1,
113 1863);
114     upSeq.createDatasetSequence();
115     upSeq.setDescription("Breast cancer type 1 susceptibility protein");
116     upSeq.addDBRef(new DBRefEntry("UNIPROT","0","P38398",null,true));
117   }
118
119   @AfterMethod(alwaysRun = true)
120   public void tearDown() throws Exception
121   {
122     seq = null;
123     upSeq=null;
124   }
125
126   @SuppressWarnings("deprecation")
127   @Test(groups = { "Functional" })
128   public void buildQueryTest()
129   {
130     System.out.println("seq >>>> " + seq);
131     
132     StructureChooserQuerySource scquery = StructureChooserQuerySource.getQuerySourceFor(new SequenceI[] { seq});
133     assertTrue(scquery instanceof PDBStructureChooserQuerySource);
134     String query = scquery.buildQuery(seq);
135     assertEquals("pdb_id:1tim", query);
136     seq.getAllPDBEntries().clear();
137     query = scquery.buildQuery(seq);
138     assertEquals(
139             "text:XYZ_1 OR text:XYZ_2 OR text:XYZ_3 OR text:XYZ_4 OR text:4kqy",
140             query);
141     seq.setDBRefs(null);
142     query = scquery.buildQuery(seq);
143     System.out.println(query);
144     assertEquals("text:4kqy", query);
145
146     DBRefEntry uniprotDBRef = new DBRefEntry();
147     uniprotDBRef.setAccessionId("P12345");
148     uniprotDBRef.setSource(DBRefSource.UNIPROT);
149     seq.addDBRef(uniprotDBRef);
150
151     DBRefEntry pdbDBRef = new DBRefEntry();
152     pdbDBRef.setAccessionId("1XYZ");
153     pdbDBRef.setSource(DBRefSource.PDB);
154     seq.addDBRef(pdbDBRef);
155
156     for (int x = 1; x < 5; x++)
157     {
158       DBRefEntry dbRef = new DBRefEntry();
159       dbRef.setAccessionId("XYZ_" + x);
160       seq.addDBRef(dbRef);
161     }
162     System.out.println("");
163     System.out.println(seq.getDBRefs());
164     System.out.println(query);
165     query = scquery.buildQuery(seq);
166     assertEquals(
167             "uniprot_accession:P12345 OR uniprot_id:P12345 OR pdb_id:1xyz",
168             query);
169   }
170
171   @SuppressWarnings("deprecation")
172   @Test(groups = { "Functional" })
173   public void buildThreeDBQueryTest()
174   {
175     System.out.println("seq >>>> " + upSeq);
176     TDBeaconsFTSRestClientTest.setMock();
177     StructureChooserQuerySource scquery = StructureChooserQuerySource.getQuerySourceFor(new SequenceI[] { upSeq});
178     assertTrue(scquery instanceof ThreeDBStructureChooserQuerySource);
179     String query = scquery.buildQuery(upSeq);
180     assertEquals("P38398", query);
181     
182     // query shouldn't change regardless of additional entries
183     // because 3DBeacons requires canonical entries.
184     upSeq.getAllPDBEntries().clear();
185     query = scquery.buildQuery(upSeq);
186     assertEquals("P38398", query);
187     upSeq.setDBRefs(null);
188     query = scquery.buildQuery(upSeq);
189     /*
190      * legacy projects/datasets will not have canonical flags set for uniprot dbrefs
191      * graceful behaviour would be to
192      *  - pick one ? not possible
193      *  - iterate through all until a 200 is obtained ?
194      *  ---> ideal but could be costly
195      *  ---> better to do a direct retrieval from uniprot to work out which is the canonical identifier..
196      *  ----> need a test to check that accessions can be promoted to canonical!
197      */
198     assertEquals(null, query);
199
200     // TODO: 
201 //    
202 //    DBRefEntry uniprotDBRef = new DBRefEntry();
203 //    uniprotDBRef.setAccessionId("P12345");
204 //    uniprotDBRef.setSource(DBRefSource.UNIPROT);
205 //    upSeq.addDBRef(uniprotDBRef);
206 //
207 //    DBRefEntry pdbDBRef = new DBRefEntry();
208 //    pdbDBRef.setAccessionId("1XYZ");
209 //    pdbDBRef.setSource(DBRefSource.PDB);
210 //    upSeq.addDBRef(pdbDBRef);
211 //
212 //    for (int x = 1; x < 5; x++)
213 //    {
214 //      DBRefEntry dbRef = new DBRefEntry();
215 //      dbRef.setAccessionId("XYZ_" + x);
216 //      seq.addDBRef(dbRef);
217 //    }
218 //    System.out.println("");
219 //    System.out.println(seq.getDBRefs());
220 //    System.out.println(query);
221 //    query = scquery.buildQuery(seq);
222 //    assertEquals(
223 //            "uniprot_accession:P12345 OR uniprot_id:P12345 OR pdb_id:1xyz",
224 //            query);
225   }
226   
227
228   @Test(groups = { "Functional" })
229   public void populateFilterComboBoxTest() throws InterruptedException
230   {
231     SequenceI[] selectedSeqs = new SequenceI[] { seq };
232     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
233     sc.populateFilterComboBox(false, false);
234     int optionsSize = sc.getCmbFilterOption().getItemCount();
235     assertEquals(2, optionsSize); // if structures are not discovered then don't
236                                   // populate filter options
237
238     sc.populateFilterComboBox(true, false);
239     optionsSize = sc.getCmbFilterOption().getItemCount();
240     assertTrue(optionsSize > 3); // if structures are found, filter options
241                                  // should be populated
242
243     sc.populateFilterComboBox(true, true);
244     assertTrue(sc.getCmbFilterOption().getSelectedItem() != null);
245     FilterOption filterOpt = (FilterOption) sc.getCmbFilterOption()
246             .getSelectedItem();
247     assertEquals("Cached Structures", filterOpt.getName());
248   }
249
250   @Test(groups = { "Network" })
251   public void fetchStructuresInfoTest()
252   {
253     SequenceI[] selectedSeqs = new SequenceI[] { seq };
254     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
255     sc.fetchStructuresMetaData();
256     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
257             "discoveredStructuresSet");
258     assertNotNull(ss);
259     assertTrue(ss.size() > 0);
260
261   }
262
263   @Test(groups = { "Functional" })
264   public void sanitizeSeqNameTest()
265   {
266     String name = "ab_cdEF|fwxyz012349";
267     assertEquals(name, PDBStructureChooserQuerySource.sanitizeSeqName(name));
268
269     // remove a [nn] substring
270     name = "abcde12[345]fg";
271     assertEquals("abcde12fg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
272
273     // remove characters other than a-zA-Z0-9 | or _
274     name = "ab[cd],.\t£$*!- \\\"@:e";
275     assertEquals("abcde", PDBStructureChooserQuerySource.sanitizeSeqName(name));
276
277     name = "abcde12[345a]fg";
278     assertEquals("abcde12345afg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
279   }
280 }