Source:
protected int doWork() { IOUtil.assertFileIsReadable(INPUT); IOUtil.assertFileIsReadable(SEQUENCE_DICTIONARY); IOUtil.assertFileIsWritable(OUTPUT); try (final SamReader in = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT)) { final SAMSequenceDictionary outputDictionary = SAMSequenceDictionaryExtractor.extractDictionary(SEQUENCE_DICTIONARY.toPath()); if (outputDictionary == null) { log.error("No reference sequence dictionary found. Aborting. You can create a sequence dictionary for the reference fasta using CreateSequenceDictionary.jar."); return 1; } printDictionary("SAM/BAM file", in.getFileHeader().getSequenceDictionary()); printDictionary("Reference", outputDictionary); final Map<Integer, Integer> newOrder; try { newOrder = buildSequenceDictionaryMap(outputDictionary, in.getFileHeader().getSequenceDictionary()); } catch (PicardException e) { log.error(e.getMessage()); return 1; } final SAMFileHeader outHeader = in.getFileHeader().clone(); outHeader.setSequenceDictionary(outputDictionary); log.info("Writing reads..."); if (in.hasIndex()) { try (final SAMFileWriter out = new SAMFileWriterFactory().makeSAMOrBAMWriter(outHeader, true, OUTPUT)) { for (final SAMSequenceRecord contig : outputDictionary.getSequences()) { if (in.getFileHeader().getSequenceDictionary().getSequenceIndex(contig.getSequenceName()) != -1) { log.info("writing the reads from " + contig.getSequenceName()); final SAMRecordIterator it = in.query(contig.getSequenceName(), 0, 0, false); writeReads(out, it, newOrder, contig.getSequenceName()); } else { log.warn(String.format("New dictionary contains new contig %s, continuing nevertheless", contig.getSequenceName())); } } writeReads(out, in.queryUnmapped(), newOrder, "unmapped"); } } else { try (final SAMFileWriter out = new SAMFileWriterFactory().makeSAMOrBAMWriter(outHeader, false, OUTPUT)) { writeReads(out, in.iterator(), newOrder, "All reads"); } } } catch (IOException e) { e.printStackTrace(); } return 0; }

Target:
protected int doWork() { IOUtil.assertFileIsReadable(INPUT); IOUtil.assertFileIsReadable(SEQUENCE_DICTIONARY); IOUtil.assertFileIsWritable(OUTPUT); try (final SamReader in = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT)) { final SAMSequenceDictionary outputDictionary = SAMSequenceDictionaryExtractor.extractDictionary(SEQUENCE_DICTIONARY.toPath()); if (outputDictionary == null) { log.error("No reference sequence dictionary found. Aborting. You can create a sequence dictionary for the reference fasta using the CreateSequenceDictionary command."); return 1; } printDictionary("SAM/BAM file", in.getFileHeader().getSequenceDictionary()); printDictionary("Reference", outputDictionary); final Map<Integer, Integer> newOrder; try { newOrder = buildSequenceDictionaryMap(outputDictionary, in.getFileHeader().getSequenceDictionary()); } catch (PicardException e) { log.error(e); return 1; } final SAMFileHeader outHeader = in.getFileHeader().clone(); outHeader.setSequenceDictionary(outputDictionary); log.info("Writing reads..."); if (in.hasIndex()) { try (final SAMFileWriter out = new SAMFileWriterFactory().makeSAMOrBAMWriter(outHeader, false, OUTPUT)) { for (final SAMSequenceRecord contig : in.getFileHeader().getSequenceDictionary().getSequences()) { log.info("writing the reads from " + contig.getSequenceName()); final SAMRecordIterator it = in.query(contig.getSequenceName(), 0, 0, false); writeReads(out, it, newOrder, contig.getSequenceName()); } writeReads(out, in.queryUnmapped(), newOrder, "unmapped"); } } else { try (final SAMFileWriter out = new SAMFileWriterFactory().makeSAMOrBAMWriter(outHeader, false, OUTPUT)) { writeReads(out, in.iterator(), newOrder, "All reads"); } } } catch (IOException e) { e.printStackTrace(); } return 0; }