/** * 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); } }
@Test(enabled = false, dataProvider = "indexComparisonData") /** Test linear index at all references and windows, comparing with existing index */ public void compareLinearIndex(String testName, String bamFile, String bamIndexFile) throws IOException { // compare index generated from bamFile with existing bamIndex file // by testing all the references' windows and comparing the counts // 1. generate bai file // 2. count its references // 3. count bamIndex references comparing counts // 1. generate bai file File bam = new File(bamFile); assertTrue(bam.exists(), testName + " input bam file doesn't exist: " + bamFile); File indexFile1 = createIndexFile(bam); assertTrue( indexFile1.exists(), testName + " generated bam file's index doesn't exist: " + indexFile1); // 2. count its references File indexFile2 = new File(bamIndexFile); assertTrue(indexFile2.exists(), testName + " input index file doesn't exist: " + indexFile2); final CachingBAMFileIndex existingIndex1 = new CachingBAMFileIndex(indexFile1, null); // todo null sequence dictionary? final CachingBAMFileIndex existingIndex2 = new CachingBAMFileIndex(indexFile2, null); final int n_ref = existingIndex1.getNumberOfReferences(); assertEquals(n_ref, existingIndex2.getNumberOfReferences()); final SAMFileReader reader1 = new SAMFileReader(bam, indexFile1, false); final SAMFileReader reader2 = new SAMFileReader(bam, indexFile2, false); System.out.println( "Comparing " + n_ref + " references in " + indexFile1 + " and " + indexFile2); for (int i = 0; i < n_ref; i++) { final BAMIndexContent content1 = existingIndex1.getQueryResults(i); final BAMIndexContent content2 = existingIndex2.getQueryResults(i); if (content1 == null) { assertTrue( content2 == null, "No content for 1st bam index, but content for second at reference" + i); continue; } int[] counts1 = new int[LinearIndex.MAX_LINEAR_INDEX_SIZE]; int[] counts2 = new int[LinearIndex.MAX_LINEAR_INDEX_SIZE]; LinearIndex li1 = content1.getLinearIndex(); LinearIndex li2 = content2.getLinearIndex(); // todo not li1 and li2 sizes may differ. Implies 0's in the smaller index windows // 3. count bamIndex references comparing counts int baiSize = Math.max(li1.size(), li2.size()); for (int win = 0; win < baiSize; win++) { counts1[win] = countAlignmentsInWindow(i, win, reader1, 0); counts2[win] = countAlignmentsInWindow(i, win, reader2, counts1[win]); assertEquals( counts2[win], counts1[win], "Counts don't match for reference " + i + " window " + win); } } indexFile1.deleteOnExit(); }