57aa23c3e4cbd9cc428bf440705332979adf86c4
[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.assertEquals;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertTrue;
27
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.DBRefSource;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceI;
33 import jalview.fts.api.FTSData;
34 import jalview.fts.api.FTSRestClientI;
35 import jalview.fts.core.FTSRestClient;
36 import jalview.fts.core.FTSRestRequest;
37 import jalview.fts.service.pdb.PDBFTSRestClient;
38 import jalview.fts.service.pdb.PDBFTSRestClientTest;
39 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
40 import jalview.fts.threedbeacons.TDBeaconsFTSRestClientTest;
41 import jalview.gui.structurechooser.PDBStructureChooserQuerySource;
42 import jalview.gui.structurechooser.StructureChooserQuerySource;
43 import jalview.gui.structurechooser.ThreeDBStructureChooserQuerySource;
44 import jalview.jbgui.FilterOption;
45 import jalview.ws.params.InvalidArgumentException;
46
47 import java.util.Collection;
48 import java.util.Vector;
49
50 import javax.swing.JComboBox;
51
52 import org.junit.Assert;
53 import org.testng.annotations.AfterMethod;
54 import org.testng.annotations.BeforeClass;
55 import org.testng.annotations.BeforeMethod;
56 import org.testng.annotations.Test;
57
58 import junit.extensions.PA;
59
60 public class StructureChooserTest
61 {
62
63   @BeforeClass(alwaysRun = true)
64   public void setUpJvOptionPane()
65   {
66     JvOptionPane.setInteractiveMode(false);
67     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
68   }
69
70   Sequence seq,upSeq;
71
72   @BeforeMethod(alwaysRun = true)
73   public void setUp() throws Exception
74   {
75     seq = new Sequence("PDB|4kqy|4KQY|A", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1,
76             26);
77     seq.createDatasetSequence();
78     for (int x = 1; x < 5; x++)
79     {
80       DBRefEntry dbRef = new DBRefEntry();
81       dbRef.setAccessionId("XYZ_" + x);
82       seq.addDBRef(dbRef);
83     }
84
85     PDBEntry dbRef = new PDBEntry();
86     dbRef.setId("1tim");
87
88     Vector<PDBEntry> pdbIds = new Vector<>();
89     pdbIds.add(dbRef);
90
91     seq.setPDBId(pdbIds);
92     
93     // Uniprot sequence for 3D-Beacons mocks
94     upSeq = new Sequence("P38398", 
95             "MDLSALRVEEVQNVINAMQKILECPICLELIKEPVSTKCDHIFCKFCMLKLLNQKKGPSQCPLCKNDITKRS\n"
96             + "LQESTRFSQLVEELLKIICAFQLDTGLEYANSYNFAKKENNSPEHLKDEVSIIQSMGYRNRAKRLLQSEPEN\n"
97             + "PSLQETSLSVQLSNLGTVRTLRTKQRIQPQKTSVYIELGSDSSEDTVNKATYCSVGDQELLQITPQGTRDEI\n"
98             + "SLDSAKKAACEFSETDVTNTEHHQPSNNDLNTTEKRAAERHPEKYQGSSVSNLHVEPCGTNTHASSLQHENS\n"
99             + "SLLLTKDRMNVEKAEFCNKSKQPGLARSQHNRWAGSKETCNDRRTPSTEKKVDLNADPLCERKEWNKQKLPC\n"
100             + "SENPRDTEDVPWITLNSSIQKVNEWFSRSDELLGSDDSHDGESESNAKVADVLDVLNEVDEYSGSSEKIDLL\n"
101             + "ASDPHEALICKSERVHSKSVESNIEDKIFGKTYRKKASLPNLSHVTENLIIGAFVTEPQIIQERPLTNKLKR\n"
102             + "KRRPTSGLHPEDFIKKADLAVQKTPEMINQGTNQTEQNGQVMNITNSGHENKTKGDSIQNEKNPNPIESLEK\n"
103             + "ESAFKTKAEPISSSISNMELELNIHNSKAPKKNRLRRKSSTRHIHALELVVSRNLSPPNCTELQIDSCSSSE\n"
104             + "EIKKKKYNQMPVRHSRNLQLMEGKEPATGAKKSNKPNEQTSKRHDSDTFPELKLTNAPGSFTKCSNTSELKE\n"
105             + "FVNPSLPREEKEEKLETVKVSNNAEDPKDLMLSGERVLQTERSVESSSISLVPGTDYGTQESISLLEVSTLG\n"
106             + "KAKTEPNKCVSQCAAFENPKGLIHGCSKDNRNDTEGFKYPLGHEVNHSRETSIEMEESELDAQYLQNTFKVS\n"
107             + "KRQSFAPFSNPGNAEEECATFSAHSGSLKKQSPKVTFECEQKEENQGKNESNIKPVQTVNITAGFPVVGQKD\n"
108             + "KPVDNAKCSIKGGSRFCLSSQFRGNETGLITPNKHGLLQNPYRIPPLFPIKSFVKTKCKKNLLEENFEEHSM\n"
109             + "SPEREMGNENIPSTVSTISRNNIRENVFKEASSSNINEVGSSTNEVGSSINEIGSSDENIQAELGRNRGPKL\n"
110             + "NAMLRLGVLQPEVYKQSLPGSNCKHPEIKKQEYEEVVQTVNTDFSPYLISDNLEQPMGSSHASQVCSETPDD\n"
111             + "LLDDGEIKEDTSFAENDIKESSAVFSKSVQKGELSRSPSPFTHTHLAQGYRRGAKKLESSEENLSSEDEELP\n"
112             + "CFQHLLFGKVNNIPSQSTRHSTVATECLSKNTEENLLSLKNSLNDCSNQVILAKASQEHHLSEETKCSASLF\n"
113             + "SSQCSELEDLTANTNTQDPFLIGSSKQMRHQSESQGVGLSDKELVSDDEERGTGLEENNQEEQSMDSNLGEA\n"
114             + "ASGCESETSVSEDCSGLSSQSDILTTQQRDTMQHNLIKLQQEMAELEAVLEQHGSQPSNSYPSIISDSSALE\n"
115             + "DLRNPEQSTSEKAVLTSQKSSEYPISQNPEGLSADKFEVSADSSTSKNKEPGVERSSPSKCPSLDDRWYMHS\n"
116             + "CSGSLQNRNYPSQEELIKVVDVEEQQLEESGPHDLTETSYLPRQDLEGTPYLESGISLFSDDPESDPSEDRA\n"
117             + "PESARVGNIPSSTSALKVPQLKVAESAQSPAAAHTTDTAGYNAMEESVSREKPELTASTERVNKRMSMVVSG\n"
118             + "LTPEEFMLVYKFARKHHITLTNLITEETTHVVMKTDAEFVCERTLKYFLGIAGGKWVVSYFWVTQSIKERKM\n"
119             + "LNEHDFEVRGDVVNGRNHQGPKRARESQDRKIFRGLEICCYGPFTNMPTDQLEWMVQLCGASVVKELSSFTL\n"
120             + "GTGVHPIVVVQPDAWTEDNGFHAIGQMCEAPVVTREWVLDSVALYQCQELDTYLIPQIPHSHY\n"
121             + "", 1,
122 1863);
123     upSeq.createDatasetSequence();
124     upSeq.setDescription("Breast cancer type 1 susceptibility protein");
125     upSeq.addDBRef(new DBRefEntry("UNIPROT","0","P38398",null,true));
126   }
127
128   @AfterMethod(alwaysRun = true)
129   public void tearDown() throws Exception
130   {
131     seq = null;
132     upSeq=null;
133   }
134
135   @Test(groups = { "Functional" })
136   public void populateFilterComboBoxTest() throws InterruptedException
137   {
138     TDBeaconsFTSRestClientTest.setMock();
139     PDBFTSRestClientTest.setMock();
140
141     SequenceI[] selectedSeqs = new SequenceI[] { seq };
142     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
143     
144     // if structures are not discovered then don't
145     // populate filter options
146     sc.populateFilterComboBox(false, false);
147     int optionsSize = sc.getCmbFilterOption().getItemCount();
148     System.out.println("Items (no data, no cache): ");
149     StringBuilder items = new StringBuilder();
150     for (int p=0;p<optionsSize;p++)
151     {
152       items.append
153       ("- ").append(sc.getCmbFilterOption().getItemAt(p).getName()).append("\n");
154
155     }
156     // report items when this fails - seems to be a race condition
157     assertEquals(optionsSize,2,items.toString()); 
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.unMock((FTSRestClient) TDBeaconsFTSRestClient.getInstance());
170     FTSRestClient.unMock((FTSRestClient) PDBFTSRestClient.getInstance());
171
172   }
173
174   @Test(groups = { "Network" })
175   public void fetchStructuresInfoTest()
176   {
177     FTSRestClient.unMock((FTSRestClient) TDBeaconsFTSRestClient.getInstance());
178     PDBFTSRestClient.unMock((FTSRestClient) PDBFTSRestClient.getInstance());
179     SequenceI[] selectedSeqs = new SequenceI[] { seq };
180     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
181     sc.fetchStructuresMetaData();
182     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
183             "discoveredStructuresSet");
184     assertNotNull(ss);
185     assertTrue(ss.size() > 0);
186   }
187
188   @Test(groups = { "Functional" })
189   public void fetchStructuresInfoMockedTest()
190   {
191     TDBeaconsFTSRestClientTest.setMock();
192     PDBFTSRestClientTest.setMock();
193     SequenceI[] selectedSeqs = new SequenceI[] { upSeq };
194     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
195     sc.fetchStructuresMetaData();
196     Collection<FTSData> ss = (Collection<FTSData>) PA.getValue(sc,
197             "discoveredStructuresSet");
198     assertNotNull(ss);
199     assertTrue(ss.size() > 0);
200   }
201
202   @Test(groups = { "Functional" })
203   public void sanitizeSeqNameTest()
204   {
205     String name = "ab_cdEF|fwxyz012349";
206     assertEquals(name, PDBStructureChooserQuerySource.sanitizeSeqName(name));
207
208     // remove a [nn] substring
209     name = "abcde12[345]fg";
210     assertEquals("abcde12fg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
211
212     // remove characters other than a-zA-Z0-9 | or _
213     name = "ab[cd],.\t£$*!- \\\"@:e";
214     assertEquals("abcde", PDBStructureChooserQuerySource.sanitizeSeqName(name));
215
216     name = "abcde12[345a]fg";
217     assertEquals("abcde12345afg", PDBStructureChooserQuerySource.sanitizeSeqName(name));
218   }
219 }