JAL-1270 main methods converted to JUnit
[jalview.git] / test / jalview / analysis / ParsePropertiesTest.java
1 package jalview.analysis;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5
6 import org.junit.Before;
7 import org.junit.Test;
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   @Before
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     // TODO - test the regex actually used by Jalview?
51     // \\W*([-+eE0-9.]+)
52     // see AlignFrame.extractScores_actionPerformed
53     String regex = ".*([-0-9.+]+)";
54     final int count = pp.getScoresFromDescription("my Score",
55             "my Score Description", regex, true);
56     System.out.println("Matched " + count + " for " + regex);
57     assertEquals(4, count);
58
59     /*
60      * Verify values 1/2/3/4 have been parsed from sequence descriptions
61      */
62     AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation();
63     assertEquals(1, anns.length);
64     assertEquals(1d, anns[0].getScore(), 0.001d);
65     assertEquals("my Score Description", anns[0].description);
66     assertEquals("my Score", anns[0].label);
67     anns = al.getSequenceAt(1).getAnnotation();
68     assertEquals(1, anns.length);
69     assertEquals(2d, anns[0].getScore(), 0.001d);
70     assertEquals("my Score Description", anns[0].description);
71     assertEquals("my Score", anns[0].label);
72     anns = al.getSequenceAt(2).getAnnotation();
73     assertEquals(1, anns.length);
74     assertEquals(3d, anns[0].getScore(), 0.001d);
75     anns = al.getSequenceAt(3).getAnnotation();
76     assertEquals(1, anns.length);
77     assertEquals(4d, anns[0].getScore(), 0.001d);
78   }
79
80   /**
81    * Test with a description pattern that matches any string (or none), followed
82    * by a 'number character' (0-9+.), followed by at least one separator
83    * character, followed by at least one 'number character', then any trailing
84    * characters.
85    */
86   @Test
87   public void testGetScoresFromDescription_twoScores()
88   {
89     String regex = ".*([-0-9.+]+).+([-0-9.+]+).*";
90     final int count = pp.getScoresFromDescription("my Score",
91             "my Score Description", regex, true);
92     System.out.println("Matched " + count + " for " + regex);
93     assertEquals(3, count);
94
95     /*
96      * Seq1 has two score values parsed out
97      */
98     AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation();
99     assertEquals(2, anns.length);
100     assertEquals(1d, anns[0].getScore(), 0.001d);
101     assertEquals("my Score Description", anns[0].description);
102     assertEquals("my Score", anns[0].label);
103     assertEquals(1d, anns[1].getScore(), 0.001d);
104     assertEquals("my Score Description (column 1)", anns[1].description);
105     assertEquals("my Score_1", anns[1].label);
106
107     /*
108      * Seq2 has no score parsed out (is this right?)
109      */
110     assertNull(al.getSequenceAt(1).getAnnotation());
111
112     /*
113      * Seq3 has two score values parsed out
114      */
115     // TODO parsed values (1.0 and 3.0) look wrong v description
116     // would expect 2.0 and 0.1
117     // undesired 'greedy' behaviour of regex?
118     anns = al.getSequenceAt(2).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(3d, 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      * Seq3 has two score values parsed out
129      */
130     // TODO parsed values (1.0 and 4.0) look wrong v description
131     // would expect 3 and 0.01
132     anns = al.getSequenceAt(3).getAnnotation();
133     assertEquals(2, anns.length);
134     assertEquals(1d, anns[0].getScore(), 0.001d);
135     assertEquals("my Score Description", anns[0].description);
136     assertEquals("my Score", anns[0].label);
137     assertEquals(4d, anns[1].getScore(), 0.001d);
138     assertEquals("my Score Description (column 1)", anns[1].description);
139     assertEquals("my Score_1", anns[1].label);
140   }
141 }