/** * Generates a BAM index file, either textual or binary, from an input BAI file. Only used for * testing, but located here for visibility into CachingBAMFileIndex. * * @param output BAM Index (.bai) file (or bai.txt file when text) * @param textOutput Whether to create text output or binary */ public static void createAndWriteIndex( final File input, final File output, final boolean textOutput) { // content is from an existing bai file. final CachingBAMFileIndex existingIndex = new CachingBAMFileIndex(input, null); final int n_ref = existingIndex.getNumberOfReferences(); final BAMIndexWriter outputWriter; if (textOutput) { outputWriter = new TextualBAMIndexWriter(n_ref, output); } else { outputWriter = new BinaryBAMIndexWriter(n_ref, output); } // write the content one reference at a time try { for (int i = 0; i < n_ref; i++) { outputWriter.writeReference(existingIndex.getQueryResults(i)); } outputWriter.writeNoCoordinateRecordCount(existingIndex.getNoCoordinateCount()); outputWriter.close(); } catch (final Exception e) { throw new SAMException("Exception creating BAM index", e); } }
/** write out any references between the currentReference and the nextReference */ private void advanceToReference(final int nextReference) { while (currentReference < nextReference) { final BAMIndexContent content = indexBuilder.processReference(currentReference); outputWriter.writeReference(content); currentReference++; if (currentReference < numReferences) { indexBuilder.startNewReference(); } } }
/** * After all the alignment records have been processed, finish is called. Writes any final * information and closes the output file. */ public void finish() { // process any remaining references advanceToReference(numReferences); outputWriter.writeNoCoordinateRecordCount(indexBuilder.getNoCoordinateRecordCount()); outputWriter.close(); }