documentation and proxy call to util.MapList.getToWord(mpos)
[jalview.git] / src / jalview / datamodel / Mapping.java
1 package jalview.datamodel;
2
3 import jalview.util.MapList;
4
5 public class Mapping {
6   /**
7    * Contains the
8    * start-end pairs mapping from
9    * the associated sequence to the
10    * sequence in the database
11    * coordinate system
12    * it also takes care of step difference between coordinate systems
13    */
14   MapList map=null;
15   /**
16    * The seuqence that map maps the associated seuqence to (if any).
17    */
18   SequenceI to=null;
19   public Mapping(MapList map) {
20     super();
21     this.map = map;
22   }
23   public Mapping(SequenceI to, MapList map) {
24     this(map);
25     this.to = to;
26   }
27   /**
28    * create a new mapping from
29    * @param to the sequence being mapped
30    * @param exon int[] {start,end,start,end} series on associated sequence
31    * @param is int[] {start,end,...} ranges on the reference frame being mapped to
32    * @param i step size on associated sequence
33    * @param j step size on mapped frame
34    */
35   public Mapping(SequenceI to, int[] exon, int[] is, int i, int j)
36   {
37     this(to, new MapList(exon, is, i, j));
38   }
39   /**
40    * create a duplicate (and independent) mapping object with
41    * the same reference to any SequenceI being mapped to.
42    * @param map2
43    */
44   public Mapping(Mapping map2)
45   {
46     if (map2!=this && map2!=null) {
47       if (map2.map!=null)
48       {
49         map=new MapList(map2.map);
50       }
51       to = map2.to;
52     }
53   }
54   /**
55    * @return the map
56    */
57   public MapList getMap() {
58     return map;
59   }
60
61   /**
62    * @param map the map to set
63    */
64   public void setMap(MapList map) {
65     this.map = map;
66   }
67   /**
68    * Equals that compares both the to references and MapList mappings.
69    * @param other
70    * @return
71    */
72   public boolean equals(Mapping other) {
73     if (other==null)
74       return false;
75     if (other==this)
76       return true;
77     if (other.to!=to)
78       return false;
79     if ((map!=null && other.map==null) || (map==null && other.map!=null))
80       return false;
81     if (map.equals(other.map))
82       return true;
83     return false;
84   }
85   /**
86    * get the 'initial' position in the associated
87    * sequence for a position in the mapped reference frame
88    * @param mpos
89    * @return
90    */
91   public int getPosition(int mpos)
92   {
93     if (map!=null) {
94       int[] mp = map.shiftTo(mpos);
95       if (mp!=null)
96       {
97         return mp[0];
98       }
99     }
100     return mpos;
101   }
102   /**
103    * gets boundary in direction of mapping 
104    * @param position in mapped reference frame
105    * @return int{start, end} positions in associated sequence (in direction of mapped word)
106    */
107   public int[] getWord(int mpos) {
108     if (map!=null) {
109       return map.getToWord(mpos);
110     }
111     return null;
112   }
113   /**
114    * width of mapped unit in associated sequence
115    *
116    */
117   public int getWidth() {
118     if (map!=null) {
119       return map.getFromRatio();
120     }
121     return 1;
122   }
123
124   /**
125    * width of unit in mapped reference frame
126    * @return
127    */
128   public int getMappedWidth() {
129     if (map!=null) {
130       return map.getToRatio();
131     }
132     return 1;
133   }
134   /**
135    * get mapped position in the associated
136    * reference frame for position pos in the
137    * associated sequence.
138    * @param pos
139    * @return
140    */
141   public int getMappedPosition(int pos) {
142     if (map!=null) {
143       int[] mp = map.shiftFrom(pos);
144       if (mp!=null)
145       {
146         return mp[0];
147       }
148     }
149     return pos;
150   }
151   public int[] getMappedWord(int pos) {
152     if (map!=null) {
153       int[] mp = map.shiftFrom(pos);
154       if (mp!=null)
155       {
156         return new int[] { mp[0], mp[0]+mp[2]*(map.getToRatio()-1)};
157       }
158     }
159     return null;
160   }
161   /**
162    * locates the region of feature f in the associated sequence's reference frame
163    * @param f
164    * @return one or more features corresponding to f
165    */
166   public SequenceFeature[] locateFeature(SequenceFeature f)
167   {
168     if (true) { // f.getBegin()!=f.getEnd()) {
169       if (map!=null) {
170         int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
171         SequenceFeature[] vf = new SequenceFeature[frange.length/2];
172         for (int i=0,v=0;i<frange.length;i+=2,v++) {
173           vf[v] = new SequenceFeature(f);
174           vf[v].setBegin(frange[i]);
175           vf[v].setEnd(frange[i+1]);
176           if (frange.length>2)
177             vf[v].setDescription(f.getDescription() +"\nPart "+v);
178         }
179         return vf;
180       }
181     }
182     if (false) //else
183     {
184       int[] word = getWord(f.getBegin());
185       if (word[0]<word[1])
186       {
187         f.setBegin(word[0]);
188       } else {
189         f.setBegin(word[1]);
190       }
191       word = getWord(f.getEnd());
192       if (word[0]>word[1])
193       {
194         f.setEnd(word[0]);
195       } else {
196         f.setEnd(word[1]);
197       }
198     }
199     // give up and just return the feature.
200     return new SequenceFeature[] { f };
201   }
202
203     /**
204    * return a series of contigs on the associated sequence corresponding to
205    * the from,to interval on the mapped reference frame
206    * @param from
207    * @param to
208    * @return
209    */
210   public int[] locateRange(int from, int to) {
211     //TODO
212     return null;
213   }
214   /**
215    * return a series of contigs on the mapped reference frame corresponding to
216    * the from,to interval on the associated sequence
217    * @param from
218    * @param to
219    * @return
220    */
221   public int[] locateMappedRange(int from, int to) {
222     //TODO
223     return null;
224   }
225 }