\r
import java.util.Vector;\r
\r
-\r
+import uk.ac.vamsas.objects.core.Local;\r
+import uk.ac.vamsas.objects.core.MapType;\r
+import uk.ac.vamsas.objects.core.Mapped;\r
import uk.ac.vamsas.objects.core.RangeType;\r
import uk.ac.vamsas.objects.core.Seg;\r
\r
+/**\r
+ * Utilities for working with RangeType and MapType objects.\r
+ * Derived from bitter experience.\r
+ * @author JimP\r
+ *\r
+ */\r
public class Range {\r
\r
/**\r
}\r
return null;\r
}\r
+\r
+ public static int[] getIntervals(RangeType range)\r
+ {\r
+ int[] intervals=null;\r
+ Vector posList = new Vector();\r
+ if (range != null)\r
+ {\r
+ int[] se = null;\r
+ if (range.getSegCount() > 0 && range.getPosCount() > 0)\r
+ {\r
+ throw new Error("Invalid vamsas RangeType - cannot resolve both lists of Pos and Seg from choice!");\r
}\r
+ if (range.getSegCount() > 0)\r
+ {\r
+ for (int s = 0, sSize = range.getSegCount(); s < sSize; s++)\r
+ {\r
+ se = getSegRange(range.getSeg(s), false);\r
+ posList.addElement(new Integer(se[0]));\r
+ posList.addElement(new Integer(se[1]));\r
+ }\r
+ }\r
+ else if (range.getPosCount() > 0)\r
+ {\r
+ int pos = range.getPos(0).getI();\r
+ for (int p = 0, pSize = range.getPosCount(); p < pSize; p++)\r
+ {\r
+ pos = range.getPos(p).getI();\r
+ posList.add(new Integer(pos));\r
+ posList.add(new Integer(pos));\r
+ }\r
+ }\r
+ }\r
+ if (posList != null && posList.size() > 0)\r
+ {\r
+ intervals=new int[posList.size()];\r
+ java.util.Enumeration e = posList.elements();\r
+ int i=0;\r
+ while (e.hasMoreElements())\r
+ {\r
+ intervals[i++] = ((Integer)e.nextElement()).intValue();\r
+ }\r
+ }\r
+ return intervals;\r
+ }\r
+ /**\r
+ * initialise a range type object from a set of start/end inclusive intervals\r
+ * @param mrt\r
+ * @param range\r
+ */\r
+ public static void initRangeType(RangeType mrt, int[] range)\r
+ {\r
+ for (int i=0; i<range.length; i+=2)\r
+ {\r
+ Seg vSeg = new Seg();\r
+ vSeg.setStart(range[i]);\r
+ vSeg.setEnd(range[i+1]);\r
+ vSeg.setInclusive(true);\r
+ mrt.addSeg(vSeg);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param maprange where the from range is the local mapped range, and the to range is the 'mapped' range in the MapRangeType\r
+ * @param default unit for local\r
+ * @param default unit for mapped\r
+ * @return MapList\r
+ */\r
+ public static MapList parsemapType(MapType maprange, int localu, int mappedu)\r
+ {\r
+ MapList ml = null;\r
+ int[] localRange = getIntervals(maprange.getLocal());\r
+ int[] mappedRange = getIntervals(maprange.getMapped());\r
+ long lu = maprange.getLocal().hasUnit() ? maprange.getLocal().getUnit() : localu;\r
+ long mu = maprange.getMapped().hasUnit() ? maprange.getMapped().getUnit() : mappedu;\r
+ ml = new MapList(localRange, mappedRange, (int)lu, (int)mu);\r
+ return ml;\r
+ }\r
+ /**\r
+ * Parse map with default units of 1,1\r
+ * @param map\r
+ * @return MapList representing map\r
+ */\r
+ public static MapList parsemapType(MapType map)\r
+ {\r
+ return parsemapType(map, 1, 1); \r
+ }\r
+\r
+ /**\r
+ * initialise a MapType object from a MapList object.\r
+ * @param maprange\r
+ * @param ml\r
+ * @param setUnits\r
+ */\r
+ public static void initMapType(MapType maprange, MapList ml, boolean setUnits)\r
+ {\r
+ initMapType(maprange, ml, setUnits, false);\r
+ }\r
+ /**\r
+ * \r
+ * @param maprange\r
+ * @param ml\r
+ * @param setUnits\r
+ * @param reverse - reverse MapList mapping for Local and Mapped ranges and units \r
+ */\r
+ public static void initMapType(MapType maprange, MapList ml, boolean setUnits, boolean reverse)\r
+ {\r
+ maprange.setLocal(new Local());\r
+ maprange.setMapped(new Mapped());\r
+ if (!reverse)\r
+ {\r
+ initRangeType(maprange.getLocal(), ml.getFromRanges());\r
+ initRangeType(maprange.getMapped(), ml.getToRanges());\r
+ } else {\r
+ initRangeType(maprange.getLocal(), ml.getToRanges());\r
+ initRangeType(maprange.getMapped(), ml.getFromRanges());\r
+ }\r
+ if (setUnits)\r
+ {\r
+ if (!reverse)\r
+ { \r
+ maprange.getLocal().setUnit(ml.getFromRatio());\r
+ maprange.getMapped().setUnit(ml.getToRatio());\r
+ } else {\r
+ maprange.getLocal().setUnit(ml.getToRatio());\r
+ maprange.getMapped().setUnit(ml.getFromRatio());\r
+ }\r
+ }\r
+ }\r
+\r
+}
\ No newline at end of file