2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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.MapType;
25 import uk.ac.vamsas.objects.core.Mapped;
26 import uk.ac.vamsas.objects.core.RangeType;
27 import uk.ac.vamsas.objects.core.Seg;
28 import jalview.io.VamsasAppDatastore;
31 * Enhances DatastoreItem objects with additional functions to do with RangeType
37 public abstract class Rangetype extends DatastoreItem
45 public Rangetype(VamsasAppDatastore datastore)
50 public Rangetype(VamsasAppDatastore datastore, Vobject vobj, Class jvClass)
52 super(datastore, vobj, jvClass);
55 public Rangetype(VamsasAppDatastore datastore, Object jvobj, Class vClass)
57 super(datastore, jvobj, vClass);
61 * get real bounds of a RangeType's specification. start and end are an
62 * inclusive range within which all segments and positions lie. TODO: refactor
66 * @return int[] { start, end}
68 protected int[] getBounds(RangeType dseta)
73 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
76 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
78 if (dseta.getSegCount() > 0)
80 se = getSegRange(dseta.getSeg(0), true);
81 for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
83 int nse[] = getSegRange(dseta.getSeg(s), true);
94 if (dseta.getPosCount() > 0)
96 // could do a polarity for pos range too. and pass back indication of
98 int pos = dseta.getPos(0).getI();
101 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
103 pos = dseta.getPos(p).getI();
120 * map from a rangeType's internal frame to the referenced object's coordinate
124 * @return int [] { ref(pos)...} for all pos in rangeType's frame.
126 protected int[] getMapping(RangeType dseta)
128 Vector posList = new Vector();
132 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
135 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
137 if (dseta.getSegCount() > 0)
139 for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
141 se = getSegRange(dseta.getSeg(s), false);
142 int se_end = se[1 - se[2]] + (se[2] == 0 ? 1 : -1);
143 for (int p = se[se[2]]; p != se_end; p += se[2] == 0 ? 1 : -1)
145 posList.add(new Integer(p));
149 else if (dseta.getPosCount() > 0)
151 int pos = dseta.getPos(0).getI();
153 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
155 pos = dseta.getPos(p).getI();
156 posList.add(new Integer(pos));
160 if (posList != null && posList.size() > 0)
162 int[] range = new int[posList.size()];
163 for (int i = 0; i < range.length; i++)
165 range[i] = ((Integer) posList.elementAt(i)).intValue();
173 protected int[] getIntervals(RangeType range)
175 int[] intervals = null;
176 Vector posList = new Vector();
180 if (range.getSegCount() > 0 && range.getPosCount() > 0)
183 "Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
185 if (range.getSegCount() > 0)
187 for (int s = 0, sSize = range.getSegCount(); s < sSize; s++)
189 se = getSegRange(range.getSeg(s), false);
190 posList.addElement(new Integer(se[0]));
191 posList.addElement(new Integer(se[1]));
194 else if (range.getPosCount() > 0)
196 int pos = range.getPos(0).getI();
198 for (int p = 0, pSize = range.getPosCount(); p < pSize; p++)
200 pos = range.getPos(p).getI();
201 posList.add(new Integer(pos));
202 posList.add(new Integer(pos));
206 if (posList != null && posList.size() > 0)
208 intervals = new int[posList.size()];
209 java.util.Enumeration e = posList.elements();
211 while (e.hasMoreElements())
213 intervals[i++] = ((Integer) e.nextElement()).intValue();
220 * initialise a range type object from a set of start/end inclusive intervals
225 protected void initRangeType(RangeType mrt, int[] range)
227 for (int i = 0; i < range.length; i += 2)
229 Seg vSeg = new Seg();
230 vSeg.setStart(range[i]);
231 vSeg.setEnd(range[i + 1]);
232 vSeg.setInclusive(true);
240 * where the from range is the local mapped range, and the to range
241 * is the 'mapped' range in the MapRangeType
242 * @param default unit for local
243 * @param default unit for mapped
246 protected jalview.util.MapList parsemapType(MapType maprange, int localu,
249 jalview.util.MapList ml = null;
250 int[] localRange = getIntervals(maprange.getLocal());
251 int[] mappedRange = getIntervals(maprange.getMapped());
252 long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit()
254 long mu = maprange.getMapped().hasUnit() ? maprange.getMapped()
255 .getUnit() : mappedu;
256 ml = new jalview.util.MapList(localRange, mappedRange, (int) lu,
261 protected jalview.util.MapList parsemapType(MapType map)
263 if (!map.getLocal().hasUnit() || map.getMapped().hasUnit())
265 jalview.bin.Cache.log
266 .warn("using default mapping length of 1:1 for map "
267 + (map.isRegistered() ? map.getVorbaId().toString()
268 : ("<no Id registered> " + map.toString())));
270 return parsemapType(map, 1, 1);
274 * initialise a MapType object from a MapList object.
280 protected void initMapType(MapType maprange, jalview.util.MapList ml,
283 initMapType(maprange, ml, setUnits, false);
292 * - reverse MapList mapping for Local and Mapped ranges and units
294 protected void initMapType(MapType maprange, jalview.util.MapList ml,
295 boolean setUnits, boolean reverse)
300 "Implementation error. MapList is null for initMapType.");
302 maprange.setLocal(new Local());
303 maprange.setMapped(new Mapped());
306 initRangeType(maprange.getLocal(), ml.getFromRanges());
307 initRangeType(maprange.getMapped(), ml.getToRanges());
311 initRangeType(maprange.getLocal(), ml.getToRanges());
312 initRangeType(maprange.getMapped(), ml.getFromRanges());
318 maprange.getLocal().setUnit(ml.getFromRatio());
319 maprange.getMapped().setUnit(ml.getToRatio());
323 maprange.getLocal().setUnit(ml.getToRatio());
324 maprange.getMapped().setUnit(ml.getFromRatio());
326 // TODO: and verify - raise an implementation fault notice if local/mapped
327 // range % Local/Mapped Ratio != 0
328 // if (uk.ac.vamsas.objects.utils.Range.getIntervals(range))