X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FColumnSelection.java;h=d0fc9277f5c5bbda923b5b7f0acc4d695c5f11de;hb=797df64fa2a0a30773d0f48f5494d4155e5a8be3;hp=c7e841885f22146c7a0933255a75ebfe5d58d79f;hpb=6fe4dc1cb082ea9df1c733d05e74ee1a187816cf;p=jalview.git diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index c7e8418..d0fc927 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -1,671 +1,1245 @@ /* - * 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 + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7) + * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle + * + * 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. + * + * 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 Jalview. If not, see . */ package jalview.datamodel; -import jalview.util.ShiftList; - import java.util.*; +import jalview.util.*; + /** * NOTE: Columns are zero based. */ public class ColumnSelection { - Vector selected = new Vector(); + Vector selected = new Vector(); - //Vector of int [] {startCol, endCol} - Vector hiddenColumns; + // Vector of int [] {startCol, endCol} + Vector hiddenColumns; - /** - * Add a column to the selection - * - * @param col index of column - */ - public void addElement(int col) + /** + * 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)) { - Integer column = new Integer(col); - if (!selected.contains(column)) - { - selected.addElement(column); - } + selected.addElement(column); } + } - /** - * clears column selection - */ - public void clear() - { - selected.removeAllElements(); - } + /** + * 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); + /** + * 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); - } + 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) + /** + * 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++) { - Integer colInt; - for(int i=start; i max) + { + max = columnAt(i); + } } - /** - * DOCUMENT ME! - * - * @param i DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public int columnAt(int i) + return max; + } + + /** + * Leftmost column in selection + * + * @return column index of leftmost column in selection + */ + public int getMin() + { + int min = 1000000000; + + for (int i = 0; i < selected.size(); i++) { - return ((Integer) selected.elementAt(i)).intValue(); + if (columnAt(i) < min) + { + min = columnAt(i); + } } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public int size() + 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 Vector compensateForEdit(int start, int change) + { + Vector deletedHiddenColumns = null; + for (int i = 0; i < size(); i++) { - return selected.size(); + int temp = columnAt(i); + + if (temp >= start) + { + selected.setElementAt(new Integer(temp - change), i); + } } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public int getMax() + if (hiddenColumns != null) { - int max = -1; - - for (int i = 0; i < selected.size(); i++) + deletedHiddenColumns = new Vector(); + int hSize = hiddenColumns.size(); + for (int i = 0; i < hSize; i++) + { + int[] region = (int[]) hiddenColumns.elementAt(i); + if (region[0] > start && start + change > region[1]) { - if (columnAt(i) > max) - { - max = columnAt(i); - } - } + deletedHiddenColumns.addElement(hiddenColumns.elementAt(i)); - return max; - } + hiddenColumns.removeElementAt(i); + i--; + hSize--; + continue; + } - /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - public int getMin() - { - int min = 1000000000; + if (region[0] > start) + { + region[0] -= change; + region[1] -= change; + } - for (int i = 0; i < selected.size(); i++) + if (region[0] < 0) { - if (columnAt(i) < min) - { - min = columnAt(i); - } + region[0] = 0; } - return min; + } + + this.revealHiddenColumns(0); } + return deletedHiddenColumns; + } - /** - * 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) + /** + * 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++) { - for (int i = 0; i < size(); i++) - { - int temp = columnAt(i); + int temp = columnAt(i); - if (temp >= start) - { - selected.setElementAt(new Integer(temp - change), 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(hiddenColumns!=null) + if (region[0] < 0) { - 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] = 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) + } + + /** + * 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) { - for (int i = 0; i < size(); i++) + Vector shifts = shiftrecord.shifts; + if (shifts != null && shifts.size() > 0) + { + int shifted = 0; + for (int i = 0, j = shifts.size(); i < j; i++) { - int temp = columnAt(i); - - if (temp >= start) - { - selected.setElementAt(new Integer(temp - change), 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; + } - if(hiddenColumns!=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) { - for(int i=0; i= start) - { - region[0] -= change; - } - if (region[1]>= start) { - region[1] -=change; - } - if (region[1]