Skip to content

Commit

Permalink
add a couple piped input data tests
Browse files Browse the repository at this point in the history
  • Loading branch information
takutosato committed Aug 6, 2024
1 parent 77f4b09 commit 95de314
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 25 deletions.
7 changes: 4 additions & 3 deletions src/test/java/picard/sam/MergeBamAlignmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public class MergeBamAlignmentTest extends CommandLineProgramTest {
private static final File DATA_DIR = new File("testdata/picard/sam");
private static final File TEST_DATA_DIR = new File(DATA_DIR, "MergeBamAlignment");

private static final File unmappedBam = new File(DATA_DIR, "unmapped.sam");
public static final File unmappedBam = new File(DATA_DIR, "unmapped.sam");
private static final File alignedBam = new File(DATA_DIR, "aligned.sam");
public static final File alignedQuerySortedBam = new File(DATA_DIR, "aligned_query_name_sorted.sam");
private static final File oneHalfAlignedBam = new File(DATA_DIR, "onehalfaligned.sam");
private static final File otherHalfAlignedBam = new File(DATA_DIR, "otherhalfaligned.sam");
private static final File mergingUnmappedBam = new File(TEST_DATA_DIR, "unmapped.sam");
Expand All @@ -87,8 +88,8 @@ public class MergeBamAlignmentTest extends CommandLineProgramTest {
private static final File secondReadAlignedBam_firstHalf = new File(TEST_DATA_DIR, "firsthalf.read2.trimmed.aligned.sam");
private static final File secondReadAlignedBam_secondHalf = new File(TEST_DATA_DIR, "secondhalf.read2.trimmed.aligned.sam");
private static final File supplementalReadAlignedBam = new File(TEST_DATA_DIR, "aligned.supplement.sam");
private static final File alignedQuerynameSortedBam = new File(DATA_DIR, "aligned_queryname_sorted.sam");
private static final File fasta = new File(DATA_DIR, "merger.fasta");
public static final File alignedQuerynameSortedBam = new File(DATA_DIR, "aligned_queryname_sorted.sam");
public static final File fasta = new File(DATA_DIR, "merger.fasta");
private static final String bigSequenceName = "chr7"; // The longest sequence in merger.fasta
private static final File sequenceDict = new File(DATA_DIR, "merger.dict");
private static final File sequenceDict2 = new File(DATA_DIR, "merger.2.dict");
Expand Down
96 changes: 74 additions & 22 deletions src/test/java/picard/sam/PipedDataTest.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,96 @@
package picard.sam;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import picard.cmdline.CommandLineProgramTest;

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

import static picard.sam.MergeBamAlignmentTest.fasta;
import static picard.sam.MergeBamAlignmentTest.unmappedBam;

public class PipedDataTest {
private final String classPath = "\"" + System.getProperty("java.class.path") + "\" ";
private final String picardCommandlinePreamble = "java -classpath " + classPath + "picard.cmdline.PicardCommandLine ";

private String classPath = "\"" + System.getProperty("java.class.path") + "\" ";
/**
* Creates the command line argument to be used for piped (/dev/stdin) input tests.
*
* @param inputSAM the path to the input SAM file.
* @return commandline for ViewSam.
*/
private String getViewSamPicardCommand(final String inputSAM){
return picardCommandlinePreamble +
"ViewSam " +
"I=" + inputSAM + " " +
"ALIGNMENT_STATUS=All " +
"PF_STATUS=All ";
}

@Test
public void testSortSam() {
String[] command = {
@DataProvider(name = "pipedInputTestData")
public Object[][] getPipedInputTestData() throws IOException {
// Note the trailing space in each string block.
// TODO: port ArgumentBuilder from GATK so this would not be necessary.
final String[] sortSamCommand = {
"/bin/bash",
"-c",
"java -classpath " +
classPath +
"picard.cmdline.PicardCommandLine " +
"ViewSam " +
"I=testdata/picard/sam/test.bam " +
"ALIGNMENT_STATUS=All " +
"PF_STATUS=All " +
"| " +
"java -classpath " +
classPath +
"picard.cmdline.PicardCommandLine " +
"SortSam " +
"I=/dev/stdin " +
"O=/dev/null " +
"SORT_ORDER=queryname"
getViewSamPicardCommand("testdata/picard/sam/test.bam") +
"| " +
picardCommandlinePreamble +
"SortSam " +
"I=/dev/stdin " +
"O=/dev/null " +
"SORT_ORDER=queryname"
};

final File temp = File.createTempFile("test", "sam");
temp.deleteOnExit();
final String[] revertSamCommand = {
"/bin/bash",
"-c",
getViewSamPicardCommand("testdata/picard/sam/test.bam") + // tsato: this has to be a "+", not ",". Best to extract a method "piped input argument builder" or something
"| " +
picardCommandlinePreamble +
"RevertSam " +
"I=/dev/stdin " +
"O=/dev/null "
};

// Only test the case where the "alignedBam" argument is /dev/stdin, which is the standard use case (bwa -> mergebamalignment)
// Make sure that the input aligned bam is query-name sorted (as it would be if it's piped from bwa)
// MergeBamAlignment fails if the coordinate sorted aligned bam is given, after trying to query-name sort dynamically and failing to find the reference header items,
// due seemingly to the fact that the input is /dev/stdin.
final String[] mergeBamAlignmentCommand = {
"/bin/bash",
"-c",
getViewSamPicardCommand(MergeBamAlignmentTest.alignedQuerynameSortedBam.getAbsolutePath()) + // here we have to use "+", not ",". Best to extract a method "piped input argument builder" or something
"| " +
picardCommandlinePreamble +
"MergeBamAlignment " +
"ALIGNED_BAM=/dev/stdin " +
"UNMAPPED=" + unmappedBam + " " +
"REFERENCE_SEQUENCE=" + fasta + " " +
"OUTPUT=/dev/null "
};

return new Object[][]{
{sortSamCommand},
{revertSamCommand},
{mergeBamAlignmentCommand}
};
}

@Test(dataProvider = "pipedInputTestData")
public void testPipedInput(final String[] command) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.inheritIO();

Process process = processBuilder.start();
Assert.assertEquals(process.waitFor(), 0);

} catch (Exception e) {
Assert.fail("Failed to pipe data from htsjdk to picard", e);
Assert.fail("Failed to pipe data to picard. The error was: ", e);
}
}
}

0 comments on commit 95de314

Please sign in to comment.