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.Enumeration;
27 import java.util.List;
28 import java.util.Vector;
31 * NOTE: Columns are zero based.
33 public class ColumnSelection
35 Vector selected = new Vector();
37 // Vector of int [] {startCol, endCol}
41 * Add a column to the selection
46 public void addElement(int col)
48 Integer column = new Integer(col);
49 if (!selected.contains(column))
51 selected.addElement(column);
56 * clears column selection
60 selected.removeAllElements();
64 * removes col from selection
67 * index of column to be removed
69 public void removeElement(int col)
71 Integer colInt = new Integer(col);
73 if (selected.contains(colInt))
75 selected.removeElement(colInt);
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.removeElement(colInt);
102 * @return Vector containing selected columns as Integers
104 public Vector 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 return ((Integer) selected.elementAt(i)).intValue();
137 * @return DOCUMENT ME!
141 return selected.size();
145 * rightmost selected column
147 * @return rightmost column in alignment that is selected
153 for (int i = 0; i < selected.size(); i++)
155 if (columnAt(i) > max)
165 * Leftmost column in selection
167 * @return column index of leftmost column in selection
171 int min = 1000000000;
173 for (int i = 0; i < selected.size(); i++)
175 if (columnAt(i) < min)
185 * propagate shift in alignment columns to column selection
190 * shift in edit (+ve for removal, or -ve for inserts)
192 public List<int[]> compensateForEdit(int start, int change)
194 List<int[]> deletedHiddenColumns = null;
195 for (int i = 0; i < size(); i++)
197 int temp = columnAt(i);
201 selected.setElementAt(new Integer(temp - change), i);
205 if (hiddenColumns != null)
207 deletedHiddenColumns = new ArrayList<int[]>();
208 int hSize = hiddenColumns.size();
209 for (int i = 0; i < hSize; i++)
211 int[] region = (int[]) hiddenColumns.elementAt(i);
212 if (region[0] > start && start + change > region[1])
214 deletedHiddenColumns.add(region);
216 hiddenColumns.removeElementAt(i);
222 if (region[0] > start)
235 this.revealHiddenColumns(0);
238 return deletedHiddenColumns;
242 * propagate shift in alignment columns to column selection special version of
243 * compensateForEdit - allowing for edits within hidden regions
248 * shift in edit (+ve for removal, or -ve for inserts)
250 private void compensateForDelEdits(int start, int change)
252 for (int i = 0; i < size(); i++)
254 int temp = columnAt(i);
258 selected.setElementAt(new Integer(temp - change), i);
262 if (hiddenColumns != null)
264 for (int i = 0; i < hiddenColumns.size(); i++)
266 int[] region = (int[]) hiddenColumns.elementAt(i);
267 if (region[0] >= start)
271 if (region[1] >= start)
275 if (region[1] < region[0])
277 hiddenColumns.removeElementAt(i--);
293 * Adjust hidden column boundaries based on a series of column additions or
294 * deletions in visible regions.
299 public ShiftList compensateForEdits(ShiftList shiftrecord)
301 if (shiftrecord != null)
303 Vector shifts = shiftrecord.shifts;
304 if (shifts != null && shifts.size() > 0)
307 for (int i = 0, j = shifts.size(); i < j; i++)
309 int[] sh = (int[]) shifts.elementAt(i);
310 // compensateForEdit(shifted+sh[0], sh[1]);
311 compensateForDelEdits(shifted + sh[0], sh[1]);
315 return shiftrecord.getInverse();
321 * removes intersection of position,length ranges in deletions from the
322 * start,end regions marked in intervals.
328 private boolean pruneIntervalVector(Vector deletions, Vector intervals)
330 boolean pruned = false;
331 int i = 0, j = intervals.size() - 1, s = 0, t = deletions.size() - 1;
332 int hr[] = (int[]) intervals.elementAt(i);
333 int sr[] = (int[]) deletions.elementAt(s);
334 while (i <= j && s <= t)
336 boolean trailinghn = hr[1] >= sr[0];
341 hr = (int[]) intervals.elementAt(++i);
349 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
350 if (endshift < hr[0] || endshift < sr[0])
351 { // leadinghc disjoint or not a deletion
354 sr = (int[]) deletions.elementAt(++s);
362 boolean leadinghn = hr[0] >= sr[0];
363 boolean leadinghc = hr[0] < endshift;
364 boolean trailinghc = hr[1] < endshift;
368 { // deleted hidden region.
369 intervals.removeElementAt(i);
374 hr = (int[]) intervals.elementAt(i);
380 hr[0] = endshift; // clip c terminal region
381 leadinghn = !leadinghn;
397 // sr contained in hr
400 sr = (int[]) deletions.elementAt(++s);
410 return pruned; // true if any interval was removed or modified by
414 private boolean pruneColumnList(Vector deletion, Vector list)
416 int s = 0, t = deletion.size();
417 int[] sr = (int[]) list.elementAt(s++);
418 boolean pruned = false;
419 int i = 0, j = list.size();
420 while (i < j && s <= t)
422 int c = ((Integer) list.elementAt(i++)).intValue();
425 if (sr[1] + sr[0] >= c)
426 { // sr[1] -ve means inseriton.
427 list.removeElementAt(--i);
434 sr = (int[]) deletion.elementAt(s);
444 * remove any hiddenColumns or selected columns and shift remaining based on a
445 * series of position, range deletions.
449 public void pruneDeletions(ShiftList deletions)
451 if (deletions != null)
453 Vector shifts = deletions.shifts;
454 if (shifts != null && shifts.size() > 0)
456 // delete any intervals intersecting.
457 if (hiddenColumns != null)
459 pruneIntervalVector(shifts, hiddenColumns);
460 if (hiddenColumns != null && hiddenColumns.size() == 0)
462 hiddenColumns = null;
465 if (selected != null && selected.size() > 0)
467 pruneColumnList(shifts, selected);
468 if (selected != null && selected.size() == 0)
473 // and shift the rest.
474 this.compensateForEdits(deletions);
480 * This Method is used to return all the HiddenColumn regions less than the
487 public Vector getHiddenColumns()
489 return hiddenColumns;
493 * Return absolute column index for a visible column index
496 * int column index in alignment view
497 * @return alignment column index for column
499 public int adjustForHiddenColumns(int column)
502 if (hiddenColumns != null)
504 for (int i = 0; i < hiddenColumns.size(); i++)
506 int[] region = (int[]) hiddenColumns.elementAt(i);
507 if (result >= region[0])
509 result += region[1] - region[0] + 1;
517 * Use this method to find out where a column will appear in the visible
518 * alignment when hidden columns exist. If the column is not visible, then the
519 * left-most visible column will always be returned.
521 * @param hiddenColumn
525 public int findColumnPosition(int hiddenColumn)
527 int result = hiddenColumn;
528 if (hiddenColumns != null)
534 region = (int[]) hiddenColumns.elementAt(index++);
535 if (hiddenColumn > region[1])
537 result -= region[1] + 1 - region[0];
539 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
540 if (hiddenColumn > region[0] && hiddenColumn < region[1])
542 return region[0] + hiddenColumn - result;
545 return result; // return the shifted position after removing hidden columns.
549 * Use this method to determine where the next hiddenRegion starts
551 public int findHiddenRegionPosition(int hiddenRegion)
554 if (hiddenColumns != null)
560 int[] region = (int[]) hiddenColumns.elementAt(index);
561 if (hiddenRegion == 0)
566 gaps += region[1] + 1 - region[0];
567 result = region[1] + 1;
569 } while (index < hiddenRegion + 1);
578 * THis method returns the rightmost limit of a region of an alignment with
579 * hidden columns. In otherwords, the next hidden column.
584 public int getHiddenBoundaryRight(int alPos)
586 if (hiddenColumns != null)
591 int[] region = (int[]) hiddenColumns.elementAt(index);
592 if (alPos < region[0])
598 } while (index < hiddenColumns.size());
606 * This method returns the leftmost limit of a region of an alignment with
607 * hidden columns. In otherwords, the previous hidden column.
612 public int getHiddenBoundaryLeft(int alPos)
614 if (hiddenColumns != null)
616 int index = hiddenColumns.size() - 1;
619 int[] region = (int[]) hiddenColumns.elementAt(index);
620 if (alPos > region[1])
626 } while (index > -1);
633 public void hideSelectedColumns()
637 int column = ((Integer) getSelected().firstElement()).intValue();
643 public void hideColumns(int start, int end)
645 if (hiddenColumns == null)
647 hiddenColumns = new Vector();
650 boolean added = false;
651 boolean overlap = false;
653 for (int i = 0; i < hiddenColumns.size(); i++)
655 int[] region = (int[]) hiddenColumns.elementAt(i);
656 if (start <= region[1] && end >= region[0])
658 hiddenColumns.removeElementAt(i);
662 else if (end < region[0] && start < region[0])
664 hiddenColumns.insertElementAt(new int[]
673 hideColumns(start, end);
677 hiddenColumns.addElement(new int[]
684 * This method will find a range of selected columns around the column
690 public void hideColumns(int col)
692 // First find out range of columns to hide
693 int min = col, max = col + 1;
694 while (contains(min))
700 while (contains(max))
713 hideColumns(min, max);
716 public void revealAllHiddenColumns()
718 if (hiddenColumns != null)
720 for (int i = 0; i < hiddenColumns.size(); i++)
722 int[] region = (int[]) hiddenColumns.elementAt(i);
723 for (int j = region[0]; j < region[1] + 1; j++)
730 hiddenColumns = null;
733 public void revealHiddenColumns(int res)
735 for (int i = 0; i < hiddenColumns.size(); i++)
737 int[] region = (int[]) hiddenColumns.elementAt(i);
738 if (res == region[0])
740 for (int j = region[0]; j < region[1] + 1; j++)
745 hiddenColumns.removeElement(region);
749 if (hiddenColumns.size() == 0)
751 hiddenColumns = null;
755 public boolean isVisible(int column)
757 if (hiddenColumns != null)
759 for (int i = 0; i < hiddenColumns.size(); i++)
761 int[] region = (int[]) hiddenColumns.elementAt(i);
762 if (column >= region[0] && column <= region[1])
777 public ColumnSelection(ColumnSelection copy)
781 if (copy.selected != null)
783 selected = new Vector();
784 for (int i = 0, j = copy.selected.size(); i < j; i++)
786 selected.addElement(copy.selected.elementAt(i));
789 if (copy.hiddenColumns != null)
791 hiddenColumns = new Vector(copy.hiddenColumns.size());
792 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
795 rh = (int[]) copy.hiddenColumns.elementAt(i);
798 cp = new int[rh.length];
799 System.arraycopy(rh, 0, cp, 0, rh.length);
800 hiddenColumns.addElement(cp);
810 public ColumnSelection()
814 public String[] getVisibleSequenceStrings(int start, int end,
817 int i, iSize = seqs.length;
818 String selection[] = new String[iSize];
819 if (hiddenColumns != null && hiddenColumns.size() > 0)
821 for (i = 0; i < iSize; i++)
823 StringBuffer visibleSeq = new StringBuffer();
824 Vector regions = getHiddenColumns();
826 int blockStart = start, blockEnd = end;
828 int hideStart, hideEnd;
830 for (int j = 0; j < regions.size(); j++)
832 region = (int[]) regions.elementAt(j);
833 hideStart = region[0];
836 if (hideStart < start)
841 blockStart = Math.min(blockStart, hideEnd + 1);
842 blockEnd = Math.min(blockEnd, hideStart);
844 if (blockStart > blockEnd)
849 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
851 blockStart = hideEnd + 1;
855 if (end > blockStart)
857 visibleSeq.append(seqs[i].getSequence(blockStart, end));
860 selection[i] = visibleSeq.toString();
865 for (i = 0; i < iSize; i++)
867 selection[i] = seqs[i].getSequenceAsString(start, end);
875 * return all visible segments between the given start and end boundaries
878 * (first column inclusive from 0)
880 * (last column - not inclusive)
881 * @return int[] {i_start, i_end, ..} where intervals lie in
882 * start<=i_start<=i_end<end
884 public int[] getVisibleContigs(int start, int end)
886 if (hiddenColumns != null && hiddenColumns.size() > 0)
888 Vector visiblecontigs = new Vector();
889 Vector regions = getHiddenColumns();
893 int hideStart, hideEnd;
895 for (int j = 0; vstart < end && j < regions.size(); j++)
897 region = (int[]) regions.elementAt(j);
898 hideStart = region[0];
901 if (hideEnd < vstart)
905 if (hideStart > vstart)
907 visiblecontigs.addElement(new int[]
908 { vstart, hideStart - 1 });
910 vstart = hideEnd + 1;
915 visiblecontigs.addElement(new int[]
916 { vstart, end - 1 });
918 int[] vcontigs = new int[visiblecontigs.size() * 2];
919 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
921 int[] vc = (int[]) visiblecontigs.elementAt(i);
922 visiblecontigs.setElementAt(null, i);
923 vcontigs[i * 2] = vc[0];
924 vcontigs[i * 2 + 1] = vc[1];
926 visiblecontigs.removeAllElements();
937 * delete any columns in alignmentAnnotation that are hidden (including
938 * sequence associated annotation).
940 * @param alignmentAnnotation
942 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
944 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
948 * delete any columns in alignmentAnnotation that are hidden (including
949 * sequence associated annotation).
952 * remove any annotation to the right of this column
954 * remove any annotation to the left of this column
955 * @param alignmentAnnotation
956 * the annotation to operate on
958 public void makeVisibleAnnotation(int start, int end,
959 AlignmentAnnotation alignmentAnnotation)
961 if (alignmentAnnotation.annotations == null)
965 if (start == end && end == -1)
968 end = alignmentAnnotation.annotations.length;
970 if (hiddenColumns != null && hiddenColumns.size() > 0)
972 // then mangle the alignmentAnnotation annotation array
973 Vector annels = new Vector();
974 Annotation[] els = null;
975 Vector regions = getHiddenColumns();
976 int blockStart = start, blockEnd = end;
978 int hideStart, hideEnd, w = 0;
980 for (int j = 0; j < regions.size(); j++)
982 region = (int[]) regions.elementAt(j);
983 hideStart = region[0];
986 if (hideStart < start)
991 blockStart = Math.min(blockStart, hideEnd + 1);
992 blockEnd = Math.min(blockEnd, hideStart);
994 if (blockStart > blockEnd)
999 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1000 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1003 blockStart = hideEnd + 1;
1007 if (end > blockStart)
1009 annels.addElement(els = new Annotation[end - blockStart + 1]);
1010 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
1012 // copy just the visible segment of the annotation row
1013 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1014 els, 0, els.length);
1018 // copy to the end of the annotation row
1019 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1021 (alignmentAnnotation.annotations.length - blockStart));
1029 Enumeration e = annels.elements();
1030 alignmentAnnotation.annotations = new Annotation[w];
1032 while (e.hasMoreElements())
1034 Annotation[] chnk = (Annotation[]) e.nextElement();
1035 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1042 alignmentAnnotation.restrict(start, end);
1047 * Invert the column selection from first to end-1. leaves hiddenColumns
1048 * untouched (and unselected)
1053 public void invertColumnSelection(int first, int width)
1055 boolean hasHidden = hiddenColumns != null && hiddenColumns.size() > 0;
1056 for (int i = first; i < width; i++)
1064 if (!hasHidden || isVisible(i))
1073 * add in any unselected columns from the given column selection, excluding
1074 * any that are hidden.
1078 public void addElementsFrom(ColumnSelection colsel)
1080 if (colsel != null && colsel.size() > 0)
1082 Enumeration e = colsel.getSelected().elements();
1083 while (e.hasMoreElements())
1085 Object eo = e.nextElement();
1086 if (hiddenColumns != null && isVisible(((Integer) eo).intValue()))
1088 if (!selected.contains(eo))
1090 selected.addElement(eo);
1098 * set the selected columns the given column selection, excluding any columns
1103 public void setElementsFrom(ColumnSelection colsel)
1105 selected = new Vector();
1106 if (colsel.selected != null && colsel.selected.size() > 0)
1108 if (hiddenColumns != null && hiddenColumns.size() > 0)
1110 // only select visible columns in this columns selection
1111 selected = new Vector();
1112 addElementsFrom(colsel);
1116 // add everything regardless
1117 Enumeration en = colsel.selected.elements();
1118 while (en.hasMoreElements())
1120 selected.addElement(en.nextElement());
1127 * Add gaps into the sequences aligned to profileseq under the given
1132 * - alignment to have gaps inserted into it
1134 * - alignment view where sequence corresponding to profileseq is
1136 * @return new Column selection for new alignment view, with insertions into
1137 * profileseq marked as hidden.
1139 public static ColumnSelection propagateInsertions(SequenceI profileseq,
1140 Alignment al, AlignmentView input)
1144 // return propagateInsertions(profileseq, al, )
1145 char gc = al.getGapCharacter();
1146 Object[] alandcolsel = input.getAlignmentAndColumnSelection(gc);
1147 ColumnSelection nview = (ColumnSelection) alandcolsel[1];
1148 SequenceI origseq = ((SequenceI[]) alandcolsel[0])[profsqpos];
1149 nview.propagateInsertions(profileseq, al, origseq);
1156 * - sequence in al which corresponds to origseq
1158 * - alignment which is to have gaps inserted into it
1160 * - sequence corresponding to profileseq which defines gap map for
1163 public void propagateInsertions(SequenceI profileseq, AlignmentI al,
1166 char gc = al.getGapCharacter();
1167 // recover mapping between sequence's non-gap positions and positions
1169 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1170 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1173 // input.pruneDeletions(ShiftList.parseMap(((SequenceI[])
1174 // alandcolsel[0])[0].gapMap()))
1175 // add profile to visible contigs
1176 for (int v = 0; v < viscontigs.length; v += 2)
1178 if (viscontigs[v] > spos)
1180 StringBuffer sb = new StringBuffer();
1181 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1185 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1187 SequenceI sqobj = al.getSequenceAt(s);
1188 if (sqobj != profileseq)
1190 String sq = al.getSequenceAt(s).getSequenceAsString();
1191 if (sq.length() <= spos + offset)
1194 int diff = spos + offset - sq.length() - 1;
1199 while ((diff = spos + offset - sq.length() - 1) > 0)
1202 // + ((diff >= sb.length()) ? sb.toString() : sb
1203 // .substring(0, diff));
1204 if (diff >= sb.length())
1206 sq += sb.toString();
1210 char[] buf = new char[diff];
1211 sb.getChars(0, diff, buf, 0);
1212 sq += buf.toString();
1216 sq += sb.toString();
1220 al.getSequenceAt(s).setSequence(
1221 sq.substring(0, spos + offset) + sb.toString()
1222 + sq.substring(spos + offset));
1226 // offset+=sb.length();
1228 spos = viscontigs[v + 1] + 1;
1230 if ((offset + spos) < profileseq.getLength())
1232 // pad the final region with gaps.
1233 StringBuffer sb = new StringBuffer();
1234 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1238 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1240 SequenceI sqobj = al.getSequenceAt(s);
1241 if (sqobj == profileseq)
1245 String sq = sqobj.getSequenceAsString();
1247 int diff = origseq.getLength() - sq.length();
1251 // + ((diff >= sb.length()) ? sb.toString() : sb
1252 // .substring(0, diff));
1253 if (diff >= sb.length())
1255 sq += sb.toString();
1259 char[] buf = new char[diff];
1260 sb.getChars(0, diff, buf, 0);
1261 sq += buf.toString();
1263 diff = origseq.getLength() - sq.length();