public class SymbolCounts
{
/**
- * the symbols seen (as char values)
+ * the symbols seen (as char values), in no particular order
*/
public final char[] symbols;
/*
* nucleotide symbols to count (including N unknown)
*/
- private static final String NUCS = "ACGTUN";
+ private static final String NUCS = "ACGNTU";
/*
* amino acid symbols to count (including X unknown)
/*
* keeps track of the maximum count value recorded
- * (if this class every allows decrements, would need to
+ * (if this class ever allows decrements, would need to
* calculate this on request instead)
*/
int maxCount;
* @param c
* @return the new value of the count for the character
*/
- public int add(char c)
+ public int add(final char c)
{
+ char u = toUpperCase(c);
int newValue = 0;
- int offset = getOffset(c);
+ int offset = getOffset(u);
/*
* offset 0 is reserved for gap counting, so 0 here means either
*/
if (offset == 0)
{
- if (Comparison.isGap(c))
+ if (Comparison.isGap(u))
{
newValue = addGap();
}
else
{
- newValue = addOtherCharacter(c);
+ newValue = addOtherCharacter(u);
}
}
else
}
/**
+ * Returns this character's offset in the count array
+ *
* @param c
* @return
*/
int getOffset(char c)
{
- /*
- * ensure upper-case (fails fast if it already is!)
- */
- if ('a' <= c && c <= 'z')
- {
- c = (char) (c + TOUPPERCASE);
- }
-
- /*
- * locate this character's offset in the count array
- */
int offset = 0;
if ('A' <= c && c <= 'Z')
{
}
/**
+ * @param c
+ * @return
+ */
+ protected char toUpperCase(final char c)
+ {
+ char u = c;
+ if ('a' <= c && c <= 'z')
+ {
+ u = (char) (c + TOUPPERCASE);
+ }
+ return u;
+ }
+
+ /**
* Increment count for some unanticipated character. The first time this
* called, a SparseCount is instantiated to hold these 'extra' counts.
*
*/
public void put(char c, int count)
{
- int offset = getOffset(c);
+ char u = toUpperCase(c);
+ int offset = getOffset(u);
/*
* offset 0 is reserved for gap counting, so 0 here means either
*/
if (offset == 0)
{
- if (Comparison.isGap(c))
+ if (Comparison.isGap(u))
{
- addGap();
+ set(0, count);
}
else
{
- setOtherCharacter(c, count);
+ setOtherCharacter(u, count);
maxCount = Math.max(maxCount, count);
}
}
*/
public int getCount(char c)
{
- int offset = getOffset(c);
+ char u = toUpperCase(c);
+ int offset = getOffset(u);
if (offset == 0)
{
- if (!Comparison.isGap(c))
+ if (!Comparison.isGap(u))
{
// should have called getGapCount()
- return otherData == null ? 0 : otherData.get(c);
+ return otherData == null ? 0 : otherData.get(u);
}
}
return useIntCounts ? intCounts[offset] : counts[offset];
/**
* Returns the character (or concatenated characters) for the symbol(s) with
* the given count in the profile. Can be used to get the modal residue by
- * supplying the modal count value. Returns an empty string if symbol has the
- * given count. The symbols are in alphabetic order of standard peptide or
+ * supplying the modal count value. Returns an empty string if no symbol has
+ * the given count. The symbols are in alphabetic order of standard peptide or
* nucleotide characters, followed by 'other' symbols if any.
*
* @return
}
/**
- * Returns the highest count for any symbol in the profile (excluding gap)
+ * Returns the highest count for any symbol(s) in the profile (excluding gap)
*
* @return
*/
}
}
}
+
+ /*
+ * include 'other' characters recorded (even if count is zero
+ * though that would be a strange use case)
+ */
if (otherData != null)
{
size += otherData.size();
}
/**
- * Returns those symbols that have a non-zero count (excluding the gap
- * symbol), with their counts. The symbols are in no special order. Returns an
- * array of size 2 whose first element is a char array of symbols, and second
- * element an int array of corresponding counts.
+ * Returns a data bean holding those symbols that have a non-zero count
+ * (excluding the gap symbol), with their counts.
*
- * @return an array [[char1, char2, ...] [char1Count, char2Count, ...] ... ]
+ * @return
*/
public SymbolCounts getSymbolCounts()
{
- char[] symbols = new char[size()];
- int[] values = new int[size()];
+ int size = size();
+ char[] symbols = new char[size];
+ int[] values = new int[size];
int j = 0;
if (useIntCounts)
{
for (int i = 0; i < otherData.size(); i++)
{
- int value = otherData.valueAt(i);
- if (value > 0)
- {
- symbols[j] = (char) otherData.keyAt(i);
- values[j] = otherData.valueAt(i);
- j++;
- }
+ symbols[j] = (char) otherData.keyAt(i);
+ values[j] = otherData.valueAt(i);
+ j++;
}
}
*/
public String getTooltip(int normaliseBy, int percentageDecPl)
{
- StringBuilder sb = new StringBuilder(64);
SymbolCounts symbolCounts = getSymbolCounts();
char[] ca = symbolCounts.symbols;
int[] vl = symbolCounts.values;
/*
* traverse in reverse order (highest count first) to build tooltip
*/
- for (int p = 0, c = ca.length - 1; c >= 0; c--)
+ boolean first = true;
+ StringBuilder sb = new StringBuilder(64);
+ for (int c = ca.length - 1; c >= 0; c--)
{
final char residue = ca[c];
- if (residue != '-')
- {
- // TODO combine residues which share a percentage
- // (see AAFrequency.completeCdnaConsensus)
- float tval = (vl[c] * 100f) / normaliseBy;
- sb.append((((p == 0) ? "" : "; "))).append(residue)
- .append(" ");
- Format.appendPercentage(sb, tval, percentageDecPl);
- sb.append("%");
- p++;
- }
+ // TODO combine residues which share a percentage
+ // (see AAFrequency.completeCdnaConsensus)
+ float tval = (vl[c] * 100f) / normaliseBy;
+ sb.append(first ? "" : "; ").append(residue).append(" ");
+ Format.appendPercentage(sb, tval, percentageDecPl);
+ sb.append("%");
+ first = false;
}
return sb.toString();
}