Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[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.Assert.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27
28 import java.util.Collection;
29 import java.util.Vector;
30
31 import org.junit.Assert;
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.PDBEntry;
39 import jalview.datamodel.Sequence;
40 import jalview.datamodel.SequenceI;
41 import jalview.fts.api.FTSData;
42 import jalview.fts.core.FTSRestClient;
43 import jalview.fts.service.pdb.PDBFTSRestClient;
44 import jalview.fts.service.pdb.PDBFTSRestClientTest;
45 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
46 import jalview.fts.threedbeacons.TDBeaconsFTSRestClientTest;
47 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
48 import jalview.jbgui.FilterOption;
49 import junit.extensions.PA;
50
51 @Test(singleThreaded = true)
52 public class StructureChooserTest
53 {
54
55   @BeforeClass(alwaysRun = true)
56   public void setUpJvOptionPane()
57   {
58     JvOptionPane.setInteractiveMode(false);
59     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
60   }
61
62   Sequence seq, upSeq, upSeq_nocanonical;
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                     + "",
114             1, 1863);
115     upSeq.setDescription("Breast cancer type 1 susceptibility protein");
116     upSeq_nocanonical = new Sequence(upSeq);
117     upSeq.createDatasetSequence();
118     upSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "P38398", null, true));
119
120     upSeq_nocanonical.createDatasetSequence();
121     // not a canonical reference
122     upSeq_nocanonical.addDBRef(new DBRefEntry("UNIPROT","0","P38398",null,false));
123   }
124
125   @AfterMethod(alwaysRun = true)
126   public void tearDown() throws Exception
127   {
128     seq = null;
129     upSeq = null;
130     upSeq_nocanonical = null;
131   }
132
133   @Test(groups = { "Functional" })
134   public void populateFilterComboBoxTest() throws InterruptedException
135   {
136     TDBeaconsFTSRestClientTest.setMock();
137     PDBFTSRestClientTest.setMock();
138
139     SequenceI[] selectedSeqs = new SequenceI[] { seq };
140     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
141     ThreadwaitFor(200, sc);
142
143     // if structures are not discovered then don't
144     // populate filter options
145     sc.populateFilterComboBox(false, false);
146     int optionsSize = sc.getCmbFilterOption().getItemCount();
147     System.out.println("Items (no data, no cache): ");
148     StringBuilder items = new StringBuilder();
149     for (int p = 0; p < optionsSize; p++)
150     {
151       items.append("- ")
152               .append(sc.getCmbFilterOption().getItemAt(p).getName())
153               .append("\n");
154
155     }
156     // report items when this fails - seems to be a race condition
157     Assert.assertEquals(items.toString(), optionsSize, 2);
158
159     sc.populateFilterComboBox(true, false);
160     optionsSize = sc.getCmbFilterOption().getItemCount();
161     assertTrue(optionsSize > 3); // if structures are found, filter options
162                                  // should be populated
163
164     sc.populateFilterComboBox(true, true);
165     assertTrue(sc.getCmbFilterOption().getSelectedItem() != null);
166     FilterOption filterOpt = (FilterOption) sc.getCmbFilterOption()
167             .getSelectedItem();
168     assertEquals("Cached Structures", filterOpt.getName());
169     FTSRestClient
170             .unMock((FTSRestClient) TDBeaconsFTSRestClient.getInstance());
171     FTSRestClient.unMock((FTSRestClient) PDBFTSRestClient.getInstance());
172
173   }
174
175   @Test(groups = { "Functional" })
176   public void displayTDBQueryTest() throws InterruptedException
177   {
178     TDBeaconsFTSRestClientTest.setMock();
179     PDBFTSRestClientTest.setMock();
180
181     SequenceI[] selectedSeqs = new SequenceI[] { upSeq_nocanonical };
182     StructureChooser sc = new StructureChooser(selectedSeqs,
183             upSeq_nocanonical, null);
184     // mock so should be quick. Exceptions from mocked PDBFTS are expected too
185     ThreadwaitFor(500, sc);
186
187     assertTrue(sc.isCanQueryTDB() && sc.isNotQueriedTDBYet());
188   }
189
190   @Test(groups = { "Network" })
191   public void fetchStructuresInfoTest()
192   {
193     FTSRestClient
194             .unMock((FTSRestClient) TDBeaconsFTSRestClient.getInstance());
195     PDBFTSRestClient.unMock((FTSRestClient) PDBFTSRestClient.getInstance());
196     SequenceI[] selectedSeqs = new SequenceI[] { seq };
197     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
198     // not mocked, wait for 2s
199     ThreadwaitFor(2000, sc);
200
201     sc.fetchStructuresMetaData();
202     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
203             "discoveredStructuresSet");
204     assertNotNull(ss);
205     assertTrue(ss.size() > 0);
206   }
207
208   @Test(groups = { "Functional" })
209   public void fetchStructuresInfoMockedTest()
210   {
211     TDBeaconsFTSRestClientTest.setMock();
212     PDBFTSRestClientTest.setMock();
213     SequenceI[] selectedSeqs = new SequenceI[] { upSeq };
214     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
215     ThreadwaitFor(500, sc);
216
217     sc.fetchStructuresMetaData();
218     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
219             "discoveredStructuresSet");
220     assertNotNull(ss);
221     assertTrue(ss.size() > 0);
222   }
223
224   private void ThreadwaitFor(int i, StructureChooser sc)
225   {
226     long timeout = i + System.currentTimeMillis();
227     while (!sc.isDialogVisible() && timeout > System.currentTimeMillis())
228     {
229       try
230       {
231         Thread.sleep(50);
232       } catch (InterruptedException x)
233       {
234
235       }
236     }
237   }
238   @Test(groups = { "Functional" })
239   public void sanitizeSeqNameTest()
240   {
241     String name = "ab_cdEF|fwxyz012349";
242     assertEquals(name,
243             PDBStructureChooserQuerySource.sanitizeSeqName(name));
244
245     // remove a [nn] substring
246     name = "abcde12[345]fg";
247     assertEquals("abcde12fg",
248             PDBStructureChooserQuerySource.sanitizeSeqName(name));
249
250     // remove characters other than a-zA-Z0-9 | or _
251     name = "ab[cd],.\t£$*!- \\\"@:e";
252     assertEquals("abcde",
253             PDBStructureChooserQuerySource.sanitizeSeqName(name));
254
255     name = "abcde12[345a]fg";
256     assertEquals("abcde12345afg",
257             PDBStructureChooserQuerySource.sanitizeSeqName(name));
258   }
259 }