for (final SAMSequenceRecord contig : refDict.getSequences() ) {
final SAMRecordIterator it = in.query(contig.getSequenceName(), 0, 0, false);
writeReads(out, it, newOrder, contig.getSequenceName());
}
// don't forget the unmapped reads
writeReads( out, in.queryUnmapped(), newOrder, "unmapped" );
out.close();
}
else {
SAMFileWriter out = new SAMFileWriterFactory().makeSAMOrBAMWriter(outHeader, false, OUTPUT);
writeReads(out, in.iterator(), newOrder, "All reads");