From 71bdb187ec67fa90357f27fa63d37663e9cf5c01 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 26 Sep 2016 11:35:33 +0100 Subject: [PATCH] JAL-98 bug fix, string optimisation, sorted AA properties --- src/jalview/analysis/Conservation.java | 58 ++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/jalview/analysis/Conservation.java b/src/jalview/analysis/Conservation.java index 26a7b9f..0467900 100755 --- a/src/jalview/analysis/Conservation.java +++ b/src/jalview/analysis/Conservation.java @@ -28,9 +28,9 @@ import jalview.ext.android.SparseIntArray; import jalview.schemes.ResidueProperties; import java.awt.Color; -import java.util.Hashtable; import java.util.List; import java.util.Map; +import java.util.TreeMap; import java.util.Vector; /** @@ -189,22 +189,22 @@ public class Conservation */ public void calculate() { - int thresh, j, jSize = sequences.length; + int jSize = sequences.length; // int[] values; // Replaces residueHash SparseIntArray values = new SparseIntArray(); - char c; - total = new Hashtable[maxLength]; + total = new Map[maxLength]; for (int i = start; i <= end; i++) { // values = new int[255]; + values.clear(); - for (j = 0; j < jSize; j++) + for (int j = 0; j < jSize; j++) { if (sequences[j].getLength() > i) { - c = sequences[j].getCharAt(i); + char c = sequences[j].getCharAt(i); if (canonicaliseAa) { // lookup the base aa code symbol @@ -231,25 +231,28 @@ public class Conservation c = toUpperCase(c); } // values[c]++; - values.put(c, values.get(c) + 1); + values.add(c, 1); } else { // values['-']++; - values.put('-', values.get('-') + 1); + values.add('-', 1); } } // What is the count threshold to count the residues in residueHash() - thresh = (threshold * (jSize)) / 100; + int thresh = (threshold * jSize) / 100; // loop over all the found residues - Hashtable resultHash = new Hashtable(); - for (char v = '-'; v < 'Z'; v++) + // Hashtable resultHash = new Hashtable(); + Map resultHash = new TreeMap(); + // for (char v = '-'; v < 'Z'; v++) + for (int key = 0; key < values.size(); key++) { - + char v = (char) values.keyAt(key); // if (values[v] > thresh) - if (values.get(v) > thresh) + if (values.valueAt(key) > thresh) { String res = String.valueOf(v); @@ -364,7 +367,7 @@ public class Conservation */ public void verdict(boolean consflag, float percentageGaps) { - StringBuffer consString = new StringBuffer(); + StringBuilder consString = new StringBuilder(end); // NOTE THIS SHOULD CHECK IF THE CONSEQUENCE ALREADY // EXISTS AND NOT OVERWRITE WITH '-', BUT THIS CASE @@ -379,7 +382,9 @@ public class Conservation int[] gapcons = countConsNGaps(i); int totGaps = gapcons[1]; float pgaps = ((float) totGaps * 100) / sequences.length; - consSymbs[i - start] = new String(); + StringBuilder positives = new StringBuilder(64); + StringBuilder negatives = new StringBuilder(32); + // consSymbs[i - start] = ""; if (percentageGaps > pgaps) { @@ -394,7 +399,9 @@ public class Conservation { if (result == 1) { - consSymbs[i - start] = type + " " + consSymbs[i - start]; + // consSymbs[i - start] = type + " " + consSymbs[i - start]; + positives.append(positives.length() == 0 ? "" : " "); + positives.append(type); count++; } } @@ -404,16 +411,31 @@ public class Conservation { if (result == 0) { - consSymbs[i - start] = consSymbs[i - start] + " !" + type; + /* + * add negatively conserved properties on the end + */ + // consSymbs[i - start] = consSymbs[i - start] + " !" + type; + negatives.append(negatives.length() == 0 ? "" : " "); + negatives.append("!").append(type); } else { - consSymbs[i - start] = type + " " + consSymbs[i - start]; + /* + * put positively conserved properties on the front + */ + // consSymbs[i - start] = type + " " + consSymbs[i - start]; + positives.append(positives.length() == 0 ? "" : " "); + positives.append(type); } count++; } } } + if (negatives.length() > 0) + { + positives.append(" ").append(negatives); + } + consSymbs[i - start] = positives.toString(); if (count < 10) { -- 1.7.10.2