From 948228d480dcd77e194de69b393dfe5f29f51bb3 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Mon, 2 Sep 2013 18:09:08 +0100 Subject: [PATCH] JAL-1373 generate percentages with appropriate precision --- src/jalview/analysis/AAFrequency.java | 19 +++++++++++++++---- src/jalview/analysis/StructureFrequency.java | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/jalview/analysis/AAFrequency.java b/src/jalview/analysis/AAFrequency.java index 33660b5..656cdcb 100755 --- a/src/jalview/analysis/AAFrequency.java +++ b/src/jalview/analysis/AAFrequency.java @@ -19,6 +19,7 @@ package jalview.analysis; import java.util.*; +import jalview.util.Format; import jalview.datamodel.*; /** @@ -220,6 +221,17 @@ public class AAFrequency // initialised properly return; } + String fmtstr="%3.1f"; + int precision=2; + while (nseq>100) { + precision++; + nseq/=10; + } + if (precision>2) + { + fmtstr = "%"+(2+precision)+"."+precision+"f"; + } + Format fmt = new Format(fmtstr); for (int i = iStart; i < width; i++) { Hashtable hci; @@ -230,7 +242,6 @@ public class AAFrequency consensus.annotations[i] = null; continue; } - value = 0; Float fv; if (ignoreGapsInConsensusCalculation) @@ -266,7 +277,7 @@ public class AAFrequency tval = profile[0][alphabet[c]] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver += ((c == 0) ? "" : "; ") + alphabet[c] + " " - + ((int) tval) + "%"; + + fmt.form(tval) + "%"; } } else @@ -289,7 +300,7 @@ public class AAFrequency * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]; mouseOver += ((p == 0) ? "" : "; ") + ((char[]) ca[c])[0] - + " " + ((int) tval) + "%"; + + " " + fmt.form(tval) + "%"; p++; } @@ -299,7 +310,7 @@ public class AAFrequency } else { - mouseOver += ((int) value + "%"); + mouseOver += (fmt.form(value) + "%"); } consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ', value); diff --git a/src/jalview/analysis/StructureFrequency.java b/src/jalview/analysis/StructureFrequency.java index fe022c0..948bdc1 100644 --- a/src/jalview/analysis/StructureFrequency.java +++ b/src/jalview/analysis/StructureFrequency.java @@ -21,6 +21,7 @@ package jalview.analysis; import java.util.*; +import jalview.util.Format; import jalview.datamodel.*; /** @@ -309,6 +310,18 @@ public class StructureFrequency // initialised properly return; } + String fmtstr="%3.1f"; + int precision=2; + while (nseq>100) { + precision++; + nseq/=10; + } + if (precision>2) + { + fmtstr = "%"+(2+precision)+"."+precision+"f"; + } + Format fmt = new Format(fmtstr); + for (int i = iStart; i < width; i++) { Hashtable hci; @@ -385,7 +398,7 @@ public class StructureFrequency tval = (vl[c] * 100f / profile[1][ignoreGapsInConsensusCalculation ? 1 : 0]); mouseOver += ((p == 0) ? "" : "; ") + (char) ((int[]) ca[c])[0] - + (char) ((int[]) ca[c])[1] + " " + ((int) tval) + "%"; + + (char) ((int[]) ca[c])[1] + " " + fmt.form(tval) + "%"; p++; } @@ -395,7 +408,7 @@ public class StructureFrequency } else { - mouseOver += ((int) value + "%"); + mouseOver += (fmt.form(value) + "%"); } consensus.annotations[i] = new Annotation(maxRes, mouseOver, ' ', value); -- 1.7.10.2