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[]> getListOfCols()
25 return hiddenColumns == null ? Collections.<int[]> emptyList()
29 public boolean hasHidden()
31 return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
35 public boolean equals(Object obj)
37 if (!(obj instanceof HiddenColumns))
41 HiddenColumns that = (HiddenColumns) obj;
44 * check hidden columns are either both null, or match
46 if (this.hiddenColumns == null)
48 return (that.hiddenColumns == null);
50 if (that.hiddenColumns == null
51 || that.hiddenColumns.size() != this.hiddenColumns.size())
56 for (int[] thisRange : hiddenColumns)
58 int[] thatRange = that.hiddenColumns.get(i++);
59 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
68 * Return absolute column index for a visible column index
71 * int column index in alignment view (count from zero)
72 * @return alignment column index for column
74 public int adjustForHiddenColumns(int column)
77 if (hiddenColumns != null)
79 for (int i = 0; i < hiddenColumns.size(); i++)
81 int[] region = hiddenColumns.elementAt(i);
82 if (result >= region[0])
84 result += region[1] - region[0] + 1;
92 * Use this method to find out where a column will appear in the visible
93 * alignment when hidden columns exist. If the column is not visible, then the
94 * left-most visible column will always be returned.
97 * the column index in the full alignment including hidden columns
98 * @return the position of the column in the visible alignment
100 public int findColumnPosition(int hiddenColumn)
102 int result = hiddenColumn;
103 if (hiddenColumns != null)
109 region = hiddenColumns.elementAt(index++);
110 if (hiddenColumn > region[1])
112 result -= region[1] + 1 - region[0];
114 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
116 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
118 // Here the hidden column is within a region, so
119 // we want to return the position of region[0]-1, adjusted for any
120 // earlier hidden columns.
121 // Calculate the difference between the actual hidden col position
122 // and region[0]-1, and then subtract from result to convert result from
123 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
125 // However, if the region begins at 0 we cannot return region[0]-1
133 return result - (hiddenColumn - region[0] + 1);
137 return result; // return the shifted position after removing hidden columns.
141 * Find the visible column which is a given visible number of columns to the
142 * left of another visible column. i.e. for a startColumn x, the column which
143 * is distance 1 away will be column x-1.
145 * @param visibleDistance
146 * the number of visible columns to offset by
148 * the column to start from
149 * @return the position of the column in the visible alignment
151 public int subtractVisibleColumns(int visibleDistance, int startColumn)
153 int distance = visibleDistance;
155 // in case startColumn is in a hidden region, move it to the left
156 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
158 // get index of hidden region to left of start
159 int index = getHiddenIndexLeft(start);
162 // no hidden regions to left of startColumn
163 return start - distance;
166 // walk backwards through the alignment subtracting the counts of visible
167 // columns from distance
170 int nextstart = start;
172 while ((index > -1) && (distance - gap > 0))
174 // subtract the gap to right of region from distance
178 // calculate the next gap
179 region = hiddenColumns.get(index);
180 gap = start - region[1];
182 // set start to just to left of current region
183 nextstart = region[0] - 1;
187 if (distance - gap > 0)
189 // fell out of loop because there are no more hidden regions
191 return nextstart - distance;
193 return start - distance;
198 * Use this method to determine where the next hiddenRegion starts
200 * @param hiddenRegion
201 * index of hidden region (counts from 0)
202 * @return column number in visible view
204 public int findHiddenRegionPosition(int hiddenRegion)
207 if (hiddenColumns != null)
213 int[] region = hiddenColumns.elementAt(index);
214 if (hiddenRegion == 0)
219 gaps += region[1] + 1 - region[0];
220 result = region[1] + 1;
222 } while (index <= hiddenRegion);
231 * This method returns the rightmost limit of a region of an alignment with
232 * hidden columns. In otherwords, the next hidden column.
237 public int getHiddenBoundaryRight(int alPos)
239 if (hiddenColumns != null)
244 int[] region = hiddenColumns.elementAt(index);
245 if (alPos < region[0])
251 } while (index < hiddenColumns.size());
259 * This method returns the leftmost limit of a region of an alignment with
260 * hidden columns. In otherwords, the previous hidden column.
265 public int getHiddenBoundaryLeft(int alPos)
267 if (hiddenColumns != null)
269 int index = hiddenColumns.size() - 1;
272 int[] region = hiddenColumns.elementAt(index);
273 if (alPos > region[1])
279 } while (index > -1);
287 * This method returns the index of the hidden region to the left of a column
288 * position. If the column is in a hidden region it returns the index of the
289 * region to the left. If there is no hidden region to the left it returns -1.
294 private int getHiddenIndexLeft(int pos)
296 if (hiddenColumns != null)
298 int index = hiddenColumns.size() - 1;
301 int[] region = hiddenColumns.elementAt(index);
308 } while (index > -1);
316 * Adds the specified column range to the hidden columns
321 public void hideColumns(int start, int end)
323 if (hiddenColumns == null)
325 hiddenColumns = new Vector<int[]>();
329 * traverse existing hidden ranges and insert / amend / append as
332 for (int i = 0; i < hiddenColumns.size(); i++)
334 int[] region = hiddenColumns.elementAt(i);
336 if (end < region[0] - 1)
339 * insert discontiguous preceding range
341 hiddenColumns.insertElementAt(new int[] { start, end }, i);
345 if (end <= region[1])
348 * new range overlaps existing, or is contiguous preceding it - adjust
351 region[0] = Math.min(region[0], start);
355 if (start <= region[1] + 1)
358 * new range overlaps existing, or is contiguous following it - adjust
359 * start and end columns
361 region[0] = Math.min(region[0], start);
362 region[1] = Math.max(region[1], end);
365 * also update or remove any subsequent ranges
366 * that are overlapped
368 while (i < hiddenColumns.size() - 1)
370 int[] nextRegion = hiddenColumns.get(i + 1);
371 if (nextRegion[0] > end + 1)
374 * gap to next hidden range - no more to update
378 region[1] = Math.max(nextRegion[1], end);
379 hiddenColumns.remove(i + 1);
386 * remaining case is that the new range follows everything else
388 hiddenColumns.addElement(new int[] { start, end });
391 public boolean isVisible(int column)
393 if (hiddenColumns != null)
395 for (int[] region : hiddenColumns)
397 if (column >= region[0] && column <= region[1])
410 public HiddenColumns()
419 public HiddenColumns(HiddenColumns copy)
423 if (copy.hiddenColumns != null)
425 hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
426 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
429 rh = copy.hiddenColumns.elementAt(i);
432 cp = new int[rh.length];
433 System.arraycopy(rh, 0, cp, 0, rh.length);
434 hiddenColumns.addElement(cp);
442 * propagate shift in alignment columns to column selection
447 * shift in edit (+ve for removal, or -ve for inserts)
449 public List<int[]> compensateForEdit(int start, int change,
452 List<int[]> deletedHiddenColumns = null;
454 if (hiddenColumns != null)
456 deletedHiddenColumns = new ArrayList<int[]>();
457 int hSize = hiddenColumns.size();
458 for (int i = 0; i < hSize; i++)
460 int[] region = hiddenColumns.elementAt(i);
461 if (region[0] > start && start + change > region[1])
463 deletedHiddenColumns.add(region);
465 hiddenColumns.removeElementAt(i);
471 if (region[0] > start)
484 this.revealHiddenColumns(0, sel);
487 return deletedHiddenColumns;
491 * propagate shift in alignment columns to column selection special version of
492 * compensateForEdit - allowing for edits within hidden regions
497 * shift in edit (+ve for removal, or -ve for inserts)
499 public void compensateForDelEdits(int start, int change)
501 if (hiddenColumns != null)
503 for (int i = 0; i < hiddenColumns.size(); i++)
505 int[] region = hiddenColumns.elementAt(i);
506 if (region[0] >= start)
510 if (region[1] >= start)
514 if (region[1] < region[0])
516 hiddenColumns.removeElementAt(i--);
532 * return all visible segments between the given start and end boundaries
535 * (first column inclusive from 0)
537 * (last column - not inclusive)
538 * @return int[] {i_start, i_end, ..} where intervals lie in
539 * start<=i_start<=i_end<end
541 public int[] getVisibleContigs(int start, int end)
543 if (hiddenColumns != null && hiddenColumns.size() > 0)
545 List<int[]> visiblecontigs = new ArrayList<int[]>();
546 List<int[]> regions = getListOfCols();
550 int hideStart, hideEnd;
552 for (int j = 0; vstart < end && j < regions.size(); j++)
554 region = regions.get(j);
555 hideStart = region[0];
558 if (hideEnd < vstart)
562 if (hideStart > vstart)
564 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
566 vstart = hideEnd + 1;
571 visiblecontigs.add(new int[] { vstart, end - 1 });
573 int[] vcontigs = new int[visiblecontigs.size() * 2];
574 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
576 int[] vc = visiblecontigs.get(i);
577 visiblecontigs.set(i, null);
578 vcontigs[i * 2] = vc[0];
579 vcontigs[i * 2 + 1] = vc[1];
581 visiblecontigs.clear();
586 return new int[] { start, end - 1 };
590 public String[] getVisibleSequenceStrings(int start, int end,
593 int i, iSize = seqs.length;
594 String selections[] = new String[iSize];
595 if (hiddenColumns != null && hiddenColumns.size() > 0)
597 for (i = 0; i < iSize; i++)
599 StringBuffer visibleSeq = new StringBuffer();
600 List<int[]> regions = getListOfCols();
602 int blockStart = start, blockEnd = end;
604 int hideStart, hideEnd;
606 for (int j = 0; j < regions.size(); j++)
608 region = regions.get(j);
609 hideStart = region[0];
612 if (hideStart < start)
617 blockStart = Math.min(blockStart, hideEnd + 1);
618 blockEnd = Math.min(blockEnd, hideStart);
620 if (blockStart > blockEnd)
625 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
627 blockStart = hideEnd + 1;
631 if (end > blockStart)
633 visibleSeq.append(seqs[i].getSequence(blockStart, end));
636 selections[i] = visibleSeq.toString();
641 for (i = 0; i < iSize; i++)
643 selections[i] = seqs[i].getSequenceAsString(start, end);
651 * Locate the first and last position visible for this sequence. if seq isn't
652 * visible then return the position of the left and right of the hidden
653 * boundary region, and the corresponding alignment column indices for the
654 * extent of the sequence
657 * @return int[] { visible start, visible end, first seqpos, last seqpos,
658 * alignment index for seq start, alignment index for seq end }
660 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
662 int fpos = seq.getStart(), lpos = seq.getEnd();
665 if (hiddenColumns == null || hiddenColumns.size() == 0)
667 int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;
668 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
671 // Simply walk along the sequence whilst watching for hidden column
673 List<int[]> regions = getListOfCols();
674 int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
675 .getLength(), hideEnd = -1;
676 int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
677 boolean foundStart = false;
678 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
681 if (!Comparison.isGap(seq.getCharAt(p)))
683 // keep track of first/last column
684 // containing sequence data regardless of visibility
690 // update hidden region start/end
691 while (hideEnd < p && rcount < regions.size())
693 int[] region = regions.get(rcount++);
695 visNext += region[0] - visPrev;
696 hideStart = region[0];
701 hideStart = seq.getLength();
703 // update visible boundary for sequence
715 // look for next sequence position
721 return new int[] { findColumnPosition(start),
722 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
724 // otherwise, sequence was completely hidden
725 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
729 * delete any columns in alignmentAnnotation that are hidden (including
730 * sequence associated annotation).
732 * @param alignmentAnnotation
734 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
736 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
740 * delete any columns in alignmentAnnotation that are hidden (including
741 * sequence associated annotation).
744 * remove any annotation to the right of this column
746 * remove any annotation to the left of this column
747 * @param alignmentAnnotation
748 * the annotation to operate on
750 public void makeVisibleAnnotation(int start, int end,
751 AlignmentAnnotation alignmentAnnotation)
753 if (alignmentAnnotation.annotations == null)
757 if (start == end && end == -1)
760 end = alignmentAnnotation.annotations.length;
762 if (hiddenColumns != null && hiddenColumns.size() > 0)
764 // then mangle the alignmentAnnotation annotation array
765 Vector<Annotation[]> annels = new Vector<Annotation[]>();
766 Annotation[] els = null;
767 List<int[]> regions = getListOfCols();
768 int blockStart = start, blockEnd = end;
770 int hideStart, hideEnd, w = 0;
772 for (int j = 0; j < regions.size(); j++)
774 region = regions.get(j);
775 hideStart = region[0];
778 if (hideStart < start)
783 blockStart = Math.min(blockStart, hideEnd + 1);
784 blockEnd = Math.min(blockEnd, hideStart);
786 if (blockStart > blockEnd)
791 annels.addElement(els = new Annotation[blockEnd - blockStart]);
792 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
795 blockStart = hideEnd + 1;
799 if (end > blockStart)
801 annels.addElement(els = new Annotation[end - blockStart + 1]);
802 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
804 // copy just the visible segment of the annotation row
805 System.arraycopy(alignmentAnnotation.annotations, blockStart,
810 // copy to the end of the annotation row
811 System.arraycopy(alignmentAnnotation.annotations, blockStart,
813 (alignmentAnnotation.annotations.length - blockStart));
822 alignmentAnnotation.annotations = new Annotation[w];
825 for (Annotation[] chnk : annels)
827 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
834 alignmentAnnotation.restrict(start, end);
840 * @return true if there are columns hidden
842 public boolean hasHiddenColumns()
844 return hiddenColumns != null && hiddenColumns.size() > 0;
849 * @return true if there are more than one set of columns hidden
851 public boolean hasManyHiddenColumns()
853 return hiddenColumns != null && hiddenColumns.size() > 1;
857 * mark the columns corresponding to gap characters as hidden in the column
862 public void hideInsertionsFor(SequenceI sr)
864 List<int[]> inserts = sr.getInsertions();
865 for (int[] r : inserts)
867 hideColumns(r[0], r[1]);
872 * Unhides, and adds to the selection list, all hidden columns
874 public void revealAllHiddenColumns(ColumnSelection sel)
876 if (hiddenColumns != null)
878 for (int i = 0; i < hiddenColumns.size(); i++)
880 int[] region = hiddenColumns.elementAt(i);
881 for (int j = region[0]; j < region[1] + 1; j++)
888 hiddenColumns = null;
892 * Reveals, and marks as selected, the hidden column range with the given
897 public void revealHiddenColumns(int start, ColumnSelection sel)
899 for (int i = 0; i < hiddenColumns.size(); i++)
901 int[] region = hiddenColumns.elementAt(i);
902 if (start == region[0])
904 for (int j = region[0]; j < region[1] + 1; j++)
909 hiddenColumns.removeElement(region);
913 if (hiddenColumns.size() == 0)
915 hiddenColumns = null;
920 * removes intersection of position,length ranges in deletions from the
921 * start,end regions marked in intervals.
927 private boolean pruneIntervalVector(final List<int[]> shifts,
928 Vector<int[]> intervals)
930 boolean pruned = false;
931 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
932 int hr[] = intervals.elementAt(i);
933 int sr[] = shifts.get(s);
934 while (i <= j && s <= t)
936 boolean trailinghn = hr[1] >= sr[0];
941 hr = intervals.elementAt(++i);
949 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
950 if (endshift < hr[0] || endshift < sr[0])
951 { // leadinghc disjoint or not a deletion
954 sr = shifts.get(++s);
962 boolean leadinghn = hr[0] >= sr[0];
963 boolean leadinghc = hr[0] < endshift;
964 boolean trailinghc = hr[1] < endshift;
968 { // deleted hidden region.
969 intervals.removeElementAt(i);
974 hr = intervals.elementAt(i);
980 hr[0] = endshift; // clip c terminal region
981 leadinghn = !leadinghn;
997 // sr contained in hr
1000 sr = shifts.get(++s);
1010 return pruned; // true if any interval was removed or modified by
1015 * remove any hiddenColumns or selected columns and shift remaining based on a
1016 * series of position, range deletions.
1020 public void pruneDeletions(List<int[]> shifts)
1022 // delete any intervals intersecting.
1023 if (hiddenColumns != null)
1025 pruneIntervalVector(shifts, hiddenColumns);
1026 if (hiddenColumns != null && hiddenColumns.size() == 0)
1028 hiddenColumns = null;
1034 * Returns a hashCode built from selected columns and hidden column ranges
1036 public int hashCode(int hc)
1039 if (hiddenColumns != null)
1041 for (int[] hidden : hiddenColumns)
1043 hashCode = 31 * hashCode + hidden[0];
1044 hashCode = 31 * hashCode + hidden[1];