From 166a912dd82096db2777150675cf115bf8332b6c Mon Sep 17 00:00:00 2001 From: kiramt Date: Wed, 7 Jun 2017 11:32:20 +0100 Subject: [PATCH] JAL-2591 Hidden columns regions can be output to string + test --- src/jalview/datamodel/HiddenColumns.java | 30 +++++++++++++++++++++++++ src/jalview/io/AnnotationFile.java | 18 +++------------ src/jalview/io/JSONFile.java | 11 ++------- test/jalview/datamodel/HiddenColumnsTest.java | 12 ++++++++++ 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 1e1a58b..0036402 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -33,6 +33,36 @@ public class HiddenColumns implements Iterable } /** + * Output regions data as a string. String is in the format: + * reg0[0]reg0[1]reg1[0]reg1[1] ... regn[1] + * + * @param delimiter + * string to delimit regions + * @param betweenstring + * to put between start and end region values + * @return regions formatted according to delimiter and between strings + */ + public String regionsToString(String delimiter, String between) + { + try + { + lock.readLock().lock(); + StringBuilder regionBuilder = new StringBuilder(); + for (int[] range : hiddenColumns) + { + regionBuilder.append(delimiter).append(range[0]).append(between) + .append(range[1]); + } + + regionBuilder.deleteCharAt(0); + return regionBuilder.toString(); + } finally + { + lock.readLock().unlock(); + } + } + + /** * Find the number of hidden columns * * @return number of hidden columns diff --git a/src/jalview/io/AnnotationFile.java b/src/jalview/io/AnnotationFile.java index 2b78cb6..b669ee5 100755 --- a/src/jalview/io/AnnotationFile.java +++ b/src/jalview/io/AnnotationFile.java @@ -171,21 +171,9 @@ public class AnnotationFile if (cs != null && cs.hasHiddenColumns()) { text.append("VIEW_HIDECOLS\t"); - boolean comma = false; - for (int[] r : cs.getHiddenRegions()) - { - if (!comma) - { - comma = true; - } - else - { - text.append(","); - } - text.append(r[0]); - text.append("-"); - text.append(r[1]); - } + + String regions = cs.regionsToString(",", "-"); + text.append(regions); text.append("\n"); } // TODO: allow efficient recovery of annotation data shown in several diff --git a/src/jalview/io/JSONFile.java b/src/jalview/io/JSONFile.java index a48ee33..c293cd4 100644 --- a/src/jalview/io/JSONFile.java +++ b/src/jalview/io/JSONFile.java @@ -279,15 +279,8 @@ public class JSONFile extends AlignFile implements ComplexAlignFile // hidden column business if (getViewport().hasHiddenColumns()) { - StringBuilder hiddenColsBuilder = new StringBuilder(); - for (int[] range : getViewport().getAlignment().getHiddenColumns()) - { - hiddenColsBuilder.append(";").append(range[0]).append("-") - .append(range[1]); - } - - hiddenColsBuilder.deleteCharAt(0); - hiddenSections[0] = hiddenColsBuilder.toString(); + hiddenSections[0] = getViewport().getAlignment().getHiddenColumns() + .regionsToString(";", "-"); } // hidden rows/seqs business diff --git a/test/jalview/datamodel/HiddenColumnsTest.java b/test/jalview/datamodel/HiddenColumnsTest.java index b9becf3..9f77eaa 100644 --- a/test/jalview/datamodel/HiddenColumnsTest.java +++ b/test/jalview/datamodel/HiddenColumnsTest.java @@ -600,4 +600,16 @@ public class HiddenColumnsTest assertEquals(5, positions.get(1).intValue()); assertEquals(8, positions.get(2).intValue()); } + + @Test(groups = { "Functional" }) + public void testRegionsToString() + { + HiddenColumns hc = new HiddenColumns(); + hc.hideColumns(3, 7); + hc.hideColumns(10, 10); + hc.hideColumns(14, 15); + + String result = hc.regionsToString(",", "--"); + assertEquals("3--7,10--10,14--15", result); + } } -- 1.7.10.2