X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fstructure%2FStructureMapping.java;h=320fa5299fa83c0ef80bdae1fff85a4ba9058081;hb=290e76172f5d4bd074764452c6a626fec4bd3b3b;hp=8a08b4739395de056f5f866326cdc2fc1c91c31e;hpb=fbc93aa214cd2976146843d2069444c59650ae54;p=jalview.git diff --git a/src/jalview/structure/StructureMapping.java b/src/jalview/structure/StructureMapping.java index 8a08b47..320fa52 100644 --- a/src/jalview/structure/StructureMapping.java +++ b/src/jalview/structure/StructureMapping.java @@ -23,6 +23,10 @@ package jalview.structure; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.SequenceI; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + public class StructureMapping { String mappingDetails; @@ -35,11 +39,31 @@ public class StructureMapping String pdbchain; - // Mapping index 0 is resNum, index 1 is atomNo - int[][] mapping; + 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, int[][] mapping, String mappingDetails) + String chain, HashMap mapping, + String mappingDetails) { sequence = seq; this.pdbfile = pdbfile; @@ -47,7 +71,6 @@ public class StructureMapping this.pdbchain = chain; this.mapping = mapping; this.mappingDetails = mappingDetails; - new Exception("StructureMapping with " + pdbfile).printStackTrace(); } public SequenceI getSequence() @@ -72,48 +95,115 @@ public class StructureMapping */ public int getAtomNum(int seqpos) { - if (mapping.length > seqpos) + int[] resNumAtomMap = mapping.get(seqpos); + if (resNumAtomMap != null) { - return mapping[seqpos][1]; + return resNumAtomMap[PDB_ATOM_NUM_INDEX]; } else { - return 0; + return UNASSIGNED_VALUE; } } /** * * @param seqpos - * @return 0 or the corresponding residue number for the sequence position + * @return UNASSIGNED_VALUE or the corresponding residue number for the + * sequence position */ public int getPDBResNum(int seqpos) { - if (mapping.length > seqpos) + int[] resNumAtomMap = mapping.get(seqpos); + if (resNumAtomMap != null) { - return mapping[seqpos][0]; + return resNumAtomMap[PDB_RES_NUM_INDEX]; } else { - return 0; + return UNASSIGNED_VALUE; } } /** + * 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++) + { + int resNo = getPDBResNum(i); + if (resNo == UNASSIGNED_VALUE) + { + 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; + } + + /* + * and add the last range + */ + if (startRes != -1) + { + result.add(new int[] { startRes, endRes }); + } + + return result; + } + + /** * * @param pdbResNum * @return -1 or the corresponding sequence position for a pdb residue number */ public int getSeqPos(int pdbResNum) { - for (int i = 0; i < mapping.length; i++) + for (Integer seqPos : mapping.keySet()) { - if (mapping[i][0] == pdbResNum) + if (pdbResNum == getPDBResNum(seqPos)) { - return i; + return seqPos; } } - return -1; + return UNASSIGNED_VALUE; } /** @@ -148,4 +238,14 @@ public class StructureMapping } return ala_copy; } + + public String getMappingDetailsOutput() + { + return mappingDetails; + } + + public HashMap getMapping() + { + return mapping; + } }