dir.mkdir();
}
SAMFileReader rdr = new SAMFileReader(new File(filename));
ThreadManager threads = new ThreadManager(numThreads);
Map<String, SAMFileWriter> outputWriterMap = new HashMap<String, SAMFileWriter>();
SAMFileWriterFactory writerFactory = new SAMFileWriterFactory();
writerFactory.setUseAsyncIo(false);
// Farm each chromosome out to its own thread.
for (SAMSequenceRecord chr : rdr.getFileHeader().getSequenceDictionary().getSequences()) {
SAMFileWriter writer = writerFactory.makeSAMOrBAMWriter(
rdr.getFileHeader(), false, new File(outputDirectory + "/" + chr.getSequenceName() + ".bam"));
outputWriterMap.put(chr.getSequenceName(), writer);
BamSplitterThread thread = new BamSplitterThread(threads, filename, chr.getSequenceName(), writer);
threads.spawnThread(thread);
}
threads.waitForAllThreadsToComplete();
// Now go back and retrieve the unmapped reads.
System.err.println("Processing unmapped reads");
Iterator<SAMRecord> iter = rdr.queryUnmapped();
while (iter.hasNext()) {