updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / analysis / AAFrequency.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19 package jalview.analysis;
20
21 import jalview.datamodel.*;
22
23 import java.util.*;
24
25
26 /**
27  * Takes in a vector of sequences and column start and column end
28  * and returns a vector of size (end-start+1). Each element of the
29  * vector contains a hashtable with the keys being residues and
30  * the values being the count of each residue in that column.
31  * This class is used extensively in calculating alignment colourschemes
32  * that depend on the amount of conservation in each alignment column.
33  * @author $author$
34  * @version $Revision$
35  */
36 public class AAFrequency
37 {
38     /** Takes in a vector of sequences and column start and column end
39     * and returns a vector of size (end-start+1). Each element of the
40     * vector contains a hashtable with the keys being residues and
41     * the values being the count of each residue in that column.
42     * This class is used extensively in calculating alignment colourschemes
43     * that depend on the amount of conservation in each alignment column. */
44     public static final Vector calculate(Vector sequences, int start, int end)
45     {
46       Vector result = new Vector();
47       Hashtable residueHash;
48       int count, maxCount, nongap, i, j, jSize = sequences.size();
49       String maxResidue, sequence, res;
50       float percentage;
51
52         for (i = start; i <= end; i++)
53         {
54             residueHash = new Hashtable();
55             maxCount = 0;
56             maxResidue = "-";
57             nongap = 0;
58
59             for (j = 0; j < jSize; j++)
60             {
61                 if (sequences.elementAt(j) instanceof Sequence)
62                 {
63                     sequence = ((Sequence) sequences.elementAt(j)).getSequence();
64
65                     if (sequence.length() > i)
66                     {
67                         res = String.valueOf(Character.toUpperCase(sequence.charAt(i)));
68
69                         if (jalview.util.Comparison.isGap(res.charAt(0)))
70                         {
71                             res = "-"; // we always use this for gaps in the property vectors
72                         }
73                         else
74                         {   nongap++;    }
75
76                         if (residueHash.containsKey(res))
77                         {
78                             count = ((Integer) residueHash.get(res)).intValue();
79                             count++;
80
81                             if (!jalview.util.Comparison.isGap(res.charAt(0)) &&
82                                     (count >= maxCount))
83                             {
84                                 if (count > maxCount)
85                                 {
86                                     maxResidue = res;
87                                 }
88                                 else if (maxResidue.indexOf(res) == -1)
89                                 {
90                                     maxResidue += res;
91                                 }
92
93                                 maxCount = count;
94                             }
95
96                             residueHash.put(res, new Integer(count));
97                         }
98                         else
99                         {
100                             residueHash.put(res, new Integer(1));
101                         }
102                     }
103                     else
104                     {
105                         if (residueHash.containsKey("-"))
106                         {
107                             count = ((Integer) residueHash.get("-")).intValue();
108                             count++;
109                             residueHash.put("-", new Integer(count));
110                         }
111                         else
112                         {
113                             residueHash.put("-", new Integer(1));
114                         }
115                     }
116                 }
117             }
118
119             residueHash.put("maxCount", new Integer(maxCount));
120             residueHash.put("maxResidue", maxResidue);
121
122
123             //Size is redundant at present if we calculate percentage here
124             //residueHash.put("size", new Integer(jSize));
125             //residueHash.put("nogaps", new Integer(nongap));
126
127             percentage = ((float)maxCount*100) / (float)jSize;
128             residueHash.put("pid_gaps", new Float(percentage) );
129
130             percentage = ((float)maxCount*100) / (float)nongap;
131             residueHash.put("pid_nogaps", new Float(percentage) );
132             result.addElement(residueHash);
133         }
134
135
136
137         return result;
138     }
139 }