Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 PrintBAMHeader 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.broadinstitute.hellbender.tools;

import htsjdk.samtools.SamReaderFactory;
import htsjdk.samtools.util.IOUtil;
import org.broadinstitute.hellbender.GATKBaseTest;
import org.broadinstitute.hellbender.cmdline.CommandLineProgramIntegrationTest;
import org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions;
import org.broadinstitute.hellbender.testutils.SamAssertionUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

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

public final class PrintBAMHeaderIntegrationTest extends CommandLineProgramIntegrationTest {

protected static final File TEST_DATA_DIR = getTestDataDir();

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

//Make sure contents are the same
IOUtil.assertFilesEqual(new File(TEST_DATA_DIR, "print_reads.sorted.bam.header.txt"), outFile);
}
}
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!