# Class BAMIndexMerger

java.lang.Object
htsjdk.samtools.IndexMerger<AbstractBAMFileIndex>
htsjdk.samtools.BAMIndexMerger

public final class BAMIndexMerger extends IndexMerger<AbstractBAMFileIndex>
Merges BAM index files for (headerless) parts of a BAM file into a single index file. The index files must have been produced using `BAMIndexer` with `fillInUninitializedValues` set to false. A partitioned BAM is a directory containing the following files:

1. A file named header containing all header bytes in BAM format.
2. Zero or more files named part-00000, part-00001, ... etc, containing a list of alignments in BAM format.
3. A file named terminator containing a BGZF end-of-file marker block.

If an index is required, a BAM index can be generated for each (headerless) part file. These files should be named .part-00000.bai, .part-00001.bai, ... etc. Note the leading . to make the files hidden. This format has the following properties:

- Parts and their indexes may be written in parallel, since one part file can be written independently of the others.
- A BAM file can be created from a partitioned BAM file by concatenating all the non-hidden files (header, part-00000, part-00001, ..., terminator).
- A BAM index can be created from a partitioned BAM file by merging all of the hidden files with a .bai suffix. Note that this is not a simple file concatenation operation. See `BAMIndexMerger`.

## Field Summary

### Fields inherited from class IndexMerger

`out, partLengths`

## Constructor Summary

Constructors
Constructor
Description
`BAMIndexMerger(OutputStream out, long headerLength)`

## Method Summary

Modifier and Type
Method
Description
`void`
`finish(long dataFileLength)`
Finish merging the indexes, and close the output stream.
`static BinningIndexContent.BinList`
`mergeBins(List<BinningIndexContent.BinList> binLists, long[] offsets)`
Merge bins for (headerless) BAM file parts.
`static LinearIndex`
`mergeLinearIndexes(int referenceSequence, List<LinearIndex> linearIndexes, long[] offsets)`
Merge linear indexes for (headerless) BAM file parts.
`static AbstractBAMFileIndex`
`openIndex(SeekableStream stream, SAMSequenceDictionary dictionary)`
`void`
`processIndex(AbstractBAMFileIndex index, long partLength)`
Process the next index and add to the merged index.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### BAMIndexMerger

public BAMIndexMerger(OutputStream out, long headerLength)

## Method Details

### processIndex

public void processIndex(AbstractBAMFileIndex index, long partLength)
Description copied from class: `IndexMerger`
Process the next index and add to the merged index.

Specified by:
  `processIndex` in class `IndexMerger<AbstractBAMFileIndex>`
Parameters:
  `index` - the index to merge
  `partLength` - the length of the part file corresponding to the index, in bytes.

### finish

public void finish(long dataFileLength)
Description copied from class: `IndexMerger`
Finish merging the indexes, and close the output stream.

Specified by:
  `finish` in class `IndexMerger<AbstractBAMFileIndex>`
Parameters:
  `dataFileLength` - the length of the total data file, in bytes.

### openIndex

public static AbstractBAMFileIndex openIndex(SeekableStream stream, SAMSequenceDictionary dictionary)

### mergeBins

public static BinningIndexContent.BinList mergeBins(List<BinningIndexContent.BinList> binLists, long[] offsets)
Merge bins for (headerless) BAM file parts.

Parameters:
  `binLists` - the bins to merge
  `offsets` - bin i will be shifted by offset i
Returns:
  the merged bins

### mergeLinearIndexes

public static LinearIndex mergeLinearIndexes(int referenceSequence, List<LinearIndex> linearIndexes, long[] offsets)
Merge linear indexes for (headerless) BAM file parts.

Parameters:
  `referenceSequence` - the reference sequence number for the linear indexes being merged
  `linearIndexes` - the linear indexes to merge
  `offsets` - linear index i will be shifted by offset i
Returns:
  the merged linear index