X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FColumnSelection.java;h=624e083eabb2d969f8a1211e120c1f09b302a779;hb=a97249105994383dca7ea64259b17ba8ddce31be;hp=2d2223111c2bb0c75b1aa104ab76ba9e98059ebf;hpb=d545553b53ac1bd1e29eb72250cdb29745f7292f;p=jalview.git diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index 2d22231..624e083 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -1,728 +1,1607 @@ /* - * 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 + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview 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 3 * 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. - * + * + * Jalview 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 + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.datamodel; +import jalview.util.Comparison; import jalview.util.ShiftList; +import jalview.viewmodel.annotationfilter.AnnotationFilterParameter; +import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField; -import java.util.*; +import java.util.ArrayList; +import java.util.BitSet; +import java.util.Collections; +import java.util.List; +import java.util.Vector; /** * NOTE: Columns are zero based. */ public class ColumnSelection { - Vector selected = new Vector(); + private class IntList + { + /* + * list of selected columns (ordered by selection order, not column order) + */ + private List order = new ArrayList(); - //Vector of int [] {startCol, endCol} - Vector hiddenColumns; + /** + * bitfield for column selection - allows quick lookup + */ + private BitSet selected = new BitSet(); /** - * Add a column to the selection - * - * @param col index of column + * adds a new column i to the selection - only if i is not already selected + * + * @param i */ - public void addElement(int col) + public void add(int i) { - Integer column = new Integer(col); - if (!selected.contains(column)) - { - selected.addElement(column); - } + if (!selected.get(i)) + { + order.add(Integer.valueOf(i)); + selected.set(i); + } } - /** - * clears column selection - */ public void clear() { - selected.removeAllElements(); + order.clear(); + selected.clear(); } - /** - * removes col from selection - * - * @param col index of column to be removed - */ - public void removeElement(int col) + public void remove(int col) { - Integer colInt = new Integer(col); - if (selected.contains(colInt)) - { - selected.removeElement(colInt); - } - } + Integer colInt = new Integer(col); - /** - * 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 getList() { - return ((Integer) selected.elementAt(i)).intValue(); + return order; } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ public int size() { - return selected.size(); + return order.size(); } /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * gets the column that was selected first, second or i'th + * + * @param i + * @return */ - public int getMax() + public int elementAt(int i) { - int max = -1; + return order.get(i); + } - for (int i = 0; i < selected.size(); i++) + protected boolean pruneColumnList(final List shifts) + { + int s = 0, t = shifts.size(); + int[] sr = shifts.get(s++); + boolean pruned = false; + int i = 0, j = order.size(); + while (i < j && s <= t) + { + int c = order.get(i++).intValue(); + if (sr[0] <= c) { - if (columnAt(i) > max) + if (sr[1] + sr[0] >= c) + { // sr[1] -ve means inseriton. + order.remove(--i); + selected.clear(c); + j--; + } + else + { + if (s < t) { - max = columnAt(i); + sr = shifts.get(s); } + s++; + } } - - return max; + } + return pruned; } /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * shift every selected column at or above start by change + * + * @param start + * - leftmost column to be shifted + * @param change + * - delta for shift */ - public int getMin() + public void compensateForEdits(int start, int change) { - int min = 1000000000; + BitSet mask = new BitSet(); + for (int i = 0; i < order.size(); i++) + { + int temp = order.get(i); - for (int i = 0; i < selected.size(); i++) + if (temp >= start) { - if (columnAt(i) < min) - { - min = columnAt(i); - } + // clear shifted bits and update List of selected columns + selected.clear(temp); + mask.set(temp - change); + order.set(i, new Integer(temp - change)); } + } + // lastly update the bitfield all at once + selected.or(mask); + } - return min; + public boolean isSelected(int column) + { + return selected.get(column); + } + + public int getMaxColumn() + { + return selected.length() - 1; } + public int getMinColumn() + { + return selected.get(0) ? 0 : selected.nextSetBit(0); + } /** - * 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) + * @return a series of selection intervals along the range */ - public void compensateForEdit(int start, int change) + public List getRanges() + { + List rlist = new ArrayList(); + if (selected.isEmpty()) + { + return rlist; + } + int next = selected.nextSetBit(0), clear = -1; + while (next != -1) + { + clear = selected.nextClearBit(next); + rlist.add(new int[] { next, clear - 1 }); + next = selected.nextSetBit(clear); + } + return rlist; + } + } + + IntList selected = new IntList(); + + /* + * list of hidden column [start, end] ranges; the list is maintained in + * ascending start column order + */ + Vector hiddenColumns; + + /** + * Add a column to the selection + * + * @param col + * index of column + */ + public void addElement(int col) + { + selected.add(col); + } + + /** + * clears column selection + */ + public void clear() + { + selected.clear(); + } + + /** + * Removes value 'col' from the selection (not the col'th item) + * + * @param col + * index of column to be removed + */ + public void removeElement(int col) + { + selected.remove(col); + } + + /** + * 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.remove(colInt); + } + } + } + + /** + * Returns a list of selected columns. The list contains no duplicates but is + * not necessarily ordered. It also may include columns hidden from the + * current view + */ + public List getSelected() + { + return selected.getList(); + } + + /** + * @return list of int arrays containing start and end column position for + * runs of selected columns ordered from right to left. + */ + public List getSelectedRanges() + { + return selected.getRanges(); + } + + /** + * + * @param col + * index to search for in column selection + * + * @return true if col is selected + */ + public boolean contains(int col) + { + return (col > -1) ? selected.isSelected(col) : false; + } + + /** + * Answers true if no columns are selected, else false + */ + public boolean isEmpty() + { + return selected == null || selected.isEmpty(); + } + + /** + * rightmost selected column + * + * @return rightmost column in alignment that is selected + */ + public int getMax() + { + if (selected.isEmpty()) { - for (int i = 0; i < size(); i++) + return -1; + } + return selected.getMaxColumn(); + } + + /** + * Leftmost column in selection + * + * @return column index of leftmost column in selection + */ + public int getMin() + { + if (selected.isEmpty()) + { + return 1000000000; + } + return selected.getMinColumn(); + } + + /** + * 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 List compensateForEdit(int start, int change) + { + List deletedHiddenColumns = null; + selected.compensateForEdits(start, change); + + if (hiddenColumns != null) + { + deletedHiddenColumns = new ArrayList(); + int hSize = hiddenColumns.size(); + for (int i = 0; i < hSize; i++) + { + int[] region = hiddenColumns.elementAt(i); + if (region[0] > start && start + change > region[1]) { - int temp = columnAt(i); + deletedHiddenColumns.add(region); - if (temp >= start) - { - selected.setElementAt(new Integer(temp - change), i); - } + hiddenColumns.removeElementAt(i); + i--; + hSize--; + continue; } - if(hiddenColumns!=null) + if (region[0] > start) { - for(int i=0; i start) - { - region[0] -= change; - region[1] -= change; - } - if(region[0]<0) - region[0] = 0; - if(region[1] <0) - region[1] = 0; - } + region[0] -= change; + region[1] -= change; } + + if (region[0] < 0) + { + region[0] = 0; + } + + } + + this.revealHiddenColumns(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) + + return deletedHiddenColumns; + } + + /** + * 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) + { + + selected.compensateForEdits(start, change); + + if (hiddenColumns != null) { - for (int i = 0; i < size(); i++) + for (int i = 0; i < hiddenColumns.size(); i++) + { + int[] region = hiddenColumns.elementAt(i); + if (region[0] >= start) { - int temp = columnAt(i); - - if (temp >= start) - { - selected.setElementAt(new Integer(temp - change), i); - } + region[0] -= change; } - - if(hiddenColumns!=null) + if (region[1] >= start) { - for(int i=0; i= start) - { - region[0] -= change; - } - if (region[1]>= start) { - region[1] -=change; - } - if (region[1]