2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.datamodel;
20 import java.util.Vector;
22 import jalview.util.MapList;
27 * Contains the start-end pairs mapping from the associated sequence to the
28 * sequence in the database coordinate system it also takes care of step
29 * difference between coordinate systems
34 * The seuqence that map maps the associated seuqence to (if any).
38 public Mapping(MapList map)
44 public Mapping(SequenceI to, MapList map)
51 * create a new mapping from
54 * the sequence being mapped
56 * int[] {start,end,start,end} series on associated sequence
58 * int[] {start,end,...} ranges on the reference frame being mapped
61 * step size on associated sequence
63 * step size on mapped frame
65 public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
67 this(to, new MapList(exon, is, i, j));
71 * create a duplicate (and independent) mapping object with the same reference
72 * to any SequenceI being mapped to.
76 public Mapping(Mapping map2)
78 if (map2 != this && map2 != null)
82 map = new MapList(map2.map);
91 public MapList getMap()
100 public void setMap(MapList map)
106 * Equals that compares both the to references and MapList mappings.
111 public boolean equals(Mapping other)
119 if ((map != null && other.map == null)
120 || (map == null && other.map != null))
122 if (map.equals(other.map))
128 * get the 'initial' position in the associated sequence for a position in the
129 * mapped reference frame
134 public int getPosition(int mpos)
138 int[] mp = map.shiftTo(mpos);
148 * gets boundary in direction of mapping
151 * in mapped reference frame
152 * @return int{start, end} positions in associated sequence (in direction of
155 public int[] getWord(int mpos)
159 return map.getToWord(mpos);
165 * width of mapped unit in associated sequence
168 public int getWidth()
172 return map.getFromRatio();
178 * width of unit in mapped reference frame
182 public int getMappedWidth()
186 return map.getToRatio();
192 * get mapped position in the associated reference frame for position pos in
193 * the associated sequence.
198 public int getMappedPosition(int pos)
202 int[] mp = map.shiftFrom(pos);
211 public int[] getMappedWord(int pos)
215 int[] mp = map.shiftFrom(pos);
219 { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
226 * locates the region of feature f in the associated sequence's reference
230 * @return one or more features corresponding to f
232 public SequenceFeature[] locateFeature(SequenceFeature f)
235 { // f.getBegin()!=f.getEnd()) {
238 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
241 // JBPNote - this isprobably not the right thing to doJBPHack
244 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
245 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
247 vf[v] = new SequenceFeature(f);
248 vf[v].setBegin(frange[i]);
249 vf[v].setEnd(frange[i + 1]);
250 if (frange.length > 2)
251 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
258 int[] word = getWord(f.getBegin());
259 if (word[0] < word[1])
267 word = getWord(f.getEnd());
268 if (word[0] > word[1])
277 // give up and just return the feature.
278 return new SequenceFeature[]
283 * return a series of contigs on the associated sequence corresponding to the
284 * from,to interval on the mapped reference frame
288 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
289 * associated sequence}
291 public int[] locateRange(int from, int to)
297 from = (map.getToLowest() < from) ? from : map.getToLowest();
298 to = (map.getToHighest() > to) ? to : map.getToHighest();
304 from = (map.getToHighest() > from) ? from : map.getToHighest();
305 to = (map.getToLowest() < to) ? to : map.getToLowest();
309 return map.locateInFrom(from, to);
316 * return a series of mapped contigs mapped from a range on the associated
323 public int[] locateMappedRange(int from, int to)
330 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
331 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
337 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
338 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
342 return map.locateInTo(from, to);
349 * return a new mapping object with a maplist modifed to only map the visible
350 * regions defined by viscontigs.
355 public Mapping intersectVisContigs(int[] viscontigs)
357 Mapping copy = new Mapping(this);
362 Vector toRange = new Vector();
363 Vector fromRange = new Vector();
364 for (int vc = 0; vc < viscontigs.length; vc += 2)
366 // find a mapped range in this visible region
367 int[] mpr = locateMappedRange(1 + viscontigs[vc],
368 viscontigs[vc + 1] - 1);
371 for (int m = 0; m < mpr.length; m += 2)
373 toRange.addElement(new int[]
374 { mpr[m], mpr[m + 1] });
375 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
376 for (int x = 0; x < xpos.length; x += 2)
378 fromRange.addElement(new int[]
379 { xpos[x], xpos[x + 1] });
384 int[] from = new int[fromRange.size() * 2];
385 int[] to = new int[toRange.size() * 2];
387 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
389 r = (int[]) fromRange.elementAt(f);
391 from[f * 2 + 1] = r[1];
393 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
395 r = (int[]) toRange.elementAt(f);
397 to[f * 2 + 1] = r[1];
399 copy.setMap(new MapList(from, to, map.getFromRatio(), map
405 public static void main(String[] args)
408 * trite test of the intersectVisContigs method for a simple DNA -> Protein
409 * exon map and a range of visContigs
411 MapList fk = new MapList(new int[]
412 { 1, 6, 8, 13, 15, 23 }, new int[]
414 Mapping m = new Mapping(fk);
415 Mapping m_1 = m.intersectVisContigs(new int[]
416 { fk.getFromLowest(), fk.getFromHighest() });
417 Mapping m_2 = m.intersectVisContigs(new int[]
419 System.out.println("" + m_1.map.getFromRanges());
424 * get the sequence being mapped to - if any
426 * @return null or a dataset sequence
428 public SequenceI getTo()
434 * set the dataset sequence being mapped to if any
438 public void setTo(SequenceI tto)
446 * @see java.lang.Object#finalize()
448 protected void finalize() throws Throwable