From 6d0af2bcecfa9eabab29c659ca151008db6379b2 Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Sat, 23 Apr 2016 18:36:11 +0100 Subject: [PATCH] JAL-2075 findPosition for SeqCigar (with test) --- src/jalview/datamodel/SeqCigar.java | 37 ++++++++++++++++++++++++++++++ test/jalview/datamodel/SeqCigarTest.java | 25 ++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/jalview/datamodel/SeqCigar.java b/src/jalview/datamodel/SeqCigar.java index 31957d7..98b0de5 100644 --- a/src/jalview/datamodel/SeqCigar.java +++ b/src/jalview/datamodel/SeqCigar.java @@ -68,6 +68,43 @@ public class SeqCigar extends CigarSimple } /** + * + * @param column + * @return position in sequence for column (or -1 if no match state exists) + */ + public int findPosition(int column) + { + int w = 0, ew, p = refseq.findPosition(start); + if (column < 0) + { + return -1; + } + if (range != null) + { + for (int i = 0; i < length; i++) + { + if (operation[i] == M || operation[i] == D) + { + p += range[i]; + } + if (operation[i] == M || operation[i] == I) + { + ew = w + range[i]; + if (column < ew) + { + if (operation[i] == I) + { + return -1; + } + return p - (ew - column); + } + w = ew; + } + } + } + return -1; + } + /** * Returns sequence as a string with cigar operations applied to it * diff --git a/test/jalview/datamodel/SeqCigarTest.java b/test/jalview/datamodel/SeqCigarTest.java index 705c773..8d3c878 100644 --- a/test/jalview/datamodel/SeqCigarTest.java +++ b/test/jalview/datamodel/SeqCigarTest.java @@ -23,6 +23,8 @@ package jalview.datamodel; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; +import jalview.util.Comparison; + import org.testng.annotations.Test; /** @@ -30,6 +32,29 @@ import org.testng.annotations.Test; */ public class SeqCigarTest { + @Test(groups = { "Functional" }) + public void testFindPosition() + { + SequenceI oseq = new Sequence("MySeq", "ASD---ASD---ASD", 37, 45); + oseq.createDatasetSequence(); + SeqCigar cs = new SeqCigar(oseq); + assertEquals(oseq.getSequenceAsString(), cs.getSequenceString('-')); + for (int c = 0, cLen = oseq.getLength(); c < cLen; c++) + { + int os_p = oseq.findPosition(c); + int cigar_p = cs.findPosition(c); + if (Comparison.isGap(oseq.getCharAt(c))) + { + assertEquals("Expected gap at position " + os_p + " column " + c, + -1, cigar_p); + } + else + { + assertEquals("Positions don't match for at column " + c, os_p, + cigar_p); + } + } + } /* * refactored 'as is' from main method * -- 1.7.10.2