1 package jalview.datamodel;
3 import jalview.util.Comparison;
5 import java.util.ArrayList;
6 import java.util.Collections;
8 import java.util.Vector;
10 public class HiddenColumns
13 * list of hidden column [start, end] ranges; the list is maintained in
14 * ascending start column order
16 private Vector<int[]> hiddenColumns;
19 * This Method is used to return all the HiddenColumn regions
21 * @return empty list or List of hidden column intervals
23 public List<int[]> getHiddenRegions()
25 return hiddenColumns == null ? Collections.<int[]> emptyList()
30 * Find the number of hidden columns
32 * @return number of hidden columns
39 for (int[] range : hiddenColumns)
41 size += range[1] - range[0] + 1;
48 * Answers if there are any hidden columns
50 * @return true if there are hidden columns
52 public boolean hasHidden()
54 return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
58 public boolean equals(Object obj)
60 if (!(obj instanceof HiddenColumns))
64 HiddenColumns that = (HiddenColumns) obj;
67 * check hidden columns are either both null, or match
69 if (this.hiddenColumns == null)
71 return (that.hiddenColumns == null);
73 if (that.hiddenColumns == null
74 || that.hiddenColumns.size() != this.hiddenColumns.size())
79 for (int[] thisRange : hiddenColumns)
81 int[] thatRange = that.hiddenColumns.get(i++);
82 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
91 * Return absolute column index for a visible column index
94 * int column index in alignment view (count from zero)
95 * @return alignment column index for column
97 public int adjustForHiddenColumns(int column)
100 if (hiddenColumns != null)
102 for (int i = 0; i < hiddenColumns.size(); i++)
104 int[] region = hiddenColumns.elementAt(i);
105 if (result >= region[0])
107 result += region[1] - region[0] + 1;
115 * Use this method to find out where a column will appear in the visible
116 * alignment when hidden columns exist. If the column is not visible, then the
117 * left-most visible column will always be returned.
119 * @param hiddenColumn
120 * the column index in the full alignment including hidden columns
121 * @return the position of the column in the visible alignment
123 public int findColumnPosition(int hiddenColumn)
125 int result = hiddenColumn;
126 if (hiddenColumns != null)
132 region = hiddenColumns.elementAt(index++);
133 if (hiddenColumn > region[1])
135 result -= region[1] + 1 - region[0];
137 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
139 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
141 // Here the hidden column is within a region, so
142 // we want to return the position of region[0]-1, adjusted for any
143 // earlier hidden columns.
144 // Calculate the difference between the actual hidden col position
145 // and region[0]-1, and then subtract from result to convert result from
146 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
148 // However, if the region begins at 0 we cannot return region[0]-1
156 return result - (hiddenColumn - region[0] + 1);
160 return result; // return the shifted position after removing hidden columns.
164 * Find the visible column which is a given visible number of columns to the
165 * left of another visible column. i.e. for a startColumn x, the column which
166 * is distance 1 away will be column x-1.
168 * @param visibleDistance
169 * the number of visible columns to offset by
171 * the column to start from
172 * @return the position of the column in the visible alignment
174 public int subtractVisibleColumns(int visibleDistance, int startColumn)
176 int distance = visibleDistance;
178 // in case startColumn is in a hidden region, move it to the left
179 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
181 // get index of hidden region to left of start
182 int index = getHiddenIndexLeft(start);
185 // no hidden regions to left of startColumn
186 return start - distance;
189 // walk backwards through the alignment subtracting the counts of visible
190 // columns from distance
193 int nextstart = start;
195 while ((index > -1) && (distance - gap > 0))
197 // subtract the gap to right of region from distance
201 // calculate the next gap
202 region = hiddenColumns.get(index);
203 gap = start - region[1];
205 // set start to just to left of current region
206 nextstart = region[0] - 1;
210 if (distance - gap > 0)
212 // fell out of loop because there are no more hidden regions
214 return nextstart - distance;
216 return start - distance;
221 * Use this method to determine where the next hiddenRegion starts
223 * @param hiddenRegion
224 * index of hidden region (counts from 0)
225 * @return column number in visible view
227 public int findHiddenRegionPosition(int hiddenRegion)
230 if (hiddenColumns != null)
236 int[] region = hiddenColumns.elementAt(index);
237 if (hiddenRegion == 0)
242 gaps += region[1] + 1 - region[0];
243 result = region[1] + 1;
245 } while (index <= hiddenRegion);
254 * This method returns the rightmost limit of a region of an alignment with
255 * hidden columns. In otherwords, the next hidden column.
260 public int getHiddenBoundaryRight(int alPos)
262 if (hiddenColumns != null)
267 int[] region = hiddenColumns.elementAt(index);
268 if (alPos < region[0])
274 } while (index < hiddenColumns.size());
282 * This method returns the leftmost limit of a region of an alignment with
283 * hidden columns. In otherwords, the previous hidden column.
288 public int getHiddenBoundaryLeft(int alPos)
290 if (hiddenColumns != null)
292 int index = hiddenColumns.size() - 1;
295 int[] region = hiddenColumns.elementAt(index);
296 if (alPos > region[1])
302 } while (index > -1);
310 * This method returns the index of the hidden region to the left of a column
311 * position. If the column is in a hidden region it returns the index of the
312 * region to the left. If there is no hidden region to the left it returns -1.
317 private int getHiddenIndexLeft(int pos)
319 if (hiddenColumns != null)
321 int index = hiddenColumns.size() - 1;
324 int[] region = hiddenColumns.elementAt(index);
331 } while (index > -1);
339 * Adds the specified column range to the hidden columns
344 public void hideColumns(int start, int end)
346 if (hiddenColumns == null)
348 hiddenColumns = new Vector<int[]>();
352 * traverse existing hidden ranges and insert / amend / append as
355 for (int i = 0; i < hiddenColumns.size(); i++)
357 int[] region = hiddenColumns.elementAt(i);
359 if (end < region[0] - 1)
362 * insert discontiguous preceding range
364 hiddenColumns.insertElementAt(new int[] { start, end }, i);
368 if (end <= region[1])
371 * new range overlaps existing, or is contiguous preceding it - adjust
374 region[0] = Math.min(region[0], start);
378 if (start <= region[1] + 1)
381 * new range overlaps existing, or is contiguous following it - adjust
382 * start and end columns
384 region[0] = Math.min(region[0], start);
385 region[1] = Math.max(region[1], end);
388 * also update or remove any subsequent ranges
389 * that are overlapped
391 while (i < hiddenColumns.size() - 1)
393 int[] nextRegion = hiddenColumns.get(i + 1);
394 if (nextRegion[0] > end + 1)
397 * gap to next hidden range - no more to update
401 region[1] = Math.max(nextRegion[1], end);
402 hiddenColumns.remove(i + 1);
409 * remaining case is that the new range follows everything else
411 hiddenColumns.addElement(new int[] { start, end });
414 public boolean isVisible(int column)
416 if (hiddenColumns != null)
418 for (int[] region : hiddenColumns)
420 if (column >= region[0] && column <= region[1])
433 public HiddenColumns()
442 public HiddenColumns(HiddenColumns copy)
446 if (copy.hiddenColumns != null)
448 hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
449 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
452 rh = copy.hiddenColumns.elementAt(i);
455 cp = new int[rh.length];
456 System.arraycopy(rh, 0, cp, 0, rh.length);
457 hiddenColumns.addElement(cp);
465 * propagate shift in alignment columns to column selection
470 * shift in edit (+ve for removal, or -ve for inserts)
472 public List<int[]> compensateForEdit(int start, int change,
475 List<int[]> deletedHiddenColumns = null;
477 if (hiddenColumns != null)
479 deletedHiddenColumns = new ArrayList<int[]>();
480 int hSize = hiddenColumns.size();
481 for (int i = 0; i < hSize; i++)
483 int[] region = hiddenColumns.elementAt(i);
484 if (region[0] > start && start + change > region[1])
486 deletedHiddenColumns.add(region);
488 hiddenColumns.removeElementAt(i);
494 if (region[0] > start)
507 this.revealHiddenColumns(0, sel);
510 return deletedHiddenColumns;
514 * propagate shift in alignment columns to column selection special version of
515 * compensateForEdit - allowing for edits within hidden regions
520 * shift in edit (+ve for removal, or -ve for inserts)
522 public void compensateForDelEdits(int start, int change)
524 if (hiddenColumns != null)
526 for (int i = 0; i < hiddenColumns.size(); i++)
528 int[] region = hiddenColumns.elementAt(i);
529 if (region[0] >= start)
533 if (region[1] >= start)
537 if (region[1] < region[0])
539 hiddenColumns.removeElementAt(i--);
555 * return all visible segments between the given start and end boundaries
558 * (first column inclusive from 0)
560 * (last column - not inclusive)
561 * @return int[] {i_start, i_end, ..} where intervals lie in
562 * start<=i_start<=i_end<end
564 public int[] getVisibleContigs(int start, int end)
566 if (hiddenColumns != null && hiddenColumns.size() > 0)
568 List<int[]> visiblecontigs = new ArrayList<int[]>();
569 List<int[]> regions = getHiddenRegions();
573 int hideStart, hideEnd;
575 for (int j = 0; vstart < end && j < regions.size(); j++)
577 region = regions.get(j);
578 hideStart = region[0];
581 if (hideEnd < vstart)
585 if (hideStart > vstart)
587 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
589 vstart = hideEnd + 1;
594 visiblecontigs.add(new int[] { vstart, end - 1 });
596 int[] vcontigs = new int[visiblecontigs.size() * 2];
597 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
599 int[] vc = visiblecontigs.get(i);
600 visiblecontigs.set(i, null);
601 vcontigs[i * 2] = vc[0];
602 vcontigs[i * 2 + 1] = vc[1];
604 visiblecontigs.clear();
609 return new int[] { start, end - 1 };
613 public String[] getVisibleSequenceStrings(int start, int end,
616 int i, iSize = seqs.length;
617 String selections[] = new String[iSize];
618 if (hiddenColumns != null && hiddenColumns.size() > 0)
620 for (i = 0; i < iSize; i++)
622 StringBuffer visibleSeq = new StringBuffer();
623 List<int[]> regions = getHiddenRegions();
625 int blockStart = start, blockEnd = end;
627 int hideStart, hideEnd;
629 for (int j = 0; j < regions.size(); j++)
631 region = regions.get(j);
632 hideStart = region[0];
635 if (hideStart < start)
640 blockStart = Math.min(blockStart, hideEnd + 1);
641 blockEnd = Math.min(blockEnd, hideStart);
643 if (blockStart > blockEnd)
648 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
650 blockStart = hideEnd + 1;
654 if (end > blockStart)
656 visibleSeq.append(seqs[i].getSequence(blockStart, end));
659 selections[i] = visibleSeq.toString();
664 for (i = 0; i < iSize; i++)
666 selections[i] = seqs[i].getSequenceAsString(start, end);
674 * Locate the first and last position visible for this sequence. if seq isn't
675 * visible then return the position of the left and right of the hidden
676 * boundary region, and the corresponding alignment column indices for the
677 * extent of the sequence
680 * @return int[] { visible start, visible end, first seqpos, last seqpos,
681 * alignment index for seq start, alignment index for seq end }
683 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
685 int fpos = seq.getStart(), lpos = seq.getEnd();
688 if (hiddenColumns == null || hiddenColumns.size() == 0)
690 int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;
691 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
694 // Simply walk along the sequence whilst watching for hidden column
696 List<int[]> regions = getHiddenRegions();
697 int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
698 .getLength(), hideEnd = -1;
699 int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
700 boolean foundStart = false;
701 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
704 if (!Comparison.isGap(seq.getCharAt(p)))
706 // keep track of first/last column
707 // containing sequence data regardless of visibility
713 // update hidden region start/end
714 while (hideEnd < p && rcount < regions.size())
716 int[] region = regions.get(rcount++);
718 visNext += region[0] - visPrev;
719 hideStart = region[0];
724 hideStart = seq.getLength();
726 // update visible boundary for sequence
738 // look for next sequence position
744 return new int[] { findColumnPosition(start),
745 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
747 // otherwise, sequence was completely hidden
748 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
752 * delete any columns in alignmentAnnotation that are hidden (including
753 * sequence associated annotation).
755 * @param alignmentAnnotation
757 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
759 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
763 * delete any columns in alignmentAnnotation that are hidden (including
764 * sequence associated annotation).
767 * remove any annotation to the right of this column
769 * remove any annotation to the left of this column
770 * @param alignmentAnnotation
771 * the annotation to operate on
773 public void makeVisibleAnnotation(int start, int end,
774 AlignmentAnnotation alignmentAnnotation)
776 if (alignmentAnnotation.annotations == null)
780 if (start == end && end == -1)
783 end = alignmentAnnotation.annotations.length;
785 if (hiddenColumns != null && hiddenColumns.size() > 0)
787 // then mangle the alignmentAnnotation annotation array
788 Vector<Annotation[]> annels = new Vector<Annotation[]>();
789 Annotation[] els = null;
790 List<int[]> regions = getHiddenRegions();
791 int blockStart = start, blockEnd = end;
793 int hideStart, hideEnd, w = 0;
795 for (int j = 0; j < regions.size(); j++)
797 region = regions.get(j);
798 hideStart = region[0];
801 if (hideStart < start)
806 blockStart = Math.min(blockStart, hideEnd + 1);
807 blockEnd = Math.min(blockEnd, hideStart);
809 if (blockStart > blockEnd)
814 annels.addElement(els = new Annotation[blockEnd - blockStart]);
815 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
818 blockStart = hideEnd + 1;
822 if (end > blockStart)
824 annels.addElement(els = new Annotation[end - blockStart + 1]);
825 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
827 // copy just the visible segment of the annotation row
828 System.arraycopy(alignmentAnnotation.annotations, blockStart,
833 // copy to the end of the annotation row
834 System.arraycopy(alignmentAnnotation.annotations, blockStart,
836 (alignmentAnnotation.annotations.length - blockStart));
845 alignmentAnnotation.annotations = new Annotation[w];
848 for (Annotation[] chnk : annels)
850 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
857 alignmentAnnotation.restrict(start, end);
863 * @return true if there are columns hidden
865 public boolean hasHiddenColumns()
867 return hiddenColumns != null && hiddenColumns.size() > 0;
872 * @return true if there are more than one set of columns hidden
874 public boolean hasManyHiddenColumns()
876 return hiddenColumns != null && hiddenColumns.size() > 1;
880 * mark the columns corresponding to gap characters as hidden in the column
885 public void hideInsertionsFor(SequenceI sr)
887 List<int[]> inserts = sr.getInsertions();
888 for (int[] r : inserts)
890 hideColumns(r[0], r[1]);
895 * Unhides, and adds to the selection list, all hidden columns
897 public void revealAllHiddenColumns(ColumnSelection sel)
899 if (hiddenColumns != null)
901 for (int i = 0; i < hiddenColumns.size(); i++)
903 int[] region = hiddenColumns.elementAt(i);
904 for (int j = region[0]; j < region[1] + 1; j++)
911 hiddenColumns = null;
915 * Reveals, and marks as selected, the hidden column range with the given
920 public void revealHiddenColumns(int start, ColumnSelection sel)
922 for (int i = 0; i < hiddenColumns.size(); i++)
924 int[] region = hiddenColumns.elementAt(i);
925 if (start == region[0])
927 for (int j = region[0]; j < region[1] + 1; j++)
932 hiddenColumns.removeElement(region);
936 if (hiddenColumns.size() == 0)
938 hiddenColumns = null;
943 * removes intersection of position,length ranges in deletions from the
944 * start,end regions marked in intervals.
950 private boolean pruneIntervalVector(final List<int[]> shifts,
951 Vector<int[]> intervals)
953 boolean pruned = false;
954 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
955 int hr[] = intervals.elementAt(i);
956 int sr[] = shifts.get(s);
957 while (i <= j && s <= t)
959 boolean trailinghn = hr[1] >= sr[0];
964 hr = intervals.elementAt(++i);
972 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
973 if (endshift < hr[0] || endshift < sr[0])
974 { // leadinghc disjoint or not a deletion
977 sr = shifts.get(++s);
985 boolean leadinghn = hr[0] >= sr[0];
986 boolean leadinghc = hr[0] < endshift;
987 boolean trailinghc = hr[1] < endshift;
991 { // deleted hidden region.
992 intervals.removeElementAt(i);
997 hr = intervals.elementAt(i);
1003 hr[0] = endshift; // clip c terminal region
1004 leadinghn = !leadinghn;
1020 // sr contained in hr
1023 sr = shifts.get(++s);
1033 return pruned; // true if any interval was removed or modified by
1038 * remove any hiddenColumns or selected columns and shift remaining based on a
1039 * series of position, range deletions.
1043 public void pruneDeletions(List<int[]> shifts)
1045 // delete any intervals intersecting.
1046 if (hiddenColumns != null)
1048 pruneIntervalVector(shifts, hiddenColumns);
1049 if (hiddenColumns != null && hiddenColumns.size() == 0)
1051 hiddenColumns = null;
1057 * Returns a hashCode built from selected columns and hidden column ranges
1059 public int hashCode(int hc)
1062 if (hiddenColumns != null)
1064 for (int[] hidden : hiddenColumns)
1066 hashCode = 31 * hashCode + hidden[0];
1067 hashCode = 31 * hashCode + hidden[1];