JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / test / jalview / analysis / ParsePropertiesTest.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 static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25
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
32 import java.util.List;
33
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
36
37 public class ParsePropertiesTest
38 {
39
40   private Alignment al;
41
42   private ParseProperties pp;
43
44   /**
45    * Construct an alignment with 4 sequences with varying description format
46    */
47   @BeforeMethod(alwaysRun = true)
48   public void setUp()
49   {
50     SequenceI[] seqs = new SequenceI[] {
51         new Sequence("sq1", "THISISAPLACEHOLDER"),
52         new Sequence("sq2", "THISISAPLACEHOLDER"),
53         new Sequence("sq3", "THISISAPLACEHOLDER"),
54         new Sequence("sq4", "THISISAPLACEHOLDER") };
55     seqs[0].setDescription("1 mydescription1");
56     seqs[1].setDescription("mydescription2");
57     seqs[2].setDescription("2. 0.1 mydescription+3");
58     seqs[3].setDescription("3 0.01 mydescription4");
59     al = new Alignment(seqs);
60
61     pp = new ParseProperties(al);
62
63   }
64
65   /**
66    * Test with a description pattern that matches any string ending in one or
67    * more 'number characters' (0-9+.), i.e. greedily matches any trailing
68    * numeric part of the string
69    */
70   @Test(groups = { "Functional" })
71   public void testGetScoresFromDescription()
72   {
73     String regex = ".*([-0-9.+]+)";
74     final int count = pp.getScoresFromDescription("my Score",
75             "my Score Description", regex, true);
76     System.out.println("Matched " + count + " for " + regex);
77     assertEquals(4, count);
78
79     /*
80      * Verify values 1/2/3/4 have been parsed from sequence descriptions
81      */
82     AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation();
83     assertEquals(1, anns.length);
84     assertEquals(1d, anns[0].getScore(), 0.001d);
85     assertEquals("my Score Description", anns[0].description);
86     assertEquals("my Score", anns[0].label);
87     anns = al.getSequenceAt(1).getAnnotation();
88     assertEquals(1, anns.length);
89     assertEquals(2d, anns[0].getScore(), 0.001d);
90     assertEquals("my Score Description", anns[0].description);
91     assertEquals("my Score", anns[0].label);
92     anns = al.getSequenceAt(2).getAnnotation();
93     assertEquals(1, anns.length);
94     assertEquals(3d, anns[0].getScore(), 0.001d);
95     anns = al.getSequenceAt(3).getAnnotation();
96     assertEquals(1, anns.length);
97     assertEquals(4d, anns[0].getScore(), 0.001d);
98   }
99
100   /**
101    * Test with a description pattern that matches any string (or none), followed
102    * by a 'number character' (0-9+.), followed by at least one separator
103    * character, followed by at least one 'number character', then any trailing
104    * characters.
105    */
106   @Test(groups = { "Functional" })
107   public void testGetScoresFromDescription_twoScores()
108   {
109     String regex = ".*([-0-9.+]+).+([-0-9.+]+).*";
110     final int count = pp.getScoresFromDescription("my Score",
111             "my Score Description", regex, true);
112     System.out.println("Matched " + count + " for " + regex);
113     assertEquals(3, count);
114
115     /*
116      * Seq1 has two score values parsed out
117      */
118     AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation();
119     assertEquals(2, anns.length);
120     assertEquals(1d, anns[0].getScore(), 0.001d);
121     assertEquals("my Score Description", anns[0].description);
122     assertEquals("my Score", anns[0].label);
123     assertEquals(1d, anns[1].getScore(), 0.001d);
124     assertEquals("my Score Description (column 1)", anns[1].description);
125     assertEquals("my Score_1", anns[1].label);
126
127     /*
128      * Seq2 has no score parsed out (is this right?)
129      */
130     assertNull(al.getSequenceAt(1).getAnnotation());
131
132     /*
133      * Seq3 has two score values parsed out
134      */
135     // TODO parsed values (1.0 and 3.0) look wrong v description
136     // would expect 2.0 and 0.1
137     // undesired 'greedy' behaviour of regex?
138     anns = al.getSequenceAt(2).getAnnotation();
139     assertEquals(2, anns.length);
140     assertEquals(1d, anns[0].getScore(), 0.001d);
141     assertEquals("my Score Description", anns[0].description);
142     assertEquals("my Score", anns[0].label);
143     assertEquals(3d, anns[1].getScore(), 0.001d);
144     assertEquals("my Score Description (column 1)", anns[1].description);
145     assertEquals("my Score_1", anns[1].label);
146
147     /*
148      * Seq3 has two score values parsed out
149      */
150     // TODO parsed values (1.0 and 4.0) look wrong v description
151     // would expect 3 and 0.01
152     anns = al.getSequenceAt(3).getAnnotation();
153     assertEquals(2, anns.length);
154     assertEquals(1d, anns[0].getScore(), 0.001d);
155     assertEquals("my Score Description", anns[0].description);
156     assertEquals("my Score", anns[0].label);
157     assertEquals(4d, anns[1].getScore(), 0.001d);
158     assertEquals("my Score Description (column 1)", anns[1].description);
159     assertEquals("my Score_1", anns[1].label);
160   }
161
162   /**
163    * Test with a regex that looks for numbers separated by words - as currently
164    * used in Jalview (May 2015)
165    * 
166    * @see AlignFrame.extractScores_actionPerformed
167    */
168   @Test(groups = { "Functional" })
169   public void testGetScoresFromDescription_wordBoundaries()
170   {
171     String regex = "\\W*([-+eE0-9.]+)";
172     List<SequenceI> seqs = al.getSequences();
173     seqs.get(0).setDescription("Ferredoxin");
174     seqs.get(1).setDescription(" Ferredoxin-1, chloroplast precursor");
175     seqs.get(2).setDescription("GH28E30p");
176     seqs.get(3).setDescription("At1g10960/T19D16_12");
177     final int count = pp.getScoresFromDescription("description column",
178             "score in description column ", regex, true);
179     assertEquals(3, count);
180
181     /*
182      * No score parsable from seq1 description
183      */
184     AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation();
185     assertNull(anns);
186
187     /*
188      * Seq2 description has a '1' in it
189      */
190     anns = al.getSequenceAt(1).getAnnotation();
191     assertEquals(1, anns.length);
192     assertEquals(1d, anns[0].getScore(), 0.001d);
193
194     /*
195      * Seq3 description has '28E30' in it
196      * 
197      * Note: 1.8E308 or larger would result in 'Infinity'
198      */
199     anns = al.getSequenceAt(2).getAnnotation();
200     assertEquals(1, anns.length);
201     assertEquals(2.8E31d, anns[0].getScore(), 0.001d);
202
203     /*
204      * Seq4 description has several numbers in it
205      */
206     anns = al.getSequenceAt(3).getAnnotation();
207     assertEquals(5, anns.length);
208     assertEquals(1d, anns[0].getScore(), 0.001d);
209     assertEquals(10960d, anns[1].getScore(), 0.001d);
210     assertEquals(19d, anns[2].getScore(), 0.001d);
211     assertEquals(16d, anns[3].getScore(), 0.001d);
212     assertEquals(12d, anns[4].getScore(), 0.001d);
213   }
214 }