2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
21 import java.util.Vector;
23 import jalview.util.MapList;
28 * Contains the start-end pairs mapping from the associated sequence to the
29 * sequence in the database coordinate system it also takes care of step
30 * difference between coordinate systems
35 * The seuqence that map maps the associated seuqence to (if any).
39 public Mapping(MapList map)
45 public Mapping(SequenceI to, MapList map)
52 * create a new mapping from
55 * the sequence being mapped
57 * int[] {start,end,start,end} series on associated sequence
59 * int[] {start,end,...} ranges on the reference frame being
62 * step size on associated sequence
64 * step size on mapped frame
66 public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
68 this(to, new MapList(exon, is, i, j));
72 * create a duplicate (and independent) mapping object with the same reference
73 * to any SequenceI being mapped to.
77 public Mapping(Mapping map2)
79 if (map2 != this && map2 != null)
83 map = new MapList(map2.map);
92 public MapList getMap()
101 public void setMap(MapList map)
107 * Equals that compares both the to references and MapList mappings.
112 public boolean equals(Mapping other)
120 if ((map != null && other.map == null)
121 || (map == null && other.map != null))
123 if (map.equals(other.map))
129 * get the 'initial' position in the associated sequence for a position in the
130 * mapped reference frame
135 public int getPosition(int mpos)
139 int[] mp = map.shiftTo(mpos);
149 * gets boundary in direction of mapping
152 * in mapped reference frame
153 * @return int{start, end} positions in associated sequence (in direction of
156 public int[] getWord(int mpos)
160 return map.getToWord(mpos);
166 * width of mapped unit in associated sequence
169 public int getWidth()
173 return map.getFromRatio();
179 * width of unit in mapped reference frame
183 public int getMappedWidth()
187 return map.getToRatio();
193 * get mapped position in the associated reference frame for position pos in
194 * the associated sequence.
199 public int getMappedPosition(int pos)
203 int[] mp = map.shiftFrom(pos);
212 public int[] getMappedWord(int pos)
216 int[] mp = map.shiftFrom(pos);
220 { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
227 * locates the region of feature f in the associated sequence's reference
231 * @return one or more features corresponding to f
233 public SequenceFeature[] locateFeature(SequenceFeature f)
236 { // f.getBegin()!=f.getEnd()) {
239 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
242 // JBPNote - this isprobably not the right thing to doJBPHack
245 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
246 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
248 vf[v] = new SequenceFeature(f);
249 vf[v].setBegin(frange[i]);
250 vf[v].setEnd(frange[i + 1]);
251 if (frange.length > 2)
252 vf[v].setDescription(f.getDescription() + "\nPart " + (v + 1));
259 int[] word = getWord(f.getBegin());
260 if (word[0] < word[1])
268 word = getWord(f.getEnd());
269 if (word[0] > word[1])
278 // give up and just return the feature.
279 return new SequenceFeature[]
284 * return a series of contigs on the associated sequence corresponding to the
285 * from,to interval on the mapped reference frame
289 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
290 * associated sequence}
292 public int[] locateRange(int from, int to)
298 from = (map.getToLowest() < from) ? from : map.getToLowest();
299 to = (map.getToHighest() > to) ? to : map.getToHighest();
305 from = (map.getToHighest() > from) ? from : map.getToHighest();
306 to = (map.getToLowest() < to) ? to : map.getToLowest();
310 return map.locateInFrom(from, to);
317 * return a series of mapped contigs mapped from a range on the associated
324 public int[] locateMappedRange(int from, int to)
331 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
332 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
338 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
339 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
343 return map.locateInTo(from, to);
350 * return a new mapping object with a maplist modifed to only map the visible
351 * regions defined by viscontigs.
356 public Mapping intersectVisContigs(int[] viscontigs)
358 Mapping copy = new Mapping(this);
363 Vector toRange = new Vector();
364 Vector fromRange = new Vector();
365 for (int vc = 0; vc < viscontigs.length; vc += 2)
367 // find a mapped range in this visible region
368 int[] mpr = locateMappedRange(1 + viscontigs[vc],
369 viscontigs[vc + 1] - 1);
372 for (int m = 0; m < mpr.length; m += 2)
374 toRange.addElement(new int[]
375 { mpr[m], mpr[m + 1] });
376 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
377 for (int x = 0; x < xpos.length; x += 2)
379 fromRange.addElement(new int[]
380 { xpos[x], xpos[x + 1] });
385 int[] from = new int[fromRange.size() * 2];
386 int[] to = new int[toRange.size() * 2];
388 for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
390 r = (int[]) fromRange.elementAt(f);
392 from[f * 2 + 1] = r[1];
394 for (int f = 0, fSize = toRange.size(); f < fSize; f++)
396 r = (int[]) toRange.elementAt(f);
398 to[f * 2 + 1] = r[1];
400 copy.setMap(new MapList(from, to, map.getFromRatio(), map
406 public static void main(String[] args)
409 * trite test of the intersectVisContigs method for a simple DNA -> Protein
410 * exon map and a range of visContigs
412 MapList fk = new MapList(new int[]
413 { 1, 6, 8, 13, 15, 23 }, new int[]
415 Mapping m = new Mapping(fk);
416 Mapping m_1 = m.intersectVisContigs(new int[]
417 { fk.getFromLowest(), fk.getFromHighest() });
418 Mapping m_2 = m.intersectVisContigs(new int[]
420 System.out.println("" + m_1.map.getFromRanges());
425 * get the sequence being mapped to - if any
427 * @return null or a dataset sequence
429 public SequenceI getTo()
435 * set the dataset sequence being mapped to if any
439 public void setTo(SequenceI tto)
447 * @see java.lang.Object#finalize()
449 protected void finalize() throws Throwable