Skip to content

Commit

Permalink
Rebase and update to obtain latest test hooks. This rebase will remov…
Browse files Browse the repository at this point in the history
…e existing line comments, but I believe they are all addressed.

Still TODO: Merge in other tool functionality also requested in the other PR comments.
  • Loading branch information
kshakir committed Jan 13, 2015
1 parent bd9b190 commit f48aacf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import htsjdk.samtools.*;
import htsjdk.samtools.util.CloserUtil;
import htsjdk.samtools.util.IOUtil;
import org.apache.commons.io.FilenameUtils;
import org.broadinstitute.hellbender.cmdline.CommandLineProgram;
import org.broadinstitute.hellbender.cmdline.CommandLineProgramProperties;
import org.broadinstitute.hellbender.cmdline.Option;
Expand All @@ -29,23 +30,20 @@ public class PrintReadsByReadGroup extends CommandLineProgram {

@Option(shortName = StandardOptionDefinitions.OUTPUT_SHORT_NAME,
doc = "The directory to write SAM or BAM or CRAM files by read group.")
public Path OUTPUT_DIRECTORY = Paths.get("");

public static void main(final String[] args) {
new PrintReadsByReadGroup().instanceMain(args);
}
public File OUTPUT_DIRECTORY = new File("");

@Override
protected int doWork() {
protected Object doWork() {
printReadsByReadGroup();
return 0;
return null;
}

/**
* This is factored out of doWork only for unit testing.
*/
protected void printReadsByReadGroup() {
IOUtil.assertFileIsReadable(INPUT);
IOUtil.assertDirectoryIsWritable(OUTPUT_DIRECTORY);
final SamReader in = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
Map<String, SAMFileWriter> outs = createWriters(in);
for (final SAMRecord rec : in) {
Expand All @@ -59,20 +57,17 @@ private Map<String, SAMFileWriter> createWriters(final SamReader in) {
final Map<String, SAMFileWriter> outs = new HashMap<>();

final SAMFileWriterFactory samFileWriterFactory = new SAMFileWriterFactory();
samFileWriterFactory.setCreateIndex(true);
samFileWriterFactory.setUseAsyncIo(true);

final SAMFileHeader samFileHeaderIn = in.getFileHeader();
final String[] tokens = INPUT.getName().split("\\.");
final String extension = tokens[tokens.length - 1];
final String extension = FilenameUtils.getExtension(INPUT.getName());

samFileHeaderIn.getReadGroups().forEach(samReadGroupRecord -> {
final SAMFileHeader samFileHeaderOut = samFileHeaderIn.clone();
samFileHeaderOut.setReadGroups(Collections.singletonList(samReadGroupRecord));

final String sample = samReadGroupRecord.getSample();
final String readGroupId = samReadGroupRecord.getReadGroupId();
final File outFile = OUTPUT_DIRECTORY.resolve(sample + "." + readGroupId + "." + extension).toFile();
final File outFile = new File(OUTPUT_DIRECTORY, sample + "." + readGroupId + "." + extension);

outs.put(readGroupId, samFileWriterFactory.makeWriter(samFileHeaderOut, true, outFile, REFERENCE_SEQUENCE));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import htsjdk.samtools.SamReaderFactory;
import htsjdk.samtools.util.CloserUtil;
import htsjdk.samtools.util.IOUtil;
import org.broadinstitute.hellbender.CommandLineProgramTest;
import org.broadinstitute.hellbender.cmdline.StandardOptionDefinitions;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -36,13 +38,20 @@
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Stream;

public class PrintReadsByReadGroupTest {
private static final String TEST_DATA_PREFIX =
"src/test/resources/org/broadinstitute/hellbender/tools/print_reads_by_read_group.";
private static final File REFERENCE_SEQUENCE = new File(TEST_DATA_PREFIX + "fasta");
public class PrintReadsByReadGroupTest extends CommandLineProgramTest {


private static final String TEST_DATA_PREFIX = "print_reads_by_read_group.";
private static final String REFERENCE_SEQUENCE = TEST_DATA_PREFIX + "fasta";

private File getReferenceSequence() {
return new File(getTestDataDir(), REFERENCE_SEQUENCE);
}

@DataProvider(name = "printReadsByReadGroupData", parallel = true)
public Object[][] getPrintReadsByReadGroupData() {
Expand All @@ -55,19 +64,30 @@ public Object[][] getPrintReadsByReadGroupData() {
@Test(dataProvider = "printReadsByReadGroupData")
public void testPrintReadsByReadGroup(final SamReader.Type type, final boolean useReference) throws Exception {
final String fileExtension = type.fileExtension();
final PrintReadsByReadGroup printReadsByReadGroup = new PrintReadsByReadGroup();
printReadsByReadGroup.INPUT = new File(TEST_DATA_PREFIX + fileExtension);
printReadsByReadGroup.OUTPUT_DIRECTORY = Files.createTempDirectory("printReadsByReadGroupData.");
if (useReference)
printReadsByReadGroup.REFERENCE_SEQUENCE = REFERENCE_SEQUENCE;
printReadsByReadGroup.printReadsByReadGroup();
final List<String> args = new ArrayList<>();

Path outputDir = Files.createTempDirectory("printReadsByReadGroupData.");
outputDir.toFile().deleteOnExit();

args.add(StandardOptionDefinitions.INPUT_SHORT_NAME + "=");
args.add(getTestDataDir() + "/" + TEST_DATA_PREFIX + fileExtension);

args.add(StandardOptionDefinitions.OUTPUT_SHORT_NAME + "=");
args.add(outputDir.toString());

if (useReference) {
args.add(StandardOptionDefinitions.REFERENCE_SHORT_NAME + "=");
args.add(getTestDataDir()+ "/" + REFERENCE_SEQUENCE);
}

Assert.assertEquals(runCommandLine(args), null);

Assert.assertEquals(
getReadCounts(printReadsByReadGroup.OUTPUT_DIRECTORY, "Momma.0", fileExtension),
getReadCounts(outputDir, "Momma.0", fileExtension),
17,
"expected read group 0 count for " + fileExtension);
Assert.assertEquals(
getReadCounts(printReadsByReadGroup.OUTPUT_DIRECTORY, "Poppa.1", fileExtension),
getReadCounts(outputDir, "Poppa.1", fileExtension),
2,
"expected read group 1 count for " + fileExtension);
}
Expand All @@ -76,7 +96,7 @@ private int getReadCounts(final Path tempDirectory, final String readGroupInfo,
final File path = tempDirectory.resolve(readGroupInfo + "." + fileExtension).toFile();
int count = 0;
IOUtil.assertFileIsReadable(path);
final SamReader in = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(path);
final SamReader in = SamReaderFactory.makeDefault().referenceSequence(getReferenceSequence()).open(path);
for (@SuppressWarnings("unused") final SAMRecord rec : in) {
count++;
}
Expand Down

0 comments on commit f48aacf

Please sign in to comment.