2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.structure;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.SequenceI;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 public class StructureMapping
32 String mappingDetails;
42 public static final int UNASSIGNED_VALUE = -1;
44 private static final int PDB_RES_NUM_INDEX = 0;
46 private static final int PDB_ATOM_NUM_INDEX = 1;
48 // Mapping key is residue index while value is an array containing PDB resNum,
50 HashMap<Integer, int[]> mapping;
60 * a map from sequence to two values, { resNo, atomNo } in the
62 * @param mappingDetails
64 public StructureMapping(SequenceI seq, String pdbfile, String pdbid,
65 String chain, HashMap<Integer, int[]> mapping,
66 String mappingDetails)
69 this.pdbfile = pdbfile;
71 this.pdbchain = chain;
72 this.mapping = mapping;
73 this.mappingDetails = mappingDetails;
76 public SequenceI getSequence()
81 public String getChain()
86 public String getPdbId()
94 * @return 0 or corresponding atom number for the sequence position
96 public int getAtomNum(int seqpos)
98 int[] resNumAtomMap = mapping.get(seqpos);
99 if (resNumAtomMap != null)
101 return resNumAtomMap[PDB_ATOM_NUM_INDEX];
105 return UNASSIGNED_VALUE;
112 * @return UNASSIGNED_VALUE or the corresponding residue number for the
115 public int getPDBResNum(int seqpos)
117 int[] resNumAtomMap = mapping.get(seqpos);
118 if (resNumAtomMap != null)
120 return resNumAtomMap[PDB_RES_NUM_INDEX];
124 return UNASSIGNED_VALUE;
129 * Returns a (possibly empty) list of [start, end] residue positions in the
130 * mapped structure, corresponding to the given range of sequence positions
136 public List<int[]> getPDBResNumRanges(int fromSeqPos, int toSeqPos)
138 List<int[]> result = new ArrayList<int[]>();
142 for (int i = fromSeqPos; i <= toSeqPos; i++)
144 int resNo = getPDBResNum(i);
145 if (resNo == UNASSIGNED_VALUE)
147 continue; // no mapping from this sequence position
154 if (resNo >= startRes && resNo <= endRes)
156 // within the current range - no change
159 if (resNo == startRes - 1)
161 // extend beginning of current range
165 if (resNo == endRes + 1)
167 // extend end of current range
173 * resNo is not within or contiguous with last range,
174 * so write out the last range
176 result.add(new int[] { startRes, endRes });
182 * and add the last range
186 result.add(new int[] { startRes, endRes });
195 * @return -1 or the corresponding sequence position for a pdb residue number
197 public int getSeqPos(int pdbResNum)
199 for (Integer seqPos : mapping.keySet())
201 if (pdbResNum == getPDBResNum(seqPos))
206 return UNASSIGNED_VALUE;
210 * transfer a copy of an alignment annotation row in the PDB chain coordinate
211 * system onto the mapped sequence
214 * @return the copy that was remapped to the mapped sequence
215 * @note this method will create a copy and add it to the dataset sequence for
216 * the mapped sequence as well as the mapped sequence (if it is not a
219 public AlignmentAnnotation transfer(AlignmentAnnotation ana)
221 AlignmentAnnotation ala_copy = new AlignmentAnnotation(ana);
222 SequenceI ds = sequence;
223 while (ds.getDatasetSequence() != null)
225 ds = ds.getDatasetSequence();
227 // need to relocate annotation from pdb coordinates to local sequence
228 // -1,-1 doesn't look at pdbresnum but fails to remap sequence positions...
230 ala_copy.remap(ds, mapping, -1, -1, 0);
231 ds.addAlignmentAnnotation(ala_copy);
234 // mapping wasn't to an original dataset sequence, so we make a copy on
235 // the mapped sequence too
236 ala_copy = new AlignmentAnnotation(ala_copy);
237 sequence.addAlignmentAnnotation(ala_copy);
242 public String getMappingDetailsOutput()
244 return mappingDetails;
247 public HashMap<Integer, int[]> getMapping()