From f8fc195bb1f6a3838f9d2b7989cff22386efa47d Mon Sep 17 00:00:00 2001 From: David Roazen Date: Tue, 10 Sep 2019 16:33:29 -0400 Subject: [PATCH 1/4] PrintBAMHeader: a new tool to print a BAM header to a file --- .../hellbender/tools/PrintBAMHeader.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/org/broadinstitute/hellbender/tools/PrintBAMHeader.java diff --git a/src/main/java/org/broadinstitute/hellbender/tools/PrintBAMHeader.java b/src/main/java/org/broadinstitute/hellbender/tools/PrintBAMHeader.java new file mode 100644 index 00000000000..4eae64b3dfe --- /dev/null +++ b/src/main/java/org/broadinstitute/hellbender/tools/PrintBAMHeader.java @@ -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); + } +} From 3ccfdfd02bbe92b6eec3a4d303662815aa123c36 Mon Sep 17 00:00:00 2001 From: Michael Gatzen Date: Mon, 16 Sep 2019 13:39:01 -0400 Subject: [PATCH 2/4] Added integration test for PrintBAMHeader - New file in test resources --- .../tools/PrintBAMHeaderIntegrationTest.java | 33 +++++++++++++++++++ .../tools/print_reads.sorted.bam.header.txt | 11 +++++++ 2 files changed, 44 insertions(+) create mode 100644 src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java create mode 100644 src/test/resources/org/broadinstitute/hellbender/tools/print_reads.sorted.bam.header.txt diff --git a/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java b/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java new file mode 100644 index 00000000000..a63f53948f1 --- /dev/null +++ b/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java @@ -0,0 +1,33 @@ +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(), + "--" + StandardArgumentDefinitions.ADD_OUTPUT_SAM_PROGRAM_RECORD, + "--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); + } +} \ No newline at end of file diff --git a/src/test/resources/org/broadinstitute/hellbender/tools/print_reads.sorted.bam.header.txt b/src/test/resources/org/broadinstitute/hellbender/tools/print_reads.sorted.bam.header.txt new file mode 100644 index 00000000000..67d6a6d56a0 --- /dev/null +++ b/src/test/resources/org/broadinstitute/hellbender/tools/print_reads.sorted.bam.header.txt @@ -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! From e93534c9e08418d401d687d3e1825f3aa6532109 Mon Sep 17 00:00:00 2001 From: Michael Gatzen Date: Mon, 16 Sep 2019 13:43:16 -0400 Subject: [PATCH 3/4] Removed unnecessary argument in integration test --- .../hellbender/tools/PrintBAMHeaderIntegrationTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java b/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java index a63f53948f1..9e2978aae07 100644 --- a/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java +++ b/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java @@ -22,7 +22,6 @@ public void testPrintBAMHeader() throws IOException { final File outFile = GATKBaseTest.createTempFile("NA12878.chr17_69k_70k.dictFix.bam.header", ".txt"); final String[] args = new String[] { "--input" , inFile.getAbsolutePath(), - "--" + StandardArgumentDefinitions.ADD_OUTPUT_SAM_PROGRAM_RECORD, "--output", outFile.getAbsolutePath() }; runCommandLine(args); From 14477e6dee5696658aaf1ee98e2aaa2f4ba88a3f Mon Sep 17 00:00:00 2001 From: David Roazen Date: Thu, 24 Oct 2019 14:51:15 -0400 Subject: [PATCH 4/4] Renamed the tool to PrintReadsHeader, and fix minor issues in the test code --- ...ntBAMHeader.java => PrintReadsHeader.java} | 6 ++-- .../tools/PrintBAMHeaderIntegrationTest.java | 32 ------------------- .../PrintReadsHeaderIntegrationTest.java | 27 ++++++++++++++++ 3 files changed, 30 insertions(+), 35 deletions(-) rename src/main/java/org/broadinstitute/hellbender/tools/{PrintBAMHeader.java => PrintReadsHeader.java} (90%) delete mode 100644 src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java create mode 100644 src/test/java/org/broadinstitute/hellbender/tools/PrintReadsHeaderIntegrationTest.java diff --git a/src/main/java/org/broadinstitute/hellbender/tools/PrintBAMHeader.java b/src/main/java/org/broadinstitute/hellbender/tools/PrintReadsHeader.java similarity index 90% rename from src/main/java/org/broadinstitute/hellbender/tools/PrintBAMHeader.java rename to src/main/java/org/broadinstitute/hellbender/tools/PrintReadsHeader.java index 4eae64b3dfe..9c30d53dcec 100644 --- a/src/main/java/org/broadinstitute/hellbender/tools/PrintBAMHeader.java +++ b/src/main/java/org/broadinstitute/hellbender/tools/PrintReadsHeader.java @@ -19,7 +19,7 @@ programGroup = ReadDataManipulationProgramGroup.class ) @DocumentedFeature -public class PrintBAMHeader extends GATKTool { +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; @@ -37,9 +37,9 @@ public void traverse() { final SAMTextHeaderCodec codec = new SAMTextHeaderCodec(); codec.encode(outputWriter, bamHeader); } catch (FileNotFoundException e ) { - throw new UserException.CouldNotCreateOutputFile("Error writing bam header to " + outputFile, e); + throw new UserException.CouldNotCreateOutputFile("Error writing reads header to " + outputFile, e); } - logger.info("Successfully wrote BAM header to destination " + outputFile); + logger.info("Successfully wrote reads header to destination: " + outputFile); } } diff --git a/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java b/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java deleted file mode 100644 index 9e2978aae07..00000000000 --- a/src/test/java/org/broadinstitute/hellbender/tools/PrintBAMHeaderIntegrationTest.java +++ /dev/null @@ -1,32 +0,0 @@ -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); - } -} \ No newline at end of file diff --git a/src/test/java/org/broadinstitute/hellbender/tools/PrintReadsHeaderIntegrationTest.java b/src/test/java/org/broadinstitute/hellbender/tools/PrintReadsHeaderIntegrationTest.java new file mode 100644 index 00000000000..e88ed240127 --- /dev/null +++ b/src/test/java/org/broadinstitute/hellbender/tools/PrintReadsHeaderIntegrationTest.java @@ -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")); + } +} \ No newline at end of file