JAL-1988 JAL-3130 New jlinked JRE in jalview_java2 with modules. Still needs jaxws...
[jalview.git] / test / jalview / analysis / AlignmentSorterTest.java
1 package jalview.analysis;
2
3 import static org.testng.Assert.assertSame;
4
5 import jalview.datamodel.Alignment;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.Sequence;
8 import jalview.datamodel.SequenceFeature;
9 import jalview.datamodel.SequenceI;
10
11 import java.util.Arrays;
12 import java.util.List;
13
14 import junit.extensions.PA;
15
16 import org.testng.annotations.Test;
17
18 public class AlignmentSorterTest
19 {
20   @Test(groups = "Functional")
21   public void testSortByFeature_score()
22   {
23     SequenceI seq1 = new Sequence("Seq1", "ABC--D-EFGHIJ");
24     SequenceI seq2 = new Sequence("Seq2", "ABCDEFGHIJ");
25     SequenceI seq3 = new Sequence("Seq3", "ABCDE-FGHIJ");
26     SequenceI seq4 = new Sequence("Seq4", "ABCDEFGHIJ");
27     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
28     AlignmentI al = new Alignment(seqs);
29     al.setDataset(null);
30
31     /*
32      * sort with no score features does nothing
33      */
34     PA.setValue(AlignmentSorter.class, "sortByFeatureCriteria", null);
35
36     AlignmentSorter.sortByFeature(null, null, 0, al.getWidth(), al,
37             AlignmentSorter.FEATURE_SCORE);
38     assertSame(al.getSequenceAt(0), seq1);
39     assertSame(al.getSequenceAt(1), seq2);
40     assertSame(al.getSequenceAt(2), seq3);
41     assertSame(al.getSequenceAt(3), seq4);
42
43     /*
44      * add score and non-score features
45      * seq1 Cath(2.0) Pfam(4.0) average 3.0
46      * seq2 Cath(2.5) Metal(NaN) average 2.5
47      * seq3 KD(-4), KD(3.0) average -0.5
48      * seq4 Helix(NaN) - should sort as if largest score
49      */
50     seq1.addSequenceFeature(new SequenceFeature("Cath", "", 2, 3, 2.0f,
51             "g1"));
52     seq1.addSequenceFeature(new SequenceFeature("Pfam", "", 4, 5, 4.0f,
53             "g2"));
54     seq2.addSequenceFeature(new SequenceFeature("Cath", "", 2, 3, 2.5f,
55             "g3"));
56     seq2.addSequenceFeature(new SequenceFeature("Metal", "", 2, 3,
57             Float.NaN, "g4"));
58     seq3.addSequenceFeature(new SequenceFeature("kD", "", 2, 3, -4f, "g5"));
59     seq3.addSequenceFeature(new SequenceFeature("kD", "", 5, 6, 3.0f, "g6"));
60     seq4.addSequenceFeature(new SequenceFeature("Helix", "", 2, 3,
61             Float.NaN, "g7"));
62
63     /*
64      * sort by ascending score, no filter on feature type or group
65      * NB sort order for the same feature set (none) gets toggled, so descending
66      */
67     PA.setValue(AlignmentSorter.class, "sortByFeatureAscending", true);
68     AlignmentSorter.sortByFeature(null, null, 0, al.getWidth(), al,
69             AlignmentSorter.FEATURE_SCORE);
70     assertSame(al.getSequenceAt(3), seq3); // -0.5
71     assertSame(al.getSequenceAt(2), seq2); // 2.5
72     assertSame(al.getSequenceAt(1), seq1); // 3.0
73     assertSame(al.getSequenceAt(0), seq4); // maximum 'score'
74
75     /*
76      * repeat sort toggles order - now ascending
77      */
78     AlignmentSorter.sortByFeature(null, null, 0, al.getWidth(), al,
79             AlignmentSorter.FEATURE_SCORE);
80     assertSame(al.getSequenceAt(0), seq3); // -0.5
81     assertSame(al.getSequenceAt(1), seq2); // 2.5
82     assertSame(al.getSequenceAt(2), seq1); // 3.0
83     assertSame(al.getSequenceAt(3), seq4);
84
85     /*
86      * specify features, excluding Pfam
87      * seq1 average is now 2.0
88      * next sort is ascending (not toggled) as for a different feature set
89      */
90     List<String> types = Arrays.asList(new String[] { "Cath", "kD" });
91     AlignmentSorter.sortByFeature(types, null, 0, al.getWidth(), al,
92             AlignmentSorter.FEATURE_SCORE);
93     assertSame(al.getSequenceAt(0), seq3); // -0.5
94     assertSame(al.getSequenceAt(1), seq1); // 2.0
95     assertSame(al.getSequenceAt(2), seq2); // 2.5
96     assertSame(al.getSequenceAt(3), seq4);
97
98     /*
99      * specify groups, excluding g5 (kD -4 score)
100      * seq3 average is now 3.0
101      * next sort is ascending (not toggled) as for a different group spec
102      */
103     List<String> groups = Arrays.asList(new String[] { "g1", "g2", "g3",
104         "g6" });
105     AlignmentSorter.sortByFeature(types, groups, 0, al.getWidth(), al,
106             AlignmentSorter.FEATURE_SCORE);
107     assertSame(al.getSequenceAt(0), seq1); // 2.0
108     assertSame(al.getSequenceAt(1), seq2); // 2.5
109     assertSame(al.getSequenceAt(2), seq3); // 3.0
110     assertSame(al.getSequenceAt(3), seq4);
111
112     /*
113      * limit to columns 0-4, excluding 2nd feature of seq1 and seq3
114      * seq1 is now 2.0, seq3 is now -4
115      */
116     // fails because seq1.findPosition(4) returns 4
117     // although residue 4 is in column 5! - JAL-2544
118     AlignmentSorter.sortByFeature(null, null, 0, 4, al,
119             AlignmentSorter.FEATURE_SCORE);
120     assertSame(al.getSequenceAt(0), seq3); // -4
121     assertSame(al.getSequenceAt(1), seq1); // 2.0
122     assertSame(al.getSequenceAt(2), seq2); // 2.5
123     assertSame(al.getSequenceAt(3), seq4);
124   }
125
126   @Test(groups = "Functional")
127   public void testSortByFeature_density()
128   {
129     // TODO
130   }
131 }