use of htsjdk.samtools.SAMFileWriter in project gatk by broadinstitute.
the class ReorderSam method doWork.
@Override
protected Object doWork() {
IOUtil.assertFileIsReadable(INPUT);
IOUtil.assertFileIsReadable(REFERENCE_SEQUENCE);
IOUtil.assertFileIsWritable(OUTPUT);
final SamReader in = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
ReferenceSequenceFile reference = ReferenceSequenceFileFactory.getReferenceSequenceFile(REFERENCE_SEQUENCE);
SAMSequenceDictionary refDict = reference.getSequenceDictionary();
if (refDict == null) {
CloserUtil.close(in);
throw new UserException("No reference sequence dictionary found. Aborting. " + "You can create a sequence dictionary for the reference fasta using CreateSequenceDictionary.jar.");
}
printDictionary("SAM/BAM file", in.getFileHeader().getSequenceDictionary());
printDictionary("Reference", refDict);
Map<Integer, Integer> newOrder = buildSequenceDictionaryMap(refDict, in.getFileHeader().getSequenceDictionary());
// has to be after we create the newOrder
SAMFileHeader outHeader = ReadUtils.cloneSAMFileHeader(in.getFileHeader());
outHeader.setSequenceDictionary(refDict);
logger.info("Writing reads...");
if (in.hasIndex()) {
try (final SAMFileWriter out = createSAMWriter(OUTPUT, REFERENCE_SEQUENCE, outHeader, true)) {
// write the reads in contig order
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");
}
} else {
try (final SAMFileWriter out = createSAMWriter(OUTPUT, REFERENCE_SEQUENCE, outHeader, false)) {
writeReads(out, in.iterator(), newOrder, "All reads");
}
}
// cleanup
CloserUtil.close(in);
return null;
}
use of htsjdk.samtools.SAMFileWriter in project gatk by broadinstitute.
the class ReadUtilsUnitTest method testCreateSAMWriter.
@Test(dataProvider = "createSAMWriter")
public void testCreateSAMWriter(final File bamFile, final boolean preSorted, final boolean createIndex, final boolean createMD5, final boolean expectIndex) throws Exception {
final File outputFile = createTempFile("samWriterTest", ".bam");
try (final SamReader samReader = SamReaderFactory.makeDefault().open(bamFile)) {
final SAMFileHeader header = samReader.getFileHeader();
if (expectIndex) {
// ensure test condition
Assert.assertEquals(expectIndex, header.getSortOrder() == SAMFileHeader.SortOrder.coordinate);
}
try (final SAMFileWriter samWriter = ReadUtils.createCommonSAMWriter(outputFile, null, samReader.getFileHeader(), preSorted, createIndex, createMD5)) {
final Iterator<SAMRecord> samRecIt = samReader.iterator();
while (samRecIt.hasNext()) {
samWriter.addAlignment(samRecIt.next());
}
}
}
final File md5File = new File(outputFile.getAbsolutePath() + ".md5");
if (md5File.exists()) {
md5File.deleteOnExit();
}
Assert.assertEquals(expectIndex, null != SamFiles.findIndex(outputFile));
Assert.assertEquals(createMD5, md5File.exists());
}
use of htsjdk.samtools.SAMFileWriter in project gatk-protected by broadinstitute.
the class AssemblyBasedCallerUtils method assembleReads.
/**
* High-level function that runs the assembler on the given region's reads,
* returning a data structure with the resulting information needed
* for further HC steps
*/
public static AssemblyResultSet assembleReads(final AssemblyRegion region, final List<VariantContext> givenAlleles, final AssemblyBasedCallerArgumentCollection argumentCollection, final SAMFileHeader header, final SampleList sampleList, final Logger logger, final ReferenceSequenceFile referenceReader, final ReadThreadingAssembler assemblyEngine) {
finalizeRegion(region, argumentCollection.errorCorrectReads, argumentCollection.dontUseSoftClippedBases, (byte) (argumentCollection.minBaseQualityScore - 1), header, sampleList);
if (argumentCollection.debug) {
logger.info("Assembling " + region.getSpan() + " with " + region.size() + " reads: (with overlap region = " + region.getExtendedSpan() + ")");
}
final byte[] fullReferenceWithPadding = region.getAssemblyRegionReference(referenceReader, REFERENCE_PADDING_FOR_ASSEMBLY);
final SimpleInterval paddedReferenceLoc = getPaddedReferenceLoc(region, REFERENCE_PADDING_FOR_ASSEMBLY, referenceReader);
final Haplotype referenceHaplotype = createReferenceHaplotype(region, paddedReferenceLoc, referenceReader);
final ReadErrorCorrector readErrorCorrector = argumentCollection.errorCorrectReads ? new ReadErrorCorrector(argumentCollection.assemblerArgs.kmerLengthForReadErrorCorrection, HaplotypeCallerEngine.MIN_TAIL_QUALITY_WITH_ERROR_CORRECTION, argumentCollection.assemblerArgs.minObservationsForKmerToBeSolid, argumentCollection.debug, fullReferenceWithPadding) : null;
try {
final AssemblyResultSet assemblyResultSet = assemblyEngine.runLocalAssembly(region, referenceHaplotype, fullReferenceWithPadding, paddedReferenceLoc, givenAlleles, readErrorCorrector, header);
assemblyResultSet.debugDump(logger);
return assemblyResultSet;
} catch (final Exception e) {
// Capture any exception that might be thrown, and write out the assembly failure BAM if requested
if (argumentCollection.captureAssemblyFailureBAM) {
try (final SAMFileWriter writer = ReadUtils.createCommonSAMWriter(new File("assemblyFailure.bam"), null, header, false, false, false)) {
for (final GATKRead read : region.getReads()) {
writer.addAlignment(read.convertToSAMRecord(header));
}
}
}
throw e;
}
}
use of htsjdk.samtools.SAMFileWriter in project hmftools by hartwigmedical.
the class Analysis method queryNameSortedBAM.
private static File queryNameSortedBAM(final SamReader reader, final QueryInterval[] intervals, final String name) throws IOException {
final SAMFileHeader header = reader.getFileHeader().clone();
header.setSortOrder(SAMFileHeader.SortOrder.queryname);
final File file = File.createTempFile(name, ".bam");
final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, false, file);
final SAMRecordIterator iterator = reader.queryOverlapping(intervals);
while (iterator.hasNext()) {
writer.addAlignment(iterator.next());
}
iterator.close();
writer.close();
return file;
}
use of htsjdk.samtools.SAMFileWriter in project hmftools by hartwigmedical.
the class BreakPointInspectorApplication method writeToSlice.
private static void writeToSlice(final String path, final SamReader reader, final QueryInterval[] intervals) {
final File outputBAM = new File(path);
final SAMFileWriter writer = new SAMFileWriterFactory().makeBAMWriter(reader.getFileHeader(), true, outputBAM);
final SAMRecordIterator iterator = reader.queryOverlapping(intervals);
while (iterator.hasNext()) {
writer.addAlignment(iterator.next());
}
iterator.close();
writer.close();
}
Aggregations