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.Mapping;
25 import jalview.datamodel.SequenceI;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 public class StructureMapping
33 String mappingDetails;
43 public static final int UNASSIGNED_VALUE = Integer.MIN_VALUE;
45 private static final int PDB_RES_NUM_INDEX = 0;
47 private static final int PDB_ATOM_NUM_INDEX = 1;
49 // Mapping key is residue index while value is an array containing PDB resNum,
51 HashMap<Integer, int[]> mapping;
53 jalview.datamodel.Mapping seqToPdbMapping = null;
62 * a map from sequence to two values, { resNo, atomNo } in the
64 * @param mappingDetails
66 public StructureMapping(SequenceI seq, String pdbfile, String pdbid,
67 String chain, HashMap<Integer, int[]> mapping,
68 String mappingDetails)
71 this.pdbfile = pdbfile;
73 this.pdbchain = chain;
74 this.mapping = mapping;
75 this.mappingDetails = mappingDetails;
78 public StructureMapping(SequenceI seq, String pdbFile2, String pdbId2,
79 String chain, HashMap<Integer, int[]> mapping2,
80 String mappingOutput, Mapping seqToPdbMapping)
82 this(seq, pdbFile2, pdbId2, chain, mapping2, mappingOutput);
83 this.seqToPdbMapping = seqToPdbMapping;
86 public SequenceI getSequence()
91 public String getChain()
96 public String getPdbId()
104 * @return 0 or corresponding atom number for the sequence position
106 public int getAtomNum(int seqpos)
108 int[] resNumAtomMap = mapping.get(seqpos);
109 if (resNumAtomMap != null)
111 return resNumAtomMap[PDB_ATOM_NUM_INDEX];
115 return UNASSIGNED_VALUE;
122 * @return UNASSIGNED_VALUE or the corresponding residue number for the
125 public int getPDBResNum(int seqpos)
127 int[] resNumAtomMap = mapping.get(seqpos);
128 if (resNumAtomMap != null)
130 return resNumAtomMap[PDB_RES_NUM_INDEX];
134 return UNASSIGNED_VALUE;
139 * Returns a (possibly empty) list of [start, end] residue positions in the
140 * mapped structure, corresponding to the given range of sequence positions
146 public List<int[]> getPDBResNumRanges(int fromSeqPos, int toSeqPos)
148 List<int[]> result = new ArrayList<int[]>();
152 for (int i = fromSeqPos; i <= toSeqPos; i++)
154 int resNo = getPDBResNum(i);
155 if (resNo == UNASSIGNED_VALUE)
157 continue; // no mapping from this sequence position
164 if (resNo >= startRes && resNo <= endRes)
166 // within the current range - no change
169 if (resNo == startRes - 1)
171 // extend beginning of current range
175 if (resNo == endRes + 1)
177 // extend end of current range
183 * resNo is not within or contiguous with last range,
184 * so write out the last range
186 result.add(new int[] { startRes, endRes });
192 * and add the last range
196 result.add(new int[] { startRes, endRes });
205 * @return -1 or the corresponding sequence position for a pdb residue number
207 public int getSeqPos(int pdbResNum)
209 for (Integer seqPos : mapping.keySet())
211 if (pdbResNum == getPDBResNum(seqPos))
216 return UNASSIGNED_VALUE;
220 * transfer a copy of an alignment annotation row in the PDB chain coordinate
221 * system onto the mapped sequence
224 * @return the copy that was remapped to the mapped sequence
225 * @note this method will create a copy and add it to the dataset sequence for
226 * the mapped sequence as well as the mapped sequence (if it is not a
229 public AlignmentAnnotation transfer(AlignmentAnnotation ana)
231 AlignmentAnnotation ala_copy = new AlignmentAnnotation(ana);
232 SequenceI ds = sequence;
233 while (ds.getDatasetSequence() != null)
235 ds = ds.getDatasetSequence();
237 // need to relocate annotation from pdb coordinates to local sequence
238 // -1,-1 doesn't look at pdbresnum but fails to remap sequence positions...
240 ala_copy.remap(ds, mapping, -1, -1, 0);
241 ds.addAlignmentAnnotation(ala_copy);
244 // mapping wasn't to an original dataset sequence, so we make a copy on
245 // the mapped sequence too
246 ala_copy = new AlignmentAnnotation(ala_copy);
247 sequence.addAlignmentAnnotation(ala_copy);
252 public String getMappingDetailsOutput()
254 return mappingDetails;
257 public HashMap<Integer, int[]> getMapping()
262 public Mapping getSeqToPdbMapping()
264 return seqToPdbMapping;