-/*
- * 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.elementAt(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.elementAt(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.setElementAt( ((Integer) copy.selected.elementAt(i)), 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.elementAt(i);
- if (rh!=null) {
- cp = new int[rh.length];
- System.arraycopy(rh, 0, cp, 0, rh.length);
- hiddenColumns.setElementAt(cp, i);
- }
- }
- }
- }
- }
- /**
- * ColumnSelection
- */
- public ColumnSelection()
- {
- }
-
- public String[] getVisibleSequenceStrings(int start, int end, SequenceI[] seqs) {
- int i,iSize=seqs.length;
- String selection[] = new String[iSize];
- if (hiddenColumns!=null && hiddenColumns.size()>0)
- {
- for (i=0; i<iSize; i++) {
- StringBuffer visibleSeq = new StringBuffer();
- Vector regions = getHiddenColumns();
-
- int blockStart = start, blockEnd=end;
- int [] region;
- int hideStart, hideEnd;
-
- for (int j = 0; j < regions.size(); j++)
- {
- region = (int[]) regions.elementAt(j);
- hideStart = region[0];
- hideEnd = region[1];
-
- if(hideStart < start)
- {
- continue;
- }
-
- blockStart = Math.min(blockStart, hideEnd+1);
- blockEnd = Math.min(blockEnd, hideStart);
-
- if(blockStart>blockEnd)
- {
- break;
- }
-
-
- visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
-
- blockStart = hideEnd+1;
- blockEnd = end;
- }
-
- if(end>blockStart)
- visibleSeq.append(seqs[i].getSequence(blockStart, end));
-
- selection[i] = visibleSeq.toString();
- }
- }
- else
- {
- for(i=0; i<iSize; i++)
- {
- selection[i] = seqs[i].getSequence(start, end);
- }
- }
-
- return selection;
- }
- /**
- * return all visible segments between the given start and end boundaries
- *
- * @param start (first column inclusive from 0)
- * @param end (last column - not inclusive)
- * @return int[] {i_start, i_end, ..} where intervals lie in start<=i_start<=i_end<end
- */
- public int[] getVisibleContigs(int start, int end) {
- if (hiddenColumns!=null && hiddenColumns.size()>0)
- {
- Vector visiblecontigs=new Vector();
- Vector regions = getHiddenColumns();
-
- int vstart = start;
- int [] region;
- int hideStart, hideEnd;
-
- for (int j = 0; vstart<end && j < regions.size(); j++)
- {
- region = (int[]) regions.elementAt(j);
- hideStart = region[0];
- hideEnd = region[1];
-
- if(hideEnd < vstart)
- {
- continue;
- }
- if (hideStart>vstart) {
- visiblecontigs.addElement(new int[] {vstart, hideStart-1});
- }
- vstart=hideEnd+1;
- }
-
- if(vstart<end)
- visiblecontigs.addElement(new int[] { vstart, end-1});
- int[] vcontigs = new int[visiblecontigs.size()*2];
- for (int i=0,j=visiblecontigs.size(); i<j; i++) {
- int [] vc = (int[]) visiblecontigs.get(i);
- visiblecontigs.setElementAt(null, i);
- vcontigs[i*2] = vc[0];
- vcontigs[i*2+1] = vc[1];
- }
- visiblecontigs.clear();
- return vcontigs;
- }
- else
- {
- return new int[] { start, end-1 };
- }
- }
-}
+/*\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 left shift in edit (+ve for removal, or -ve for inserts)\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
+ /**\r
+ * propagate shift in alignment columns to column selection\r
+ * special version of compensateForEdit - allowing for edits within hidden regions\r
+ * @param start beginning of edit\r
+ * @param left shift in edit (+ve for removal, or -ve for inserts)\r
+ */\r
+ private void compensateForDelEdits(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
+ }\r
+ if (region[1]>= start) {\r
+ region[1] -=change;\r
+ }\r
+ if (region[1]<region[0]) {\r
+ hiddenColumns.removeElementAt(i--);\r
+ }\r
+\r
+ if(region[0]<0)\r
+ region[0] = 0;\r
+ if(region[1] <0)\r
+ region[1] = 0;\r
+ }\r
+ }\r
+ }\r
+ /**\r
+ * Adjust hidden column boundaries based on a series of column\r
+ * additions or deletions in visible regions.\r
+ * @param shiftrecord\r
+ * @return\r
+ */\r
+ public ShiftList compensateForEdits(ShiftList shiftrecord) {\r
+ if (shiftrecord!=null) {\r
+ Vector shifts = shiftrecord.shifts;\r
+ if (shifts!=null && shifts.size()>0) {\r
+ int shifted=0;\r
+ for (int i=0,j=shifts.size(); i<j; i++) {\r
+ int[] sh = (int[]) shifts.elementAt(i);\r
+ //compensateForEdit(shifted+sh[0], sh[1]);\r
+ compensateForDelEdits(shifted+sh[0], sh[1]);\r
+ shifted-=sh[1];\r
+ }\r
+ }\r
+ return shiftrecord.getInverse();\r
+ }\r
+ return null;\r
+ }\r
+ /**\r
+ * removes intersection of position,length ranges in deletions\r
+ * from the start,end regions marked in intervals.\r
+ * @param deletions\r
+ * @param intervals\r
+ * @return\r
+ */\r
+ private boolean pruneIntervalVector(Vector deletions, Vector intervals) {\r
+ boolean pruned=false;\r
+ int i=0,j=intervals.size()-1, s=0, t=deletions.size()-1;\r
+ int hr[]=(int[]) intervals.elementAt(i);\r
+ int sr[]=(int[]) deletions.elementAt(s);\r
+ while (i<=j && s<=t) {\r
+ boolean trailinghn=hr[1]>=sr[0];\r
+ if (!trailinghn) {\r
+ if (i<j)\r
+ hr=(int[]) intervals.elementAt(++i);\r
+ else\r
+ i++;\r
+ continue;\r
+ }\r
+ int endshift=sr[0]+sr[1]; // deletion ranges - -ve means an insert\r
+ if (endshift<hr[0] || endshift<sr[0]) { // leadinghc disjoint or not a deletion\r
+ if (s<t)\r
+ sr=(int[]) deletions.elementAt(++s);\r
+ else\r
+ s++;\r
+ continue;\r
+ }\r
+ boolean leadinghn=hr[0]>=sr[0];\r
+ boolean leadinghc=hr[0]<endshift;\r
+ boolean trailinghc=hr[1]<endshift;\r
+ if (leadinghn) {\r
+ if (trailinghc) {// deleted hidden region.\r
+ intervals.removeElementAt(i);\r
+ pruned=true;\r
+ j--;\r
+ if (i<=j)\r
+ hr=(int[]) intervals.elementAt(i);\r
+ continue;\r
+ }\r
+ if (leadinghc) {\r
+ hr[0]=endshift; // clip c terminal region\r
+ leadinghn=!leadinghn;\r
+ pruned=true;\r
+ }\r
+ }\r
+ if (!leadinghn) {\r
+ if (trailinghc) {\r
+ if (trailinghn) {\r
+ hr[1]=sr[0]-1;\r
+ pruned=true;\r
+ }\r
+ } else {\r
+ // sr contained in hr\r
+ if (s<t)\r
+ sr=(int[]) deletions.elementAt(++s);\r
+ else\r
+ s++;\r
+ continue;\r
+ }\r
+ }\r
+ }\r
+ return pruned; // true if any interval was removed or modified by operations.\r
+ }\r
+ private boolean pruneColumnList(Vector deletion, Vector list) {\r
+ int s=0,t=deletion.size();\r
+ int[] sr=(int[])list.elementAt(s++);\r
+ boolean pruned=false;\r
+ int i=0, j=list.size();\r
+ while (i<j && s<=t) {\r
+ int c=((Integer)list.elementAt(i++)).intValue();\r
+ if (sr[0]<=c) {\r
+ if (sr[1]+sr[0]>=c) { // sr[1] -ve means inseriton.\r
+ list.removeElementAt(--i);\r
+ j--;\r
+ } else {\r
+ if (s<t)\r
+ sr = (int[])deletion.elementAt(s);\r
+ s++;\r
+ }\r
+ }\r
+ }\r
+ return pruned;\r
+ }\r
+ /**\r
+ * remove any hiddenColumns or selected columns and shift remaining\r
+ * based on a series of position, range deletions.\r
+ * @param deletions\r
+ */\r
+ public void pruneDeletions(ShiftList deletions) {\r
+ if (deletions!=null) {\r
+ Vector shifts=deletions.shifts;\r
+ if (shifts!=null && shifts.size()>0) {\r
+ // delete any intervals intersecting.\r
+ if (hiddenColumns!=null) {\r
+ pruneIntervalVector(shifts, hiddenColumns);\r
+ if (hiddenColumns!=null && hiddenColumns.size()==0) {\r
+ hiddenColumns=null;\r
+ }\r
+ }\r
+ if (selected!=null && selected.size()>0) {\r
+ pruneColumnList(shifts, selected);\r
+ if (selected!=null && selected.size()==0)\r
+ selected=null;\r
+ }\r
+ // and shift the rest.\r
+ this.compensateForEdits(deletions);\r
+ }\r
+ }\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
+ * ColumnSelection\r
+ */\r
+ public ColumnSelection()\r
+ {\r
+ }\r
+\r
+ public String[] getVisibleSequenceStrings(int start, int end, SequenceI[] seqs) {\r
+ int i,iSize=seqs.length;\r
+ String selection[] = new String[iSize];\r
+ if (hiddenColumns!=null && hiddenColumns.size()>0)\r
+ {\r
+ for (i=0; i<iSize; i++) {\r
+ StringBuffer visibleSeq = new StringBuffer();\r
+ Vector regions = getHiddenColumns();\r
+\r
+ int blockStart = start, blockEnd=end;\r
+ int [] region;\r
+ int hideStart, hideEnd;\r
+\r
+ for (int j = 0; j < regions.size(); j++)\r
+ {\r
+ region = (int[]) regions.elementAt(j);\r
+ hideStart = region[0];\r
+ hideEnd = region[1];\r
+\r
+ if(hideStart < start)\r
+ {\r
+ continue;\r
+ }\r
+\r
+ blockStart = Math.min(blockStart, hideEnd+1);\r
+ blockEnd = Math.min(blockEnd, hideStart);\r
+\r
+ if(blockStart>blockEnd)\r
+ {\r
+ break;\r
+ }\r
+\r
+\r
+ visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));\r
+\r
+ blockStart = hideEnd+1;\r
+ blockEnd = end;\r
+ }\r
+\r
+ if(end>blockStart)\r
+ visibleSeq.append(seqs[i].getSequence(blockStart, end));\r
+\r
+ selection[i] = visibleSeq.toString();\r
+ }\r
+ }\r
+ else\r
+ {\r
+ for(i=0; i<iSize; i++)\r
+ {\r
+ selection[i] = seqs[i].getSequence(start, end);\r
+ }\r
+ }\r
+\r
+ return selection;\r
+ }\r
+ /**\r
+ * return all visible segments between the given start and end boundaries\r
+ *\r
+ * @param start (first column inclusive from 0)\r
+ * @param end (last column - not inclusive)\r
+ * @return int[] {i_start, i_end, ..} where intervals lie in start<=i_start<=i_end<end\r
+ */\r
+ public int[] getVisibleContigs(int start, int end) {\r
+ if (hiddenColumns!=null && hiddenColumns.size()>0)\r
+ {\r
+ Vector visiblecontigs=new Vector();\r
+ Vector regions = getHiddenColumns();\r
+\r
+ int vstart = start;\r
+ int [] region;\r
+ int hideStart, hideEnd;\r
+\r
+ for (int j = 0; vstart<end && j < regions.size(); j++)\r
+ {\r
+ region = (int[]) regions.elementAt(j);\r
+ hideStart = region[0];\r
+ hideEnd = region[1];\r
+\r
+ if(hideEnd < vstart)\r
+ {\r
+ continue;\r
+ }\r
+ if (hideStart>vstart) {\r
+ visiblecontigs.addElement(new int[] {vstart, hideStart-1});\r
+ }\r
+ vstart=hideEnd+1;\r
+ }\r
+\r
+ if(vstart<end)\r
+ visiblecontigs.addElement(new int[] { vstart, end-1});\r
+ int[] vcontigs = new int[visiblecontigs.size()*2];\r
+ for (int i=0,j=visiblecontigs.size(); i<j; i++) {\r
+ int [] vc = (int[]) visiblecontigs.elementAt(i);\r
+ visiblecontigs.setElementAt(null, i);\r
+ vcontigs[i*2] = vc[0];\r
+ vcontigs[i*2+1] = vc[1];\r
+ }\r
+ visiblecontigs.removeAllElements();\r
+ return vcontigs;\r
+ }\r
+ else\r
+ {\r
+ return new int[] { start, end-1 };\r
+ }\r
+ }\r
+}\r