2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.datamodel;
23 import jalview.util.*;
26 * NOTE: Columns are zero based.
28 public class ColumnSelection
30 Vector selected = new Vector();
32 // Vector of int [] {startCol, endCol}
36 * Add a column to the selection
41 public void addElement(int col)
43 Integer column = new Integer(col);
44 if (!selected.contains(column))
46 selected.addElement(column);
51 * clears column selection
55 selected.removeAllElements();
59 * removes col from selection
62 * index of column to be removed
64 public void removeElement(int col)
66 Integer colInt = new Integer(col);
68 if (selected.contains(colInt))
70 selected.removeElement(colInt);
75 * removes a range of columns from the selection
78 * int - first column in range to be removed
82 public void removeElements(int start, int end)
85 for (int i = start; i < end; i++)
87 colInt = new Integer(i);
88 if (selected.contains(colInt))
90 selected.removeElement(colInt);
97 * @return Vector containing selected columns as Integers
99 public Vector getSelected()
107 * index to search for in column selection
109 * @return true if Integer(col) is in selection.
111 public boolean contains(int col)
113 return selected.contains(new Integer(col));
117 * Column number at position i in selection
120 * index into selected columns
122 * @return column number in alignment
124 public int columnAt(int i)
126 return ((Integer) selected.elementAt(i)).intValue();
132 * @return DOCUMENT ME!
136 return selected.size();
140 * rightmost selected column
142 * @return rightmost column in alignment that is selected
148 for (int i = 0; i < selected.size(); i++)
150 if (columnAt(i) > max)
160 * Leftmost column in selection
162 * @return column index of leftmost column in selection
166 int min = 1000000000;
168 for (int i = 0; i < selected.size(); i++)
170 if (columnAt(i) < min)
180 * propagate shift in alignment columns to column selection
185 * shift in edit (+ve for removal, or -ve for inserts)
187 public Vector compensateForEdit(int start, int change)
189 Vector deletedHiddenColumns = null;
190 for (int i = 0; i < size(); i++)
192 int temp = columnAt(i);
196 selected.setElementAt(new Integer(temp - change), i);
200 if (hiddenColumns != null)
202 deletedHiddenColumns = new Vector();
203 int hSize = hiddenColumns.size();
204 for (int i = 0; i < hSize; i++)
206 int[] region = (int[]) hiddenColumns.elementAt(i);
207 if (region[0] > start && start + change > region[1])
209 deletedHiddenColumns.addElement(hiddenColumns.elementAt(i));
211 hiddenColumns.removeElementAt(i);
217 if (region[0] > start)
230 this.revealHiddenColumns(0);
233 return deletedHiddenColumns;
237 * propagate shift in alignment columns to column selection special version of
238 * compensateForEdit - allowing for edits within hidden regions
243 * shift in edit (+ve for removal, or -ve for inserts)
245 private void compensateForDelEdits(int start, int change)
247 for (int i = 0; i < size(); i++)
249 int temp = columnAt(i);
253 selected.setElementAt(new Integer(temp - change), i);
257 if (hiddenColumns != null)
259 for (int i = 0; i < hiddenColumns.size(); i++)
261 int[] region = (int[]) hiddenColumns.elementAt(i);
262 if (region[0] >= start)
266 if (region[1] >= start)
270 if (region[1] < region[0])
272 hiddenColumns.removeElementAt(i--);
288 * Adjust hidden column boundaries based on a series of column additions or
289 * deletions in visible regions.
294 public ShiftList compensateForEdits(ShiftList shiftrecord)
296 if (shiftrecord != null)
298 Vector shifts = shiftrecord.shifts;
299 if (shifts != null && shifts.size() > 0)
302 for (int i = 0, j = shifts.size(); i < j; i++)
304 int[] sh = (int[]) shifts.elementAt(i);
305 // compensateForEdit(shifted+sh[0], sh[1]);
306 compensateForDelEdits(shifted + sh[0], sh[1]);
310 return shiftrecord.getInverse();
316 * removes intersection of position,length ranges in deletions from the
317 * start,end regions marked in intervals.
323 private boolean pruneIntervalVector(Vector deletions, Vector intervals)
325 boolean pruned = false;
326 int i = 0, j = intervals.size() - 1, s = 0, t = deletions.size() - 1;
327 int hr[] = (int[]) intervals.elementAt(i);
328 int sr[] = (int[]) deletions.elementAt(s);
329 while (i <= j && s <= t)
331 boolean trailinghn = hr[1] >= sr[0];
336 hr = (int[]) intervals.elementAt(++i);
344 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
345 if (endshift < hr[0] || endshift < sr[0])
346 { // leadinghc disjoint or not a deletion
349 sr = (int[]) deletions.elementAt(++s);
357 boolean leadinghn = hr[0] >= sr[0];
358 boolean leadinghc = hr[0] < endshift;
359 boolean trailinghc = hr[1] < endshift;
363 { // deleted hidden region.
364 intervals.removeElementAt(i);
369 hr = (int[]) intervals.elementAt(i);
375 hr[0] = endshift; // clip c terminal region
376 leadinghn = !leadinghn;
392 // sr contained in hr
395 sr = (int[]) deletions.elementAt(++s);
405 return pruned; // true if any interval was removed or modified by
409 private boolean pruneColumnList(Vector deletion, Vector list)
411 int s = 0, t = deletion.size();
412 int[] sr = (int[]) list.elementAt(s++);
413 boolean pruned = false;
414 int i = 0, j = list.size();
415 while (i < j && s <= t)
417 int c = ((Integer) list.elementAt(i++)).intValue();
420 if (sr[1] + sr[0] >= c)
421 { // sr[1] -ve means inseriton.
422 list.removeElementAt(--i);
429 sr = (int[]) deletion.elementAt(s);
439 * remove any hiddenColumns or selected columns and shift remaining based on a
440 * series of position, range deletions.
444 public void pruneDeletions(ShiftList deletions)
446 if (deletions != null)
448 Vector shifts = deletions.shifts;
449 if (shifts != null && shifts.size() > 0)
451 // delete any intervals intersecting.
452 if (hiddenColumns != null)
454 pruneIntervalVector(shifts, hiddenColumns);
455 if (hiddenColumns != null && hiddenColumns.size() == 0)
457 hiddenColumns = null;
460 if (selected != null && selected.size() > 0)
462 pruneColumnList(shifts, selected);
463 if (selected != null && selected.size() == 0)
468 // and shift the rest.
469 this.compensateForEdits(deletions);
475 * This Method is used to return all the HiddenColumn regions less than the
482 public Vector getHiddenColumns()
484 return hiddenColumns;
488 * Return absolute column index for a visible column index
491 * int column index in alignment view
492 * @return alignment column index for column
494 public int adjustForHiddenColumns(int column)
497 if (hiddenColumns != null)
499 for (int i = 0; i < hiddenColumns.size(); i++)
501 int[] region = (int[]) hiddenColumns.elementAt(i);
502 if (result >= region[0])
504 result += region[1] - region[0] + 1;
512 * Use this method to find out where a column will appear in the visible
513 * alignment when hidden columns exist. If the column is not visible, then the
514 * left-most visible column will always be returned.
516 * @param hiddenColumn
520 public int findColumnPosition(int hiddenColumn)
522 int result = hiddenColumn;
523 if (hiddenColumns != null)
529 region = (int[]) hiddenColumns.elementAt(index++);
530 if (hiddenColumn > region[1])
532 result -= region[1] + 1 - region[0];
534 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
535 if (hiddenColumn > region[0] && hiddenColumn < region[1])
537 return region[0] + hiddenColumn - result;
540 return result; // return the shifted position after removing hidden columns.
544 * Use this method to determine where the next hiddenRegion starts
546 public int findHiddenRegionPosition(int hiddenRegion)
549 if (hiddenColumns != null)
555 int[] region = (int[]) hiddenColumns.elementAt(index);
556 if (hiddenRegion == 0)
561 gaps += region[1] + 1 - region[0];
562 result = region[1] + 1;
564 } while (index < hiddenRegion + 1);
573 * THis method returns the rightmost limit of a region of an alignment with
574 * hidden columns. In otherwords, the next hidden column.
579 public int getHiddenBoundaryRight(int alPos)
581 if (hiddenColumns != null)
586 int[] region = (int[]) hiddenColumns.elementAt(index);
587 if (alPos < region[0])
593 } while (index < hiddenColumns.size());
601 * This method returns the leftmost limit of a region of an alignment with
602 * hidden columns. In otherwords, the previous hidden column.
607 public int getHiddenBoundaryLeft(int alPos)
609 if (hiddenColumns != null)
611 int index = hiddenColumns.size() - 1;
614 int[] region = (int[]) hiddenColumns.elementAt(index);
615 if (alPos > region[1])
621 } while (index > -1);
628 public void hideSelectedColumns()
632 int column = ((Integer) getSelected().firstElement()).intValue();
638 public void hideColumns(int start, int end)
640 if (hiddenColumns == null)
642 hiddenColumns = new Vector();
645 boolean added = false;
646 boolean overlap = false;
648 for (int i = 0; i < hiddenColumns.size(); i++)
650 int[] region = (int[]) hiddenColumns.elementAt(i);
651 if (start <= region[1] && end >= region[0])
653 hiddenColumns.removeElementAt(i);
657 else if (end < region[0] && start < region[0])
659 hiddenColumns.insertElementAt(new int[]
668 hideColumns(start, end);
672 hiddenColumns.addElement(new int[]
679 * This method will find a range of selected columns around the column
685 public void hideColumns(int col)
687 // First find out range of columns to hide
688 int min = col, max = col + 1;
689 while (contains(min))
695 while (contains(max))
708 hideColumns(min, max);
711 public void revealAllHiddenColumns()
713 if (hiddenColumns != null)
715 for (int i = 0; i < hiddenColumns.size(); i++)
717 int[] region = (int[]) hiddenColumns.elementAt(i);
718 for (int j = region[0]; j < region[1] + 1; j++)
725 hiddenColumns = null;
728 public void revealHiddenColumns(int res)
730 for (int i = 0; i < hiddenColumns.size(); i++)
732 int[] region = (int[]) hiddenColumns.elementAt(i);
733 if (res == region[0])
735 for (int j = region[0]; j < region[1] + 1; j++)
740 hiddenColumns.removeElement(region);
744 if (hiddenColumns.size() == 0)
746 hiddenColumns = null;
750 public boolean isVisible(int column)
752 if (hiddenColumns != null)
753 for (int i = 0; i < hiddenColumns.size(); i++)
755 int[] region = (int[]) hiddenColumns.elementAt(i);
756 if (column >= region[0] && column <= region[1])
770 public ColumnSelection(ColumnSelection copy)
774 if (copy.selected != null)
776 selected = new Vector();
777 for (int i = 0, j = copy.selected.size(); i < j; i++)
779 selected.addElement(copy.selected.elementAt(i));
782 if (copy.hiddenColumns != null)
784 hiddenColumns = new Vector(copy.hiddenColumns.size());
785 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
788 rh = (int[]) copy.hiddenColumns.elementAt(i);
791 cp = new int[rh.length];
792 System.arraycopy(rh, 0, cp, 0, rh.length);
793 hiddenColumns.addElement(cp);
803 public ColumnSelection()
807 public String[] getVisibleSequenceStrings(int start, int end,
810 int i, iSize = seqs.length;
811 String selection[] = new String[iSize];
812 if (hiddenColumns != null && hiddenColumns.size() > 0)
814 for (i = 0; i < iSize; i++)
816 StringBuffer visibleSeq = new StringBuffer();
817 Vector regions = getHiddenColumns();
819 int blockStart = start, blockEnd = end;
821 int hideStart, hideEnd;
823 for (int j = 0; j < regions.size(); j++)
825 region = (int[]) regions.elementAt(j);
826 hideStart = region[0];
829 if (hideStart < start)
834 blockStart = Math.min(blockStart, hideEnd + 1);
835 blockEnd = Math.min(blockEnd, hideStart);
837 if (blockStart > blockEnd)
842 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
844 blockStart = hideEnd + 1;
848 if (end > blockStart)
850 visibleSeq.append(seqs[i].getSequence(blockStart, end));
853 selection[i] = visibleSeq.toString();
858 for (i = 0; i < iSize; i++)
860 selection[i] = seqs[i].getSequenceAsString(start, end);
868 * return all visible segments between the given start and end boundaries
871 * (first column inclusive from 0)
873 * (last column - not inclusive)
874 * @return int[] {i_start, i_end, ..} where intervals lie in
875 * start<=i_start<=i_end<end
877 public int[] getVisibleContigs(int start, int end)
879 if (hiddenColumns != null && hiddenColumns.size() > 0)
881 Vector visiblecontigs = new Vector();
882 Vector regions = getHiddenColumns();
886 int hideStart, hideEnd;
888 for (int j = 0; vstart < end && j < regions.size(); j++)
890 region = (int[]) regions.elementAt(j);
891 hideStart = region[0];
894 if (hideEnd < vstart)
898 if (hideStart > vstart)
900 visiblecontigs.addElement(new int[]
901 { vstart, hideStart - 1 });
903 vstart = hideEnd + 1;
908 visiblecontigs.addElement(new int[]
909 { vstart, end - 1 });
911 int[] vcontigs = new int[visiblecontigs.size() * 2];
912 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
914 int[] vc = (int[]) visiblecontigs.elementAt(i);
915 visiblecontigs.setElementAt(null, i);
916 vcontigs[i * 2] = vc[0];
917 vcontigs[i * 2 + 1] = vc[1];
919 visiblecontigs.removeAllElements();
930 * delete any columns in alignmentAnnotation that are hidden (including
931 * sequence associated annotation).
933 * @param alignmentAnnotation
935 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
937 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
941 * delete any columns in alignmentAnnotation that are hidden (including
942 * sequence associated annotation).
945 * remove any annotation to the right of this column
947 * remove any annotation to the left of this column
948 * @param alignmentAnnotation
949 * the annotation to operate on
951 public void makeVisibleAnnotation(int start, int end,
952 AlignmentAnnotation alignmentAnnotation)
954 if (alignmentAnnotation.annotations == null)
958 if (start == end && end == -1)
961 end = alignmentAnnotation.annotations.length;
963 if (hiddenColumns != null && hiddenColumns.size() > 0)
965 // then mangle the alignmentAnnotation annotation array
966 Vector annels = new Vector();
967 Annotation[] els = null;
968 Vector regions = getHiddenColumns();
969 int blockStart = start, blockEnd = end;
971 int hideStart, hideEnd, w = 0;
973 for (int j = 0; j < regions.size(); j++)
975 region = (int[]) regions.elementAt(j);
976 hideStart = region[0];
979 if (hideStart < start)
984 blockStart = Math.min(blockStart, hideEnd + 1);
985 blockEnd = Math.min(blockEnd, hideStart);
987 if (blockStart > blockEnd)
992 annels.addElement(els = new Annotation[blockEnd - blockStart]);
993 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
996 blockStart = hideEnd + 1;
1000 if (end > blockStart)
1002 annels.addElement(els = new Annotation[end - blockStart + 1]);
1003 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
1005 // copy just the visible segment of the annotation row
1006 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1007 els, 0, els.length);
1011 // copy to the end of the annotation row
1012 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1014 (alignmentAnnotation.annotations.length - blockStart));
1020 Enumeration e = annels.elements();
1021 alignmentAnnotation.annotations = new Annotation[w];
1023 while (e.hasMoreElements())
1025 Annotation[] chnk = (Annotation[]) e.nextElement();
1026 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1033 alignmentAnnotation.restrict(start, end);
1038 * Invert the column selection from first to end-1. leaves hiddenColumns
1039 * untouched (and unselected)
1044 public void invertColumnSelection(int first, int width)
1046 boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;
1047 for (int i = first; i < width; i++)
1055 if (!hasHidden || isVisible(i))
1064 * add in any unselected columns from the given column selection, excluding
1065 * any that are hidden.
1069 public void addElementsFrom(ColumnSelection colsel)
1071 if (colsel != null && colsel.size() > 0)
1073 Enumeration e = colsel.getSelected().elements();
1074 while (e.hasMoreElements())
1076 Object eo = e.nextElement();
1077 if (hiddenColumns != null && isVisible(((Integer) eo).intValue()))
1079 if (!selected.contains(eo))
1081 selected.addElement(eo);
1089 * set the selected columns the given column selection, excluding any columns
1094 public void setElementsFrom(ColumnSelection colsel)
1096 selected = new Vector();
1097 if (colsel.selected != null && colsel.selected.size() > 0)
1099 if (hiddenColumns != null && hiddenColumns.size() > 0)
1101 // only select visible columns in this columns selection
1102 selected = new Vector();
1103 addElementsFrom(colsel);
1107 // add everything regardless
1108 Enumeration en = colsel.selected.elements();
1109 while (en.hasMoreElements())
1111 selected.addElement(en.nextElement());
1118 * Add gaps into the sequences aligned to profileseq under the given
1123 * - alignment to have gaps inserted into it
1125 * - alignment view where sequence corresponding to profileseq is
1127 * @return new Column selection for new alignment view, with insertions into
1128 * profileseq marked as hidden.
1130 public static ColumnSelection propagateInsertions(SequenceI profileseq,
1131 Alignment al, AlignmentView input)
1135 // return propagateInsertions(profileseq, al, )
1136 char gc = al.getGapCharacter();
1137 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
1138 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
1139 SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];
1140 nview.propagateInsertions(profileseq, al, origseq);
1147 * - sequence in al which corresponds to origseq
1149 * - alignment which is to have gaps inserted into it
1151 * - sequence corresponding to profileseq which defines gap map for
1154 public void propagateInsertions(SequenceI profileseq, AlignmentI al,
1157 char gc = al.getGapCharacter();
1158 // recover mapping between sequence's non-gap positions and positions
1160 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1161 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1164 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
1165 // alandcolsel[0])[0].gapMap()))
1166 // add profile to visible contigs
1167 for (int v = 0; v < viscontigs.length; v += 2)
1169 if (viscontigs[v] > spos)
1171 StringBuffer sb = new StringBuffer();
1172 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1176 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1178 SequenceI sqobj = al.getSequenceAt(s);
1179 if (sqobj != profileseq)
1181 String sq = al.getSequenceAt(s).getSequenceAsString();
1182 if (sq.length() <= spos + offset)
1185 int diff = spos + offset - sq.length() - 1;
1190 while ((diff = spos + offset - sq.length() - 1) > 0)
1193 // + ((diff >= sb.length()) ? sb.toString() : sb
1194 // .substring(0, diff));
1195 if (diff >= sb.length())
1197 sq += sb.toString();
1201 char[] buf = new char[diff];
1202 sb.getChars(0, diff, buf, 0);
1203 sq += buf.toString();
1207 sq += sb.toString();
1211 al.getSequenceAt(s).setSequence(
1212 sq.substring(0, spos + offset) + sb.toString()
1213 + sq.substring(spos + offset));
1217 // offset+=sb.length();
1219 spos = viscontigs[v + 1] + 1;
1221 if ((offset + spos) < profileseq.getLength())
1223 // pad the final region with gaps.
1224 StringBuffer sb = new StringBuffer();
1225 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1229 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1231 SequenceI sqobj = al.getSequenceAt(s);
1232 if (sqobj == profileseq)
1236 String sq = sqobj.getSequenceAsString();
1238 int diff = origseq.getLength() - sq.length();
1242 // + ((diff >= sb.length()) ? sb.toString() : sb
1243 // .substring(0, diff));
1244 if (diff >= sb.length())
1246 sq += sb.toString();
1250 char[] buf = new char[diff];
1251 sb.getChars(0, diff, buf, 0);
1252 sq += buf.toString();
1254 diff = origseq.getLength() - sq.length();