X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fstructure%2FStructureMapping.java;h=b8a0fc6550d0ff99074bcec374a3fbafa4b123a8;hb=19b5db52eb1285a22f8225f4baa914c2d7b15edd;hp=a62f1aea7413e5d7ad770d01b8aa207869ea95cf;hpb=ad15cff29620f960119f80176f1fd443da9f6763;p=jalview.git diff --git a/src/jalview/structure/StructureMapping.java b/src/jalview/structure/StructureMapping.java index a62f1ae..b8a0fc6 100644 --- a/src/jalview/structure/StructureMapping.java +++ b/src/jalview/structure/StructureMapping.java @@ -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 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 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 getPDBResNumRanges(int fromSeqPos, int toSeqPos) + { + List result = new ArrayList(); + 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 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; + } + }