JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / analysis / AAFrequencyTest.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.Sequence;
27 import jalview.datamodel.SequenceI;
28
29 import java.util.Hashtable;
30
31 import org.testng.annotations.Test;
32
33 public class AAFrequencyTest
34 {
35   private static final String C = AAFrequency.MAXCOUNT;
36
37   private static final String R = AAFrequency.MAXRESIDUE;
38
39   private static final String G = AAFrequency.PID_GAPS;
40
41   private static final String N = AAFrequency.PID_NOGAPS;
42
43   private static final String P = AAFrequency.PROFILE;
44
45   @Test(groups = { "Functional" })
46   public void testCalculate_noProfile()
47   {
48     SequenceI seq1 = new Sequence("Seq1", "CAGT");
49     SequenceI seq2 = new Sequence("Seq2", "CACT");
50     SequenceI seq3 = new Sequence("Seq3", "C--G");
51     SequenceI seq4 = new Sequence("Seq4", "CA-t");
52     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
53     Hashtable[] result = new Hashtable[seq1.getLength()];
54
55     AAFrequency.calculate(seqs, 0, seq1.getLength(), result, false);
56
57     // col 0 is 100% C
58     Hashtable col = result[0];
59     assertEquals(100f, (Float) col.get(G), 0.0001f);
60     assertEquals(100f, (Float) col.get(N), 0.0001f);
61     assertEquals(4, col.get(C));
62     assertEquals("C", col.get(R));
63     assertNull(col.get(P));
64
65     // col 1 is 75% A
66     col = result[1];
67     assertEquals(75f, (Float) col.get(G), 0.0001f);
68     assertEquals(100f, (Float) col.get(N), 0.0001f);
69     assertEquals(3, col.get(C));
70     assertEquals("A", col.get(R));
71
72     // col 2 is 50% G 50% C or 25/25 counting gaps
73     col = result[2];
74     assertEquals(25f, (Float) col.get(G), 0.0001f);
75     assertEquals(50f, (Float) col.get(N), 0.0001f);
76     assertEquals(1, col.get(C));
77     assertEquals("CG", col.get(R));
78
79     // col 3 is 75% T 25% G
80     col = result[3];
81     assertEquals(75f, (Float) col.get(G), 0.0001f);
82     assertEquals(75f, (Float) col.get(N), 0.0001f);
83     assertEquals(3, col.get(C));
84     assertEquals("T", col.get(R));
85   }
86
87   @Test(groups = { "Functional" })
88   public void testCalculate_withProfile()
89   {
90     SequenceI seq1 = new Sequence("Seq1", "CAGT");
91     SequenceI seq2 = new Sequence("Seq2", "CACT");
92     SequenceI seq3 = new Sequence("Seq3", "C--G");
93     SequenceI seq4 = new Sequence("Seq4", "CA-t");
94     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
95     Hashtable[] result = new Hashtable[seq1.getLength()];
96
97     AAFrequency.calculate(seqs, 0, seq1.getLength(), result, true);
98     int[][] profile = (int[][]) result[0].get(P);
99     assertEquals(4, profile[0]['C']);
100     assertEquals(4, profile[1][0]); // no of seqs
101     assertEquals(4, profile[1][1]); // nongapped in column
102
103     profile = (int[][]) result[1].get(P);
104     assertEquals(3, profile[0]['A']);
105     assertEquals(4, profile[1][0]);
106     assertEquals(3, profile[1][1]);
107
108     profile = (int[][]) result[2].get(P);
109     assertEquals(1, profile[0]['G']);
110     assertEquals(1, profile[0]['C']);
111     assertEquals(4, profile[1][0]);
112     assertEquals(2, profile[1][1]);
113
114     profile = (int[][]) result[3].get(P);
115     assertEquals(3, profile[0]['T']);
116     assertEquals(1, profile[0]['G']);
117     assertEquals(4, profile[1][0]);
118     assertEquals(4, profile[1][1]);
119   }
120
121   @Test(groups = { "Functional" })
122   public void testCalculate_withProfileTiming()
123   {
124     SequenceI seq1 = new Sequence("Seq1", "CAGT");
125     SequenceI seq2 = new Sequence("Seq2", "CACT");
126     SequenceI seq3 = new Sequence("Seq3", "C--G");
127     SequenceI seq4 = new Sequence("Seq4", "CA-t");
128     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
129     Hashtable[] result = new Hashtable[seq1.getLength()];
130
131     // ensure class loaded and initialized
132     AAFrequency.calculate(seqs, 0, seq1.getLength(), result, true);
133     int reps = 100000;
134     long start = System.currentTimeMillis();
135     for (int i = 0; i < reps; i++)
136     {
137       AAFrequency.calculate(seqs, 0, seq1.getLength(), result, true);
138     }
139     System.out.println(System.currentTimeMillis() - start);
140   }
141
142   @Test(groups = { "Functional" })
143   public void testGetPercentageFormat()
144   {
145     assertNull(AAFrequency.getPercentageFormat(0));
146     assertNull(AAFrequency.getPercentageFormat(99));
147     assertEquals("%3.1f", AAFrequency.getPercentageFormat(100).toString());
148     assertEquals("%3.1f", AAFrequency.getPercentageFormat(999).toString());
149     assertEquals("%3.2f", AAFrequency.getPercentageFormat(1000).toString());
150     assertEquals("%3.2f", AAFrequency.getPercentageFormat(9999).toString());
151   }
152 }