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.analysis.Conservation;
13 import jalview.schemes.NucleotideColourScheme;
14 import jalview.schemes.PIDColourScheme;
16 import java.awt.Color;
17 import java.util.Collections;
19 import junit.extensions.PA;
21 import org.testng.annotations.Test;
23 public class SequenceGroupTest
25 @Test(groups={"Functional"})
26 public void testAddSequence()
28 SequenceGroup sg = new SequenceGroup();
29 assertTrue(sg.getSequences().isEmpty());
31 SequenceI seq1 = new Sequence("seq1", "abc");
32 SequenceI seq2 = new Sequence("seq2", "abc");
33 SequenceI seq3 = new Sequence(seq1);
35 sg.addSequence(null, false);
36 assertTrue(sg.getSequences().isEmpty());
37 sg.addSequence(seq1, false);
38 assertEquals(sg.getSequences().size(), 1);
39 assertTrue(sg.getSequences().contains(seq1));
40 // adding the same sequence again does nothing
41 sg.addSequence(seq1, false);
42 assertEquals(sg.getSequences().size(), 1);
43 assertTrue(sg.getSequences().contains(seq1));
44 sg.addSequence(seq2, false);
45 sg.addSequence(seq2, false);
46 sg.addSequence(seq3, false);
47 assertEquals(sg.getSequences().size(), 3);
48 assertTrue(sg.getSequences().contains(seq1));
49 assertTrue(sg.getSequences().contains(seq2));
50 assertTrue(sg.getSequences().contains(seq3));
53 @Test(groups={"Functional"})
54 public void testAddOrRemove()
56 SequenceGroup sg = new SequenceGroup();
57 assertTrue(sg.getSequences().isEmpty());
59 SequenceI seq1 = new Sequence("seq1", "abc");
60 SequenceI seq2 = new Sequence("seq2", "abc");
61 SequenceI seq3 = new Sequence(seq1);
63 sg.addOrRemove(seq1, false);
64 assertEquals(sg.getSequences().size(), 1);
65 sg.addOrRemove(seq2, false);
66 assertEquals(sg.getSequences().size(), 2);
67 sg.addOrRemove(seq3, false);
68 assertEquals(sg.getSequences().size(), 3);
69 assertTrue(sg.getSequences().contains(seq1));
70 assertTrue(sg.getSequences().contains(seq2));
71 assertTrue(sg.getSequences().contains(seq3));
72 sg.addOrRemove(seq1, false);
73 assertEquals(sg.getSequences().size(), 2);
74 assertFalse(sg.getSequences().contains(seq1));
77 @Test(groups={"Functional"})
78 public void testGetColourScheme()
80 SequenceGroup sg = new SequenceGroup();
81 assertNotNull(sg.getGroupColourScheme());
82 assertNull(sg.getColourScheme());
84 sg.setGroupColourScheme(null);
85 assertNull(sg.getColourScheme());
87 NucleotideColourScheme scheme = new NucleotideColourScheme();
88 sg.setColourScheme(scheme);
89 assertSame(scheme, sg.getColourScheme());
92 @Test(groups={"Functional"})
93 public void testSetContext()
95 SequenceGroup sg1 = new SequenceGroup();
96 SequenceGroup sg2 = new SequenceGroup();
97 SequenceGroup sg3 = new SequenceGroup();
98 assertNull(sg1.getContext());
100 assertNull(sg1.getContext());
103 sg1.setContext(sg1); // self-reference :-O
104 fail("Expected exception");
105 } catch (IllegalArgumentException e)
108 assertNull(sg1.getContext());
111 assertSame(sg2, sg1.getContext());
115 sg3.setContext(sg1); // circular reference :-O
116 fail("Expected exception");
117 } catch (IllegalArgumentException e)
120 assertNull(sg3.getContext());
124 * use PrivilegedAccessor to 'force' a SequenceGroup with
125 * a circular context reference
127 PA.setValue(sg2, "context", sg2);
130 sg3.setContext(sg2, false); // circular reference in sg2
131 fail("Expected exception");
132 } catch (IllegalArgumentException e)
135 assertNull(sg3.getContext());
138 // test isDefined setting behaviour
139 sg2 = new SequenceGroup();
140 sg1.setContext(null, false);
141 assertFalse(sg1.isDefined());
143 sg1.setContext(sg2, false);
144 assertFalse(sg1.isDefined());
146 sg1.setContext(sg2, true);
147 assertTrue(sg1.isDefined());
149 // setContext without defined parameter does not change isDefined
150 sg1.setContext(null);
151 assertTrue(sg1.isDefined());
153 sg1.setContext(null, false);
155 assertFalse(sg1.isDefined());
158 @Test(groups = { "Functional" })
159 public void testContains()
162 * essentially the same tests as AlignmentI.findGroup
163 * but from a particular group's perspective
166 SequenceI seq1 = new Sequence("seq1", "ABCDEF---GHI");
167 SequenceI seq2 = new Sequence("seq2", "---JKLMNO---");
168 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2 });
170 * add a group consisting of just "DEF"
172 SequenceGroup sg1 = new SequenceGroup();
173 sg1.addSequence(seq1, false);
178 * test sequence membership
180 assertTrue(sg1.contains(seq1));
181 assertFalse(sg1.contains(seq2));
184 * test sequence+position
187 assertFalse(sg1.contains(seq1, 2)); // position not in group
188 assertFalse(sg1.contains(seq1, 6)); // position not in group
189 assertFalse(sg1.contains(seq2, 5)); // sequence not in group
190 assertTrue(sg1.contains(seq1, 3)); // yes
191 assertTrue(sg1.contains(seq1, 4));
192 assertTrue(sg1.contains(seq1, 5));
195 * add a group consisting of
199 SequenceGroup sg2 = new SequenceGroup();
200 sg2.addSequence(seq1, false);
201 sg2.addSequence(seq2, false);
207 * if a residue is in more than one group, method returns
208 * the first found (in order groups were added)
210 assertTrue(sg2.contains(seq1, 4));
211 assertTrue(sg2.contains(seq1, 5));
214 * seq2 only belongs to the second group
216 assertTrue(sg2.contains(seq2, 4));
217 assertTrue(sg2.contains(seq2, 5));
218 assertTrue(sg2.contains(seq2, 6));
219 assertTrue(sg2.contains(seq2, 7));
220 assertFalse(sg2.contains(seq2, 3));
221 assertFalse(sg2.contains(seq2, 8));
223 assertTrue(sg2.contains(seq2, 8));
224 sg2.deleteSequence(seq2, false);
225 assertFalse(sg2.contains(seq2));
228 @Test(groups = { "Functional" })
229 public void testCopyConstructor()
231 SequenceI seq = new Sequence("seq", "ABC");
232 SequenceGroup sg = new SequenceGroup();
233 sg.addSequence(seq, false);
235 sg.setDescription("desc");
236 sg.setColourScheme(new PIDColourScheme());
237 Conservation cons = new Conservation("Cons", 2,
238 Collections.<SequenceI> emptyList(), 3, 12);
239 PA.setValue(cons, "consSequence", new Sequence("s", "abc"));
240 sg.getGroupColourScheme().setConservation(cons);
241 sg.getGroupColourScheme().setConsensus(new Profiles(null));
242 sg.setDisplayBoxes(false);
243 sg.setDisplayText(false);
244 sg.setColourText(true);
246 sg.setShowNonconserved(true);
247 sg.setOutlineColour(Color.red);
248 sg.setIdColour(Color.blue);
249 sg.thresholdTextColour = 1;
250 sg.textColour = Color.orange;
251 sg.textColour2 = Color.yellow;
252 sg.setIgnoreGapsConsensus(false);
253 sg.setshowSequenceLogo(true);
254 sg.setNormaliseSequenceLogo(true);
255 sg.setHidereps(true);
256 sg.setHideCols(true);
257 sg.setShowConsensusHistogram(true);
258 sg.setContext(new SequenceGroup());
260 SequenceGroup sg2 = new SequenceGroup(sg);
261 assertEquals(sg2.getName(), sg.getName());
262 assertEquals(sg2.getDescription(), sg.getDescription());
263 assertNotSame(sg2.getGroupColourScheme(), sg.getGroupColourScheme());
264 assertSame(sg2.getColourScheme(), sg.getColourScheme());
265 assertSame(PA.getValue(sg2.getGroupColourScheme(), "consensus"),
266 PA.getValue(sg.getGroupColourScheme(), "consensus"));
267 assertSame(PA.getValue(sg2.getGroupColourScheme(), "conservation"),
268 PA.getValue(sg.getGroupColourScheme(), "conservation"));
269 assertEquals(sg2.getDisplayBoxes(), sg.getDisplayBoxes());
270 assertEquals(sg2.getDisplayText(), sg.getDisplayText());
271 assertEquals(sg2.getColourText(), sg.getColourText());
272 assertEquals(sg2.getShowNonconserved(), sg.getShowNonconserved());
273 assertEquals(sg2.getOutlineColour(), sg.getOutlineColour());
274 assertEquals(sg2.getIdColour(), sg.getIdColour());
275 assertEquals(sg2.thresholdTextColour, sg.thresholdTextColour);
276 assertEquals(sg2.textColour, sg.textColour);
277 assertEquals(sg2.textColour2, sg.textColour2);
278 assertEquals(sg2.getIgnoreGapsConsensus(), sg.getIgnoreGapsConsensus());
279 assertEquals(sg2.isShowSequenceLogo(), sg.isShowSequenceLogo());
280 assertEquals(sg2.isNormaliseSequenceLogo(),
281 sg.isNormaliseSequenceLogo());
282 assertEquals(sg2.isHidereps(), sg.isHidereps());
283 assertEquals(sg2.isHideCols(), sg.isHideCols());
284 assertEquals(sg2.isShowConsensusHistogram(),
285 sg.isShowConsensusHistogram());
290 assertNotSame(sg2.getSequences(), sg.getSequences());
291 assertEquals(sg2.getSequences(), sg.getSequences());
294 * isDefined should only be set true when a new group is added to
295 * an alignment, not in the copy constructor
297 assertFalse(sg2.isDefined());
300 * context should be set explicitly, not by copy
302 assertNull(sg2.getContext());