Merge branch 'develop' into Release_2_9_0b1_Branch
[jalview.git] / src / jalview / datamodel / Mapping.java
index 1272538..06e2243 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -48,6 +48,11 @@ public class Mapping
     private final char[] alignedSeq;
 
     /*
+     * the sequence start residue
+     */
+    private int start;
+
+    /*
      * Next position (base 0) in the aligned sequence
      */
     private int alignedColumn = 0;
@@ -90,13 +95,14 @@ public class Mapping
     /**
      * Constructor
      * 
-     * @param cs
-     *          the aligned sequence characters
+     * @param seq
+     *          the aligned sequence
      * @param gapChar
      */
-    public AlignedCodonIterator(char[] cs, char gapChar)
+    public AlignedCodonIterator(SequenceI seq, char gapChar)
     {
-      this.alignedSeq = cs;
+      this.alignedSeq = seq.getSequence();
+      this.start = seq.getStart();
       this.gap = gapChar;
       fromRanges = map.getFromRanges().iterator();
       toRanges = map.getToRanges().iterator();
@@ -165,7 +171,8 @@ public class Mapping
       // i.e. code like getNextCodon()
       if (toPosition <= currentToRange[1])
       {
-        char pep = Mapping.this.to.getSequence()[toPosition - 1];
+        SequenceI seq = Mapping.this.to;
+        char pep = seq.getSequence()[toPosition - seq.getStart()];
         toPosition++;
         return String.valueOf(pep);
       }
@@ -242,7 +249,11 @@ public class Mapping
      */
     private int getAlignedColumn(int sequencePos)
     {
-      while (alignedBases < sequencePos
+      /*
+       * allow for offset e.g. treat pos 8 as 2 if sequence starts at 7
+       */
+      int truePos = sequencePos - (start - 1);
+      while (alignedBases < truePos
               && alignedColumn < alignedSeq.length)
       {
         if (alignedSeq[alignedColumn++] != gap)
@@ -692,7 +703,7 @@ public class Mapping
 
   public Iterator<AlignedCodon> getCodonIterator(SequenceI seq, char gapChar)
   {
-    return new AlignedCodonIterator(seq.getSequence(), gapChar);
+    return new AlignedCodonIterator(seq, gapChar);
   }
 
 }