1 package jalview.datamodel;
3 import jalview.util.Comparison;
4 import jalview.util.ShiftList;
6 import java.util.ArrayList;
7 import java.util.Collections;
9 import java.util.Vector;
11 public class HiddenColumns
14 * list of hidden column [start, end] ranges; the list is maintained in
15 * ascending start column order
17 private Vector<int[]> hiddenColumns;
20 * This Method is used to return all the HiddenColumn regions
22 * @return empty list or List of hidden column intervals
24 public List<int[]> getListOfCols()
26 return hiddenColumns == null ? Collections.<int[]> emptyList()
30 public boolean hasHidden()
32 return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
36 public boolean equals(Object obj)
38 if (!(obj instanceof HiddenColumns))
42 HiddenColumns that = (HiddenColumns) obj;
45 * check hidden columns are either both null, or match
47 if (this.hiddenColumns == null)
49 return (that.hiddenColumns == null);
51 if (that.hiddenColumns == null
52 || that.hiddenColumns.size() != this.hiddenColumns.size())
57 for (int[] thisRange : hiddenColumns)
59 int[] thatRange = that.hiddenColumns.get(i++);
60 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
69 * Return absolute column index for a visible column index
72 * int column index in alignment view (count from zero)
73 * @return alignment column index for column
75 public int adjustForHiddenColumns(int column)
78 if (hiddenColumns != null)
80 for (int i = 0; i < hiddenColumns.size(); i++)
82 int[] region = hiddenColumns.elementAt(i);
83 if (result >= region[0])
85 result += region[1] - region[0] + 1;
93 * Use this method to find out where a column will appear in the visible
94 * alignment when hidden columns exist. If the column is not visible, then the
95 * left-most visible column will always be returned.
98 * the column index in the full alignment including hidden columns
99 * @return the position of the column in the visible alignment
101 public int findColumnPosition(int hiddenColumn)
103 int result = hiddenColumn;
104 if (hiddenColumns != null)
110 region = hiddenColumns.elementAt(index++);
111 if (hiddenColumn > region[1])
113 result -= region[1] + 1 - region[0];
115 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
117 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
119 // Here the hidden column is within a region, so
120 // we want to return the position of region[0]-1, adjusted for any
121 // earlier hidden columns.
122 // Calculate the difference between the actual hidden col position
123 // and region[0]-1, and then subtract from result to convert result from
124 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
126 // However, if the region begins at 0 we cannot return region[0]-1
134 return result - (hiddenColumn - region[0] + 1);
138 return result; // return the shifted position after removing hidden columns.
142 * Find the visible column which is a given visible number of columns to the
143 * left of another visible column. i.e. for a startColumn x, the column which
144 * is distance 1 away will be column x-1.
146 * @param visibleDistance
147 * the number of visible columns to offset by
149 * the column to start from
150 * @return the position of the column in the visible alignment
152 public int subtractVisibleColumns(int visibleDistance, int startColumn)
154 int distance = visibleDistance;
156 // in case startColumn is in a hidden region, move it to the left
157 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
159 // get index of hidden region to left of start
160 int index = getHiddenIndexLeft(start);
163 // no hidden regions to left of startColumn
164 return start - distance;
167 // walk backwards through the alignment subtracting the counts of visible
168 // columns from distance
171 int nextstart = start;
173 while ((index > -1) && (distance - gap > 0))
175 // subtract the gap to right of region from distance
179 // calculate the next gap
180 region = hiddenColumns.get(index);
181 gap = start - region[1];
183 // set start to just to left of current region
184 nextstart = region[0] - 1;
188 if (distance - gap > 0)
190 // fell out of loop because there are no more hidden regions
192 return nextstart - distance;
194 return start - distance;
199 * Use this method to determine where the next hiddenRegion starts
201 * @param hiddenRegion
202 * index of hidden region (counts from 0)
203 * @return column number in visible view
205 public int findHiddenRegionPosition(int hiddenRegion)
208 if (hiddenColumns != null)
214 int[] region = hiddenColumns.elementAt(index);
215 if (hiddenRegion == 0)
220 gaps += region[1] + 1 - region[0];
221 result = region[1] + 1;
223 } while (index <= hiddenRegion);
232 * This method returns the rightmost limit of a region of an alignment with
233 * hidden columns. In otherwords, the next hidden column.
238 public int getHiddenBoundaryRight(int alPos)
240 if (hiddenColumns != null)
245 int[] region = hiddenColumns.elementAt(index);
246 if (alPos < region[0])
252 } while (index < hiddenColumns.size());
260 * This method returns the leftmost limit of a region of an alignment with
261 * hidden columns. In otherwords, the previous hidden column.
266 public int getHiddenBoundaryLeft(int alPos)
268 if (hiddenColumns != null)
270 int index = hiddenColumns.size() - 1;
273 int[] region = hiddenColumns.elementAt(index);
274 if (alPos > region[1])
280 } while (index > -1);
288 * This method returns the index of the hidden region to the left of a column
289 * position. If the column is in a hidden region it returns the index of the
290 * region to the left. If there is no hidden region to the left it returns -1.
295 private int getHiddenIndexLeft(int pos)
297 if (hiddenColumns != null)
299 int index = hiddenColumns.size() - 1;
302 int[] region = hiddenColumns.elementAt(index);
309 } while (index > -1);
317 * Adds the specified column range to the hidden columns
322 public void hideColumns(int start, int end)
324 if (hiddenColumns == null)
326 hiddenColumns = new Vector<int[]>();
330 * traverse existing hidden ranges and insert / amend / append as
333 for (int i = 0; i < hiddenColumns.size(); i++)
335 int[] region = hiddenColumns.elementAt(i);
337 if (end < region[0] - 1)
340 * insert discontiguous preceding range
342 hiddenColumns.insertElementAt(new int[] { start, end }, i);
346 if (end <= region[1])
349 * new range overlaps existing, or is contiguous preceding it - adjust
352 region[0] = Math.min(region[0], start);
356 if (start <= region[1] + 1)
359 * new range overlaps existing, or is contiguous following it - adjust
360 * start and end columns
362 region[0] = Math.min(region[0], start);
363 region[1] = Math.max(region[1], end);
366 * also update or remove any subsequent ranges
367 * that are overlapped
369 while (i < hiddenColumns.size() - 1)
371 int[] nextRegion = hiddenColumns.get(i + 1);
372 if (nextRegion[0] > end + 1)
375 * gap to next hidden range - no more to update
379 region[1] = Math.max(nextRegion[1], end);
380 hiddenColumns.remove(i + 1);
387 * remaining case is that the new range follows everything else
389 hiddenColumns.addElement(new int[] { start, end });
392 public boolean isVisible(int column)
394 if (hiddenColumns != null)
396 for (int[] region : hiddenColumns)
398 if (column >= region[0] && column <= region[1])
411 public HiddenColumns()
420 public HiddenColumns(HiddenColumns copy)
424 if (copy.hiddenColumns != null)
426 hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
427 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
430 rh = copy.hiddenColumns.elementAt(i);
433 cp = new int[rh.length];
434 System.arraycopy(rh, 0, cp, 0, rh.length);
435 hiddenColumns.addElement(cp);
443 * propagate shift in alignment columns to column selection
448 * shift in edit (+ve for removal, or -ve for inserts)
450 public List<int[]> compensateForEdit(int start, int change,
453 List<int[]> deletedHiddenColumns = null;
455 if (hiddenColumns != null)
457 deletedHiddenColumns = new ArrayList<int[]>();
458 int hSize = hiddenColumns.size();
459 for (int i = 0; i < hSize; i++)
461 int[] region = hiddenColumns.elementAt(i);
462 if (region[0] > start && start + change > region[1])
464 deletedHiddenColumns.add(region);
466 hiddenColumns.removeElementAt(i);
472 if (region[0] > start)
485 this.revealHiddenColumns(0, sel);
488 return deletedHiddenColumns;
492 * propagate shift in alignment columns to column selection special version of
493 * compensateForEdit - allowing for edits within hidden regions
498 * shift in edit (+ve for removal, or -ve for inserts)
500 public void compensateForDelEdits(int start, int change)
502 if (hiddenColumns != null)
504 for (int i = 0; i < hiddenColumns.size(); i++)
506 int[] region = hiddenColumns.elementAt(i);
507 if (region[0] >= start)
511 if (region[1] >= start)
515 if (region[1] < region[0])
517 hiddenColumns.removeElementAt(i--);
533 * return all visible segments between the given start and end boundaries
536 * (first column inclusive from 0)
538 * (last column - not inclusive)
539 * @return int[] {i_start, i_end, ..} where intervals lie in
540 * start<=i_start<=i_end<end
542 public int[] getVisibleContigs(int start, int end)
544 if (hiddenColumns != null && hiddenColumns.size() > 0)
546 List<int[]> visiblecontigs = new ArrayList<int[]>();
547 List<int[]> regions = getListOfCols();
551 int hideStart, hideEnd;
553 for (int j = 0; vstart < end && j < regions.size(); j++)
555 region = regions.get(j);
556 hideStart = region[0];
559 if (hideEnd < vstart)
563 if (hideStart > vstart)
565 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
567 vstart = hideEnd + 1;
572 visiblecontigs.add(new int[] { vstart, end - 1 });
574 int[] vcontigs = new int[visiblecontigs.size() * 2];
575 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
577 int[] vc = visiblecontigs.get(i);
578 visiblecontigs.set(i, null);
579 vcontigs[i * 2] = vc[0];
580 vcontigs[i * 2 + 1] = vc[1];
582 visiblecontigs.clear();
587 return new int[] { start, end - 1 };
591 public String[] getVisibleSequenceStrings(int start, int end,
594 int i, iSize = seqs.length;
595 String selections[] = new String[iSize];
596 if (hiddenColumns != null && hiddenColumns.size() > 0)
598 for (i = 0; i < iSize; i++)
600 StringBuffer visibleSeq = new StringBuffer();
601 List<int[]> regions = getListOfCols();
603 int blockStart = start, blockEnd = end;
605 int hideStart, hideEnd;
607 for (int j = 0; j < regions.size(); j++)
609 region = regions.get(j);
610 hideStart = region[0];
613 if (hideStart < start)
618 blockStart = Math.min(blockStart, hideEnd + 1);
619 blockEnd = Math.min(blockEnd, hideStart);
621 if (blockStart > blockEnd)
626 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
628 blockStart = hideEnd + 1;
632 if (end > blockStart)
634 visibleSeq.append(seqs[i].getSequence(blockStart, end));
637 selections[i] = visibleSeq.toString();
642 for (i = 0; i < iSize; i++)
644 selections[i] = seqs[i].getSequenceAsString(start, end);
652 * Locate the first and last position visible for this sequence. if seq isn't
653 * visible then return the position of the left and right of the hidden
654 * boundary region, and the corresponding alignment column indices for the
655 * extent of the sequence
658 * @return int[] { visible start, visible end, first seqpos, last seqpos,
659 * alignment index for seq start, alignment index for seq end }
661 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
663 int fpos = seq.getStart(), lpos = seq.getEnd();
666 if (hiddenColumns == null || hiddenColumns.size() == 0)
668 int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;
669 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
672 // Simply walk along the sequence whilst watching for hidden column
674 List<int[]> regions = getListOfCols();
675 int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
676 .getLength(), hideEnd = -1;
677 int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
678 boolean foundStart = false;
679 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
682 if (!Comparison.isGap(seq.getCharAt(p)))
684 // keep track of first/last column
685 // containing sequence data regardless of visibility
691 // update hidden region start/end
692 while (hideEnd < p && rcount < regions.size())
694 int[] region = regions.get(rcount++);
696 visNext += region[0] - visPrev;
697 hideStart = region[0];
702 hideStart = seq.getLength();
704 // update visible boundary for sequence
716 // look for next sequence position
722 return new int[] { findColumnPosition(start),
723 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
725 // otherwise, sequence was completely hidden
726 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
730 * delete any columns in alignmentAnnotation that are hidden (including
731 * sequence associated annotation).
733 * @param alignmentAnnotation
735 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
737 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
741 * delete any columns in alignmentAnnotation that are hidden (including
742 * sequence associated annotation).
745 * remove any annotation to the right of this column
747 * remove any annotation to the left of this column
748 * @param alignmentAnnotation
749 * the annotation to operate on
751 public void makeVisibleAnnotation(int start, int end,
752 AlignmentAnnotation alignmentAnnotation)
754 if (alignmentAnnotation.annotations == null)
758 if (start == end && end == -1)
761 end = alignmentAnnotation.annotations.length;
763 if (hiddenColumns != null && hiddenColumns.size() > 0)
765 // then mangle the alignmentAnnotation annotation array
766 Vector<Annotation[]> annels = new Vector<Annotation[]>();
767 Annotation[] els = null;
768 List<int[]> regions = getListOfCols();
769 int blockStart = start, blockEnd = end;
771 int hideStart, hideEnd, w = 0;
773 for (int j = 0; j < regions.size(); j++)
775 region = regions.get(j);
776 hideStart = region[0];
779 if (hideStart < start)
784 blockStart = Math.min(blockStart, hideEnd + 1);
785 blockEnd = Math.min(blockEnd, hideStart);
787 if (blockStart > blockEnd)
792 annels.addElement(els = new Annotation[blockEnd - blockStart]);
793 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
796 blockStart = hideEnd + 1;
800 if (end > blockStart)
802 annels.addElement(els = new Annotation[end - blockStart + 1]);
803 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
805 // copy just the visible segment of the annotation row
806 System.arraycopy(alignmentAnnotation.annotations, blockStart,
811 // copy to the end of the annotation row
812 System.arraycopy(alignmentAnnotation.annotations, blockStart,
814 (alignmentAnnotation.annotations.length - blockStart));
823 alignmentAnnotation.annotations = new Annotation[w];
826 for (Annotation[] chnk : annels)
828 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
835 alignmentAnnotation.restrict(start, end);
841 * @return true if there are columns hidden
843 public boolean hasHiddenColumns()
845 return hiddenColumns != null && hiddenColumns.size() > 0;
850 * @return true if there are more than one set of columns hidden
852 public boolean hasManyHiddenColumns()
854 return hiddenColumns != null && hiddenColumns.size() > 1;
858 * mark the columns corresponding to gap characters as hidden in the column
863 public void hideInsertionsFor(SequenceI sr)
865 List<int[]> inserts = sr.getInsertions();
866 for (int[] r : inserts)
868 hideColumns(r[0], r[1]);
873 * Unhides, and adds to the selection list, all hidden columns
875 public void revealAllHiddenColumns(ColumnSelection sel)
877 if (hiddenColumns != null)
879 for (int i = 0; i < hiddenColumns.size(); i++)
881 int[] region = hiddenColumns.elementAt(i);
882 for (int j = region[0]; j < region[1] + 1; j++)
889 hiddenColumns = null;
893 * Reveals, and marks as selected, the hidden column range with the given
898 public void revealHiddenColumns(int start, ColumnSelection sel)
900 for (int i = 0; i < hiddenColumns.size(); i++)
902 int[] region = hiddenColumns.elementAt(i);
903 if (start == region[0])
905 for (int j = region[0]; j < region[1] + 1; j++)
910 hiddenColumns.removeElement(region);
914 if (hiddenColumns.size() == 0)
916 hiddenColumns = null;
921 * removes intersection of position,length ranges in deletions from the
922 * start,end regions marked in intervals.
928 private boolean pruneIntervalVector(final List<int[]> shifts,
929 Vector<int[]> intervals)
931 boolean pruned = false;
932 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
933 int hr[] = intervals.elementAt(i);
934 int sr[] = shifts.get(s);
935 while (i <= j && s <= t)
937 boolean trailinghn = hr[1] >= sr[0];
942 hr = intervals.elementAt(++i);
950 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
951 if (endshift < hr[0] || endshift < sr[0])
952 { // leadinghc disjoint or not a deletion
955 sr = shifts.get(++s);
963 boolean leadinghn = hr[0] >= sr[0];
964 boolean leadinghc = hr[0] < endshift;
965 boolean trailinghc = hr[1] < endshift;
969 { // deleted hidden region.
970 intervals.removeElementAt(i);
975 hr = intervals.elementAt(i);
981 hr[0] = endshift; // clip c terminal region
982 leadinghn = !leadinghn;
998 // sr contained in hr
1001 sr = shifts.get(++s);
1011 return pruned; // true if any interval was removed or modified by
1016 * remove any hiddenColumns or selected columns and shift remaining based on a
1017 * series of position, range deletions.
1021 public void pruneDeletions(List<int[]> shifts)
1023 // delete any intervals intersecting.
1024 if (hiddenColumns != null)
1026 pruneIntervalVector(shifts, hiddenColumns);
1027 if (hiddenColumns != null && hiddenColumns.size() == 0)
1029 hiddenColumns = null;
1035 * Add gaps into the sequences aligned to profileseq under the given
1040 * - alignment to have gaps inserted into it
1042 * - alignment view where sequence corresponding to profileseq is
1044 * @return new HiddenColumns for new alignment view, with insertions into
1045 * profileseq marked as hidden.
1047 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1048 AlignmentI al, AlignmentView input)
1052 char gc = al.getGapCharacter();
1053 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1054 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1055 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1056 nview.propagateInsertions(profileseq, al, origseq);
1063 * - sequence in al which corresponds to origseq
1065 * - alignment which is to have gaps inserted into it
1067 * - sequence corresponding to profileseq which defines gap map for
1070 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1073 char gc = al.getGapCharacter();
1074 // recover mapping between sequence's non-gap positions and positions
1076 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1077 int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
1078 profileseq.getLength());
1082 // add profile to visible contigs
1083 for (int v = 0; v < viscontigs.length; v += 2)
1085 if (viscontigs[v] > spos)
1087 StringBuffer sb = new StringBuffer();
1088 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1092 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1094 SequenceI sqobj = al.getSequenceAt(s);
1095 if (sqobj != profileseq)
1097 String sq = al.getSequenceAt(s).getSequenceAsString();
1098 if (sq.length() <= spos + offset)
1101 int diff = spos + offset - sq.length() - 1;
1106 while ((diff = spos + offset - sq.length() - 1) > 0)
1109 // + ((diff >= sb.length()) ? sb.toString() : sb
1110 // .substring(0, diff));
1111 if (diff >= sb.length())
1113 sq += sb.toString();
1117 char[] buf = new char[diff];
1118 sb.getChars(0, diff, buf, 0);
1119 sq += buf.toString();
1123 sq += sb.toString();
1127 al.getSequenceAt(s).setSequence(
1128 sq.substring(0, spos + offset) + sb.toString()
1129 + sq.substring(spos + offset));
1133 // offset+=sb.length();
1135 spos = viscontigs[v + 1] + 1;
1137 if ((offset + spos) < profileseq.getLength())
1139 // pad the final region with gaps.
1140 StringBuffer sb = new StringBuffer();
1141 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1145 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1147 SequenceI sqobj = al.getSequenceAt(s);
1148 if (sqobj == profileseq)
1152 String sq = sqobj.getSequenceAsString();
1154 int diff = origseq.getLength() - sq.length();
1158 // + ((diff >= sb.length()) ? sb.toString() : sb
1159 // .substring(0, diff));
1160 if (diff >= sb.length())
1162 sq += sb.toString();
1166 char[] buf = new char[diff];
1167 sb.getChars(0, diff, buf, 0);
1168 sq += buf.toString();
1170 diff = origseq.getLength() - sq.length();
1177 * remove any hiddenColumns or selected columns and shift remaining based on a
1178 * series of position, range deletions.
1182 private void pruneDeletions(ShiftList deletions)
1184 if (deletions != null)
1186 final List<int[]> shifts = deletions.getShifts();
1187 if (shifts != null && shifts.size() > 0)
1189 pruneDeletions(shifts);
1191 // and shift the rest.
1192 this.compensateForEdits(deletions);
1198 * Adjust hidden column boundaries based on a series of column additions or
1199 * deletions in visible regions.
1201 * @param shiftrecord
1204 private ShiftList compensateForEdits(ShiftList shiftrecord)
1206 if (shiftrecord != null)
1208 final List<int[]> shifts = shiftrecord.getShifts();
1209 if (shifts != null && shifts.size() > 0)
1212 for (int i = 0, j = shifts.size(); i < j; i++)
1214 int[] sh = shifts.get(i);
1215 compensateForDelEdits(shifted + sh[0], sh[1]);
1219 return shiftrecord.getInverse();
1225 * Returns a hashCode built from hidden column ranges
1227 public int hashCode(int hc)
1230 if (hiddenColumns != null)
1232 for (int[] hidden : hiddenColumns)
1234 hashCode = 31 * hashCode + hidden[0];
1235 hashCode = 31 * hashCode + hidden[1];