2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.io.vamsas;
22 import java.util.Vector;
24 import uk.ac.vamsas.objects.core.Local;
25 import uk.ac.vamsas.objects.core.Map;
26 import uk.ac.vamsas.objects.core.MapType;
27 import uk.ac.vamsas.objects.core.Mapped;
28 import uk.ac.vamsas.objects.core.RangeType;
29 import uk.ac.vamsas.objects.core.Seg;
30 import jalview.datamodel.Mapping;
31 import jalview.io.VamsasAppDatastore;
33 public class Rangetype extends DatastoreItem
41 public Rangetype(VamsasAppDatastore datastore)
47 * get real bounds of a RangeType's specification. start and end are an
48 * inclusive range within which all segments and positions lie.
49 * TODO: refactor to vamsas utils
51 * @return int[] { start, end}
53 protected int[] getBounds(RangeType dseta)
58 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
60 throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
62 if (dseta.getSegCount() > 0)
64 se = getSegRange(dseta.getSeg(0), true);
65 for (int s = 1, sSize = dseta.getSegCount(); s < sSize; s++)
67 int nse[] = getSegRange(dseta.getSeg(s), true);
78 if (dseta.getPosCount() > 0)
80 // could do a polarity for pos range too. and pass back indication of discontinuities.
81 int pos = dseta.getPos(0).getI();
85 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
87 pos = dseta.getPos(p).getI();
104 * map from a rangeType's internal frame to the referenced object's coordinate frame.
106 * @return int [] { ref(pos)...} for all pos in rangeType's frame.
108 protected int[] getMapping(RangeType dseta)
110 Vector posList = new Vector();
114 if (dseta.getSegCount() > 0 && dseta.getPosCount() > 0)
116 throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
118 if (dseta.getSegCount() > 0)
120 for (int s = 0, sSize = dseta.getSegCount(); s < sSize; s++)
122 se = getSegRange(dseta.getSeg(s), false);
123 int se_end = se[1 - se[2]] + (se[2] == 0 ? 1 : -1);
124 for (int p = se[se[2]]; p != se_end; p += se[2] == 0 ? 1 : -1)
126 posList.add(new Integer(p));
130 else if (dseta.getPosCount() > 0)
132 int pos = dseta.getPos(0).getI();
134 for (int p = 0, pSize = dseta.getPosCount(); p < pSize; p++)
136 pos = dseta.getPos(p).getI();
137 posList.add(new Integer(pos));
141 if (posList != null && posList.size() > 0)
143 int[] range = new int[posList.size()];
144 for (int i = 0; i < range.length; i++)
146 range[i] = ( (Integer) posList.elementAt(i)).intValue();
154 protected int[] getIntervals(RangeType range)
156 int[] intervals=null;
157 Vector posList = new Vector();
161 if (range.getSegCount() > 0 && range.getPosCount() > 0)
163 throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");
165 if (range.getSegCount() > 0)
167 for (int s = 0, sSize = range.getSegCount(); s < sSize; s++)
169 se = getSegRange(range.getSeg(s), false);
170 posList.addElement(new Integer(se[0]));
171 posList.addElement(new Integer(se[1]));
174 else if (range.getPosCount() > 0)
176 int pos = range.getPos(0).getI();
178 for (int p = 0, pSize = range.getPosCount(); p < pSize; p++)
180 pos = range.getPos(p).getI();
181 posList.add(new Integer(pos));
182 posList.add(new Integer(pos));
186 if (posList != null && posList.size() > 0)
188 intervals=new int[posList.size()];
189 java.util.Enumeration e = posList.elements();
191 while (e.hasMoreElements())
193 intervals[i++] = ((Integer)e.nextElement()).intValue();
199 * initialise a range type object from a set of start/end inclusive intervals
203 protected void initRangeType(RangeType mrt, int[] range)
205 for (int i=0; i<range.length; i+=2)
207 Seg vSeg = new Seg();
208 vSeg.setStart(range[i]);
209 vSeg.setEnd(range[i+1]);
210 vSeg.setInclusive(true);
217 * @param maprange where the from range is the local mapped range, and the to range is the 'mapped' range in the MapRangeType
218 * @param default unit for local
219 * @param default unit for mapped
222 protected jalview.util.MapList parsemapType(MapType maprange, int localu, int mappedu)
224 jalview.util.MapList ml = null;
225 int[] localRange = getIntervals(maprange.getLocal());
226 int[] mappedRange = getIntervals(maprange.getMapped());
227 long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit() : localu;
228 long mu = maprange.getMapped().hasUnit() ? maprange.getMapped().getUnit() : mappedu;
229 ml = new jalview.util.MapList(localRange, mappedRange, (int)lu, (int)mu);
232 protected jalview.util.MapList parsemapType(MapType map)
234 if (!map.getLocal().hasUnit() || map.getMapped().hasUnit())
236 jalview.bin.Cache.log.warn("using default mapping length of 1:1 for map "
237 +(map.isRegistered() ? map.getVorbaId().toString() : ("<no Id registered> "+map.toString())));
239 return parsemapType(map, 1, 1);
243 * initialise a MapType object from a MapList object.
248 protected void initMapType(MapType maprange, jalview.util.MapList ml, boolean setUnits)
250 initMapType(maprange, ml, setUnits, false);
257 * @param reverse - reverse MapList mapping for Local and Mapped ranges and units
259 protected void initMapType(MapType maprange, jalview.util.MapList ml, boolean setUnits, boolean reverse)
263 throw new Error("Implementation error. MapList is null for initMapType.");
265 maprange.setLocal(new Local());
266 maprange.setMapped(new Mapped());
269 initRangeType(maprange.getLocal(), ml.getFromRanges());
270 initRangeType(maprange.getMapped(), ml.getToRanges());
272 initRangeType(maprange.getLocal(), ml.getToRanges());
273 initRangeType(maprange.getMapped(), ml.getFromRanges());
279 maprange.getLocal().setUnit(ml.getFromRatio());
280 maprange.getMapped().setUnit(ml.getToRatio());
282 maprange.getLocal().setUnit(ml.getToRatio());
283 maprange.getMapped().setUnit(ml.getFromRatio());