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, ilpos, fpos, lpos, 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),
986 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
988 // otherwise, sequence was completely hidden
989 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
992 LOCK.readLock().unlock();
997 * delete any columns in alignmentAnnotation that are hidden (including
998 * sequence associated annotation).
1000 * @param alignmentAnnotation
1002 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1004 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1008 * delete any columns in alignmentAnnotation that are hidden (including
1009 * sequence associated annotation).
1012 * remove any annotation to the right of this column
1014 * remove any annotation to the left of this column
1015 * @param alignmentAnnotation
1016 * the annotation to operate on
1018 public void makeVisibleAnnotation(int start, int end,
1019 AlignmentAnnotation alignmentAnnotation)
1023 LOCK.readLock().lock();
1024 if (alignmentAnnotation.annotations == null)
1028 if (start == end && end == -1)
1031 end = alignmentAnnotation.annotations.length;
1033 if (hiddenColumns != null && hiddenColumns.size() > 0)
1035 // then mangle the alignmentAnnotation annotation array
1036 Vector<Annotation[]> annels = new Vector<>();
1037 Annotation[] els = null;
1038 List<int[]> regions = getHiddenRegions();
1039 int blockStart = start;
1046 for (int j = 0; j < regions.size(); j++)
1048 region = regions.get(j);
1049 hideStart = region[0];
1050 hideEnd = region[1];
1052 if (hideStart < start)
1057 blockStart = Math.min(blockStart, hideEnd + 1);
1058 blockEnd = Math.min(blockEnd, hideStart);
1060 if (blockStart > blockEnd)
1065 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1066 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1069 blockStart = hideEnd + 1;
1073 if (end > blockStart)
1075 annels.addElement(els = new Annotation[end - blockStart + 1]);
1077 + blockStart) <= alignmentAnnotation.annotations.length)
1079 // copy just the visible segment of the annotation row
1080 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1081 els, 0, els.length);
1085 // copy to the end of the annotation row
1086 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1088 (alignmentAnnotation.annotations.length - blockStart));
1097 alignmentAnnotation.annotations = new Annotation[w];
1100 for (Annotation[] chnk : annels)
1102 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1109 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]);
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.get(i);
1184 for (int j = region[0]; j < region[1] + 1; j++)
1191 hiddenColumns = null;
1194 LOCK.writeLock().unlock();
1199 * Reveals, and marks as selected, the hidden column range with the given
1204 public void revealHiddenColumns(int start, ColumnSelection sel)
1208 LOCK.writeLock().lock();
1209 for (int i = 0; i < hiddenColumns.size(); i++)
1211 int[] region = hiddenColumns.get(i);
1212 if (start == region[0])
1214 for (int j = region[0]; j < region[1] + 1; j++)
1219 hiddenColumns.remove(region);
1223 if (hiddenColumns.size() == 0)
1225 hiddenColumns = null;
1229 LOCK.writeLock().unlock();
1234 * removes intersection of position,length ranges in deletions from the
1235 * start,end regions marked in intervals.
1241 private boolean pruneIntervalList(final List<int[]> shifts,
1242 ArrayList<int[]> intervals)
1244 boolean pruned = false;
1246 int j = intervals.size() - 1;
1248 int t = shifts.size() - 1;
1249 int[] hr = intervals.get(i);
1250 int[] sr = shifts.get(s);
1251 while (i <= j && s <= t)
1253 boolean trailinghn = hr[1] >= sr[0];
1258 hr = intervals.get(++i);
1266 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
1267 if (endshift < hr[0] || endshift < sr[0])
1268 { // leadinghc disjoint or not a deletion
1271 sr = shifts.get(++s);
1279 boolean leadinghn = hr[0] >= sr[0];
1280 boolean leadinghc = hr[0] < endshift;
1281 boolean trailinghc = hr[1] < endshift;
1285 { // deleted hidden region.
1286 intervals.remove(i);
1291 hr = intervals.get(i);
1297 hr[0] = endshift; // clip c terminal region
1298 leadinghn = !leadinghn;
1314 // sr contained in hr
1317 sr = shifts.get(++s);
1327 return pruned; // true if any interval was removed or modified by
1332 * remove any hiddenColumns or selected columns and shift remaining based on a
1333 * series of position, range deletions.
1337 public void pruneDeletions(List<int[]> shifts)
1341 LOCK.writeLock().lock();
1342 // delete any intervals intersecting.
1343 if (hiddenColumns != null)
1345 pruneIntervalList(shifts, hiddenColumns);
1346 if (hiddenColumns != null && hiddenColumns.size() == 0)
1348 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 = getVisibleContigs(0, profileseq.getLength());
1404 // add profile to visible contigs
1405 for (int v = 0; v < viscontigs.length; v += 2)
1407 if (viscontigs[v] > spos)
1409 StringBuffer sb = new StringBuffer();
1410 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1414 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1416 SequenceI sqobj = al.getSequenceAt(s);
1417 if (sqobj != profileseq)
1419 String sq = al.getSequenceAt(s).getSequenceAsString();
1420 if (sq.length() <= spos + offset)
1423 int diff = spos + offset - sq.length() - 1;
1428 while ((diff = spos + offset - sq.length() - 1) > 0)
1431 // + ((diff >= sb.length()) ? sb.toString() : sb
1432 // .substring(0, diff));
1433 if (diff >= sb.length())
1435 sq += sb.toString();
1439 char[] buf = new char[diff];
1440 sb.getChars(0, diff, buf, 0);
1441 sq += buf.toString();
1445 sq += sb.toString();
1449 al.getSequenceAt(s).setSequence(sq.substring(0, spos + offset)
1450 + sb.toString() + sq.substring(spos + offset));
1454 // offset+=sb.length();
1456 spos = viscontigs[v + 1] + 1;
1458 if ((offset + spos) < profileseq.getLength())
1460 // pad the final region with gaps.
1461 StringBuffer sb = new StringBuffer();
1462 for (int s = 0, ns = profileseq.getLength() - spos
1463 - offset; s < ns; s++)
1467 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1469 SequenceI sqobj = al.getSequenceAt(s);
1470 if (sqobj == profileseq)
1474 String sq = sqobj.getSequenceAsString();
1476 int diff = origseq.getLength() - sq.length();
1480 // + ((diff >= sb.length()) ? sb.toString() : sb
1481 // .substring(0, diff));
1482 if (diff >= sb.length())
1484 sq += sb.toString();
1488 char[] buf = new char[diff];
1489 sb.getChars(0, diff, buf, 0);
1490 sq += buf.toString();
1492 diff = origseq.getLength() - sq.length();
1499 * remove any hiddenColumns or selected columns and shift remaining based on a
1500 * series of position, range deletions.
1504 private void pruneDeletions(ShiftList deletions)
1506 if (deletions != null)
1508 final List<int[]> shifts = deletions.getShifts();
1509 if (shifts != null && shifts.size() > 0)
1511 pruneDeletions(shifts);
1513 // and shift the rest.
1514 this.compensateForEdits(deletions);
1520 * Adjust hidden column boundaries based on a series of column additions or
1521 * deletions in visible regions.
1523 * @param shiftrecord
1526 private ShiftList compensateForEdits(ShiftList shiftrecord)
1528 if (shiftrecord != null)
1530 final List<int[]> shifts = shiftrecord.getShifts();
1531 if (shifts != null && shifts.size() > 0)
1534 for (int i = 0, j = shifts.size(); i < j; i++)
1536 int[] sh = shifts.get(i);
1537 compensateForDelEdits(shifted + sh[0], sh[1]);
1541 return shiftrecord.getInverse();
1547 * Returns a hashCode built from hidden column ranges
1550 public int hashCode()
1554 LOCK.readLock().lock();
1556 if (hiddenColumns != null)
1558 for (int[] hidden : hiddenColumns)
1560 hashCode = 31 * hashCode + hidden[0];
1561 hashCode = 31 * hashCode + hidden[1];
1567 LOCK.readLock().unlock();
1572 * Hide columns corresponding to the marked bits
1575 * - columns map to bits starting from zero
1577 public void hideMarkedBits(BitSet inserts)
1581 LOCK.writeLock().lock();
1582 for (int firstSet = inserts
1583 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1584 .nextSetBit(lastSet))
1586 lastSet = inserts.nextClearBit(firstSet);
1587 hideColumns(firstSet, lastSet - 1);
1591 LOCK.writeLock().unlock();
1598 * BitSet where hidden columns will be marked
1600 public void markHiddenRegions(BitSet inserts)
1604 LOCK.readLock().lock();
1605 if (hiddenColumns == null)
1609 for (int[] range : hiddenColumns)
1611 inserts.set(range[0], range[1] + 1);
1615 LOCK.readLock().unlock();
1620 * Calculate the visible start and end index of an alignment.
1623 * full alignment width
1624 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1626 public int[] getVisibleStartAndEndIndex(int width)
1630 LOCK.readLock().lock();
1631 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1632 int startPos = alignmentStartEnd[0];
1633 int endPos = alignmentStartEnd[1];
1635 int[] lowestRange = new int[] { -1, -1 };
1636 int[] higestRange = new int[] { -1, -1 };
1638 if (hiddenColumns == null)
1640 return new int[] { startPos, endPos };
1643 for (int[] hiddenCol : hiddenColumns)
1645 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1646 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1649 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1651 startPos = alignmentStartEnd[0];
1655 startPos = lowestRange[1] + 1;
1658 if (higestRange[0] == -1 && higestRange[1] == -1)
1660 endPos = alignmentStartEnd[1];
1664 endPos = higestRange[0] - 1;
1666 return new int[] { startPos, endPos };
1669 LOCK.readLock().unlock();
1675 * Finds the hidden region (if any) which starts or ends at res
1678 * visible residue position, unadjusted for hidden columns
1679 * @return region as [start,end] or null if no matching region is found
1681 public int[] getRegionWithEdgeAtRes(int res)
1685 LOCK.readLock().lock();
1686 int adjres = adjustForHiddenColumns(res);
1688 int[] reveal = null;
1689 if (hiddenColumns != null)
1691 for (int[] region : hiddenColumns)
1693 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1703 LOCK.readLock().unlock();