Skip to content

Commit

Permalink
PrintHeader: a new tool to print a BAM header to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
droazen committed Sep 10, 2019
1 parent d27692d commit b11ad11
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/org/broadinstitute/hellbender/tools/PrintHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.broadinstitute.hellbender.tools;

import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SAMTextHeaderCodec;
import org.broadinstitute.barclay.argparser.Argument;
import org.broadinstitute.barclay.argparser.CommandLineProgramProperties;
import org.broadinstitute.barclay.help.DocumentedFeature;
import org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions;
import org.broadinstitute.hellbender.engine.GATKTool;
import org.broadinstitute.hellbender.exceptions.UserException;
import picard.cmdline.programgroups.ReadDataManipulationProgramGroup;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

@CommandLineProgramProperties(
summary = "Prints the header from the input SAM/BAM/CRAM file to a textual output file",
oneLineSummary = "Print the header from a SAM/BAM/CRAM file",
programGroup = ReadDataManipulationProgramGroup.class
)
@DocumentedFeature
public class PrintHeader extends GATKTool {

@Argument(fullName = StandardArgumentDefinitions.OUTPUT_LONG_NAME, shortName = StandardArgumentDefinitions.OUTPUT_SHORT_NAME, doc = "file to write the bam header to", optional = false)
private String outputFile;

@Override
public boolean requiresReads() {
return true;
}

@Override
public void traverse() {
final SAMFileHeader bamHeader = getHeaderForReads();

try ( final PrintWriter outputWriter = new PrintWriter(outputFile) ) {
final SAMTextHeaderCodec codec = new SAMTextHeaderCodec();
codec.encode(outputWriter, bamHeader);
} catch (FileNotFoundException e ) {
throw new UserException.CouldNotCreateOutputFile("Error writing bam header to " + outputFile, e);
}

logger.info("Successfully wrote BAM header to destination " + outputFile);
}
}

0 comments on commit b11ad11

Please sign in to comment.