1 package jalview.datamodel;
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertNotNull;
6 import static org.testng.Assert.assertNotSame;
7 import static org.testng.Assert.assertNull;
8 import static org.testng.Assert.assertSame;
9 import static org.testng.Assert.assertTrue;
10 import static org.testng.Assert.fail;
12 import jalview.schemes.NucleotideColourScheme;
13 import jalview.schemes.PIDColourScheme;
15 import java.awt.Color;
17 import junit.extensions.PA;
19 import org.testng.annotations.Test;
21 public class SequenceGroupTest
23 @Test(groups={"Functional"})
24 public void testAddSequence()
26 SequenceGroup sg = new SequenceGroup();
27 assertTrue(sg.getSequences().isEmpty());
29 SequenceI seq1 = new Sequence("seq1", "abc");
30 SequenceI seq2 = new Sequence("seq2", "abc");
31 SequenceI seq3 = new Sequence(seq1);
33 sg.addSequence(null, false);
34 assertTrue(sg.getSequences().isEmpty());
35 sg.addSequence(seq1, false);
36 assertEquals(sg.getSequences().size(), 1);
37 assertTrue(sg.getSequences().contains(seq1));
38 // adding the same sequence again does nothing
39 sg.addSequence(seq1, false);
40 assertEquals(sg.getSequences().size(), 1);
41 assertTrue(sg.getSequences().contains(seq1));
42 sg.addSequence(seq2, false);
43 sg.addSequence(seq2, false);
44 sg.addSequence(seq3, false);
45 assertEquals(sg.getSequences().size(), 3);
46 assertTrue(sg.getSequences().contains(seq1));
47 assertTrue(sg.getSequences().contains(seq2));
48 assertTrue(sg.getSequences().contains(seq3));
51 @Test(groups={"Functional"})
52 public void testAddOrRemove()
54 SequenceGroup sg = new SequenceGroup();
55 assertTrue(sg.getSequences().isEmpty());
57 SequenceI seq1 = new Sequence("seq1", "abc");
58 SequenceI seq2 = new Sequence("seq2", "abc");
59 SequenceI seq3 = new Sequence(seq1);
61 sg.addOrRemove(seq1, false);
62 assertEquals(sg.getSequences().size(), 1);
63 sg.addOrRemove(seq2, false);
64 assertEquals(sg.getSequences().size(), 2);
65 sg.addOrRemove(seq3, false);
66 assertEquals(sg.getSequences().size(), 3);
67 assertTrue(sg.getSequences().contains(seq1));
68 assertTrue(sg.getSequences().contains(seq2));
69 assertTrue(sg.getSequences().contains(seq3));
70 sg.addOrRemove(seq1, false);
71 assertEquals(sg.getSequences().size(), 2);
72 assertFalse(sg.getSequences().contains(seq1));
75 @Test(groups={"Functional"})
76 public void testGetColourScheme()
78 SequenceGroup sg = new SequenceGroup();
79 assertNotNull(sg.getGroupColourScheme());
80 assertNull(sg.getColourScheme());
82 sg.setGroupColourScheme(null);
83 assertNull(sg.getColourScheme());
85 NucleotideColourScheme scheme = new NucleotideColourScheme();
86 sg.setColourScheme(scheme);
87 assertSame(scheme, sg.getColourScheme());
90 @Test(groups={"Functional"})
91 public void testSetContext()
93 SequenceGroup sg1 = new SequenceGroup();
94 SequenceGroup sg2 = new SequenceGroup();
95 SequenceGroup sg3 = new SequenceGroup();
96 assertNull(sg1.getContext());
98 assertNull(sg1.getContext());
101 sg1.setContext(sg1); // self-reference :-O
102 fail("Expected exception");
103 } catch (IllegalArgumentException e)
106 assertNull(sg1.getContext());
109 assertSame(sg2, sg1.getContext());
113 sg3.setContext(sg1); // circular reference :-O
114 fail("Expected exception");
115 } catch (IllegalArgumentException e)
118 assertNull(sg3.getContext());
122 * use PrivilegedAccessor to 'force' a SequenceGroup with
123 * a circular context reference
125 PA.setValue(sg2, "context", sg2);
128 sg3.setContext(sg2, false); // circular reference in sg2
129 fail("Expected exception");
130 } catch (IllegalArgumentException e)
133 assertNull(sg3.getContext());
136 // test isDefined setting behaviour
137 sg2 = new SequenceGroup();
138 sg1.setContext(null, false);
139 assertFalse(sg1.isDefined());
141 sg1.setContext(sg2, false);
142 assertFalse(sg1.isDefined());
144 sg1.setContext(sg2, true);
145 assertTrue(sg1.isDefined());
147 // setContext without defined parameter does not change isDefined
148 sg1.setContext(null);
149 assertTrue(sg1.isDefined());
151 sg1.setContext(null, false);
153 assertFalse(sg1.isDefined());
156 @Test(groups = { "Functional" })
157 public void testContains()
160 * essentially the same tests as AlignmentI.findGroup
161 * but from a particular group's perspective
164 SequenceI seq1 = new Sequence("seq1", "ABCDEF---GHI");
165 SequenceI seq2 = new Sequence("seq2", "---JKLMNO---");
166 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2 });
168 * add a group consisting of just "DEF"
170 SequenceGroup sg1 = new SequenceGroup();
171 sg1.addSequence(seq1, false);
176 * test sequence membership
178 assertTrue(sg1.contains(seq1));
179 assertFalse(sg1.contains(seq2));
182 * test sequence+position
185 assertFalse(sg1.contains(seq1, 2)); // position not in group
186 assertFalse(sg1.contains(seq1, 6)); // position not in group
187 assertFalse(sg1.contains(seq2, 5)); // sequence not in group
188 assertTrue(sg1.contains(seq1, 3)); // yes
189 assertTrue(sg1.contains(seq1, 4));
190 assertTrue(sg1.contains(seq1, 5));
193 * add a group consisting of
197 SequenceGroup sg2 = new SequenceGroup();
198 sg2.addSequence(seq1, false);
199 sg2.addSequence(seq2, false);
205 * if a residue is in more than one group, method returns
206 * the first found (in order groups were added)
208 assertTrue(sg2.contains(seq1, 4));
209 assertTrue(sg2.contains(seq1, 5));
212 * seq2 only belongs to the second group
214 assertTrue(sg2.contains(seq2, 4));
215 assertTrue(sg2.contains(seq2, 5));
216 assertTrue(sg2.contains(seq2, 6));
217 assertTrue(sg2.contains(seq2, 7));
218 assertFalse(sg2.contains(seq2, 3));
219 assertFalse(sg2.contains(seq2, 8));
221 assertTrue(sg2.contains(seq2, 8));
222 sg2.deleteSequence(seq2, false);
223 assertFalse(sg2.contains(seq2));
226 @Test(groups = { "Functional" })
227 public void testCopyConstructor()
229 SequenceI seq = new Sequence("seq", "ABC");
230 SequenceGroup sg = new SequenceGroup();
231 sg.addSequence(seq, false);
233 sg.setDescription("desc");
234 sg.setColourScheme(new PIDColourScheme());
235 sg.setDisplayBoxes(false);
236 sg.setDisplayText(false);
237 sg.setColourText(true);
239 sg.setShowNonconserved(true);
240 sg.setOutlineColour(Color.red);
241 sg.setIdColour(Color.blue);
242 sg.thresholdTextColour = 1;
243 sg.textColour = Color.orange;
244 sg.textColour2 = Color.yellow;
245 sg.setIgnoreGapsConsensus(false);
246 sg.setshowSequenceLogo(true);
247 sg.setNormaliseSequenceLogo(true);
248 sg.setHidereps(true);
249 sg.setHideCols(true);
250 sg.setShowConsensusHistogram(true);
251 sg.setContext(new SequenceGroup());
253 SequenceGroup sg2 = new SequenceGroup(sg);
254 assertEquals(sg2.getName(), sg.getName());
255 assertEquals(sg2.getDescription(), sg.getDescription());
256 assertNotSame(sg2.getGroupColourScheme(), sg.getGroupColourScheme());
257 assertSame(sg2.getColourScheme(), sg.getColourScheme());
258 assertEquals(sg2.getDisplayBoxes(), sg.getDisplayBoxes());
259 assertEquals(sg2.getDisplayText(), sg.getDisplayText());
260 assertEquals(sg2.getColourText(), sg.getColourText());
261 assertEquals(sg2.getShowNonconserved(), sg.getShowNonconserved());
262 assertEquals(sg2.getOutlineColour(), sg.getOutlineColour());
263 assertEquals(sg2.getIdColour(), sg.getIdColour());
264 assertEquals(sg2.thresholdTextColour, sg.thresholdTextColour);
265 assertEquals(sg2.textColour, sg.textColour);
266 assertEquals(sg2.textColour2, sg.textColour2);
267 assertEquals(sg2.getIgnoreGapsConsensus(), sg.getIgnoreGapsConsensus());
268 assertEquals(sg2.isShowSequenceLogo(), sg.isShowSequenceLogo());
269 assertEquals(sg2.isNormaliseSequenceLogo(),
270 sg.isNormaliseSequenceLogo());
271 assertEquals(sg2.isHidereps(), sg.isHidereps());
272 assertEquals(sg2.isHideCols(), sg.isHideCols());
273 assertEquals(sg2.isShowConsensusHistogram(),
274 sg.isShowConsensusHistogram());
279 assertNotSame(sg2.getSequences(), sg.getSequences());
280 assertEquals(sg2.getSequences(), sg.getSequences());
283 * isDefined should only be set true when a new group is added to
284 * an alignment, not in the copy constructor
286 assertFalse(sg2.isDefined());
289 * context should be set explicitly, not by copy
291 assertNull(sg2.getContext());