JAL-1432 updated copyright notices
[jalview.git] / src / jalview / datamodel / Mapping.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.datamodel;
20
21 import java.util.Vector;
22
23 import jalview.util.MapList;
24
25 public class Mapping
26 {
27   /**
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
31    */
32   MapList map = null;
33
34   /**
35    * The seuqence that map maps the associated seuqence to (if any).
36    */
37   SequenceI to = null;
38
39   public Mapping(MapList map)
40   {
41     super();
42     this.map = map;
43   }
44
45   public Mapping(SequenceI to, MapList map)
46   {
47     this(map);
48     this.to = to;
49   }
50
51   /**
52    * create a new mapping from
53    * 
54    * @param to
55    *          the sequence being mapped
56    * @param exon
57    *          int[] {start,end,start,end} series on associated sequence
58    * @param is
59    *          int[] {start,end,...} ranges on the reference frame being mapped
60    *          to
61    * @param i
62    *          step size on associated sequence
63    * @param j
64    *          step size on mapped frame
65    */
66   public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
67   {
68     this(to, new MapList(exon, is, i, j));
69   }
70
71   /**
72    * create a duplicate (and independent) mapping object with the same reference
73    * to any SequenceI being mapped to.
74    * 
75    * @param map2
76    */
77   public Mapping(Mapping map2)
78   {
79     if (map2 != this && map2 != null)
80     {
81       if (map2.map != null)
82       {
83         map = new MapList(map2.map);
84       }
85       to = map2.to;
86     }
87   }
88
89   /**
90    * @return the map
91    */
92   public MapList getMap()
93   {
94     return map;
95   }
96
97   /**
98    * @param map
99    *          the map to set
100    */
101   public void setMap(MapList map)
102   {
103     this.map = map;
104   }
105
106   /**
107    * Equals that compares both the to references and MapList mappings.
108    * 
109    * @param other
110    * @return
111    */
112   public boolean equals(Mapping other)
113   {
114     if (other == null)
115       return false;
116     if (other == this)
117       return true;
118     if (other.to != to)
119       return false;
120     if ((map != null && other.map == null)
121             || (map == null && other.map != null))
122       return false;
123     if (map.equals(other.map))
124       return true;
125     return false;
126   }
127
128   /**
129    * get the 'initial' position in the associated sequence for a position in the
130    * mapped reference frame
131    * 
132    * @param mpos
133    * @return
134    */
135   public int getPosition(int mpos)
136   {
137     if (map != null)
138     {
139       int[] mp = map.shiftTo(mpos);
140       if (mp != null)
141       {
142         return mp[0];
143       }
144     }
145     return mpos;
146   }
147
148   /**
149    * gets boundary in direction of mapping
150    * 
151    * @param position
152    *          in mapped reference frame
153    * @return int{start, end} positions in associated sequence (in direction of
154    *         mapped word)
155    */
156   public int[] getWord(int mpos)
157   {
158     if (map != null)
159     {
160       return map.getToWord(mpos);
161     }
162     return null;
163   }
164
165   /**
166    * width of mapped unit in associated sequence
167    * 
168    */
169   public int getWidth()
170   {
171     if (map != null)
172     {
173       return map.getFromRatio();
174     }
175     return 1;
176   }
177
178   /**
179    * width of unit in mapped reference frame
180    * 
181    * @return
182    */
183   public int getMappedWidth()
184   {
185     if (map != null)
186     {
187       return map.getToRatio();
188     }
189     return 1;
190   }
191
192   /**
193    * get mapped position in the associated reference frame for position pos in
194    * the associated sequence.
195    * 
196    * @param pos
197    * @return
198    */
199   public int getMappedPosition(int pos)
200   {
201     if (map != null)
202     {
203       int[] mp = map.shiftFrom(pos);
204       if (mp != null)
205       {
206         return mp[0];
207       }
208     }
209     return pos;
210   }
211
212   public int[] getMappedWord(int pos)
213   {
214     if (map != null)
215     {
216       int[] mp = map.shiftFrom(pos);
217       if (mp != null)
218       {
219         return new int[]
220         { mp[0], mp[0] + mp[2] * (map.getToRatio() - 1) };
221       }
222     }
223     return null;
224   }
225
226   /**
227    * locates the region of feature f in the associated sequence's reference
228    * frame
229    * 
230    * @param f
231    * @return one or more features corresponding to f
232    */
233   public SequenceFeature[] locateFeature(SequenceFeature f)
234   {
235     if (true)
236     { // f.getBegin()!=f.getEnd()) {
237       if (map != null)
238       {
239         int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
240         if (frange == null)
241         {
242           // JBPNote - this isprobably not the right thing to doJBPHack
243           return null;
244         }
245         SequenceFeature[] vf = new SequenceFeature[frange.length / 2];
246         for (int i = 0, v = 0; i < frange.length; i += 2, v++)
247         {
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));
253         }
254         return vf;
255       }
256     }
257     if (false) // else
258     {
259       int[] word = getWord(f.getBegin());
260       if (word[0] < word[1])
261       {
262         f.setBegin(word[0]);
263       }
264       else
265       {
266         f.setBegin(word[1]);
267       }
268       word = getWord(f.getEnd());
269       if (word[0] > word[1])
270       {
271         f.setEnd(word[0]);
272       }
273       else
274       {
275         f.setEnd(word[1]);
276       }
277     }
278     // give up and just return the feature.
279     return new SequenceFeature[]
280     { f };
281   }
282
283   /**
284    * return a series of contigs on the associated sequence corresponding to the
285    * from,to interval on the mapped reference frame
286    * 
287    * @param from
288    * @param to
289    * @return int[] { from_i, to_i for i=1 to n contiguous regions in the
290    *         associated sequence}
291    */
292   public int[] locateRange(int from, int to)
293   {
294     if (map != null)
295     {
296       if (from <= to)
297       {
298         from = (map.getToLowest() < from) ? from : map.getToLowest();
299         to = (map.getToHighest() > to) ? to : map.getToHighest();
300         if (from > to)
301           return null;
302       }
303       else
304       {
305         from = (map.getToHighest() > from) ? from : map.getToHighest();
306         to = (map.getToLowest() < to) ? to : map.getToLowest();
307         if (from < to)
308           return null;
309       }
310       return map.locateInFrom(from, to);
311     }
312     return new int[]
313     { from, to };
314   }
315
316   /**
317    * return a series of mapped contigs mapped from a range on the associated
318    * sequence
319    * 
320    * @param from
321    * @param to
322    * @return
323    */
324   public int[] locateMappedRange(int from, int to)
325   {
326     if (map != null)
327     {
328
329       if (from <= to)
330       {
331         from = (map.getFromLowest() < from) ? from : map.getFromLowest();
332         to = (map.getFromHighest() > to) ? to : map.getFromHighest();
333         if (from > to)
334           return null;
335       }
336       else
337       {
338         from = (map.getFromHighest() > from) ? from : map.getFromHighest();
339         to = (map.getFromLowest() < to) ? to : map.getFromLowest();
340         if (from < to)
341           return null;
342       }
343       return map.locateInTo(from, to);
344     }
345     return new int[]
346     { from, to };
347   }
348
349   /**
350    * return a new mapping object with a maplist modifed to only map the visible
351    * regions defined by viscontigs.
352    * 
353    * @param viscontigs
354    * @return
355    */
356   public Mapping intersectVisContigs(int[] viscontigs)
357   {
358     Mapping copy = new Mapping(this);
359     if (map != null)
360     {
361       int vpos = 0;
362       int apos = 0;
363       Vector toRange = new Vector();
364       Vector fromRange = new Vector();
365       for (int vc = 0; vc < viscontigs.length; vc += 2)
366       {
367         // find a mapped range in this visible region
368         int[] mpr = locateMappedRange(1 + viscontigs[vc],
369                 viscontigs[vc + 1] - 1);
370         if (mpr != null)
371         {
372           for (int m = 0; m < mpr.length; m += 2)
373           {
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)
378             {
379               fromRange.addElement(new int[]
380               { xpos[x], xpos[x + 1] });
381             }
382           }
383         }
384       }
385       int[] from = new int[fromRange.size() * 2];
386       int[] to = new int[toRange.size() * 2];
387       int[] r;
388       for (int f = 0, fSize = fromRange.size(); f < fSize; f++)
389       {
390         r = (int[]) fromRange.elementAt(f);
391         from[f * 2] = r[0];
392         from[f * 2 + 1] = r[1];
393       }
394       for (int f = 0, fSize = toRange.size(); f < fSize; f++)
395       {
396         r = (int[]) toRange.elementAt(f);
397         to[f * 2] = r[0];
398         to[f * 2 + 1] = r[1];
399       }
400       copy.setMap(new MapList(from, to, map.getFromRatio(), map
401               .getToRatio()));
402     }
403     return copy;
404   }
405
406   public static void main(String[] args)
407   {
408     /**
409      * trite test of the intersectVisContigs method for a simple DNA -> Protein
410      * exon map and a range of visContigs
411      */
412     MapList fk = new MapList(new int[]
413     { 1, 6, 8, 13, 15, 23 }, new int[]
414     { 1, 7 }, 3, 1);
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[]
419     { 1, 7, 11, 20 });
420     System.out.println("" + m_1.map.getFromRanges());
421
422   }
423
424   /**
425    * get the sequence being mapped to - if any
426    * 
427    * @return null or a dataset sequence
428    */
429   public SequenceI getTo()
430   {
431     return to;
432   }
433
434   /**
435    * set the dataset sequence being mapped to if any
436    * 
437    * @param tto
438    */
439   public void setTo(SequenceI tto)
440   {
441     to = tto;
442   }
443
444   /*
445    * (non-Javadoc)
446    * 
447    * @see java.lang.Object#finalize()
448    */
449   protected void finalize() throws Throwable
450   {
451     map = null;
452     to = null;
453     super.finalize();
454   }
455
456 }