55823e41e5cfac41984be29047e87c561159c490
[jalview.git] / test / jalview / analysis / GroupingTest.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.analysis;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32
33 import org.testng.AssertJUnit;
34 import org.testng.annotations.Test;
35
36 public class GroupingTest
37 {
38   Sequence s1 = new Sequence("s1", "AAAADDDDEEEE");
39
40   Sequence s2 = new Sequence("s2", "AAAADDDDEEEE");
41
42   Sequence s3 = new Sequence("s3", "ACAADDEDEEEE");
43
44   Sequence s4 = new Sequence("s4", "AAAADDEDEEEE");
45
46   Sequence s5 = new Sequence("s5", "AAAADDEDTTEE");
47
48   SequenceGroup sg1 = new SequenceGroup(Arrays.asList(new SequenceI[] { s1,
49       s2 }), "Group1", null, false, false, false, 0, 5);
50
51   SequenceGroup sg2 = new SequenceGroup(Arrays.asList(new SequenceI[] { s3,
52       s4, s5 }), "Group2", null, false, false, false, 0, 5);
53
54   AlignmentI alignment = new Alignment(
55           new SequenceI[] { s1, s2, s3, s4, s5 });
56
57   int[] positions = new int[] { 1, 7, 9 };
58
59   @Test(groups = { "Functional" })
60   public void testMakeGroupsWithBoth()
61   {
62     ArrayList<String> str = new ArrayList<String>();
63     for (SequenceI s : alignment.getSequences())
64     {
65       StringBuilder sb = new StringBuilder();
66       for (int p : positions)
67       {
68         sb.append(s.getCharAt(p));
69       }
70       str.add(sb.toString());
71     }
72     SequenceGroup[] seqgroupsString = Grouping.makeGroupsFrom(
73             alignment.getSequencesArray(),
74             str.toArray(new String[str.size()]),
75             Arrays.asList(new SequenceGroup[] { sg1, sg2 }));
76     ColumnSelection cs = new ColumnSelection();
77     for (int p : positions)
78     {
79       cs.addElement(p);
80     }
81     SequenceGroup[] seqgroupsColSel = Grouping.makeGroupsFromCols(
82             alignment.getSequencesArray(), cs,
83             Arrays.asList(new SequenceGroup[] { sg1, sg2 }));
84     AssertJUnit
85             .assertEquals(seqgroupsString.length, seqgroupsColSel.length);
86     for (int p = 0; p < seqgroupsString.length; p++)
87     {
88       AssertJUnit.assertEquals(seqgroupsString[p].getName(),
89               seqgroupsColSel[p].getName());
90       AssertJUnit.assertArrayEquals(
91               seqgroupsString[p].getSequencesInOrder(alignment),
92               seqgroupsColSel[p].getSequencesInOrder(alignment));
93       if (seqgroupsString[p].getSequences().contains(s2))
94       {
95         AssertJUnit.assertTrue(seqgroupsString[p].getSize() == 2);
96       }
97     }
98   }
99
100 }