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