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[]> getHiddenRegions()
26 return hiddenColumns == null ? Collections.<int[]> emptyList()
31 * Find the number of hidden columns
33 * @return number of hidden columns
40 for (int[] range : hiddenColumns)
42 size += range[1] - range[0] + 1;
49 * Answers if there are any hidden columns
51 * @return true if there are hidden columns
53 public boolean hasHidden()
55 return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
59 public boolean equals(Object obj)
61 if (!(obj instanceof HiddenColumns))
65 HiddenColumns that = (HiddenColumns) obj;
68 * check hidden columns are either both null, or match
70 if (this.hiddenColumns == null)
72 return (that.hiddenColumns == null);
74 if (that.hiddenColumns == null
75 || that.hiddenColumns.size() != this.hiddenColumns.size())
80 for (int[] thisRange : hiddenColumns)
82 int[] thatRange = that.hiddenColumns.get(i++);
83 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
92 * Return absolute column index for a visible column index
95 * int column index in alignment view (count from zero)
96 * @return alignment column index for column
98 public int adjustForHiddenColumns(int column)
101 if (hiddenColumns != null)
103 for (int i = 0; i < hiddenColumns.size(); i++)
105 int[] region = hiddenColumns.elementAt(i);
106 if (result >= region[0])
108 result += region[1] - region[0] + 1;
116 * Use this method to find out where a column will appear in the visible
117 * alignment when hidden columns exist. If the column is not visible, then the
118 * left-most visible column will always be returned.
120 * @param hiddenColumn
121 * the column index in the full alignment including hidden columns
122 * @return the position of the column in the visible alignment
124 public int findColumnPosition(int hiddenColumn)
126 int result = hiddenColumn;
127 if (hiddenColumns != null)
133 region = hiddenColumns.elementAt(index++);
134 if (hiddenColumn > region[1])
136 result -= region[1] + 1 - region[0];
138 } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));
140 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
142 // Here the hidden column is within a region, so
143 // we want to return the position of region[0]-1, adjusted for any
144 // earlier hidden columns.
145 // Calculate the difference between the actual hidden col position
146 // and region[0]-1, and then subtract from result to convert result from
147 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
149 // However, if the region begins at 0 we cannot return region[0]-1
157 return result - (hiddenColumn - region[0] + 1);
161 return result; // return the shifted position after removing hidden columns.
165 * Find the visible column which is a given visible number of columns to the
166 * left of another visible column. i.e. for a startColumn x, the column which
167 * is distance 1 away will be column x-1.
169 * @param visibleDistance
170 * the number of visible columns to offset by
172 * the column to start from
173 * @return the position of the column in the visible alignment
175 public int subtractVisibleColumns(int visibleDistance, int startColumn)
177 int distance = visibleDistance;
179 // in case startColumn is in a hidden region, move it to the left
180 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
182 // get index of hidden region to left of start
183 int index = getHiddenIndexLeft(start);
186 // no hidden regions to left of startColumn
187 return start - distance;
190 // walk backwards through the alignment subtracting the counts of visible
191 // columns from distance
194 int nextstart = start;
196 while ((index > -1) && (distance - gap > 0))
198 // subtract the gap to right of region from distance
202 // calculate the next gap
203 region = hiddenColumns.get(index);
204 gap = start - region[1];
206 // set start to just to left of current region
207 nextstart = region[0] - 1;
211 if (distance - gap > 0)
213 // fell out of loop because there are no more hidden regions
215 return nextstart - distance;
217 return start - distance;
222 * Use this method to determine where the next hiddenRegion starts
224 * @param hiddenRegion
225 * index of hidden region (counts from 0)
226 * @return column number in visible view
228 public int findHiddenRegionPosition(int hiddenRegion)
231 if (hiddenColumns != null)
237 int[] region = hiddenColumns.elementAt(index);
238 if (hiddenRegion == 0)
243 gaps += region[1] + 1 - region[0];
244 result = region[1] + 1;
246 } while (index <= hiddenRegion);
255 * This method returns the rightmost limit of a region of an alignment with
256 * hidden columns. In otherwords, the next hidden column.
261 public int getHiddenBoundaryRight(int alPos)
263 if (hiddenColumns != null)
268 int[] region = hiddenColumns.elementAt(index);
269 if (alPos < region[0])
275 } while (index < hiddenColumns.size());
283 * This method returns the leftmost limit of a region of an alignment with
284 * hidden columns. In otherwords, the previous hidden column.
289 public int getHiddenBoundaryLeft(int alPos)
291 if (hiddenColumns != null)
293 int index = hiddenColumns.size() - 1;
296 int[] region = hiddenColumns.elementAt(index);
297 if (alPos > region[1])
303 } while (index > -1);
311 * This method returns the index of the hidden region to the left of a column
312 * position. If the column is in a hidden region it returns the index of the
313 * region to the left. If there is no hidden region to the left it returns -1.
318 private int getHiddenIndexLeft(int pos)
320 if (hiddenColumns != null)
322 int index = hiddenColumns.size() - 1;
325 int[] region = hiddenColumns.elementAt(index);
332 } while (index > -1);
340 * Adds the specified column range to the hidden columns
345 public void hideColumns(int start, int end)
347 if (hiddenColumns == null)
349 hiddenColumns = new Vector<int[]>();
353 * traverse existing hidden ranges and insert / amend / append as
356 for (int i = 0; i < hiddenColumns.size(); i++)
358 int[] region = hiddenColumns.elementAt(i);
360 if (end < region[0] - 1)
363 * insert discontiguous preceding range
365 hiddenColumns.insertElementAt(new int[] { start, end }, i);
369 if (end <= region[1])
372 * new range overlaps existing, or is contiguous preceding it - adjust
375 region[0] = Math.min(region[0], start);
379 if (start <= region[1] + 1)
382 * new range overlaps existing, or is contiguous following it - adjust
383 * start and end columns
385 region[0] = Math.min(region[0], start);
386 region[1] = Math.max(region[1], end);
389 * also update or remove any subsequent ranges
390 * that are overlapped
392 while (i < hiddenColumns.size() - 1)
394 int[] nextRegion = hiddenColumns.get(i + 1);
395 if (nextRegion[0] > end + 1)
398 * gap to next hidden range - no more to update
402 region[1] = Math.max(nextRegion[1], end);
403 hiddenColumns.remove(i + 1);
410 * remaining case is that the new range follows everything else
412 hiddenColumns.addElement(new int[] { start, end });
415 public boolean isVisible(int column)
417 if (hiddenColumns != null)
419 for (int[] region : hiddenColumns)
421 if (column >= region[0] && column <= region[1])
434 public HiddenColumns()
443 public HiddenColumns(HiddenColumns copy)
447 if (copy.hiddenColumns != null)
449 hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
450 for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
453 rh = copy.hiddenColumns.elementAt(i);
456 cp = new int[rh.length];
457 System.arraycopy(rh, 0, cp, 0, rh.length);
458 hiddenColumns.addElement(cp);
466 * propagate shift in alignment columns to column selection
471 * shift in edit (+ve for removal, or -ve for inserts)
473 public List<int[]> compensateForEdit(int start, int change,
476 List<int[]> deletedHiddenColumns = null;
478 if (hiddenColumns != null)
480 deletedHiddenColumns = new ArrayList<int[]>();
481 int hSize = hiddenColumns.size();
482 for (int i = 0; i < hSize; i++)
484 int[] region = hiddenColumns.elementAt(i);
485 if (region[0] > start && start + change > region[1])
487 deletedHiddenColumns.add(region);
489 hiddenColumns.removeElementAt(i);
495 if (region[0] > start)
508 this.revealHiddenColumns(0, sel);
511 return deletedHiddenColumns;
515 * propagate shift in alignment columns to column selection special version of
516 * compensateForEdit - allowing for edits within hidden regions
521 * shift in edit (+ve for removal, or -ve for inserts)
523 public void compensateForDelEdits(int start, int change)
525 if (hiddenColumns != null)
527 for (int i = 0; i < hiddenColumns.size(); i++)
529 int[] region = hiddenColumns.elementAt(i);
530 if (region[0] >= start)
534 if (region[1] >= start)
538 if (region[1] < region[0])
540 hiddenColumns.removeElementAt(i--);
556 * return all visible segments between the given start and end boundaries
559 * (first column inclusive from 0)
561 * (last column - not inclusive)
562 * @return int[] {i_start, i_end, ..} where intervals lie in
563 * start<=i_start<=i_end<end
565 public int[] getVisibleContigs(int start, int end)
567 if (hiddenColumns != null && hiddenColumns.size() > 0)
569 List<int[]> visiblecontigs = new ArrayList<int[]>();
570 List<int[]> regions = getHiddenRegions();
574 int hideStart, hideEnd;
576 for (int j = 0; vstart < end && j < regions.size(); j++)
578 region = regions.get(j);
579 hideStart = region[0];
582 if (hideEnd < vstart)
586 if (hideStart > vstart)
588 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
590 vstart = hideEnd + 1;
595 visiblecontigs.add(new int[] { vstart, end - 1 });
597 int[] vcontigs = new int[visiblecontigs.size() * 2];
598 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
600 int[] vc = visiblecontigs.get(i);
601 visiblecontigs.set(i, null);
602 vcontigs[i * 2] = vc[0];
603 vcontigs[i * 2 + 1] = vc[1];
605 visiblecontigs.clear();
610 return new int[] { start, end - 1 };
614 public String[] getVisibleSequenceStrings(int start, int end,
617 int i, iSize = seqs.length;
618 String selections[] = new String[iSize];
619 if (hiddenColumns != null && hiddenColumns.size() > 0)
621 for (i = 0; i < iSize; i++)
623 StringBuffer visibleSeq = new StringBuffer();
624 List<int[]> regions = getHiddenRegions();
626 int blockStart = start, blockEnd = end;
628 int hideStart, hideEnd;
630 for (int j = 0; j < regions.size(); j++)
632 region = regions.get(j);
633 hideStart = region[0];
636 if (hideStart < start)
641 blockStart = Math.min(blockStart, hideEnd + 1);
642 blockEnd = Math.min(blockEnd, hideStart);
644 if (blockStart > blockEnd)
649 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
651 blockStart = hideEnd + 1;
655 if (end > blockStart)
657 visibleSeq.append(seqs[i].getSequence(blockStart, end));
660 selections[i] = visibleSeq.toString();
665 for (i = 0; i < iSize; i++)
667 selections[i] = seqs[i].getSequenceAsString(start, end);
675 * Locate the first and last position visible for this sequence. if seq isn't
676 * visible then return the position of the left and right of the hidden
677 * boundary region, and the corresponding alignment column indices for the
678 * extent of the sequence
681 * @return int[] { visible start, visible end, first seqpos, last seqpos,
682 * alignment index for seq start, alignment index for seq end }
684 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
686 int fpos = seq.getStart(), lpos = seq.getEnd();
689 if (hiddenColumns == null || hiddenColumns.size() == 0)
691 int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;
692 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
695 // Simply walk along the sequence whilst watching for hidden column
697 List<int[]> regions = getHiddenRegions();
698 int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
699 .getLength(), hideEnd = -1;
700 int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
701 boolean foundStart = false;
702 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
705 if (!Comparison.isGap(seq.getCharAt(p)))
707 // keep track of first/last column
708 // containing sequence data regardless of visibility
714 // update hidden region start/end
715 while (hideEnd < p && rcount < regions.size())
717 int[] region = regions.get(rcount++);
719 visNext += region[0] - visPrev;
720 hideStart = region[0];
725 hideStart = seq.getLength();
727 // update visible boundary for sequence
739 // look for next sequence position
745 return new int[] { findColumnPosition(start),
746 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
748 // otherwise, sequence was completely hidden
749 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
753 * delete any columns in alignmentAnnotation that are hidden (including
754 * sequence associated annotation).
756 * @param alignmentAnnotation
758 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
760 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
764 * delete any columns in alignmentAnnotation that are hidden (including
765 * sequence associated annotation).
768 * remove any annotation to the right of this column
770 * remove any annotation to the left of this column
771 * @param alignmentAnnotation
772 * the annotation to operate on
774 public void makeVisibleAnnotation(int start, int end,
775 AlignmentAnnotation alignmentAnnotation)
777 if (alignmentAnnotation.annotations == null)
781 if (start == end && end == -1)
784 end = alignmentAnnotation.annotations.length;
786 if (hiddenColumns != null && hiddenColumns.size() > 0)
788 // then mangle the alignmentAnnotation annotation array
789 Vector<Annotation[]> annels = new Vector<Annotation[]>();
790 Annotation[] els = null;
791 List<int[]> regions = getHiddenRegions();
792 int blockStart = start, blockEnd = end;
794 int hideStart, hideEnd, w = 0;
796 for (int j = 0; j < regions.size(); j++)
798 region = regions.get(j);
799 hideStart = region[0];
802 if (hideStart < start)
807 blockStart = Math.min(blockStart, hideEnd + 1);
808 blockEnd = Math.min(blockEnd, hideStart);
810 if (blockStart > blockEnd)
815 annels.addElement(els = new Annotation[blockEnd - blockStart]);
816 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
819 blockStart = hideEnd + 1;
823 if (end > blockStart)
825 annels.addElement(els = new Annotation[end - blockStart + 1]);
826 if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)
828 // copy just the visible segment of the annotation row
829 System.arraycopy(alignmentAnnotation.annotations, blockStart,
834 // copy to the end of the annotation row
835 System.arraycopy(alignmentAnnotation.annotations, blockStart,
837 (alignmentAnnotation.annotations.length - blockStart));
846 alignmentAnnotation.annotations = new Annotation[w];
849 for (Annotation[] chnk : annels)
851 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
858 alignmentAnnotation.restrict(start, end);
864 * @return true if there are columns hidden
866 public boolean hasHiddenColumns()
868 return hiddenColumns != null && hiddenColumns.size() > 0;
873 * @return true if there are more than one set of columns hidden
875 public boolean hasManyHiddenColumns()
877 return hiddenColumns != null && hiddenColumns.size() > 1;
881 * mark the columns corresponding to gap characters as hidden in the column
886 public void hideInsertionsFor(SequenceI sr)
888 List<int[]> inserts = sr.getInsertions();
889 for (int[] r : inserts)
891 hideColumns(r[0], r[1]);
896 * Unhides, and adds to the selection list, all hidden columns
898 public void revealAllHiddenColumns(ColumnSelection sel)
900 if (hiddenColumns != null)
902 for (int i = 0; i < hiddenColumns.size(); i++)
904 int[] region = hiddenColumns.elementAt(i);
905 for (int j = region[0]; j < region[1] + 1; j++)
912 hiddenColumns = null;
916 * Reveals, and marks as selected, the hidden column range with the given
921 public void revealHiddenColumns(int start, ColumnSelection sel)
923 for (int i = 0; i < hiddenColumns.size(); i++)
925 int[] region = hiddenColumns.elementAt(i);
926 if (start == region[0])
928 for (int j = region[0]; j < region[1] + 1; j++)
933 hiddenColumns.removeElement(region);
937 if (hiddenColumns.size() == 0)
939 hiddenColumns = null;
944 * removes intersection of position,length ranges in deletions from the
945 * start,end regions marked in intervals.
951 private boolean pruneIntervalVector(final List<int[]> shifts,
952 Vector<int[]> intervals)
954 boolean pruned = false;
955 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
956 int hr[] = intervals.elementAt(i);
957 int sr[] = shifts.get(s);
958 while (i <= j && s <= t)
960 boolean trailinghn = hr[1] >= sr[0];
965 hr = intervals.elementAt(++i);
973 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
974 if (endshift < hr[0] || endshift < sr[0])
975 { // leadinghc disjoint or not a deletion
978 sr = shifts.get(++s);
986 boolean leadinghn = hr[0] >= sr[0];
987 boolean leadinghc = hr[0] < endshift;
988 boolean trailinghc = hr[1] < endshift;
992 { // deleted hidden region.
993 intervals.removeElementAt(i);
998 hr = intervals.elementAt(i);
1004 hr[0] = endshift; // clip c terminal region
1005 leadinghn = !leadinghn;
1021 // sr contained in hr
1024 sr = shifts.get(++s);
1034 return pruned; // true if any interval was removed or modified by
1039 * remove any hiddenColumns or selected columns and shift remaining based on a
1040 * series of position, range deletions.
1044 public void pruneDeletions(List<int[]> shifts)
1046 // delete any intervals intersecting.
1047 if (hiddenColumns != null)
1049 pruneIntervalVector(shifts, hiddenColumns);
1050 if (hiddenColumns != null && hiddenColumns.size() == 0)
1052 hiddenColumns = null;
1058 * Add gaps into the sequences aligned to profileseq under the given
1063 * - alignment to have gaps inserted into it
1065 * - alignment view where sequence corresponding to profileseq is
1067 * @return new HiddenColumns for new alignment view, with insertions into
1068 * profileseq marked as hidden.
1070 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1071 AlignmentI al, AlignmentView input)
1075 char gc = al.getGapCharacter();
1076 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1077 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1078 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1079 nview.propagateInsertions(profileseq, al, origseq);
1086 * - sequence in al which corresponds to origseq
1088 * - alignment which is to have gaps inserted into it
1090 * - sequence corresponding to profileseq which defines gap map for
1093 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1096 char gc = al.getGapCharacter();
1097 // recover mapping between sequence's non-gap positions and positions
1099 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1100 int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
1101 profileseq.getLength());
1105 // add profile to visible contigs
1106 for (int v = 0; v < viscontigs.length; v += 2)
1108 if (viscontigs[v] > spos)
1110 StringBuffer sb = new StringBuffer();
1111 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1115 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1117 SequenceI sqobj = al.getSequenceAt(s);
1118 if (sqobj != profileseq)
1120 String sq = al.getSequenceAt(s).getSequenceAsString();
1121 if (sq.length() <= spos + offset)
1124 int diff = spos + offset - sq.length() - 1;
1129 while ((diff = spos + offset - sq.length() - 1) > 0)
1132 // + ((diff >= sb.length()) ? sb.toString() : sb
1133 // .substring(0, diff));
1134 if (diff >= sb.length())
1136 sq += sb.toString();
1140 char[] buf = new char[diff];
1141 sb.getChars(0, diff, buf, 0);
1142 sq += buf.toString();
1146 sq += sb.toString();
1150 al.getSequenceAt(s).setSequence(
1151 sq.substring(0, spos + offset) + sb.toString()
1152 + sq.substring(spos + offset));
1156 // offset+=sb.length();
1158 spos = viscontigs[v + 1] + 1;
1160 if ((offset + spos) < profileseq.getLength())
1162 // pad the final region with gaps.
1163 StringBuffer sb = new StringBuffer();
1164 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1168 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1170 SequenceI sqobj = al.getSequenceAt(s);
1171 if (sqobj == profileseq)
1175 String sq = sqobj.getSequenceAsString();
1177 int diff = origseq.getLength() - sq.length();
1181 // + ((diff >= sb.length()) ? sb.toString() : sb
1182 // .substring(0, diff));
1183 if (diff >= sb.length())
1185 sq += sb.toString();
1189 char[] buf = new char[diff];
1190 sb.getChars(0, diff, buf, 0);
1191 sq += buf.toString();
1193 diff = origseq.getLength() - sq.length();
1200 * remove any hiddenColumns or selected columns and shift remaining based on a
1201 * series of position, range deletions.
1205 private void pruneDeletions(ShiftList deletions)
1207 if (deletions != null)
1209 final List<int[]> shifts = deletions.getShifts();
1210 if (shifts != null && shifts.size() > 0)
1212 pruneDeletions(shifts);
1214 // and shift the rest.
1215 this.compensateForEdits(deletions);
1221 * Adjust hidden column boundaries based on a series of column additions or
1222 * deletions in visible regions.
1224 * @param shiftrecord
1227 private ShiftList compensateForEdits(ShiftList shiftrecord)
1229 if (shiftrecord != null)
1231 final List<int[]> shifts = shiftrecord.getShifts();
1232 if (shifts != null && shifts.size() > 0)
1235 for (int i = 0, j = shifts.size(); i < j; i++)
1237 int[] sh = shifts.get(i);
1238 compensateForDelEdits(shifted + sh[0], sh[1]);
1242 return shiftrecord.getInverse();
1248 * Returns a hashCode built from hidden column ranges
1250 public int hashCode(int hc)
1253 if (hiddenColumns != null)
1255 for (int[] hidden : hiddenColumns)
1257 hashCode = 31 * hashCode + hidden[0];
1258 hashCode = 31 * hashCode + hidden[1];