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 private 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()
664 if (hiddenColumns != null)
666 size = hiddenColumns.size();
668 ArrayList<int[]> copy = new ArrayList<>(size);
670 for (int i = 0, j = size; i < j; i++)
674 rh = hiddenColumns.elementAt(i);
677 cp = new int[rh.length];
678 System.arraycopy(rh, 0, cp, 0, rh.length);
687 * Returns a copy of the vector of hidden regions, as a vector. Before using
688 * this method please consider if you really need access to the hidden regions
689 * - a new (or existing!) method on HiddenColumns might be more appropriate.
691 * @return hidden regions as vector of [start,end] pairs
693 public Vector<int[]> getHiddenColumnsCopy()
697 LOCK.readLock().lock();
698 return copyHiddenRegions();
701 LOCK.readLock().unlock();
706 * Returns a copy of the vector of hidden regions, as an ArrayList. Before
707 * using this method please consider if you really need access to the hidden
708 * regions - a new (or existing!) method on HiddenColumns might be more
711 * @return hidden regions as an ArrayList of [start,end] pairs
713 public ArrayList<int[]> getHiddenColumnsCopyAsList()
717 LOCK.readLock().lock();
718 return copyHiddenRegionsToArrayList();
721 LOCK.readLock().unlock();
726 * propagate shift in alignment columns to column selection
731 * shift in edit (+ve for removal, or -ve for inserts)
733 public List<int[]> compensateForEdit(int start, int change,
738 LOCK.writeLock().lock();
739 List<int[]> deletedHiddenColumns = null;
741 if (hiddenColumns != null)
743 deletedHiddenColumns = new ArrayList<>();
744 int hSize = hiddenColumns.size();
745 for (int i = 0; i < hSize; i++)
747 int[] region = hiddenColumns.elementAt(i);
748 if (region[0] > start && start + change > region[1])
750 deletedHiddenColumns.add(region);
752 hiddenColumns.removeElementAt(i);
758 if (region[0] > start)
771 this.revealHiddenColumns(0, sel);
774 return deletedHiddenColumns;
777 LOCK.writeLock().unlock();
782 * propagate shift in alignment columns to column selection special version of
783 * compensateForEdit - allowing for edits within hidden regions
788 * shift in edit (+ve for removal, or -ve for inserts)
790 public void compensateForDelEdits(int start, int change)
794 LOCK.writeLock().lock();
795 if (hiddenColumns != null)
797 for (int i = 0; i < hiddenColumns.size(); i++)
799 int[] region = hiddenColumns.elementAt(i);
800 if (region[0] >= start)
804 if (region[1] >= start)
808 if (region[1] < region[0])
810 hiddenColumns.removeElementAt(i--);
826 LOCK.writeLock().unlock();
831 * return all visible segments between the given start and end boundaries
834 * (first column inclusive from 0)
836 * (last column - not inclusive)
837 * @return int[] {i_start, i_end, ..} where intervals lie in
838 * start<=i_start<=i_end<end
840 public int[] getVisibleContigs(int start, int end)
844 LOCK.readLock().lock();
845 if (hiddenColumns != null && hiddenColumns.size() > 0)
847 List<int[]> visiblecontigs = new ArrayList<>();
848 List<int[]> regions = getHiddenRegions();
855 for (int j = 0; vstart < end && j < regions.size(); j++)
857 region = regions.get(j);
858 hideStart = region[0];
861 if (hideEnd < vstart)
865 if (hideStart > vstart)
867 visiblecontigs.add(new int[] { vstart, hideStart - 1 });
869 vstart = hideEnd + 1;
874 visiblecontigs.add(new int[] { vstart, end - 1 });
876 int[] vcontigs = new int[visiblecontigs.size() * 2];
877 for (int i = 0, j = visiblecontigs.size(); i < j; i++)
879 int[] vc = visiblecontigs.get(i);
880 visiblecontigs.set(i, null);
881 vcontigs[i * 2] = vc[0];
882 vcontigs[i * 2 + 1] = vc[1];
884 visiblecontigs.clear();
889 return new int[] { start, end - 1 };
894 LOCK.readLock().unlock();
898 public String[] getVisibleSequenceStrings(int start, int end,
903 LOCK.readLock().lock();
904 int iSize = seqs.length;
905 String selections[] = new String[iSize];
906 if (hiddenColumns != null && hiddenColumns.size() > 0)
908 for (int i = 0; i < iSize; i++)
910 StringBuffer visibleSeq = new StringBuffer();
911 List<int[]> regions = getHiddenRegions();
913 int blockStart = start;
919 for (int j = 0; j < regions.size(); j++)
921 region = regions.get(j);
922 hideStart = region[0];
925 if (hideStart < start)
930 blockStart = Math.min(blockStart, hideEnd + 1);
931 blockEnd = Math.min(blockEnd, hideStart);
933 if (blockStart > blockEnd)
938 visibleSeq.append(seqs[i].getSequence(blockStart, blockEnd));
940 blockStart = hideEnd + 1;
944 if (end > blockStart)
946 visibleSeq.append(seqs[i].getSequence(blockStart, end));
949 selections[i] = visibleSeq.toString();
954 for (int i = 0; i < iSize; i++)
956 selections[i] = seqs[i].getSequenceAsString(start, end);
964 LOCK.readLock().unlock();
969 * Locate the first and last position visible for this sequence. if seq isn't
970 * visible then return the position of the left and right of the hidden
971 * boundary region, and the corresponding alignment column indices for the
972 * extent of the sequence
975 * @return int[] { visible start, visible end, first seqpos, last seqpos,
976 * alignment index for seq start, alignment index for seq end }
978 public int[] locateVisibleBoundsOfSequence(SequenceI seq)
982 LOCK.readLock().lock();
983 int fpos = seq.getStart();
984 int lpos = seq.getEnd();
987 if (hiddenColumns == null || hiddenColumns.size() == 0)
989 int ifpos = seq.findIndex(fpos) - 1;
990 int ilpos = seq.findIndex(lpos) - 1;
991 return new int[] { ifpos, ilpos, fpos, lpos, ifpos, ilpos };
994 // Simply walk along the sequence whilst watching for hidden column
996 List<int[]> regions = getHiddenRegions();
1000 int hideStart = seq.getLength();
1006 boolean foundStart = false;
1007 for (int p = 0, pLen = seq.getLength(); spos <= seq.getEnd()
1010 if (!Comparison.isGap(seq.getCharAt(p)))
1012 // keep track of first/last column
1013 // containing sequence data regardless of visibility
1019 // update hidden region start/end
1020 while (hideEnd < p && rcount < regions.size())
1022 int[] region = regions.get(rcount++);
1024 visNext += region[0] - visPrev;
1025 hideStart = region[0];
1026 hideEnd = region[1];
1030 hideStart = seq.getLength();
1032 // update visible boundary for sequence
1044 // look for next sequence position
1050 return new int[] { findColumnPosition(start),
1051 findColumnPosition(lastvispos), fpos, lpos, firstP, lastP };
1053 // otherwise, sequence was completely hidden
1054 return new int[] { visPrev, visNext, 0, 0, firstP, lastP };
1058 LOCK.readLock().unlock();
1063 * delete any columns in alignmentAnnotation that are hidden (including
1064 * sequence associated annotation).
1066 * @param alignmentAnnotation
1068 public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
1070 makeVisibleAnnotation(-1, -1, alignmentAnnotation);
1074 * delete any columns in alignmentAnnotation that are hidden (including
1075 * sequence associated annotation).
1078 * remove any annotation to the right of this column
1080 * remove any annotation to the left of this column
1081 * @param alignmentAnnotation
1082 * the annotation to operate on
1084 public void makeVisibleAnnotation(int start, int end,
1085 AlignmentAnnotation alignmentAnnotation)
1089 LOCK.readLock().lock();
1090 if (alignmentAnnotation.annotations == null)
1094 if (start == end && end == -1)
1097 end = alignmentAnnotation.annotations.length;
1099 if (hiddenColumns != null && hiddenColumns.size() > 0)
1101 // then mangle the alignmentAnnotation annotation array
1102 Vector<Annotation[]> annels = new Vector<>();
1103 Annotation[] els = null;
1104 List<int[]> regions = getHiddenRegions();
1105 int blockStart = start;
1112 for (int j = 0; j < regions.size(); j++)
1114 region = regions.get(j);
1115 hideStart = region[0];
1116 hideEnd = region[1];
1118 if (hideStart < start)
1123 blockStart = Math.min(blockStart, hideEnd + 1);
1124 blockEnd = Math.min(blockEnd, hideStart);
1126 if (blockStart > blockEnd)
1131 annels.addElement(els = new Annotation[blockEnd - blockStart]);
1132 System.arraycopy(alignmentAnnotation.annotations, blockStart, els,
1135 blockStart = hideEnd + 1;
1139 if (end > blockStart)
1141 annels.addElement(els = new Annotation[end - blockStart + 1]);
1143 + blockStart) <= alignmentAnnotation.annotations.length)
1145 // copy just the visible segment of the annotation row
1146 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1147 els, 0, els.length);
1151 // copy to the end of the annotation row
1152 System.arraycopy(alignmentAnnotation.annotations, blockStart,
1154 (alignmentAnnotation.annotations.length - blockStart));
1163 alignmentAnnotation.annotations = new Annotation[w];
1166 for (Annotation[] chnk : annels)
1168 System.arraycopy(chnk, 0, alignmentAnnotation.annotations, w,
1175 alignmentAnnotation.restrict(start, end);
1180 LOCK.readLock().unlock();
1186 * @return true if there are columns hidden
1188 public boolean hasHiddenColumns()
1192 LOCK.readLock().lock();
1193 return hiddenColumns != null && hiddenColumns.size() > 0;
1196 LOCK.readLock().unlock();
1202 * @return true if there are more than one set of columns hidden
1204 public boolean hasManyHiddenColumns()
1208 LOCK.readLock().lock();
1209 return hiddenColumns != null && hiddenColumns.size() > 1;
1212 LOCK.readLock().unlock();
1217 * mark the columns corresponding to gap characters as hidden in the column
1222 public void hideInsertionsFor(SequenceI sr)
1226 LOCK.writeLock().lock();
1227 List<int[]> inserts = sr.getInsertions();
1228 for (int[] r : inserts)
1230 hideColumns(r[0], r[1], true);
1234 LOCK.writeLock().unlock();
1239 * Unhides, and adds to the selection list, all hidden columns
1241 public void revealAllHiddenColumns(ColumnSelection sel)
1245 LOCK.writeLock().lock();
1246 if (hiddenColumns != null)
1248 for (int i = 0; i < hiddenColumns.size(); i++)
1250 int[] region = hiddenColumns.elementAt(i);
1251 for (int j = region[0]; j < region[1] + 1; j++)
1258 hiddenColumns = null;
1262 LOCK.writeLock().unlock();
1267 * Reveals, and marks as selected, the hidden column range with the given
1272 public void revealHiddenColumns(int start, ColumnSelection sel)
1276 LOCK.writeLock().lock();
1277 for (int i = 0; i < hiddenColumns.size(); i++)
1279 int[] region = hiddenColumns.elementAt(i);
1280 if (start == region[0])
1282 for (int j = region[0]; j < region[1] + 1; j++)
1287 hiddenColumns.removeElement(region);
1291 if (hiddenColumns.size() == 0)
1293 hiddenColumns = null;
1298 LOCK.writeLock().unlock();
1303 * removes intersection of position,length ranges in deletions from the
1304 * start,end regions marked in intervals.
1310 private boolean pruneIntervalVector(final List<int[]> shifts,
1311 Vector<int[]> intervals)
1313 boolean pruned = false;
1315 int j = intervals.size() - 1;
1317 int t = shifts.size() - 1;
1318 int hr[] = intervals.elementAt(i);
1319 int sr[] = shifts.get(s);
1320 while (i <= j && s <= t)
1322 boolean trailinghn = hr[1] >= sr[0];
1327 hr = intervals.elementAt(++i);
1335 int endshift = sr[0] + sr[1]; // deletion ranges - -ve means an insert
1336 if (endshift < hr[0] || endshift < sr[0])
1337 { // leadinghc disjoint or not a deletion
1340 sr = shifts.get(++s);
1348 boolean leadinghn = hr[0] >= sr[0];
1349 boolean leadinghc = hr[0] < endshift;
1350 boolean trailinghc = hr[1] < endshift;
1354 { // deleted hidden region.
1355 intervals.removeElementAt(i);
1360 hr = intervals.elementAt(i);
1366 hr[0] = endshift; // clip c terminal region
1367 leadinghn = !leadinghn;
1383 // sr contained in hr
1386 sr = shifts.get(++s);
1396 return pruned; // true if any interval was removed or modified by
1401 * remove any hiddenColumns or selected columns and shift remaining based on a
1402 * series of position, range deletions.
1406 public void pruneDeletions(List<int[]> shifts)
1410 LOCK.writeLock().lock();
1411 // delete any intervals intersecting.
1412 if (hiddenColumns != null)
1414 pruneIntervalVector(shifts, hiddenColumns);
1415 if (hiddenColumns != null && hiddenColumns.size() == 0)
1417 hiddenColumns = null;
1423 LOCK.writeLock().unlock();
1428 * Add gaps into the sequences aligned to profileseq under the given
1433 * - alignment to have gaps inserted into it
1435 * - alignment view where sequence corresponding to profileseq is
1437 * @return new HiddenColumns for new alignment view, with insertions into
1438 * profileseq marked as hidden.
1440 public static HiddenColumns propagateInsertions(SequenceI profileseq,
1441 AlignmentI al, AlignmentView input)
1445 char gc = al.getGapCharacter();
1446 Object[] alandhidden = input.getAlignmentAndHiddenColumns(gc);
1447 HiddenColumns nview = (HiddenColumns) alandhidden[1];
1448 SequenceI origseq = ((SequenceI[]) alandhidden[0])[profsqpos];
1449 nview.propagateInsertions(profileseq, al, origseq);
1456 * - sequence in al which corresponds to origseq
1458 * - alignment which is to have gaps inserted into it
1460 * - sequence corresponding to profileseq which defines gap map for
1463 private void propagateInsertions(SequenceI profileseq, AlignmentI al,
1466 char gc = al.getGapCharacter();
1467 // recover mapping between sequence's non-gap positions and positions
1469 pruneDeletions(ShiftList.parseMap(origseq.gapMap()));
1470 int[] viscontigs = al.getHiddenColumns().getVisibleContigs(0,
1471 profileseq.getLength());
1475 // add profile to visible contigs
1476 for (int v = 0; v < viscontigs.length; v += 2)
1478 if (viscontigs[v] > spos)
1480 StringBuffer sb = new StringBuffer();
1481 for (int s = 0, ns = viscontigs[v] - spos; s < ns; s++)
1485 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1487 SequenceI sqobj = al.getSequenceAt(s);
1488 if (sqobj != profileseq)
1490 String sq = al.getSequenceAt(s).getSequenceAsString();
1491 if (sq.length() <= spos + offset)
1494 int diff = spos + offset - sq.length() - 1;
1499 while ((diff = spos + offset - sq.length() - 1) > 0)
1502 // + ((diff >= sb.length()) ? sb.toString() : sb
1503 // .substring(0, diff));
1504 if (diff >= sb.length())
1506 sq += sb.toString();
1510 char[] buf = new char[diff];
1511 sb.getChars(0, diff, buf, 0);
1512 sq += buf.toString();
1516 sq += sb.toString();
1520 al.getSequenceAt(s).setSequence(
1521 sq.substring(0, spos + offset) + sb.toString()
1522 + sq.substring(spos + offset));
1526 // offset+=sb.length();
1528 spos = viscontigs[v + 1] + 1;
1530 if ((offset + spos) < profileseq.getLength())
1532 // pad the final region with gaps.
1533 StringBuffer sb = new StringBuffer();
1534 for (int s = 0, ns = profileseq.getLength() - spos - offset; s < ns; s++)
1538 for (int s = 0, ns = al.getHeight(); s < ns; s++)
1540 SequenceI sqobj = al.getSequenceAt(s);
1541 if (sqobj == profileseq)
1545 String sq = sqobj.getSequenceAsString();
1547 int diff = origseq.getLength() - sq.length();
1551 // + ((diff >= sb.length()) ? sb.toString() : sb
1552 // .substring(0, diff));
1553 if (diff >= sb.length())
1555 sq += sb.toString();
1559 char[] buf = new char[diff];
1560 sb.getChars(0, diff, buf, 0);
1561 sq += buf.toString();
1563 diff = origseq.getLength() - sq.length();
1570 * remove any hiddenColumns or selected columns and shift remaining based on a
1571 * series of position, range deletions.
1575 private void pruneDeletions(ShiftList deletions)
1577 if (deletions != null)
1579 final List<int[]> shifts = deletions.getShifts();
1580 if (shifts != null && shifts.size() > 0)
1582 pruneDeletions(shifts);
1584 // and shift the rest.
1585 this.compensateForEdits(deletions);
1591 * Adjust hidden column boundaries based on a series of column additions or
1592 * deletions in visible regions.
1594 * @param shiftrecord
1597 private ShiftList compensateForEdits(ShiftList shiftrecord)
1599 if (shiftrecord != null)
1601 final List<int[]> shifts = shiftrecord.getShifts();
1602 if (shifts != null && shifts.size() > 0)
1605 for (int i = 0, j = shifts.size(); i < j; i++)
1607 int[] sh = shifts.get(i);
1608 compensateForDelEdits(shifted + sh[0], sh[1]);
1612 return shiftrecord.getInverse();
1618 * Returns a hashCode built from hidden column ranges
1621 public int hashCode()
1625 LOCK.readLock().lock();
1627 if (hiddenColumns != null)
1629 for (int[] hidden : hiddenColumns)
1631 hashCode = 31 * hashCode + hidden[0];
1632 hashCode = 31 * hashCode + hidden[1];
1639 LOCK.readLock().unlock();
1644 * Hide columns corresponding to the marked bits
1647 * - columns map to bits starting from zero
1649 public void hideMarkedBits(BitSet inserts)
1653 LOCK.writeLock().lock();
1654 for (int firstSet = inserts
1655 .nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
1656 .nextSetBit(lastSet))
1658 lastSet = inserts.nextClearBit(firstSet);
1659 hideColumns(firstSet, lastSet - 1, true);
1663 LOCK.writeLock().unlock();
1670 * BitSet where hidden columns will be marked
1672 public void markHiddenRegions(BitSet inserts)
1676 LOCK.readLock().lock();
1677 if (hiddenColumns == null)
1681 for (int[] range : hiddenColumns)
1683 inserts.set(range[0], range[1] + 1);
1688 LOCK.readLock().unlock();
1693 * Calculate the visible start and end index of an alignment.
1696 * full alignment width
1697 * @return integer array where: int[0] = startIndex, and int[1] = endIndex
1699 public int[] getVisibleStartAndEndIndex(int width)
1703 LOCK.readLock().lock();
1704 int[] alignmentStartEnd = new int[] { 0, width - 1 };
1705 int startPos = alignmentStartEnd[0];
1706 int endPos = alignmentStartEnd[1];
1708 int[] lowestRange = new int[] { -1, -1 };
1709 int[] higestRange = new int[] { -1, -1 };
1711 if (hiddenColumns == null)
1713 return new int[] { startPos, endPos };
1716 for (int[] hiddenCol : hiddenColumns)
1718 lowestRange = (hiddenCol[0] <= startPos) ? hiddenCol : lowestRange;
1719 higestRange = (hiddenCol[1] >= endPos) ? hiddenCol : higestRange;
1722 if (lowestRange[0] == -1 && lowestRange[1] == -1)
1724 startPos = alignmentStartEnd[0];
1728 startPos = lowestRange[1] + 1;
1731 if (higestRange[0] == -1 && higestRange[1] == -1)
1733 endPos = alignmentStartEnd[1];
1737 endPos = higestRange[0] - 1;
1739 return new int[] { startPos, endPos };
1742 LOCK.readLock().unlock();
1748 * Finds the hidden region (if any) which starts or ends at res
1751 * visible residue position, unadjusted for hidden columns
1752 * @return region as [start,end] or null if no matching region is found
1754 public int[] getRegionWithEdgeAtRes(int res)
1758 LOCK.readLock().lock();
1759 int adjres = adjustForHiddenColumns(res);
1761 int[] reveal = null;
1762 if (hiddenColumns != null)
1764 for (int[] region : hiddenColumns)
1766 if (adjres + 1 == region[0] || adjres - 1 == region[1])
1776 LOCK.readLock().unlock();