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
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.
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.
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
19 package jalview.analysis;
23 import jalview.datamodel.*;
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.
34 public class AAFrequency
36 // No need to store 1000s of strings which are not
37 // visible to the user.
38 public static final String MAXCOUNT = "C";
40 public static final String MAXRESIDUE = "R";
42 public static final String PID_GAPS = "G";
44 public static final String PID_NOGAPS = "N";
46 public static final Hashtable[] calculate(Vector sequences, int start,
49 SequenceI[] seqs = new SequenceI[sequences.size()];
51 for (int i = 0; i < sequences.size(); i++)
53 seqs[i] = (SequenceI) sequences.elementAt(i);
54 if (seqs[i].getLength() > width)
56 width = seqs[i].getLength();
60 Hashtable[] reply = new Hashtable[width];
67 calculate(seqs, start, end, reply);
72 public static final void calculate(SequenceI[] sequences, int start,
73 int end, Hashtable[] result)
75 Hashtable residueHash;
76 int maxCount, nongap, i, j, v, jSize = sequences.length;
81 int[] values = new int[255];
85 for (i = start; i < end; i++)
87 residueHash = new Hashtable();
91 values = new int[255];
93 for (j = 0; j < jSize; j++)
95 seq = sequences[j].getSequence();
100 if (c == '.' || c == ' ')
110 else if ('a' <= c && c <= 'z')
112 c -= 32; // ('a' - 'A');
125 for (v = 'A'; v < 'Z'; v++)
127 if (values[v] < 2 || values[v] < maxCount)
132 if (values[v] > maxCount)
134 maxResidue = String.valueOf((char) v);
136 else if (values[v] == maxCount)
138 maxResidue += String.valueOf((char) v);
140 maxCount = values[v];
143 if (maxResidue.length() == 0)
148 residueHash.put(MAXCOUNT, new Integer(maxCount));
149 residueHash.put(MAXRESIDUE, maxResidue);
151 percentage = ((float) maxCount * 100) / (float) jSize;
152 residueHash.put(PID_GAPS, new Float(percentage));
154 percentage = ((float) maxCount * 100) / (float) nongap;
155 residueHash.put(PID_NOGAPS, new Float(percentage));
156 result[i] = residueHash;
161 * Compute all or part of the annotation row from the given consensus hashtable
162 * @param consensus - pre-allocated annotation row
166 * @param ignoreGapsInConsensusCalculation
168 public static void completeConsensus(AlignmentAnnotation consensus,
169 Hashtable[] hconsensus, int iStart, int width,
170 boolean ignoreGapsInConsensusCalculation)
172 for (int i = iStart; i < width; i++)
175 if (ignoreGapsInConsensusCalculation)
177 value = ((Float) hconsensus[i].get(AAFrequency.PID_NOGAPS))
182 value = ((Float) hconsensus[i].get(AAFrequency.PID_GAPS))
186 String maxRes = hconsensus[i].get(AAFrequency.MAXRESIDUE)
188 String mouseOver = hconsensus[i].get(AAFrequency.MAXRESIDUE)
191 if (maxRes.length() > 1)
193 mouseOver = "[" + maxRes + "] ";
197 mouseOver += ((int) value + "%");
198 consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ',