1 package uk.ac.vamsas.objects.utils;
3 * This code was originated from
4 * Jalview - A Sequence Alignment Editor and Viewer
5 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
7 * This program 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 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import java.util.Vector;
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).
37 public Mapping(MapList map)
43 /* public Mapping(SequenceI to, MapList map)
50 * create a new mapping from
52 * (in Jalview) param to was
53 * the destination sequence being mapped
55 * int[] {start,end,start,end} series on associated sequence
57 * int[] {start,end,...} ranges on the reference frame being mapped
60 * step size on associated sequence
62 * step size on mapped frame
64 public Mapping(//SequenceI to,
65 int[] local, int[] mapped, int i, int j)
67 this(new MapList(local, mapped, 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);
90 public MapList getMap()
99 public void setMap(MapList map)
105 * Equals that compares both the to references and MapList mappings.
110 public boolean equals(Mapping other)
116 if ((map != null && other.map == null)
117 || (map == null && other.map != null))
119 if (map.equals(other.map))
125 * get the 'initial' position in the associated sequence for a position in the
126 * mapped reference frame
131 public int getPosition(int mpos)
135 int[] mp = map.shiftTo(mpos);
145 * gets boundary in direction of mapping
148 * in mapped reference frame
149 * @return int{start, end} positions in associated sequence (in direction of
152 public int[] getWord(int mpos)
156 return map.getToWord(mpos);
162 * width of mapped unit in associated sequence
165 public int getWidth()
169 return map.getFromRatio();
175 * width of unit in mapped reference frame
179 public int getMappedWidth()
183 return map.getToRatio();
189 * get mapped position in the associated reference frame for position pos in
190 * the associated sequence.
195 public int getMappedPosition(int pos)
199 int[] mp = map.shiftFrom(pos);
208 public int[] getMappedWord(int pos)
212 int[] mp = map.shiftFrom(pos);
216 { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
223 * locates the region of feature f in the associated (local) sequence's reference
227 * @return int[] { start1, end1, ... starti, endi } for the corresponding interval in local reference frame
229 public int[] locateFeature(int begin, int end)
232 { // f.getBegin()!=f.getEnd()) {
235 int[] frange = map.locateInFrom(begin, end); //f.getBegin(), f.getEnd());
236 /* left in as an example as to how this is used in Jalview
237 * SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
238 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
240 vf[v] = new SequenceFeature(f);
241 vf[v].setBegin(frange[i]);
242 vf[v].setEnd(frange[i + 1]);
243 if (frange.length > 2)
244 vf[v].setDescription(f.getDescription() + "\nPart " + v);
250 // give up and just return the interval unchanged - this might not be the correct behaviour
251 return new int[] { begin, end };
255 * return a series of contigs on the associated sequence corresponding to the
256 * from,to interval on the mapped reference frame
260 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
261 * associated sequence}
263 public int[] locateRange(int from, int to)
269 from = (map.getToLowest() < from) ? from : map.getToLowest();
270 to = (map.getToHighest() > to) ? to : map.getToHighest();
276 from = (map.getToHighest() > from) ? from : map.getToHighest();
277 to = (map.getToLowest() < to) ? to : map.getToLowest();
281 return map.locateInFrom(from, to);
288 * return a series of mapped contigs mapped from a range on the associated
295 public int[] locateMappedRange(int from, int to)
302 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
303 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
309 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
310 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
314 return map.locateInTo(from, to);
321 * return a new mapping object with a maplist modifed to only map the visible
322 * regions defined by viscontigs.
327 public Mapping intersectVisContigs(int[] viscontigs)
329 Mapping copy = new Mapping(this);
332 Vector toRange = new Vector();
333 Vector fromRange = new Vector();
334 for (int vc = 0; vc < viscontigs.length; vc += 2)
336 // find a mapped range in this visible region
337 int[] mpr = locateMappedRange(1+viscontigs[vc], viscontigs[vc + 1]-1);
340 for (int m = 0; m < mpr.length; m += 2)
342 toRange.addElement(new int[]
343 { mpr[m], mpr[m + 1] });
344 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
345 for (int x = 0; x < xpos.length; x += 2)
347 fromRange.addElement(new int[]
348 { xpos[x], xpos[x + 1] });
353 int[] from = new int[fromRange.size()*2];
354 int[] to = new int[toRange.size()*2];
356 for (int f=0,fSize=fromRange.size(); f<fSize; f++)
358 r = (int[]) fromRange.elementAt(f);
362 for (int f=0,fSize=toRange.size(); f<fSize; f++)
364 r = (int[]) toRange.elementAt(f);
368 copy.setMap(new MapList(from, to, map.getFromRatio(), map.getToRatio()));
372 public static void main(String[] args)
375 * trite test of the intersectVisContigs method
376 * for a simple DNA -> Protein exon map and a range of visContigs
378 MapList fk = new MapList(new int[] { 1,6,8,13,15,23}, new int[] { 1,7}, 3, 1);
379 Mapping m = new Mapping(fk);
380 Mapping m_1 = m.intersectVisContigs(new int[] {fk.getFromLowest(), fk.getFromHighest()});
381 Mapping m_2 = m.intersectVisContigs(new int[] {1,7,11,20});
382 System.out.println(""+m_1.map.getFromRanges());
383 // this test was for debugging purposes only - it should run without exceptions, but the
384 // integrity of the mapping was checked using an interactive debugger rather than programmatically.