bugfix for sequence features mapped to multiple contigs on local sequence frame
[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     // this is a stopgap - features broken over exon boundaries will not be
167     // broken into a collection of feature fragments.
168     // TODO: implement creation of several features from a single feature on a discontinuously mapped seuqence
169     // need a function like int [] fromrange = map.getRange(from,to)
170     // need to make subgrouped sequence features.
171     if (true) {
172       if (map!=null) {
173         int[] frange = map.locateInFrom(f.getBegin(), f.getEnd());
174         SequenceFeature[] vf = new SequenceFeature[frange.length/2];
175         for (int i=0,v=0;i<frange.length;i+=2,v++) {
176           vf[v] = new SequenceFeature(f);
177           vf[v].setBegin(frange[i]);
178           vf[v].setEnd(frange[i+1]);
179           if (frange.length>2)
180             vf[v].setDescription(f.getDescription() +"\nPart "+v);
181         }
182         return vf;
183       }
184     }
185     if (false){
186       int[] word = getWord(f.getBegin());
187       if (word[0]<word[1]) 
188       {
189         f.setBegin(word[0]);
190       } else {
191         f.setBegin(word[1]);
192       }
193       word = getWord(f.getEnd());
194       if (word[0]>word[1]) 
195       {
196         f.setEnd(word[0]);
197       } else {
198         f.setEnd(word[1]);
199       }
200     }
201     // give up and just return the feature.
202     return new SequenceFeature[] { f };
203   }
204       
205     /**
206    * return a series of contigs on the associated sequence corresponding to
207    * the from,to interval on the mapped reference frame
208    * @param from
209    * @param to
210    * @return
211    */
212   public int[] locateRange(int from, int to) {
213     //TODO
214     return null;
215   }
216   /**
217    * return a series of contigs on the mapped reference frame corresponding to
218    * the from,to interval on the associated sequence
219    * @param from
220    * @param to
221    * @return
222    */
223   public int[] locateMappedRange(int from, int to) {
224     //TODO 
225     return null;
226   }
227 }