-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PrintReadsHeader: a new tool to print a BAM/SAM/CRAM header to a file (…
…#6153) * PrintReadsHeader: a new tool to print a BAM/SAM/CRAM header to a file * Added an integration test for PrintReadsHeader
- Loading branch information
Showing
3 changed files
with
83 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/PrintReadsHeader.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 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); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/org/broadinstitute/hellbender/tools/PrintReadsHeaderIntegrationTest.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,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")); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/test/resources/org/broadinstitute/hellbender/tools/print_reads.sorted.bam.header.txt
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,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! |