-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
- */\r
-package jalview.datamodel;\r
-\r
-import jalview.util.ShiftList;\r
-\r
-import java.util.*;\r
-\r
-/**\r
- * NOTE: Columns are zero based.\r
- */\r
-public class ColumnSelection\r
-{\r
- Vector selected = new Vector();\r
-\r
- //Vector of int [] {startCol, endCol}\r
- Vector hiddenColumns;\r
-\r
- /**\r
- * Add a column to the selection\r
- *\r
- * @param col index of column\r
- */\r
- public void addElement(int col)\r
- {\r
- Integer column = new Integer(col);\r
- if (!selected.contains(column))\r
- {\r
- selected.addElement(column);\r
- }\r
- }\r
-\r
- /**\r
- * clears column selection\r
- */\r
- public void clear()\r
- {\r
- selected.removeAllElements();\r
- }\r
-\r
- /**\r
- * removes col from selection\r
- *\r
- * @param col index of column to be removed\r
- */\r
- public void removeElement(int col)\r
- {\r
- Integer colInt = new Integer(col);\r
-\r
- if (selected.contains(colInt))\r
- {\r
- selected.removeElement(colInt);\r
- }\r
- }\r
-\r
- /**\r
- * removes a range of columns from the selection\r
- * @param start int - first column in range to be removed\r
- * @param end int - last col\r
- */\r
- public void removeElements(int start, int end)\r
- {\r
- Integer colInt;\r
- for(int i=start; i<end; i++)\r
- {\r
- colInt = new Integer(i);\r
- if (selected.contains(colInt))\r
- {\r
- selected.removeElement(colInt);\r
- }\r
- }\r
- }\r
- /**\r
- *\r
- * @return Vector containing selected columns as Integers\r
- */\r
- public Vector getSelected()\r
- {\r
- return selected;\r
- }\r
-\r
- /**\r
- *\r
- * @param col index to search for in column selection\r
- *\r
- * @return true if Integer(col) is in selection.\r
- */\r
- public boolean contains(int col)\r
- {\r
- return selected.contains(new Integer(col));\r
- }\r
-\r
- /**\r
- * DOCUMENT ME!\r
- *\r
- * @param i DOCUMENT ME!\r
- *\r
- * @return DOCUMENT ME!\r
- */\r
- public int columnAt(int i)\r
- {\r
- return ((Integer) selected.elementAt(i)).intValue();\r
- }\r
-\r
- /**\r
- * DOCUMENT ME!\r
- *\r
- * @return DOCUMENT ME!\r
- */\r
- public int size()\r
- {\r
- return selected.size();\r
- }\r
-\r
- /**\r
- * DOCUMENT ME!\r
- *\r
- * @return DOCUMENT ME!\r
- */\r
- public int getMax()\r
- {\r
- int max = -1;\r
-\r
- for (int i = 0; i < selected.size(); i++)\r
- {\r
- if (columnAt(i) > max)\r
- {\r
- max = columnAt(i);\r
- }\r
- }\r
-\r
- return max;\r
- }\r
-\r
- /**\r
- * DOCUMENT ME!\r
- *\r
- * @return DOCUMENT ME!\r
- */\r
- public int getMin()\r
- {\r
- int min = 1000000000;\r
-\r
- for (int i = 0; i < selected.size(); i++)\r
- {\r
- if (columnAt(i) < min)\r
- {\r
- min = columnAt(i);\r
- }\r
- }\r
-\r
- return min;\r
- }\r
-\r
-\r
- /**\r
- * propagate shift in alignment columns to column selection\r
- *\r
- * @param start beginning of edit\r
- * @param change shift in edit (-ve or +ve number of columns)\r
- */\r
- public void compensateForEdit(int start, int change)\r
- {\r
- for (int i = 0; i < size(); i++)\r
- {\r
- int temp = columnAt(i);\r
-\r
- if (temp >= start)\r
- {\r
- selected.setElementAt(new Integer(temp - change), i);\r
- }\r
- }\r
-\r
- if(hiddenColumns!=null)\r
- {\r
- for(int i=0; i<hiddenColumns.size(); i++)\r
- {\r
- int[] region = (int[]) hiddenColumns.elementAt(i);\r
- if(region[0] > start)\r
- {\r
- region[0] -= change;\r
- region[1] -= change;\r
- }\r
- if(region[0]<0)\r
- region[0] = 0;\r
- if(region[1] <0)\r
- region[1] = 0;\r
- }\r
- }\r
- }\r
- public ShiftList compensateForEdits(ShiftList shiftrecord) {\r
- if (shiftrecord!=null) {\r
- Vector shifts = shiftrecord.shifts;\r
- if (shifts!=null && shifts.size()>0) {\r
- for (int i=0,j=shifts.size(); i<j; i++) {\r
- int[] sh = (int[]) shifts.elementAt(i);\r
- compensateForEdit(sh[0], sh[1]);\r
- }\r
- }\r
- return shiftrecord.getInverse();\r
- }\r
- return null;\r
- }\r
- /**\r
- * This Method is used to return all the HiddenColumn regions\r
- * less than the given index.\r
- * @param end int\r
- * @return Vector\r
- */\r
- public Vector getHiddenColumns()\r
- {\r
- return hiddenColumns;\r
- }\r
- /**\r
- * Return absolute column index for a visible column index\r
- * @param column int column index in alignment view\r
- * @return alignment column index for column\r
- */\r
- public int adjustForHiddenColumns(int column)\r
- {\r
- int result = column;\r
- if (hiddenColumns != null)\r
- {\r
- for (int i = 0; i < hiddenColumns.size(); i++)\r
- {\r
- int[] region = (int[]) hiddenColumns.elementAt(i);\r
- if (result >= region[0])\r
- {\r
- result += region[1] - region[0] + 1;\r
- }\r
- }\r
- }\r
- return result;\r
- }\r
-\r
- /**\r
- * Use this method to find out where a visible column is in the alignment\r
- * when hidden columns exist\r
- * @param hiddenColumn int\r
- * @return int\r
- */\r
- public int findColumnPosition(int hiddenColumn)\r
- {\r
- int result = hiddenColumn;\r
- if (hiddenColumns != null)\r
- {\r
- int index = 0;\r
- int gaps = 0;\r
- do\r
- {\r
- int[] region = (int[]) hiddenColumns.elementAt(index);\r
- if (hiddenColumn > region[1])\r
- {\r
- result -= region[1]+1-region[0];\r
- }\r
- index++;\r
- }\r
- while (index < hiddenColumns.size());\r
-\r
- result -= gaps;\r
- }\r
-\r
- return result;\r
- }\r
-\r
- /**\r
- * Use this method to determine where the next hiddenRegion starts\r
- */\r
- public int findHiddenRegionPosition(int hiddenRegion)\r
- {\r
- int result = 0;\r
- if (hiddenColumns != null)\r
- {\r
- int index = 0;\r
- int gaps = 0;\r
- do\r
- {\r
- int[] region = (int[]) hiddenColumns.elementAt(index);\r
- if(hiddenRegion==0)\r
- {\r
- return region[0];\r
- }\r
-\r
- gaps += region[1] +1 - region[0];\r
- result = region[1] +1;\r
- index++;\r
- }\r
- while(index < hiddenRegion+1);\r
-\r
- result -= gaps;\r
- }\r
-\r
- return result;\r
- }\r
-\r
- /**\r
- * THis method returns the rightmost limit of a\r
- * region of an alignment with hidden columns.\r
- * In otherwords, the next hidden column.\r
- * @param index int\r
- */\r
- public int getHiddenBoundaryRight(int alPos)\r
- {\r
- if (hiddenColumns != null)\r
- {\r
- int index = 0;\r
- do\r
- {\r
- int[] region = (int[]) hiddenColumns.elementAt(index);\r
- if(alPos < region[0])\r
- return region[0];\r
-\r
- index++;\r
- }\r
- while(index < hiddenColumns.size());\r
- }\r
-\r
- return alPos;\r
-\r
- }\r
- /**\r
- * THis method returns the rightmost limit of a\r
- * region of an alignment with hidden columns.\r
- * In otherwords, the next hidden column.\r
- * @param index int\r
- */\r
- public int getHiddenBoundaryLeft(int alPos)\r
- {\r
- if (hiddenColumns != null)\r
- {\r
- int index = hiddenColumns.size()-1;\r
- do\r
- {\r
- int[] region = (int[]) hiddenColumns.elementAt(index);\r
- if(alPos > region[1])\r
- return region[1];\r
-\r
- index--;\r
- }\r
- while(index >-1);\r
- }\r
-\r
- return alPos;\r
-\r
- }\r
-\r
- public void hideSelectedColumns()\r
- {\r
- while (size() > 0)\r
- {\r
- int column = ( (Integer) getSelected().firstElement()).intValue();\r
- hideColumns(column);\r
- }\r
-\r
- }\r
-\r
- public void hideColumns(int start, int end)\r
- {\r
- if(hiddenColumns==null)\r
- hiddenColumns = new Vector();\r
-\r
- boolean added = false;\r
- boolean overlap = false;\r
-\r
- for (int i = 0; i < hiddenColumns.size(); i++)\r
- {\r
- int[] region = (int[]) hiddenColumns.elementAt(i);\r
- if ( start<=region[1] && end>=region[0])\r
- {\r
- hiddenColumns.removeElementAt(i);\r
- overlap = true;\r
- break;\r
- }\r
- else if (end < region[0] && start < region[0])\r
- {\r
- hiddenColumns.insertElementAt(new int[]\r
- {start, end}, i);\r
- added = true;\r
- break;\r
- }\r
- }\r
-\r
- if(overlap)\r
- {\r
- hideColumns(start, end);\r
- }\r
- else if (!added)\r
- hiddenColumns.addElement(new int[] {start, end});\r
-\r
- }\r
-\r
- /**\r
- * This method will find a range of selected columns\r
- * around the column specified\r
- * @param res int\r
- */\r
- public void hideColumns(int col)\r
- {\r
- // First find out range of columns to hide\r
- int min = col, max = col+1;\r
- while( contains(min) )\r
- { removeElement(min); min --; }\r
-\r
- while( contains(max) )\r
- { removeElement(max); max ++; }\r
-\r
- min++; max--;\r
-\r
- hideColumns(min, max);\r
- }\r
-\r
- public void revealAllHiddenColumns()\r
- {\r
- if(hiddenColumns!=null)\r
- {\r
- for (int i = 0; i < hiddenColumns.size(); i++)\r
- {\r
- int[] region = (int[]) hiddenColumns.elementAt(i);\r
- for (int j = region[0]; j < region[1]+1; j++)\r
- {\r
- addElement(j);\r
- }\r
- }\r
- }\r
-\r
- hiddenColumns = null;\r
- }\r
-\r
- public void revealHiddenColumns(int res)\r
- {\r
- for(int i=0; i<hiddenColumns.size(); i++)\r
- {\r
- int [] region = (int[])hiddenColumns.elementAt(i);\r
- if( res == region[0])\r
- {\r
- for (int j = region[0]; j < region[1]+1; j++)\r
- {\r
- addElement(j);\r
- }\r
-\r
- hiddenColumns.removeElement(region);\r
- break;\r
- }\r
- }\r
- if(hiddenColumns.size()==0)\r
- hiddenColumns = null;\r
- }\r
-\r
- public boolean isVisible(int column)\r
- {\r
- for(int i=0; i<hiddenColumns.size(); i++)\r
- {\r
- int [] region = (int[])hiddenColumns.elementAt(i);\r
- if( column >= region[0] && column <= region[1])\r
- {\r
- return false;\r
- }\r
- }\r
- return true;\r
- }\r
- /**\r
- * Copy constructor\r
- * @param copy\r
- */\r
- public ColumnSelection(ColumnSelection copy) {\r
- if (copy!=null) {\r
- if (copy.selected!=null) {\r
- selected = new Vector();\r
- for (int i=0,j=copy.selected.size(); i<j; i++) {\r
- selected.setElementAt(((Integer) copy.selected.elementAt(i)), i);\r
- }\r
- }\r
- if (copy.hiddenColumns!=null) {\r
- hiddenColumns=new Vector();\r
- for (int i=0,j=copy.hiddenColumns.size(); i<j; i++) {\r
- int[] rh,cp;\r
- rh = (int[])copy.hiddenColumns.elementAt(i);\r
- if (rh!=null) {\r
- cp = new int[rh.length];\r
- System.arraycopy(rh, 0, cp, 0, rh.length);\r
- hiddenColumns.setElementAt(cp, i);\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * ColumnSelection\r
- */\r
- public ColumnSelection()\r
- {\r
- }\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer
+ * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+package jalview.datamodel;
+
+import jalview.util.ShiftList;
+
+import java.util.*;
+
+/**
+ * NOTE: Columns are zero based.
+ */
+public class ColumnSelection
+{
+ Vector selected = new Vector();
+
+ //Vector of int [] {startCol, endCol}
+ Vector hiddenColumns;
+
+ /**
+ * Add a column to the selection
+ *
+ * @param col index of column
+ */
+ public void addElement(int col)
+ {
+ Integer column = new Integer(col);
+ if (!selected.contains(column))
+ {
+ selected.addElement(column);
+ }
+ }
+
+ /**
+ * clears column selection
+ */
+ public void clear()
+ {
+ selected.removeAllElements();
+ }
+
+ /**
+ * removes col from selection
+ *
+ * @param col index of column to be removed
+ */
+ public void removeElement(int col)
+ {
+ Integer colInt = new Integer(col);
+
+ if (selected.contains(colInt))
+ {
+ selected.removeElement(colInt);
+ }
+ }
+
+ /**
+ * removes a range of columns from the selection
+ * @param start int - first column in range to be removed
+ * @param end int - last col
+ */
+ public void removeElements(int start, int end)
+ {
+ Integer colInt;
+ for(int i=start; i<end; i++)
+ {
+ colInt = new Integer(i);
+ if (selected.contains(colInt))
+ {
+ selected.removeElement(colInt);
+ }
+ }
+ }
+ /**
+ *
+ * @return Vector containing selected columns as Integers
+ */
+ public Vector getSelected()
+ {
+ return selected;
+ }
+
+ /**
+ *
+ * @param col index to search for in column selection
+ *
+ * @return true if Integer(col) is in selection.
+ */
+ public boolean contains(int col)
+ {
+ return selected.contains(new Integer(col));
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @param i DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public int columnAt(int i)
+ {
+ return ((Integer) selected.elementAt(i)).intValue();
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public int size()
+ {
+ return selected.size();
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public int getMax()
+ {
+ int max = -1;
+
+ for (int i = 0; i < selected.size(); i++)
+ {
+ if (columnAt(i) > max)
+ {
+ max = columnAt(i);
+ }
+ }
+
+ return max;
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public int getMin()
+ {
+ int min = 1000000000;
+
+ for (int i = 0; i < selected.size(); i++)
+ {
+ if (columnAt(i) < min)
+ {
+ min = columnAt(i);
+ }
+ }
+
+ return min;
+ }
+
+
+ /**
+ * propagate shift in alignment columns to column selection
+ *
+ * @param start beginning of edit
+ * @param left shift in edit (+ve for removal, or -ve for inserts)
+ */
+ public void compensateForEdit(int start, int change)
+ {
+ for (int i = 0; i < size(); i++)
+ {
+ int temp = columnAt(i);
+
+ if (temp >= start)
+ {
+ selected.setElementAt(new Integer(temp - change), i);
+ }
+ }
+
+ if(hiddenColumns!=null)
+ {
+ for(int i=0; i<hiddenColumns.size(); i++)
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(i);
+ if(region[0] > start)
+ {
+ region[0] -= change;
+ region[1] -= change;
+ }
+ if(region[0]<0)
+ region[0] = 0;
+ if(region[1] <0)
+ region[1] = 0;
+ }
+ }
+ }
+ /**
+ * propagate shift in alignment columns to column selection
+ * special version of compensateForEdit - allowing for edits within hidden regions
+ * @param start beginning of edit
+ * @param left shift in edit (+ve for removal, or -ve for inserts)
+ */
+ private void compensateForDelEdits(int start, int change)
+ {
+ for (int i = 0; i < size(); i++)
+ {
+ int temp = columnAt(i);
+
+ if (temp >= start)
+ {
+ selected.setElementAt(new Integer(temp - change), i);
+ }
+ }
+
+ if(hiddenColumns!=null)
+ {
+ for(int i=0; i<hiddenColumns.size(); i++)
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(i);
+ if(region[0] >= start)
+ {
+ region[0] -= change;
+ }
+ if (region[1]>= start) {
+ region[1] -=change;
+ }
+ if (region[1]<region[0]) {
+ hiddenColumns.removeElementAt(i--);
+ }
+
+ if(region[0]<0)
+ region[0] = 0;
+ if(region[1] <0)
+ region[1] = 0;
+ }
+ }
+ }
+ /**
+ * Adjust hidden column boundaries based on a series of column
+ * additions or deletions in visible regions.
+ * @param shiftrecord
+ * @return
+ */
+ public ShiftList compensateForEdits(ShiftList shiftrecord) {
+ if (shiftrecord!=null) {
+ Vector shifts = shiftrecord.shifts;
+ if (shifts!=null && shifts.size()>0) {
+ int shifted=0;
+ for (int i=0,j=shifts.size(); i<j; i++) {
+ int[] sh = (int[]) shifts.get(i);
+ //compensateForEdit(shifted+sh[0], sh[1]);
+ compensateForDelEdits(shifted+sh[0], sh[1]);
+ shifted-=sh[1];
+ }
+ }
+ return shiftrecord.getInverse();
+ }
+ return null;
+ }
+ /**
+ * removes intersection of position,length ranges in deletions
+ * from the start,end regions marked in intervals.
+ * @param deletions
+ * @param intervals
+ * @return
+ */
+ private boolean pruneIntervalVector(Vector deletions, Vector intervals) {
+ boolean pruned=false;
+ int i=0,j=intervals.size()-1, s=0, t=deletions.size()-1;
+ int hr[]=(int[]) intervals.elementAt(i);
+ int sr[]=(int[]) deletions.elementAt(s);
+ while (i<=j && s<=t) {
+ boolean trailinghn=hr[1]>=sr[0];
+ if (!trailinghn) {
+ if (i<j)
+ hr=(int[]) intervals.elementAt(++i);
+ else
+ i++;
+ continue;
+ }
+ int endshift=sr[0]+sr[1]; // deletion ranges - -ve means an insert
+ if (endshift<hr[0] || endshift<sr[0]) { // leadinghc disjoint or not a deletion
+ if (s<t)
+ sr=(int[]) deletions.elementAt(++s);
+ else
+ s++;
+ continue;
+ }
+ boolean leadinghn=hr[0]>=sr[0];
+ boolean leadinghc=hr[0]<endshift;
+ boolean trailinghc=hr[1]<endshift;
+ if (leadinghn) {
+ if (trailinghc) {// deleted hidden region.
+ intervals.removeElementAt(i);
+ pruned=true;
+ j--;
+ if (i<=j)
+ hr=(int[]) intervals.elementAt(i);
+ continue;
+ }
+ if (leadinghc) {
+ hr[0]=endshift; // clip c terminal region
+ leadinghn=!leadinghn;
+ pruned=true;
+ }
+ }
+ if (!leadinghn) {
+ if (trailinghc) {
+ if (trailinghn) {
+ hr[1]=sr[0]-1;
+ pruned=true;
+ }
+ } else {
+ // sr contained in hr
+ if (s<t)
+ sr=(int[]) deletions.elementAt(++s);
+ else
+ s++;
+ continue;
+ }
+ }
+ }
+ return pruned; // true if any interval was removed or modified by operations.
+ }
+ private boolean pruneColumnList(Vector deletion, Vector list) {
+ int s=0,t=deletion.size();
+ int[] sr=(int[])list.get(s++);
+ boolean pruned=false;
+ int i=0, j=list.size();
+ while (i<j && s<=t) {
+ int c=((Integer)list.elementAt(i++)).intValue();
+ if (sr[0]<=c) {
+ if (sr[1]+sr[0]>=c) { // sr[1] -ve means inseriton.
+ list.removeElementAt(--i);
+ j--;
+ } else {
+ if (s<t)
+ sr = (int[])deletion.elementAt(s);
+ s++;
+ }
+ }
+ }
+ return pruned;
+ }
+ /**
+ * remove any hiddenColumns or selected columns and shift remaining
+ * based on a series of position, range deletions.
+ * @param deletions
+ */
+ public void pruneDeletions(ShiftList deletions) {
+ if (deletions!=null) {
+ Vector shifts=deletions.shifts;
+ if (shifts!=null && shifts.size()>0) {
+ // delete any intervals intersecting.
+ if (hiddenColumns!=null) {
+ pruneIntervalVector(shifts, hiddenColumns);
+ if (hiddenColumns!=null && hiddenColumns.size()==0) {
+ hiddenColumns=null;
+ }
+ }
+ if (selected!=null && selected.size()>0) {
+ pruneColumnList(shifts, selected);
+ if (selected!=null && selected.size()==0)
+ selected=null;
+ }
+ // and shift the rest.
+ this.compensateForEdits(deletions);
+ }
+ }
+ }
+ /**
+ * This Method is used to return all the HiddenColumn regions
+ * less than the given index.
+ * @param end int
+ * @return Vector
+ */
+ public Vector getHiddenColumns()
+ {
+ return hiddenColumns;
+ }
+ /**
+ * Return absolute column index for a visible column index
+ * @param column int column index in alignment view
+ * @return alignment column index for column
+ */
+ public int adjustForHiddenColumns(int column)
+ {
+ int result = column;
+ if (hiddenColumns != null)
+ {
+ for (int i = 0; i < hiddenColumns.size(); i++)
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(i);
+ if (result >= region[0])
+ {
+ result += region[1] - region[0] + 1;
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Use this method to find out where a visible column is in the alignment
+ * when hidden columns exist
+ * @param hiddenColumn int
+ * @return int
+ */
+ public int findColumnPosition(int hiddenColumn)
+ {
+ int result = hiddenColumn;
+ if (hiddenColumns != null)
+ {
+ int index = 0;
+ int gaps = 0;
+ do
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(index);
+ if (hiddenColumn > region[1])
+ {
+ result -= region[1]+1-region[0];
+ }
+ index++;
+ }
+ while (index < hiddenColumns.size());
+
+ result -= gaps;
+ }
+
+ return result;
+ }
+
+ /**
+ * Use this method to determine where the next hiddenRegion starts
+ */
+ public int findHiddenRegionPosition(int hiddenRegion)
+ {
+ int result = 0;
+ if (hiddenColumns != null)
+ {
+ int index = 0;
+ int gaps = 0;
+ do
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(index);
+ if(hiddenRegion==0)
+ {
+ return region[0];
+ }
+
+ gaps += region[1] +1 - region[0];
+ result = region[1] +1;
+ index++;
+ }
+ while(index < hiddenRegion+1);
+
+ result -= gaps;
+ }
+
+ return result;
+ }
+
+ /**
+ * THis method returns the rightmost limit of a
+ * region of an alignment with hidden columns.
+ * In otherwords, the next hidden column.
+ * @param index int
+ */
+ public int getHiddenBoundaryRight(int alPos)
+ {
+ if (hiddenColumns != null)
+ {
+ int index = 0;
+ do
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(index);
+ if(alPos < region[0])
+ return region[0];
+
+ index++;
+ }
+ while(index < hiddenColumns.size());
+ }
+
+ return alPos;
+
+ }
+ /**
+ * THis method returns the rightmost limit of a
+ * region of an alignment with hidden columns.
+ * In otherwords, the next hidden column.
+ * @param index int
+ */
+ public int getHiddenBoundaryLeft(int alPos)
+ {
+ if (hiddenColumns != null)
+ {
+ int index = hiddenColumns.size()-1;
+ do
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(index);
+ if(alPos > region[1])
+ return region[1];
+
+ index--;
+ }
+ while(index >-1);
+ }
+
+ return alPos;
+
+ }
+
+ public void hideSelectedColumns()
+ {
+ while (size() > 0)
+ {
+ int column = ( (Integer) getSelected().firstElement()).intValue();
+ hideColumns(column);
+ }
+
+ }
+
+ public void hideColumns(int start, int end)
+ {
+ if(hiddenColumns==null)
+ hiddenColumns = new Vector();
+
+ boolean added = false;
+ boolean overlap = false;
+
+ for (int i = 0; i < hiddenColumns.size(); i++)
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(i);
+ if ( start<=region[1] && end>=region[0])
+ {
+ hiddenColumns.removeElementAt(i);
+ overlap = true;
+ break;
+ }
+ else if (end < region[0] && start < region[0])
+ {
+ hiddenColumns.insertElementAt(new int[]
+ {start, end}, i);
+ added = true;
+ break;
+ }
+ }
+
+ if(overlap)
+ {
+ hideColumns(start, end);
+ }
+ else if (!added)
+ hiddenColumns.addElement(new int[] {start, end});
+
+ }
+
+ /**
+ * This method will find a range of selected columns
+ * around the column specified
+ * @param res int
+ */
+ public void hideColumns(int col)
+ {
+ // First find out range of columns to hide
+ int min = col, max = col+1;
+ while( contains(min) )
+ { removeElement(min); min --; }
+
+ while( contains(max) )
+ { removeElement(max); max ++; }
+
+ min++; max--;
+
+ hideColumns(min, max);
+ }
+
+ public void revealAllHiddenColumns()
+ {
+ if(hiddenColumns!=null)
+ {
+ for (int i = 0; i < hiddenColumns.size(); i++)
+ {
+ int[] region = (int[]) hiddenColumns.elementAt(i);
+ for (int j = region[0]; j < region[1]+1; j++)
+ {
+ addElement(j);
+ }
+ }
+ }
+
+ hiddenColumns = null;
+ }
+
+ public void revealHiddenColumns(int res)
+ {
+ for(int i=0; i<hiddenColumns.size(); i++)
+ {
+ int [] region = (int[])hiddenColumns.elementAt(i);
+ if( res == region[0])
+ {
+ for (int j = region[0]; j < region[1]+1; j++)
+ {
+ addElement(j);
+ }
+
+ hiddenColumns.removeElement(region);
+ break;
+ }
+ }
+ if(hiddenColumns.size()==0)
+ hiddenColumns = null;
+ }
+
+ public boolean isVisible(int column)
+ {
+ for(int i=0; i<hiddenColumns.size(); i++)
+ {
+ int [] region = (int[])hiddenColumns.elementAt(i);
+ if( column >= region[0] && column <= region[1])
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+ /**
+ * Copy constructor
+ * @param copy
+ */
+ public ColumnSelection(ColumnSelection copy) {
+ if (copy!=null) {
+ if (copy.selected!=null) {
+ selected = new Vector();
+ for (int i=0,j=copy.selected.size(); i<j; i++) {
+ selected.set(i, ((Integer) copy.selected.get(i)));
+ }
+ }
+ if (copy.hiddenColumns!=null) {
+ hiddenColumns=new Vector();
+ for (int i=0,j=copy.hiddenColumns.size(); i<j; i++) {
+ int[] rh,cp;
+ rh = (int[])copy.hiddenColumns.get(i);
+ if (rh!=null) {
+ cp = new int[rh.length];
+ System.arraycopy(rh, 0, cp, 0, rh.length);
+ hiddenColumns.set(i, cp);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * ColumnSelection
+ */
+ public ColumnSelection()
+ {
+ }
+}
-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
- */\r
-package jalview.util;\r
-\r
-import jalview.datamodel.SequenceI;\r
-\r
-import java.util.*;\r
-\r
-/**\r
- * ShiftList\r
- * Simple way of mapping a linear series to a new linear range with new points introduced.\r
- * Use at your own risk!\r
- * <p>Title: ShiftList</p>\r
- *\r
- * <p>Description: </p>\r
- *\r
- * <p>Copyright: Copyright (c) 2004</p>\r
- *\r
- * <p>Company: Dundee University</p>\r
- *\r
- * @author not attributable\r
- * @version 1.0\r
- */\r
-public class ShiftList\r
-{\r
- public Vector shifts;\r
- public ShiftList()\r
- {\r
- shifts = new Vector();\r
- }\r
-\r
- /**\r
- * addShift\r
- * @param pos start position for shift (in original reference frame)\r
- * @param shift length of shift\r
- */\r
- public void addShift(int pos, int shift)\r
- {\r
- int sidx = 0;\r
- int[] rshift=null;\r
- while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)\r
- sidx++;\r
- if (sidx==shifts.size())\r
- shifts.insertElementAt(new int[] { pos, shift}, sidx);\r
- else\r
- rshift[1]+=shift;\r
- }\r
-\r
- /**\r
- * shift\r
- *\r
- * @param pos int\r
- * @return int shifted position\r
- */\r
- public int shift(int pos)\r
- {\r
- if (shifts.size()==0)\r
- return pos;\r
- int shifted=pos;\r
- int sidx=0;\r
- int rshift[];\r
- while (sidx<shifts.size()\r
- &&\r
- (rshift=((int[]) shifts.elementAt(sidx++)))[0]<=pos) {\r
- shifted += rshift[1];\r
- }\r
- return shifted;\r
- }\r
-\r
- /**\r
- * clear all shifts\r
- */\r
- public void clear()\r
- {\r
- shifts.removeAllElements();\r
- }\r
- /**\r
- * invert the shifts\r
- * @return ShiftList with inverse shift operations\r
- */\r
- public ShiftList getInverse() {\r
- ShiftList inverse=new ShiftList();\r
- if (shifts!=null) {\r
- for (int i=0,j=shifts.size(); i<j; i++) {\r
- int[] sh=(int[]) shifts.elementAt(i);\r
- if (sh!=null)\r
- inverse.shifts.addElement(new int[] {sh[0], -sh[1]});\r
- }\r
- }\r
- return inverse;\r
- }\r
-\r
- /**\r
- * parse a 1d map of position 1<i<n to L<pos[i]<N\r
- * such as that returned from SequenceI.gapMap()\r
- * @param gapMap\r
- * @return\r
- */\r
- public static ShiftList parseMap(int[] gapMap) {\r
- ShiftList shiftList = null;\r
- if (gapMap!=null && gapMap.length>0) {\r
- shiftList=new ShiftList();\r
-\r
- for (int i=0,p=0; i<gapMap.length; p++,i++) {\r
- if (p!=gapMap[i]) {\r
- shiftList.addShift(p, gapMap[i]-p);\r
- p=gapMap[i];\r
- }\r
- }\r
- }\r
- return shiftList;\r
- }\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer
+ * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+package jalview.util;
+
+import jalview.datamodel.SequenceI;
+
+import java.util.*;
+
+/**
+ * ShiftList
+ * Simple way of mapping a linear series to a new linear range with new points introduced.
+ * Use at your own risk!
+ * Now growing to be used for interval ranges (position, offset) storing deletions/insertions
+ */
+public class ShiftList
+{
+ public Vector shifts;
+ public ShiftList()
+ {
+ shifts = new Vector();
+ }
+
+ /**
+ * addShift
+ * @param pos start position for shift (in original reference frame)
+ * @param shift length of shift
+ */
+ public void addShift(int pos, int shift)
+ {
+ int sidx = 0;
+ int[] rshift=null;
+ while (sidx<shifts.size() && (rshift=(int[]) shifts.elementAt(sidx))[0]<pos)
+ sidx++;
+ if (sidx==shifts.size())
+ shifts.insertElementAt(new int[] { pos, shift}, sidx);
+ else
+ rshift[1]+=shift;
+ }
+
+ /**
+ * shift
+ *
+ * @param pos int
+ * @return int shifted position
+ */
+ public int shift(int pos)
+ {
+ if (shifts.size()==0)
+ return pos;
+ int shifted=pos;
+ int sidx=0;
+ int rshift[];
+ while (sidx<shifts.size()
+ &&
+ (rshift=((int[]) shifts.elementAt(sidx++)))[0]<=pos) {
+ shifted += rshift[1];
+ }
+ return shifted;
+ }
+
+ /**
+ * clear all shifts
+ */
+ public void clear()
+ {
+ shifts.removeAllElements();
+ }
+ /**
+ * invert the shifts
+ * @return ShiftList with inverse shift operations
+ */
+ public ShiftList getInverse() {
+ ShiftList inverse=new ShiftList();
+ if (shifts!=null) {
+ for (int i=0,j=shifts.size(); i<j; i++) {
+ int[] sh=(int[]) shifts.get(i);
+ if (sh!=null)
+ inverse.shifts.add(new int[] {sh[0], -sh[1]});
+ }
+ }
+ return inverse;
+ }
+
+ /**
+ * parse a 1d map of position 1<i<n to L<pos[i]<N
+ * such as that returned from SequenceI.gapMap()
+ * @param gapMap
+ * @return shifts from map index to mapped position
+ */
+ public static ShiftList parseMap(int[] gapMap) {
+ ShiftList shiftList = null;
+ if (gapMap!=null && gapMap.length>0) {
+ shiftList=new ShiftList();
+ for (int i=0,p=0; i<gapMap.length; p++,i++) {
+ if (p!=gapMap[i]) {
+ shiftList.addShift(p, gapMap[i]-p);
+ p=gapMap[i];
+ }
+ }
+ }
+ return shiftList;
+ }
+}