/** * 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); }
List<GATKChunk> unmergedChunks = fileChunksEntry.getValue(); mergedFileSpans.put(fileChunksEntry.getKey(), (new GATKBAMFileSpan(unmergedChunks.toArray(new GATKChunk[unmergedChunks.size()]))).union(new GATKBAMFileSpan()));
@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 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 testUnionOfFileSpansFirstRegionEndsWithinSecondRegion() { // Region 1 ends within Region 2: // |---2----| // |---1----| GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(0,(1<<16)|32767)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|65535)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),1,"Elements to be merged were not."); Assert.assertEquals(union.getGATKChunks().get(0),new GATKChunk(0,(1<<16)|65535)); }
@Test public void testUnionOfFileSpansFirstRegionEndsAtSecondRegionEnd() { // Region 1 ends at Region 2 end: // |---2----| // |---1-----------| GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(0,(1<<16)|65535)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|65535)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),1,"Elements to be merged were not."); Assert.assertEquals(union.getGATKChunks().get(0),new GATKChunk(0,(1<<16)|65535)); }
@Test public void testUnionOfFileSpansFirstRegionEqualToSecondRegion() { // Region 1 and Region 2 represent the same region: // |---2----| // |---1----| GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|65535)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|65535)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),1,"Elements to be merged were not."); Assert.assertEquals(union.getGATKChunks().get(0),new GATKChunk(1<<16,(1<<16)|65535)); }
@Test public void testUnionOfFileSpansFirstRegionEndsAfterSecondRegionEnd() { // Region 1 ends after Region 2 end: // |---2----| // |---1---------------| GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(0,(1<<16)|65535)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|32767)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),1,"Elements to be merged were not."); Assert.assertEquals(union.getGATKChunks().get(0),new GATKChunk(0,(1<<16)|65535)); }
@Test public void testUnionOfContiguousFileSpans() { // Region 1 ends at position adjacent to Region 2 start: // |---1----|---2----| GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(0,1<<16)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|65535)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),1,"Elements to be merged were not."); Assert.assertEquals(union.getGATKChunks().get(0),new GATKChunk(0,(1<<16)|65535)); }
@Test public void testUnionOfFileSpansFirstRegionStartsAtSecondRegionStart() { // Region 1 starts at Region 2 start, but ends before Region 2: // |---2--------| // |---1----| GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|32767)); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,(1<<16)|65535)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),1,"Elements to be merged were not."); Assert.assertEquals(union.getGATKChunks().get(0),new GATKChunk(1<<16,(1<<16)|65535)); }
@Test public void testUnionOfStringOfFileSpans() { GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk[] { new GATKChunk(0,1<<16), new GATKChunk(2<<16,3<<16) }); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,2<<16)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),1,"Elements to be merged were not."); Assert.assertEquals(union.getGATKChunks().get(0),new GATKChunk(0,3<<16)); }
@Test public void testUnionAllFileSpansAdded() { GATKBAMFileSpan regionOne = new GATKBAMFileSpan(new GATKChunk[] { new GATKChunk(0,1<<16), new GATKChunk(2<<16,3<<16), new GATKChunk(20<<16,21<<16) }); GATKBAMFileSpan regionTwo = new GATKBAMFileSpan(new GATKChunk(1<<16,2<<16)); GATKBAMFileSpan union = regionOne.union(regionTwo); Assert.assertEquals(union.getGATKChunks().size(),2,"Elements to be merged were not."); Assert.assertEquals(union.getGATKChunks().get(0),new GATKChunk(0,3<<16)); Assert.assertEquals(union.getGATKChunks().get(1),new GATKChunk(20<<16,21<<16)); }