1 package jalview.io.vamsas;
4 import java.util.Vector;
6 import uk.ac.vamsas.objects.core.Local;
7 import uk.ac.vamsas.objects.core.Map;
8 import uk.ac.vamsas.objects.core.MapType;
9 import uk.ac.vamsas.objects.core.Mapped;
10 import uk.ac.vamsas.objects.core.RangeType;
11 import uk.ac.vamsas.objects.core.Seg;
12 import jalview.datamodel.Mapping;
13 import jalview.io.VamsasAppDatastore;
15 public class Rangetype extends DatastoreItem
23 public Rangetype(VamsasAppDatastore datastore)
29 * get real bounds of a RangeType's specification. start and end are an
30 * inclusive range within which all segments and positions lie.
31 * TODO: refactor to vamsas utils
33 * @return int[] { start, end}
35 protected int[] getBounds(RangeType dseta)
40 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
42 throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
44 if (dseta.getSegCount() > 0)
46 se = getSegRange(dseta.getSeg(0), true);
47 for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
49 int nse[] = getSegRange(dseta.getSeg(s), true);
60 if (dseta.getPosCount() > 0)
62 // could do a polarity for pos range too. and pass back indication of discontinuities.
63 int pos = dseta.getPos(0).getI();
67 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
69 pos = dseta.getPos(p).getI();
86 * map from a rangeType's internal frame to the referenced object's coordinate frame.
88 * @return int [] { ref(pos)...} for all pos in rangeType's frame.
90 protected int[] getMapping(RangeType dseta)
92 Vector posList = new Vector();
96 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
98 throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
100 if (dseta.getSegCount() > 0)
102 for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
104 se = getSegRange(dseta.getSeg(s), false);
105 int se_end = se[1 - se[2]] + (se[2] == 0 ? 1 : -1);
106 for (int p = se[se[2]]; p != se_end; p += se[2] == 0 ? 1 : -1)
108 posList.add(new Integer(p));
112 else if (dseta.getPosCount() > 0)
114 int pos = dseta.getPos(0).getI();
116 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
118 pos = dseta.getPos(p).getI();
119 posList.add(new Integer(pos));
123 if (posList != null && posList.size() > 0)
125 int[] range = new int[posList.size()];
126 for (int i = 0; i < range.length; i++)
128 range[i] = ( (Integer) posList.elementAt(i)).intValue();
136 protected int[] getIntervals(RangeType range)
138 int[] intervals=null;
139 Vector posList = new Vector();
143 if (range.getSegCount() > 0 && range.getPosCount() > 0)
145 throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
147 if (range.getSegCount() > 0)
149 for (int s = 0, sSize = range.getSegCount(); s < sSize; s++)
151 se = getSegRange(range.getSeg(s), false);
152 posList.addElement(new Integer(se[0]));
153 posList.addElement(new Integer(se[1]));
156 else if (range.getPosCount() > 0)
158 int pos = range.getPos(0).getI();
160 for (int p = 0, pSize = range.getPosCount(); p < pSize; p++)
162 pos = range.getPos(p).getI();
163 posList.add(new Integer(pos));
164 posList.add(new Integer(pos));
168 if (posList != null && posList.size() > 0)
170 intervals=new int[posList.size()];
171 java.util.Enumeration e = posList.elements();
173 while (e.hasMoreElements())
175 intervals[i++] = ((Integer)e.nextElement()).intValue();
181 * initialise a range type object from a set of start/end inclusive intervals
185 protected void initRangeType(RangeType mrt, int[] range)
187 for (int i=0; i<range.length; i+=2)
189 Seg vSeg = new Seg();
190 vSeg.setStart(range[i]);
191 vSeg.setEnd(range[i+1]);
192 vSeg.setInclusive(true);
199 * @param maprange where the from range is the local mapped range, and the to range is the 'mapped' range in the MapRangeType
200 * @param default unit for local
201 * @param default unit for mapped
204 protected jalview.util.MapList parsemapType(MapType maprange, int localu, int mappedu)
206 jalview.util.MapList ml = null;
207 int[] localRange = getIntervals(maprange.getLocal());
208 int[] mappedRange = getIntervals(maprange.getMapped());
209 long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit() : localu;
210 long mu = maprange.getMapped().hasUnit() ? maprange.getMapped().getUnit() : mappedu;
211 ml = new jalview.util.MapList(localRange, mappedRange, (int)lu, (int)mu);
214 protected jalview.util.MapList parsemapType(MapType map)
216 if (!map.getLocal().hasUnit() || map.getMapped().hasUnit())
218 jalview.bin.Cache.log.warn("using default mapping length of 1:1 for map "
219 +(map.isRegistered() ? map.getVorbaId().toString() : ("<no Id registered> "+map.toString())));
221 return parsemapType(map, 1, 1);
225 * initialise a MapType object from a MapList object.
230 protected void initMapType(MapType maprange, jalview.util.MapList ml, boolean setUnits)
232 initMapType(maprange, ml, setUnits, false);
239 * @param reverse - reverse MapList mapping for Local and Mapped ranges and units
241 protected void initMapType(MapType maprange, jalview.util.MapList ml, boolean setUnits, boolean reverse)
245 throw new Error("Implementation error. MapList is null for initMapType.");
247 maprange.setLocal(new Local());
248 maprange.setMapped(new Mapped());
251 initRangeType(maprange.getLocal(), ml.getFromRanges());
252 initRangeType(maprange.getMapped(), ml.getToRanges());
254 initRangeType(maprange.getLocal(), ml.getToRanges());
255 initRangeType(maprange.getMapped(), ml.getFromRanges());
261 maprange.getLocal().setUnit(ml.getFromRatio());
262 maprange.getMapped().setUnit(ml.getToRatio());
264 maprange.getLocal().setUnit(ml.getToRatio());
265 maprange.getMapped().setUnit(ml.getFromRatio());