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