Merge branch 'develop' into bug/JAL-2346annotationChoice
[jalview.git] / src / jalview / structure / StructureMapping.java
index 78634e0..40789ed 100644 (file)
@@ -23,7 +23,9 @@ package jalview.structure;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.SequenceI;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
 public class StructureMapping
 {
@@ -47,6 +49,18 @@ public class StructureMapping
   // and atomNo
   HashMap<Integer, int[]> mapping;
 
+  /**
+   * Constructor
+   * 
+   * @param seq
+   * @param pdbfile
+   * @param pdbid
+   * @param chain
+   * @param mapping
+   *          a map from sequence to two values, { resNo, atomNo } in the
+   *          structure
+   * @param mappingDetails
+   */
   public StructureMapping(SequenceI seq, String pdbfile, String pdbid,
           String chain, HashMap<Integer, int[]> mapping,
           String mappingDetails)
@@ -111,6 +125,70 @@ public class StructureMapping
   }
 
   /**
+   * Returns a (possibly empty) list of [start, end] residue positions in the
+   * mapped structure, corresponding to the given range of sequence positions
+   * 
+   * @param fromSeqPos
+   * @param toSeqPos
+   * @return
+   */
+  public List<int[]> getPDBResNumRanges(int fromSeqPos, int toSeqPos)
+  {
+    List<int[]> result = new ArrayList<int[]>();
+    int startRes = -1;
+    int endRes = -1;
+
+    for (int i = fromSeqPos; i <= toSeqPos; i++)
+    {
+      int resNo = getPDBResNum(i);
+      if (resNo == UNASSIGNED_VALUE)
+      {
+        continue; // no mapping from this sequence position
+      }
+      if (startRes == -1)
+      {
+        startRes = resNo;
+        endRes = resNo;
+      }
+      if (resNo >= startRes && resNo <= endRes)
+      {
+        // within the current range - no change
+        continue;
+      }
+      if (resNo == startRes - 1)
+      {
+        // extend beginning of current range
+        startRes--;
+        continue;
+      }
+      if (resNo == endRes + 1)
+      {
+        // extend end of current range
+        endRes++;
+        continue;
+      }
+
+      /*
+       * resNo is not within or contiguous with last range,
+       * so write out the last range
+       */
+      result.add(new int[] { startRes, endRes });
+      startRes = resNo;
+      endRes = resNo;
+    }
+
+    /*
+     * and add the last range
+     */
+    if (startRes != -1)
+    {
+      result.add(new int[] { startRes, endRes });
+    }
+
+    return result;
+  }
+
+  /**
    * 
    * @param pdbResNum
    * @return -1 or the corresponding sequence position for a pdb residue number