2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.datamodel;
23 import jalview.util.MapList;
25 import java.util.Vector;
30 * Contains the start-end pairs mapping from the associated sequence to the
31 * sequence in the database coordinate system. It also takes care of step
32 * difference between coordinate systems.
37 * The sequence that map maps the associated sequence to (if any).
41 public Mapping(MapList map)
47 public Mapping(SequenceI to, MapList map)
54 * create a new mapping from
57 * the sequence being mapped
59 * int[] {start,end,start,end} series on associated sequence
61 * int[] {start,end,...} ranges on the reference frame being mapped
64 * step size on associated sequence
66 * step size on mapped frame
68 public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
70 this(to, new MapList(exon, is, i, j));
74 * create a duplicate (and independent) mapping object with the same reference
75 * to any SequenceI being mapped to.
79 public Mapping(Mapping map2)
81 if (map2 != this && map2 != null)
85 map = new MapList(map2.map);
94 public MapList getMap()
103 public void setMap(MapList map)
109 * Equals that compares both the to references and MapList mappings.
115 public boolean equals(Object o)
117 if (o == null || !(o instanceof Mapping))
121 Mapping other = (Mapping) o;
130 if ((map != null && other.map == null)
131 || (map == null && other.map != null))
135 if (map.equals(other.map))
143 * get the 'initial' position in the associated sequence for a position in the
144 * mapped reference frame
149 public int getPosition(int mpos)
153 int[] mp = map.shiftTo(mpos);
163 * gets boundary in direction of mapping
166 * in mapped reference frame
167 * @return int{start, end} positions in associated sequence (in direction of
170 public int[] getWord(int mpos)
174 return map.getToWord(mpos);
180 * width of mapped unit in associated sequence
183 public int getWidth()
187 return map.getFromRatio();
193 * width of unit in mapped reference frame
197 public int getMappedWidth()
201 return map.getToRatio();
207 * get mapped position in the associated reference frame for position pos in
208 * the associated sequence.
213 public int getMappedPosition(int pos)
217 int[] mp = map.shiftFrom(pos);
226 public int[] getMappedWord(int pos)
230 int[] mp = map.shiftFrom(pos);
234 { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
241 * locates the region of feature f in the associated sequence's reference
245 * @return one or more features corresponding to f
247 public SequenceFeature[] locateFeature(SequenceFeature f)
250 { // f.getBegin()!=f.getEnd()) {
253 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
256 // JBPNote - this isprobably not the right thing to doJBPHack
259 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
260 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
262 vf[v] = new SequenceFeature(f);
263 vf[v].setBegin(frange[i]);
264 vf[v].setEnd(frange[i + 1]);
265 if (frange.length > 2)
267 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
275 int[] word = getWord(f.getBegin());
276 if (word[0] < word[1])
284 word = getWord(f.getEnd());
285 if (word[0] > word[1])
294 // give up and just return the feature.
295 return new SequenceFeature[]
300 * return a series of contigs on the associated sequence corresponding to the
301 * from,to interval on the mapped reference frame
305 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
306 * associated sequence}
308 public int[] locateRange(int from, int to)
314 from = (map.getToLowest() < from) ? from : map.getToLowest();
315 to = (map.getToHighest() > to) ? to : map.getToHighest();
323 from = (map.getToHighest() > from) ? from : map.getToHighest();
324 to = (map.getToLowest() < to) ? to : map.getToLowest();
330 return map.locateInFrom(from, to);
337 * return a series of mapped contigs mapped from a range on the associated
344 public int[] locateMappedRange(int from, int to)
351 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
352 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
360 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
361 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
367 return map.locateInTo(from, to);
374 * return a new mapping object with a maplist modifed to only map the visible
375 * regions defined by viscontigs.
380 public Mapping intersectVisContigs(int[] viscontigs)
382 Mapping copy = new Mapping(this);
387 Vector toRange = new Vector();
388 Vector fromRange = new Vector();
389 for (int vc = 0; vc < viscontigs.length; vc += 2)
391 // find a mapped range in this visible region
392 int[] mpr = locateMappedRange(1 + viscontigs[vc],
393 viscontigs[vc + 1] - 1);
396 for (int m = 0; m < mpr.length; m += 2)
398 toRange.addElement(new int[]
399 { mpr[m], mpr[m + 1] });
400 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
401 for (int x = 0; x < xpos.length; x += 2)
403 fromRange.addElement(new int[]
404 { xpos[x], xpos[x + 1] });
409 int[] from = new int[fromRange.size() * 2];
410 int[] to = new int[toRange.size() * 2];
412 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
414 r = (int[]) fromRange.elementAt(f);
416 from[f * 2 + 1] = r[1];
418 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
420 r = (int[]) toRange.elementAt(f);
422 to[f * 2 + 1] = r[1];
424 copy.setMap(new MapList(from, to, map.getFromRatio(), map
430 public static void main(String[] args)
433 * trite test of the intersectVisContigs method for a simple DNA -> Protein
434 * exon map and a range of visContigs
436 MapList fk = new MapList(new int[]
437 { 1, 6, 8, 13, 15, 23 }, new int[]
439 Mapping m = new Mapping(fk);
440 Mapping m_1 = m.intersectVisContigs(new int[]
441 { fk.getFromLowest(), fk.getFromHighest() });
442 Mapping m_2 = m.intersectVisContigs(new int[]
444 System.out.println("" + m_1.map.getFromRanges());
449 * get the sequence being mapped to - if any
451 * @return null or a dataset sequence
453 public SequenceI getTo()
459 * set the dataset sequence being mapped to if any
463 public void setTo(SequenceI tto)
471 * @see java.lang.Object#finalize()
473 protected void finalize() throws Throwable