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;
25 import jalview.util.*;
28 * NOTE: Columns are zero based.
30 public class ColumnSelection
32 Vector selected = new Vector();
34 // Vector of int [] {startCol, endCol}
38 * Add a column to the selection
43 public void addElement(int col)
45 Integer column = new Integer(col);
46 if (!selected.contains(column))
48 selected.addElement(column);
53 * clears column selection
57 selected.removeAllElements();
61 * removes col from selection
64 * index of column to be removed
66 public void removeElement(int col)
68 Integer colInt = new Integer(col);
70 if (selected.contains(colInt))
72 selected.removeElement(colInt);
77 * removes a range of columns from the selection
80 * int - first column in range to be removed
84 public void removeElements(int start, int end)
87 for (int i = start; i < end; i++)
89 colInt = new Integer(i);
90 if (selected.contains(colInt))
92 selected.removeElement(colInt);
99 * @return Vector containing selected columns as Integers
101 public Vector getSelected()
109 * index to search for in column selection
111 * @return true if Integer(col) is in selection.
113 public boolean contains(int col)
115 return selected.contains(new Integer(col));
119 * Column number at position i in selection
122 * index into selected columns
124 * @return column number in alignment
126 public int columnAt(int i)
128 return ((Integer) selected.elementAt(i)).intValue();
134 * @return DOCUMENT ME!
138 return selected.size();
142 * rightmost selected column
144 * @return rightmost column in alignment that is selected
150 for (int i = 0; i < selected.size(); i++)
152 if (columnAt(i) > max)
162 * Leftmost column in selection
164 * @return column index of leftmost column in selection
168 int min = 1000000000;
170 for (int i = 0; i < selected.size(); i++)
172 if (columnAt(i) < min)
182 * propagate shift in alignment columns to column selection
187 * shift in edit (+ve for removal, or -ve for inserts)
189 public Vector compensateForEdit(int start, int change)
191 Vector deletedHiddenColumns = null;
192 for (int i = 0; i < size(); i++)
194 int temp = columnAt(i);
198 selected.setElementAt(new Integer(temp - change), i);
202 if (hiddenColumns != null)
204 deletedHiddenColumns = new Vector();
205 int hSize = hiddenColumns.size();
206 for (int i = 0; i < hSize; i++)
208 int[] region = (int[]) hiddenColumns.elementAt(i);
209 if (region[0] > start && start + change > region[1])
211 deletedHiddenColumns.addElement(hiddenColumns.elementAt(i));
213 hiddenColumns.removeElementAt(i);
219 if (region[0] > start)
232 this.revealHiddenColumns(0);
235 return deletedHiddenColumns;
239 * propagate shift in alignment columns to column selection special version of
240 * compensateForEdit - allowing for edits within hidden regions
245 * shift in edit (+ve for removal, or -ve for inserts)
247 private void compensateForDelEdits(int start, int change)
249 for (int i = 0; i < size(); i++)
251 int temp = columnAt(i);
255 selected.setElementAt(new Integer(temp - change), i);
259 if (hiddenColumns != null)
261 for (int i = 0; i < hiddenColumns.size(); i++)
263 int[] region = (int[]) hiddenColumns.elementAt(i);
264 if (region[0] >= start)
268 if (region[1] >= start)
272 if (region[1] < region[0])
274 hiddenColumns.removeElementAt(i--);
290 * Adjust hidden column boundaries based on a series of column additions or
291 * deletions in visible regions.
296 public ShiftList compensateForEdits(ShiftList shiftrecord)
298 if (shiftrecord != null)
300 Vector shifts = shiftrecord.shifts;
301 if (shifts != null && shifts.size() > 0)
304 for (int i = 0, j = shifts.size(); i < j; i++)
306 int[] sh = (int[]) shifts.elementAt(i);
307 // compensateForEdit(shifted+sh[0], sh[1]);
308 compensateForDelEdits(shifted + sh[0], sh[1]);
312 return shiftrecord.getInverse();
318 * removes intersection of position,length ranges in deletions from the
319 * start,end regions marked in intervals.
325 private boolean pruneIntervalVector(Vector deletions, Vector intervals)
327 boolean pruned = false;
328 int i = 0, j = intervals.size() - 1, s = 0, t = deletions.size() - 1;
329 int hr[] = (int[]) intervals.elementAt(i);
330 int sr[] = (int[]) deletions.elementAt(s);
331 while (i <= j && s <= t)
333 boolean trailinghn = hr[1] >= sr[0];
338 hr = (int[]) intervals.elementAt(++i);
346 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
347 if (endshift < hr[0] || endshift < sr[0])
348 { // leadinghc disjoint or not a deletion
351 sr = (int[]) deletions.elementAt(++s);
359 boolean leadinghn = hr[0] >= sr[0];
360 boolean leadinghc = hr[0] < endshift;
361 boolean trailinghc = hr[1] < endshift;
365 { // deleted hidden region.
366 intervals.removeElementAt(i);
371 hr = (int[]) intervals.elementAt(i);
377 hr[0] = endshift; // clip c terminal region
378 leadinghn = !leadinghn;
394 // sr contained in hr
397 sr = (int[]) deletions.elementAt(++s);
407 return pruned; // true if any interval was removed or modified by
411 private boolean pruneColumnList(Vector deletion, Vector list)
413 int s = 0, t = deletion.size();
414 int[] sr = (int[]) list.elementAt(s++);
415 boolean pruned = false;
416 int i = 0, j = list.size();
417 while (i < j && s <= t)
419 int c = ((Integer) list.elementAt(i++)).intValue();
422 if (sr[1] + sr[0] >= c)
423 { // sr[1] -ve means inseriton.
424 list.removeElementAt(--i);
431 sr = (int[]) deletion.elementAt(s);
441 * remove any hiddenColumns or selected columns and shift remaining based on a
442 * series of position, range deletions.
446 public void pruneDeletions(ShiftList deletions)
448 if (deletions != null)
450 Vector shifts = deletions.shifts;
451 if (shifts != null && shifts.size() > 0)
453 // delete any intervals intersecting.
454 if (hiddenColumns != null)
456 pruneIntervalVector(shifts, hiddenColumns);
457 if (hiddenColumns != null && hiddenColumns.size() == 0)
459 hiddenColumns = null;
462 if (selected != null && selected.size() > 0)
464 pruneColumnList(shifts, selected);
465 if (selected != null && selected.size() == 0)
470 // and shift the rest.
471 this.compensateForEdits(deletions);
477 * This Method is used to return all the HiddenColumn regions less than the
484 public Vector getHiddenColumns()
486 return hiddenColumns;
490 * Return absolute column index for a visible column index
493 * int column index in alignment view
494 * @return alignment column index for column
496 public int adjustForHiddenColumns(int column)
499 if (hiddenColumns != null)
501 for (int i = 0; i < hiddenColumns.size(); i++)
503 int[] region = (int[]) hiddenColumns.elementAt(i);
504 if (result >= region[0])
506 result += region[1] - region[0] + 1;
514 * Use this method to find out where a column will appear in the visible
515 * alignment when hidden columns exist. If the column is not visible, then the
516 * left-most visible column will always be returned.
518 * @param hiddenColumn
522 public int findColumnPosition(int hiddenColumn)
524 int result = hiddenColumn;
525 if (hiddenColumns != null)
531 region = (int[]) hiddenColumns.elementAt(index++);
532 if (hiddenColumn > region[1])
534 result -= region[1] + 1 - region[0];
536 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
537 if (hiddenColumn > region[0] && hiddenColumn < region[1])
539 return region[0] + hiddenColumn - result;
542 return result; // return the shifted position after removing hidden columns.
546 * Use this method to determine where the next hiddenRegion starts
548 public int findHiddenRegionPosition(int hiddenRegion)
551 if (hiddenColumns != null)
557 int[] region = (int[]) hiddenColumns.elementAt(index);
558 if (hiddenRegion == 0)
563 gaps += region[1] + 1 - region[0];
564 result = region[1] + 1;
566 } while (index < hiddenRegion + 1);
575 * THis method returns the rightmost limit of a region of an alignment with
576 * hidden columns. In otherwords, the next hidden column.
581 public int getHiddenBoundaryRight(int alPos)
583 if (hiddenColumns != null)
588 int[] region = (int[]) hiddenColumns.elementAt(index);
589 if (alPos < region[0])
595 } while (index < hiddenColumns.size());
603 * This method returns the leftmost limit of a region of an alignment with
604 * hidden columns. In otherwords, the previous hidden column.
609 public int getHiddenBoundaryLeft(int alPos)
611 if (hiddenColumns != null)
613 int index = hiddenColumns.size() - 1;
616 int[] region = (int[]) hiddenColumns.elementAt(index);
617 if (alPos > region[1])
623 } while (index > -1);
630 public void hideSelectedColumns()
634 int column = ((Integer) getSelected().firstElement()).intValue();
640 public void hideColumns(int start, int end)
642 if (hiddenColumns == null)
644 hiddenColumns = new Vector();
647 boolean added = false;
648 boolean overlap = false;
650 for (int i = 0; i < hiddenColumns.size(); i++)
652 int[] region = (int[]) hiddenColumns.elementAt(i);
653 if (start <= region[1] && end >= region[0])
655 hiddenColumns.removeElementAt(i);
659 else if (end < region[0] && start < region[0])
661 hiddenColumns.insertElementAt(new int[]
670 hideColumns(start, end);
674 hiddenColumns.addElement(new int[]
681 * This method will find a range of selected columns around the column
687 public void hideColumns(int col)
689 // First find out range of columns to hide
690 int min = col, max = col + 1;
691 while (contains(min))
697 while (contains(max))
710 hideColumns(min, max);
713 public void revealAllHiddenColumns()
715 if (hiddenColumns != null)
717 for (int i = 0; i < hiddenColumns.size(); i++)
719 int[] region = (int[]) hiddenColumns.elementAt(i);
720 for (int j = region[0]; j < region[1] + 1; j++)
727 hiddenColumns = null;
730 public void revealHiddenColumns(int res)
732 for (int i = 0; i < hiddenColumns.size(); i++)
734 int[] region = (int[]) hiddenColumns.elementAt(i);
735 if (res == region[0])
737 for (int j = region[0]; j < region[1] + 1; j++)
742 hiddenColumns.removeElement(region);
746 if (hiddenColumns.size() == 0)
748 hiddenColumns = null;
752 public boolean isVisible(int column)
754 if (hiddenColumns != null)
755 for (int i = 0; i < hiddenColumns.size(); i++)
757 int[] region = (int[]) hiddenColumns.elementAt(i);
758 if (column >= region[0] && column <= region[1])
772 public ColumnSelection(ColumnSelection copy)
776 if (copy.selected != null)
778 selected = new Vector();
779 for (int i = 0, j = copy.selected.size(); i < j; i++)
781 selected.addElement(copy.selected.elementAt(i));
784 if (copy.hiddenColumns != null)
786 hiddenColumns = new Vector(copy.hiddenColumns.size());
787 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
790 rh = (int[]) copy.hiddenColumns.elementAt(i);
793 cp = new int[rh.length];
794 System.arraycopy(rh, 0, cp, 0, rh.length);
795 hiddenColumns.addElement(cp);
805 public ColumnSelection()
809 public String[] getVisibleSequenceStrings(int start, int end,
812 int i, iSize = seqs.length;
813 String selection[] = new String[iSize];
814 if (hiddenColumns != null && hiddenColumns.size() > 0)
816 for (i = 0; i < iSize; i++)
818 StringBuffer visibleSeq = new StringBuffer();
819 Vector regions = getHiddenColumns();
821 int blockStart = start, blockEnd = end;
823 int hideStart, hideEnd;
825 for (int j = 0; j < regions.size(); j++)
827 region = (int[]) regions.elementAt(j);
828 hideStart = region[0];
831 if (hideStart < start)
836 blockStart = Math.min(blockStart, hideEnd + 1);
837 blockEnd = Math.min(blockEnd, hideStart);
839 if (blockStart > blockEnd)
844 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
846 blockStart = hideEnd + 1;
850 if (end > blockStart)
852 visibleSeq.append(seqs[i].getSequence(blockStart, end));
855 selection[i] = visibleSeq.toString();
860 for (i = 0; i < iSize; i++)
862 selection[i] = seqs[i].getSequenceAsString(start, end);
870 * return all visible segments between the given start and end boundaries
873 * (first column inclusive from 0)
875 * (last column - not inclusive)
876 * @return int[] {i_start, i_end, ..} where intervals lie in
877 * start<=i_start<=i_end<end
879 public int[] getVisibleContigs(int start, int end)
881 if (hiddenColumns != null && hiddenColumns.size() > 0)
883 Vector visiblecontigs = new Vector();
884 Vector regions = getHiddenColumns();
888 int hideStart, hideEnd;
890 for (int j = 0; vstart < end && j < regions.size(); j++)
892 region = (int[]) regions.elementAt(j);
893 hideStart = region[0];
896 if (hideEnd < vstart)
900 if (hideStart > vstart)
902 visiblecontigs.addElement(new int[]
903 { vstart, hideStart - 1 });
905 vstart = hideEnd + 1;
910 visiblecontigs.addElement(new int[]
911 { vstart, end - 1 });
913 int[] vcontigs = new int[visiblecontigs.size() * 2];
914 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
916 int[] vc = (int[]) visiblecontigs.elementAt(i);
917 visiblecontigs.setElementAt(null, i);
918 vcontigs[i * 2] = vc[0];
919 vcontigs[i * 2 + 1] = vc[1];
921 visiblecontigs.removeAllElements();
932 * delete any columns in alignmentAnnotation that are hidden (including
933 * sequence associated annotation).
935 * @param alignmentAnnotation
937 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
939 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
943 * delete any columns in alignmentAnnotation that are hidden (including
944 * sequence associated annotation).
947 * remove any annotation to the right of this column
949 * remove any annotation to the left of this column
950 * @param alignmentAnnotation
951 * the annotation to operate on
953 public void makeVisibleAnnotation(int start, int end,
954 AlignmentAnnotation alignmentAnnotation)
956 if (alignmentAnnotation.annotations == null)
960 if (start == end && end == -1)
963 end = alignmentAnnotation.annotations.length;
965 if (hiddenColumns != null && hiddenColumns.size() > 0)
967 // then mangle the alignmentAnnotation annotation array
968 Vector annels = new Vector();
969 Annotation[] els = null;
970 Vector regions = getHiddenColumns();
971 int blockStart = start, blockEnd = end;
973 int hideStart, hideEnd, w = 0;
975 for (int j = 0; j < regions.size(); j++)
977 region = (int[]) regions.elementAt(j);
978 hideStart = region[0];
981 if (hideStart < start)
986 blockStart = Math.min(blockStart, hideEnd + 1);
987 blockEnd = Math.min(blockEnd, hideStart);
989 if (blockStart > blockEnd)
994 annels.addElement(els = new Annotation[blockEnd - blockStart]);
995 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
998 blockStart = hideEnd + 1;
1002 if (end > blockStart)
1004 annels.addElement(els = new Annotation[end - blockStart + 1]);
1005 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
1007 // copy just the visible segment of the annotation row
1008 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1009 els, 0, els.length);
1013 // copy to the end of the annotation row
1014 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1016 (alignmentAnnotation.annotations.length - blockStart));
1022 Enumeration e = annels.elements();
1023 alignmentAnnotation.annotations = new Annotation[w];
1025 while (e.hasMoreElements())
1027 Annotation[] chnk = (Annotation[]) e.nextElement();
1028 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1035 alignmentAnnotation.restrict(start, end);
1040 * Invert the column selection from first to end-1. leaves hiddenColumns
1041 * untouched (and unselected)
1046 public void invertColumnSelection(int first, int width)
1048 boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;
1049 for (int i = first; i < width; i++)
1057 if (!hasHidden || isVisible(i))
1066 * add in any unselected columns from the given column selection, excluding
1067 * any that are hidden.
1071 public void addElementsFrom(ColumnSelection colsel)
1073 if (colsel != null && colsel.size() > 0)
1075 Enumeration e = colsel.getSelected().elements();
1076 while (e.hasMoreElements())
1078 Object eo = e.nextElement();
1079 if (hiddenColumns != null && isVisible(((Integer) eo).intValue()))
1081 if (!selected.contains(eo))
1083 selected.addElement(eo);
1091 * set the selected columns the given column selection, excluding any columns
1096 public void setElementsFrom(ColumnSelection colsel)
1098 selected = new Vector();
1099 if (colsel.selected != null && colsel.selected.size() > 0)
1101 if (hiddenColumns != null && hiddenColumns.size() > 0)
1103 // only select visible columns in this columns selection
1104 selected = new Vector();
1105 addElementsFrom(colsel);
1109 // add everything regardless
1110 Enumeration en = colsel.selected.elements();
1111 while (en.hasMoreElements())
1113 selected.addElement(en.nextElement());
1120 * Add gaps into the sequences aligned to profileseq under the given
1125 * - alignment to have gaps inserted into it
1127 * - alignment view where sequence corresponding to profileseq is
1129 * @return new Column selection for new alignment view, with insertions into
1130 * profileseq marked as hidden.
1132 public static ColumnSelection propagateInsertions(SequenceI profileseq,
1133 Alignment al, AlignmentView input)
1137 // return propagateInsertions(profileseq, al, )
1138 char gc = al.getGapCharacter();
1139 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
1140 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
1141 SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];
1142 nview.propagateInsertions(profileseq, al, origseq);
1149 * - sequence in al which corresponds to origseq
1151 * - alignment which is to have gaps inserted into it
1153 * - sequence corresponding to profileseq which defines gap map for
1156 public void propagateInsertions(SequenceI profileseq, AlignmentI al,
1159 char gc = al.getGapCharacter();
1160 // recover mapping between sequence's non-gap positions and positions
1162 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1163 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1166 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
1167 // alandcolsel[0])[0].gapMap()))
1168 // add profile to visible contigs
1169 for (int v = 0; v < viscontigs.length; v += 2)
1171 if (viscontigs[v] > spos)
1173 StringBuffer sb = new StringBuffer();
1174 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1178 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1180 SequenceI sqobj = al.getSequenceAt(s);
1181 if (sqobj != profileseq)
1183 String sq = al.getSequenceAt(s).getSequenceAsString();
1184 if (sq.length() <= spos + offset)
1187 int diff = spos + offset - sq.length() - 1;
1192 while ((diff = spos + offset - sq.length() - 1) > 0)
1195 // + ((diff >= sb.length()) ? sb.toString() : sb
1196 // .substring(0, diff));
1197 if (diff >= sb.length())
1199 sq += sb.toString();
1203 char[] buf = new char[diff];
1204 sb.getChars(0, diff, buf, 0);
1205 sq += buf.toString();
1209 sq += sb.toString();
1213 al.getSequenceAt(s).setSequence(
1214 sq.substring(0, spos + offset) + sb.toString()
1215 + sq.substring(spos + offset));
1219 // offset+=sb.length();
1221 spos = viscontigs[v + 1] + 1;
1223 if ((offset + spos) < profileseq.getLength())
1225 // pad the final region with gaps.
1226 StringBuffer sb = new StringBuffer();
1227 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1231 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1233 SequenceI sqobj = al.getSequenceAt(s);
1234 if (sqobj == profileseq)
1238 String sq = sqobj.getSequenceAsString();
1240 int diff = origseq.getLength() - sq.length();
1244 // + ((diff >= sb.length()) ? sb.toString() : sb
1245 // .substring(0, diff));
1246 if (diff >= sb.length())
1248 sq += sb.toString();
1252 char[] buf = new char[diff];
1253 sb.getChars(0, diff, buf, 0);
1254 sq += buf.toString();
1256 diff = origseq.getLength() - sq.length();