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 java.util.Vector;
25 import jalview.util.MapList;
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 seuqence that map maps the associated seuqence 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.
114 public boolean equals(Mapping other)
122 if ((map != null && other.map == null)
123 || (map == null && other.map != null))
125 if (map.equals(other.map))
131 * get the 'initial' position in the associated sequence for a position in the
132 * mapped reference frame
137 public int getPosition(int mpos)
141 int[] mp = map.shiftTo(mpos);
151 * gets boundary in direction of mapping
154 * in mapped reference frame
155 * @return int{start, end} positions in associated sequence (in direction of
158 public int[] getWord(int mpos)
162 return map.getToWord(mpos);
168 * width of mapped unit in associated sequence
171 public int getWidth()
175 return map.getFromRatio();
181 * width of unit in mapped reference frame
185 public int getMappedWidth()
189 return map.getToRatio();
195 * get mapped position in the associated reference frame for position pos in
196 * the associated sequence.
201 public int getMappedPosition(int pos)
205 int[] mp = map.shiftFrom(pos);
214 public int[] getMappedWord(int pos)
218 int[] mp = map.shiftFrom(pos);
222 { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
229 * locates the region of feature f in the associated sequence's reference
233 * @return one or more features corresponding to f
235 public SequenceFeature[] locateFeature(SequenceFeature f)
238 { // f.getBegin()!=f.getEnd()) {
241 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
244 // JBPNote - this isprobably not the right thing to doJBPHack
247 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
248 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
250 vf[v] = new SequenceFeature(f);
251 vf[v].setBegin(frange[i]);
252 vf[v].setEnd(frange[i + 1]);
253 if (frange.length > 2)
254 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
261 int[] word = getWord(f.getBegin());
262 if (word[0] < word[1])
270 word = getWord(f.getEnd());
271 if (word[0] > word[1])
280 // give up and just return the feature.
281 return new SequenceFeature[]
286 * return a series of contigs on the associated sequence corresponding to the
287 * from,to interval on the mapped reference frame
291 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
292 * associated sequence}
294 public int[] locateRange(int from, int to)
300 from = (map.getToLowest() < from) ? from : map.getToLowest();
301 to = (map.getToHighest() > to) ? to : map.getToHighest();
307 from = (map.getToHighest() > from) ? from : map.getToHighest();
308 to = (map.getToLowest() < to) ? to : map.getToLowest();
312 return map.locateInFrom(from, to);
319 * return a series of mapped contigs mapped from a range on the associated
326 public int[] locateMappedRange(int from, int to)
333 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
334 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
340 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
341 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
345 return map.locateInTo(from, to);
352 * return a new mapping object with a maplist modifed to only map the visible
353 * regions defined by viscontigs.
358 public Mapping intersectVisContigs(int[] viscontigs)
360 Mapping copy = new Mapping(this);
365 Vector toRange = new Vector();
366 Vector fromRange = new Vector();
367 for (int vc = 0; vc < viscontigs.length; vc += 2)
369 // find a mapped range in this visible region
370 int[] mpr = locateMappedRange(1 + viscontigs[vc],
371 viscontigs[vc + 1] - 1);
374 for (int m = 0; m < mpr.length; m += 2)
376 toRange.addElement(new int[]
377 { mpr[m], mpr[m + 1] });
378 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
379 for (int x = 0; x < xpos.length; x += 2)
381 fromRange.addElement(new int[]
382 { xpos[x], xpos[x + 1] });
387 int[] from = new int[fromRange.size() * 2];
388 int[] to = new int[toRange.size() * 2];
390 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
392 r = (int[]) fromRange.elementAt(f);
394 from[f * 2 + 1] = r[1];
396 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
398 r = (int[]) toRange.elementAt(f);
400 to[f * 2 + 1] = r[1];
402 copy.setMap(new MapList(from, to, map.getFromRatio(), map
408 public static void main(String[] args)
411 * trite test of the intersectVisContigs method for a simple DNA -> Protein
412 * exon map and a range of visContigs
414 MapList fk = new MapList(new int[]
415 { 1, 6, 8, 13, 15, 23 }, new int[]
417 Mapping m = new Mapping(fk);
418 Mapping m_1 = m.intersectVisContigs(new int[]
419 { fk.getFromLowest(), fk.getFromHighest() });
420 Mapping m_2 = m.intersectVisContigs(new int[]
422 System.out.println("" + m_1.map.getFromRanges());
427 * get the sequence being mapped to - if any
429 * @return null or a dataset sequence
431 public SequenceI getTo()
437 * set the dataset sequence being mapped to if any
441 public void setTo(SequenceI tto)
449 * @see java.lang.Object#finalize()
451 protected void finalize() throws Throwable