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 Vector<int[]> hiddenColumns;
44 * This Method is used to return all the HiddenColumn regions
46 * @return empty list or List of hidden column intervals
48 public List<int[]> getHiddenRegions()
50 return hiddenColumns == null ? Collections.<int[]> emptyList()
55 * Output regions data as a string. String is in the format:
56 * reg0[0]<between>reg0[1]<delimiter>reg1[0]<between>reg1[1] ... regn[1]
59 * string to delimit regions
60 * @param betweenstring
61 * to put between start and end region values
62 * @return regions formatted according to delimiter and between strings
64 public String regionsToString(String delimiter, String between)
68 LOCK.readLock().lock();
69 StringBuilder regionBuilder = new StringBuilder();
70 for (int[] range : hiddenColumns)
72 regionBuilder.append(delimiter).append(range[0]).append(between)
76 regionBuilder.deleteCharAt(0);
77 return regionBuilder.toString();
80 LOCK.readLock().unlock();
85 * Find the number of hidden columns
87 * @return number of hidden columns
93 LOCK.readLock().lock();
97 for (int[] range : hiddenColumns)
99 size += range[1] - range[0] + 1;
106 LOCK.readLock().unlock();
111 * Answers if there are any hidden columns
113 * @return true if there are hidden columns
115 public boolean hasHidden()
119 LOCK.readLock().lock();
120 return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
123 LOCK.readLock().unlock();
129 public boolean equals(Object obj)
133 LOCK.readLock().lock();
135 if (!(obj instanceof HiddenColumns))
139 HiddenColumns that = (HiddenColumns) obj;
142 * check hidden columns are either both null, or match
144 if (this.hiddenColumns == null)
146 return (that.hiddenColumns == null);
148 if (that.hiddenColumns == null
149 || that.hiddenColumns.size() != this.hiddenColumns.size())
154 for (int[] thisRange : hiddenColumns)
156 int[] thatRange = that.hiddenColumns.get(i++);
157 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
165 LOCK.readLock().unlock();
170 * Return absolute column index for a visible column index
173 * int column index in alignment view (count from zero)
174 * @return alignment column index for column
176 public int adjustForHiddenColumns(int column)
180 LOCK.readLock().lock();
182 if (hiddenColumns != null)
184 for (int i = 0; i < hiddenColumns.size(); i++)
186 int[] region = hiddenColumns.elementAt(i);
187 if (result >= region[0])
189 result += region[1] - region[0] + 1;
196 LOCK.readLock().unlock();
201 * Use this method to find out where a column will appear in the visible
202 * alignment when hidden columns exist. If the column is not visible, then the
203 * left-most visible column will always be returned.
205 * @param hiddenColumn
206 * the column index in the full alignment including hidden columns
207 * @return the position of the column in the visible alignment
209 public int findColumnPosition(int hiddenColumn)
213 LOCK.readLock().lock();
214 int result = hiddenColumn;
215 if (hiddenColumns != null)
221 region = hiddenColumns.elementAt(index++);
222 if (hiddenColumn > region[1])
224 result -= region[1] + 1 - region[0];
226 } while ((hiddenColumn > region[1])
227 && (index < hiddenColumns.size()));
229 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
231 // Here the hidden column is within a region, so
232 // we want to return the position of region[0]-1, adjusted for any
233 // earlier hidden columns.
234 // Calculate the difference between the actual hidden col position
235 // and region[0]-1, and then subtract from result to convert result
237 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
239 // However, if the region begins at 0 we cannot return region[0]-1
247 return result - (hiddenColumn - region[0] + 1);
251 return result; // return the shifted position after removing hidden
255 LOCK.readLock().unlock();
260 * Find the visible column which is a given visible number of columns to the
261 * left of another visible column. i.e. for a startColumn x, the column which
262 * is distance 1 away will be column x-1.
264 * @param visibleDistance
265 * the number of visible columns to offset by
267 * the column to start from
268 * @return the position of the column in the visible alignment
270 public int subtractVisibleColumns(int visibleDistance, int startColumn)
275 LOCK.readLock().lock();
276 int distance = visibleDistance;
278 // in case startColumn is in a hidden region, move it to the left
279 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
281 // get index of hidden region to left of start
282 int index = getHiddenIndexLeft(start);
285 // no hidden regions to left of startColumn
286 return start - distance;
289 // walk backwards through the alignment subtracting the counts of visible
290 // columns from distance
293 int nextstart = start;
295 while ((index > -1) && (distance - gap > 0))
297 // subtract the gap to right of region from distance
301 // calculate the next gap
302 region = hiddenColumns.get(index);
303 gap = start - region[1];
305 // set start to just to left of current region
306 nextstart = region[0] - 1;
310 if (distance - gap > 0)
312 // fell out of loop because there are no more hidden regions
314 return nextstart - distance;
316 return start - distance;
319 LOCK.readLock().unlock();
325 * Use this method to determine the set of hiddenRegion start positions
327 * @return list of column number in visible view where hidden regions start
329 public List<Integer> findHiddenRegionPositions()
333 LOCK.readLock().lock();
334 List<Integer> positions = new ArrayList<>(
335 hiddenColumns.size());
337 positions.add(hiddenColumns.elementAt(0)[0]);
338 for (int i = 1; i < hiddenColumns.size(); ++i)
342 if (hiddenColumns != null)
348 int[] region = hiddenColumns.elementAt(index);
349 gaps += region[1] + 1 - region[0];
350 result = region[1] + 1;
352 } while (index <= i);
356 positions.add(result);
363 LOCK.readLock().unlock();
368 * This method returns the rightmost limit of a region of an alignment with
369 * hidden columns. In otherwords, the next hidden column.
374 public int getHiddenBoundaryRight(int alPos)
378 LOCK.readLock().lock();
379 if (hiddenColumns != null)
384 int[] region = hiddenColumns.elementAt(index);
385 if (alPos < region[0])
391 } while (index < hiddenColumns.size());
397 LOCK.readLock().unlock();
403 * This method returns the leftmost limit of a region of an alignment with
404 * hidden columns. In otherwords, the previous hidden column.
409 public int getHiddenBoundaryLeft(int alPos)
413 LOCK.readLock().lock();
415 if (hiddenColumns != null)
417 int index = hiddenColumns.size() - 1;
420 int[] region = hiddenColumns.elementAt(index);
421 if (alPos > region[1])
427 } while (index > -1);
433 LOCK.readLock().unlock();
438 * This method returns the index of the hidden region to the left of a column
439 * position. If the column is in a hidden region it returns the index of the
440 * region to the left. If there is no hidden region to the left it returns -1.
445 private int getHiddenIndexLeft(int pos)
450 LOCK.readLock().lock();
451 if (hiddenColumns != null)
453 int index = hiddenColumns.size() - 1;
456 int[] region = hiddenColumns.elementAt(index);
463 } while (index > -1);
469 LOCK.readLock().unlock();
475 * Adds the specified column range to the hidden columns
480 public void hideColumns(int start, int end)
482 hideColumns(start, end, false);
486 * Adds the specified column range to the hidden columns
491 private void hideColumns(int start, int end, boolean alreadyLocked)
498 LOCK.writeLock().lock();
501 if (hiddenColumns == null)
503 hiddenColumns = new Vector<>();
507 * traverse existing hidden ranges and insert / amend / append as
510 for (int i = 0; i < hiddenColumns.size(); i++)
512 int[] region = hiddenColumns.elementAt(i);
514 if (end < region[0] - 1)
517 * insert discontiguous preceding range
519 hiddenColumns.insertElementAt(new int[] { start, end }, i);
523 if (end <= region[1])
526 * new range overlaps existing, or is contiguous preceding it - adjust
529 region[0] = Math.min(region[0], start);
533 if (start <= region[1] + 1)
536 * new range overlaps existing, or is contiguous following it - adjust
537 * start and end columns
539 region[0] = Math.min(region[0], start);
540 region[1] = Math.max(region[1], end);
543 * also update or remove any subsequent ranges
544 * that are overlapped
546 while (i < hiddenColumns.size() - 1)
548 int[] nextRegion = hiddenColumns.get(i + 1);
549 if (nextRegion[0] > end + 1)
552 * gap to next hidden range - no more to update
556 region[1] = Math.max(nextRegion[1], end);
557 hiddenColumns.remove(i + 1);
564 * remaining case is that the new range follows everything else
566 hiddenColumns.addElement(new int[] { start, end });
571 LOCK.writeLock().unlock();
576 public boolean isVisible(int column)
580 LOCK.readLock().lock();
582 if (hiddenColumns != null)
584 for (int[] region : hiddenColumns)
586 if (column >= region[0] && column <= region[1])
596 LOCK.readLock().unlock();
603 public HiddenColumns()
612 public HiddenColumns(HiddenColumns copy)
617 LOCK.readLock().lock();
620 if (copy.hiddenColumns != null)
622 hiddenColumns = copy.copyHiddenRegions();
628 LOCK.readLock().unlock();
632 private Vector<int[]> copyHiddenRegions()
634 Vector<int[]> copy = new Vector<>(hiddenColumns.size());
635 for (int i = 0, j = hiddenColumns.size(); i < j; i++)
639 rh = hiddenColumns.elementAt(i);
642 cp = new int[rh.length];
643 System.arraycopy(rh, 0, cp, 0, rh.length);
650 private ArrayList<int[]> copyHiddenRegionsToArrayList()
652 ArrayList<int[]> copy = new ArrayList<>(hiddenColumns.size());
653 for (int i = 0, j = hiddenColumns.size(); i < j; i++)
657 rh = hiddenColumns.elementAt(i);
660 cp = new int[rh.length];
661 System.arraycopy(rh, 0, cp, 0, rh.length);
668 public Vector<int[]> getHiddenColumnsCopy()
672 LOCK.readLock().lock();
673 return copyHiddenRegions();
676 LOCK.readLock().unlock();
680 public ArrayList<int[]> getHiddenColumnsCopyAsList()
684 LOCK.readLock().lock();
685 return copyHiddenRegionsToArrayList();
688 LOCK.readLock().unlock();
693 * propagate shift in alignment columns to column selection
698 * shift in edit (+ve for removal, or -ve for inserts)
700 public List<int[]> compensateForEdit(int start, int change,
705 LOCK.writeLock().lock();
706 List<int[]> deletedHiddenColumns = null;
708 if (hiddenColumns != null)
710 deletedHiddenColumns = new ArrayList<>();
711 int hSize = hiddenColumns.size();
712 for (int i = 0; i < hSize; i++)
714 int[] region = hiddenColumns.elementAt(i);
715 if (region[0] > start && start + change > region[1])
717 deletedHiddenColumns.add(region);
719 hiddenColumns.removeElementAt(i);
725 if (region[0] > start)
738 this.revealHiddenColumns(0, sel);
741 return deletedHiddenColumns;
744 LOCK.writeLock().unlock();
749 * propagate shift in alignment columns to column selection special version of
750 * compensateForEdit - allowing for edits within hidden regions
755 * shift in edit (+ve for removal, or -ve for inserts)
757 public void compensateForDelEdits(int start, int change)
761 LOCK.writeLock().lock();
762 if (hiddenColumns != null)
764 for (int i = 0; i < hiddenColumns.size(); i++)
766 int[] region = hiddenColumns.elementAt(i);
767 if (region[0] >= start)
771 if (region[1] >= start)
775 if (region[1] < region[0])
777 hiddenColumns.removeElementAt(i--);
793 LOCK.writeLock().unlock();
798 * return all visible segments between the given start and end boundaries
801 * (first column inclusive from 0)
803 * (last column - not inclusive)
804 * @return int[] {i_start, i_end, ..} where intervals lie in
805 * start<=i_start<=i_end<end
807 public int[] getVisibleContigs(int start, int end)
811 LOCK.readLock().lock();
812 if (hiddenColumns != null && hiddenColumns.size() > 0)
814 List<int[]> visiblecontigs = new ArrayList<>();
815 List<int[]> regions = getHiddenRegions();
822 for (int j = 0; vstart < end && j < regions.size(); j++)
824 region = regions.get(j);
825 hideStart = region[0];
828 if (hideEnd < vstart)
832 if (hideStart > vstart)
834 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
836 vstart = hideEnd + 1;
841 visiblecontigs.add(new int[] { vstart, end - 1 });
843 int[] vcontigs = new int[visiblecontigs.size() * 2];
844 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
846 int[] vc = visiblecontigs.get(i);
847 visiblecontigs.set(i, null);
848 vcontigs[i * 2] = vc[0];
849 vcontigs[i * 2 + 1] = vc[1];
851 visiblecontigs.clear();
856 return new int[] { start, end - 1 };
861 LOCK.readLock().unlock();
865 public String[] getVisibleSequenceStrings(int start, int end,
870 LOCK.readLock().lock();
871 int iSize = seqs.length;
872 String selections[] = new String[iSize];
873 if (hiddenColumns != null && hiddenColumns.size() > 0)
875 for (int i = 0; i < iSize; i++)
877 StringBuffer visibleSeq = new StringBuffer();
878 List<int[]> regions = getHiddenRegions();
880 int blockStart = start;
886 for (int j = 0; j < regions.size(); j++)
888 region = regions.get(j);
889 hideStart = region[0];
892 if (hideStart < start)
897 blockStart = Math.min(blockStart, hideEnd + 1);
898 blockEnd = Math.min(blockEnd, hideStart);
900 if (blockStart > blockEnd)
905 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
907 blockStart = hideEnd + 1;
911 if (end > blockStart)
913 visibleSeq.append(seqs[i].getSequence(blockStart, end));
916 selections[i] = visibleSeq.toString();
921 for (int i = 0; i < iSize; i++)
923 selections[i] = seqs[i].getSequenceAsString(start, end);
931 LOCK.readLock().unlock();
936 * Locate the first and last position visible for this sequence. if seq isn't
937 * visible then return the position of the left and right of the hidden
938 * boundary region, and the corresponding alignment column indices for the
939 * extent of the sequence
942 * @return int[] { visible start, visible end, first seqpos, last seqpos,
943 * alignment index for seq start, alignment index for seq end }
945 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
949 LOCK.readLock().lock();
950 int fpos = seq.getStart();
951 int lpos = seq.getEnd();
954 if (hiddenColumns == null || hiddenColumns.size() == 0)
956 int ifpos = seq.findIndex(fpos) - 1;
957 int ilpos = seq.findIndex(lpos) - 1;
958 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
961 // Simply walk along the sequence whilst watching for hidden column
963 List<int[]> regions = getHiddenRegions();
967 int hideStart = seq.getLength();
973 boolean foundStart = false;
974 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
977 if (!Comparison.isGap(seq.getCharAt(p)))
979 // keep track of first/last column
980 // containing sequence data regardless of visibility
986 // update hidden region start/end
987 while (hideEnd < p && rcount < regions.size())
989 int[] region = regions.get(rcount++);
991 visNext += region[0] - visPrev;
992 hideStart = region[0];
997 hideStart = seq.getLength();
999 // update visible boundary for sequence
1011 // look for next sequence position
1017 return new int[] { findColumnPosition(start),
1018 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
1020 // otherwise, sequence was completely hidden
1021 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
1025 LOCK.readLock().unlock();
1030 * delete any columns in alignmentAnnotation that are hidden (including
1031 * sequence associated annotation).
1033 * @param alignmentAnnotation
1035 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1037 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1041 * delete any columns in alignmentAnnotation that are hidden (including
1042 * sequence associated annotation).
1045 * remove any annotation to the right of this column
1047 * remove any annotation to the left of this column
1048 * @param alignmentAnnotation
1049 * the annotation to operate on
1051 public void makeVisibleAnnotation(int start, int end,
1052 AlignmentAnnotation alignmentAnnotation)
1056 LOCK.readLock().lock();
1057 if (alignmentAnnotation.annotations == null)
1061 if (start == end && end == -1)
1064 end = alignmentAnnotation.annotations.length;
1066 if (hiddenColumns != null && hiddenColumns.size() > 0)
1068 // then mangle the alignmentAnnotation annotation array
1069 Vector<Annotation[]> annels = new Vector<>();
1070 Annotation[] els = null;
1071 List<int[]> regions = getHiddenRegions();
1072 int blockStart = start;
1079 for (int j = 0; j < regions.size(); j++)
1081 region = regions.get(j);
1082 hideStart = region[0];
1083 hideEnd = region[1];
1085 if (hideStart < start)
1090 blockStart = Math.min(blockStart, hideEnd + 1);
1091 blockEnd = Math.min(blockEnd, hideStart);
1093 if (blockStart > blockEnd)
1098 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1099 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1102 blockStart = hideEnd + 1;
1106 if (end > blockStart)
1108 annels.addElement(els = new Annotation[end - blockStart + 1]);
1110 + blockStart) <= alignmentAnnotation.annotations.length)
1112 // copy just the visible segment of the annotation row
1113 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1114 els, 0, els.length);
1118 // copy to the end of the annotation row
1119 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1121 (alignmentAnnotation.annotations.length - blockStart));
1130 alignmentAnnotation.annotations = new Annotation[w];
1133 for (Annotation[] chnk : annels)
1135 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1142 alignmentAnnotation.restrict(start, end);
1147 LOCK.readLock().unlock();
1153 * @return true if there are columns hidden
1155 public boolean hasHiddenColumns()
1159 LOCK.readLock().lock();
1160 return hiddenColumns != null && hiddenColumns.size() > 0;
1163 LOCK.readLock().unlock();
1169 * @return true if there are more than one set of columns hidden
1171 public boolean hasManyHiddenColumns()
1175 LOCK.readLock().lock();
1176 return hiddenColumns != null && hiddenColumns.size() > 1;
1179 LOCK.readLock().unlock();
1184 * mark the columns corresponding to gap characters as hidden in the column
1189 public void hideInsertionsFor(SequenceI sr)
1193 LOCK.writeLock().lock();
1194 List<int[]> inserts = sr.getInsertions();
1195 for (int[] r : inserts)
1197 hideColumns(r[0], r[1], true);
1201 LOCK.writeLock().unlock();
1206 * Unhides, and adds to the selection list, all hidden columns
1208 public void revealAllHiddenColumns(ColumnSelection sel)
1212 LOCK.writeLock().lock();
1213 if (hiddenColumns != null)
1215 for (int i = 0; i < hiddenColumns.size(); i++)
1217 int[] region = hiddenColumns.elementAt(i);
1218 for (int j = region[0]; j < region[1] + 1; j++)
1225 hiddenColumns = null;
1229 LOCK.writeLock().unlock();
1234 * Reveals, and marks as selected, the hidden column range with the given
1239 public void revealHiddenColumns(int start, ColumnSelection sel)
1243 LOCK.writeLock().lock();
1244 for (int i = 0; i < hiddenColumns.size(); i++)
1246 int[] region = hiddenColumns.elementAt(i);
1247 if (start == region[0])
1249 for (int j = region[0]; j < region[1] + 1; j++)
1254 hiddenColumns.removeElement(region);
1258 if (hiddenColumns.size() == 0)
1260 hiddenColumns = null;
1265 LOCK.writeLock().unlock();
1270 * removes intersection of position,length ranges in deletions from the
1271 * start,end regions marked in intervals.
1277 private boolean pruneIntervalVector(final List<int[]> shifts,
1278 Vector<int[]> intervals)
1280 boolean pruned = false;
1282 int j = intervals.size() - 1;
1284 int t = shifts.size() - 1;
1285 int hr[] = intervals.elementAt(i);
1286 int sr[] = shifts.get(s);
1287 while (i <= j && s <= t)
1289 boolean trailinghn = hr[1] >= sr[0];
1294 hr = intervals.elementAt(++i);
1302 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
1303 if (endshift < hr[0] || endshift < sr[0])
1304 { // leadinghc disjoint or not a deletion
1307 sr = shifts.get(++s);
1315 boolean leadinghn = hr[0] >= sr[0];
1316 boolean leadinghc = hr[0] < endshift;
1317 boolean trailinghc = hr[1] < endshift;
1321 { // deleted hidden region.
1322 intervals.removeElementAt(i);
1327 hr = intervals.elementAt(i);
1333 hr[0] = endshift; // clip c terminal region
1334 leadinghn = !leadinghn;
1350 // sr contained in hr
1353 sr = shifts.get(++s);
1363 return pruned; // true if any interval was removed or modified by
1368 * remove any hiddenColumns or selected columns and shift remaining based on a
1369 * series of position, range deletions.
1373 public void pruneDeletions(List<int[]> shifts)
1377 LOCK.writeLock().lock();
1378 // delete any intervals intersecting.
1379 if (hiddenColumns != null)
1381 pruneIntervalVector(shifts, hiddenColumns);
1382 if (hiddenColumns != null && hiddenColumns.size() == 0)
1384 hiddenColumns = null;
1390 LOCK.writeLock().unlock();
1395 * Add gaps into the sequences aligned to profileseq under the given
1400 * - alignment to have gaps inserted into it
1402 * - alignment view where sequence corresponding to profileseq is
1404 * @return new HiddenColumns for new alignment view, with insertions into
1405 * profileseq marked as hidden.
1407 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1408 AlignmentI al, AlignmentView input)
1412 char gc = al.getGapCharacter();
1413 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1414 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1415 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1416 nview.propagateInsertions(profileseq, al, origseq);
1423 * - sequence in al which corresponds to origseq
1425 * - alignment which is to have gaps inserted into it
1427 * - sequence corresponding to profileseq which defines gap map for
1430 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1433 char gc = al.getGapCharacter();
1434 // recover mapping between sequence's non-gap positions and positions
1436 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1437 int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
1438 profileseq.getLength());
1442 // add profile to visible contigs
1443 for (int v = 0; v < viscontigs.length; v += 2)
1445 if (viscontigs[v] > spos)
1447 StringBuffer sb = new StringBuffer();
1448 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1452 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1454 SequenceI sqobj = al.getSequenceAt(s);
1455 if (sqobj != profileseq)
1457 String sq = al.getSequenceAt(s).getSequenceAsString();
1458 if (sq.length() <= spos + offset)
1461 int diff = spos + offset - sq.length() - 1;
1466 while ((diff = spos + offset - sq.length() - 1) > 0)
1469 // + ((diff >= sb.length()) ? sb.toString() : sb
1470 // .substring(0, diff));
1471 if (diff >= sb.length())
1473 sq += sb.toString();
1477 char[] buf = new char[diff];
1478 sb.getChars(0, diff, buf, 0);
1479 sq += buf.toString();
1483 sq += sb.toString();
1487 al.getSequenceAt(s).setSequence(
1488 sq.substring(0, spos + offset) + sb.toString()
1489 + sq.substring(spos + offset));
1493 // offset+=sb.length();
1495 spos = viscontigs[v + 1] + 1;
1497 if ((offset + spos) < profileseq.getLength())
1499 // pad the final region with gaps.
1500 StringBuffer sb = new StringBuffer();
1501 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1505 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1507 SequenceI sqobj = al.getSequenceAt(s);
1508 if (sqobj == profileseq)
1512 String sq = sqobj.getSequenceAsString();
1514 int diff = origseq.getLength() - sq.length();
1518 // + ((diff >= sb.length()) ? sb.toString() : sb
1519 // .substring(0, diff));
1520 if (diff >= sb.length())
1522 sq += sb.toString();
1526 char[] buf = new char[diff];
1527 sb.getChars(0, diff, buf, 0);
1528 sq += buf.toString();
1530 diff = origseq.getLength() - sq.length();
1537 * remove any hiddenColumns or selected columns and shift remaining based on a
1538 * series of position, range deletions.
1542 private void pruneDeletions(ShiftList deletions)
1544 if (deletions != null)
1546 final List<int[]> shifts = deletions.getShifts();
1547 if (shifts != null && shifts.size() > 0)
1549 pruneDeletions(shifts);
1551 // and shift the rest.
1552 this.compensateForEdits(deletions);
1558 * Adjust hidden column boundaries based on a series of column additions or
1559 * deletions in visible regions.
1561 * @param shiftrecord
1564 private ShiftList compensateForEdits(ShiftList shiftrecord)
1566 if (shiftrecord != null)
1568 final List<int[]> shifts = shiftrecord.getShifts();
1569 if (shifts != null && shifts.size() > 0)
1572 for (int i = 0, j = shifts.size(); i < j; i++)
1574 int[] sh = shifts.get(i);
1575 compensateForDelEdits(shifted + sh[0], sh[1]);
1579 return shiftrecord.getInverse();
1585 * Returns a hashCode built from hidden column ranges
1588 public int hashCode()
1592 LOCK.readLock().lock();
1594 if (hiddenColumns != null)
1596 for (int[] hidden : hiddenColumns)
1598 hashCode = 31 * hashCode + hidden[0];
1599 hashCode = 31 * hashCode + hidden[1];
1606 LOCK.readLock().unlock();
1611 * Hide columns corresponding to the marked bits
1614 * - columns map to bits starting from zero
1616 public void hideMarkedBits(BitSet inserts)
1620 LOCK.writeLock().lock();
1621 for (int firstSet = inserts
1622 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1623 .nextSetBit(lastSet))
1625 lastSet = inserts.nextClearBit(firstSet);
1626 hideColumns(firstSet, lastSet - 1, true);
1630 LOCK.writeLock().unlock();
1637 * BitSet where hidden columns will be marked
1639 public void markHiddenRegions(BitSet inserts)
1643 LOCK.readLock().lock();
1644 if (hiddenColumns == null)
1648 for (int[] range : hiddenColumns)
1650 inserts.set(range[0], range[1] + 1);
1655 LOCK.readLock().unlock();
1660 * Calculate the visible start and end index of an alignment.
1663 * full alignment width
1664 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1666 public int[] getVisibleStartAndEndIndex(int width)
1670 LOCK.readLock().lock();
1671 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1672 int startPos = alignmentStartEnd[0];
1673 int endPos = alignmentStartEnd[1];
1675 int[] lowestRange = new int[] { -1, -1 };
1676 int[] higestRange = new int[] { -1, -1 };
1678 if (hiddenColumns == null)
1680 return new int[] { startPos, endPos };
1683 for (int[] hiddenCol : hiddenColumns)
1685 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1686 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1689 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1691 startPos = alignmentStartEnd[0];
1695 startPos = lowestRange[1] + 1;
1698 if (higestRange[0] == -1 && higestRange[1] == -1)
1700 endPos = alignmentStartEnd[1];
1704 endPos = higestRange[0] - 1;
1706 return new int[] { startPos, endPos };
1709 LOCK.readLock().unlock();