2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import jalview.util.Comparison;
24 import jalview.util.ShiftList;
26 import java.util.ArrayList;
27 import java.util.BitSet;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Vector;
31 import java.util.concurrent.locks.ReentrantReadWriteLock;
33 public class HiddenColumns
35 private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
38 * list of hidden column [start, end] ranges; the list is maintained in
39 * ascending start column order
41 private ArrayList<int[]> hiddenColumns;
46 public HiddenColumns()
55 public HiddenColumns(HiddenColumns copy)
59 LOCK.writeLock().lock();
62 if (copy.hiddenColumns != null)
64 hiddenColumns = copy.copyHiddenRegionsToArrayList();
69 LOCK.writeLock().unlock();
74 * This method is used to return all the HiddenColumn regions and is intended
75 * to remain private. External callers which need a copy of the regions can
76 * call getHiddenColumnsCopyAsList.
78 * @return empty list or List of hidden column intervals
80 private List<int[]> getHiddenRegions()
82 return hiddenColumns == null ? Collections.<int[]> emptyList()
87 * Output regions data as a string. String is in the format:
88 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
91 * string to delimit regions
92 * @param betweenstring
93 * to put between start and end region values
94 * @return regions formatted according to delimiter and between strings
96 public String regionsToString(String delimiter, String between)
100 LOCK.readLock().lock();
101 StringBuilder regionBuilder = new StringBuilder();
102 if (hiddenColumns != null)
104 for (int[] range : hiddenColumns)
106 regionBuilder.append(delimiter).append(range[0]).append(between)
110 regionBuilder.deleteCharAt(0);
112 return regionBuilder.toString();
115 LOCK.readLock().unlock();
120 * Find the number of hidden columns
122 * @return number of hidden columns
128 LOCK.readLock().lock();
130 if (hasHiddenColumns())
132 for (int[] range : hiddenColumns)
134 size += range[1] - range[0] + 1;
141 LOCK.readLock().unlock();
146 public boolean equals(Object obj)
150 LOCK.readLock().lock();
152 if (!(obj instanceof HiddenColumns))
156 HiddenColumns that = (HiddenColumns) obj;
159 * check hidden columns are either both null, or match
161 if (this.hiddenColumns == null)
163 return (that.hiddenColumns == null);
165 if (that.hiddenColumns == null
166 || that.hiddenColumns.size() != this.hiddenColumns.size())
171 for (int[] thisRange : hiddenColumns)
173 int[] thatRange = that.hiddenColumns.get(i++);
174 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
182 LOCK.readLock().unlock();
187 * Return absolute column index for a visible column index
190 * int column index in alignment view (count from zero)
191 * @return alignment column index for column
193 public int adjustForHiddenColumns(int column)
197 LOCK.readLock().lock();
199 if (hiddenColumns != null)
201 for (int i = 0; i < hiddenColumns.size(); i++)
203 int[] region = hiddenColumns.get(i);
204 if (result >= region[0])
206 result += region[1] - region[0] + 1;
213 LOCK.readLock().unlock();
218 * Use this method to find out where a column will appear in the visible
219 * alignment when hidden columns exist. If the column is not visible, then the
220 * left-most visible column will always be returned.
222 * @param hiddenColumn
223 * the column index in the full alignment including hidden columns
224 * @return the position of the column in the visible alignment
226 public int findColumnPosition(int hiddenColumn)
230 LOCK.readLock().lock();
231 int result = hiddenColumn;
232 if (hiddenColumns != null)
238 region = hiddenColumns.get(index++);
239 if (hiddenColumn > region[1])
241 result -= region[1] + 1 - region[0];
243 } while ((hiddenColumn > region[1])
244 && (index < hiddenColumns.size()));
246 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
248 // Here the hidden column is within a region, so
249 // we want to return the position of region[0]-1, adjusted for any
250 // earlier hidden columns.
251 // Calculate the difference between the actual hidden col position
252 // and region[0]-1, and then subtract from result to convert result
254 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
256 // However, if the region begins at 0 we cannot return region[0]-1
264 return result - (hiddenColumn - region[0] + 1);
268 return result; // return the shifted position after removing hidden
272 LOCK.readLock().unlock();
277 * Find the visible column which is a given visible number of columns to the
278 * left of another visible column. i.e. for a startColumn x, the column which
279 * is distance 1 away will be column x-1.
281 * @param visibleDistance
282 * the number of visible columns to offset by
284 * the column to start from
285 * @return the position of the column in the visible alignment
287 public int subtractVisibleColumns(int visibleDistance, int startColumn)
292 LOCK.readLock().lock();
293 int distance = visibleDistance;
295 // in case startColumn is in a hidden region, move it to the left
296 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
298 // get index of hidden region to left of start
299 int index = getHiddenIndexLeft(start);
302 // no hidden regions to left of startColumn
303 return start - distance;
306 // walk backwards through the alignment subtracting the counts of visible
307 // columns from distance
310 int nextstart = start;
312 while ((index > -1) && (distance - gap > 0))
314 // subtract the gap to right of region from distance
318 // calculate the next gap
319 region = hiddenColumns.get(index);
320 gap = start - region[1];
322 // set start to just to left of current region
323 nextstart = region[0] - 1;
327 if (distance - gap > 0)
329 // fell out of loop because there are no more hidden regions
331 return nextstart - distance;
333 return start - distance;
336 LOCK.readLock().unlock();
342 * Use this method to determine the set of hiddenRegion start positions
344 * @return list of column number in visible view where hidden regions start
346 public List<Integer> findHiddenRegionPositions()
350 LOCK.readLock().lock();
351 List<Integer> positions = null;
353 if (hiddenColumns != null)
355 positions = new ArrayList<>(hiddenColumns.size());
357 positions.add(hiddenColumns.get(0)[0]);
358 for (int i = 1; i < hiddenColumns.size(); ++i)
362 if (hiddenColumns != null)
368 int[] region = hiddenColumns.get(index);
369 gaps += region[1] + 1 - region[0];
370 result = region[1] + 1;
372 } while (index <= i);
376 positions.add(result);
381 positions = new ArrayList<>();
388 LOCK.readLock().unlock();
393 * This method returns the rightmost limit of a region of an alignment with
394 * hidden columns. In otherwords, the next hidden column.
399 public int getHiddenBoundaryRight(int alPos)
403 LOCK.readLock().lock();
404 if (hiddenColumns != null)
409 int[] region = hiddenColumns.get(index);
410 if (alPos < region[0])
416 } while (index < hiddenColumns.size());
422 LOCK.readLock().unlock();
428 * This method returns the leftmost limit of a region of an alignment with
429 * hidden columns. In otherwords, the previous hidden column.
434 public int getHiddenBoundaryLeft(int alPos)
438 LOCK.readLock().lock();
440 if (hiddenColumns != null)
442 int index = hiddenColumns.size() - 1;
445 int[] region = hiddenColumns.get(index);
446 if (alPos > region[1])
452 } while (index > -1);
458 LOCK.readLock().unlock();
463 * This method returns the index of the hidden region to the left of a column
464 * position. If the column is in a hidden region it returns the index of the
465 * region to the left. If there is no hidden region to the left it returns -1.
470 private int getHiddenIndexLeft(int pos)
475 LOCK.readLock().lock();
476 if (hiddenColumns != null)
478 int index = hiddenColumns.size() - 1;
481 int[] region = hiddenColumns.get(index);
488 } while (index > -1);
494 LOCK.readLock().unlock();
500 * Adds the specified column range to the hidden columns
505 public void hideColumns(int start, int end)
507 boolean wasAlreadyLocked = false;
510 // check if the write lock was already locked by this thread,
511 // as this method can be called internally in loops within HiddenColumns
512 if (!LOCK.isWriteLockedByCurrentThread())
514 LOCK.writeLock().lock();
518 wasAlreadyLocked = true;
521 if (hiddenColumns == null)
523 hiddenColumns = new ArrayList<>();
527 * traverse existing hidden ranges and insert / amend / append as
530 for (int i = 0; i < hiddenColumns.size(); i++)
532 int[] region = hiddenColumns.get(i);
534 if (end < region[0] - 1)
537 * insert discontiguous preceding range
539 hiddenColumns.add(i, new int[] { start, end });
543 if (end <= region[1])
546 * new range overlaps existing, or is contiguous preceding it - adjust
549 region[0] = Math.min(region[0], start);
553 if (start <= region[1] + 1)
556 * new range overlaps existing, or is contiguous following it - adjust
557 * start and end columns
559 region[0] = Math.min(region[0], start);
560 region[1] = Math.max(region[1], end);
563 * also update or remove any subsequent ranges
564 * that are overlapped
566 while (i < hiddenColumns.size() - 1)
568 int[] nextRegion = hiddenColumns.get(i + 1);
569 if (nextRegion[0] > end + 1)
572 * gap to next hidden range - no more to update
576 region[1] = Math.max(nextRegion[1], end);
577 hiddenColumns.remove(i + 1);
584 * remaining case is that the new range follows everything else
586 hiddenColumns.add(new int[] { start, end });
589 if (!wasAlreadyLocked)
591 LOCK.writeLock().unlock();
596 public boolean isVisible(int column)
600 LOCK.readLock().lock();
602 if (hiddenColumns != null)
604 for (int[] region : hiddenColumns)
606 if (column >= region[0] && column <= region[1])
616 LOCK.readLock().unlock();
620 private ArrayList<int[]> copyHiddenRegionsToArrayList()
623 if (hiddenColumns != null)
625 size = hiddenColumns.size();
627 ArrayList<int[]> copy = new ArrayList<>(size);
629 for (int i = 0, j = size; i < j; i++)
633 rh = hiddenColumns.get(i);
636 cp = new int[rh.length];
637 System.arraycopy(rh, 0, cp, 0, rh.length);
646 * Returns a copy of the vector of hidden regions, as an ArrayList. Before
647 * using this method please consider if you really need access to the hidden
648 * regions - a new (or existing!) method on HiddenColumns might be more
651 * @return hidden regions as an ArrayList of [start,end] pairs
653 public ArrayList<int[]> getHiddenColumnsCopy()
657 LOCK.readLock().lock();
658 return copyHiddenRegionsToArrayList();
661 LOCK.readLock().unlock();
666 * propagate shift in alignment columns to column selection
671 * shift in edit (+ve for removal, or -ve for inserts)
673 public List<int[]> compensateForEdit(int start, int change,
678 LOCK.writeLock().lock();
679 List<int[]> deletedHiddenColumns = null;
681 if (hiddenColumns != null)
683 deletedHiddenColumns = new ArrayList<>();
684 int hSize = hiddenColumns.size();
685 for (int i = 0; i < hSize; i++)
687 int[] region = hiddenColumns.get(i);
688 if (region[0] > start && start + change > region[1])
690 deletedHiddenColumns.add(region);
692 hiddenColumns.remove(i);
698 if (region[0] > start)
711 this.revealHiddenColumns(0, sel);
714 return deletedHiddenColumns;
717 LOCK.writeLock().unlock();
722 * propagate shift in alignment columns to column selection special version of
723 * compensateForEdit - allowing for edits within hidden regions
728 * shift in edit (+ve for removal, or -ve for inserts)
730 public void compensateForDelEdits(int start, int change)
734 LOCK.writeLock().lock();
735 if (hiddenColumns != null)
737 for (int i = 0; i < hiddenColumns.size(); i++)
739 int[] region = hiddenColumns.get(i);
740 if (region[0] >= start)
744 if (region[1] >= start)
748 if (region[1] < region[0])
750 hiddenColumns.remove(i--);
766 LOCK.writeLock().unlock();
771 * return all visible segments between the given start and end boundaries
774 * (first column inclusive from 0)
776 * (last column - not inclusive)
777 * @return int[] {i_start, i_end, ..} where intervals lie in
778 * start<=i_start<=i_end<end
780 public int[] getVisibleContigs(int start, int end)
784 LOCK.readLock().lock();
785 if (hiddenColumns != null && hiddenColumns.size() > 0)
787 List<int[]> visiblecontigs = new ArrayList<>();
788 List<int[]> regions = getHiddenRegions();
795 for (int j = 0; vstart < end && j < regions.size(); j++)
797 region = regions.get(j);
798 hideStart = region[0];
801 if (hideEnd < vstart)
805 if (hideStart > vstart)
807 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
809 vstart = hideEnd + 1;
814 visiblecontigs.add(new int[] { vstart, end - 1 });
816 int[] vcontigs = new int[visiblecontigs.size() * 2];
817 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
819 int[] vc = visiblecontigs.get(i);
820 visiblecontigs.set(i, null);
821 vcontigs[i * 2] = vc[0];
822 vcontigs[i * 2 + 1] = vc[1];
824 visiblecontigs.clear();
829 return new int[] { start, end - 1 };
834 LOCK.readLock().unlock();
838 public String[] getVisibleSequenceStrings(int start, int end,
843 LOCK.readLock().lock();
844 int iSize = seqs.length;
845 String[] selections = new String[iSize];
846 if (hiddenColumns != null && hiddenColumns.size() > 0)
848 for (int i = 0; i < iSize; i++)
850 StringBuffer visibleSeq = new StringBuffer();
851 List<int[]> regions = getHiddenRegions();
853 int blockStart = start;
859 for (int j = 0; j < regions.size(); j++)
861 region = regions.get(j);
862 hideStart = region[0];
865 if (hideStart < start)
870 blockStart = Math.min(blockStart, hideEnd + 1);
871 blockEnd = Math.min(blockEnd, hideStart);
873 if (blockStart > blockEnd)
878 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
880 blockStart = hideEnd + 1;
884 if (end > blockStart)
886 visibleSeq.append(seqs[i].getSequence(blockStart, end));
889 selections[i] = visibleSeq.toString();
894 for (int i = 0; i < iSize; i++)
896 selections[i] = seqs[i].getSequenceAsString(start, end);
904 LOCK.readLock().unlock();
909 * Locate the first and last position visible for this sequence. if seq isn't
910 * visible then return the position of the left and right of the hidden
911 * boundary region, and the corresponding alignment column indices for the
912 * extent of the sequence
915 * @return int[] { visible start, visible end, first seqpos, last seqpos,
916 * alignment index for seq start, alignment index for seq end }
918 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
922 LOCK.readLock().lock();
923 int fpos = seq.getStart();
924 int lpos = seq.getEnd();
927 if (hiddenColumns == null || hiddenColumns.size() == 0)
929 int ifpos = seq.findIndex(fpos) - 1;
930 int ilpos = seq.findIndex(lpos) - 1;
931 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
934 // Simply walk along the sequence whilst watching for hidden column
936 List<int[]> regions = getHiddenRegions();
940 int hideStart = seq.getLength();
946 boolean foundStart = false;
947 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
950 if (!Comparison.isGap(seq.getCharAt(p)))
952 // keep track of first/last column
953 // containing sequence data regardless of visibility
959 // update hidden region start/end
960 while (hideEnd < p && rcount < regions.size())
962 int[] region = regions.get(rcount++);
964 visNext += region[0] - visPrev;
965 hideStart = region[0];
970 hideStart = seq.getLength();
972 // update visible boundary for sequence
984 // look for next sequence position
990 return new int[] { findColumnPosition(start),
991 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
993 // otherwise, sequence was completely hidden
994 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
998 LOCK.readLock().unlock();
1003 * delete any columns in alignmentAnnotation that are hidden (including
1004 * sequence associated annotation).
1006 * @param alignmentAnnotation
1008 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1010 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1014 * delete any columns in alignmentAnnotation that are hidden (including
1015 * sequence associated annotation).
1018 * remove any annotation to the right of this column
1020 * remove any annotation to the left of this column
1021 * @param alignmentAnnotation
1022 * the annotation to operate on
1024 public void makeVisibleAnnotation(int start, int end,
1025 AlignmentAnnotation alignmentAnnotation)
1029 LOCK.readLock().lock();
1030 if (alignmentAnnotation.annotations == null)
1034 if (start == end && end == -1)
1037 end = alignmentAnnotation.annotations.length;
1039 if (hiddenColumns != null && hiddenColumns.size() > 0)
1041 // then mangle the alignmentAnnotation annotation array
1042 Vector<Annotation[]> annels = new Vector<>();
1043 Annotation[] els = null;
1044 List<int[]> regions = getHiddenRegions();
1045 int blockStart = start;
1052 for (int j = 0; j < regions.size(); j++)
1054 region = regions.get(j);
1055 hideStart = region[0];
1056 hideEnd = region[1];
1058 if (hideStart < start)
1063 blockStart = Math.min(blockStart, hideEnd + 1);
1064 blockEnd = Math.min(blockEnd, hideStart);
1066 if (blockStart > blockEnd)
1071 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1072 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1075 blockStart = hideEnd + 1;
1079 if (end > blockStart)
1081 annels.addElement(els = new Annotation[end - blockStart + 1]);
1083 + blockStart) <= alignmentAnnotation.annotations.length)
1085 // copy just the visible segment of the annotation row
1086 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1087 els, 0, els.length);
1091 // copy to the end of the annotation row
1092 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1094 (alignmentAnnotation.annotations.length - blockStart));
1103 alignmentAnnotation.annotations = new Annotation[w];
1106 for (Annotation[] chnk : annels)
1108 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1115 alignmentAnnotation.restrict(start, end);
1120 LOCK.readLock().unlock();
1126 * @return true if there are columns hidden
1128 public boolean hasHiddenColumns()
1132 LOCK.readLock().lock();
1133 return hiddenColumns != null && hiddenColumns.size() > 0;
1136 LOCK.readLock().unlock();
1142 * @return true if there are more than one set of columns hidden
1144 public boolean hasManyHiddenColumns()
1148 LOCK.readLock().lock();
1149 return hiddenColumns != null && hiddenColumns.size() > 1;
1152 LOCK.readLock().unlock();
1157 * mark the columns corresponding to gap characters as hidden in the column
1162 public void hideInsertionsFor(SequenceI sr)
1166 LOCK.writeLock().lock();
1167 List<int[]> inserts = sr.getInsertions();
1168 for (int[] r : inserts)
1170 hideColumns(r[0], r[1]);
1174 LOCK.writeLock().unlock();
1179 * Unhides, and adds to the selection list, all hidden columns
1181 public void revealAllHiddenColumns(ColumnSelection sel)
1185 LOCK.writeLock().lock();
1186 if (hiddenColumns != null)
1188 for (int i = 0; i < hiddenColumns.size(); i++)
1190 int[] region = hiddenColumns.get(i);
1191 for (int j = region[0]; j < region[1] + 1; j++)
1198 hiddenColumns = null;
1202 LOCK.writeLock().unlock();
1207 * Reveals, and marks as selected, the hidden column range with the given
1212 public void revealHiddenColumns(int start, ColumnSelection sel)
1216 LOCK.writeLock().lock();
1217 for (int i = 0; i < hiddenColumns.size(); i++)
1219 int[] region = hiddenColumns.get(i);
1220 if (start == region[0])
1222 for (int j = region[0]; j < region[1] + 1; j++)
1227 hiddenColumns.remove(region);
1231 if (hiddenColumns.size() == 0)
1233 hiddenColumns = null;
1238 LOCK.writeLock().unlock();
1243 * removes intersection of position,length ranges in deletions from the
1244 * start,end regions marked in intervals.
1250 private boolean pruneIntervalList(final List<int[]> shifts,
1251 ArrayList<int[]> intervals)
1253 boolean pruned = false;
1255 int j = intervals.size() - 1;
1257 int t = shifts.size() - 1;
1258 int[] hr = intervals.get(i);
1259 int[] sr = shifts.get(s);
1260 while (i <= j && s <= t)
1262 boolean trailinghn = hr[1] >= sr[0];
1267 hr = intervals.get(++i);
1275 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
1276 if (endshift < hr[0] || endshift < sr[0])
1277 { // leadinghc disjoint or not a deletion
1280 sr = shifts.get(++s);
1288 boolean leadinghn = hr[0] >= sr[0];
1289 boolean leadinghc = hr[0] < endshift;
1290 boolean trailinghc = hr[1] < endshift;
1294 { // deleted hidden region.
1295 intervals.remove(i);
1300 hr = intervals.get(i);
1306 hr[0] = endshift; // clip c terminal region
1307 leadinghn = !leadinghn;
1323 // sr contained in hr
1326 sr = shifts.get(++s);
1336 return pruned; // true if any interval was removed or modified by
1341 * remove any hiddenColumns or selected columns and shift remaining based on a
1342 * series of position, range deletions.
1346 public void pruneDeletions(List<int[]> shifts)
1350 LOCK.writeLock().lock();
1351 // delete any intervals intersecting.
1352 if (hiddenColumns != null)
1354 pruneIntervalList(shifts, hiddenColumns);
1355 if (hiddenColumns != null && hiddenColumns.size() == 0)
1357 hiddenColumns = null;
1363 LOCK.writeLock().unlock();
1368 * Add gaps into the sequences aligned to profileseq under the given
1373 * - alignment to have gaps inserted into it
1375 * - alignment view where sequence corresponding to profileseq is
1377 * @return new HiddenColumns for new alignment view, with insertions into
1378 * profileseq marked as hidden.
1380 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1381 AlignmentI al, AlignmentView input)
1385 char gc = al.getGapCharacter();
1386 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1387 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1388 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1389 nview.propagateInsertions(profileseq, al, origseq);
1396 * - sequence in al which corresponds to origseq
1398 * - alignment which is to have gaps inserted into it
1400 * - sequence corresponding to profileseq which defines gap map for
1403 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1406 char gc = al.getGapCharacter();
1407 // recover mapping between sequence's non-gap positions and positions
1409 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1410 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1414 // add profile to visible contigs
1415 for (int v = 0; v < viscontigs.length; v += 2)
1417 if (viscontigs[v] > spos)
1419 StringBuffer sb = new StringBuffer();
1420 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1424 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1426 SequenceI sqobj = al.getSequenceAt(s);
1427 if (sqobj != profileseq)
1429 String sq = al.getSequenceAt(s).getSequenceAsString();
1430 if (sq.length() <= spos + offset)
1433 int diff = spos + offset - sq.length() - 1;
1438 while ((diff = spos + offset - sq.length() - 1) > 0)
1441 // + ((diff >= sb.length()) ? sb.toString() : sb
1442 // .substring(0, diff));
1443 if (diff >= sb.length())
1445 sq += sb.toString();
1449 char[] buf = new char[diff];
1450 sb.getChars(0, diff, buf, 0);
1451 sq += buf.toString();
1455 sq += sb.toString();
1459 al.getSequenceAt(s).setSequence(
1460 sq.substring(0, spos + offset) + sb.toString()
1461 + sq.substring(spos + offset));
1465 // offset+=sb.length();
1467 spos = viscontigs[v + 1] + 1;
1469 if ((offset + spos) < profileseq.getLength())
1471 // pad the final region with gaps.
1472 StringBuffer sb = new StringBuffer();
1473 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1477 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1479 SequenceI sqobj = al.getSequenceAt(s);
1480 if (sqobj == profileseq)
1484 String sq = sqobj.getSequenceAsString();
1486 int diff = origseq.getLength() - sq.length();
1490 // + ((diff >= sb.length()) ? sb.toString() : sb
1491 // .substring(0, diff));
1492 if (diff >= sb.length())
1494 sq += sb.toString();
1498 char[] buf = new char[diff];
1499 sb.getChars(0, diff, buf, 0);
1500 sq += buf.toString();
1502 diff = origseq.getLength() - sq.length();
1509 * remove any hiddenColumns or selected columns and shift remaining based on a
1510 * series of position, range deletions.
1514 private void pruneDeletions(ShiftList deletions)
1516 if (deletions != null)
1518 final List<int[]> shifts = deletions.getShifts();
1519 if (shifts != null && shifts.size() > 0)
1521 pruneDeletions(shifts);
1523 // and shift the rest.
1524 this.compensateForEdits(deletions);
1530 * Adjust hidden column boundaries based on a series of column additions or
1531 * deletions in visible regions.
1533 * @param shiftrecord
1536 private ShiftList compensateForEdits(ShiftList shiftrecord)
1538 if (shiftrecord != null)
1540 final List<int[]> shifts = shiftrecord.getShifts();
1541 if (shifts != null && shifts.size() > 0)
1544 for (int i = 0, j = shifts.size(); i < j; i++)
1546 int[] sh = shifts.get(i);
1547 compensateForDelEdits(shifted + sh[0], sh[1]);
1551 return shiftrecord.getInverse();
1557 * Returns a hashCode built from hidden column ranges
1560 public int hashCode()
1564 LOCK.readLock().lock();
1566 if (hiddenColumns != null)
1568 for (int[] hidden : hiddenColumns)
1570 hashCode = 31 * hashCode + hidden[0];
1571 hashCode = 31 * hashCode + hidden[1];
1578 LOCK.readLock().unlock();
1583 * Hide columns corresponding to the marked bits
1586 * - columns map to bits starting from zero
1588 public void hideMarkedBits(BitSet inserts)
1592 LOCK.writeLock().lock();
1593 for (int firstSet = inserts
1594 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1595 .nextSetBit(lastSet))
1597 lastSet = inserts.nextClearBit(firstSet);
1598 hideColumns(firstSet, lastSet - 1);
1602 LOCK.writeLock().unlock();
1609 * BitSet where hidden columns will be marked
1611 public void markHiddenRegions(BitSet inserts)
1615 LOCK.readLock().lock();
1616 if (hiddenColumns == null)
1620 for (int[] range : hiddenColumns)
1622 inserts.set(range[0], range[1] + 1);
1627 LOCK.readLock().unlock();
1632 * Calculate the visible start and end index of an alignment.
1635 * full alignment width
1636 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1638 public int[] getVisibleStartAndEndIndex(int width)
1642 LOCK.readLock().lock();
1643 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1644 int startPos = alignmentStartEnd[0];
1645 int endPos = alignmentStartEnd[1];
1647 int[] lowestRange = new int[] { -1, -1 };
1648 int[] higestRange = new int[] { -1, -1 };
1650 if (hiddenColumns == null)
1652 return new int[] { startPos, endPos };
1655 for (int[] hiddenCol : hiddenColumns)
1657 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1658 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1661 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1663 startPos = alignmentStartEnd[0];
1667 startPos = lowestRange[1] + 1;
1670 if (higestRange[0] == -1 && higestRange[1] == -1)
1672 endPos = alignmentStartEnd[1];
1676 endPos = higestRange[0] - 1;
1678 return new int[] { startPos, endPos };
1681 LOCK.readLock().unlock();
1687 * Finds the hidden region (if any) which starts or ends at res
1690 * visible residue position, unadjusted for hidden columns
1691 * @return region as [start,end] or null if no matching region is found
1693 public int[] getRegionWithEdgeAtRes(int res)
1697 LOCK.readLock().lock();
1698 int adjres = adjustForHiddenColumns(res);
1700 int[] reveal = null;
1701 if (hiddenColumns != null)
1703 for (int[] region : hiddenColumns)
1705 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1715 LOCK.readLock().unlock();