moving word methods to MapList and begin bug-fix for single-residue codon span featur...
[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   public int[] getWord(int mpos) {
103     if (map!=null) {
104       int[] mp=map.shiftTo(mpos);
105       if (mp!=null) {
106         return new int[] {mp[0], mp[0]+mp[2]*(map.getFromRatio()-1)};
107       }
108     }
109     return null;
110   }
111   /**
112    * width of mapped unit in associated sequence
113    *
114    */
115   public int getWidth() {
116     if (map!=null) {
117       return map.getFromRatio();
118     }
119     return 1;
120   }
121
122   /**
123    * width of unit in mapped reference frame
124    * @return
125    */
126   public int getMappedWidth() {
127     if (map!=null) {
128       return map.getToRatio();
129     }
130     return 1;
131   }
132   /**
133    * get mapped position in the associated
134    * reference frame for position pos in the
135    * associated sequence.
136    * @param pos
137    * @return
138    */
139   public int getMappedPosition(int pos) {
140     if (map!=null) {
141       int[] mp = map.shiftFrom(pos);
142       if (mp!=null)
143       {
144         return mp[0];
145       }
146     }
147     return pos;
148   }
149   public int[] getMappedWord(int pos) {
150     if (map!=null) {
151       int[] mp = map.shiftFrom(pos);
152       if (mp!=null)
153       {
154         return new int[] { mp[0], mp[0]+mp[2]*(map.getToRatio()-1)};
155       }
156     }
157     return null;
158   }
159   /**
160    * locates the region of feature f in the associated sequence's reference frame
161    * @param f
162    * @return one or more features corresponding to f
163    */
164   public SequenceFeature[] locateFeature(SequenceFeature f)
165   {
166     if (true) { // f.getBegin()!=f.getEnd()) {
167       if (map!=null) {
168         int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
169         SequenceFeature[] vf = new SequenceFeature[frange.length/2];
170         for (int i=0,v=0;i<frange.length;i+=2,v++) {
171           vf[v] = new SequenceFeature(f);
172           vf[v].setBegin(frange[i]);
173           vf[v].setEnd(frange[i+1]);
174           if (frange.length>2)
175             vf[v].setDescription(f.getDescription() +"\nPart "+v);
176         }
177         return vf;
178       }
179     }
180     if (false) //else
181     {
182       int[] word = getWord(f.getBegin());
183       if (word[0]<word[1])
184       {
185         f.setBegin(word[0]);
186       } else {
187         f.setBegin(word[1]);
188       }
189       word = getWord(f.getEnd());
190       if (word[0]>word[1])
191       {
192         f.setEnd(word[0]);
193       } else {
194         f.setEnd(word[1]);
195       }
196     }
197     // give up and just return the feature.
198     return new SequenceFeature[] { f };
199   }
200
201     /**
202    * return a series of contigs on the associated sequence corresponding to
203    * the from,to interval on the mapped reference frame
204    * @param from
205    * @param to
206    * @return
207    */
208   public int[] locateRange(int from, int to) {
209     //TODO
210     return null;
211   }
212   /**
213    * return a series of contigs on the mapped reference frame corresponding to
214    * the from,to interval on the associated sequence
215    * @param from
216    * @param to
217    * @return
218    */
219   public int[] locateMappedRange(int from, int to) {
220     //TODO
221     return null;
222   }
223 }