1 package jalview.analysis;
3 import static org.testng.Assert.assertSame;
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;
11 import java.util.Arrays;
12 import java.util.List;
14 import junit.extensions.PA;
16 import org.testng.annotations.Test;
18 public class AlignmentSorterTest
20 @Test(groups = "Functional")
21 public void testSortByFeature_score()
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);
32 * sort with no score features does nothing
34 PA.setValue(AlignmentSorter.class, "sortByFeatureCriteria", null);
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);
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
50 seq1.addSequenceFeature(new SequenceFeature("Cath", "", 2, 3, 2.0f,
52 seq1.addSequenceFeature(new SequenceFeature("Pfam", "", 4, 5, 4.0f,
54 seq2.addSequenceFeature(new SequenceFeature("Cath", "", 2, 3, 2.5f,
56 seq2.addSequenceFeature(new SequenceFeature("Metal", "", 2, 3,
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,
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
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'
76 * repeat sort toggles order - now ascending
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);
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
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);
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
103 List<String> groups = Arrays.asList(new String[] { "g1", "g2", "g3",
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);
113 * limit to columns 0-4, excluding 2nd feature of seq1 and seq3
114 * seq1 is now 2.0, seq3 is now -4
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);
126 @Test(groups = "Functional")
127 public void testSortByFeature_density()