2 * This file is part of the Vamsas Client version 0.2.
\r
3 * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
22 package uk.ac.vamsas.objects.utils;
\r
25 * This code was originated from
\r
26 * Jalview - A Sequence Alignment Editor and Viewer
\r
27 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
29 * This program is free software; you can redistribute it and/or
\r
30 * modify it under the terms of the GNU General Public License
\r
31 * as published by the Free Software Foundation; either version 2
\r
32 * of the License, or (at your option) any later version.
\r
34 * This program is distributed in the hope that it will be useful,
\r
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
37 * GNU General Public License for more details.
\r
39 * You should have received a copy of the GNU General Public License
\r
40 * along with this program; if not, write to the Free Software
\r
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
44 import java.util.Vector;
\r
46 public class Mapping {
\r
48 * Contains the start-end pairs mapping from the associated sequence to the
\r
49 * sequence in the database coordinate system it also takes care of step
\r
50 * difference between coordinate systems
\r
55 * The seuqence that map maps the associated seuqence to (if any). SequenceI
\r
58 public Mapping(MapList map) {
\r
64 * public Mapping(SequenceI to, MapList map) { this(map); this.to = to; }
\r
67 * create a new mapping from
\r
69 * (in Jalview) param to was the destination sequence being mapped
\r
72 * int[] {start,end,start,end} series on associated sequence
\r
74 * int[] {start,end,...} ranges on the reference frame being mapped
\r
77 * step size on associated sequence
\r
79 * step size on mapped frame
\r
81 public Mapping(// SequenceI to,
\r
82 int[] local, int[] mapped, int i, int j) {
\r
83 this(new MapList(local, mapped, i, j));
\r
87 * create a duplicate (and independent) mapping object with the same reference
\r
88 * to any SequenceI being mapped to.
\r
92 public Mapping(Mapping map2) {
\r
93 if (map2 != this && map2 != null) {
\r
94 if (map2.map != null) {
\r
95 map = new MapList(map2.map);
\r
103 public MapList getMap() {
\r
111 public void setMap(MapList map) {
\r
116 * Equals that compares both the to references and MapList mappings.
\r
121 public boolean equals(Mapping other) {
\r
126 if ((map != null && other.map == null)
\r
127 || (map == null && other.map != null))
\r
129 if (map.equals(other.map))
\r
135 * get the 'initial' position in the associated sequence for a position in the
\r
136 * mapped reference frame
\r
141 public int getPosition(int mpos) {
\r
143 int[] mp = map.shiftTo(mpos);
\r
152 * gets boundary in direction of mapping
\r
155 * in mapped reference frame
\r
156 * @return int{start, end} positions in associated sequence (in direction of
\r
159 public int[] getWord(int mpos) {
\r
161 return map.getToWord(mpos);
\r
167 * width of mapped unit in associated sequence
\r
170 public int getWidth() {
\r
172 return map.getFromRatio();
\r
178 * width of unit in mapped reference frame
\r
182 public int getMappedWidth() {
\r
184 return map.getToRatio();
\r
190 * get mapped position in the associated reference frame for position pos in
\r
191 * the associated sequence.
\r
196 public int getMappedPosition(int pos) {
\r
198 int[] mp = map.shiftFrom(pos);
\r
206 public int[] getMappedWord(int pos) {
\r
208 int[] mp = map.shiftFrom(pos);
\r
210 return new int[] { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
\r
217 * locates the region of feature f in the associated (local) sequence's
\r
221 * @return int[] { start1, end1, ... starti, endi } for the corresponding
\r
222 * interval in local reference frame
\r
224 public int[] locateFeature(int begin, int end) {
\r
225 if (true) { // f.getBegin()!=f.getEnd()) {
\r
227 int[] frange = map.locateInFrom(begin, end); // f.getBegin(),
\r
230 * left in as an example as to how this is used in Jalview
\r
231 * SequenceFeature[] vf = new SequenceFeature[frange.length / 2]; for
\r
232 * (int i = 0, v = 0; i < frange.length; i += 2, v++) { vf[v] = new
\r
233 * SequenceFeature(f); vf[v].setBegin(frange[i]); vf[v].setEnd(frange[i
\r
234 * + 1]); if (frange.length > 2) vf[v].setDescription(f.getDescription()
\r
235 * + "\nPart " + v); }
\r
240 // give up and just return the interval unchanged - this might not be the
\r
241 // correct behaviour
\r
242 return new int[] { begin, end };
\r
246 * return a series of contigs on the associated sequence corresponding to the
\r
247 * from,to interval on the mapped reference frame
\r
251 * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
\r
252 * associated sequence}
\r
254 public int[] locateRange(int from, int to) {
\r
257 from = (map.getToLowest() < from) ? from : map.getToLowest();
\r
258 to = (map.getToHighest() > to) ? to : map.getToHighest();
\r
262 from = (map.getToHighest() > from) ? from : map.getToHighest();
\r
263 to = (map.getToLowest() < to) ? to : map.getToLowest();
\r
267 return map.locateInFrom(from, to);
\r
269 return new int[] { from, to };
\r
273 * return a series of mapped contigs mapped from a range on the associated
\r
280 public int[] locateMappedRange(int from, int to) {
\r
284 from = (map.getFromLowest() < from) ? from : map.getFromLowest();
\r
285 to = (map.getFromHighest() > to) ? to : map.getFromHighest();
\r
289 from = (map.getFromHighest() > from) ? from : map.getFromHighest();
\r
290 to = (map.getFromLowest() < to) ? to : map.getFromLowest();
\r
294 return map.locateInTo(from, to);
\r
296 return new int[] { from, to };
\r
300 * return a new mapping object with a maplist modifed to only map the visible
\r
301 * regions defined by viscontigs.
\r
303 * @param viscontigs
\r
306 public Mapping intersectVisContigs(int[] viscontigs) {
\r
307 Mapping copy = new Mapping(this);
\r
309 Vector toRange = new Vector();
\r
310 Vector fromRange = new Vector();
\r
311 for (int vc = 0; vc < viscontigs.length; vc += 2) {
\r
312 // find a mapped range in this visible region
\r
313 int[] mpr = locateMappedRange(1 + viscontigs[vc],
\r
314 viscontigs[vc + 1] - 1);
\r
316 for (int m = 0; m < mpr.length; m += 2) {
\r
317 toRange.addElement(new int[] { mpr[m], mpr[m + 1] });
\r
318 int[] xpos = locateRange(mpr[m], mpr[m + 1]);
\r
319 for (int x = 0; x < xpos.length; x += 2) {
\r
320 fromRange.addElement(new int[] { xpos[x], xpos[x + 1] });
\r
325 int[] from = new int[fromRange.size() * 2];
\r
326 int[] to = new int[toRange.size() * 2];
\r
328 for (int f = 0, fSize = fromRange.size(); f < fSize; f++) {
\r
329 r = (int[]) fromRange.elementAt(f);
\r
330 from[f * 2] = r[0];
\r
331 from[f * 2 + 1] = r[1];
\r
333 for (int f = 0, fSize = toRange.size(); f < fSize; f++) {
\r
334 r = (int[]) toRange.elementAt(f);
\r
336 to[f * 2 + 1] = r[1];
\r
338 copy.setMap(new MapList(from, to, map.getFromRatio(), map.getToRatio()));
\r
343 public static void main(String[] args) {
\r
345 * trite test of the intersectVisContigs method for a simple DNA -> Protein
\r
346 * exon map and a range of visContigs
\r
348 MapList fk = new MapList(new int[] { 1, 6, 8, 13, 15, 23 }, new int[] { 1,
\r
350 Mapping m = new Mapping(fk);
\r
351 Mapping m_1 = m.intersectVisContigs(new int[] { fk.getFromLowest(),
\r
352 fk.getFromHighest() });
\r
353 Mapping m_2 = m.intersectVisContigs(new int[] { 1, 7, 11, 20 });
\r
354 System.out.println("" + m_1.map.getFromRanges());
\r
355 // this test was for debugging purposes only - it should run without
\r
356 // exceptions, but the
\r
357 // integrity of the mapping was checked using an interactive debugger rather
\r
358 // than programmatically.
\r