-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PrintHeader: a new tool to print a BAM header to a file
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/org/broadinstitute/hellbender/tools/PrintHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |