JAL-2434 omit unmapped positions from mapping
[jalview.git] / src / jalview / structure / StructureMapping.java
index 06a67ff..b8a0fc6 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b1)
- * Copyright (C) 2015 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -23,8 +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;
@@ -35,11 +52,25 @@ public class StructureMapping
 
   String pdbchain;
 
-  // Mapping index 0 is resNum, index 1 is atomNo
-  int[][] mapping;
+  // 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, int[][] mapping, String mappingDetails)
+          String chain, HashMap<Integer, int[]> mapping,
+          String mappingDetails)
   {
     sequence = seq;
     this.pdbfile = pdbfile;
@@ -65,54 +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)
   {
-    if (mapping.length > seqpos)
-    {
-      return mapping[seqpos][1];
-    }
-    else
-    {
-      return 0;
-    }
+    int[] resNumAtomMap = mapping.get(seqpos);
+    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)
   {
-    if (mapping.length > seqpos)
+    int[] resNumAtomMap = mapping.get(seqpos);
+    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 mapping[seqpos][0];
+      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 0;
+      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 (int i = 0; i < mapping.length; i++)
+    for (Entry<Integer, int[]> map : mapping.entrySet())
     {
-      if (mapping[i][0] == pdbResNum)
+      if (pdbResNum == map.getValue()[PDB_RES_NUM_INDEX])
       {
-        return i;
+        return map.getKey();
       }
     }
-    return -1;
+    return UNASSIGNED;
   }
 
   /**
@@ -147,4 +238,10 @@ public class StructureMapping
     }
     return ala_copy;
   }
+
+  public String getMappingDetailsOutput()
+  {
+    return mappingDetails;
+  }
+
 }