2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.util.Comparison;
24 import jalview.util.ShiftList;
25 import jalview.viewmodel.annotationfilter.AnnotationFilterParameter;
26 import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField;
28 import java.util.ArrayList;
29 import java.util.BitSet;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Vector;
35 * Data class holding the selected columns and hidden column ranges for a view.
38 public class ColumnSelection
41 * A class to hold an efficient representation of selected columns
46 * list of selected columns (ordered by selection order, not column order)
48 private List<Integer> order;
51 * an unmodifiable view of the selected columns list
53 private List<Integer> _uorder;
56 * bitfield for column selection - allows quick lookup
58 private BitSet selected;
65 order = new ArrayList<Integer>();
66 _uorder = Collections.unmodifiableList(order);
67 selected = new BitSet();
75 IntList(IntList other)
81 for (int i = 0; i < j; i++)
83 add(other.elementAt(i));
89 * adds a new column i to the selection - only if i is not already selected
97 order.add(Integer.valueOf(i));
111 Integer colInt = new Integer(col);
113 if (selected.get(col))
115 // if this ever changes to List.remove(), ensure Integer not int
117 // as List.remove(int i) removes the i'th item which is wrong
118 order.remove(colInt);
123 boolean contains(Integer colInt)
125 return selected.get(colInt);
130 return order.isEmpty();
134 * Returns a read-only view of the selected columns list
138 List<Integer> getList()
149 * gets the column that was selected first, second or i'th
159 protected boolean pruneColumnList(final List<int[]> shifts)
161 int s = 0, t = shifts.size();
162 int[] sr = shifts.get(s++);
163 boolean pruned = false;
164 int i = 0, j = order.size();
165 while (i < j && s <= t)
167 int c = order.get(i++).intValue();
170 if (sr[1] + sr[0] >= c)
171 { // sr[1] -ve means inseriton.
190 * shift every selected column at or above start by change
193 * - leftmost column to be shifted
197 void compensateForEdits(int start, int change)
199 BitSet mask = new BitSet();
200 for (int i = 0; i < order.size(); i++)
202 int temp = order.get(i);
206 // clear shifted bits and update List of selected columns
207 selected.clear(temp);
208 mask.set(temp - change);
209 order.set(i, new Integer(temp - change));
212 // lastly update the bitfield all at once
216 boolean isSelected(int column)
218 return selected.get(column);
223 return selected.length() - 1;
228 return selected.get(0) ? 0 : selected.nextSetBit(0);
232 * @return a series of selection intervals along the range
234 List<int[]> getRanges()
236 List<int[]> rlist = new ArrayList<int[]>();
237 if (selected.isEmpty())
241 int next = selected.nextSetBit(0), clear = -1;
244 clear = selected.nextClearBit(next);
245 rlist.add(new int[] { next, clear - 1 });
246 next = selected.nextSetBit(clear);
252 public int hashCode()
254 // TODO Auto-generated method stub
255 return selected.hashCode();
259 public boolean equals(Object obj)
261 if (obj instanceof IntList)
263 return ((IntList) obj).selected.equals(selected);
269 IntList selection = new IntList();
272 * list of hidden column [start, end] ranges; the list is maintained in
273 * ascending start column order
275 Vector<int[]> hiddenColumns;
278 * Add a column to the selection
283 public void addElement(int col)
289 * clears column selection
297 * Removes value 'col' from the selection (not the col'th item)
300 * index of column to be removed
302 public void removeElement(int col)
304 selection.remove(col);
308 * removes a range of columns from the selection
311 * int - first column in range to be removed
315 public void removeElements(int start, int end)
318 for (int i = start; i < end; i++)
320 colInt = new Integer(i);
321 if (selection.contains(colInt))
323 selection.remove(colInt);
329 * Returns a read-only view of the (possibly empty) list of selected columns
331 * The list contains no duplicates but is not necessarily ordered. It also may
332 * include columns hidden from the current view. To modify (for example sort)
333 * the list, you should first make a copy.
335 * The list is not thread-safe: iterating over it could result in
336 * ConcurrentModificationException if it is modified by another thread.
338 public List<Integer> getSelected()
340 return selection.getList();
344 * @return list of int arrays containing start and end column position for
345 * runs of selected columns ordered from right to left.
347 public List<int[]> getSelectedRanges()
349 return selection.getRanges();
355 * index to search for in column selection
357 * @return true if col is selected
359 public boolean contains(int col)
361 return (col > -1) ? selection.isSelected(col) : false;
365 * Answers true if no columns are selected, else false
367 public boolean isEmpty()
369 return selection == null || selection.isEmpty();
373 * rightmost selected column
375 * @return rightmost column in alignment that is selected
379 if (selection.isEmpty())
383 return selection.getMaxColumn();
387 * Leftmost column in selection
389 * @return column index of leftmost column in selection
393 if (selection.isEmpty())
397 return selection.getMinColumn();
401 * propagate shift in alignment columns to column selection
406 * shift in edit (+ve for removal, or -ve for inserts)
408 public List<int[]> compensateForEdit(int start, int change)
410 List<int[]> deletedHiddenColumns = null;
411 selection.compensateForEdits(start, change);
413 if (hiddenColumns != null)
415 deletedHiddenColumns = new ArrayList<int[]>();
416 int hSize = hiddenColumns.size();
417 for (int i = 0; i < hSize; i++)
419 int[] region = hiddenColumns.elementAt(i);
420 if (region[0] > start && start + change > region[1])
422 deletedHiddenColumns.add(region);
424 hiddenColumns.removeElementAt(i);
430 if (region[0] > start)
443 this.revealHiddenColumns(0);
446 return deletedHiddenColumns;
450 * propagate shift in alignment columns to column selection special version of
451 * compensateForEdit - allowing for edits within hidden regions
456 * shift in edit (+ve for removal, or -ve for inserts)
458 private void compensateForDelEdits(int start, int change)
461 selection.compensateForEdits(start, change);
463 if (hiddenColumns != null)
465 for (int i = 0; i < hiddenColumns.size(); i++)
467 int[] region = hiddenColumns.elementAt(i);
468 if (region[0] >= start)
472 if (region[1] >= start)
476 if (region[1] < region[0])
478 hiddenColumns.removeElementAt(i--);
494 * Adjust hidden column boundaries based on a series of column additions or
495 * deletions in visible regions.
500 public ShiftList compensateForEdits(ShiftList shiftrecord)
502 if (shiftrecord != null)
504 final List<int[]> shifts = shiftrecord.getShifts();
505 if (shifts != null && shifts.size() > 0)
508 for (int i = 0, j = shifts.size(); i < j; i++)
510 int[] sh = shifts.get(i);
511 // compensateForEdit(shifted+sh[0], sh[1]);
512 compensateForDelEdits(shifted + sh[0], sh[1]);
516 return shiftrecord.getInverse();
522 * removes intersection of position,length ranges in deletions from the
523 * start,end regions marked in intervals.
529 private boolean pruneIntervalVector(final List<int[]> shifts,
530 Vector<int[]> intervals)
532 boolean pruned = false;
533 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
534 int hr[] = intervals.elementAt(i);
535 int sr[] = shifts.get(s);
536 while (i <= j && s <= t)
538 boolean trailinghn = hr[1] >= sr[0];
543 hr = intervals.elementAt(++i);
551 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
552 if (endshift < hr[0] || endshift < sr[0])
553 { // leadinghc disjoint or not a deletion
556 sr = shifts.get(++s);
564 boolean leadinghn = hr[0] >= sr[0];
565 boolean leadinghc = hr[0] < endshift;
566 boolean trailinghc = hr[1] < endshift;
570 { // deleted hidden region.
571 intervals.removeElementAt(i);
576 hr = intervals.elementAt(i);
582 hr[0] = endshift; // clip c terminal region
583 leadinghn = !leadinghn;
599 // sr contained in hr
602 sr = shifts.get(++s);
612 return pruned; // true if any interval was removed or modified by
617 * remove any hiddenColumns or selected columns and shift remaining based on a
618 * series of position, range deletions.
622 public void pruneDeletions(ShiftList deletions)
624 if (deletions != null)
626 final List<int[]> shifts = deletions.getShifts();
627 if (shifts != null && shifts.size() > 0)
629 // delete any intervals intersecting.
630 if (hiddenColumns != null)
632 pruneIntervalVector(shifts, hiddenColumns);
633 if (hiddenColumns != null && hiddenColumns.size() == 0)
635 hiddenColumns = null;
638 if (selection != null && selection.size() > 0)
640 selection.pruneColumnList(shifts);
641 if (selection != null && selection.size() == 0)
646 // and shift the rest.
647 this.compensateForEdits(deletions);
653 * This Method is used to return all the HiddenColumn regions
655 * @return empty list or List of hidden column intervals
657 public List<int[]> getHiddenColumns()
659 return hiddenColumns == null ? Collections.<int[]> emptyList()
664 * Return absolute column index for a visible column index
667 * int column index in alignment view (count from zero)
668 * @return alignment column index for column
670 public int adjustForHiddenColumns(int column)
673 if (hiddenColumns != null)
675 for (int i = 0; i < hiddenColumns.size(); i++)
677 int[] region = hiddenColumns.elementAt(i);
678 if (result >= region[0])
680 result += region[1] - region[0] + 1;
688 * Use this method to find out where a column will appear in the visible
689 * alignment when hidden columns exist. If the column is not visible, then the
690 * left-most visible column will always be returned.
692 * @param hiddenColumn
693 * the column index in the full alignment including hidden columns
694 * @return the position of the column in the visible alignment
696 public int findColumnPosition(int hiddenColumn)
698 int result = hiddenColumn;
699 if (hiddenColumns != null)
705 region = hiddenColumns.elementAt(index++);
706 if (hiddenColumn > region[1])
708 result -= region[1] + 1 - region[0];
710 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
712 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
714 // Here the hidden column is within a region, so
715 // we want to return the position of region[0]-1, adjusted for any
716 // earlier hidden columns.
717 // Calculate the difference between the actual hidden col position
718 // and region[0]-1, and then subtract from result to convert result from
719 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
721 // However, if the region begins at 0 we cannot return region[0]-1
722 // so return region[1] + 1 instead, adjusted for earlier hidden columns,
723 // by calculating the difference between region[1]+1 and the actual
725 // column, and then adding to result to convert result from the adjusted
726 // hiddenColumn value to the adjusted region[1]+1 value
729 return result + (region[1] + 1 - hiddenColumn);
733 return result - (hiddenColumn - region[0] + 1);
737 return result; // return the shifted position after removing hidden columns.
741 * Use this method to determine where the next hiddenRegion starts
743 * @param hiddenRegion
744 * index of hidden region (counts from 0)
745 * @return column number in visible view
747 public int findHiddenRegionPosition(int hiddenRegion)
750 if (hiddenColumns != null)
756 int[] region = hiddenColumns.elementAt(index);
757 if (hiddenRegion == 0)
762 gaps += region[1] + 1 - region[0];
763 result = region[1] + 1;
765 } while (index <= hiddenRegion);
774 * THis method returns the rightmost limit of a region of an alignment with
775 * hidden columns. In otherwords, the next hidden column.
780 public int getHiddenBoundaryRight(int alPos)
782 if (hiddenColumns != null)
787 int[] region = hiddenColumns.elementAt(index);
788 if (alPos < region[0])
794 } while (index < hiddenColumns.size());
802 * This method returns the leftmost limit of a region of an alignment with
803 * hidden columns. In otherwords, the previous hidden column.
808 public int getHiddenBoundaryLeft(int alPos)
810 if (hiddenColumns != null)
812 int index = hiddenColumns.size() - 1;
815 int[] region = hiddenColumns.elementAt(index);
816 if (alPos > region[1])
822 } while (index > -1);
829 public void hideSelectedColumns()
831 synchronized (selection)
833 for (int[] selregions : selection.getRanges())
835 hideColumns(selregions[0], selregions[1]);
843 * Adds the specified column range to the hidden columns
848 public void hideColumns(int start, int end)
850 if (hiddenColumns == null)
852 hiddenColumns = new Vector<int[]>();
856 * traverse existing hidden ranges and insert / amend / append as
859 for (int i = 0; i < hiddenColumns.size(); i++)
861 int[] region = hiddenColumns.elementAt(i);
863 if (end < region[0] - 1)
866 * insert discontiguous preceding range
868 hiddenColumns.insertElementAt(new int[] { start, end }, i);
872 if (end <= region[1])
875 * new range overlaps existing, or is contiguous preceding it - adjust
878 region[0] = Math.min(region[0], start);
882 if (start <= region[1] + 1)
885 * new range overlaps existing, or is contiguous following it - adjust
886 * start and end columns
888 region[0] = Math.min(region[0], start);
889 region[1] = Math.max(region[1], end);
892 * also update or remove any subsequent ranges
893 * that are overlapped
895 while (i < hiddenColumns.size() - 1)
897 int[] nextRegion = hiddenColumns.get(i + 1);
898 if (nextRegion[0] > end + 1)
901 * gap to next hidden range - no more to update
905 region[1] = Math.max(nextRegion[1], end);
906 hiddenColumns.remove(i + 1);
913 * remaining case is that the new range follows everything else
915 hiddenColumns.addElement(new int[] { start, end });
919 * Hides the specified column and any adjacent selected columns
924 public void hideColumns(int col)
927 * deselect column (whether selected or not!)
932 * find adjacent selected columns
934 int min = col - 1, max = col + 1;
935 while (contains(min))
941 while (contains(max))
948 * min, max are now the closest unselected columns
957 hideColumns(min, max);
961 * Unhides, and adds to the selection list, all hidden columns
963 public void revealAllHiddenColumns()
965 if (hiddenColumns != null)
967 for (int i = 0; i < hiddenColumns.size(); i++)
969 int[] region = hiddenColumns.elementAt(i);
970 for (int j = region[0]; j < region[1] + 1; j++)
977 hiddenColumns = null;
981 * Reveals, and marks as selected, the hidden column range with the given
986 public void revealHiddenColumns(int start)
988 for (int i = 0; i < hiddenColumns.size(); i++)
990 int[] region = hiddenColumns.elementAt(i);
991 if (start == region[0])
993 for (int j = region[0]; j < region[1] + 1; j++)
998 hiddenColumns.removeElement(region);
1002 if (hiddenColumns.size() == 0)
1004 hiddenColumns = null;
1008 public boolean isVisible(int column)
1010 if (hiddenColumns != null)
1012 for (int[] region : hiddenColumns)
1014 if (column >= region[0] && column <= region[1])
1029 public ColumnSelection(ColumnSelection copy)
1033 selection = new IntList(copy.selection);
1034 if (copy.hiddenColumns != null)
1036 hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
1037 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
1040 rh = copy.hiddenColumns.elementAt(i);
1043 cp = new int[rh.length];
1044 System.arraycopy(rh, 0, cp, 0, rh.length);
1045 hiddenColumns.addElement(cp);
1055 public ColumnSelection()
1059 public String[] getVisibleSequenceStrings(int start, int end,
1062 int i, iSize = seqs.length;
1063 String selections[] = new String[iSize];
1064 if (hiddenColumns != null && hiddenColumns.size() > 0)
1066 for (i = 0; i < iSize; i++)
1068 StringBuffer visibleSeq = new StringBuffer();
1069 List<int[]> regions = getHiddenColumns();
1071 int blockStart = start, blockEnd = end;
1073 int hideStart, hideEnd;
1075 for (int j = 0; j < regions.size(); j++)
1077 region = regions.get(j);
1078 hideStart = region[0];
1079 hideEnd = region[1];
1081 if (hideStart < start)
1086 blockStart = Math.min(blockStart, hideEnd + 1);
1087 blockEnd = Math.min(blockEnd, hideStart);
1089 if (blockStart > blockEnd)
1094 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
1096 blockStart = hideEnd + 1;
1100 if (end > blockStart)
1102 visibleSeq.append(seqs[i].getSequence(blockStart, end));
1105 selections[i] = visibleSeq.toString();
1110 for (i = 0; i < iSize; i++)
1112 selections[i] = seqs[i].getSequenceAsString(start, end);
1120 * return all visible segments between the given start and end boundaries
1123 * (first column inclusive from 0)
1125 * (last column - not inclusive)
1126 * @return int[] {i_start, i_end, ..} where intervals lie in
1127 * start<=i_start<=i_end<end
1129 public int[] getVisibleContigs(int start, int end)
1131 if (hiddenColumns != null && hiddenColumns.size() > 0)
1133 List<int[]> visiblecontigs = new ArrayList<int[]>();
1134 List<int[]> regions = getHiddenColumns();
1138 int hideStart, hideEnd;
1140 for (int j = 0; vstart < end && j < regions.size(); j++)
1142 region = regions.get(j);
1143 hideStart = region[0];
1144 hideEnd = region[1];
1146 if (hideEnd < vstart)
1150 if (hideStart > vstart)
1152 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
1154 vstart = hideEnd + 1;
1159 visiblecontigs.add(new int[] { vstart, end - 1 });
1161 int[] vcontigs = new int[visiblecontigs.size() * 2];
1162 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
1164 int[] vc = visiblecontigs.get(i);
1165 visiblecontigs.set(i, null);
1166 vcontigs[i * 2] = vc[0];
1167 vcontigs[i * 2 + 1] = vc[1];
1169 visiblecontigs.clear();
1174 return new int[] { start, end - 1 };
1179 * Locate the first and last position visible for this sequence. if seq isn't
1180 * visible then return the position of the left and right of the hidden
1181 * boundary region, and the corresponding alignment column indices for the
1182 * extent of the sequence
1185 * @return int[] { visible start, visible end, first seqpos, last seqpos,
1186 * alignment index for seq start, alignment index for seq end }
1188 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
1190 int fpos = seq.getStart(), lpos = seq.getEnd();
1193 if (hiddenColumns == null || hiddenColumns.size() == 0)
1195 int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;
1196 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
1199 // Simply walk along the sequence whilst watching for hidden column
1201 List<int[]> regions = getHiddenColumns();
1202 int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
1203 .getLength(), hideEnd = -1;
1204 int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
1205 boolean foundStart = false;
1206 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
1209 if (!Comparison.isGap(seq.getCharAt(p)))
1211 // keep track of first/last column
1212 // containing sequence data regardless of visibility
1218 // update hidden region start/end
1219 while (hideEnd < p && rcount < regions.size())
1221 int[] region = regions.get(rcount++);
1223 visNext += region[0] - visPrev;
1224 hideStart = region[0];
1225 hideEnd = region[1];
1229 hideStart = seq.getLength();
1231 // update visible boundary for sequence
1243 // look for next sequence position
1249 return new int[] { findColumnPosition(start),
1250 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
1252 // otherwise, sequence was completely hidden
1253 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
1257 * delete any columns in alignmentAnnotation that are hidden (including
1258 * sequence associated annotation).
1260 * @param alignmentAnnotation
1262 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1264 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1268 * delete any columns in alignmentAnnotation that are hidden (including
1269 * sequence associated annotation).
1272 * remove any annotation to the right of this column
1274 * remove any annotation to the left of this column
1275 * @param alignmentAnnotation
1276 * the annotation to operate on
1278 public void makeVisibleAnnotation(int start, int end,
1279 AlignmentAnnotation alignmentAnnotation)
1281 if (alignmentAnnotation.annotations == null)
1285 if (start == end && end == -1)
1288 end = alignmentAnnotation.annotations.length;
1290 if (hiddenColumns != null && hiddenColumns.size() > 0)
1292 // then mangle the alignmentAnnotation annotation array
1293 Vector<Annotation[]> annels = new Vector<Annotation[]>();
1294 Annotation[] els = null;
1295 List<int[]> regions = getHiddenColumns();
1296 int blockStart = start, blockEnd = end;
1298 int hideStart, hideEnd, w = 0;
1300 for (int j = 0; j < regions.size(); j++)
1302 region = regions.get(j);
1303 hideStart = region[0];
1304 hideEnd = region[1];
1306 if (hideStart < start)
1311 blockStart = Math.min(blockStart, hideEnd + 1);
1312 blockEnd = Math.min(blockEnd, hideStart);
1314 if (blockStart > blockEnd)
1319 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1320 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1323 blockStart = hideEnd + 1;
1327 if (end > blockStart)
1329 annels.addElement(els = new Annotation[end - blockStart + 1]);
1330 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
1332 // copy just the visible segment of the annotation row
1333 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1334 els, 0, els.length);
1338 // copy to the end of the annotation row
1339 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1341 (alignmentAnnotation.annotations.length - blockStart));
1350 alignmentAnnotation.annotations = new Annotation[w];
1353 for (Annotation[] chnk : annels)
1355 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1362 alignmentAnnotation.restrict(start, end);
1367 * Invert the column selection from first to end-1. leaves hiddenColumns
1368 * untouched (and unselected)
1373 public void invertColumnSelection(int first, int width)
1375 boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;
1376 for (int i = first; i < width; i++)
1384 if (!hasHidden || isVisible(i))
1393 * add in any unselected columns from the given column selection, excluding
1394 * any that are hidden.
1398 public void addElementsFrom(ColumnSelection colsel)
1400 if (colsel != null && !colsel.isEmpty())
1402 for (Integer col : colsel.getSelected())
1404 if (hiddenColumns != null && isVisible(col.intValue()))
1413 * set the selected columns the given column selection, excluding any columns
1418 public void setElementsFrom(ColumnSelection colsel)
1420 selection = new IntList();
1421 if (colsel.selection != null && colsel.selection.size() > 0)
1423 if (hiddenColumns != null && hiddenColumns.size() > 0)
1425 // only select visible columns in this columns selection
1426 addElementsFrom(colsel);
1430 // add everything regardless
1431 for (Integer col : colsel.getSelected())
1440 * Add gaps into the sequences aligned to profileseq under the given
1445 * - alignment to have gaps inserted into it
1447 * - alignment view where sequence corresponding to profileseq is
1449 * @return new Column selection for new alignment view, with insertions into
1450 * profileseq marked as hidden.
1452 public static ColumnSelection propagateInsertions(SequenceI profileseq,
1453 AlignmentI al, AlignmentView input)
1457 // return propagateInsertions(profileseq, al, )
1458 char gc = al.getGapCharacter();
1459 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
1460 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
1461 SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];
1462 nview.propagateInsertions(profileseq, al, origseq);
1469 * - sequence in al which corresponds to origseq
1471 * - alignment which is to have gaps inserted into it
1473 * - sequence corresponding to profileseq which defines gap map for
1476 public void propagateInsertions(SequenceI profileseq, AlignmentI al,
1479 char gc = al.getGapCharacter();
1480 // recover mapping between sequence's non-gap positions and positions
1482 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1483 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1486 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
1487 // alandcolsel[0])[0].gapMap()))
1488 // add profile to visible contigs
1489 for (int v = 0; v < viscontigs.length; v += 2)
1491 if (viscontigs[v] > spos)
1493 StringBuffer sb = new StringBuffer();
1494 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1498 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1500 SequenceI sqobj = al.getSequenceAt(s);
1501 if (sqobj != profileseq)
1503 String sq = al.getSequenceAt(s).getSequenceAsString();
1504 if (sq.length() <= spos + offset)
1507 int diff = spos + offset - sq.length() - 1;
1512 while ((diff = spos + offset - sq.length() - 1) > 0)
1515 // + ((diff >= sb.length()) ? sb.toString() : sb
1516 // .substring(0, diff));
1517 if (diff >= sb.length())
1519 sq += sb.toString();
1523 char[] buf = new char[diff];
1524 sb.getChars(0, diff, buf, 0);
1525 sq += buf.toString();
1529 sq += sb.toString();
1533 al.getSequenceAt(s).setSequence(
1534 sq.substring(0, spos + offset) + sb.toString()
1535 + sq.substring(spos + offset));
1539 // offset+=sb.length();
1541 spos = viscontigs[v + 1] + 1;
1543 if ((offset + spos) < profileseq.getLength())
1545 // pad the final region with gaps.
1546 StringBuffer sb = new StringBuffer();
1547 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1551 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1553 SequenceI sqobj = al.getSequenceAt(s);
1554 if (sqobj == profileseq)
1558 String sq = sqobj.getSequenceAsString();
1560 int diff = origseq.getLength() - sq.length();
1564 // + ((diff >= sb.length()) ? sb.toString() : sb
1565 // .substring(0, diff));
1566 if (diff >= sb.length())
1568 sq += sb.toString();
1572 char[] buf = new char[diff];
1573 sb.getChars(0, diff, buf, 0);
1574 sq += buf.toString();
1576 diff = origseq.getLength() - sq.length();
1584 * @return true if there are columns marked
1586 public boolean hasSelectedColumns()
1588 return (selection != null && selection.size() > 0);
1593 * @return true if there are columns hidden
1595 public boolean hasHiddenColumns()
1597 return hiddenColumns != null && hiddenColumns.size() > 0;
1602 * @return true if there are more than one set of columns hidden
1604 public boolean hasManyHiddenColumns()
1606 return hiddenColumns != null && hiddenColumns.size() > 1;
1610 * mark the columns corresponding to gap characters as hidden in the column
1615 public void hideInsertionsFor(SequenceI sr)
1617 List<int[]> inserts = sr.getInsertions();
1618 for (int[] r : inserts)
1620 hideColumns(r[0], r[1]);
1624 public boolean filterAnnotations(Annotation[] annotations,
1625 AnnotationFilterParameter filterParams)
1627 // JBPNote - this method needs to be refactored to become independent of
1628 // viewmodel package
1629 this.revealAllHiddenColumns();
1634 if (annotations[count] != null)
1637 boolean itemMatched = false;
1639 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
1640 && annotations[count].value >= filterParams
1641 .getThresholdValue())
1645 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
1646 && annotations[count].value <= filterParams
1647 .getThresholdValue())
1652 if (filterParams.isFilterAlphaHelix()
1653 && annotations[count].secondaryStructure == 'H')
1658 if (filterParams.isFilterBetaSheet()
1659 && annotations[count].secondaryStructure == 'E')
1664 if (filterParams.isFilterTurn()
1665 && annotations[count].secondaryStructure == 'S')
1670 String regexSearchString = filterParams.getRegexString();
1671 if (regexSearchString != null
1672 && !filterParams.getRegexSearchFields().isEmpty())
1674 List<SearchableAnnotationField> fields = filterParams
1675 .getRegexSearchFields();
1678 if (fields.contains(SearchableAnnotationField.DISPLAY_STRING)
1679 && annotations[count].displayCharacter
1680 .matches(regexSearchString))
1684 } catch (java.util.regex.PatternSyntaxException pse)
1686 if (annotations[count].displayCharacter
1687 .equals(regexSearchString))
1692 if (fields.contains(SearchableAnnotationField.DESCRIPTION)
1693 && annotations[count].description != null
1694 && annotations[count].description
1695 .matches(regexSearchString))
1703 this.addElement(count);
1707 } while (count < annotations.length);
1712 * Returns a hashCode built from selected columns and hidden column ranges
1715 public int hashCode()
1717 int hashCode = selection.hashCode();
1718 if (hiddenColumns != null)
1720 for (int[] hidden : hiddenColumns)
1722 hashCode = 31 * hashCode + hidden[0];
1723 hashCode = 31 * hashCode + hidden[1];
1730 * Answers true if comparing to a ColumnSelection with the same selected
1731 * columns and hidden columns, else false
1734 public boolean equals(Object obj)
1736 if (!(obj instanceof ColumnSelection))
1740 ColumnSelection that = (ColumnSelection) obj;
1743 * check columns selected are either both null, or match
1745 if (this.selection == null)
1747 if (that.selection != null)
1752 if (!this.selection.equals(that.selection))
1758 * check hidden columns are either both null, or match
1760 if (this.hiddenColumns == null)
1762 return (that.hiddenColumns == null);
1764 if (that.hiddenColumns == null
1765 || that.hiddenColumns.size() != this.hiddenColumns.size())
1770 for (int[] thisRange : hiddenColumns)
1772 int[] thatRange = that.hiddenColumns.get(i++);
1773 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
1782 * Updates the column selection depending on the parameters, and returns true
1783 * if any change was made to the selection
1785 * @param markedColumns
1786 * a set identifying marked columns (base 0)
1788 * the first column of the range to operate over (base 0)
1790 * the last column of the range to operate over (base 0)
1792 * if true, deselect marked columns and select unmarked
1793 * @param extendCurrent
1794 * if true, extend rather than replacing the current column selection
1796 * if true, toggle the selection state of marked columns
1800 public boolean markColumns(BitSet markedColumns, int startCol,
1801 int endCol, boolean invert, boolean extendCurrent, boolean toggle)
1803 boolean changed = false;
1804 if (!extendCurrent && !toggle)
1806 changed = !this.isEmpty();
1811 // invert only in the currently selected sequence region
1812 int i = markedColumns.nextClearBit(startCol);
1813 int ibs = markedColumns.nextSetBit(startCol);
1814 while (i >= startCol && i <= endCol)
1816 if (ibs < 0 || i < ibs)
1819 if (toggle && contains(i))
1830 i = markedColumns.nextClearBit(ibs);
1831 ibs = markedColumns.nextSetBit(i);
1837 int i = markedColumns.nextSetBit(startCol);
1838 while (i >= startCol && i <= endCol)
1841 if (toggle && contains(i))
1849 i = markedColumns.nextSetBit(i + 1);
1856 * Adjusts column selections, and the given selection group, to match the
1857 * range of a stretch (e.g. mouse drag) operation
1859 * Method refactored from ScalePanel.mouseDragged
1862 * current column position, adjusted for hidden columns
1864 * current selection group
1866 * start position of the stretch group
1868 * end position of the stretch group
1870 public void stretchGroup(int res, SequenceGroup sg, int min, int max)
1877 if (res > sg.getStartRes())
1879 // expand selection group to the right
1882 if (res < sg.getStartRes())
1884 // expand selection group to the left
1885 sg.setStartRes(res);
1889 * expand or shrink column selection to match the
1890 * range of the drag operation
1892 for (int col = min; col <= max; col++)
1894 if (col < sg.getStartRes() || col > sg.getEndRes())
1896 // shrinking drag - remove from selection
1901 // expanding drag - add to selection