@Test public void testUnionOfEmptyFileSpans() { GATKBAMFileSpan empty1 = new GATKBAMFileSpan(); GATKBAMFileSpan empty2 = new GATKBAMFileSpan(); GATKBAMFileSpan union = empty1.union(empty2); Assert.assertEquals(union.getGATKChunks().size(),0,"Elements inserted in union of two empty sets"); }
@Test public void testSubtractionOfEmptyChunkLists() { GATKBAMFileSpan regionOne = new GATKBAMFileSpan(); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(); GATKBAMFileSpan subtraction = regionOne.minus(regionTwo); Assert.assertEquals(subtraction.getGATKChunks().size(),0,"Elements inserted in subtraction of two empty sets"); }
/** * Get a GATKChunk representing the "extent" of this file span, from the start of the first * chunk to the end of the last chunk.The chunks list must be sorted in order to use this method. * * @return a GATKChunk representing the extent of this file span, or a GATKChunk representing * a span of size 0 if there are no chunks */ public GATKChunk getExtent() { validateSorted(); // TODO: defensive measure: may be unnecessary List<Chunk> chunks = getChunks(); if ( chunks.isEmpty() ) { return new GATKChunk(0L, 0L); } return new GATKChunk(chunks.get(0).getChunkStart(), chunks.get(chunks.size() - 1).getChunkEnd()); }
/** * Returns true if any of the file spans in this FilePointer overlap their counterparts in * the other FilePointer. "Overlap" is defined as having an overlapping extent (the region * from the start of the first chunk to the end of the last chunk). * * @param other the FilePointer against which to check overlap with this FilePointer * @return true if any file spans overlap their counterparts in other, otherwise false */ public boolean hasFileSpansOverlappingWith( FilePointer other ) { for ( Map.Entry<SAMReaderID, SAMFileSpan> thisFilePointerEntry : fileSpans.entrySet() ) { GATKBAMFileSpan thisFileSpan = new GATKBAMFileSpan(thisFilePointerEntry.getValue()); SAMFileSpan otherEntry = other.fileSpans.get(thisFilePointerEntry.getKey()); if ( otherEntry == null ) { continue; // no counterpart for this file span in other } GATKBAMFileSpan otherFileSpan = new GATKBAMFileSpan(otherEntry); if ( thisFileSpan.getExtent().overlaps(otherFileSpan.getExtent()) ) { return true; } } return false; }
@Test public void testIntersectionOfEmptyFileSpans() { GATKBAMFileSpan empty1 = new GATKBAMFileSpan(); GATKBAMFileSpan empty2 = new GATKBAMFileSpan(); GATKBAMFileSpan intersection = empty1.intersection(empty2); Assert.assertEquals(intersection.getGATKChunks().size(),0,"Elements inserted in intersection of two empty sets"); }
/** * Create a span from the given start point to the end of the file. * @param startOfRegion Start of the region, in encoded coordinates (block start << 16 & block offset). * @return A file span from the given point to the end of the file. */ private GATKBAMFileSpan createSpanToEndOfFile(final long startOfRegion) { return new GATKBAMFileSpan(new GATKChunk(startOfRegion,Long.MAX_VALUE)); }
if(getGATKChunks().size() == 0 && other.getGATKChunks().size() == 0) return new GATKBAMFileSpan(); unmergedUnion.addAll(getGATKChunks()); unmergedUnion.addAll(other.getGATKChunks()); Collections.sort(unmergedUnion); mergedUnion.add(currentChunk); return new GATKBAMFileSpan(mergedUnion.toArray(new GATKChunk[mergedUnion.size()]));
public String toString() { StringBuilder builder = new StringBuilder(); for(GATKChunk chunk: getGATKChunks()) builder.append(String.format("%s;",chunk)); return builder.toString(); }
if(!fileSpan.isEmpty()) { ByteBuffer buffer = allocateByteBuffer(2*INT_SIZE_IN_BYTES + INT_SIZE_IN_BYTES + fileSpan.getGATKChunks().size()*LONG_SIZE_IN_BYTES*2); buffer.putInt(binStart); buffer.putInt(binStop); buffer.putInt(fileSpan.getGATKChunks().size()); for(GATKChunk chunk: fileSpan.getGATKChunks()) { buffer.putLong(chunk.getChunkStart()); buffer.putLong(chunk.getChunkEnd()); maxChunkCount = Math.max(maxChunkCount,fileSpan.getGATKChunks().size());
/** * Roll the next element in the iterator into the combined entry. * @param combined Entry into which to roll the next element. * @param iterators Sources of next elements. */ private void mergeElementsInto(final FilePointer combined, Iterator<Map.Entry<SAMReaderID,SAMFileSpan>>... iterators) { if(iterators.length == 0) throw new ReviewedGATKException("Tried to add zero elements to an existing file pointer."); Map.Entry<SAMReaderID,SAMFileSpan> initialElement = iterators[0].next(); GATKBAMFileSpan fileSpan = (GATKBAMFileSpan)initialElement.getValue(); for(int i = 1; i < iterators.length; i++) fileSpan = fileSpan.union((GATKBAMFileSpan)iterators[i].next().getValue()); combined.addFileSpans(initialElement.getKey(),fileSpan); }
/** * Gets the constituent chunks stored in this span. * @return An unmodifiable list of chunks. */ public List<GATKChunk> getGATKChunks() { List<GATKChunk> gatkChunks = new ArrayList<GATKChunk>(); for(Chunk chunk: getChunks()) gatkChunks.add(new GATKChunk(chunk)); return gatkChunks; }
/** * Create a new GATKBAMFileSpan from an existing BAMFileSpan. * @param sourceFileSpan */ public GATKBAMFileSpan(SAMFileSpan sourceFileSpan) { if(!(sourceFileSpan instanceof BAMFileSpan)) throw new SAMException("Unable to create GATKBAMFileSpan from a SAMFileSpan. Please submit a BAMFileSpan instead"); BAMFileSpan sourceBAMFileSpan = (BAMFileSpan)sourceFileSpan; for(Chunk chunk: sourceBAMFileSpan.getChunks()) add(chunk instanceof GATKChunk ? chunk : new GATKChunk(chunk)); }
@Test public void testIntersectionOfStrictSubset() { GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(0,1<<16)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(0,2<<16)); GATKBAMFileSpan intersection = regionOne.intersection(regionTwo); Assert.assertEquals(intersection.getGATKChunks().size(),1,"No intersection found between two partially overlapping filespans"); Assert.assertEquals(intersection.getGATKChunks().get(0),new GATKChunk(0<<16,1<<16),"Determined intersection is incorrect."); // Make sure intersection is symmetric intersection = regionTwo.intersection(regionOne); Assert.assertEquals(intersection.getGATKChunks().size(),1,"No intersection found between two partially overlapping filespans"); Assert.assertEquals(intersection.getGATKChunks().get(0),new GATKChunk(0<<16,1<<16),"Determined intersection is incorrect."); }
nextScheduleEntry.addFileSpan(reader,new GATKBAMFileSpan(chunks));
Iterator<GATKChunk> thisIterator = getGATKChunks().iterator(); Iterator<GATKChunk> otherIterator = other.getGATKChunks().iterator(); subtracted.add(thisIterator.next()); return new GATKBAMFileSpan(subtracted.toArray(new GATKChunk[subtracted.size()]));
BAMAccessPlan(final SAMReaderID reader, final BlockInputStream inputStream, GATKBAMFileSpan fileSpan) { this.reader = reader; this.inputStream = inputStream; this.positions = fileSpan.getGATKChunks(); initialize(); }
/** * Validates the list of chunks to ensure that they appear in sorted order. */ private void validateSorted() { List<Chunk> chunks = getChunks(); for ( int i = 1; i < chunks.size(); i++ ) { if ( chunks.get(i).getChunkStart() < chunks.get(i-1).getChunkEnd() ) { throw new ReviewedGATKException(String.format("Chunk list is unsorted; chunk %s is before chunk %s", chunks.get(i-1), chunks.get(i))); } } }
@Test public void testUnionOfNonOverlappingFileSpans() { GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(0,65535)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|65535)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),2,"Discontiguous elements were merged"); Assert.assertEquals(union.getGATKChunks().get(0),regionOne.getGATKChunks().get(0),"Wrong chunk was first in list"); Assert.assertEquals(union.getGATKChunks().get(1),regionTwo.getGATKChunks().get(0),"Wrong chunk was second in list"); }
@Test public void testSubtractionOfPartialOverlap() { GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(0,2<<16)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,3<<16)); GATKBAMFileSpan subtraction = regionOne.minus(regionTwo); Assert.assertEquals(subtraction.getGATKChunks().size(),1,"Incorrect size in partial subset subtraction of region"); Assert.assertEquals(subtraction.getGATKChunks().get(0),new GATKChunk(0<<16,1<<16),"Determined subtraction is incorrect."); } }