2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.io.vamsas;
20 import java.util.Vector;
22 import uk.ac.vamsas.client.Vobject;
23 import uk.ac.vamsas.objects.core.Local;
24 import uk.ac.vamsas.objects.core.Map;
25 import uk.ac.vamsas.objects.core.MapType;
26 import uk.ac.vamsas.objects.core.Mapped;
27 import uk.ac.vamsas.objects.core.RangeType;
28 import uk.ac.vamsas.objects.core.Seg;
29 import jalview.datamodel.DBRefEntry;
30 import jalview.datamodel.Mapping;
31 import jalview.io.VamsasAppDatastore;
34 * Enhances DatastoreItem objects with additional functions to do with RangeType
40 public abstract class Rangetype extends DatastoreItem
48 public Rangetype(VamsasAppDatastore datastore)
53 public Rangetype(VamsasAppDatastore datastore, Vobject vobj, Class jvClass)
55 super(datastore, vobj, jvClass);
58 public Rangetype(VamsasAppDatastore datastore, Object jvobj, Class vClass)
60 super(datastore, jvobj, vClass);
64 * get real bounds of a RangeType's specification. start and end are an
65 * inclusive range within which all segments and positions lie. TODO: refactor
69 * @return int[] { start, end}
71 protected int[] getBounds(RangeType dseta)
76 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
79 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
81 if (dseta.getSegCount() > 0)
83 se = getSegRange(dseta.getSeg(0), true);
84 for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
86 int nse[] = getSegRange(dseta.getSeg(s), true);
97 if (dseta.getPosCount() > 0)
99 // could do a polarity for pos range too. and pass back indication of
101 int pos = dseta.getPos(0).getI();
104 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
106 pos = dseta.getPos(p).getI();
123 * map from a rangeType's internal frame to the referenced object's coordinate
127 * @return int [] { ref(pos)...} for all pos in rangeType's frame.
129 protected int[] getMapping(RangeType dseta)
131 Vector posList = new Vector();
135 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
138 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
140 if (dseta.getSegCount() > 0)
142 for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
144 se = getSegRange(dseta.getSeg(s), false);
145 int se_end = se[1 - se[2]] + (se[2] == 0 ? 1 : -1);
146 for (int p = se[se[2]]; p != se_end; p += se[2] == 0 ? 1 : -1)
148 posList.add(new Integer(p));
152 else if (dseta.getPosCount() > 0)
154 int pos = dseta.getPos(0).getI();
156 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
158 pos = dseta.getPos(p).getI();
159 posList.add(new Integer(pos));
163 if (posList != null && posList.size() > 0)
165 int[] range = new int[posList.size()];
166 for (int i = 0; i < range.length; i++)
168 range[i] = ((Integer) posList.elementAt(i)).intValue();
176 protected int[] getIntervals(RangeType range)
178 int[] intervals = null;
179 Vector posList = new Vector();
183 if (range.getSegCount() > 0 && range.getPosCount() > 0)
186 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
188 if (range.getSegCount() > 0)
190 for (int s = 0, sSize = range.getSegCount(); s < sSize; s++)
192 se = getSegRange(range.getSeg(s), false);
193 posList.addElement(new Integer(se[0]));
194 posList.addElement(new Integer(se[1]));
197 else if (range.getPosCount() > 0)
199 int pos = range.getPos(0).getI();
201 for (int p = 0, pSize = range.getPosCount(); p < pSize; p++)
203 pos = range.getPos(p).getI();
204 posList.add(new Integer(pos));
205 posList.add(new Integer(pos));
209 if (posList != null && posList.size() > 0)
211 intervals = new int[posList.size()];
212 java.util.Enumeration e = posList.elements();
214 while (e.hasMoreElements())
216 intervals[i++] = ((Integer) e.nextElement()).intValue();
223 * initialise a range type object from a set of start/end inclusive intervals
228 protected void initRangeType(RangeType mrt, int[] range)
230 for (int i = 0; i < range.length; i += 2)
232 Seg vSeg = new Seg();
233 vSeg.setStart(range[i]);
234 vSeg.setEnd(range[i + 1]);
235 vSeg.setInclusive(true);
243 * where the from range is the local mapped range, and the to range
244 * is the 'mapped' range in the MapRangeType
245 * @param default unit for local
246 * @param default unit for mapped
249 protected jalview.util.MapList parsemapType(MapType maprange, int localu,
252 jalview.util.MapList ml = null;
253 int[] localRange = getIntervals(maprange.getLocal());
254 int[] mappedRange = getIntervals(maprange.getMapped());
255 long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit()
257 long mu = maprange.getMapped().hasUnit() ? maprange.getMapped()
258 .getUnit() : mappedu;
259 ml = new jalview.util.MapList(localRange, mappedRange, (int) lu,
264 protected jalview.util.MapList parsemapType(MapType map)
266 if (!map.getLocal().hasUnit() || map.getMapped().hasUnit())
268 jalview.bin.Cache.log
269 .warn("using default mapping length of 1:1 for map "
270 + (map.isRegistered() ? map.getVorbaId().toString()
271 : ("<no Id registered> " + map.toString())));
273 return parsemapType(map, 1, 1);
277 * initialise a MapType object from a MapList object.
283 protected void initMapType(MapType maprange, jalview.util.MapList ml,
286 initMapType(maprange, ml, setUnits, false);
295 * - reverse MapList mapping for Local and Mapped ranges and units
297 protected void initMapType(MapType maprange, jalview.util.MapList ml,
298 boolean setUnits, boolean reverse)
303 "Implementation error. MapList is null for initMapType.");
305 maprange.setLocal(new Local());
306 maprange.setMapped(new Mapped());
309 initRangeType(maprange.getLocal(), ml.getFromRanges());
310 initRangeType(maprange.getMapped(), ml.getToRanges());
314 initRangeType(maprange.getLocal(), ml.getToRanges());
315 initRangeType(maprange.getMapped(), ml.getFromRanges());
321 maprange.getLocal().setUnit(ml.getFromRatio());
322 maprange.getMapped().setUnit(ml.getToRatio());
326 maprange.getLocal().setUnit(ml.getToRatio());
327 maprange.getMapped().setUnit(ml.getFromRatio());
329 // TODO: and verify - raise an implementation fault notice if local/mapped
330 // range % Local/Mapped Ratio != 0
331 // if (uk.ac.vamsas.objects.utils.Range.getIntervals(range))