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;
29 import java.util.Map.Entry;
31 public class StructureMapping
33 public static final int UNASSIGNED = -1;
35 public static final int PDB_RES_NUM_INDEX = 0;
37 public static final int PDB_ATOM_NUM_INDEX = 1;
40 * Space character constant, for consistent representation when no chain
43 public static String NO_CHAIN = " ";
45 String mappingDetails;
55 // Mapping key is residue index while value is an array containing PDB resNum,
57 HashMap<Integer, int[]> mapping;
67 * a map from sequence to two values, { resNo, atomNo } in the
69 * @param mappingDetails
71 public StructureMapping(SequenceI seq, String pdbfile, String pdbid,
72 String chain, HashMap<Integer, int[]> mapping,
73 String mappingDetails)
76 this.pdbfile = pdbfile;
78 this.pdbchain = chain;
79 this.mapping = mapping;
80 this.mappingDetails = mappingDetails;
83 public SequenceI getSequence()
88 public String getChain()
93 public String getPdbId()
99 * Answers the structure atom number mapped to the given sequence position, or
105 public int getAtomNum(int seqpos)
107 int[] resNumAtomMap = mapping.get(seqpos);
108 return (resNumAtomMap == null ? UNASSIGNED
109 : resNumAtomMap[PDB_ATOM_NUM_INDEX]);
113 * Answers the structure residue number mapped to the given sequence position,
114 * or -1 if no mapping
119 public int getPDBResNum(int seqpos)
121 int[] resNumAtomMap = mapping.get(seqpos);
122 return (resNumAtomMap == null ? UNASSIGNED
123 : resNumAtomMap[PDB_RES_NUM_INDEX]);
127 * Returns a (possibly empty) list of [start, end] residue positions in the
128 * mapped structure, corresponding to the given range of sequence positions
134 public List<int[]> getPDBResNumRanges(int fromSeqPos, int toSeqPos)
136 List<int[]> result = new ArrayList<int[]>();
140 for (int i = fromSeqPos; i <= toSeqPos; i++)
142 int resNo = getPDBResNum(i);
143 if (resNo == UNASSIGNED)
145 continue; // no mapping from this sequence position
152 if (resNo >= startRes && resNo <= endRes)
154 // within the current range - no change
157 if (resNo == startRes - 1)
159 // extend beginning of current range
163 if (resNo == endRes + 1)
165 // extend end of current range
171 * resNo is not within or contiguous with last range,
172 * so write out the last range
174 result.add(new int[] { startRes, endRes });
180 * and add the last range
184 result.add(new int[] { startRes, endRes });
191 * Answers the sequence position mapped to the given structure residue number,
192 * or -1 if no mapping is found
197 public int getSeqPos(int pdbResNum)
199 for (Entry<Integer, int[]> map : mapping.entrySet())
201 if (pdbResNum == map.getValue()[PDB_RES_NUM_INDEX])
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;