X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fstructure%2FStructureMapping.java;h=b8a0fc6550d0ff99074bcec374a3fbafa4b123a8;hb=19b5db52eb1285a22f8225f4baa914c2d7b15edd;hp=1b87fecd245e9106ddd31ceca50d28e5e1b6af80;hpb=db93a1adcbe0a4eaaf06e0a70ade0d6c5c1961c3;p=jalview.git diff --git a/src/jalview/structure/StructureMapping.java b/src/jalview/structure/StructureMapping.java index 1b87fec..b8a0fc6 100644 --- a/src/jalview/structure/StructureMapping.java +++ b/src/jalview/structure/StructureMapping.java @@ -1,6 +1,6 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2) - * 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,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 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 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 getPDBResNumRanges(int fromSeqPos, int toSeqPos) + { + List result = new ArrayList(); + 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 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 getMapping() - { - return mapping; - } }