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 = new ArrayList<Integer>();
51 * bitfield for column selection - allows quick lookup
53 private BitSet selected = new BitSet();
56 * adds a new column i to the selection - only if i is not already selected
60 public void add(int i)
64 order.add(Integer.valueOf(i));
75 public void remove(int col)
78 Integer colInt = new Integer(col);
80 if (selected.get(col))
82 // if this ever changes to List.remove(), ensure Integer not int
84 // as List.remove(int i) removes the i'th item which is wrong
90 public boolean contains(Integer colInt)
92 return selected.get(colInt);
95 public boolean isEmpty()
97 return order.isEmpty();
100 public List<Integer> getList()
111 * gets the column that was selected first, second or i'th
116 public int elementAt(int i)
121 protected boolean pruneColumnList(final List<int[]> shifts)
123 int s = 0, t = shifts.size();
124 int[] sr = shifts.get(s++);
125 boolean pruned = false;
126 int i = 0, j = order.size();
127 while (i < j && s <= t)
129 int c = order.get(i++).intValue();
132 if (sr[1] + sr[0] >= c)
133 { // sr[1] -ve means inseriton.
152 * shift every selected column at or above start by change
155 * - leftmost column to be shifted
159 public void compensateForEdits(int start, int change)
161 BitSet mask = new BitSet();
162 for (int i = 0; i < order.size(); i++)
164 int temp = order.get(i);
168 // clear shifted bits and update List of selected columns
169 selected.clear(temp);
170 mask.set(temp - change);
171 order.set(i, new Integer(temp - change));
174 // lastly update the bitfield all at once
178 public boolean isSelected(int column)
180 return selected.get(column);
183 public int getMaxColumn()
185 return selected.length() - 1;
188 public int getMinColumn()
190 return selected.get(0) ? 0 : selected.nextSetBit(0);
194 * @return a series of selection intervals along the range
196 public List<int[]> getRanges()
198 List<int[]> rlist = new ArrayList<int[]>();
199 if (selected.isEmpty())
203 int next = selected.nextSetBit(0), clear = -1;
206 clear = selected.nextClearBit(next);
207 rlist.add(new int[] { next, clear - 1 });
208 next = selected.nextSetBit(clear);
214 public int hashCode()
216 // TODO Auto-generated method stub
217 return selected.hashCode();
221 public boolean equals(Object obj)
223 if (obj instanceof IntList)
225 return ((IntList) obj).selected.equals(selected);
231 IntList selection = new IntList();
234 * list of hidden column [start, end] ranges; the list is maintained in
235 * ascending start column order
237 Vector<int[]> hiddenColumns;
240 * Add a column to the selection
245 public void addElement(int col)
251 * clears column selection
259 * Removes value 'col' from the selection (not the col'th item)
262 * index of column to be removed
264 public void removeElement(int col)
266 selection.remove(col);
270 * removes a range of columns from the selection
273 * int - first column in range to be removed
277 public void removeElements(int start, int end)
280 for (int i = start; i < end; i++)
282 colInt = new Integer(i);
283 if (selection.contains(colInt))
285 selection.remove(colInt);
291 * Returns a list of selected columns. The list contains no duplicates but is
292 * not necessarily ordered. It also may include columns hidden from the
293 * current view. This returns a copy of the actual list, and changes to the
294 * copy will not affect the selection.
296 public List<Integer> getSelected()
298 return new ArrayList<Integer>(selection.getList());
302 * @return list of int arrays containing start and end column position for
303 * runs of selected columns ordered from right to left.
305 public List<int[]> getSelectedRanges()
307 return selection.getRanges();
313 * index to search for in column selection
315 * @return true if col is selected
317 public boolean contains(int col)
319 return (col > -1) ? selection.isSelected(col) : false;
323 * Answers true if no columns are selected, else false
325 public boolean isEmpty()
327 return selection == null || selection.isEmpty();
331 * rightmost selected column
333 * @return rightmost column in alignment that is selected
337 if (selection.isEmpty())
341 return selection.getMaxColumn();
345 * Leftmost column in selection
347 * @return column index of leftmost column in selection
351 if (selection.isEmpty())
355 return selection.getMinColumn();
359 * propagate shift in alignment columns to column selection
364 * shift in edit (+ve for removal, or -ve for inserts)
366 public List<int[]> compensateForEdit(int start, int change)
368 List<int[]> deletedHiddenColumns = null;
369 selection.compensateForEdits(start, change);
371 if (hiddenColumns != null)
373 deletedHiddenColumns = new ArrayList<int[]>();
374 int hSize = hiddenColumns.size();
375 for (int i = 0; i < hSize; i++)
377 int[] region = hiddenColumns.elementAt(i);
378 if (region[0] > start && start + change > region[1])
380 deletedHiddenColumns.add(region);
382 hiddenColumns.removeElementAt(i);
388 if (region[0] > start)
401 this.revealHiddenColumns(0);
404 return deletedHiddenColumns;
408 * propagate shift in alignment columns to column selection special version of
409 * compensateForEdit - allowing for edits within hidden regions
414 * shift in edit (+ve for removal, or -ve for inserts)
416 private void compensateForDelEdits(int start, int change)
419 selection.compensateForEdits(start, change);
421 if (hiddenColumns != null)
423 for (int i = 0; i < hiddenColumns.size(); i++)
425 int[] region = hiddenColumns.elementAt(i);
426 if (region[0] >= start)
430 if (region[1] >= start)
434 if (region[1] < region[0])
436 hiddenColumns.removeElementAt(i--);
452 * Adjust hidden column boundaries based on a series of column additions or
453 * deletions in visible regions.
458 public ShiftList compensateForEdits(ShiftList shiftrecord)
460 if (shiftrecord != null)
462 final List<int[]> shifts = shiftrecord.getShifts();
463 if (shifts != null && shifts.size() > 0)
466 for (int i = 0, j = shifts.size(); i < j; i++)
468 int[] sh = shifts.get(i);
469 // compensateForEdit(shifted+sh[0], sh[1]);
470 compensateForDelEdits(shifted + sh[0], sh[1]);
474 return shiftrecord.getInverse();
480 * removes intersection of position,length ranges in deletions from the
481 * start,end regions marked in intervals.
487 private boolean pruneIntervalVector(final List<int[]> shifts,
488 Vector<int[]> intervals)
490 boolean pruned = false;
491 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
492 int hr[] = intervals.elementAt(i);
493 int sr[] = shifts.get(s);
494 while (i <= j && s <= t)
496 boolean trailinghn = hr[1] >= sr[0];
501 hr = intervals.elementAt(++i);
509 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
510 if (endshift < hr[0] || endshift < sr[0])
511 { // leadinghc disjoint or not a deletion
514 sr = shifts.get(++s);
522 boolean leadinghn = hr[0] >= sr[0];
523 boolean leadinghc = hr[0] < endshift;
524 boolean trailinghc = hr[1] < endshift;
528 { // deleted hidden region.
529 intervals.removeElementAt(i);
534 hr = intervals.elementAt(i);
540 hr[0] = endshift; // clip c terminal region
541 leadinghn = !leadinghn;
557 // sr contained in hr
560 sr = shifts.get(++s);
570 return pruned; // true if any interval was removed or modified by
575 * remove any hiddenColumns or selected columns and shift remaining based on a
576 * series of position, range deletions.
580 public void pruneDeletions(ShiftList deletions)
582 if (deletions != null)
584 final List<int[]> shifts = deletions.getShifts();
585 if (shifts != null && shifts.size() > 0)
587 // delete any intervals intersecting.
588 if (hiddenColumns != null)
590 pruneIntervalVector(shifts, hiddenColumns);
591 if (hiddenColumns != null && hiddenColumns.size() == 0)
593 hiddenColumns = null;
596 if (selection != null && selection.size() > 0)
598 selection.pruneColumnList(shifts);
599 if (selection != null && selection.size() == 0)
604 // and shift the rest.
605 this.compensateForEdits(deletions);
611 * This Method is used to return all the HiddenColumn regions
613 * @return empty list or List of hidden column intervals
615 public List<int[]> getHiddenColumns()
617 return hiddenColumns == null ? Collections.<int[]> emptyList()
622 * Return absolute column index for a visible column index
625 * int column index in alignment view (count from zero)
626 * @return alignment column index for column
628 public int adjustForHiddenColumns(int column)
631 if (hiddenColumns != null)
633 for (int i = 0; i < hiddenColumns.size(); i++)
635 int[] region = hiddenColumns.elementAt(i);
636 if (result >= region[0])
638 result += region[1] - region[0] + 1;
646 * Use this method to find out where a column will appear in the visible
647 * alignment when hidden columns exist. If the column is not visible, then the
648 * left-most visible column will always be returned.
650 * @param hiddenColumn
654 public int findColumnPosition(int hiddenColumn)
656 int result = hiddenColumn;
657 if (hiddenColumns != null)
663 region = hiddenColumns.elementAt(index++);
664 if (hiddenColumn > region[1])
666 result -= region[1] + 1 - region[0];
668 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
669 if (hiddenColumn > region[0] && hiddenColumn < region[1])
671 return region[0] + hiddenColumn - result;
674 return result; // return the shifted position after removing hidden columns.
678 * Use this method to determine where the next hiddenRegion starts
680 * @param hiddenRegion
681 * index of hidden region (counts from 0)
682 * @return column number in visible view
684 public int findHiddenRegionPosition(int hiddenRegion)
687 if (hiddenColumns != null)
693 int[] region = hiddenColumns.elementAt(index);
694 if (hiddenRegion == 0)
699 gaps += region[1] + 1 - region[0];
700 result = region[1] + 1;
702 } while (index <= hiddenRegion);
711 * THis method returns the rightmost limit of a region of an alignment with
712 * hidden columns. In otherwords, the next hidden column.
717 public int getHiddenBoundaryRight(int alPos)
719 if (hiddenColumns != null)
724 int[] region = hiddenColumns.elementAt(index);
725 if (alPos < region[0])
731 } while (index < hiddenColumns.size());
739 * This method returns the leftmost limit of a region of an alignment with
740 * hidden columns. In otherwords, the previous hidden column.
745 public int getHiddenBoundaryLeft(int alPos)
747 if (hiddenColumns != null)
749 int index = hiddenColumns.size() - 1;
752 int[] region = hiddenColumns.elementAt(index);
753 if (alPos > region[1])
759 } while (index > -1);
766 public void hideSelectedColumns()
768 synchronized (selection)
770 for (int[] selregions : selection.getRanges())
772 hideColumns(selregions[0], selregions[1]);
780 * Adds the specified column range to the hidden columns
785 public void hideColumns(int start, int end)
787 if (hiddenColumns == null)
789 hiddenColumns = new Vector<int[]>();
793 * traverse existing hidden ranges and insert / amend / append as
796 for (int i = 0; i < hiddenColumns.size(); i++)
798 int[] region = hiddenColumns.elementAt(i);
800 if (end < region[0] - 1)
803 * insert discontiguous preceding range
805 hiddenColumns.insertElementAt(new int[] { start, end }, i);
809 if (end <= region[1])
812 * new range overlaps existing, or is contiguous preceding it - adjust
815 region[0] = Math.min(region[0], start);
819 if (start <= region[1] + 1)
822 * new range overlaps existing, or is contiguous following it - adjust
823 * start and end columns
825 region[0] = Math.min(region[0], start);
826 region[1] = Math.max(region[1], end);
832 * remaining case is that the new range follows everything else
834 hiddenColumns.addElement(new int[] { start, end });
838 * Hides the specified column and any adjacent selected columns
843 public void hideColumns(int col)
846 * deselect column (whether selected or not!)
851 * find adjacent selected columns
853 int min = col - 1, max = col + 1;
854 while (contains(min))
860 while (contains(max))
867 * min, max are now the closest unselected columns
876 hideColumns(min, max);
880 * Unhides, and adds to the selection list, all hidden columns
882 public void revealAllHiddenColumns()
884 if (hiddenColumns != null)
886 for (int i = 0; i < hiddenColumns.size(); i++)
888 int[] region = hiddenColumns.elementAt(i);
889 for (int j = region[0]; j < region[1] + 1; j++)
896 hiddenColumns = null;
900 * Reveals, and marks as selected, the hidden column range with the given
905 public void revealHiddenColumns(int start)
907 for (int i = 0; i < hiddenColumns.size(); i++)
909 int[] region = hiddenColumns.elementAt(i);
910 if (start == region[0])
912 for (int j = region[0]; j < region[1] + 1; j++)
917 hiddenColumns.removeElement(region);
921 if (hiddenColumns.size() == 0)
923 hiddenColumns = null;
927 public boolean isVisible(int column)
929 if (hiddenColumns != null)
931 for (int[] region : hiddenColumns)
933 if (column >= region[0] && column <= region[1])
948 public ColumnSelection(ColumnSelection copy)
952 if (copy.selection != null)
954 selection = new IntList();
955 for (int i = 0, j = copy.selection.size(); i < j; i++)
957 selection.add(copy.selection.elementAt(i));
960 if (copy.hiddenColumns != null)
962 hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
963 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
966 rh = copy.hiddenColumns.elementAt(i);
969 cp = new int[rh.length];
970 System.arraycopy(rh, 0, cp, 0, rh.length);
971 hiddenColumns.addElement(cp);
981 public ColumnSelection()
985 public String[] getVisibleSequenceStrings(int start, int end,
988 int i, iSize = seqs.length;
989 String selections[] = new String[iSize];
990 if (hiddenColumns != null && hiddenColumns.size() > 0)
992 for (i = 0; i < iSize; i++)
994 StringBuffer visibleSeq = new StringBuffer();
995 List<int[]> regions = getHiddenColumns();
997 int blockStart = start, blockEnd = end;
999 int hideStart, hideEnd;
1001 for (int j = 0; j < regions.size(); j++)
1003 region = regions.get(j);
1004 hideStart = region[0];
1005 hideEnd = region[1];
1007 if (hideStart < start)
1012 blockStart = Math.min(blockStart, hideEnd + 1);
1013 blockEnd = Math.min(blockEnd, hideStart);
1015 if (blockStart > blockEnd)
1020 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
1022 blockStart = hideEnd + 1;
1026 if (end > blockStart)
1028 visibleSeq.append(seqs[i].getSequence(blockStart, end));
1031 selections[i] = visibleSeq.toString();
1036 for (i = 0; i < iSize; i++)
1038 selections[i] = seqs[i].getSequenceAsString(start, end);
1046 * return all visible segments between the given start and end boundaries
1049 * (first column inclusive from 0)
1051 * (last column - not inclusive)
1052 * @return int[] {i_start, i_end, ..} where intervals lie in
1053 * start<=i_start<=i_end<end
1055 public int[] getVisibleContigs(int start, int end)
1057 if (hiddenColumns != null && hiddenColumns.size() > 0)
1059 List<int[]> visiblecontigs = new ArrayList<int[]>();
1060 List<int[]> regions = getHiddenColumns();
1064 int hideStart, hideEnd;
1066 for (int j = 0; vstart < end && j < regions.size(); j++)
1068 region = regions.get(j);
1069 hideStart = region[0];
1070 hideEnd = region[1];
1072 if (hideEnd < vstart)
1076 if (hideStart > vstart)
1078 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
1080 vstart = hideEnd + 1;
1085 visiblecontigs.add(new int[] { vstart, end - 1 });
1087 int[] vcontigs = new int[visiblecontigs.size() * 2];
1088 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
1090 int[] vc = visiblecontigs.get(i);
1091 visiblecontigs.set(i, null);
1092 vcontigs[i * 2] = vc[0];
1093 vcontigs[i * 2 + 1] = vc[1];
1095 visiblecontigs.clear();
1100 return new int[] { start, end - 1 };
1105 * Locate the first and last position visible for this sequence. if seq isn't
1106 * visible then return the position of the left and right of the hidden
1107 * boundary region, and the corresponding alignment column indices for the
1108 * extent of the sequence
1111 * @return int[] { visible start, visible end, first seqpos, last seqpos,
1112 * alignment index for seq start, alignment index for seq end }
1114 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
1116 int fpos=seq.getStart(),lpos= seq.getEnd();
1119 if (hiddenColumns == null || hiddenColumns.size() == 0)
1121 int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;
1122 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
1125 // Simply walk along the sequence whilst watching for hidden column
1127 List<int[]> regions = getHiddenColumns();
1128 int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
1129 .getLength(), hideEnd = -1;
1130 int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
1131 boolean foundStart = false;
1132 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
1135 if (!Comparison.isGap(seq.getCharAt(p)))
1137 // keep track of first/last column
1138 // containing sequence data regardless of visibility
1144 // update hidden region start/end
1145 while (hideEnd < p && rcount < regions.size())
1147 int[] region = regions.get(rcount++);
1149 visNext += region[0] - visPrev;
1150 hideStart = region[0];
1151 hideEnd = region[1];
1155 hideStart = seq.getLength();
1157 // update visible boundary for sequence
1169 // look for next sequence position
1175 return new int[] { findColumnPosition(start),
1176 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
1178 // otherwise, sequence was completely hidden
1179 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
1183 * delete any columns in alignmentAnnotation that are hidden (including
1184 * sequence associated annotation).
1186 * @param alignmentAnnotation
1188 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1190 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1194 * delete any columns in alignmentAnnotation that are hidden (including
1195 * sequence associated annotation).
1198 * remove any annotation to the right of this column
1200 * remove any annotation to the left of this column
1201 * @param alignmentAnnotation
1202 * the annotation to operate on
1204 public void makeVisibleAnnotation(int start, int end,
1205 AlignmentAnnotation alignmentAnnotation)
1207 if (alignmentAnnotation.annotations == null)
1211 if (start == end && end == -1)
1214 end = alignmentAnnotation.annotations.length;
1216 if (hiddenColumns != null && hiddenColumns.size() > 0)
1218 // then mangle the alignmentAnnotation annotation array
1219 Vector<Annotation[]> annels = new Vector<Annotation[]>();
1220 Annotation[] els = null;
1221 List<int[]> regions = getHiddenColumns();
1222 int blockStart = start, blockEnd = end;
1224 int hideStart, hideEnd, w = 0;
1226 for (int j = 0; j < regions.size(); j++)
1228 region = regions.get(j);
1229 hideStart = region[0];
1230 hideEnd = region[1];
1232 if (hideStart < start)
1237 blockStart = Math.min(blockStart, hideEnd + 1);
1238 blockEnd = Math.min(blockEnd, hideStart);
1240 if (blockStart > blockEnd)
1245 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1246 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1249 blockStart = hideEnd + 1;
1253 if (end > blockStart)
1255 annels.addElement(els = new Annotation[end - blockStart + 1]);
1256 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
1258 // copy just the visible segment of the annotation row
1259 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1260 els, 0, els.length);
1264 // copy to the end of the annotation row
1265 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1267 (alignmentAnnotation.annotations.length - blockStart));
1276 alignmentAnnotation.annotations = new Annotation[w];
1279 for (Annotation[] chnk : annels)
1281 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1288 alignmentAnnotation.restrict(start, end);
1293 * Invert the column selection from first to end-1. leaves hiddenColumns
1294 * untouched (and unselected)
1299 public void invertColumnSelection(int first, int width)
1301 boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;
1302 for (int i = first; i < width; i++)
1310 if (!hasHidden || isVisible(i))
1319 * add in any unselected columns from the given column selection, excluding
1320 * any that are hidden.
1324 public void addElementsFrom(ColumnSelection colsel)
1326 if (colsel != null && !colsel.isEmpty())
1328 for (Integer col : colsel.getSelected())
1330 if (hiddenColumns != null && isVisible(col.intValue()))
1339 * set the selected columns the given column selection, excluding any columns
1344 public void setElementsFrom(ColumnSelection colsel)
1346 selection = new IntList();
1347 if (colsel.selection != null && colsel.selection.size() > 0)
1349 if (hiddenColumns != null && hiddenColumns.size() > 0)
1351 // only select visible columns in this columns selection
1352 addElementsFrom(colsel);
1356 // add everything regardless
1357 for (Integer col : colsel.getSelected())
1366 * Add gaps into the sequences aligned to profileseq under the given
1371 * - alignment to have gaps inserted into it
1373 * - alignment view where sequence corresponding to profileseq is
1375 * @return new Column selection for new alignment view, with insertions into
1376 * profileseq marked as hidden.
1378 public static ColumnSelection propagateInsertions(SequenceI profileseq,
1379 AlignmentI al, AlignmentView input)
1383 // return propagateInsertions(profileseq, al, )
1384 char gc = al.getGapCharacter();
1385 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
1386 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
1387 SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];
1388 nview.propagateInsertions(profileseq, al, origseq);
1395 * - sequence in al which corresponds to origseq
1397 * - alignment which is to have gaps inserted into it
1399 * - sequence corresponding to profileseq which defines gap map for
1402 public void propagateInsertions(SequenceI profileseq, AlignmentI al,
1405 char gc = al.getGapCharacter();
1406 // recover mapping between sequence's non-gap positions and positions
1408 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1409 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1412 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
1413 // alandcolsel[0])[0].gapMap()))
1414 // add profile to visible contigs
1415 for (int v = 0; v < viscontigs.length; v += 2)
1417 if (viscontigs[v] > spos)
1419 StringBuffer sb = new StringBuffer();
1420 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1424 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1426 SequenceI sqobj = al.getSequenceAt(s);
1427 if (sqobj != profileseq)
1429 String sq = al.getSequenceAt(s).getSequenceAsString();
1430 if (sq.length() <= spos + offset)
1433 int diff = spos + offset - sq.length() - 1;
1438 while ((diff = spos + offset - sq.length() - 1) > 0)
1441 // + ((diff >= sb.length()) ? sb.toString() : sb
1442 // .substring(0, diff));
1443 if (diff >= sb.length())
1445 sq += sb.toString();
1449 char[] buf = new char[diff];
1450 sb.getChars(0, diff, buf, 0);
1451 sq += buf.toString();
1455 sq += sb.toString();
1459 al.getSequenceAt(s).setSequence(
1460 sq.substring(0, spos + offset) + sb.toString()
1461 + sq.substring(spos + offset));
1465 // offset+=sb.length();
1467 spos = viscontigs[v + 1] + 1;
1469 if ((offset + spos) < profileseq.getLength())
1471 // pad the final region with gaps.
1472 StringBuffer sb = new StringBuffer();
1473 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1477 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1479 SequenceI sqobj = al.getSequenceAt(s);
1480 if (sqobj == profileseq)
1484 String sq = sqobj.getSequenceAsString();
1486 int diff = origseq.getLength() - sq.length();
1490 // + ((diff >= sb.length()) ? sb.toString() : sb
1491 // .substring(0, diff));
1492 if (diff >= sb.length())
1494 sq += sb.toString();
1498 char[] buf = new char[diff];
1499 sb.getChars(0, diff, buf, 0);
1500 sq += buf.toString();
1502 diff = origseq.getLength() - sq.length();
1510 * @return true if there are columns marked
1512 public boolean hasSelectedColumns()
1514 return (selection != null && selection.size() > 0);
1519 * @return true if there are columns hidden
1521 public boolean hasHiddenColumns()
1523 return hiddenColumns != null && hiddenColumns.size() > 0;
1528 * @return true if there are more than one set of columns hidden
1530 public boolean hasManyHiddenColumns()
1532 return hiddenColumns != null && hiddenColumns.size() > 1;
1536 * mark the columns corresponding to gap characters as hidden in the column
1541 public void hideInsertionsFor(SequenceI sr)
1543 List<int[]> inserts = sr.getInsertions();
1544 for (int[] r : inserts)
1546 hideColumns(r[0], r[1]);
1550 public boolean filterAnnotations(Annotation[] annotations,
1551 AnnotationFilterParameter filterParams)
1553 // JBPNote - this method needs to be refactored to become independent of
1554 // viewmodel package
1555 this.revealAllHiddenColumns();
1560 if (annotations[count] != null)
1563 boolean itemMatched = false;
1565 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.ABOVE_THRESHOLD
1566 && annotations[count].value >= filterParams
1567 .getThresholdValue())
1571 if (filterParams.getThresholdType() == AnnotationFilterParameter.ThresholdType.BELOW_THRESHOLD
1572 && annotations[count].value <= filterParams
1573 .getThresholdValue())
1578 if (filterParams.isFilterAlphaHelix()
1579 && annotations[count].secondaryStructure == 'H')
1584 if (filterParams.isFilterBetaSheet()
1585 && annotations[count].secondaryStructure == 'E')
1590 if (filterParams.isFilterTurn()
1591 && annotations[count].secondaryStructure == 'S')
1596 String regexSearchString = filterParams.getRegexString();
1597 if (regexSearchString != null
1598 && !filterParams.getRegexSearchFields().isEmpty())
1600 List<SearchableAnnotationField> fields = filterParams
1601 .getRegexSearchFields();
1604 if (fields.contains(SearchableAnnotationField.DISPLAY_STRING)
1605 && annotations[count].displayCharacter
1606 .matches(regexSearchString))
1610 } catch (java.util.regex.PatternSyntaxException pse)
1612 if (annotations[count].displayCharacter
1613 .equals(regexSearchString))
1618 if (fields.contains(SearchableAnnotationField.DESCRIPTION)
1619 && annotations[count].description != null
1620 && annotations[count].description
1621 .matches(regexSearchString))
1629 this.addElement(count);
1633 } while (count < annotations.length);
1638 * Returns a hashCode built from selected columns and hidden column ranges
1641 public int hashCode()
1643 int hashCode = selection.hashCode();
1644 if (hiddenColumns != null)
1646 for (int[] hidden : hiddenColumns)
1648 hashCode = 31 * hashCode + hidden[0];
1649 hashCode = 31 * hashCode + hidden[1];
1656 * Answers true if comparing to a ColumnSelection with the same selected
1657 * columns and hidden columns, else false
1660 public boolean equals(Object obj)
1662 if (!(obj instanceof ColumnSelection))
1666 ColumnSelection that = (ColumnSelection) obj;
1669 * check columns selected are either both null, or match
1671 if (this.selection == null)
1673 if (that.selection != null)
1678 if (!this.selection.equals(that.selection))
1684 * check hidden columns are either both null, or match
1686 if (this.hiddenColumns == null)
1688 return (that.hiddenColumns == null);
1690 if (that.hiddenColumns == null
1691 || that.hiddenColumns.size() != this.hiddenColumns.size())
1696 for (int[] thisRange : hiddenColumns)
1698 int[] thatRange = that.hiddenColumns.get(i++);
1699 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
1708 * Updates the column selection depending on the parameters, and returns true
1709 * if any change was made to the selection
1711 * @param markedColumns
1712 * a set identifying marked columns (base 0)
1714 * the first column of the range to operate over (base 0)
1716 * the last column of the range to operate over (base 0)
1718 * if true, deselect marked columns and select unmarked
1719 * @param extendCurrent
1720 * if true, extend rather than replacing the current column selection
1722 * if true, toggle the selection state of marked columns
1726 public boolean markColumns(BitSet markedColumns, int startCol,
1727 int endCol, boolean invert, boolean extendCurrent, boolean toggle)
1729 boolean changed = false;
1730 if (!extendCurrent && !toggle)
1732 changed = !this.isEmpty();
1737 // invert only in the currently selected sequence region
1738 int i = markedColumns.nextClearBit(startCol);
1739 int ibs = markedColumns.nextSetBit(startCol);
1740 while (i >= startCol && i <= endCol)
1742 if (ibs < 0 || i < ibs)
1745 if (toggle && contains(i))
1756 i = markedColumns.nextClearBit(ibs);
1757 ibs = markedColumns.nextSetBit(i);
1763 int i = markedColumns.nextSetBit(startCol);
1764 while (i >= startCol && i <= endCol)
1767 if (toggle && contains(i))
1775 i = markedColumns.nextSetBit(i + 1);