1e41a16146a5987267c6492d755e49ff2048f3d7
[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() throws InterruptedException
109   {
110     SequenceI[] selectedSeqs = new SequenceI[] { seq };
111     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
112     sc.populateFilterComboBox(false);
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.populateFilterComboBox(true);
118     optionsSize = sc.getCmbFilterOption().getItemCount();
119     assertTrue(optionsSize > 3); // if structures are found, filter options
120                                  // should be populated
121   }
122
123   @Test(groups = { "Functional" })
124   public void fetchStructuresInfoTest()
125   {
126     SequenceI[] selectedSeqs = new SequenceI[] { seq };
127     StructureChooser sc = new StructureChooser(selectedSeqs, seq, null);
128     sc.fetchStructuresMetaData();
129     assertTrue(sc.getDiscoveredStructuresSet() != null);
130     assertTrue(sc.getDiscoveredStructuresSet().size() > 0);
131
132   }
133
134   @Test(groups = { "Functional" })
135   public void sanitizeSeqNameTest()
136   {
137     String name = "ab_cdEF|fwxyz012349";
138     assertEquals(name, StructureChooser.sanitizeSeqName(name));
139
140     // remove a [nn] substring
141     name = "abcde12[345]fg";
142     assertEquals("abcde12fg", StructureChooser.sanitizeSeqName(name));
143
144     // remove characters other than a-zA-Z0-9 | or _
145     name = "ab[cd],.\t£$*!- \\\"@:e";
146     assertEquals("abcde", StructureChooser.sanitizeSeqName(name));
147
148     name = "abcde12[345a]fg";
149     assertEquals("abcde12345afg", StructureChooser.sanitizeSeqName(name));
150   }
151 }