import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
+/**
+ * This class manages the collection of hidden columns associated with an
+ * alignment. To iterate over the collection, or over visible columns/regions,
+ * use an iterator obtained from one of:
+ *
+ * - getBoundedIterator: iterates over the hidden regions, within some bounds,
+ * returning absolute positions
+ *
+ * - getBoundedStartIterator: iterates over the start positions of hidden
+ * regions, within some bounds, returning visible positions
+ *
+ * - getVisContigsIterator: iterates over visible regions in a range, returning
+ * absolute positions
+ *
+ * - getVisibleColsIterator: iterates over the visible *columns*
+ *
+ * For performance reasons, provide bounds where possible.
+ *
+ * @author kmourao
+ *
+ */
public class HiddenColumns
{
private static final int HASH_MULTIPLIER = 31;
* Methods which change the hiddenColumns collection. These methods should
* use a writeLock to prevent other threads accessing the hiddenColumns
* collection while changes are being made. They should also reset the hidden
- * columns cursor, and either update the hidden columns count, or set it to 0.
+ * columns cursor, and either update the hidden columns count, or set it to 0
+ * (so that it will later be updated when needed).
*/
/**
wasAlreadyLocked = true;
}
+ int previndex = 0;
+ int prevHiddenCount = 0;
+ int regionindex = 0;
if (hiddenColumns == null)
{
hiddenColumns = new ArrayList<>();
}
+ else
+ {
+ // set up cursor reset values
+ HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
+ regionindex = cursorPos.getRegionIndex();
+
+ if (regionindex > 0)
+ {
+ // get previous index and hidden count for updating the cursor later
+ previndex = regionindex - 1;
+ int[] prevRegion = hiddenColumns.get(previndex);
+ prevHiddenCount = cursorPos.getHiddenSoFar()
+ - (prevRegion[1] - prevRegion[0] + 1);
+ }
+ }
/*
* new range follows everything else; check first to avoid looping over whole hiddenColumns collection
* appropriate
*/
boolean added = false;
- for (int i = 0; !added && i < hiddenColumns.size(); i++)
+ if (regionindex > 0)
+ {
+ added = insertRangeAtRegion(regionindex - 1, start, end);
+ }
+ if (!added && regionindex < hiddenColumns.size())
{
- added = insertRangeAtRegion(i, start, end);
- } // for
+ insertRangeAtRegion(regionindex, start, end);
+ }
}
- if (!wasAlreadyLocked)
- {
- cursor.resetCursor(hiddenColumns);
- // reset the number of columns so they will be recounted
- numColumns = 0;
- }
+ // reset the cursor to just before our insertion point: this saves
+ // a lot of reprocessing in large alignments
+ cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
+
+ // reset the number of columns so they will be recounted
+ numColumns = 0;
+
} finally
{
if (!wasAlreadyLocked)
break;
}
region[1] = Math.max(nextRegion[1], end);
+
+ // in theory this is faster than hiddenColumns.remove(i+1)
+ // benchmarking results a bit ambivalent
hiddenColumns.subList(i + 1, i + 2).clear();
}
added = true;
{
// numColumns is out of date, so recalculate
int size = 0;
- if (hiddenColumns != null)
+
+ Iterator<int[]> it = hiddenColumns.iterator();
+ while (it.hasNext())
{
- Iterator<int[]> it = hiddenColumns.iterator();
- while (it.hasNext())
- {
- int[] range = it.next();
- size += range[1] - range[0] + 1;
- }
+ int[] range = it.next();
+ size += range[1] - range[0] + 1;
}
+
numColumns = size;
}
if (hiddenColumns != null)
{
- result += cursor.getHiddenOffset(column).getHiddenSoFar();
+ result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
}
return result;
* @param alPos
* the absolute (visible) alignmentPosition to find the next hidden
* column for
+ * @return the index of the next hidden column, or alPos if there is no next
+ * hidden column
*/
public int getHiddenBoundaryRight(int alPos)
{
if (regionindex > -1 && regionindex < hiddenColumns.size())
{
int[] region = hiddenColumns.get(regionindex);
- if (column >= region[0] && column <= region[1])
+ // already know that column <= region[1] as cursor returns containing
+ // region or region to right
+ if (column >= region[0])
{
return false;
}
*/
public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
{
- makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
+ if (alignmentAnnotation != null
+ && alignmentAnnotation.annotations != null)
+ {
+ makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
alignmentAnnotation);
+ }
}
/**
int startFrom = start;
int endAt = end;
- if (alignmentAnnotation.annotations != null)
+ if (alignmentAnnotation != null
+ && alignmentAnnotation.annotations != null)
{
if (hiddenColumns != null && hiddenColumns.size() > 0)
{
try
{
LOCK.readLock().lock();
+
+ // we don't use getSize()>0 here because it has to iterate over
+ // the full hiddenColumns collection and so will be much slower
return hiddenColumns != null && hiddenColumns.size() > 0;
} finally
{
/**
*
- * @return true if there are more than one set of columns hidden
+ * @return true if there is more than one hidden column region
*/
- public boolean hasManyHiddenColumns()
+ public boolean hasMultiHiddenColumnRegions()
{
try
{
higestRange = (range[1] >= endPos) ? range : higestRange;
}
- if (lowestRange[0] == -1 && lowestRange[1] == -1)
+ if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
{
startPos = alignmentStartEnd[0];
}
startPos = lowestRange[1] + 1;
}
- if (higestRange[0] == -1 && higestRange[1] == -1)
+ if (higestRange[0] == -1) // includes (higestRange[1] == -1)
{
endPos = alignmentStartEnd[1];
}
{
reveal = hiddenColumns.get(regionindex);
}
- // or try the next region
- else
- {
- regionindex++;
- if (regionindex < hiddenColumns.size()
- && hiddenColumns.get(regionindex)[0] == adjres + 1)
- {
- reveal = hiddenColumns.get(regionindex);
- }
- }
}
return reveal;
try
{
LOCK.readLock().lock();
- return new BoundedHiddenColsIterator(hiddenColumns);
+ return new HiddenColsIterator(hiddenColumns);
} finally
{
LOCK.readLock().unlock();
try
{
LOCK.readLock().lock();
- return new BoundedHiddenColsIterator(start, end, hiddenColumns);
+ return new HiddenColsIterator(start, end, hiddenColumns);
} finally
{
LOCK.readLock().unlock();
try
{
LOCK.readLock().lock();
- return new BoundedStartRegionIterator(start, end, hiddenColumns);
+
+ // get absolute position of column in alignment
+ int absoluteStart = adjustForHiddenColumns(start);
+
+ // Get cursor position and supply it to the iterator:
+ // Since we want visible region start, we look for a cursor for the
+ // (absoluteStart-1), then if absoluteStart is the start of a visible
+ // region we'll get the cursor pointing to the region before, which is
+ // what we want
+ HiddenCursorPosition pos = cursor
+ .findRegionForColumn(absoluteStart - 1);
+
+ return new BoundedStartRegionIterator(pos, start, end,
+ hiddenColumns);
} finally
{
LOCK.readLock().unlock();
* boundaries
*
* @param start
- * (first column inclusive from 0)
+ * first column, inclusive from 0
* @param end
- * (last column - not inclusive)
- */
- public Iterator<int[]> getVisContigsIterator(int start, int end)
- {
- try
- {
- LOCK.readLock().lock();
- return new VisibleContigsIterator(start, end, hiddenColumns);
- } finally
- {
- LOCK.readLock().unlock();
- }
- }
-
- /**
- * return an iterator over visible segments between the given start and end
- * boundaries
- *
- * @param start
- * (first column - inclusive from 0)
- * @param end
- * (last column - inclusive)
+ * last column - not inclusive
* @param useVisibleCoords
* if true, start and end are visible column positions, not absolute
- * positions
+ * positions*
*/
- public Iterator<int[]> getVisibleBlocksIterator(int start, int end,
+ public Iterator<int[]> getVisContigsIterator(int start, int end,
boolean useVisibleCoords)
{
+ int adjstart = start;
+ int adjend = end;
if (useVisibleCoords)
{
- // TODO
- // we should really just convert start and end here with
- // adjustForHiddenColumns
- // and then create a VisibleContigsIterator
- // but without a cursor this will be horribly slow in some situations
- // ... so until then...
- // return new VisibleBlocksVisBoundsIterator(start, end, true);
- try
- {
- LOCK.readLock().lock();
- int adjstart = adjustForHiddenColumns(start);
- int adjend = adjustForHiddenColumns(end);
- return new VisibleContigsIterator(adjstart, adjend + 1,
- hiddenColumns);
- } finally
- {
- LOCK.readLock().unlock();
- }
- }
- else
- {
- try
- {
- LOCK.readLock().lock();
- return new VisibleContigsIterator(start, end + 1, hiddenColumns);
- } finally
- {
- LOCK.readLock().unlock();
- }
- }
- }
-
- /**
- * An iterator which iterates over visible regions in a range. The range is
- * specified in terms of visible column positions. Provides a special
- * "endsAtHidden" indicator to allow callers to determine if the final visible
- * column is adjacent to a hidden region.
- */
- public class VisibleBlocksVisBoundsIterator implements Iterator<int[]>
- {
- private List<int[]> vcontigs = new ArrayList<>();
-
- private int currentPosition = 0;
-
- private boolean endsAtHidden = false;
-
- /**
- * Constructor for iterator over visible regions in a range.
- *
- * @param start
- * start position in terms of visible column position
- * @param end
- * end position in terms of visible column position
- * @param usecopy
- * whether to use a local copy of hidden columns
- */
- VisibleBlocksVisBoundsIterator(int start, int end, boolean usecopy)
- {
- /* actually this implementation always uses a local copy but this may change in future */
- try
- {
- if (usecopy)
- {
- LOCK.readLock().lock();
- }
-
- if (hiddenColumns != null && hiddenColumns.size() > 0)
- {
- int blockStart = start;
- int blockEnd = end;
- int hiddenSoFar = 0;
- int visSoFar = 0;
-
- // iterate until a region begins within (start,end]
- int i = 0;
- while ((i < hiddenColumns.size())
- && (hiddenColumns.get(i)[0] <= blockStart + hiddenSoFar))
- {
- hiddenSoFar += hiddenColumns.get(i)[1] - hiddenColumns.get(i)[0]
- + 1;
- i++;
- }
-
- blockStart += hiddenSoFar; // convert start to absolute position
- blockEnd += hiddenSoFar; // convert end to absolute position
-
- // iterate from start to end, adding each visible region. Positions
- // are
- // absolute, and all hidden regions which overlap [start,end] are
- // used.
- while (i < hiddenColumns.size()
- && (hiddenColumns.get(i)[0] <= blockEnd))
- {
- int[] region = hiddenColumns.get(i);
-
- // end position of this visible region is either just before the
- // start of the next hidden region, or the absolute position of
- // 'end', whichever is lowest
- blockEnd = Math.min(blockEnd, region[0] - 1);
-
- vcontigs.add(new int[] { blockStart, blockEnd });
-
- visSoFar += blockEnd - blockStart + 1;
-
- // next visible region starts after this hidden region
- blockStart = region[1] + 1;
-
- hiddenSoFar += region[1] - region[0] + 1;
-
- // reset blockEnd to absolute position of 'end', assuming we've now
- // passed all hidden regions before end
- blockEnd = end + hiddenSoFar;
-
- i++;
- }
- if (visSoFar < end - start + 1)
- {
- // the number of visible columns we've accounted for is less than
- // the number specified by end-start; work out the end position of
- // the last visible region
- blockEnd = blockStart + end - start - visSoFar;
- vcontigs.add(new int[] { blockStart, blockEnd });
-
- // if the last visible region ends at the next hidden region, set
- // endsAtHidden=true
- if (i < hiddenColumns.size()
- && hiddenColumns.get(i)[0] - 1 == blockEnd)
- {
- endsAtHidden = true;
- }
- }
- }
- else
- {
- // there are no hidden columns, return a single visible contig
- vcontigs.add(new int[] { start, end });
- endsAtHidden = false;
- }
- } finally
- {
- if (usecopy)
- {
- LOCK.readLock().unlock();
- }
- }
- }
-
- @Override
- public boolean hasNext()
- {
- return (currentPosition < vcontigs.size());
+ adjstart = adjustForHiddenColumns(start);
+ adjend = adjustForHiddenColumns(end);
}
- @Override
- public int[] next()
+ try
{
- int[] result = vcontigs.get(currentPosition);
- currentPosition++;
- return result;
- }
-
- public boolean endsAtHidden()
+ LOCK.readLock().lock();
+ return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
+ } finally
{
- return endsAtHidden;
+ LOCK.readLock().unlock();
}
}
}