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;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Vector;
12 import java.util.concurrent.locks.ReentrantReadWriteLock;
14 public class HiddenColumns implements Iterable<int[]>
16 private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
19 * list of hidden column [start, end] ranges; the list is maintained in
20 * ascending start column order
22 private Vector<int[]> hiddenColumns;
25 * This Method is used to return all the HiddenColumn regions
27 * @return empty list or List of hidden column intervals
29 public List<int[]> getHiddenRegions()
31 return hiddenColumns == null ? Collections.<int[]> emptyList()
36 * Output regions data as a string. String is in the format:
37 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
40 * string to delimit regions
41 * @param betweenstring
42 * to put between start and end region values
43 * @return regions formatted according to delimiter and between strings
45 public String regionsToString(String delimiter, String between)
49 lock.readLock().lock();
50 StringBuilder regionBuilder = new StringBuilder();
51 for (int[] range : hiddenColumns)
53 regionBuilder.append(delimiter).append(range[0]).append(between)
57 regionBuilder.deleteCharAt(0);
58 return regionBuilder.toString();
61 lock.readLock().unlock();
66 * Find the number of hidden columns
68 * @return number of hidden columns
74 lock.readLock().lock();
78 for (int[] range : hiddenColumns)
80 size += range[1] - range[0] + 1;
87 lock.readLock().unlock();
92 * Answers if there are any hidden columns
94 * @return true if there are hidden columns
96 public boolean hasHidden()
100 lock.readLock().lock();
101 return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
104 lock.readLock().unlock();
110 public boolean equals(Object obj)
114 lock.readLock().lock();
116 if (!(obj instanceof HiddenColumns))
120 HiddenColumns that = (HiddenColumns) obj;
123 * check hidden columns are either both null, or match
125 if (this.hiddenColumns == null)
127 return (that.hiddenColumns == null);
129 if (that.hiddenColumns == null
130 || that.hiddenColumns.size() != this.hiddenColumns.size())
135 for (int[] thisRange : hiddenColumns)
137 int[] thatRange = that.hiddenColumns.get(i++);
138 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
146 lock.readLock().unlock();
151 * Return absolute column index for a visible column index
154 * int column index in alignment view (count from zero)
155 * @return alignment column index for column
157 public int adjustForHiddenColumns(int column)
161 lock.readLock().lock();
163 if (hiddenColumns != null)
165 for (int i = 0; i < hiddenColumns.size(); i++)
167 int[] region = hiddenColumns.elementAt(i);
168 if (result >= region[0])
170 result += region[1] - region[0] + 1;
177 lock.readLock().unlock();
182 * Use this method to find out where a column will appear in the visible
183 * alignment when hidden columns exist. If the column is not visible, then the
184 * left-most visible column will always be returned.
186 * @param hiddenColumn
187 * the column index in the full alignment including hidden columns
188 * @return the position of the column in the visible alignment
190 public int findColumnPosition(int hiddenColumn)
194 lock.readLock().lock();
195 int result = hiddenColumn;
196 if (hiddenColumns != null)
202 region = hiddenColumns.elementAt(index++);
203 if (hiddenColumn > region[1])
205 result -= region[1] + 1 - region[0];
207 } while ((hiddenColumn > region[1])
208 && (index < hiddenColumns.size()));
210 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
212 // Here the hidden column is within a region, so
213 // we want to return the position of region[0]-1, adjusted for any
214 // earlier hidden columns.
215 // Calculate the difference between the actual hidden col position
216 // and region[0]-1, and then subtract from result to convert result
218 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
220 // However, if the region begins at 0 we cannot return region[0]-1
228 return result - (hiddenColumn - region[0] + 1);
232 return result; // return the shifted position after removing hidden
236 lock.readLock().unlock();
241 * Find the visible column which is a given visible number of columns to the
242 * left of another visible column. i.e. for a startColumn x, the column which
243 * is distance 1 away will be column x-1.
245 * @param visibleDistance
246 * the number of visible columns to offset by
248 * the column to start from
249 * @return the position of the column in the visible alignment
251 public int subtractVisibleColumns(int visibleDistance, int startColumn)
256 lock.readLock().lock();
257 int distance = visibleDistance;
259 // in case startColumn is in a hidden region, move it to the left
260 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
262 // get index of hidden region to left of start
263 int index = getHiddenIndexLeft(start);
266 // no hidden regions to left of startColumn
267 return start - distance;
270 // walk backwards through the alignment subtracting the counts of visible
271 // columns from distance
274 int nextstart = start;
276 while ((index > -1) && (distance - gap > 0))
278 // subtract the gap to right of region from distance
282 // calculate the next gap
283 region = hiddenColumns.get(index);
284 gap = start - region[1];
286 // set start to just to left of current region
287 nextstart = region[0] - 1;
291 if (distance - gap > 0)
293 // fell out of loop because there are no more hidden regions
295 return nextstart - distance;
297 return start - distance;
300 lock.readLock().unlock();
306 * Use this method to determine the set of hiddenRegion start positions
308 * @return list of column number in visible view where hidden regions start
310 public List<Integer> findHiddenRegionPositions()
314 lock.readLock().lock();
315 List<Integer> positions = new ArrayList<>(
316 hiddenColumns.size());
318 positions.add(hiddenColumns.elementAt(0)[0]);
319 for (int i = 1; i < hiddenColumns.size(); ++i)
323 if (hiddenColumns != null)
329 int[] region = hiddenColumns.elementAt(index);
330 gaps += region[1] + 1 - region[0];
331 result = region[1] + 1;
333 } while (index <= i);
337 positions.add(result);
344 lock.readLock().unlock();
349 * This method returns the rightmost limit of a region of an alignment with
350 * hidden columns. In otherwords, the next hidden column.
355 public int getHiddenBoundaryRight(int alPos)
359 lock.readLock().lock();
360 if (hiddenColumns != null)
365 int[] region = hiddenColumns.elementAt(index);
366 if (alPos < region[0])
372 } while (index < hiddenColumns.size());
378 lock.readLock().unlock();
384 * This method returns the leftmost limit of a region of an alignment with
385 * hidden columns. In otherwords, the previous hidden column.
390 public int getHiddenBoundaryLeft(int alPos)
394 lock.readLock().lock();
396 if (hiddenColumns != null)
398 int index = hiddenColumns.size() - 1;
401 int[] region = hiddenColumns.elementAt(index);
402 if (alPos > region[1])
408 } while (index > -1);
414 lock.readLock().unlock();
419 * This method returns the index of the hidden region to the left of a column
420 * position. If the column is in a hidden region it returns the index of the
421 * region to the left. If there is no hidden region to the left it returns -1.
426 private int getHiddenIndexLeft(int pos)
431 lock.readLock().lock();
432 if (hiddenColumns != null)
434 int index = hiddenColumns.size() - 1;
437 int[] region = hiddenColumns.elementAt(index);
444 } while (index > -1);
450 lock.readLock().unlock();
456 * Adds the specified column range to the hidden columns
461 public void hideColumns(int start, int end)
463 hideColumns(start, end, false);
467 * Adds the specified column range to the hidden columns
472 private void hideColumns(int start, int end, boolean alreadyLocked)
479 lock.writeLock().lock();
482 if (hiddenColumns == null)
484 hiddenColumns = new Vector<>();
488 * traverse existing hidden ranges and insert / amend / append as
491 for (int i = 0; i < hiddenColumns.size(); i++)
493 int[] region = hiddenColumns.elementAt(i);
495 if (end < region[0] - 1)
498 * insert discontiguous preceding range
500 hiddenColumns.insertElementAt(new int[] { start, end }, i);
504 if (end <= region[1])
507 * new range overlaps existing, or is contiguous preceding it - adjust
510 region[0] = Math.min(region[0], start);
514 if (start <= region[1] + 1)
517 * new range overlaps existing, or is contiguous following it - adjust
518 * start and end columns
520 region[0] = Math.min(region[0], start);
521 region[1] = Math.max(region[1], end);
524 * also update or remove any subsequent ranges
525 * that are overlapped
527 while (i < hiddenColumns.size() - 1)
529 int[] nextRegion = hiddenColumns.get(i + 1);
530 if (nextRegion[0] > end + 1)
533 * gap to next hidden range - no more to update
537 region[1] = Math.max(nextRegion[1], end);
538 hiddenColumns.remove(i + 1);
545 * remaining case is that the new range follows everything else
547 hiddenColumns.addElement(new int[] { start, end });
552 lock.writeLock().unlock();
557 public boolean isVisible(int column)
561 lock.readLock().lock();
563 if (hiddenColumns != null)
565 for (int[] region : hiddenColumns)
567 if (column >= region[0] && column <= region[1])
577 lock.readLock().unlock();
584 public HiddenColumns()
593 public HiddenColumns(HiddenColumns copy)
598 lock.readLock().lock();
601 if (copy.hiddenColumns != null)
603 hiddenColumns = copy.copyHiddenRegions();
609 lock.readLock().unlock();
613 private Vector<int[]> copyHiddenRegions()
615 Vector<int[]> copy = new Vector<>(hiddenColumns.size());
616 for (int i = 0, j = hiddenColumns.size(); i < j; i++)
619 rh = hiddenColumns.elementAt(i);
622 cp = new int[rh.length];
623 System.arraycopy(rh, 0, cp, 0, rh.length);
630 private ArrayList<int[]> copyHiddenRegionsToArrayList()
632 ArrayList<int[]> copy = new ArrayList<>(hiddenColumns.size());
633 for (int i = 0, j = hiddenColumns.size(); i < j; i++)
636 rh = hiddenColumns.elementAt(i);
639 cp = new int[rh.length];
640 System.arraycopy(rh, 0, cp, 0, rh.length);
647 public void getHiddenColumnsCopy(Vector<int[]> copy)
651 lock.readLock().lock();
652 copy = copyHiddenRegions();
655 lock.readLock().unlock();
659 public void getHiddenColumnsCopy(ArrayList<int[]> copy)
663 lock.readLock().lock();
664 copy = copyHiddenRegionsToArrayList();
667 lock.readLock().unlock();
672 * propagate shift in alignment columns to column selection
677 * shift in edit (+ve for removal, or -ve for inserts)
679 public List<int[]> compensateForEdit(int start, int change,
684 lock.writeLock().lock();
685 List<int[]> deletedHiddenColumns = null;
687 if (hiddenColumns != null)
689 deletedHiddenColumns = new ArrayList<>();
690 int hSize = hiddenColumns.size();
691 for (int i = 0; i < hSize; i++)
693 int[] region = hiddenColumns.elementAt(i);
694 if (region[0] > start && start + change > region[1])
696 deletedHiddenColumns.add(region);
698 hiddenColumns.removeElementAt(i);
704 if (region[0] > start)
717 this.revealHiddenColumns(0, sel);
720 return deletedHiddenColumns;
723 lock.writeLock().unlock();
728 * propagate shift in alignment columns to column selection special version of
729 * compensateForEdit - allowing for edits within hidden regions
734 * shift in edit (+ve for removal, or -ve for inserts)
736 public void compensateForDelEdits(int start, int change)
740 lock.writeLock().lock();
741 if (hiddenColumns != null)
743 for (int i = 0; i < hiddenColumns.size(); i++)
745 int[] region = hiddenColumns.elementAt(i);
746 if (region[0] >= start)
750 if (region[1] >= start)
754 if (region[1] < region[0])
756 hiddenColumns.removeElementAt(i--);
772 lock.writeLock().unlock();
777 * return all visible segments between the given start and end boundaries
780 * (first column inclusive from 0)
782 * (last column - not inclusive)
783 * @return int[] {i_start, i_end, ..} where intervals lie in
784 * start<=i_start<=i_end<end
786 public int[] getVisibleContigs(int start, int end)
790 lock.readLock().lock();
791 if (hiddenColumns != null && hiddenColumns.size() > 0)
793 List<int[]> visiblecontigs = new ArrayList<>();
794 List<int[]> regions = getHiddenRegions();
798 int hideStart, hideEnd;
800 for (int j = 0; vstart < end && j < regions.size(); j++)
802 region = regions.get(j);
803 hideStart = region[0];
806 if (hideEnd < vstart)
810 if (hideStart > vstart)
812 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
814 vstart = hideEnd + 1;
819 visiblecontigs.add(new int[] { vstart, end - 1 });
821 int[] vcontigs = new int[visiblecontigs.size() * 2];
822 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
824 int[] vc = visiblecontigs.get(i);
825 visiblecontigs.set(i, null);
826 vcontigs[i * 2] = vc[0];
827 vcontigs[i * 2 + 1] = vc[1];
829 visiblecontigs.clear();
834 return new int[] { start, end - 1 };
839 lock.readLock().unlock();
843 public String[] getVisibleSequenceStrings(int start, int end,
848 lock.readLock().lock();
849 int i, iSize = seqs.length;
850 String selections[] = new String[iSize];
851 if (hiddenColumns != null && hiddenColumns.size() > 0)
853 for (i = 0; i < iSize; i++)
855 StringBuffer visibleSeq = new StringBuffer();
856 List<int[]> regions = getHiddenRegions();
858 int blockStart = start, blockEnd = end;
860 int hideStart, hideEnd;
862 for (int j = 0; j < regions.size(); j++)
864 region = regions.get(j);
865 hideStart = region[0];
868 if (hideStart < start)
873 blockStart = Math.min(blockStart, hideEnd + 1);
874 blockEnd = Math.min(blockEnd, hideStart);
876 if (blockStart > blockEnd)
881 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
883 blockStart = hideEnd + 1;
887 if (end > blockStart)
889 visibleSeq.append(seqs[i].getSequence(blockStart, end));
892 selections[i] = visibleSeq.toString();
897 for (i = 0; i < iSize; i++)
899 selections[i] = seqs[i].getSequenceAsString(start, end);
907 lock.readLock().unlock();
912 * Locate the first and last position visible for this sequence. if seq isn't
913 * visible then return the position of the left and right of the hidden
914 * boundary region, and the corresponding alignment column indices for the
915 * extent of the sequence
918 * @return int[] { visible start, visible end, first seqpos, last seqpos,
919 * alignment index for seq start, alignment index for seq end }
921 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
925 lock.readLock().lock();
926 int fpos = seq.getStart(), lpos = seq.getEnd();
929 if (hiddenColumns == null || hiddenColumns.size() == 0)
931 int ifpos = seq.findIndex(fpos) - 1,
932 ilpos = seq.findIndex(lpos) - 1;
933 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
936 // Simply walk along the sequence whilst watching for hidden column
938 List<int[]> regions = getHiddenRegions();
939 int spos = fpos, lastvispos = -1, rcount = 0,
940 hideStart = seq.getLength(), hideEnd = -1;
941 int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
942 boolean foundStart = false;
943 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
946 if (!Comparison.isGap(seq.getCharAt(p)))
948 // keep track of first/last column
949 // containing sequence data regardless of visibility
955 // update hidden region start/end
956 while (hideEnd < p && rcount < regions.size())
958 int[] region = regions.get(rcount++);
960 visNext += region[0] - visPrev;
961 hideStart = region[0];
966 hideStart = seq.getLength();
968 // update visible boundary for sequence
980 // look for next sequence position
986 return new int[] { findColumnPosition(start),
987 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
989 // otherwise, sequence was completely hidden
990 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
994 lock.readLock().unlock();
999 * delete any columns in alignmentAnnotation that are hidden (including
1000 * sequence associated annotation).
1002 * @param alignmentAnnotation
1004 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1006 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1010 * delete any columns in alignmentAnnotation that are hidden (including
1011 * sequence associated annotation).
1014 * remove any annotation to the right of this column
1016 * remove any annotation to the left of this column
1017 * @param alignmentAnnotation
1018 * the annotation to operate on
1020 public void makeVisibleAnnotation(int start, int end,
1021 AlignmentAnnotation alignmentAnnotation)
1025 lock.readLock().lock();
1026 if (alignmentAnnotation.annotations == null)
1030 if (start == end && end == -1)
1033 end = alignmentAnnotation.annotations.length;
1035 if (hiddenColumns != null && hiddenColumns.size() > 0)
1037 // then mangle the alignmentAnnotation annotation array
1038 Vector<Annotation[]> annels = new Vector<>();
1039 Annotation[] els = null;
1040 List<int[]> regions = getHiddenRegions();
1041 int blockStart = start, blockEnd = end;
1043 int hideStart, hideEnd, w = 0;
1045 for (int j = 0; j < regions.size(); j++)
1047 region = regions.get(j);
1048 hideStart = region[0];
1049 hideEnd = region[1];
1051 if (hideStart < start)
1056 blockStart = Math.min(blockStart, hideEnd + 1);
1057 blockEnd = Math.min(blockEnd, hideStart);
1059 if (blockStart > blockEnd)
1064 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1065 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1068 blockStart = hideEnd + 1;
1072 if (end > blockStart)
1074 annels.addElement(els = new Annotation[end - blockStart + 1]);
1076 + blockStart) <= alignmentAnnotation.annotations.length)
1078 // copy just the visible segment of the annotation row
1079 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1080 els, 0, els.length);
1084 // copy to the end of the annotation row
1085 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1087 (alignmentAnnotation.annotations.length - blockStart));
1096 alignmentAnnotation.annotations = new Annotation[w];
1099 for (Annotation[] chnk : annels)
1101 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1108 alignmentAnnotation.restrict(start, end);
1113 lock.readLock().unlock();
1119 * @return true if there are columns hidden
1121 public boolean hasHiddenColumns()
1125 lock.readLock().lock();
1126 return hiddenColumns != null && hiddenColumns.size() > 0;
1129 lock.readLock().unlock();
1135 * @return true if there are more than one set of columns hidden
1137 public boolean hasManyHiddenColumns()
1141 lock.readLock().lock();
1142 return hiddenColumns != null && hiddenColumns.size() > 1;
1145 lock.readLock().unlock();
1150 * mark the columns corresponding to gap characters as hidden in the column
1155 public void hideInsertionsFor(SequenceI sr)
1159 lock.writeLock().lock();
1160 List<int[]> inserts = sr.getInsertions();
1161 for (int[] r : inserts)
1163 hideColumns(r[0], r[1], true);
1167 lock.writeLock().unlock();
1172 * Unhides, and adds to the selection list, all hidden columns
1174 public void revealAllHiddenColumns(ColumnSelection sel)
1178 lock.writeLock().lock();
1179 if (hiddenColumns != null)
1181 for (int i = 0; i < hiddenColumns.size(); i++)
1183 int[] region = hiddenColumns.elementAt(i);
1184 for (int j = region[0]; j < region[1] + 1; j++)
1191 hiddenColumns = null;
1195 lock.writeLock().unlock();
1200 * Reveals, and marks as selected, the hidden column range with the given
1205 public void revealHiddenColumns(int start, ColumnSelection sel)
1209 lock.writeLock().lock();
1210 for (int i = 0; i < hiddenColumns.size(); i++)
1212 int[] region = hiddenColumns.elementAt(i);
1213 if (start == region[0])
1215 for (int j = region[0]; j < region[1] + 1; j++)
1220 hiddenColumns.removeElement(region);
1224 if (hiddenColumns.size() == 0)
1226 hiddenColumns = null;
1231 lock.writeLock().unlock();
1236 * removes intersection of position,length ranges in deletions from the
1237 * start,end regions marked in intervals.
1243 private boolean pruneIntervalVector(final List<int[]> shifts,
1244 Vector<int[]> intervals)
1246 boolean pruned = false;
1247 int i = 0, j = intervals.size() - 1, s = 0, t = shifts.size() - 1;
1248 int hr[] = intervals.elementAt(i);
1249 int sr[] = shifts.get(s);
1250 while (i <= j && s <= t)
1252 boolean trailinghn = hr[1] >= sr[0];
1257 hr = intervals.elementAt(++i);
1265 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
1266 if (endshift < hr[0] || endshift < sr[0])
1267 { // leadinghc disjoint or not a deletion
1270 sr = shifts.get(++s);
1278 boolean leadinghn = hr[0] >= sr[0];
1279 boolean leadinghc = hr[0] < endshift;
1280 boolean trailinghc = hr[1] < endshift;
1284 { // deleted hidden region.
1285 intervals.removeElementAt(i);
1290 hr = intervals.elementAt(i);
1296 hr[0] = endshift; // clip c terminal region
1297 leadinghn = !leadinghn;
1313 // sr contained in hr
1316 sr = shifts.get(++s);
1326 return pruned; // true if any interval was removed or modified by
1331 * remove any hiddenColumns or selected columns and shift remaining based on a
1332 * series of position, range deletions.
1336 public void pruneDeletions(List<int[]> shifts)
1340 lock.writeLock().lock();
1341 // delete any intervals intersecting.
1342 if (hiddenColumns != null)
1344 pruneIntervalVector(shifts, hiddenColumns);
1345 if (hiddenColumns != null && hiddenColumns.size() == 0)
1347 hiddenColumns = null;
1353 lock.writeLock().unlock();
1358 * Add gaps into the sequences aligned to profileseq under the given
1363 * - alignment to have gaps inserted into it
1365 * - alignment view where sequence corresponding to profileseq is
1367 * @return new HiddenColumns for new alignment view, with insertions into
1368 * profileseq marked as hidden.
1370 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1371 AlignmentI al, AlignmentView input)
1375 char gc = al.getGapCharacter();
1376 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1377 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1378 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1379 nview.propagateInsertions(profileseq, al, origseq);
1386 * - sequence in al which corresponds to origseq
1388 * - alignment which is to have gaps inserted into it
1390 * - sequence corresponding to profileseq which defines gap map for
1393 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1396 char gc = al.getGapCharacter();
1397 // recover mapping between sequence's non-gap positions and positions
1399 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1400 int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
1401 profileseq.getLength());
1405 // add profile to visible contigs
1406 for (int v = 0; v < viscontigs.length; v += 2)
1408 if (viscontigs[v] > spos)
1410 StringBuffer sb = new StringBuffer();
1411 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1415 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1417 SequenceI sqobj = al.getSequenceAt(s);
1418 if (sqobj != profileseq)
1420 String sq = al.getSequenceAt(s).getSequenceAsString();
1421 if (sq.length() <= spos + offset)
1424 int diff = spos + offset - sq.length() - 1;
1429 while ((diff = spos + offset - sq.length() - 1) > 0)
1432 // + ((diff >= sb.length()) ? sb.toString() : sb
1433 // .substring(0, diff));
1434 if (diff >= sb.length())
1436 sq += sb.toString();
1440 char[] buf = new char[diff];
1441 sb.getChars(0, diff, buf, 0);
1442 sq += buf.toString();
1446 sq += sb.toString();
1450 al.getSequenceAt(s).setSequence(
1451 sq.substring(0, spos + offset) + sb.toString()
1452 + sq.substring(spos + offset));
1456 // offset+=sb.length();
1458 spos = viscontigs[v + 1] + 1;
1460 if ((offset + spos) < profileseq.getLength())
1462 // pad the final region with gaps.
1463 StringBuffer sb = new StringBuffer();
1464 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1468 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1470 SequenceI sqobj = al.getSequenceAt(s);
1471 if (sqobj == profileseq)
1475 String sq = sqobj.getSequenceAsString();
1477 int diff = origseq.getLength() - sq.length();
1481 // + ((diff >= sb.length()) ? sb.toString() : sb
1482 // .substring(0, diff));
1483 if (diff >= sb.length())
1485 sq += sb.toString();
1489 char[] buf = new char[diff];
1490 sb.getChars(0, diff, buf, 0);
1491 sq += buf.toString();
1493 diff = origseq.getLength() - sq.length();
1500 * remove any hiddenColumns or selected columns and shift remaining based on a
1501 * series of position, range deletions.
1505 private void pruneDeletions(ShiftList deletions)
1507 if (deletions != null)
1509 final List<int[]> shifts = deletions.getShifts();
1510 if (shifts != null && shifts.size() > 0)
1512 pruneDeletions(shifts);
1514 // and shift the rest.
1515 this.compensateForEdits(deletions);
1521 * Adjust hidden column boundaries based on a series of column additions or
1522 * deletions in visible regions.
1524 * @param shiftrecord
1527 private ShiftList compensateForEdits(ShiftList shiftrecord)
1529 if (shiftrecord != null)
1531 final List<int[]> shifts = shiftrecord.getShifts();
1532 if (shifts != null && shifts.size() > 0)
1535 for (int i = 0, j = shifts.size(); i < j; i++)
1537 int[] sh = shifts.get(i);
1538 compensateForDelEdits(shifted + sh[0], sh[1]);
1542 return shiftrecord.getInverse();
1548 * Returns a hashCode built from hidden column ranges
1551 public int hashCode()
1555 lock.readLock().lock();
1557 if (hiddenColumns != null)
1559 for (int[] hidden : hiddenColumns)
1561 hashCode = 31 * hashCode + hidden[0];
1562 hashCode = 31 * hashCode + hidden[1];
1569 lock.readLock().unlock();
1574 * Hide columns corresponding to the marked bits
1577 * - columns map to bits starting from zero
1579 public void hideMarkedBits(BitSet inserts)
1583 lock.writeLock().lock();
1584 for (int firstSet = inserts
1585 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1586 .nextSetBit(lastSet))
1588 lastSet = inserts.nextClearBit(firstSet);
1589 hideColumns(firstSet, lastSet - 1, true);
1593 lock.writeLock().unlock();
1600 * BitSet where hidden columns will be marked
1602 public void markHiddenRegions(BitSet inserts)
1606 lock.readLock().lock();
1607 if (hiddenColumns == null)
1611 for (int[] range : hiddenColumns)
1613 inserts.set(range[0], range[1] + 1);
1618 lock.readLock().unlock();
1623 public Iterator<int[]> iterator()
1625 if (hiddenColumns == null)
1627 return Collections.<int[]> emptyList().iterator();
1629 return hiddenColumns.iterator();