apply gpl development license
[jalview.git] / src / jalview / analysis / AAFrequency.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 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 java.util.*;
22
23 import jalview.datamodel.*;
24
25 /**
26  * Takes in a vector or array of sequences and column start and column end and
27  * returns a new Hashtable[] of size maxSeqLength, if Hashtable not supplied.
28  * This class is used extensively in calculating alignment colourschemes that
29  * depend on the amount of conservation in each alignment column.
30  * 
31  * @author $author$
32  * @version $Revision$
33  */
34 public class AAFrequency
35 {
36   // No need to store 1000s of strings which are not
37   // visible to the user.
38   public static final String MAXCOUNT = "C";
39
40   public static final String MAXRESIDUE = "R";
41
42   public static final String PID_GAPS = "G";
43
44   public static final String PID_NOGAPS = "N";
45
46   public static final Hashtable[] calculate(Vector sequences, int start,
47           int end)
48   {
49     SequenceI[] seqs = new SequenceI[sequences.size()];
50     int width = 0;
51     for (int i = 0; i < sequences.size(); i++)
52     {
53       seqs[i] = (SequenceI) sequences.elementAt(i);
54       if (seqs[i].getLength() > width)
55       {
56         width = seqs[i].getLength();
57       }
58     }
59
60     Hashtable[] reply = new Hashtable[width];
61
62     if (end >= width)
63     {
64       end = width;
65     }
66
67     calculate(seqs, start, end, reply);
68
69     return reply;
70   }
71
72   public static final void calculate(SequenceI[] sequences, int start,
73           int end, Hashtable[] result)
74   {
75     Hashtable residueHash;
76     int maxCount, nongap, i, j, v, jSize = sequences.length;
77     String maxResidue;
78     char c;
79     float percentage;
80
81     int[] values = new int[255];
82
83     char[] seq;
84
85     for (i = start; i < end; i++)
86     {
87       residueHash = new Hashtable();
88       maxCount = 0;
89       maxResidue = "";
90       nongap = 0;
91       values = new int[255];
92
93       for (j = 0; j < jSize; j++)
94       {
95         seq = sequences[j].getSequence();
96         if (seq.length > i)
97         {
98           c = seq[i];
99
100           if (c == '.' || c == ' ')
101           {
102             c = '-';
103           }
104
105           if (c == '-')
106           {
107             values['-']++;
108             continue;
109           }
110           else if ('a' <= c && c <= 'z')
111           {
112             c -= 32; // ('a' - 'A');
113           }
114
115           nongap++;
116           values[c]++;
117
118         }
119         else
120         {
121           values['-']++;
122         }
123       }
124
125       for (v = 'A'; v < 'Z'; v++)
126       {
127         if (values[v] < 2 || values[v] < maxCount)
128         {
129           continue;
130         }
131
132         if (values[v] > maxCount)
133         {
134           maxResidue = String.valueOf((char) v);
135         }
136         else if (values[v] == maxCount)
137         {
138           maxResidue += String.valueOf((char) v);
139         }
140         maxCount = values[v];
141       }
142
143       if (maxResidue.length() == 0)
144       {
145         maxResidue = "-";
146       }
147
148       residueHash.put(MAXCOUNT, new Integer(maxCount));
149       residueHash.put(MAXRESIDUE, maxResidue);
150
151       percentage = ((float) maxCount * 100) / (float) jSize;
152       residueHash.put(PID_GAPS, new Float(percentage));
153
154       percentage = ((float) maxCount * 100) / (float) nongap;
155       residueHash.put(PID_NOGAPS, new Float(percentage));
156       result[i] = residueHash;
157     }
158   }
159 }