From 6568287d7ca9ab336a83e27b9bfbd548a10cf748 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Sun, 4 Jun 2017 10:41:56 +0100 Subject: [PATCH] JAL-2291 SequenceI.getInsertionsAsBits for convenient logical operations --- src/jalview/datamodel/Sequence.java | 35 ++++++++++++++++++++++++++++++ src/jalview/datamodel/SequenceI.java | 8 +++++++ test/jalview/datamodel/SequenceTest.java | 8 +++++++ 3 files changed, 51 insertions(+) diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index b0faf21..a8cff26 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -29,6 +29,7 @@ import jalview.util.StringUtils; import java.util.ArrayList; import java.util.Arrays; +import java.util.BitSet; import java.util.Collections; import java.util.Enumeration; import java.util.List; @@ -819,6 +820,40 @@ public class Sequence extends ASequence implements SequenceI } @Override + public BitSet getInsertionsAsBits() + { + BitSet map = new BitSet(); + int lastj = -1, j = 0; + int pos = start; + int seqlen = sequence.length; + while ((j < seqlen)) + { + if (jalview.util.Comparison.isGap(sequence[j])) + { + if (lastj == -1) + { + lastj = j; + } + } + else + { + if (lastj != -1) + { + map.set(lastj, j - 1); + lastj = -1; + } + } + j++; + } + if (lastj != -1) + { + map.set(lastj, j - 1); + lastj = -1; + } + return map; + } + + @Override public void deleteChars(int i, int j) { int newstart = start, newend = end; diff --git a/src/jalview/datamodel/SequenceI.java b/src/jalview/datamodel/SequenceI.java index e81553b..12ddf60 100755 --- a/src/jalview/datamodel/SequenceI.java +++ b/src/jalview/datamodel/SequenceI.java @@ -20,6 +20,7 @@ */ package jalview.datamodel; +import java.util.BitSet; import java.util.List; import java.util.Vector; @@ -476,4 +477,11 @@ public interface SequenceI extends ASequenceI * list */ public List getPrimaryDBRefs(); + + /** + * + * @return BitSet corresponding to index [0,length) where Comparison.isGap() + * returns true. + */ + BitSet getInsertionsAsBits(); } diff --git a/test/jalview/datamodel/SequenceTest.java b/test/jalview/datamodel/SequenceTest.java index 08e6f7d..ae82d13 100644 --- a/test/jalview/datamodel/SequenceTest.java +++ b/test/jalview/datamodel/SequenceTest.java @@ -35,6 +35,7 @@ import jalview.util.MapList; import java.io.File; import java.util.ArrayList; import java.util.Arrays; +import java.util.BitSet; import java.util.List; import java.util.Vector; @@ -74,6 +75,13 @@ public class SequenceTest assertEquals("Gap interval 1 end wrong", 4, gapInt.get(0)[1]); assertEquals("Gap interval 2 start wrong", 6, gapInt.get(1)[0]); assertEquals("Gap interval 2 end wrong", 8, gapInt.get(1)[1]); + + BitSet gapfield = aseq.getInsertionsAsBits(); + BitSet expectedgaps = new BitSet(); + expectedgaps.set(2, 4); + expectedgaps.set(6, 8); + + assertEquals("getInsertionsAsBits not correct.", expectedgaps, gapfield); } @Test(groups = ("Functional")) -- 1.7.10.2