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