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.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.AlignFrame;
31 import jalview.gui.JvOptionPane;
33 import java.util.List;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.BeforeMethod;
37 import org.testng.annotations.Test;
39 public class ParsePropertiesTest
42 @BeforeClass(alwaysRun = true)
43 public void setUpJvOptionPane()
45 JvOptionPane.setInteractiveMode(false);
46 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
51 private ParseProperties pp;
54 * Construct an alignment with 4 sequences with varying description format
56 @BeforeMethod(alwaysRun = true)
59 SequenceI[] seqs = new SequenceI[] {
60 new Sequence("sq1", "THISISAPLACEHOLDER"),
61 new Sequence("sq2", "THISISAPLACEHOLDER"),
62 new Sequence("sq3", "THISISAPLACEHOLDER"),
63 new Sequence("sq4", "THISISAPLACEHOLDER") };
64 seqs[0].setDescription("1 mydescription1");
65 seqs[1].setDescription("mydescription2");
66 seqs[2].setDescription("2. 0.1 mydescription+3");
67 seqs[3].setDescription("3 0.01 mydescription4");
68 al = new Alignment(seqs);
70 pp = new ParseProperties(al);
75 * Test with a description pattern that matches any string ending in one or
76 * more 'number characters' (0-9+.), i.e. greedily matches any trailing
77 * numeric part of the string
79 @Test(groups = { "Functional" })
80 public void testGetScoresFromDescription()
82 String regex = ".*([-0-9.+]+)";
83 final int count = pp.getScoresFromDescription("my Score",
84 "my Score Description", regex, true);
85 System.out.println("Matched " + count + " for " + regex);
86 assertEquals(4, count);
89 * Verify values 1/2/3/4 have been parsed from sequence descriptions
91 AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation();
92 assertEquals(1, anns.length);
93 assertEquals(1d, anns[0].getScore(), 0.001d);
94 assertEquals("my Score Description", anns[0].description);
95 assertEquals("my Score", anns[0].label);
96 anns = al.getSequenceAt(1).getAnnotation();
97 assertEquals(1, anns.length);
98 assertEquals(2d, anns[0].getScore(), 0.001d);
99 assertEquals("my Score Description", anns[0].description);
100 assertEquals("my Score", anns[0].label);
101 anns = al.getSequenceAt(2).getAnnotation();
102 assertEquals(1, anns.length);
103 assertEquals(3d, anns[0].getScore(), 0.001d);
104 anns = al.getSequenceAt(3).getAnnotation();
105 assertEquals(1, anns.length);
106 assertEquals(4d, anns[0].getScore(), 0.001d);
110 * Test with a description pattern that matches any string (or none), followed
111 * by a 'number character' (0-9+.), followed by at least one separator
112 * character, followed by at least one 'number character', then any trailing
115 @Test(groups = { "Functional" })
116 public void testGetScoresFromDescription_twoScores()
118 String regex = ".*([-0-9.+]+).+([-0-9.+]+).*";
119 final int count = pp.getScoresFromDescription("my Score",
120 "my Score Description", regex, true);
121 System.out.println("Matched " + count + " for " + regex);
122 assertEquals(3, count);
125 * Seq1 has two score values parsed out
127 AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation();
128 assertEquals(2, anns.length);
129 assertEquals(1d, anns[0].getScore(), 0.001d);
130 assertEquals("my Score Description", anns[0].description);
131 assertEquals("my Score", anns[0].label);
132 assertEquals(1d, anns[1].getScore(), 0.001d);
133 assertEquals("my Score Description (column 1)", anns[1].description);
134 assertEquals("my Score_1", anns[1].label);
137 * Seq2 has no score parsed out (is this right?)
139 assertNull(al.getSequenceAt(1).getAnnotation());
142 * Seq3 has two score values parsed out
144 // TODO parsed values (1.0 and 3.0) look wrong v description
145 // would expect 2.0 and 0.1
146 // undesired 'greedy' behaviour of regex?
147 anns = al.getSequenceAt(2).getAnnotation();
148 assertEquals(2, anns.length);
149 assertEquals(1d, anns[0].getScore(), 0.001d);
150 assertEquals("my Score Description", anns[0].description);
151 assertEquals("my Score", anns[0].label);
152 assertEquals(3d, anns[1].getScore(), 0.001d);
153 assertEquals("my Score Description (column 1)", anns[1].description);
154 assertEquals("my Score_1", anns[1].label);
157 * Seq3 has two score values parsed out
159 // TODO parsed values (1.0 and 4.0) look wrong v description
160 // would expect 3 and 0.01
161 anns = al.getSequenceAt(3).getAnnotation();
162 assertEquals(2, anns.length);
163 assertEquals(1d, anns[0].getScore(), 0.001d);
164 assertEquals("my Score Description", anns[0].description);
165 assertEquals("my Score", anns[0].label);
166 assertEquals(4d, anns[1].getScore(), 0.001d);
167 assertEquals("my Score Description (column 1)", anns[1].description);
168 assertEquals("my Score_1", anns[1].label);
172 * Test with a regex that looks for numbers separated by words - as currently
173 * used in Jalview (May 2015)
175 * @see AlignFrame.extractScores_actionPerformed
177 @Test(groups = { "Functional" })
178 public void testGetScoresFromDescription_wordBoundaries()
180 String regex = "\\W*([-+eE0-9.]+)";
181 List<SequenceI> seqs = al.getSequences();
182 seqs.get(0).setDescription("Ferredoxin");
183 seqs.get(1).setDescription(" Ferredoxin-1, chloroplast precursor");
184 seqs.get(2).setDescription("GH28E30p");
185 seqs.get(3).setDescription("At1g10960/T19D16_12");
186 final int count = pp.getScoresFromDescription("description column",
187 "score in description column ", regex, true);
188 assertEquals(3, count);
191 * No score parsable from seq1 description
193 AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation();
197 * Seq2 description has a '1' in it
199 anns = al.getSequenceAt(1).getAnnotation();
200 assertEquals(1, anns.length);
201 assertEquals(1d, anns[0].getScore(), 0.001d);
204 * Seq3 description has '28E30' in it
206 * Note: 1.8E308 or larger would result in 'Infinity'
208 anns = al.getSequenceAt(2).getAnnotation();
209 assertEquals(1, anns.length);
210 assertEquals(2.8E31d, anns[0].getScore(), 0.001d);
213 * Seq4 description has several numbers in it
215 anns = al.getSequenceAt(3).getAnnotation();
216 assertEquals(5, anns.length);
217 assertEquals(1d, anns[0].getScore(), 0.001d);
218 assertEquals(10960d, anns[1].getScore(), 0.001d);
219 assertEquals(19d, anns[2].getScore(), 0.001d);
220 assertEquals(16d, anns[3].getScore(), 0.001d);
221 assertEquals(12d, anns[4].getScore(), 0.001d);