Clover icon

Coverage Report

  1. Project Clover database Mon Nov 11 2024 16:01:40 GMT
  2. Package jalview.datamodel

File SequenceGroupTest.java

 

Code metrics

0
180
7
1
358
246
10
0.06
25.71
7
1.43

Classes

Class Line # Actions
SequenceGroupTest 45 180 10
0.9839572398.4%
 

Contributing tests

This file is covered by 7 tests. .

Source view

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.datamodel;
22   
23    import static org.testng.Assert.assertEquals;
24    import static org.testng.Assert.assertFalse;
25    import static org.testng.Assert.assertNotNull;
26    import static org.testng.Assert.assertNotSame;
27    import static org.testng.Assert.assertNull;
28    import static org.testng.Assert.assertSame;
29    import static org.testng.Assert.assertTrue;
30    import static org.testng.Assert.fail;
31   
32    import jalview.analysis.Conservation;
33    import jalview.schemes.NucleotideColourScheme;
34    import jalview.schemes.PIDColourScheme;
35   
36    import java.awt.Color;
37    import java.util.ArrayList;
38    import java.util.Collections;
39    import java.util.List;
40   
41    import org.testng.annotations.Test;
42   
43    import junit.extensions.PA;
44   
 
45    public class SequenceGroupTest
46    {
 
47  1 toggle @Test(groups = { "Functional" })
48    public void testAddSequence()
49    {
50  1 SequenceGroup sg = new SequenceGroup();
51  1 assertTrue(sg.getSequences().isEmpty());
52   
53  1 SequenceI seq1 = new Sequence("seq1", "abc");
54  1 SequenceI seq2 = new Sequence("seq2", "abc");
55  1 SequenceI seq3 = new Sequence(seq1);
56   
57  1 sg.addSequence(null, false);
58  1 assertTrue(sg.getSequences().isEmpty());
59  1 sg.addSequence(seq1, false);
60  1 assertEquals(sg.getSequences().size(), 1);
61  1 assertTrue(sg.getSequences().contains(seq1));
62    // adding the same sequence again does nothing
63  1 sg.addSequence(seq1, false);
64  1 assertEquals(sg.getSequences().size(), 1);
65  1 assertTrue(sg.getSequences().contains(seq1));
66  1 sg.addSequence(seq2, false);
67  1 sg.addSequence(seq2, false);
68  1 sg.addSequence(seq3, false);
69  1 assertEquals(sg.getSequences().size(), 3);
70  1 assertTrue(sg.getSequences().contains(seq1));
71  1 assertTrue(sg.getSequences().contains(seq2));
72  1 assertTrue(sg.getSequences().contains(seq3));
73    }
74   
 
75  1 toggle @Test(groups = { "Functional" })
76    public void testAddOrRemove()
77    {
78  1 SequenceGroup sg = new SequenceGroup();
79  1 assertTrue(sg.getSequences().isEmpty());
80   
81  1 SequenceI seq1 = new Sequence("seq1", "abc");
82  1 SequenceI seq2 = new Sequence("seq2", "abc");
83  1 SequenceI seq3 = new Sequence(seq1);
84   
85  1 sg.addOrRemove(seq1, false);
86  1 assertEquals(sg.getSequences().size(), 1);
87  1 sg.addOrRemove(seq2, false);
88  1 assertEquals(sg.getSequences().size(), 2);
89  1 sg.addOrRemove(seq3, false);
90  1 assertEquals(sg.getSequences().size(), 3);
91  1 assertTrue(sg.getSequences().contains(seq1));
92  1 assertTrue(sg.getSequences().contains(seq2));
93  1 assertTrue(sg.getSequences().contains(seq3));
94  1 sg.addOrRemove(seq1, false);
95  1 assertEquals(sg.getSequences().size(), 2);
96  1 assertFalse(sg.getSequences().contains(seq1));
97    }
98   
 
99  1 toggle @Test(groups = { "Functional" })
100    public void testGetColourScheme()
101    {
102  1 SequenceGroup sg = new SequenceGroup();
103  1 assertNotNull(sg.getGroupColourScheme());
104  1 assertNull(sg.getColourScheme());
105   
106  1 sg.setGroupColourScheme(null);
107  1 assertNull(sg.getColourScheme());
108   
109  1 NucleotideColourScheme scheme = new NucleotideColourScheme();
110  1 sg.setColourScheme(scheme);
111  1 assertSame(scheme, sg.getColourScheme());
112    }
113   
 
114  1 toggle @Test(groups = { "Functional" })
115    public void testSetContext()
116    {
117  1 SequenceGroup sg1 = new SequenceGroup();
118  1 SequenceGroup sg2 = new SequenceGroup();
119  1 SequenceGroup sg3 = new SequenceGroup();
120  1 assertNull(sg1.getContext());
121  1 sg1.setContext(null);
122  1 assertNull(sg1.getContext());
123  1 try
124    {
125  1 sg1.setContext(sg1); // self-reference :-O
126  0 fail("Expected exception");
127    } catch (IllegalArgumentException e)
128    {
129    // expected
130  1 assertNull(sg1.getContext());
131    }
132  1 sg1.setContext(sg2);
133  1 assertSame(sg2, sg1.getContext());
134  1 sg2.setContext(sg3);
135  1 try
136    {
137  1 sg3.setContext(sg1); // circular reference :-O
138  0 fail("Expected exception");
139    } catch (IllegalArgumentException e)
140    {
141    // expected
142  1 assertNull(sg3.getContext());
143    }
144   
145    /*
146    * use PrivilegedAccessor to 'force' a SequenceGroup with
147    * a circular context reference
148    */
149  1 PA.setValue(sg2, "context", sg2);
150  1 try
151    {
152  1 sg3.setContext(sg2, false); // circular reference in sg2
153  0 fail("Expected exception");
154    } catch (IllegalArgumentException e)
155    {
156    // expected
157  1 assertNull(sg3.getContext());
158    }
159   
160    // test isDefined setting behaviour
161  1 sg2 = new SequenceGroup();
162  1 sg1.setContext(null, false);
163  1 assertFalse(sg1.isDefined());
164   
165  1 sg1.setContext(sg2, false);
166  1 assertFalse(sg1.isDefined());
167   
168  1 sg1.setContext(sg2, true);
169  1 assertTrue(sg1.isDefined());
170   
171    // setContext without defined parameter does not change isDefined
172  1 sg1.setContext(null);
173  1 assertTrue(sg1.isDefined());
174   
175  1 sg1.setContext(null, false);
176  1 sg1.setContext(sg2);
177  1 assertFalse(sg1.isDefined());
178    }
179   
 
180  1 toggle @Test(groups = { "Functional" })
181    public void testContains()
182    {
183    /*
184    * essentially the same tests as AlignmentI.findGroup
185    * but from a particular group's perspective
186    */
187   
188  1 SequenceI seq1 = new Sequence("seq1", "ABCDEF---GHI");
189  1 SequenceI seq2 = new Sequence("seq2", "---JKLMNO---");
190  1 AlignmentI a = new Alignment(new SequenceI[] { seq1, seq2 });
191    /*
192    * add a group consisting of just "DEF"
193    */
194  1 SequenceGroup sg1 = new SequenceGroup();
195  1 sg1.addSequence(seq1, false);
196  1 sg1.setStartRes(3);
197  1 sg1.setEndRes(5);
198   
199    /*
200    * test sequence membership
201    */
202  1 assertTrue(sg1.contains(seq1));
203  1 assertFalse(sg1.contains(seq2));
204   
205    /*
206    * test sequence+position
207    */
208   
209  1 assertFalse(sg1.contains(seq1, 2)); // position not in group
210  1 assertFalse(sg1.contains(seq1, 6)); // position not in group
211  1 assertFalse(sg1.contains(seq2, 5)); // sequence not in group
212  1 assertTrue(sg1.contains(seq1, 3)); // yes
213  1 assertTrue(sg1.contains(seq1, 4));
214  1 assertTrue(sg1.contains(seq1, 5));
215   
216    /*
217    * add a group consisting of
218    * EF--
219    * KLMN
220    */
221  1 SequenceGroup sg2 = new SequenceGroup();
222  1 sg2.addSequence(seq1, false);
223  1 sg2.addSequence(seq2, false);
224  1 sg2.setStartRes(4);
225  1 sg2.setEndRes(7);
226  1 a.addGroup(sg2);
227   
228    /*
229    * if a residue is in more than one group, method returns
230    * the first found (in order groups were added)
231    */
232  1 assertTrue(sg2.contains(seq1, 4));
233  1 assertTrue(sg2.contains(seq1, 5));
234   
235    /*
236    * seq2 only belongs to the second group
237    */
238  1 assertTrue(sg2.contains(seq2, 4));
239  1 assertTrue(sg2.contains(seq2, 5));
240  1 assertTrue(sg2.contains(seq2, 6));
241  1 assertTrue(sg2.contains(seq2, 7));
242  1 assertFalse(sg2.contains(seq2, 3));
243  1 assertFalse(sg2.contains(seq2, 8));
244  1 sg2.setEndRes(8);
245  1 assertTrue(sg2.contains(seq2, 8));
246  1 sg2.deleteSequence(seq2, false);
247  1 assertFalse(sg2.contains(seq2));
248    }
249   
 
250  1 toggle @Test(groups = { "Functional" })
251    public void testCopyConstructor()
252    {
253  1 SequenceI seq = new Sequence("seq", "ABC");
254  1 SequenceGroup sg = new SequenceGroup();
255  1 sg.addSequence(seq, false);
256  1 sg.setName("g1");
257  1 sg.setDescription("desc");
258  1 sg.setColourScheme(new PIDColourScheme());
259  1 Conservation cons = new Conservation("Cons", 2,
260    Collections.<SequenceI> emptyList(), 3, 12);
261  1 PA.setValue(cons, "consSequence", new Sequence("s", "abc"));
262  1 sg.getGroupColourScheme().setConservation(cons);
263  1 sg.getGroupColourScheme().setConsensus(new Profiles(0,null));
264  1 sg.setDisplayBoxes(false);
265  1 sg.setDisplayText(false);
266  1 sg.setColourText(true);
267  1 sg.isDefined = true;
268  1 sg.setShowNonconserved(true);
269  1 sg.setOutlineColour(Color.red);
270  1 sg.setIdColour(Color.blue);
271  1 sg.thresholdTextColour = 1;
272  1 sg.textColour = Color.orange;
273  1 sg.textColour2 = Color.yellow;
274  1 sg.setIgnoreGapsConsensus(false);
275  1 sg.setshowSequenceLogo(true);
276  1 sg.setNormaliseSequenceLogo(true);
277  1 sg.setHidereps(true);
278  1 sg.setHideCols(true);
279  1 sg.setShowConsensusHistogram(true);
280  1 sg.setContext(new SequenceGroup());
281   
282  1 SequenceGroup sg2 = new SequenceGroup(sg);
283  1 assertEquals(sg2.getName(), sg.getName());
284  1 assertEquals(sg2.getDescription(), sg.getDescription());
285  1 assertNotSame(sg2.getGroupColourScheme(), sg.getGroupColourScheme());
286  1 assertSame(sg2.getColourScheme(), sg.getColourScheme());
287  1 assertSame(PA.getValue(sg2.getGroupColourScheme(), "consensus"),
288    PA.getValue(sg.getGroupColourScheme(), "consensus"));
289  1 assertSame(PA.getValue(sg2.getGroupColourScheme(), "conservation"),
290    PA.getValue(sg.getGroupColourScheme(), "conservation"));
291  1 assertEquals(sg2.getDisplayBoxes(), sg.getDisplayBoxes());
292  1 assertEquals(sg2.getDisplayText(), sg.getDisplayText());
293  1 assertEquals(sg2.getColourText(), sg.getColourText());
294  1 assertEquals(sg2.getShowNonconserved(), sg.getShowNonconserved());
295  1 assertEquals(sg2.getOutlineColour(), sg.getOutlineColour());
296  1 assertEquals(sg2.getIdColour(), sg.getIdColour());
297  1 assertEquals(sg2.thresholdTextColour, sg.thresholdTextColour);
298  1 assertEquals(sg2.textColour, sg.textColour);
299  1 assertEquals(sg2.textColour2, sg.textColour2);
300  1 assertEquals(sg2.getIgnoreGapsConsensus(), sg.getIgnoreGapsConsensus());
301  1 assertEquals(sg2.isShowSequenceLogo(), sg.isShowSequenceLogo());
302  1 assertEquals(sg2.isNormaliseSequenceLogo(),
303    sg.isNormaliseSequenceLogo());
304  1 assertEquals(sg2.isHidereps(), sg.isHidereps());
305  1 assertEquals(sg2.isHideCols(), sg.isHideCols());
306  1 assertEquals(sg2.isShowConsensusHistogram(),
307    sg.isShowConsensusHistogram());
308   
309    /*
310    * copy of sequences
311    */
312  1 assertNotSame(sg2.getSequences(), sg.getSequences());
313  1 assertEquals(sg2.getSequences(), sg.getSequences());
314   
315    /*
316    * isDefined should only be set true when a new group is added to
317    * an alignment, not in the copy constructor
318    */
319  1 assertFalse(sg2.isDefined());
320   
321    /*
322    * context should be set explicitly, not by copy
323    */
324  1 assertNull(sg2.getContext());
325    }
326   
 
327  1 toggle @Test(groups = { "Functional" })
328    public void testConstructor_list()
329    {
330  1 SequenceI s1 = new Sequence("abcde", "fg");
331  1 SequenceI s2 = new Sequence("foo", "bar");
332  1 List<SequenceI> seqs = new ArrayList<SequenceI>();
333  1 seqs.add(s1);
334  1 seqs.add(s2);
335  1 SequenceGroup sg = new SequenceGroup(seqs);
336   
337    /*
338    * verify sg has a copy of the original list
339    */
340  1 List<SequenceI> sgList = sg.getSequences();
341  1 assertNotSame(sgList, seqs);
342  1 assertEquals(sgList, seqs);
343   
344    /*
345    * add to sgList, original is unchanged
346    */
347  1 sg.addSequence(new Sequence("bar", "foo"), false);
348  1 assertEquals(sgList.size(), 3);
349  1 assertEquals(seqs.size(), 2);
350   
351    /*
352    * delete from original, sgList is unchanged
353    */
354  1 seqs.remove(s1);
355  1 assertEquals(sgList.size(), 3);
356  1 assertEquals(seqs.size(), 1);
357    }
358    }