2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis;
23 import static org.testng.Assert.assertSame;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
31 import java.util.Arrays;
32 import java.util.List;
34 import junit.extensions.PA;
36 import org.testng.annotations.Test;
38 public class AlignmentSorterTest
40 @Test(groups = "Functional")
41 public void testSortByFeature_score()
43 SequenceI seq1 = new Sequence("Seq1", "ABC--D-EFGHIJ");
44 SequenceI seq2 = new Sequence("Seq2", "ABCDEFGHIJ");
45 SequenceI seq3 = new Sequence("Seq3", "ABCDE-FGHIJ");
46 SequenceI seq4 = new Sequence("Seq4", "ABCDEFGHIJ");
47 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
48 AlignmentI al = new Alignment(seqs);
52 * sort with no score features does nothing
54 PA.setValue(AlignmentSorter.class, "sortByFeatureCriteria", null);
56 AlignmentSorter.sortByFeature(null, null, 0, al.getWidth(), al,
57 AlignmentSorter.FEATURE_SCORE);
58 assertSame(al.getSequenceAt(0), seq1);
59 assertSame(al.getSequenceAt(1), seq2);
60 assertSame(al.getSequenceAt(2), seq3);
61 assertSame(al.getSequenceAt(3), seq4);
64 * add score and non-score features
65 * seq1 Cath(2.0) Pfam(4.0) average 3.0
66 * seq2 Cath(2.5) Metal(NaN) average 2.5
67 * seq3 KD(-4), KD(3.0) average -0.5
68 * seq4 Helix(NaN) - should sort as if largest score
70 seq1.addSequenceFeature(
71 new SequenceFeature("Cath", "", 2, 3, 2.0f, "g1"));
72 seq1.addSequenceFeature(
73 new SequenceFeature("Pfam", "", 4, 5, 4.0f, "g2"));
74 seq2.addSequenceFeature(
75 new SequenceFeature("Cath", "", 2, 3, 2.5f, "g3"));
76 seq2.addSequenceFeature(
77 new SequenceFeature("Metal", "", 2, 3, Float.NaN, "g4"));
78 seq3.addSequenceFeature(new SequenceFeature("kD", "", 2, 3, -4f, "g5"));
79 seq3.addSequenceFeature(
80 new SequenceFeature("kD", "", 5, 6, 3.0f, "g6"));
81 seq4.addSequenceFeature(
82 new SequenceFeature("Helix", "", 2, 3, Float.NaN, "g7"));
85 * sort by ascending score, no filter on feature type or group
86 * NB sort order for the same feature set (none) gets toggled, so descending
88 PA.setValue(AlignmentSorter.class, "sortByFeatureAscending", true);
89 AlignmentSorter.sortByFeature(null, null, 0, al.getWidth(), al,
90 AlignmentSorter.FEATURE_SCORE);
91 assertSame(al.getSequenceAt(3), seq3); // -0.5
92 assertSame(al.getSequenceAt(2), seq2); // 2.5
93 assertSame(al.getSequenceAt(1), seq1); // 3.0
94 assertSame(al.getSequenceAt(0), seq4); // maximum 'score'
97 * repeat sort toggles order - now ascending
99 AlignmentSorter.sortByFeature(null, null, 0, al.getWidth(), al,
100 AlignmentSorter.FEATURE_SCORE);
101 assertSame(al.getSequenceAt(0), seq3); // -0.5
102 assertSame(al.getSequenceAt(1), seq2); // 2.5
103 assertSame(al.getSequenceAt(2), seq1); // 3.0
104 assertSame(al.getSequenceAt(3), seq4);
107 * specify features, excluding Pfam
108 * seq1 average is now 2.0
109 * next sort is ascending (not toggled) as for a different feature set
111 List<String> types = Arrays.asList(new String[] { "Cath", "kD" });
112 AlignmentSorter.sortByFeature(types, null, 0, al.getWidth(), al,
113 AlignmentSorter.FEATURE_SCORE);
114 assertSame(al.getSequenceAt(0), seq3); // -0.5
115 assertSame(al.getSequenceAt(1), seq1); // 2.0
116 assertSame(al.getSequenceAt(2), seq2); // 2.5
117 assertSame(al.getSequenceAt(3), seq4);
120 * specify groups, excluding g5 (kD -4 score)
121 * seq3 average is now 3.0
122 * next sort is ascending (not toggled) as for a different group spec
124 List<String> groups = Arrays
126 { "g1", "g2", "g3", "g6" });
127 AlignmentSorter.sortByFeature(types, groups, 0, al.getWidth(), al,
128 AlignmentSorter.FEATURE_SCORE);
129 assertSame(al.getSequenceAt(0), seq1); // 2.0
130 assertSame(al.getSequenceAt(1), seq2); // 2.5
131 assertSame(al.getSequenceAt(2), seq3); // 3.0
132 assertSame(al.getSequenceAt(3), seq4);
135 * limit to columns 0-4, excluding 2nd feature of seq1 and seq3
136 * seq1 is now 2.0, seq3 is now -4
138 // fails because seq1.findPosition(4) returns 4
139 // although residue 4 is in column 5! - JAL-2544
140 AlignmentSorter.sortByFeature(null, null, 0, 4, al,
141 AlignmentSorter.FEATURE_SCORE);
142 assertSame(al.getSequenceAt(0), seq3); // -4
143 assertSame(al.getSequenceAt(1), seq1); // 2.0
144 assertSame(al.getSequenceAt(2), seq2); // 2.5
145 assertSame(al.getSequenceAt(3), seq4);
148 @Test(groups = "Functional")
149 public void testSortByFeature_density()