JAL-2434 omit unmapped positions from mapping
[jalview.git] / src / jalview / structure / StructureMapping.java
index 78634e0..b8a0fc6 100644 (file)
@@ -23,10 +23,25 @@ package jalview.structure;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.SequenceI;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
+import java.util.Map.Entry;
 
 public class StructureMapping
 {
+  public static final int UNASSIGNED = -1;
+
+  public static final int PDB_RES_NUM_INDEX = 0;
+
+  public static final int PDB_ATOM_NUM_INDEX = 1;
+
+  /**
+   * Space character constant, for consistent representation when no chain
+   * specified
+   */
+  public static String NO_CHAIN = " ";
+
   String mappingDetails;
 
   SequenceI sequence;
@@ -37,16 +52,22 @@ public class StructureMapping
 
   String pdbchain;
 
-  public static final int UNASSIGNED_VALUE = -1;
-
-  private static final int PDB_RES_NUM_INDEX = 0;
-
-  private static final int PDB_ATOM_NUM_INDEX = 1;
-
   // Mapping key is residue index while value is an array containing PDB resNum,
   // 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)
@@ -75,56 +96,114 @@ public class StructureMapping
   }
 
   /**
+   * Answers the structure atom number mapped to the given sequence position, or
+   * -1 if no mapping
    * 
    * @param seqpos
-   * @return 0 or corresponding atom number for the sequence position
+   * @return
    */
   public int getAtomNum(int seqpos)
   {
     int[] resNumAtomMap = mapping.get(seqpos);
-    if (resNumAtomMap != null)
-    {
-      return resNumAtomMap[PDB_ATOM_NUM_INDEX];
-    }
-    else
-    {
-      return UNASSIGNED_VALUE;
-    }
+    return (resNumAtomMap == null ? UNASSIGNED
+            : resNumAtomMap[PDB_ATOM_NUM_INDEX]);
   }
 
   /**
+   * Answers the structure residue number mapped to the given sequence position,
+   * or -1 if no mapping
    * 
    * @param seqpos
-   * @return 0 or the corresponding residue number for the sequence position
+   * @return
    */
   public int getPDBResNum(int seqpos)
   {
     int[] resNumAtomMap = mapping.get(seqpos);
-    if (resNumAtomMap != null)
+    return (resNumAtomMap == null ? UNASSIGNED
+            : resNumAtomMap[PDB_RES_NUM_INDEX]);
+  }
+
+  /**
+   * 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++)
     {
-      return resNumAtomMap[PDB_RES_NUM_INDEX];
+      int resNo = getPDBResNum(i);
+      if (resNo == UNASSIGNED)
+      {
+        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;
     }
-    else
+
+    /*
+     * and add the last range
+     */
+    if (startRes != -1)
     {
-      return UNASSIGNED_VALUE;
+      result.add(new int[] { startRes, endRes });
     }
+
+    return result;
   }
 
   /**
+   * Answers the sequence position mapped to the given structure residue number,
+   * or -1 if no mapping is found
    * 
    * @param pdbResNum
-   * @return -1 or the corresponding sequence position for a pdb residue number
+   * @return
    */
   public int getSeqPos(int pdbResNum)
   {
-    for (Integer seqPos : mapping.keySet())
+    for (Entry<Integer, int[]> map : mapping.entrySet())
     {
-      if (pdbResNum == getPDBResNum(seqPos))
+      if (pdbResNum == map.getValue()[PDB_RES_NUM_INDEX])
       {
-        return seqPos;
+        return map.getKey();
       }
     }
-    return UNASSIGNED_VALUE;
+    return UNASSIGNED;
   }
 
   /**
@@ -165,8 +244,4 @@ public class StructureMapping
     return mappingDetails;
   }
 
-  public HashMap<Integer, int[]> getMapping()
-  {
-    return mapping;
-  }
 }