1 package jalview.datamodel;
3 import jalview.util.Comparison;
4 import jalview.util.ShiftList;
6 import java.util.ArrayList;
7 import java.util.BitSet;
8 import java.util.Collections;
10 import java.util.Vector;
12 public class HiddenColumns
15 * list of hidden column [start, end] ranges; the list is maintained in
16 * ascending start column order
18 private Vector<int[]> hiddenColumns;
21 * This Method is used to return all the HiddenColumn regions
23 * @return empty list or List of hidden column intervals
25 public List<int[]> getHiddenRegions()
27 return hiddenColumns == null ? Collections.<int[]> emptyList()
32 * Find the number of hidden columns
34 * @return number of hidden columns
41 for (int[] range : hiddenColumns)
43 size += range[1] - range[0] + 1;
50 * Answers if there are any hidden columns
52 * @return true if there are hidden columns
54 public boolean hasHidden()
56 return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
60 public boolean equals(Object obj)
62 if (!(obj instanceof HiddenColumns))
66 HiddenColumns that = (HiddenColumns) obj;
69 * check hidden columns are either both null, or match
71 if (this.hiddenColumns == null)
73 return (that.hiddenColumns == null);
75 if (that.hiddenColumns == null
76 || that.hiddenColumns.size() != this.hiddenColumns.size())
81 for (int[] thisRange : hiddenColumns)
83 int[] thatRange = that.hiddenColumns.get(i++);
84 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
93 * Return absolute column index for a visible column index
96 * int column index in alignment view (count from zero)
97 * @return alignment column index for column
99 public int adjustForHiddenColumns(int column)
102 if (hiddenColumns != null)
104 for (int i = 0; i < hiddenColumns.size(); i++)
106 int[] region = hiddenColumns.elementAt(i);
107 if (result >= region[0])
109 result += region[1] - region[0] + 1;
117 * Use this method to find out where a column will appear in the visible
118 * alignment when hidden columns exist. If the column is not visible, then the
119 * left-most visible column will always be returned.
121 * @param hiddenColumn
122 * the column index in the full alignment including hidden columns
123 * @return the position of the column in the visible alignment
125 public int findColumnPosition(int hiddenColumn)
127 int result = hiddenColumn;
128 if (hiddenColumns != null)
134 region = hiddenColumns.elementAt(index++);
135 if (hiddenColumn > region[1])
137 result -= region[1] + 1 - region[0];
139 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
141 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
143 // Here the hidden column is within a region, so
144 // we want to return the position of region[0]-1, adjusted for any
145 // earlier hidden columns.
146 // Calculate the difference between the actual hidden col position
147 // and region[0]-1, and then subtract from result to convert result from
148 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
150 // However, if the region begins at 0 we cannot return region[0]-1
158 return result - (hiddenColumn - region[0] + 1);
162 return result; // return the shifted position after removing hidden columns.
166 * Find the visible column which is a given visible number of columns to the
167 * left of another visible column. i.e. for a startColumn x, the column which
168 * is distance 1 away will be column x-1.
170 * @param visibleDistance
171 * the number of visible columns to offset by
173 * the column to start from
174 * @return the position of the column in the visible alignment
176 public int subtractVisibleColumns(int visibleDistance, int startColumn)
178 int distance = visibleDistance;
180 // in case startColumn is in a hidden region, move it to the left
181 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
183 // get index of hidden region to left of start
184 int index = getHiddenIndexLeft(start);
187 // no hidden regions to left of startColumn
188 return start - distance;
191 // walk backwards through the alignment subtracting the counts of visible
192 // columns from distance
195 int nextstart = start;
197 while ((index > -1) && (distance - gap > 0))
199 // subtract the gap to right of region from distance
203 // calculate the next gap
204 region = hiddenColumns.get(index);
205 gap = start - region[1];
207 // set start to just to left of current region
208 nextstart = region[0] - 1;
212 if (distance - gap > 0)
214 // fell out of loop because there are no more hidden regions
216 return nextstart - distance;
218 return start - distance;
223 * Use this method to determine where the next hiddenRegion starts
225 * @param hiddenRegion
226 * index of hidden region (counts from 0)
227 * @return column number in visible view
229 public int findHiddenRegionPosition(int hiddenRegion)
232 if (hiddenColumns != null)
238 int[] region = hiddenColumns.elementAt(index);
239 if (hiddenRegion == 0)
244 gaps += region[1] + 1 - region[0];
245 result = region[1] + 1;
247 } while (index <= hiddenRegion);
256 * This method returns the rightmost limit of a region of an alignment with
257 * hidden columns. In otherwords, the next hidden column.
262 public int getHiddenBoundaryRight(int alPos)
264 if (hiddenColumns != null)
269 int[] region = hiddenColumns.elementAt(index);
270 if (alPos < region[0])
276 } while (index < hiddenColumns.size());
284 * This method returns the leftmost limit of a region of an alignment with
285 * hidden columns. In otherwords, the previous hidden column.
290 public int getHiddenBoundaryLeft(int alPos)
292 if (hiddenColumns != null)
294 int index = hiddenColumns.size() - 1;
297 int[] region = hiddenColumns.elementAt(index);
298 if (alPos > region[1])
304 } while (index > -1);
312 * This method returns the index of the hidden region to the left of a column
313 * position. If the column is in a hidden region it returns the index of the
314 * region to the left. If there is no hidden region to the left it returns -1.
319 private int getHiddenIndexLeft(int pos)
321 if (hiddenColumns != null)
323 int index = hiddenColumns.size() - 1;
326 int[] region = hiddenColumns.elementAt(index);
333 } while (index > -1);
341 * Adds the specified column range to the hidden columns
346 public void hideColumns(int start, int end)
348 if (hiddenColumns == null)
350 hiddenColumns = new Vector<int[]>();
354 * traverse existing hidden ranges and insert / amend / append as
357 for (int i = 0; i < hiddenColumns.size(); i++)
359 int[] region = hiddenColumns.elementAt(i);
361 if (end < region[0] - 1)
364 * insert discontiguous preceding range
366 hiddenColumns.insertElementAt(new int[] { start, end }, i);
370 if (end <= region[1])
373 * new range overlaps existing, or is contiguous preceding it - adjust
376 region[0] = Math.min(region[0], start);
380 if (start <= region[1] + 1)
383 * new range overlaps existing, or is contiguous following it - adjust
384 * start and end columns
386 region[0] = Math.min(region[0], start);
387 region[1] = Math.max(region[1], end);
390 * also update or remove any subsequent ranges
391 * that are overlapped
393 while (i < hiddenColumns.size() - 1)
395 int[] nextRegion = hiddenColumns.get(i + 1);
396 if (nextRegion[0] > end + 1)
399 * gap to next hidden range - no more to update
403 region[1] = Math.max(nextRegion[1], end);
404 hiddenColumns.remove(i + 1);
411 * remaining case is that the new range follows everything else
413 hiddenColumns.addElement(new int[] { start, end });
416 public boolean isVisible(int column)
418 if (hiddenColumns != null)
420 for (int[] region : hiddenColumns)
422 if (column >= region[0] && column <= region[1])
435 public HiddenColumns()
444 public HiddenColumns(HiddenColumns copy)
448 if (copy.hiddenColumns != null)
450 hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
451 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
454 rh = copy.hiddenColumns.elementAt(i);
457 cp = new int[rh.length];
458 System.arraycopy(rh, 0, cp, 0, rh.length);
459 hiddenColumns.addElement(cp);
467 * propagate shift in alignment columns to column selection
472 * shift in edit (+ve for removal, or -ve for inserts)
474 public List<int[]> compensateForEdit(int start, int change,
477 List<int[]> deletedHiddenColumns = null;
479 if (hiddenColumns != null)
481 deletedHiddenColumns = new ArrayList<int[]>();
482 int hSize = hiddenColumns.size();
483 for (int i = 0; i < hSize; i++)
485 int[] region = hiddenColumns.elementAt(i);
486 if (region[0] > start && start + change > region[1])
488 deletedHiddenColumns.add(region);
490 hiddenColumns.removeElementAt(i);
496 if (region[0] > start)
509 this.revealHiddenColumns(0, sel);
512 return deletedHiddenColumns;
516 * propagate shift in alignment columns to column selection special version of
517 * compensateForEdit - allowing for edits within hidden regions
522 * shift in edit (+ve for removal, or -ve for inserts)
524 public void compensateForDelEdits(int start, int change)
526 if (hiddenColumns != null)
528 for (int i = 0; i < hiddenColumns.size(); i++)
530 int[] region = hiddenColumns.elementAt(i);
531 if (region[0] >= start)
535 if (region[1] >= start)
539 if (region[1] < region[0])
541 hiddenColumns.removeElementAt(i--);
557 * return all visible segments between the given start and end boundaries
560 * (first column inclusive from 0)
562 * (last column - not inclusive)
563 * @return int[] {i_start, i_end, ..} where intervals lie in
564 * start<=i_start<=i_end<end
566 public int[] getVisibleContigs(int start, int end)
568 if (hiddenColumns != null && hiddenColumns.size() > 0)
570 List<int[]> visiblecontigs = new ArrayList<int[]>();
571 List<int[]> regions = getHiddenRegions();
575 int hideStart, hideEnd;
577 for (int j = 0; vstart < end && j < regions.size(); j++)
579 region = regions.get(j);
580 hideStart = region[0];
583 if (hideEnd < vstart)
587 if (hideStart > vstart)
589 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
591 vstart = hideEnd + 1;
596 visiblecontigs.add(new int[] { vstart, end - 1 });
598 int[] vcontigs = new int[visiblecontigs.size() * 2];
599 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
601 int[] vc = visiblecontigs.get(i);
602 visiblecontigs.set(i, null);
603 vcontigs[i * 2] = vc[0];
604 vcontigs[i * 2 + 1] = vc[1];
606 visiblecontigs.clear();
611 return new int[] { start, end - 1 };
615 public String[] getVisibleSequenceStrings(int start, int end,
618 int i, iSize = seqs.length;
619 String selections[] = new String[iSize];
620 if (hiddenColumns != null && hiddenColumns.size() > 0)
622 for (i = 0; i < iSize; i++)
624 StringBuffer visibleSeq = new StringBuffer();
625 List<int[]> regions = getHiddenRegions();
627 int blockStart = start, blockEnd = end;
629 int hideStart, hideEnd;
631 for (int j = 0; j < regions.size(); j++)
633 region = regions.get(j);
634 hideStart = region[0];
637 if (hideStart < start)
642 blockStart = Math.min(blockStart, hideEnd + 1);
643 blockEnd = Math.min(blockEnd, hideStart);
645 if (blockStart > blockEnd)
650 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
652 blockStart = hideEnd + 1;
656 if (end > blockStart)
658 visibleSeq.append(seqs[i].getSequence(blockStart, end));
661 selections[i] = visibleSeq.toString();
666 for (i = 0; i < iSize; i++)
668 selections[i] = seqs[i].getSequenceAsString(start, end);
676 * Locate the first and last position visible for this sequence. if seq isn't
677 * visible then return the position of the left and right of the hidden
678 * boundary region, and the corresponding alignment column indices for the
679 * extent of the sequence
682 * @return int[] { visible start, visible end, first seqpos, last seqpos,
683 * alignment index for seq start, alignment index for seq end }
685 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
687 int fpos = seq.getStart(), lpos = seq.getEnd();
690 if (hiddenColumns == null || hiddenColumns.size() == 0)
692 int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;
693 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
696 // Simply walk along the sequence whilst watching for hidden column
698 List<int[]> regions = getHiddenRegions();
699 int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
700 .getLength(), hideEnd = -1;
701 int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
702 boolean foundStart = false;
703 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
706 if (!Comparison.isGap(seq.getCharAt(p)))
708 // keep track of first/last column
709 // containing sequence data regardless of visibility
715 // update hidden region start/end
716 while (hideEnd < p && rcount < regions.size())
718 int[] region = regions.get(rcount++);
720 visNext += region[0] - visPrev;
721 hideStart = region[0];
726 hideStart = seq.getLength();
728 // update visible boundary for sequence
740 // look for next sequence position
746 return new int[] { findColumnPosition(start),
747 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
749 // otherwise, sequence was completely hidden
750 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
754 * delete any columns in alignmentAnnotation that are hidden (including
755 * sequence associated annotation).
757 * @param alignmentAnnotation
759 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
761 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
765 * delete any columns in alignmentAnnotation that are hidden (including
766 * sequence associated annotation).
769 * remove any annotation to the right of this column
771 * remove any annotation to the left of this column
772 * @param alignmentAnnotation
773 * the annotation to operate on
775 public void makeVisibleAnnotation(int start, int end,
776 AlignmentAnnotation alignmentAnnotation)
778 if (alignmentAnnotation.annotations == null)
782 if (start == end && end == -1)
785 end = alignmentAnnotation.annotations.length;
787 if (hiddenColumns != null && hiddenColumns.size() > 0)
789 // then mangle the alignmentAnnotation annotation array
790 Vector<Annotation[]> annels = new Vector<Annotation[]>();
791 Annotation[] els = null;
792 List<int[]> regions = getHiddenRegions();
793 int blockStart = start, blockEnd = end;
795 int hideStart, hideEnd, w = 0;
797 for (int j = 0; j < regions.size(); j++)
799 region = regions.get(j);
800 hideStart = region[0];
803 if (hideStart < start)
808 blockStart = Math.min(blockStart, hideEnd + 1);
809 blockEnd = Math.min(blockEnd, hideStart);
811 if (blockStart > blockEnd)
816 annels.addElement(els = new Annotation[blockEnd - blockStart]);
817 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
820 blockStart = hideEnd + 1;
824 if (end > blockStart)
826 annels.addElement(els = new Annotation[end - blockStart + 1]);
827 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
829 // copy just the visible segment of the annotation row
830 System.arraycopy(alignmentAnnotation.annotations, blockStart,
835 // copy to the end of the annotation row
836 System.arraycopy(alignmentAnnotation.annotations, blockStart,
838 (alignmentAnnotation.annotations.length - blockStart));
847 alignmentAnnotation.annotations = new Annotation[w];
850 for (Annotation[] chnk : annels)
852 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
859 alignmentAnnotation.restrict(start, end);
865 * @return true if there are columns hidden
867 public boolean hasHiddenColumns()
869 return hiddenColumns != null && hiddenColumns.size() > 0;
874 * @return true if there are more than one set of columns hidden
876 public boolean hasManyHiddenColumns()
878 return hiddenColumns != null && hiddenColumns.size() > 1;
882 * mark the columns corresponding to gap characters as hidden in the column
887 public void hideInsertionsFor(SequenceI sr)
889 List<int[]> inserts = sr.getInsertions();
890 for (int[] r : inserts)
892 hideColumns(r[0], r[1]);
897 * Unhides, and adds to the selection list, all hidden columns
899 public void revealAllHiddenColumns(ColumnSelection sel)
901 if (hiddenColumns != null)
903 for (int i = 0; i < hiddenColumns.size(); i++)
905 int[] region = hiddenColumns.elementAt(i);
906 for (int j = region[0]; j < region[1] + 1; j++)
913 hiddenColumns = null;
917 * Reveals, and marks as selected, the hidden column range with the given
922 public void revealHiddenColumns(int start, ColumnSelection sel)
924 for (int i = 0; i < hiddenColumns.size(); i++)
926 int[] region = hiddenColumns.elementAt(i);
927 if (start == region[0])
929 for (int j = region[0]; j < region[1] + 1; j++)
934 hiddenColumns.removeElement(region);
938 if (hiddenColumns.size() == 0)
940 hiddenColumns = null;
945 * removes intersection of position,length ranges in deletions from the
946 * start,end regions marked in intervals.
952 private boolean pruneIntervalVector(final List<int[]> shifts,
953 Vector<int[]> intervals)
955 boolean pruned = false;
956 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
957 int hr[] = intervals.elementAt(i);
958 int sr[] = shifts.get(s);
959 while (i <= j && s <= t)
961 boolean trailinghn = hr[1] >= sr[0];
966 hr = intervals.elementAt(++i);
974 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
975 if (endshift < hr[0] || endshift < sr[0])
976 { // leadinghc disjoint or not a deletion
979 sr = shifts.get(++s);
987 boolean leadinghn = hr[0] >= sr[0];
988 boolean leadinghc = hr[0] < endshift;
989 boolean trailinghc = hr[1] < endshift;
993 { // deleted hidden region.
994 intervals.removeElementAt(i);
999 hr = intervals.elementAt(i);
1005 hr[0] = endshift; // clip c terminal region
1006 leadinghn = !leadinghn;
1022 // sr contained in hr
1025 sr = shifts.get(++s);
1035 return pruned; // true if any interval was removed or modified by
1040 * remove any hiddenColumns or selected columns and shift remaining based on a
1041 * series of position, range deletions.
1045 public void pruneDeletions(List<int[]> shifts)
1047 // delete any intervals intersecting.
1048 if (hiddenColumns != null)
1050 pruneIntervalVector(shifts, hiddenColumns);
1051 if (hiddenColumns != null && hiddenColumns.size() == 0)
1053 hiddenColumns = null;
1059 * Add gaps into the sequences aligned to profileseq under the given
1064 * - alignment to have gaps inserted into it
1066 * - alignment view where sequence corresponding to profileseq is
1068 * @return new HiddenColumns for new alignment view, with insertions into
1069 * profileseq marked as hidden.
1071 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1072 AlignmentI al, AlignmentView input)
1076 char gc = al.getGapCharacter();
1077 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1078 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1079 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1080 nview.propagateInsertions(profileseq, al, origseq);
1087 * - sequence in al which corresponds to origseq
1089 * - alignment which is to have gaps inserted into it
1091 * - sequence corresponding to profileseq which defines gap map for
1094 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1097 char gc = al.getGapCharacter();
1098 // recover mapping between sequence's non-gap positions and positions
1100 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1101 int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
1102 profileseq.getLength());
1106 // add profile to visible contigs
1107 for (int v = 0; v < viscontigs.length; v += 2)
1109 if (viscontigs[v] > spos)
1111 StringBuffer sb = new StringBuffer();
1112 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1116 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1118 SequenceI sqobj = al.getSequenceAt(s);
1119 if (sqobj != profileseq)
1121 String sq = al.getSequenceAt(s).getSequenceAsString();
1122 if (sq.length() <= spos + offset)
1125 int diff = spos + offset - sq.length() - 1;
1130 while ((diff = spos + offset - sq.length() - 1) > 0)
1133 // + ((diff >= sb.length()) ? sb.toString() : sb
1134 // .substring(0, diff));
1135 if (diff >= sb.length())
1137 sq += sb.toString();
1141 char[] buf = new char[diff];
1142 sb.getChars(0, diff, buf, 0);
1143 sq += buf.toString();
1147 sq += sb.toString();
1151 al.getSequenceAt(s).setSequence(
1152 sq.substring(0, spos + offset) + sb.toString()
1153 + sq.substring(spos + offset));
1157 // offset+=sb.length();
1159 spos = viscontigs[v + 1] + 1;
1161 if ((offset + spos) < profileseq.getLength())
1163 // pad the final region with gaps.
1164 StringBuffer sb = new StringBuffer();
1165 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1169 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1171 SequenceI sqobj = al.getSequenceAt(s);
1172 if (sqobj == profileseq)
1176 String sq = sqobj.getSequenceAsString();
1178 int diff = origseq.getLength() - sq.length();
1182 // + ((diff >= sb.length()) ? sb.toString() : sb
1183 // .substring(0, diff));
1184 if (diff >= sb.length())
1186 sq += sb.toString();
1190 char[] buf = new char[diff];
1191 sb.getChars(0, diff, buf, 0);
1192 sq += buf.toString();
1194 diff = origseq.getLength() - sq.length();
1201 * remove any hiddenColumns or selected columns and shift remaining based on a
1202 * series of position, range deletions.
1206 private void pruneDeletions(ShiftList deletions)
1208 if (deletions != null)
1210 final List<int[]> shifts = deletions.getShifts();
1211 if (shifts != null && shifts.size() > 0)
1213 pruneDeletions(shifts);
1215 // and shift the rest.
1216 this.compensateForEdits(deletions);
1222 * Adjust hidden column boundaries based on a series of column additions or
1223 * deletions in visible regions.
1225 * @param shiftrecord
1228 private ShiftList compensateForEdits(ShiftList shiftrecord)
1230 if (shiftrecord != null)
1232 final List<int[]> shifts = shiftrecord.getShifts();
1233 if (shifts != null && shifts.size() > 0)
1236 for (int i = 0, j = shifts.size(); i < j; i++)
1238 int[] sh = shifts.get(i);
1239 compensateForDelEdits(shifted + sh[0], sh[1]);
1243 return shiftrecord.getInverse();
1249 * Returns a hashCode built from hidden column ranges
1252 public int hashCode()
1255 if (hiddenColumns != null)
1257 for (int[] hidden : hiddenColumns)
1259 hashCode = 31 * hashCode + hidden[0];
1260 hashCode = 31 * hashCode + hidden[1];
1267 * Hide columns corresponding to the marked bits
1270 * - columns map to bits starting from zero
1272 public void hideMarkedBits(BitSet inserts)
1274 for (int firstSet = inserts.nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1275 .nextSetBit(lastSet))
1277 lastSet = inserts.nextClearBit(firstSet);
1278 hideColumns(firstSet, lastSet - 1);
1285 * BitSet where hidden columns will be marked
1287 public void markHiddenRegions(BitSet inserts)
1289 if (hiddenColumns == null)
1293 for (int[] range : hiddenColumns)
1295 inserts.set(range[0], range[1] + 1);