1 package jalview.datamodel;
3 import java.util.Vector;
5 import jalview.util.MapList;
10 * Contains the start-end pairs mapping from the associated sequence to the
11 * sequence in the database coordinate system it also takes care of step
12 * difference between coordinate systems
17 * The seuqence that map maps the associated seuqence to (if any).
21 public Mapping(MapList map)
27 public Mapping(SequenceI to, MapList map)
34 * create a new mapping from
37 * the sequence being mapped
39 * int[] {start,end,start,end} series on associated sequence
41 * int[] {start,end,...} ranges on the reference frame being mapped
44 * step size on associated sequence
46 * step size on mapped frame
48 public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
50 this(to, new MapList(exon, is, i, j));
54 * create a duplicate (and independent) mapping object with the same reference
55 * to any SequenceI being mapped to.
59 public Mapping(Mapping map2)
61 if (map2 != this && map2 != null)
65 map = new MapList(map2.map);
74 public MapList getMap()
83 public void setMap(MapList map)
89 * Equals that compares both the to references and MapList mappings.
94 public boolean equals(Mapping other)
102 if ((map != null && other.map == null)
103 || (map == null && other.map != null))
105 if (map.equals(other.map))
111 * get the 'initial' position in the associated sequence for a position in the
112 * mapped reference frame
117 public int getPosition(int mpos)
121 int[] mp = map.shiftTo(mpos);
131 * gets boundary in direction of mapping
134 * in mapped reference frame
135 * @return int{start, end} positions in associated sequence (in direction of
138 public int[] getWord(int mpos)
142 return map.getToWord(mpos);
148 * width of mapped unit in associated sequence
151 public int getWidth()
155 return map.getFromRatio();
161 * width of unit in mapped reference frame
165 public int getMappedWidth()
169 return map.getToRatio();
175 * get mapped position in the associated reference frame for position pos in
176 * the associated sequence.
181 public int getMappedPosition(int pos)
185 int[] mp = map.shiftFrom(pos);
194 public int[] getMappedWord(int pos)
198 int[] mp = map.shiftFrom(pos);
202 { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
209 * locates the region of feature f in the associated sequence's reference
213 * @return one or more features corresponding to f
215 public SequenceFeature[] locateFeature(SequenceFeature f)
218 { // f.getBegin()!=f.getEnd()) {
221 int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
222 SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
223 for (int i = 0, v = 0; i < frange.length; i += 2, v++)
225 vf[v] = new SequenceFeature(f);
226 vf[v].setBegin(frange[i]);
227 vf[v].setEnd(frange[i + 1]);
228 if (frange.length > 2)
229 vf[v].setDescription(f.getDescription() + "\nPart " + v);
236 int[] word = getWord(f.getBegin());
237 if (word[0] < word[1])
245 word = getWord(f.getEnd());
246 if (word[0] > word[1])
255 // give up and just return the feature.
256 return new SequenceFeature[]
261 * return a series of contigs on the associated sequence corresponding to the
262 * from,to interval on the mapped reference frame
266 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
267 * associated sequence}
269 public int[] locateRange(int from, int to)
275 from = (map.getToLowest() < from) ? from : map.getToLowest();
276 to = (map.getToHighest() > to) ? to : map.getToHighest();
282 from = (map.getToHighest() > from) ? from : map.getToHighest();
283 to = (map.getToLowest() < to) ? to : map.getToLowest();
287 return map.locateInFrom(from, to);
294 * return a series of mapped contigs mapped from a range on the associated
301 public int[] locateMappedRange(int from, int to)
308 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
309 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
315 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
316 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
320 return map.locateInTo(from, to);
327 * return a new mapping object with a maplist modifed to only map the visible
328 * regions defined by viscontigs.
333 public Mapping intersectVisContigs(int[] viscontigs)
335 Mapping copy = new Mapping(this);
340 Vector toRange = new Vector();
341 Vector fromRange = new Vector();
342 for (int vc = 0; vc < viscontigs.length; vc += 2)
344 // find a mapped range in this visible region
345 int[] mpr = locateMappedRange(1+viscontigs[vc], viscontigs[vc + 1]-1);
348 for (int m = 0; m < mpr.length; m += 2)
350 toRange.addElement(new int[]
351 { mpr[m], mpr[m + 1] });
352 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
353 for (int x = 0; x < xpos.length; x += 2)
355 fromRange.addElement(new int[]
356 { xpos[x], xpos[x + 1] });
361 int[] from = new int[fromRange.size()*2];
362 int[] to = new int[toRange.size()*2];
364 for (int f=0,fSize=fromRange.size(); f<fSize; f++)
366 r = (int[]) fromRange.elementAt(f);
370 for (int f=0,fSize=toRange.size(); f<fSize; f++)
372 r = (int[]) toRange.elementAt(f);
376 copy.setMap(new MapList(from, to, map.getFromRatio(), map.getToRatio()));
380 public static void main(String[] args)
383 * trite test of the intersectVisContigs method
384 * for a simple DNA -> Protein exon map and a range of visContigs
386 MapList fk = new MapList(new int[] { 1,6,8,13,15,23}, new int[] { 1,7}, 3, 1);
387 Mapping m = new Mapping(fk);
388 Mapping m_1 = m.intersectVisContigs(new int[] {fk.getFromLowest(), fk.getFromHighest()});
389 Mapping m_2 = m.intersectVisContigs(new int[] {1,7,11,20});
390 System.out.println(""+m_1.map.getFromRanges());
395 * get the sequence being mapped to - if any
396 * @return null or a dataset sequence
398 public SequenceI getTo()
403 * set the dataset sequence being mapped to if any
406 public void setTo(SequenceI tto)
412 * @see java.lang.Object#finalize()
414 protected void finalize() throws Throwable