2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
21 import jalview.util.ShiftList;
26 * NOTE: Columns are zero based.
28 public class ColumnSelection
30 Vector selected = new Vector();
32 //Vector of int [] {startCol, endCol}
36 * Add a column to the selection
38 * @param col index of column
40 public void addElement(int col)
42 Integer column = new Integer(col);
43 if (!selected.contains(column))
45 selected.addElement(column);
50 * clears column selection
54 selected.removeAllElements();
58 * removes col from selection
60 * @param col index of column to be removed
62 public void removeElement(int col)
64 Integer colInt = new Integer(col);
66 if (selected.contains(colInt))
68 selected.removeElement(colInt);
73 * removes a range of columns from the selection
74 * @param start int - first column in range to be removed
75 * @param end int - last col
77 public void removeElements(int start, int end)
80 for(int i=start; i<end; i++)
82 colInt = new Integer(i);
83 if (selected.contains(colInt))
85 selected.removeElement(colInt);
91 * @return Vector containing selected columns as Integers
93 public Vector getSelected()
100 * @param col index to search for in column selection
102 * @return true if Integer(col) is in selection.
104 public boolean contains(int col)
106 return selected.contains(new Integer(col));
112 * @param i DOCUMENT ME!
114 * @return DOCUMENT ME!
116 public int columnAt(int i)
118 return ((Integer) selected.elementAt(i)).intValue();
124 * @return DOCUMENT ME!
128 return selected.size();
134 * @return DOCUMENT ME!
140 for (int i = 0; i < selected.size(); i++)
142 if (columnAt(i) > max)
154 * @return DOCUMENT ME!
158 int min = 1000000000;
160 for (int i = 0; i < selected.size(); i++)
162 if (columnAt(i) < min)
173 * propagate shift in alignment columns to column selection
175 * @param start beginning of edit
176 * @param left shift in edit (+ve for removal, or -ve for inserts)
178 public void compensateForEdit(int start, int change)
180 for (int i = 0; i < size(); i++)
182 int temp = columnAt(i);
186 selected.setElementAt(new Integer(temp - change), i);
190 if(hiddenColumns!=null)
192 for(int i=0; i<hiddenColumns.size(); i++)
194 int[] region = (int[]) hiddenColumns.elementAt(i);
195 if(region[0] > start)
208 * propagate shift in alignment columns to column selection
209 * special version of compensateForEdit - allowing for edits within hidden regions
210 * @param start beginning of edit
211 * @param left shift in edit (+ve for removal, or -ve for inserts)
213 private void compensateForDelEdits(int start, int change)
215 for (int i = 0; i < size(); i++)
217 int temp = columnAt(i);
221 selected.setElementAt(new Integer(temp - change), i);
225 if(hiddenColumns!=null)
227 for(int i=0; i<hiddenColumns.size(); i++)
229 int[] region = (int[]) hiddenColumns.elementAt(i);
230 if(region[0] >= start)
234 if (region[1]>= start) {
237 if (region[1]<region[0]) {
238 hiddenColumns.removeElementAt(i--);
249 * Adjust hidden column boundaries based on a series of column
250 * additions or deletions in visible regions.
254 public ShiftList compensateForEdits(ShiftList shiftrecord) {
255 if (shiftrecord!=null) {
256 Vector shifts = shiftrecord.shifts;
257 if (shifts!=null && shifts.size()>0) {
259 for (int i=0,j=shifts.size(); i<j; i++) {
260 int[] sh = (int[]) shifts.elementAt(i);
261 //compensateForEdit(shifted+sh[0], sh[1]);
262 compensateForDelEdits(shifted+sh[0], sh[1]);
266 return shiftrecord.getInverse();
271 * removes intersection of position,length ranges in deletions
272 * from the start,end regions marked in intervals.
277 private boolean pruneIntervalVector(Vector deletions, Vector intervals) {
278 boolean pruned=false;
279 int i=0,j=intervals.size()-1, s=0, t=deletions.size()-1;
280 int hr[]=(int[]) intervals.elementAt(i);
281 int sr[]=(int[]) deletions.elementAt(s);
282 while (i<=j && s<=t) {
283 boolean trailinghn=hr[1]>=sr[0];
286 hr=(int[]) intervals.elementAt(++i);
291 int endshift=sr[0]+sr[1]; // deletion ranges - -ve means an insert
292 if (endshift<hr[0] || endshift<sr[0]) { // leadinghc disjoint or not a deletion
294 sr=(int[]) deletions.elementAt(++s);
299 boolean leadinghn=hr[0]>=sr[0];
300 boolean leadinghc=hr[0]<endshift;
301 boolean trailinghc=hr[1]<endshift;
303 if (trailinghc) {// deleted hidden region.
304 intervals.removeElementAt(i);
308 hr=(int[]) intervals.elementAt(i);
312 hr[0]=endshift; // clip c terminal region
313 leadinghn=!leadinghn;
324 // sr contained in hr
326 sr=(int[]) deletions.elementAt(++s);
333 return pruned; // true if any interval was removed or modified by operations.
335 private boolean pruneColumnList(Vector deletion, Vector list) {
336 int s=0,t=deletion.size();
337 int[] sr=(int[])list.elementAt(s++);
338 boolean pruned=false;
339 int i=0, j=list.size();
340 while (i<j && s<=t) {
341 int c=((Integer)list.elementAt(i++)).intValue();
343 if (sr[1]+sr[0]>=c) { // sr[1] -ve means inseriton.
344 list.removeElementAt(--i);
348 sr = (int[])deletion.elementAt(s);
356 * remove any hiddenColumns or selected columns and shift remaining
357 * based on a series of position, range deletions.
360 public void pruneDeletions(ShiftList deletions) {
361 if (deletions!=null) {
362 Vector shifts=deletions.shifts;
363 if (shifts!=null && shifts.size()>0) {
364 // delete any intervals intersecting.
365 if (hiddenColumns!=null) {
366 pruneIntervalVector(shifts, hiddenColumns);
367 if (hiddenColumns!=null && hiddenColumns.size()==0) {
371 if (selected!=null && selected.size()>0) {
372 pruneColumnList(shifts, selected);
373 if (selected!=null && selected.size()==0)
376 // and shift the rest.
377 this.compensateForEdits(deletions);
382 * This Method is used to return all the HiddenColumn regions
383 * less than the given index.
387 public Vector getHiddenColumns()
389 return hiddenColumns;
392 * Return absolute column index for a visible column index
393 * @param column int column index in alignment view
394 * @return alignment column index for column
396 public int adjustForHiddenColumns(int column)
399 if (hiddenColumns != null)
401 for (int i = 0; i < hiddenColumns.size(); i++)
403 int[] region = (int[]) hiddenColumns.elementAt(i);
404 if (result >= region[0])
406 result += region[1] - region[0] + 1;
414 * Use this method to find out where a visible column is in the alignment
415 * when hidden columns exist
416 * @param hiddenColumn int
419 public int findColumnPosition(int hiddenColumn)
421 int result = hiddenColumn;
422 if (hiddenColumns != null)
428 int[] region = (int[]) hiddenColumns.elementAt(index);
429 if (hiddenColumn > region[1])
431 result -= region[1]+1-region[0];
435 while (index < hiddenColumns.size());
444 * Use this method to determine where the next hiddenRegion starts
446 public int findHiddenRegionPosition(int hiddenRegion)
449 if (hiddenColumns != null)
455 int[] region = (int[]) hiddenColumns.elementAt(index);
461 gaps += region[1] +1 - region[0];
462 result = region[1] +1;
465 while(index < hiddenRegion+1);
474 * THis method returns the rightmost limit of a
475 * region of an alignment with hidden columns.
476 * In otherwords, the next hidden column.
479 public int getHiddenBoundaryRight(int alPos)
481 if (hiddenColumns != null)
486 int[] region = (int[]) hiddenColumns.elementAt(index);
487 if(alPos < region[0])
492 while(index < hiddenColumns.size());
499 * THis method returns the rightmost limit of a
500 * region of an alignment with hidden columns.
501 * In otherwords, the next hidden column.
504 public int getHiddenBoundaryLeft(int alPos)
506 if (hiddenColumns != null)
508 int index = hiddenColumns.size()-1;
511 int[] region = (int[]) hiddenColumns.elementAt(index);
512 if(alPos > region[1])
524 public void hideSelectedColumns()
528 int column = ( (Integer) getSelected().firstElement()).intValue();
534 public void hideColumns(int start, int end)
536 if(hiddenColumns==null)
537 hiddenColumns = new Vector();
539 boolean added = false;
540 boolean overlap = false;
542 for (int i = 0; i < hiddenColumns.size(); i++)
544 int[] region = (int[]) hiddenColumns.elementAt(i);
545 if ( start<=region[1] && end>=region[0])
547 hiddenColumns.removeElementAt(i);
551 else if (end < region[0] && start < region[0])
553 hiddenColumns.insertElementAt(new int[]
562 hideColumns(start, end);
565 hiddenColumns.addElement(new int[] {start, end});
570 * This method will find a range of selected columns
571 * around the column specified
574 public void hideColumns(int col)
576 // First find out range of columns to hide
577 int min = col, max = col+1;
578 while( contains(min) )
579 { removeElement(min); min --; }
581 while( contains(max) )
582 { removeElement(max); max ++; }
586 hideColumns(min, max);
589 public void revealAllHiddenColumns()
591 if(hiddenColumns!=null)
593 for (int i = 0; i < hiddenColumns.size(); i++)
595 int[] region = (int[]) hiddenColumns.elementAt(i);
596 for (int j = region[0]; j < region[1]+1; j++)
603 hiddenColumns = null;
606 public void revealHiddenColumns(int res)
608 for(int i=0; i<hiddenColumns.size(); i++)
610 int [] region = (int[])hiddenColumns.elementAt(i);
611 if( res == region[0])
613 for (int j = region[0]; j < region[1]+1; j++)
618 hiddenColumns.removeElement(region);
622 if(hiddenColumns.size()==0)
623 hiddenColumns = null;
626 public boolean isVisible(int column)
628 for(int i=0; i<hiddenColumns.size(); i++)
630 int [] region = (int[])hiddenColumns.elementAt(i);
631 if( column >= region[0] && column <= region[1])
642 public ColumnSelection(ColumnSelection copy) {
644 if (copy.selected!=null) {
645 selected = new Vector();
646 for (int i=0,j=copy.selected.size(); i<j; i++) {
647 selected.setElementAt( ((Integer) copy.selected.elementAt(i)), i);
650 if (copy.hiddenColumns!=null) {
651 hiddenColumns=new Vector();
652 for (int i=0,j=copy.hiddenColumns.size(); i<j; i++) {
654 rh = (int[])copy.hiddenColumns.elementAt(i);
656 cp = new int[rh.length];
657 System.arraycopy(rh, 0, cp, 0, rh.length);
658 hiddenColumns.setElementAt(cp, i);
667 public ColumnSelection()
671 public String[] getVisibleSequenceStrings(int start, int end, SequenceI[] seqs) {
672 int i,iSize=seqs.length;
673 String selection[] = new String[iSize];
674 if (hiddenColumns!=null && hiddenColumns.size()>0)
676 for (i=0; i<iSize; i++) {
677 StringBuffer visibleSeq = new StringBuffer();
678 Vector regions = getHiddenColumns();
680 int blockStart = start, blockEnd=end;
682 int hideStart, hideEnd;
684 for (int j = 0; j < regions.size(); j++)
686 region = (int[]) regions.elementAt(j);
687 hideStart = region[0];
690 if(hideStart < start)
695 blockStart = Math.min(blockStart, hideEnd+1);
696 blockEnd = Math.min(blockEnd, hideStart);
698 if(blockStart>blockEnd)
704 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
706 blockStart = hideEnd+1;
711 visibleSeq.append(seqs[i].getSequence(blockStart, end));
713 selection[i] = visibleSeq.toString();
718 for(i=0; i<iSize; i++)
720 selection[i] = seqs[i].getSequence(start, end);
727 * return all visible segments between the given start and end boundaries
729 * @param start (first column inclusive from 0)
730 * @param end (last column - not inclusive)
731 * @return int[] {i_start, i_end, ..} where intervals lie in start<=i_start<=i_end<end
733 public int[] getVisibleContigs(int start, int end) {
734 if (hiddenColumns!=null && hiddenColumns.size()>0)
736 Vector visiblecontigs=new Vector();
737 Vector regions = getHiddenColumns();
741 int hideStart, hideEnd;
743 for (int j = 0; vstart<end && j < regions.size(); j++)
745 region = (int[]) regions.elementAt(j);
746 hideStart = region[0];
753 if (hideStart>vstart) {
754 visiblecontigs.addElement(new int[] {vstart, hideStart-1});
760 visiblecontigs.addElement(new int[] { vstart, end-1});
761 int[] vcontigs = new int[visiblecontigs.size()*2];
762 for (int i=0,j=visiblecontigs.size(); i<j; i++) {
763 int [] vc = (int[]) visiblecontigs.get(i);
764 visiblecontigs.setElementAt(null, i);
765 vcontigs[i*2] = vc[0];
766 vcontigs[i*2+1] = vc[1];
768 visiblecontigs.clear();
773 return new int[] { start, end-1 };