X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fstructure%2FStructureMapping.java;h=4174f5b8316eb82d900217207a4567d31ea1712d;hb=bb5d9c75833e2d0b9ef8f66b8fcd7d5649a3271f;hp=923633152e0821d559fcfe1c27ddcff78bda1334;hpb=e0172c233914b64dc561b0a3b0d9aad850e99efc;p=jalview.git diff --git a/src/jalview/structure/StructureMapping.java b/src/jalview/structure/StructureMapping.java index 9236331..4174f5b 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.8.2) - * Copyright (C) 2014 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. * @@ -21,10 +21,21 @@ package jalview.structure; import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.Mapping; import jalview.datamodel.SequenceI; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + public class StructureMapping { + public static final int UNASSIGNED_VALUE = Integer.MIN_VALUE; + + private static final int PDB_RES_NUM_INDEX = 0; + + private static final int PDB_ATOM_NUM_INDEX = 1; + String mappingDetails; SequenceI sequence; @@ -35,11 +46,27 @@ 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; + jalview.datamodel.Mapping seqToPdbMapping = null; + + /** + * 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; @@ -49,6 +76,14 @@ public class StructureMapping this.mappingDetails = mappingDetails; } + public StructureMapping(SequenceI seq, String pdbFile2, String pdbId2, + String chain, HashMap mapping2, + String mappingOutput, Mapping seqToPdbMapping) + { + this(seq, pdbFile2, pdbId2, chain, mapping2, mappingOutput); + this.seqToPdbMapping = seqToPdbMapping; + } + public SequenceI getSequence() { return sequence; @@ -64,40 +99,122 @@ public class StructureMapping return pdbid; } + /** + * + * @param seqpos + * @return 0 or corresponding atom number for the sequence position + */ 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 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; } /** @@ -106,11 +223,146 @@ public class StructureMapping * * @param ana * @return the copy that was remapped to the mapped sequence + * @note this method will create a copy and add it to the dataset sequence for + * the mapped sequence as well as the mapped sequence (if it is not a + * dataset sequence). */ public AlignmentAnnotation transfer(AlignmentAnnotation ana) { AlignmentAnnotation ala_copy = new AlignmentAnnotation(ana); - ala_copy.remap(sequence, mapping, 0, -1, 1); + SequenceI ds = sequence; + while (ds.getDatasetSequence() != null) + { + ds = ds.getDatasetSequence(); + } + // need to relocate annotation from pdb coordinates to local sequence + // -1,-1 doesn't look at pdbresnum but fails to remap sequence positions... + + ala_copy.remap(ds, mapping, -1, -1, 0); + ds.addAlignmentAnnotation(ala_copy); + if (ds != sequence) + { + // mapping wasn't to an original dataset sequence, so we make a copy on + // the mapped sequence too + ala_copy = new AlignmentAnnotation(ala_copy); + sequence.addAlignmentAnnotation(ala_copy); + } return ala_copy; } + + public String getMappingDetailsOutput() + { + return mappingDetails; + } + + public HashMap getMapping() + { + return mapping; + } + + public Mapping getSeqToPdbMapping() + { + return seqToPdbMapping; + } + + /** + * A hash function that satisfies the contract that if two mappings are + * equal(), they have the same hashCode + */ + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + + ((mappingDetails == null) ? 0 : mappingDetails.hashCode()); + result = prime * result + + ((pdbchain == null) ? 0 : pdbchain.hashCode()); + result = prime * result + ((pdbfile == null) ? 0 : pdbfile.hashCode()); + result = prime * result + ((pdbid == null) ? 0 : pdbid.hashCode()); + result = prime * result + + ((seqToPdbMapping == null) ? 0 : seqToPdbMapping.hashCode()); + result = prime * result + + ((sequence == null) ? 0 : sequence.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + if (obj == null) + { + return false; + } + if (getClass() != obj.getClass()) + { + return false; + } + StructureMapping other = (StructureMapping) obj; + if (mappingDetails == null) + { + if (other.mappingDetails != null) + { + return false; + } + } + else if (!mappingDetails.equals(other.mappingDetails)) + { + return false; + } + if (pdbchain == null) + { + if (other.pdbchain != null) + { + return false; + } + } + else if (!pdbchain.equals(other.pdbchain)) + { + return false; + } + if (pdbfile == null) + { + if (other.pdbfile != null) + { + return false; + } + } + else if (!pdbfile.equals(other.pdbfile)) + { + return false; + } + if (pdbid == null) + { + if (other.pdbid != null) + { + return false; + } + } + else if (!pdbid.equals(other.pdbid)) + { + return false; + } + if (seqToPdbMapping == null) + { + if (other.seqToPdbMapping != null) + { + return false; + } + } + else if (!seqToPdbMapping.equals(other.seqToPdbMapping)) + { + return false; + } + if (sequence != other.sequence) + { + return false; + } + + return true; + } }