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;
140 LOCK.readLock().unlock();
145 public boolean equals(Object obj)
149 LOCK.readLock().lock();
151 if (!(obj instanceof HiddenColumns))
155 HiddenColumns that = (HiddenColumns) obj;
158 * check hidden columns are either both null, or match
160 if (this.hiddenColumns == null)
162 return (that.hiddenColumns == null);
164 if (that.hiddenColumns == null
165 || that.hiddenColumns.size() != this.hiddenColumns.size())
170 for (int[] thisRange : hiddenColumns)
172 int[] thatRange = that.hiddenColumns.get(i++);
173 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
181 LOCK.readLock().unlock();
186 * Return absolute column index for a visible column index
189 * int column index in alignment view (count from zero)
190 * @return alignment column index for column
192 public int adjustForHiddenColumns(int column)
196 LOCK.readLock().lock();
198 if (hiddenColumns != null)
200 for (int i = 0; i < hiddenColumns.size(); i++)
202 int[] region = hiddenColumns.get(i);
203 if (result >= region[0])
205 result += region[1] - region[0] + 1;
212 LOCK.readLock().unlock();
217 * Use this method to find out where a column will appear in the visible
218 * alignment when hidden columns exist. If the column is not visible, then the
219 * left-most visible column will always be returned.
221 * @param hiddenColumn
222 * the column index in the full alignment including hidden columns
223 * @return the position of the column in the visible alignment
225 public int findColumnPosition(int hiddenColumn)
229 LOCK.readLock().lock();
230 int result = hiddenColumn;
231 if (hiddenColumns != null)
237 region = hiddenColumns.get(index++);
238 if (hiddenColumn > region[1])
240 result -= region[1] + 1 - region[0];
242 } while ((hiddenColumn > region[1])
243 && (index < hiddenColumns.size()));
245 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
247 // Here the hidden column is within a region, so
248 // we want to return the position of region[0]-1, adjusted for any
249 // earlier hidden columns.
250 // Calculate the difference between the actual hidden col position
251 // and region[0]-1, and then subtract from result to convert result
253 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
255 // However, if the region begins at 0 we cannot return region[0]-1
263 return result - (hiddenColumn - region[0] + 1);
267 return result; // return the shifted position after removing hidden
271 LOCK.readLock().unlock();
276 * Find the visible column which is a given visible number of columns to the
277 * left of another visible column. i.e. for a startColumn x, the column which
278 * is distance 1 away will be column x-1.
280 * @param visibleDistance
281 * the number of visible columns to offset by
283 * the column to start from
284 * @return the position of the column in the visible alignment
286 public int subtractVisibleColumns(int visibleDistance, int startColumn)
291 LOCK.readLock().lock();
292 int distance = visibleDistance;
294 // in case startColumn is in a hidden region, move it to the left
295 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
297 // get index of hidden region to left of start
298 int index = getHiddenIndexLeft(start);
301 // no hidden regions to left of startColumn
302 return start - distance;
305 // walk backwards through the alignment subtracting the counts of visible
306 // columns from distance
309 int nextstart = start;
311 while ((index > -1) && (distance - gap > 0))
313 // subtract the gap to right of region from distance
317 // calculate the next gap
318 region = hiddenColumns.get(index);
319 gap = start - region[1];
321 // set start to just to left of current region
322 nextstart = region[0] - 1;
326 if (distance - gap > 0)
328 // fell out of loop because there are no more hidden regions
330 return nextstart - distance;
332 return start - distance;
335 LOCK.readLock().unlock();
341 * Use this method to determine the set of hiddenRegion start positions
343 * @return list of column number in visible view where hidden regions start
345 public List<Integer> findHiddenRegionPositions()
349 LOCK.readLock().lock();
350 List<Integer> positions = null;
352 if (hiddenColumns != null)
354 positions = new ArrayList<>(hiddenColumns.size());
356 positions.add(hiddenColumns.get(0)[0]);
357 for (int i = 1; i < hiddenColumns.size(); ++i)
361 if (hiddenColumns != null)
367 int[] region = hiddenColumns.get(index);
368 gaps += region[1] + 1 - region[0];
369 result = region[1] + 1;
371 } while (index <= i);
375 positions.add(result);
380 positions = new ArrayList<>();
386 LOCK.readLock().unlock();
391 * This method returns the rightmost limit of a region of an alignment with
392 * hidden columns. In otherwords, the next hidden column.
397 public int getHiddenBoundaryRight(int alPos)
401 LOCK.readLock().lock();
402 if (hiddenColumns != null)
407 int[] region = hiddenColumns.get(index);
408 if (alPos < region[0])
414 } while (index < hiddenColumns.size());
420 LOCK.readLock().unlock();
426 * This method returns the leftmost limit of a region of an alignment with
427 * hidden columns. In otherwords, the previous hidden column.
432 public int getHiddenBoundaryLeft(int alPos)
436 LOCK.readLock().lock();
438 if (hiddenColumns != null)
440 int index = hiddenColumns.size() - 1;
443 int[] region = hiddenColumns.get(index);
444 if (alPos > region[1])
450 } while (index > -1);
456 LOCK.readLock().unlock();
461 * This method returns the index of the hidden region to the left of a column
462 * position. If the column is in a hidden region it returns the index of the
463 * region to the left. If there is no hidden region to the left it returns -1.
468 private int getHiddenIndexLeft(int pos)
473 LOCK.readLock().lock();
474 if (hiddenColumns != null)
476 int index = hiddenColumns.size() - 1;
479 int[] region = hiddenColumns.get(index);
486 } while (index > -1);
492 LOCK.readLock().unlock();
498 * Adds the specified column range to the hidden columns
503 public void hideColumns(int start, int end)
505 boolean wasAlreadyLocked = false;
508 // check if the write lock was already locked by this thread,
509 // as this method can be called internally in loops within HiddenColumns
510 if (!LOCK.isWriteLockedByCurrentThread())
512 LOCK.writeLock().lock();
516 wasAlreadyLocked = true;
519 if (hiddenColumns == null)
521 hiddenColumns = new ArrayList<>();
525 * traverse existing hidden ranges and insert / amend / append as
528 for (int i = 0; i < hiddenColumns.size(); i++)
530 int[] region = hiddenColumns.get(i);
532 if (end < region[0] - 1)
535 * insert discontiguous preceding range
537 hiddenColumns.add(i, new int[] { start, end });
541 if (end <= region[1])
544 * new range overlaps existing, or is contiguous preceding it - adjust
547 region[0] = Math.min(region[0], start);
551 if (start <= region[1] + 1)
554 * new range overlaps existing, or is contiguous following it - adjust
555 * start and end columns
557 region[0] = Math.min(region[0], start);
558 region[1] = Math.max(region[1], end);
561 * also update or remove any subsequent ranges
562 * that are overlapped
564 while (i < hiddenColumns.size() - 1)
566 int[] nextRegion = hiddenColumns.get(i + 1);
567 if (nextRegion[0] > end + 1)
570 * gap to next hidden range - no more to update
574 region[1] = Math.max(nextRegion[1], end);
575 hiddenColumns.remove(i + 1);
582 * remaining case is that the new range follows everything else
584 hiddenColumns.add(new int[] { start, end });
587 if (!wasAlreadyLocked)
589 LOCK.writeLock().unlock();
594 public boolean isVisible(int column)
598 LOCK.readLock().lock();
600 if (hiddenColumns != null)
602 for (int[] region : hiddenColumns)
604 if (column >= region[0] && column <= region[1])
614 LOCK.readLock().unlock();
618 private ArrayList<int[]> copyHiddenRegionsToArrayList()
621 if (hiddenColumns != null)
623 size = hiddenColumns.size();
625 ArrayList<int[]> copy = new ArrayList<>(size);
627 for (int i = 0, j = size; i < j; i++)
631 rh = hiddenColumns.get(i);
634 cp = new int[rh.length];
635 System.arraycopy(rh, 0, cp, 0, rh.length);
644 * Returns a copy of the vector of hidden regions, as an ArrayList. Before
645 * using this method please consider if you really need access to the hidden
646 * regions - a new (or existing!) method on HiddenColumns might be more
649 * @return hidden regions as an ArrayList of [start,end] pairs
651 public ArrayList<int[]> getHiddenColumnsCopy()
655 LOCK.readLock().lock();
656 return copyHiddenRegionsToArrayList();
659 LOCK.readLock().unlock();
664 * propagate shift in alignment columns to column selection
669 * shift in edit (+ve for removal, or -ve for inserts)
671 public List<int[]> compensateForEdit(int start, int change,
676 LOCK.writeLock().lock();
677 List<int[]> deletedHiddenColumns = null;
679 if (hiddenColumns != null)
681 deletedHiddenColumns = new ArrayList<>();
682 int hSize = hiddenColumns.size();
683 for (int i = 0; i < hSize; i++)
685 int[] region = hiddenColumns.get(i);
686 if (region[0] > start && start + change > region[1])
688 deletedHiddenColumns.add(region);
690 hiddenColumns.remove(i);
696 if (region[0] > start)
709 this.revealHiddenColumns(0, sel);
712 return deletedHiddenColumns;
715 LOCK.writeLock().unlock();
720 * propagate shift in alignment columns to column selection special version of
721 * compensateForEdit - allowing for edits within hidden regions
726 * shift in edit (+ve for removal, or -ve for inserts)
728 public void compensateForDelEdits(int start, int change)
732 LOCK.writeLock().lock();
733 if (hiddenColumns != null)
735 for (int i = 0; i < hiddenColumns.size(); i++)
737 int[] region = hiddenColumns.get(i);
738 if (region[0] >= start)
742 if (region[1] >= start)
746 if (region[1] < region[0])
748 hiddenColumns.remove(i--);
763 LOCK.writeLock().unlock();
768 * return all visible segments between the given start and end boundaries
771 * (first column inclusive from 0)
773 * (last column - not inclusive)
774 * @return int[] {i_start, i_end, ..} where intervals lie in
775 * start<=i_start<=i_end<end
777 public int[] getVisibleContigs(int start, int end)
781 LOCK.readLock().lock();
782 if (hiddenColumns != null && hiddenColumns.size() > 0)
784 List<int[]> visiblecontigs = new ArrayList<>();
785 List<int[]> regions = getHiddenRegions();
792 for (int j = 0; vstart < end && j < regions.size(); j++)
794 region = regions.get(j);
795 hideStart = region[0];
798 if (hideEnd < vstart)
802 if (hideStart > vstart)
804 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
806 vstart = hideEnd + 1;
811 visiblecontigs.add(new int[] { vstart, end - 1 });
813 int[] vcontigs = new int[visiblecontigs.size() * 2];
814 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
816 int[] vc = visiblecontigs.get(i);
817 visiblecontigs.set(i, null);
818 vcontigs[i * 2] = vc[0];
819 vcontigs[i * 2 + 1] = vc[1];
821 visiblecontigs.clear();
826 return new int[] { start, end - 1 };
830 LOCK.readLock().unlock();
834 public String[] getVisibleSequenceStrings(int start, int end,
839 LOCK.readLock().lock();
840 int iSize = seqs.length;
841 String[] selections = new String[iSize];
842 if (hiddenColumns != null && hiddenColumns.size() > 0)
844 for (int i = 0; i < iSize; i++)
846 StringBuffer visibleSeq = new StringBuffer();
847 List<int[]> regions = getHiddenRegions();
849 int blockStart = start;
855 for (int j = 0; j < regions.size(); j++)
857 region = regions.get(j);
858 hideStart = region[0];
861 if (hideStart < start)
866 blockStart = Math.min(blockStart, hideEnd + 1);
867 blockEnd = Math.min(blockEnd, hideStart);
869 if (blockStart > blockEnd)
874 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
876 blockStart = hideEnd + 1;
880 if (end > blockStart)
882 visibleSeq.append(seqs[i].getSequence(blockStart, end));
885 selections[i] = visibleSeq.toString();
890 for (int i = 0; i < iSize; i++)
892 selections[i] = seqs[i].getSequenceAsString(start, end);
899 LOCK.readLock().unlock();
904 * Locate the first and last position visible for this sequence. if seq isn't
905 * visible then return the position of the left and right of the hidden
906 * boundary region, and the corresponding alignment column indices for the
907 * extent of the sequence
910 * @return int[] { visible start, visible end, first seqpos, last seqpos,
911 * alignment index for seq start, alignment index for seq end }
913 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
917 LOCK.readLock().lock();
918 int fpos = seq.getStart();
919 int lpos = seq.getEnd();
922 if (hiddenColumns == null || hiddenColumns.size() == 0)
924 int ifpos = seq.findIndex(fpos) - 1;
925 int ilpos = seq.findIndex(lpos) - 1;
926 return new int[] { ifpos, ifpos, ilpos };
929 // Simply walk along the sequence whilst watching for hidden column
931 List<int[]> regions = getHiddenRegions();
935 int hideStart = seq.getLength();
941 boolean foundStart = false;
942 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
945 if (!Comparison.isGap(seq.getCharAt(p)))
947 // keep track of first/last column
948 // containing sequence data regardless of visibility
954 // update hidden region start/end
955 while (hideEnd < p && rcount < regions.size())
957 int[] region = regions.get(rcount++);
959 visNext += region[0] - visPrev;
960 hideStart = region[0];
965 hideStart = seq.getLength();
967 // update visible boundary for sequence
979 // look for next sequence position
985 return new int[] { findColumnPosition(start), firstP, lastP };
987 // otherwise, sequence was completely hidden
988 return new int[] { visPrev, firstP, lastP };
991 LOCK.readLock().unlock();
996 * delete any columns in alignmentAnnotation that are hidden (including
997 * sequence associated annotation).
999 * @param alignmentAnnotation
1001 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1003 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1007 * delete any columns in alignmentAnnotation that are hidden (including
1008 * sequence associated annotation).
1011 * remove any annotation to the right of this column
1013 * remove any annotation to the left of this column
1014 * @param alignmentAnnotation
1015 * the annotation to operate on
1017 public void makeVisibleAnnotation(int start, int end,
1018 AlignmentAnnotation alignmentAnnotation)
1022 LOCK.readLock().lock();
1023 if (alignmentAnnotation.annotations == null)
1027 if (start == end && end == -1)
1030 end = alignmentAnnotation.annotations.length;
1032 if (hiddenColumns != null && hiddenColumns.size() > 0)
1034 // then mangle the alignmentAnnotation annotation array
1035 Vector<Annotation[]> annels = new Vector<>();
1036 Annotation[] els = null;
1037 List<int[]> regions = getHiddenRegions();
1038 int blockStart = start;
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);
1112 LOCK.readLock().unlock();
1118 * @return true if there are columns hidden
1120 public boolean hasHiddenColumns()
1124 LOCK.readLock().lock();
1125 return hiddenColumns != null && hiddenColumns.size() > 0;
1128 LOCK.readLock().unlock();
1134 * @return true if there are more than one set of columns hidden
1136 public boolean hasManyHiddenColumns()
1140 LOCK.readLock().lock();
1141 return hiddenColumns != null && hiddenColumns.size() > 1;
1144 LOCK.readLock().unlock();
1149 * mark the columns corresponding to gap characters as hidden in the column
1154 public void hideInsertionsFor(SequenceI sr)
1158 LOCK.writeLock().lock();
1159 List<int[]> inserts = sr.getInsertions();
1160 for (int[] r : inserts)
1162 hideColumns(r[0], r[1]);
1166 LOCK.writeLock().unlock();
1171 * Unhides, and adds to the selection list, all hidden columns
1173 public void revealAllHiddenColumns(ColumnSelection sel)
1177 LOCK.writeLock().lock();
1178 if (hiddenColumns != null)
1180 for (int i = 0; i < hiddenColumns.size(); i++)
1182 int[] region = hiddenColumns.get(i);
1183 for (int j = region[0]; j < region[1] + 1; j++)
1190 hiddenColumns = null;
1193 LOCK.writeLock().unlock();
1198 * Reveals, and marks as selected, the hidden column range with the given
1203 public void revealHiddenColumns(int start, ColumnSelection sel)
1207 LOCK.writeLock().lock();
1208 for (int i = 0; i < hiddenColumns.size(); i++)
1210 int[] region = hiddenColumns.get(i);
1211 if (start == region[0])
1213 for (int j = region[0]; j < region[1] + 1; j++)
1218 hiddenColumns.remove(region);
1222 if (hiddenColumns.size() == 0)
1224 hiddenColumns = null;
1228 LOCK.writeLock().unlock();
1233 * removes intersection of position,length ranges in deletions from the
1234 * start,end regions marked in intervals.
1240 private boolean pruneIntervalList(final List<int[]> shifts,
1241 ArrayList<int[]> intervals)
1243 boolean pruned = false;
1245 int j = intervals.size() - 1;
1247 int t = shifts.size() - 1;
1248 int[] hr = intervals.get(i);
1249 int[] sr = shifts.get(s);
1250 while (i <= j && s <= t)
1252 boolean trailinghn = hr[1] >= sr[0];
1257 hr = intervals.get(++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.remove(i);
1290 hr = intervals.get(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 pruneIntervalList(shifts, hiddenColumns);
1345 if (hiddenColumns != null && hiddenColumns.size() == 0)
1347 hiddenColumns = null;
1352 LOCK.writeLock().unlock();
1357 * Add gaps into the sequences aligned to profileseq under the given
1362 * - alignment to have gaps inserted into it
1364 * - alignment view where sequence corresponding to profileseq is
1366 * @return new HiddenColumns for new alignment view, with insertions into
1367 * profileseq marked as hidden.
1369 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1370 AlignmentI al, AlignmentView input)
1374 char gc = al.getGapCharacter();
1375 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1376 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1377 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1378 nview.propagateInsertions(profileseq, al, origseq);
1385 * - sequence in al which corresponds to origseq
1387 * - alignment which is to have gaps inserted into it
1389 * - sequence corresponding to profileseq which defines gap map for
1392 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1395 char gc = al.getGapCharacter();
1396 // recover mapping between sequence's non-gap positions and positions
1398 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1399 int[] viscontigs = getVisibleContigs(0, profileseq.getLength());
1403 // add profile to visible contigs
1404 for (int v = 0; v < viscontigs.length; v += 2)
1406 if (viscontigs[v] > spos)
1408 StringBuffer sb = new StringBuffer();
1409 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1413 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1415 SequenceI sqobj = al.getSequenceAt(s);
1416 if (sqobj != profileseq)
1418 String sq = al.getSequenceAt(s).getSequenceAsString();
1419 if (sq.length() <= spos + offset)
1422 int diff = spos + offset - sq.length() - 1;
1427 while ((diff = spos + offset - sq.length() - 1) > 0)
1430 // + ((diff >= sb.length()) ? sb.toString() : sb
1431 // .substring(0, diff));
1432 if (diff >= sb.length())
1434 sq += sb.toString();
1438 char[] buf = new char[diff];
1439 sb.getChars(0, diff, buf, 0);
1440 sq += buf.toString();
1444 sq += sb.toString();
1448 al.getSequenceAt(s).setSequence(sq.substring(0, spos + offset)
1449 + sb.toString() + sq.substring(spos + offset));
1453 // offset+=sb.length();
1455 spos = viscontigs[v + 1] + 1;
1457 if ((offset + spos) < profileseq.getLength())
1459 // pad the final region with gaps.
1460 StringBuffer sb = new StringBuffer();
1461 for (int s = 0, ns = profileseq.getLength() - spos
1462 - offset; s < ns; s++)
1466 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1468 SequenceI sqobj = al.getSequenceAt(s);
1469 if (sqobj == profileseq)
1473 String sq = sqobj.getSequenceAsString();
1475 int diff = origseq.getLength() - sq.length();
1479 // + ((diff >= sb.length()) ? sb.toString() : sb
1480 // .substring(0, diff));
1481 if (diff >= sb.length())
1483 sq += sb.toString();
1487 char[] buf = new char[diff];
1488 sb.getChars(0, diff, buf, 0);
1489 sq += buf.toString();
1491 diff = origseq.getLength() - sq.length();
1498 * remove any hiddenColumns or selected columns and shift remaining based on a
1499 * series of position, range deletions.
1503 private void pruneDeletions(ShiftList deletions)
1505 if (deletions != null)
1507 final List<int[]> shifts = deletions.getShifts();
1508 if (shifts != null && shifts.size() > 0)
1510 pruneDeletions(shifts);
1512 // and shift the rest.
1513 this.compensateForEdits(deletions);
1519 * Adjust hidden column boundaries based on a series of column additions or
1520 * deletions in visible regions.
1522 * @param shiftrecord
1525 private ShiftList compensateForEdits(ShiftList shiftrecord)
1527 if (shiftrecord != null)
1529 final List<int[]> shifts = shiftrecord.getShifts();
1530 if (shifts != null && shifts.size() > 0)
1533 for (int i = 0, j = shifts.size(); i < j; i++)
1535 int[] sh = shifts.get(i);
1536 compensateForDelEdits(shifted + sh[0], sh[1]);
1540 return shiftrecord.getInverse();
1546 * Returns a hashCode built from hidden column ranges
1549 public int hashCode()
1553 LOCK.readLock().lock();
1555 if (hiddenColumns != null)
1557 for (int[] hidden : hiddenColumns)
1559 hashCode = 31 * hashCode + hidden[0];
1560 hashCode = 31 * hashCode + hidden[1];
1566 LOCK.readLock().unlock();
1571 * Hide columns corresponding to the marked bits
1574 * - columns map to bits starting from zero
1576 public void hideMarkedBits(BitSet inserts)
1580 LOCK.writeLock().lock();
1581 for (int firstSet = inserts
1582 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1583 .nextSetBit(lastSet))
1585 lastSet = inserts.nextClearBit(firstSet);
1586 hideColumns(firstSet, lastSet - 1);
1590 LOCK.writeLock().unlock();
1597 * BitSet where hidden columns will be marked
1599 public void markHiddenRegions(BitSet inserts)
1603 LOCK.readLock().lock();
1604 if (hiddenColumns == null)
1608 for (int[] range : hiddenColumns)
1610 inserts.set(range[0], range[1] + 1);
1614 LOCK.readLock().unlock();
1619 * Calculate the visible start and end index of an alignment.
1622 * full alignment width
1623 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1625 public int[] getVisibleStartAndEndIndex(int width)
1629 LOCK.readLock().lock();
1630 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1631 int startPos = alignmentStartEnd[0];
1632 int endPos = alignmentStartEnd[1];
1634 int[] lowestRange = new int[] { -1, -1 };
1635 int[] higestRange = new int[] { -1, -1 };
1637 if (hiddenColumns == null)
1639 return new int[] { startPos, endPos };
1642 for (int[] hiddenCol : hiddenColumns)
1644 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1645 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1648 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1650 startPos = alignmentStartEnd[0];
1654 startPos = lowestRange[1] + 1;
1657 if (higestRange[0] == -1 && higestRange[1] == -1)
1659 endPos = alignmentStartEnd[1];
1663 endPos = higestRange[0] - 1;
1665 return new int[] { startPos, endPos };
1668 LOCK.readLock().unlock();
1674 * Finds the hidden region (if any) which starts or ends at res
1677 * visible residue position, unadjusted for hidden columns
1678 * @return region as [start,end] or null if no matching region is found
1680 public int[] getRegionWithEdgeAtRes(int res)
1684 LOCK.readLock().lock();
1685 int adjres = adjustForHiddenColumns(res);
1687 int[] reveal = null;
1688 if (hiddenColumns != null)
1690 for (int[] region : hiddenColumns)
1692 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1702 LOCK.readLock().unlock();