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 if (hiddenColumns != null)
72 for (int[] range : hiddenColumns)
74 regionBuilder.append(delimiter).append(range[0]).append(between)
78 regionBuilder.deleteCharAt(0);
80 return regionBuilder.toString();
83 LOCK.readLock().unlock();
88 * Find the number of hidden columns
90 * @return number of hidden columns
96 LOCK.readLock().lock();
100 for (int[] range : hiddenColumns)
102 size += range[1] - range[0] + 1;
109 LOCK.readLock().unlock();
114 * Answers if there are any hidden columns
116 * @return true if there are hidden columns
118 public boolean hasHidden()
122 LOCK.readLock().lock();
123 return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
126 LOCK.readLock().unlock();
132 public boolean equals(Object obj)
136 LOCK.readLock().lock();
138 if (!(obj instanceof HiddenColumns))
142 HiddenColumns that = (HiddenColumns) obj;
145 * check hidden columns are either both null, or match
147 if (this.hiddenColumns == null)
149 return (that.hiddenColumns == null);
151 if (that.hiddenColumns == null
152 || that.hiddenColumns.size() != this.hiddenColumns.size())
157 for (int[] thisRange : hiddenColumns)
159 int[] thatRange = that.hiddenColumns.get(i++);
160 if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
168 LOCK.readLock().unlock();
173 * Return absolute column index for a visible column index
176 * int column index in alignment view (count from zero)
177 * @return alignment column index for column
179 public int adjustForHiddenColumns(int column)
183 LOCK.readLock().lock();
185 if (hiddenColumns != null)
187 for (int i = 0; i < hiddenColumns.size(); i++)
189 int[] region = hiddenColumns.elementAt(i);
190 if (result >= region[0])
192 result += region[1] - region[0] + 1;
199 LOCK.readLock().unlock();
204 * Use this method to find out where a column will appear in the visible
205 * alignment when hidden columns exist. If the column is not visible, then the
206 * left-most visible column will always be returned.
208 * @param hiddenColumn
209 * the column index in the full alignment including hidden columns
210 * @return the position of the column in the visible alignment
212 public int findColumnPosition(int hiddenColumn)
216 LOCK.readLock().lock();
217 int result = hiddenColumn;
218 if (hiddenColumns != null)
224 region = hiddenColumns.elementAt(index++);
225 if (hiddenColumn > region[1])
227 result -= region[1] + 1 - region[0];
229 } while ((hiddenColumn > region[1])
230 && (index < hiddenColumns.size()));
232 if (hiddenColumn >= region[0] && hiddenColumn <= region[1])
234 // Here the hidden column is within a region, so
235 // we want to return the position of region[0]-1, adjusted for any
236 // earlier hidden columns.
237 // Calculate the difference between the actual hidden col position
238 // and region[0]-1, and then subtract from result to convert result
240 // the adjusted hiddenColumn value to the adjusted region[0]-1 value
242 // However, if the region begins at 0 we cannot return region[0]-1
250 return result - (hiddenColumn - region[0] + 1);
254 return result; // return the shifted position after removing hidden
258 LOCK.readLock().unlock();
263 * Find the visible column which is a given visible number of columns to the
264 * left of another visible column. i.e. for a startColumn x, the column which
265 * is distance 1 away will be column x-1.
267 * @param visibleDistance
268 * the number of visible columns to offset by
270 * the column to start from
271 * @return the position of the column in the visible alignment
273 public int subtractVisibleColumns(int visibleDistance, int startColumn)
278 LOCK.readLock().lock();
279 int distance = visibleDistance;
281 // in case startColumn is in a hidden region, move it to the left
282 int start = adjustForHiddenColumns(findColumnPosition(startColumn));
284 // get index of hidden region to left of start
285 int index = getHiddenIndexLeft(start);
288 // no hidden regions to left of startColumn
289 return start - distance;
292 // walk backwards through the alignment subtracting the counts of visible
293 // columns from distance
296 int nextstart = start;
298 while ((index > -1) && (distance - gap > 0))
300 // subtract the gap to right of region from distance
304 // calculate the next gap
305 region = hiddenColumns.get(index);
306 gap = start - region[1];
308 // set start to just to left of current region
309 nextstart = region[0] - 1;
313 if (distance - gap > 0)
315 // fell out of loop because there are no more hidden regions
317 return nextstart - distance;
319 return start - distance;
322 LOCK.readLock().unlock();
328 * Use this method to determine the set of hiddenRegion start positions
330 * @return list of column number in visible view where hidden regions start
332 public List<Integer> findHiddenRegionPositions()
336 LOCK.readLock().lock();
337 List<Integer> positions = null;
339 if (hiddenColumns != null)
341 positions = new ArrayList<>(hiddenColumns.size());
343 positions.add(hiddenColumns.elementAt(0)[0]);
344 for (int i = 1; i < hiddenColumns.size(); ++i)
348 if (hiddenColumns != null)
354 int[] region = hiddenColumns.elementAt(index);
355 gaps += region[1] + 1 - region[0];
356 result = region[1] + 1;
358 } while (index <= i);
362 positions.add(result);
367 positions = new ArrayList<>();
374 LOCK.readLock().unlock();
379 * This method returns the rightmost limit of a region of an alignment with
380 * hidden columns. In otherwords, the next hidden column.
385 public int getHiddenBoundaryRight(int alPos)
389 LOCK.readLock().lock();
390 if (hiddenColumns != null)
395 int[] region = hiddenColumns.elementAt(index);
396 if (alPos < region[0])
402 } while (index < hiddenColumns.size());
408 LOCK.readLock().unlock();
414 * This method returns the leftmost limit of a region of an alignment with
415 * hidden columns. In otherwords, the previous hidden column.
420 public int getHiddenBoundaryLeft(int alPos)
424 LOCK.readLock().lock();
426 if (hiddenColumns != null)
428 int index = hiddenColumns.size() - 1;
431 int[] region = hiddenColumns.elementAt(index);
432 if (alPos > region[1])
438 } while (index > -1);
444 LOCK.readLock().unlock();
449 * This method returns the index of the hidden region to the left of a column
450 * position. If the column is in a hidden region it returns the index of the
451 * region to the left. If there is no hidden region to the left it returns -1.
456 private int getHiddenIndexLeft(int pos)
461 LOCK.readLock().lock();
462 if (hiddenColumns != null)
464 int index = hiddenColumns.size() - 1;
467 int[] region = hiddenColumns.elementAt(index);
474 } while (index > -1);
480 LOCK.readLock().unlock();
486 * Adds the specified column range to the hidden columns
491 public void hideColumns(int start, int end)
493 hideColumns(start, end, false);
497 * Adds the specified column range to the hidden columns
502 private void hideColumns(int start, int end, boolean alreadyLocked)
509 LOCK.writeLock().lock();
512 if (hiddenColumns == null)
514 hiddenColumns = new Vector<>();
518 * traverse existing hidden ranges and insert / amend / append as
521 for (int i = 0; i < hiddenColumns.size(); i++)
523 int[] region = hiddenColumns.elementAt(i);
525 if (end < region[0] - 1)
528 * insert discontiguous preceding range
530 hiddenColumns.insertElementAt(new int[] { start, end }, i);
534 if (end <= region[1])
537 * new range overlaps existing, or is contiguous preceding it - adjust
540 region[0] = Math.min(region[0], start);
544 if (start <= region[1] + 1)
547 * new range overlaps existing, or is contiguous following it - adjust
548 * start and end columns
550 region[0] = Math.min(region[0], start);
551 region[1] = Math.max(region[1], end);
554 * also update or remove any subsequent ranges
555 * that are overlapped
557 while (i < hiddenColumns.size() - 1)
559 int[] nextRegion = hiddenColumns.get(i + 1);
560 if (nextRegion[0] > end + 1)
563 * gap to next hidden range - no more to update
567 region[1] = Math.max(nextRegion[1], end);
568 hiddenColumns.remove(i + 1);
575 * remaining case is that the new range follows everything else
577 hiddenColumns.addElement(new int[] { start, end });
582 LOCK.writeLock().unlock();
587 public boolean isVisible(int column)
591 LOCK.readLock().lock();
593 if (hiddenColumns != null)
595 for (int[] region : hiddenColumns)
597 if (column >= region[0] && column <= region[1])
607 LOCK.readLock().unlock();
614 public HiddenColumns()
623 public HiddenColumns(HiddenColumns copy)
628 LOCK.readLock().lock();
631 if (copy.hiddenColumns != null)
633 hiddenColumns = copy.copyHiddenRegions();
639 LOCK.readLock().unlock();
643 private Vector<int[]> copyHiddenRegions()
645 Vector<int[]> copy = new Vector<>(hiddenColumns.size());
646 for (int i = 0, j = hiddenColumns.size(); i < j; i++)
650 rh = hiddenColumns.elementAt(i);
653 cp = new int[rh.length];
654 System.arraycopy(rh, 0, cp, 0, rh.length);
661 private ArrayList<int[]> copyHiddenRegionsToArrayList()
663 ArrayList<int[]> copy = new ArrayList<>(hiddenColumns.size());
664 for (int i = 0, j = hiddenColumns.size(); i < j; i++)
668 rh = hiddenColumns.elementAt(i);
671 cp = new int[rh.length];
672 System.arraycopy(rh, 0, cp, 0, rh.length);
680 * Returns a copy of the vector of hidden regions, as a vector. Before using
681 * this method please consider if you really need access to the hidden regions
682 * - a new (or existing!) method on HiddenColumns might be more appropriate.
684 * @return hidden regions as vector of [start,end] pairs
686 public Vector<int[]> getHiddenColumnsCopy()
690 LOCK.readLock().lock();
691 return copyHiddenRegions();
694 LOCK.readLock().unlock();
699 * Returns a copy of the vector of hidden regions, as an ArrayList. Before
700 * using this method please consider if you really need access to the hidden
701 * regions - a new (or existing!) method on HiddenColumns might be more
704 * @return hidden regions as an ArrayList of [start,end] pairs
706 public ArrayList<int[]> getHiddenColumnsCopyAsList()
710 LOCK.readLock().lock();
711 return copyHiddenRegionsToArrayList();
714 LOCK.readLock().unlock();
719 * propagate shift in alignment columns to column selection
724 * shift in edit (+ve for removal, or -ve for inserts)
726 public List<int[]> compensateForEdit(int start, int change,
731 LOCK.writeLock().lock();
732 List<int[]> deletedHiddenColumns = null;
734 if (hiddenColumns != null)
736 deletedHiddenColumns = new ArrayList<>();
737 int hSize = hiddenColumns.size();
738 for (int i = 0; i < hSize; i++)
740 int[] region = hiddenColumns.elementAt(i);
741 if (region[0] > start && start + change > region[1])
743 deletedHiddenColumns.add(region);
745 hiddenColumns.removeElementAt(i);
751 if (region[0] > start)
764 this.revealHiddenColumns(0, sel);
767 return deletedHiddenColumns;
770 LOCK.writeLock().unlock();
775 * propagate shift in alignment columns to column selection special version of
776 * compensateForEdit - allowing for edits within hidden regions
781 * shift in edit (+ve for removal, or -ve for inserts)
783 public void compensateForDelEdits(int start, int change)
787 LOCK.writeLock().lock();
788 if (hiddenColumns != null)
790 for (int i = 0; i < hiddenColumns.size(); i++)
792 int[] region = hiddenColumns.elementAt(i);
793 if (region[0] >= start)
797 if (region[1] >= start)
801 if (region[1] < region[0])
803 hiddenColumns.removeElementAt(i--);
819 LOCK.writeLock().unlock();
824 * return all visible segments between the given start and end boundaries
827 * (first column inclusive from 0)
829 * (last column - not inclusive)
830 * @return int[] {i_start, i_end, ..} where intervals lie in
831 * start<=i_start<=i_end<end
833 public int[] getVisibleContigs(int start, int end)
837 LOCK.readLock().lock();
838 if (hiddenColumns != null && hiddenColumns.size() > 0)
840 List<int[]> visiblecontigs = new ArrayList<>();
841 List<int[]> regions = getHiddenRegions();
848 for (int j = 0; vstart < end && j < regions.size(); j++)
850 region = regions.get(j);
851 hideStart = region[0];
854 if (hideEnd < vstart)
858 if (hideStart > vstart)
860 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
862 vstart = hideEnd + 1;
867 visiblecontigs.add(new int[] { vstart, end - 1 });
869 int[] vcontigs = new int[visiblecontigs.size() * 2];
870 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
872 int[] vc = visiblecontigs.get(i);
873 visiblecontigs.set(i, null);
874 vcontigs[i * 2] = vc[0];
875 vcontigs[i * 2 + 1] = vc[1];
877 visiblecontigs.clear();
882 return new int[] { start, end - 1 };
887 LOCK.readLock().unlock();
891 public String[] getVisibleSequenceStrings(int start, int end,
896 LOCK.readLock().lock();
897 int iSize = seqs.length;
898 String selections[] = new String[iSize];
899 if (hiddenColumns != null && hiddenColumns.size() > 0)
901 for (int i = 0; i < iSize; i++)
903 StringBuffer visibleSeq = new StringBuffer();
904 List<int[]> regions = getHiddenRegions();
906 int blockStart = start;
912 for (int j = 0; j < regions.size(); j++)
914 region = regions.get(j);
915 hideStart = region[0];
918 if (hideStart < start)
923 blockStart = Math.min(blockStart, hideEnd + 1);
924 blockEnd = Math.min(blockEnd, hideStart);
926 if (blockStart > blockEnd)
931 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
933 blockStart = hideEnd + 1;
937 if (end > blockStart)
939 visibleSeq.append(seqs[i].getSequence(blockStart, end));
942 selections[i] = visibleSeq.toString();
947 for (int i = 0; i < iSize; i++)
949 selections[i] = seqs[i].getSequenceAsString(start, end);
957 LOCK.readLock().unlock();
962 * Locate the first and last position visible for this sequence. if seq isn't
963 * visible then return the position of the left and right of the hidden
964 * boundary region, and the corresponding alignment column indices for the
965 * extent of the sequence
968 * @return int[] { visible start, visible end, first seqpos, last seqpos,
969 * alignment index for seq start, alignment index for seq end }
971 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
975 LOCK.readLock().lock();
976 int fpos = seq.getStart();
977 int lpos = seq.getEnd();
980 if (hiddenColumns == null || hiddenColumns.size() == 0)
982 int ifpos = seq.findIndex(fpos) - 1;
983 int ilpos = seq.findIndex(lpos) - 1;
984 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
987 // Simply walk along the sequence whilst watching for hidden column
989 List<int[]> regions = getHiddenRegions();
993 int hideStart = seq.getLength();
999 boolean foundStart = false;
1000 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
1003 if (!Comparison.isGap(seq.getCharAt(p)))
1005 // keep track of first/last column
1006 // containing sequence data regardless of visibility
1012 // update hidden region start/end
1013 while (hideEnd < p && rcount < regions.size())
1015 int[] region = regions.get(rcount++);
1017 visNext += region[0] - visPrev;
1018 hideStart = region[0];
1019 hideEnd = region[1];
1023 hideStart = seq.getLength();
1025 // update visible boundary for sequence
1037 // look for next sequence position
1043 return new int[] { findColumnPosition(start),
1044 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
1046 // otherwise, sequence was completely hidden
1047 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
1051 LOCK.readLock().unlock();
1056 * delete any columns in alignmentAnnotation that are hidden (including
1057 * sequence associated annotation).
1059 * @param alignmentAnnotation
1061 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1063 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1067 * delete any columns in alignmentAnnotation that are hidden (including
1068 * sequence associated annotation).
1071 * remove any annotation to the right of this column
1073 * remove any annotation to the left of this column
1074 * @param alignmentAnnotation
1075 * the annotation to operate on
1077 public void makeVisibleAnnotation(int start, int end,
1078 AlignmentAnnotation alignmentAnnotation)
1082 LOCK.readLock().lock();
1083 if (alignmentAnnotation.annotations == null)
1087 if (start == end && end == -1)
1090 end = alignmentAnnotation.annotations.length;
1092 if (hiddenColumns != null && hiddenColumns.size() > 0)
1094 // then mangle the alignmentAnnotation annotation array
1095 Vector<Annotation[]> annels = new Vector<>();
1096 Annotation[] els = null;
1097 List<int[]> regions = getHiddenRegions();
1098 int blockStart = start;
1105 for (int j = 0; j < regions.size(); j++)
1107 region = regions.get(j);
1108 hideStart = region[0];
1109 hideEnd = region[1];
1111 if (hideStart < start)
1116 blockStart = Math.min(blockStart, hideEnd + 1);
1117 blockEnd = Math.min(blockEnd, hideStart);
1119 if (blockStart > blockEnd)
1124 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1125 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1128 blockStart = hideEnd + 1;
1132 if (end > blockStart)
1134 annels.addElement(els = new Annotation[end - blockStart + 1]);
1136 + blockStart) <= alignmentAnnotation.annotations.length)
1138 // copy just the visible segment of the annotation row
1139 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1140 els, 0, els.length);
1144 // copy to the end of the annotation row
1145 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1147 (alignmentAnnotation.annotations.length - blockStart));
1156 alignmentAnnotation.annotations = new Annotation[w];
1159 for (Annotation[] chnk : annels)
1161 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1168 alignmentAnnotation.restrict(start, end);
1173 LOCK.readLock().unlock();
1179 * @return true if there are columns hidden
1181 public boolean hasHiddenColumns()
1185 LOCK.readLock().lock();
1186 return hiddenColumns != null && hiddenColumns.size() > 0;
1189 LOCK.readLock().unlock();
1195 * @return true if there are more than one set of columns hidden
1197 public boolean hasManyHiddenColumns()
1201 LOCK.readLock().lock();
1202 return hiddenColumns != null && hiddenColumns.size() > 1;
1205 LOCK.readLock().unlock();
1210 * mark the columns corresponding to gap characters as hidden in the column
1215 public void hideInsertionsFor(SequenceI sr)
1219 LOCK.writeLock().lock();
1220 List<int[]> inserts = sr.getInsertions();
1221 for (int[] r : inserts)
1223 hideColumns(r[0], r[1], true);
1227 LOCK.writeLock().unlock();
1232 * Unhides, and adds to the selection list, all hidden columns
1234 public void revealAllHiddenColumns(ColumnSelection sel)
1238 LOCK.writeLock().lock();
1239 if (hiddenColumns != null)
1241 for (int i = 0; i < hiddenColumns.size(); i++)
1243 int[] region = hiddenColumns.elementAt(i);
1244 for (int j = region[0]; j < region[1] + 1; j++)
1251 hiddenColumns = null;
1255 LOCK.writeLock().unlock();
1260 * Reveals, and marks as selected, the hidden column range with the given
1265 public void revealHiddenColumns(int start, ColumnSelection sel)
1269 LOCK.writeLock().lock();
1270 for (int i = 0; i < hiddenColumns.size(); i++)
1272 int[] region = hiddenColumns.elementAt(i);
1273 if (start == region[0])
1275 for (int j = region[0]; j < region[1] + 1; j++)
1280 hiddenColumns.removeElement(region);
1284 if (hiddenColumns.size() == 0)
1286 hiddenColumns = null;
1291 LOCK.writeLock().unlock();
1296 * removes intersection of position,length ranges in deletions from the
1297 * start,end regions marked in intervals.
1303 private boolean pruneIntervalVector(final List<int[]> shifts,
1304 Vector<int[]> intervals)
1306 boolean pruned = false;
1308 int j = intervals.size() - 1;
1310 int t = shifts.size() - 1;
1311 int hr[] = intervals.elementAt(i);
1312 int sr[] = shifts.get(s);
1313 while (i <= j && s <= t)
1315 boolean trailinghn = hr[1] >= sr[0];
1320 hr = intervals.elementAt(++i);
1328 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
1329 if (endshift < hr[0] || endshift < sr[0])
1330 { // leadinghc disjoint or not a deletion
1333 sr = shifts.get(++s);
1341 boolean leadinghn = hr[0] >= sr[0];
1342 boolean leadinghc = hr[0] < endshift;
1343 boolean trailinghc = hr[1] < endshift;
1347 { // deleted hidden region.
1348 intervals.removeElementAt(i);
1353 hr = intervals.elementAt(i);
1359 hr[0] = endshift; // clip c terminal region
1360 leadinghn = !leadinghn;
1376 // sr contained in hr
1379 sr = shifts.get(++s);
1389 return pruned; // true if any interval was removed or modified by
1394 * remove any hiddenColumns or selected columns and shift remaining based on a
1395 * series of position, range deletions.
1399 public void pruneDeletions(List<int[]> shifts)
1403 LOCK.writeLock().lock();
1404 // delete any intervals intersecting.
1405 if (hiddenColumns != null)
1407 pruneIntervalVector(shifts, hiddenColumns);
1408 if (hiddenColumns != null && hiddenColumns.size() == 0)
1410 hiddenColumns = null;
1416 LOCK.writeLock().unlock();
1421 * Add gaps into the sequences aligned to profileseq under the given
1426 * - alignment to have gaps inserted into it
1428 * - alignment view where sequence corresponding to profileseq is
1430 * @return new HiddenColumns for new alignment view, with insertions into
1431 * profileseq marked as hidden.
1433 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1434 AlignmentI al, AlignmentView input)
1438 char gc = al.getGapCharacter();
1439 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1440 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1441 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1442 nview.propagateInsertions(profileseq, al, origseq);
1449 * - sequence in al which corresponds to origseq
1451 * - alignment which is to have gaps inserted into it
1453 * - sequence corresponding to profileseq which defines gap map for
1456 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1459 char gc = al.getGapCharacter();
1460 // recover mapping between sequence's non-gap positions and positions
1462 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1463 int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
1464 profileseq.getLength());
1468 // add profile to visible contigs
1469 for (int v = 0; v < viscontigs.length; v += 2)
1471 if (viscontigs[v] > spos)
1473 StringBuffer sb = new StringBuffer();
1474 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1478 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1480 SequenceI sqobj = al.getSequenceAt(s);
1481 if (sqobj != profileseq)
1483 String sq = al.getSequenceAt(s).getSequenceAsString();
1484 if (sq.length() <= spos + offset)
1487 int diff = spos + offset - sq.length() - 1;
1492 while ((diff = spos + offset - sq.length() - 1) > 0)
1495 // + ((diff >= sb.length()) ? sb.toString() : sb
1496 // .substring(0, diff));
1497 if (diff >= sb.length())
1499 sq += sb.toString();
1503 char[] buf = new char[diff];
1504 sb.getChars(0, diff, buf, 0);
1505 sq += buf.toString();
1509 sq += sb.toString();
1513 al.getSequenceAt(s).setSequence(
1514 sq.substring(0, spos + offset) + sb.toString()
1515 + sq.substring(spos + offset));
1519 // offset+=sb.length();
1521 spos = viscontigs[v + 1] + 1;
1523 if ((offset + spos) < profileseq.getLength())
1525 // pad the final region with gaps.
1526 StringBuffer sb = new StringBuffer();
1527 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1531 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1533 SequenceI sqobj = al.getSequenceAt(s);
1534 if (sqobj == profileseq)
1538 String sq = sqobj.getSequenceAsString();
1540 int diff = origseq.getLength() - sq.length();
1544 // + ((diff >= sb.length()) ? sb.toString() : sb
1545 // .substring(0, diff));
1546 if (diff >= sb.length())
1548 sq += sb.toString();
1552 char[] buf = new char[diff];
1553 sb.getChars(0, diff, buf, 0);
1554 sq += buf.toString();
1556 diff = origseq.getLength() - sq.length();
1563 * remove any hiddenColumns or selected columns and shift remaining based on a
1564 * series of position, range deletions.
1568 private void pruneDeletions(ShiftList deletions)
1570 if (deletions != null)
1572 final List<int[]> shifts = deletions.getShifts();
1573 if (shifts != null && shifts.size() > 0)
1575 pruneDeletions(shifts);
1577 // and shift the rest.
1578 this.compensateForEdits(deletions);
1584 * Adjust hidden column boundaries based on a series of column additions or
1585 * deletions in visible regions.
1587 * @param shiftrecord
1590 private ShiftList compensateForEdits(ShiftList shiftrecord)
1592 if (shiftrecord != null)
1594 final List<int[]> shifts = shiftrecord.getShifts();
1595 if (shifts != null && shifts.size() > 0)
1598 for (int i = 0, j = shifts.size(); i < j; i++)
1600 int[] sh = shifts.get(i);
1601 compensateForDelEdits(shifted + sh[0], sh[1]);
1605 return shiftrecord.getInverse();
1611 * Returns a hashCode built from hidden column ranges
1614 public int hashCode()
1618 LOCK.readLock().lock();
1620 if (hiddenColumns != null)
1622 for (int[] hidden : hiddenColumns)
1624 hashCode = 31 * hashCode + hidden[0];
1625 hashCode = 31 * hashCode + hidden[1];
1632 LOCK.readLock().unlock();
1637 * Hide columns corresponding to the marked bits
1640 * - columns map to bits starting from zero
1642 public void hideMarkedBits(BitSet inserts)
1646 LOCK.writeLock().lock();
1647 for (int firstSet = inserts
1648 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1649 .nextSetBit(lastSet))
1651 lastSet = inserts.nextClearBit(firstSet);
1652 hideColumns(firstSet, lastSet - 1, true);
1656 LOCK.writeLock().unlock();
1663 * BitSet where hidden columns will be marked
1665 public void markHiddenRegions(BitSet inserts)
1669 LOCK.readLock().lock();
1670 if (hiddenColumns == null)
1674 for (int[] range : hiddenColumns)
1676 inserts.set(range[0], range[1] + 1);
1681 LOCK.readLock().unlock();
1686 * Calculate the visible start and end index of an alignment.
1689 * full alignment width
1690 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1692 public int[] getVisibleStartAndEndIndex(int width)
1696 LOCK.readLock().lock();
1697 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1698 int startPos = alignmentStartEnd[0];
1699 int endPos = alignmentStartEnd[1];
1701 int[] lowestRange = new int[] { -1, -1 };
1702 int[] higestRange = new int[] { -1, -1 };
1704 if (hiddenColumns == null)
1706 return new int[] { startPos, endPos };
1709 for (int[] hiddenCol : hiddenColumns)
1711 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1712 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1715 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1717 startPos = alignmentStartEnd[0];
1721 startPos = lowestRange[1] + 1;
1724 if (higestRange[0] == -1 && higestRange[1] == -1)
1726 endPos = alignmentStartEnd[1];
1730 endPos = higestRange[0] - 1;
1732 return new int[] { startPos, endPos };
1735 LOCK.readLock().unlock();
1741 * Finds the hidden region (if any) which starts or ends at res
1744 * visible residue position, unadjusted for hidden columns
1745 * @return region as [start,end] or null if no matching region is found
1747 public int[] getRegionWithEdgeAtRes(int res)
1751 LOCK.readLock().lock();
1752 int adjres = adjustForHiddenColumns(res);
1754 int[] reveal = null;
1755 if (hiddenColumns != null)
1757 for (int[] region : hiddenColumns)
1759 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1769 LOCK.readLock().unlock();