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