Skip to content

Commit

Permalink
PrintReadsHeader: a new tool to print a BAM/SAM/CRAM header to a file (
Browse files Browse the repository at this point in the history
…#6153)

* PrintReadsHeader: a new tool to print a BAM/SAM/CRAM header to a file

* Added an integration test for PrintReadsHeader
  • Loading branch information
droazen authored Oct 24, 2019
1 parent 1451c2b commit 34cdadf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
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 PrintReadsHeader 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 reads header to " + outputFile, e);
}

logger.info("Successfully wrote reads header to destination: " + outputFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.broadinstitute.hellbender.tools;

import org.broadinstitute.hellbender.CommandLineProgramTest;
import org.broadinstitute.hellbender.testutils.IntegrationTestSpec;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

public final class PrintReadsHeaderIntegrationTest extends CommandLineProgramTest {

private static final File TEST_DATA_DIR = getTestDataDir();

@Test
public void testPrintReadsHeader() throws IOException {
final File inFile = new File(TEST_DATA_DIR, "print_reads.sorted.bam");
final File outFile = createTempFile("PrintReadsHeaderIntegrationTest_testPrintBAMHeader", ".txt");
final String[] args = new String[] {
"--input" , inFile.getAbsolutePath(),
"--output", outFile.getAbsolutePath()
};
runCommandLine(args);

//Make sure contents are the same
IntegrationTestSpec.assertEqualTextFiles(outFile, new File(TEST_DATA_DIR, "print_reads.sorted.bam.header.txt"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@HD VN:1.6 SO:coordinate
@SQ SN:chr1 LN:101
@SQ SN:chr2 LN:101
@SQ SN:chr3 LN:101
@SQ SN:chr4 LN:101
@SQ SN:chr5 LN:101
@SQ SN:chr6 LN:101
@SQ SN:chr7 LN:454
@SQ SN:chr8 LN:202
@RG ID:0 SM:Hi,Mom! PL:ILLUMINA
@PG ID:1 VN:2.0 PN:Hey!

0 comments on commit 34cdadf

Please sign in to comment.