JAL-2434 omit unmapped positions from mapping
[jalview.git] / src / jalview / structure / StructureMapping.java
index d6eb1eb..b8a0fc6 100644 (file)
@@ -26,12 +26,19 @@ 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, recommended for consistent representation when no
-   * chain specified
+   * Space character constant, for consistent representation when no chain
+   * specified
    */
   public static String NO_CHAIN = " ";
 
@@ -45,12 +52,6 @@ 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;
@@ -95,39 +96,31 @@ 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[PDB_RES_NUM_INDEX];
-    }
-    else
-    {
-      return UNASSIGNED_VALUE;
-    }
+    return (resNumAtomMap == null ? UNASSIGNED
+            : resNumAtomMap[PDB_RES_NUM_INDEX]);
   }
 
   /**
@@ -147,7 +140,7 @@ public class StructureMapping
     for (int i = fromSeqPos; i <= toSeqPos; i++)
     {
       int resNo = getPDBResNum(i);
-      if (resNo == UNASSIGNED_VALUE)
+      if (resNo == UNASSIGNED)
       {
         continue; // no mapping from this sequence position
       }
@@ -195,20 +188,22 @@ public class StructureMapping
   }
 
   /**
+   * 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;
   }
 
   /**