2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.ShiftList;
25 import java.util.ArrayList;
26 import java.util.List;
29 * NOTE: Columns are zero based.
31 public class ColumnSelection
34 * Because autoboxing can convert int to Integer, have to be very careful
35 * using methods remove(int), remove(Integer) and set(int, Integer).
36 * Verification is in unit tests.
38 List<Integer> selected = new ArrayList<Integer>();
41 * List of {startCol, endCol} hidden column ranges
43 List<int[]> hiddenColumns;
46 * Add a column to the selection
51 public void addElement(int col)
53 Integer column = new Integer(col);
54 if (!selected.contains(column))
61 * clears column selection
69 * Removes column 'col' from selection (NOT the col'th item).
72 * index of column to be removed
74 public void removeElement(int col)
76 selected.remove(new Integer(col));
80 * removes a range of columns from the selection
83 * int - first column in range to be removed
87 public void removeElements(int start, int end)
90 for (int i = start; i < end; i++)
92 colInt = new Integer(i);
93 if (selected.contains(colInt))
95 selected.remove(colInt);
102 * @return list containing selected columns as Integers
104 public List<Integer> getSelected()
112 * index to search for in column selection
114 * @return true if Integer(col) is in selection.
116 public boolean contains(int col)
118 return selected.contains(new Integer(col));
122 * Column number at position i in selection
125 * index into selected columns
127 * @return column number in alignment
129 public int columnAt(int i)
131 // autoboxing Integer -> int
132 return selected.get(i);
138 * @return DOCUMENT ME!
142 return selected.size();
146 * rightmost selected column
148 * @return rightmost column in alignment that is selected
154 for (int i = 0; i < selected.size(); i++)
156 if (columnAt(i) > max)
166 * Leftmost column in selection
168 * @return column index of leftmost column in selection
172 int min = Integer.MAX_VALUE;
174 for (int i = 0; i < selected.size(); i++)
176 if (columnAt(i) < min)
186 * propagate shift in alignment columns to column selection
191 * shift in edit (+ve for removal, or -ve for inserts)
193 public List<int[]> compensateForEdit(int start, int change)
195 List<int[]> deletedHiddenColumns = null;
196 for (int i = 0; i < size(); i++)
198 int temp = columnAt(i);
203 selected.set(i, new Integer(temp - change));
207 if (hiddenColumns != null)
209 deletedHiddenColumns = new ArrayList<int[]>();
210 int hSize = hiddenColumns.size();
211 for (int i = 0; i < hSize; i++)
213 int[] region = hiddenColumns.get(i);
214 if (region[0] > start && start + change > region[1])
216 deletedHiddenColumns.add(region);
218 hiddenColumns.remove(i);
224 if (region[0] > start)
237 this.revealHiddenColumns(0);
240 return deletedHiddenColumns;
244 * propagate shift in alignment columns to column selection special version of
245 * compensateForEdit - allowing for edits within hidden regions
250 * shift in edit (+ve for removal, or -ve for inserts)
252 private void compensateForDelEdits(int start, int change)
254 for (int i = 0; i < size(); i++)
256 int temp = columnAt(i);
260 // i is index position in list here!
261 selected.set(i, new Integer(temp - change));
265 if (hiddenColumns != null)
267 for (int i = 0; i < hiddenColumns.size(); i++)
269 int[] region = hiddenColumns.get(i);
270 if (region[0] >= start)
274 if (region[1] >= start)
278 if (region[1] < region[0])
280 hiddenColumns.remove(i--);
296 * Adjust hidden column boundaries based on a series of column additions or
297 * deletions in visible regions.
302 public ShiftList compensateForEdits(ShiftList shiftrecord)
304 if (shiftrecord != null)
306 List<int[]> shifts = shiftrecord.getShifts();
307 if (shifts != null && shifts.size() > 0)
310 for (int i = 0, j = shifts.size(); i < j; i++)
312 int[] sh = shifts.get(i);
313 // compensateForEdit(shifted+sh[0], sh[1]);
314 compensateForDelEdits(shifted + sh[0], sh[1]);
318 return shiftrecord.getInverse();
324 * removes intersection of position,length ranges in deletions from the
325 * start,end regions marked in intervals.
328 * @param hiddenColumns2
331 private boolean pruneIntervalVector(List<int[]> shifts,
332 List<int[]> hiddenColumns2)
334 boolean pruned = false;
335 int i = 0, j = hiddenColumns2.size() - 1, s = 0, t = shifts.size() - 1;
336 int hr[] = hiddenColumns2.get(i);
337 int sr[] = shifts.get(s);
338 while (i <= j && s <= t)
340 boolean trailinghn = hr[1] >= sr[0];
345 hr = hiddenColumns2.get(++i);
353 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
354 if (endshift < hr[0] || endshift < sr[0])
355 { // leadinghc disjoint or not a deletion
358 sr = shifts.get(++s);
366 boolean leadinghn = hr[0] >= sr[0];
367 boolean leadinghc = hr[0] < endshift;
368 boolean trailinghc = hr[1] < endshift;
372 { // deleted hidden region.
373 hiddenColumns2.remove(i);
378 hr = hiddenColumns2.remove(i);
384 hr[0] = endshift; // clip c terminal region
385 leadinghn = !leadinghn;
401 // sr contained in hr
404 sr = shifts.get(++s);
414 return pruned; // true if any interval was removed or modified by
418 private boolean pruneColumnList(List<int[]> shifts, List<Integer> list)
420 int s = 0, t = shifts.size();
421 int[] sr = shifts.get(s++);
422 boolean pruned = false;
423 int i = 0, j = list.size();
424 while (i < j && s <= t)
426 int c = list.get(i++);
429 if (sr[1] + sr[0] >= c)
430 { // sr[1] -ve means insertion.
431 // remove the (i-1)'th element
449 * remove any hiddenColumns or selected columns and shift remaining based on a
450 * series of position, range deletions.
454 public void pruneDeletions(ShiftList deletions)
456 if (deletions != null)
458 List<int[]> shifts = deletions.getShifts();
459 if (shifts != null && shifts.size() > 0)
461 // delete any intervals intersecting.
462 if (hiddenColumns != null)
464 pruneIntervalVector(shifts, hiddenColumns);
465 if (hiddenColumns != null && hiddenColumns.size() == 0)
467 hiddenColumns = null;
470 if (selected != null && selected.size() > 0)
472 pruneColumnList(shifts, selected);
473 if (selected != null && selected.size() == 0)
478 // and shift the rest.
479 this.compensateForEdits(deletions);
485 * This method is used to return all the HiddenColumn regions less than the
492 public List<int[]> getHiddenColumns()
494 return hiddenColumns;
498 * Return absolute column index for a visible column index
501 * int column index in alignment view
502 * @return alignment column index for column
504 public int adjustForHiddenColumns(int column)
507 if (hiddenColumns != null)
509 for (int i = 0; i < hiddenColumns.size(); i++)
511 int[] region = hiddenColumns.get(i);
512 if (result >= region[0])
514 result += region[1] - region[0] + 1;
522 * Use this method to find out where a column will appear in the visible
523 * alignment when hidden columns exist. If the column is not visible, then the
524 * left-most visible column will always be returned.
526 * @param hiddenColumn
530 public int findColumnPosition(int hiddenColumn)
532 int result = hiddenColumn;
533 if (hiddenColumns != null)
539 region = hiddenColumns.get(index++);
540 if (hiddenColumn > region[1])
542 result -= region[1] + 1 - region[0];
544 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
545 if (hiddenColumn > region[0] && hiddenColumn < region[1])
547 return region[0] + hiddenColumn - result;
550 return result; // return the shifted position after removing hidden columns.
554 * Use this method to determine where the next hiddenRegion starts
556 public int findHiddenRegionPosition(int hiddenRegion)
559 if (hiddenColumns != null)
565 int[] region = hiddenColumns.get(index);
566 if (hiddenRegion == 0)
571 gaps += region[1] + 1 - region[0];
572 result = region[1] + 1;
574 } while (index < hiddenRegion + 1);
583 * THis method returns the rightmost limit of a region of an alignment with
584 * hidden columns. In otherwords, the next hidden column.
589 public int getHiddenBoundaryRight(int alPos)
591 if (hiddenColumns != null)
596 int[] region = hiddenColumns.get(index);
597 if (alPos < region[0])
603 } while (index < hiddenColumns.size());
611 * This method returns the leftmost limit of a region of an alignment with
612 * hidden columns. In otherwords, the previous hidden column.
617 public int getHiddenBoundaryLeft(int alPos)
619 if (hiddenColumns != null)
621 int index = hiddenColumns.size() - 1;
624 int[] region = hiddenColumns.get(index);
625 if (alPos > region[1])
631 } while (index > -1);
638 public void hideSelectedColumns()
642 int column = getSelected().get(0).intValue();
648 public void hideColumns(int start, int end)
650 if (hiddenColumns == null)
652 hiddenColumns = new ArrayList<int[]>();
655 boolean added = false;
656 boolean overlap = false;
658 for (int i = 0; i < hiddenColumns.size(); i++)
660 int[] region = hiddenColumns.get(i);
661 if (start <= region[1] && end >= region[0])
663 hiddenColumns.remove(i);
667 else if (end < region[0] && start < region[0])
669 hiddenColumns.add(i, new int[]
678 hideColumns(start, end);
682 hiddenColumns.add(new int[]
689 * This method will find a range of selected columns around the column
695 public void hideColumns(int col)
697 // First find out range of columns to hide
698 int min = col, max = col + 1;
699 while (contains(min))
705 while (contains(max))
718 hideColumns(min, max);
721 public void revealAllHiddenColumns()
723 if (hiddenColumns != null)
725 for (int i = 0; i < hiddenColumns.size(); i++)
727 int[] region = hiddenColumns.get(i);
728 for (int j = region[0]; j < region[1] + 1; j++)
735 hiddenColumns = null;
738 public void revealHiddenColumns(int res)
740 for (int i = 0; i < hiddenColumns.size(); i++)
742 int[] region = hiddenColumns.get(i);
743 if (res == region[0])
745 for (int j = region[0]; j < region[1] + 1; j++)
750 hiddenColumns.remove(region);
754 if (hiddenColumns.size() == 0)
756 hiddenColumns = null;
760 public boolean isVisible(int column)
762 if (hiddenColumns != null)
764 for (int i = 0; i < hiddenColumns.size(); i++)
766 int[] region = hiddenColumns.get(i);
767 if (column >= region[0] && column <= region[1])
782 public ColumnSelection(ColumnSelection copy)
786 if (copy.selected != null)
788 selected = new ArrayList<Integer>();
789 for (int i = 0, j = copy.selected.size(); i < j; i++)
791 selected.add(copy.selected.get(i));
794 if (copy.hiddenColumns != null)
796 hiddenColumns = new ArrayList<int[]>(copy.hiddenColumns.size());
797 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
800 rh = copy.hiddenColumns.get(i);
803 cp = new int[rh.length];
804 System.arraycopy(rh, 0, cp, 0, rh.length);
805 hiddenColumns.add(cp);
815 public ColumnSelection()
819 public String[] getVisibleSequenceStrings(int start, int end,
822 int i, iSize = seqs.length;
823 String selection[] = new String[iSize];
824 if (hiddenColumns != null && hiddenColumns.size() > 0)
826 for (i = 0; i < iSize; i++)
828 StringBuffer visibleSeq = new StringBuffer();
829 List<int[]> regions = getHiddenColumns();
831 int blockStart = start, blockEnd = end;
832 int hideStart, hideEnd;
834 for (int[] region : regions)
836 hideStart = region[0];
839 if (hideStart < start)
844 blockStart = Math.min(blockStart, hideEnd + 1);
845 blockEnd = Math.min(blockEnd, hideStart);
847 if (blockStart > blockEnd)
852 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
854 blockStart = hideEnd + 1;
858 if (end > blockStart)
860 visibleSeq.append(seqs[i].getSequence(blockStart, end));
863 selection[i] = visibleSeq.toString();
868 for (i = 0; i < iSize; i++)
870 selection[i] = seqs[i].getSequenceAsString(start, end);
878 * return all visible segments between the given start and end boundaries
881 * (first column inclusive from 0)
883 * (last column - not inclusive)
884 * @return int[] {i_start, i_end, ..} where intervals lie in
885 * start<=i_start<=i_end<end
887 public int[] getVisibleContigs(int start, int end)
889 if (hiddenColumns != null && hiddenColumns.size() > 0)
891 List<int[]> visiblecontigs = new ArrayList<int[]>();
892 List<int[]> regions = getHiddenColumns();
896 int hideStart, hideEnd;
898 for (int j = 0; vstart < end && j < regions.size(); j++)
900 region = regions.get(j);
901 hideStart = region[0];
904 if (hideEnd < vstart)
908 if (hideStart > vstart)
910 visiblecontigs.add(new int[]
911 { vstart, hideStart - 1 });
913 vstart = hideEnd + 1;
918 visiblecontigs.add(new int[]
919 { vstart, end - 1 });
921 int[] vcontigs = new int[visiblecontigs.size() * 2];
922 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
924 int[] vc = visiblecontigs.get(i);
925 visiblecontigs.set(i, null);
926 vcontigs[i * 2] = vc[0];
927 vcontigs[i * 2 + 1] = vc[1];
929 visiblecontigs.clear();
940 * delete any columns in alignmentAnnotation that are hidden (including
941 * sequence associated annotation).
943 * @param alignmentAnnotation
945 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
947 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
951 * delete any columns in alignmentAnnotation that are hidden (including
952 * sequence associated annotation).
955 * remove any annotation to the right of this column
957 * remove any annotation to the left of this column
958 * @param alignmentAnnotation
959 * the annotation to operate on
961 public void makeVisibleAnnotation(int start, int end,
962 AlignmentAnnotation alignmentAnnotation)
964 if (alignmentAnnotation.annotations == null)
968 if (start == end && end == -1)
971 end = alignmentAnnotation.annotations.length;
973 if (hiddenColumns != null && hiddenColumns.size() > 0)
975 // then mangle the alignmentAnnotation annotation array
976 List<Annotation[]> annels = new ArrayList<Annotation[]>();
977 Annotation[] els = null;
978 List<int[]> regions = getHiddenColumns();
979 int blockStart = start, blockEnd = end;
980 int hideStart, hideEnd, w = 0;
982 for (int[] region : regions)
984 hideStart = region[0];
987 if (hideStart < start)
992 blockStart = Math.min(blockStart, hideEnd + 1);
993 blockEnd = Math.min(blockEnd, hideStart);
995 if (blockStart > blockEnd)
1000 annels.add(els = new Annotation[blockEnd - blockStart]);
1001 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1004 blockStart = hideEnd + 1;
1008 if (end > blockStart)
1010 annels.add(els = new Annotation[end - blockStart + 1]);
1011 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
1013 // copy just the visible segment of the annotation row
1014 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1015 els, 0, els.length);
1019 // copy to the end of the annotation row
1020 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1022 (alignmentAnnotation.annotations.length - blockStart));
1030 for (Annotation[] chnk : annels)
1032 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1039 alignmentAnnotation.restrict(start, end);
1044 * Invert the column selection from first to end-1. leaves hiddenColumns
1045 * untouched (and unselected)
1050 public void invertColumnSelection(int first, int width)
1052 boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;
1053 for (int i = first; i < width; i++)
1061 if (!hasHidden || isVisible(i))
1070 * add in any unselected columns from the given column selection, excluding
1071 * any that are hidden.
1075 public void addElementsFrom(ColumnSelection colsel)
1077 if (colsel != null && colsel.size() > 0)
1079 for (Integer eo : colsel.getSelected())
1081 if (hiddenColumns != null && isVisible(eo))
1083 if (!selected.contains(eo))
1093 * set the selected columns the given column selection, excluding any columns
1098 public void setElementsFrom(ColumnSelection colsel)
1100 selected = new ArrayList<Integer>();
1101 if (colsel.selected != null && colsel.selected.size() > 0)
1103 if (hiddenColumns != null && hiddenColumns.size() > 0)
1105 // only select visible columns in this columns selection
1106 selected = new ArrayList<Integer>();
1107 addElementsFrom(colsel);
1111 // add everything regardless
1112 for (Integer e : colsel.getSelected())
1121 * Add gaps into the sequences aligned to profileseq under the given
1126 * - alignment to have gaps inserted into it
1128 * - alignment view where sequence corresponding to profileseq is
1130 * @return new Column selection for new alignment view, with insertions into
1131 * profileseq marked as hidden.
1133 public static ColumnSelection propagateInsertions(SequenceI profileseq,
1134 Alignment al, AlignmentView input)
1138 // return propagateInsertions(profileseq, al, )
1139 char gc = al.getGapCharacter();
1140 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
1141 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
1142 SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];
1143 nview.propagateInsertions(profileseq, al, origseq);
1150 * - sequence in al which corresponds to origseq
1152 * - alignment which is to have gaps inserted into it
1154 * - sequence corresponding to profileseq which defines gap map for
1157 public void propagateInsertions(SequenceI profileseq, AlignmentI al,
1160 char gc = al.getGapCharacter();
1161 // recover mapping between sequence's non-gap positions and positions
1163 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1164 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1167 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
1168 // alandcolsel[0])[0].gapMap()))
1169 // add profile to visible contigs
1170 for (int v = 0; v < viscontigs.length; v += 2)
1172 if (viscontigs[v] > spos)
1174 StringBuilder sb = new StringBuilder(64);
1175 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1179 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1181 SequenceI sqobj = al.getSequenceAt(s);
1182 if (sqobj != profileseq)
1184 String sq = al.getSequenceAt(s).getSequenceAsString();
1185 if (sq.length() <= spos + offset)
1188 int diff = spos + offset - sq.length() - 1;
1193 while ((diff = spos + offset - sq.length() - 1) > 0)
1196 // + ((diff >= sb.length()) ? sb.toString() : sb
1197 // .substring(0, diff));
1198 if (diff >= sb.length())
1200 sq += sb.toString();
1204 char[] buf = new char[diff];
1205 sb.getChars(0, diff, buf, 0);
1206 sq += buf.toString();
1210 sq += sb.toString();
1214 al.getSequenceAt(s).setSequence(
1215 sq.substring(0, spos + offset) + sb.toString()
1216 + sq.substring(spos + offset));
1220 // offset+=sb.length();
1222 spos = viscontigs[v + 1] + 1;
1224 if ((offset + spos) < profileseq.getLength())
1226 // pad the final region with gaps.
1227 StringBuffer sb = new StringBuffer();
1228 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1232 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1234 SequenceI sqobj = al.getSequenceAt(s);
1235 if (sqobj == profileseq)
1239 String sq = sqobj.getSequenceAsString();
1241 int diff = origseq.getLength() - sq.length();
1245 // + ((diff >= sb.length()) ? sb.toString() : sb
1246 // .substring(0, diff));
1247 if (diff >= sb.length())
1249 sq += sb.toString();
1253 char[] buf = new char[diff];
1254 sb.getChars(0, diff, buf, 0);
1255 sq += buf.toString();
1257 diff = origseq.getLength() - sq.length();