/** * Main method for the program. Checks that all input files are present and readable and that the * output file can be written to. Then iterates through all the records generating a BAM Index, * then writes the bai file. */ protected int doWork() { try { inputUrl = new URL(INPUT); } catch (java.net.MalformedURLException e) { inputFile = new File(INPUT); } // set default output file - input-file.bai if (OUTPUT == null) { final String baseFileName; if (inputUrl != null) { String path = inputUrl.getPath(); int lastSlash = path.lastIndexOf("/"); baseFileName = path.substring(lastSlash + 1, path.length()); } else { baseFileName = inputFile.getAbsolutePath(); } if (baseFileName.endsWith(BamFileIoUtils.BAM_FILE_EXTENSION)) { final int index = baseFileName.lastIndexOf("."); OUTPUT = new File(baseFileName.substring(0, index) + BAMIndex.BAMIndexSuffix); } else { OUTPUT = new File(baseFileName + BAMIndex.BAMIndexSuffix); } } IOUtil.assertFileIsWritable(OUTPUT); final SAMFileReader bam; if (inputUrl != null) { // remote input bam = new SAMFileReader(inputUrl, null, false); } else { // input from a normal file IOUtil.assertFileIsReadable(inputFile); bam = new SAMFileReader(inputFile); } if (!bam.isBinary()) { throw new SAMException("Input file must be bam file, not sam file."); } if (!bam.getFileHeader().getSortOrder().equals(SAMFileHeader.SortOrder.coordinate)) { throw new SAMException("Input bam file must be sorted by coordinates"); } BAMIndexer.createIndex(bam, OUTPUT); log.info("Successfully wrote bam index file " + OUTPUT); CloserUtil.close(bam); return 0; }
/** * Generates a BAM index file from an input BAM file * * @param reader SAMFileReader for input BAM file * @param output File for output index file */ public static void createIndex(SAMFileReader reader, File output) { createIndex(reader, output, null); }