16dfa6bf59e59342944597051762608e194e54da
[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.assertTrue;
25
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.DBRefSource;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceI;
31
32 import java.util.Vector;
33
34 import org.testng.annotations.AfterMethod;
35 import org.testng.annotations.BeforeMethod;
36 import org.testng.annotations.Test;
37
38 public class StructureChooserTest
39 {
40   Sequence seq;
41
42   @BeforeMethod(alwaysRun = true)
43   public void setUp() throws Exception
44   {
45     seq = new Sequence("PDB|4kqy|4KQY|A", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1,
46             26);
47     seq.createDatasetSequence();
48     for (int x = 1; x < 5; x++)
49     {
50       DBRefEntry dbRef = new DBRefEntry();
51       dbRef.setAccessionId("XYZ_" + x);
52       seq.addDBRef(dbRef);
53     }
54
55     PDBEntry dbRef = new PDBEntry();
56     dbRef.setId("1tim");
57
58     Vector<PDBEntry> pdbIds = new Vector<PDBEntry>();
59     pdbIds.add(dbRef);
60
61     seq.setPDBId(pdbIds);
62   }
63
64   @AfterMethod(alwaysRun = true)
65   public void tearDown() throws Exception
66   {
67     seq = null;
68   }
69
70   @Test(groups = { "Functional" })
71   public void buildQueryTest()
72   {
73     String query = StructureChooser.buildQuery(seq);
74     assertEquals("pdb_id:1tim", query);
75     System.out.println("seq >>>> " + seq);
76     seq.getAllPDBEntries().clear();
77     query = StructureChooser.buildQuery(seq);
78     assertEquals(
79             "text:XYZ_1 OR text:XYZ_2 OR text:XYZ_3 OR text:XYZ_4 OR text:4kqy",
80             query);
81     seq.setDBRefs(null);
82     query = StructureChooser.buildQuery(seq);
83     assertEquals("text:4kqy", query);
84
85     DBRefEntry uniprotDBRef = new DBRefEntry();
86     uniprotDBRef.setAccessionId("P12345");
87     uniprotDBRef.setSource(DBRefSource.UNIPROT);
88     seq.addDBRef(uniprotDBRef);
89
90     DBRefEntry pdbDBRef = new DBRefEntry();
91     pdbDBRef.setAccessionId("1XYZ");
92     pdbDBRef.setSource(DBRefSource.PDB);
93     seq.addDBRef(pdbDBRef);
94
95     for (int x = 1; x < 5; x++)
96     {
97       DBRefEntry dbRef = new DBRefEntry();
98       dbRef.setAccessionId("XYZ_" + x);
99       seq.addDBRef(dbRef);
100     }
101     query = StructureChooser.buildQuery(seq);
102     assertEquals(
103             "uniprot_accession:P12345 OR uniprot_id:P12345 OR pdb_id:1xyz",
104             query);
105   }
106
107   @Test(groups = { "Functional" })
108   public void populateFilterComboBoxTest()
109   {
110     SequenceI[] selectedSeqs = new SequenceI[] { seq };
111     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
112     sc.populateFilterComboBox();
113     int optionsSize = sc.getCmbFilterOption().getItemCount();
114     assertEquals(3, optionsSize); // if structures are not discovered then don't
115                                   // populate filter options
116
117     sc.setStructuresDiscovered(true);
118     sc.populateFilterComboBox();
119     try
120     {
121       Thread.sleep(2000);
122     } catch (InterruptedException e)
123     {
124       e.printStackTrace();
125     }
126     optionsSize = sc.getCmbFilterOption().getItemCount();
127     assertTrue(optionsSize > 3); // if structures are found, filter options
128                                  // should be populated
129   }
130
131   @Test(groups = { "Functional" })
132   public void fetchStructuresInfoTest()
133   {
134     SequenceI[] selectedSeqs = new SequenceI[] { seq };
135     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
136     sc.fetchStructuresMetaData();
137     assertTrue(sc.getDiscoveredStructuresSet() != null);
138     assertTrue(sc.getDiscoveredStructuresSet().size() > 0);
139
140   }
141
142   @Test(groups = { "Functional" })
143   public void sanitizeSeqNameTest()
144   {
145     String name = "ab_cdEF|fwxyz012349";
146     assertEquals(name, StructureChooser.sanitizeSeqName(name));
147
148     // remove a [nn] substring
149     name = "abcde12[345]fg";
150     assertEquals("abcde12fg", StructureChooser.sanitizeSeqName(name));
151
152     // remove characters other than a-zA-Z0-9 | or _
153     name = "ab[cd],.\t£$*!- \\\"@:e";
154     assertEquals("abcde", StructureChooser.sanitizeSeqName(name));
155
156     name = "abcde12[345a]fg";
157     assertEquals("abcde12345afg", StructureChooser.sanitizeSeqName(name));
158   }
159 }