add a way to test files generated by a walker that aren't command-line arguments; added some example code in CoverageStatisticsIntegrationTest for Chris.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2929 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2010-03-04 20:20:58 +00:00
parent adea38fd5e
commit 80cc6bbeb4
2 changed files with 62 additions and 34 deletions

View File

@ -4,7 +4,9 @@ import junit.framework.Assert;
import org.broadinstitute.sting.gatk.CommandLineExecutable; import org.broadinstitute.sting.gatk.CommandLineExecutable;
import org.broadinstitute.sting.gatk.CommandLineGATK; import org.broadinstitute.sting.gatk.CommandLineGATK;
import org.broadinstitute.sting.utils.Pair; import org.broadinstitute.sting.utils.Pair;
import org.broadinstitute.sting.utils.StingException;
import org.broadinstitute.sting.utils.Utils; import org.broadinstitute.sting.utils.Utils;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.apache.log4j.Appender; import org.apache.log4j.Appender;
import org.apache.log4j.WriterAppender; import org.apache.log4j.WriterAppender;
@ -18,16 +20,23 @@ import java.security.MessageDigest;
import java.util.*; import java.util.*;
public class WalkerTest extends BaseTest { public class WalkerTest extends BaseTest {
public String assertMatchingMD5(final String name, final File resultsFile, final String expectedMD5 ) { // the default output path for the integration test
private File outputFileLocation = null;
public void setOutputFileLocation(File outputFileLocation) {
this.outputFileLocation = outputFileLocation;
}
public String assertMatchingMD5(final String name, final File resultsFile, final String expectedMD5) {
try { try {
byte[] bytesOfMessage = getBytesFromFile(resultsFile); byte[] bytesOfMessage = getBytesFromFile(resultsFile);
byte[] thedigest = MessageDigest.getInstance("MD5").digest(bytesOfMessage); byte[] thedigest = MessageDigest.getInstance("MD5").digest(bytesOfMessage);
BigInteger bigInt = new BigInteger(1, thedigest); BigInteger bigInt = new BigInteger(1, thedigest);
String filemd5sum = bigInt.toString(16); String filemd5sum = bigInt.toString(16);
while (filemd5sum.length() < 32) filemd5sum = "0" + filemd5sum; // pad to length 32 while (filemd5sum.length() < 32) filemd5sum = "0" + filemd5sum; // pad to length 32
if ( parameterize() || expectedMD5.equals("") ) { if (parameterize() || expectedMD5.equals("")) {
System.out.println(String.format("PARAMETERIZATION[%s]: file %s has md5 = %s, stated expectation is %s, equal? = %b", System.out.println(String.format("PARAMETERIZATION[%s]: file %s has md5 = %s, stated expectation is %s, equal? = %b",
name, resultsFile, filemd5sum, expectedMD5, filemd5sum.equals(expectedMD5))); name, resultsFile, filemd5sum, expectedMD5, filemd5sum.equals(expectedMD5)));
} else { } else {
System.out.println(String.format("Checking MD5 for %s [calculated=%s, expected=%s]", resultsFile, filemd5sum, expectedMD5)); System.out.println(String.format("Checking MD5 for %s [calculated=%s, expected=%s]", resultsFile, filemd5sum, expectedMD5));
System.out.flush(); System.out.flush();
@ -36,14 +45,14 @@ public class WalkerTest extends BaseTest {
} }
return filemd5sum; return filemd5sum;
} catch ( Exception e ) { } catch (Exception e) {
throw new RuntimeException("Failed to read bytes from calls file: " + resultsFile); throw new RuntimeException("Failed to read bytes from calls file: " + resultsFile);
} }
} }
public List<String> assertMatchingMD5s(final String name, List<File> resultFiles, List<String> expectedMD5s ) { public List<String> assertMatchingMD5s(final String name, List<File> resultFiles, List<String> expectedMD5s) {
List<String> md5s = new ArrayList<String>(); List<String> md5s = new ArrayList<String>();
for ( int i = 0; i < resultFiles.size(); i++ ) { for (int i = 0; i < resultFiles.size(); i++) {
String md5 = assertMatchingMD5(name, resultFiles.get(i), expectedMD5s.get(i)); String md5 = assertMatchingMD5(name, resultFiles.get(i), expectedMD5s.get(i));
md5s.add(i, md5); md5s.add(i, md5);
} }
@ -63,19 +72,19 @@ public class WalkerTest extends BaseTest {
} }
// Create the byte array to hold the data // Create the byte array to hold the data
byte[] bytes = new byte[(int)length]; byte[] bytes = new byte[(int) length];
// Read in the bytes // Read in the bytes
int offset = 0; int offset = 0;
int numRead = 0; int numRead = 0;
while (offset < bytes.length while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead; offset += numRead;
} }
// Ensure all the bytes have been read in // Ensure all the bytes have been read in
if (offset < bytes.length) { if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName()); throw new IOException("Could not completely read file " + file.getName());
} }
// Close the input stream and return bytes // Close the input stream and return bytes
@ -89,7 +98,7 @@ public class WalkerTest extends BaseTest {
List<String> md5s = null; List<String> md5s = null;
List<String> exts = null; List<String> exts = null;
Map<String,File> auxillaryFiles = new HashMap<String,File>(); protected Map<String, File> auxillaryFiles = new HashMap<String, File>();
public WalkerTestSpec(String args, int nOutputFiles, List<String> md5s) { public WalkerTestSpec(String args, int nOutputFiles, List<String> md5s) {
this.args = args; this.args = args;
@ -105,7 +114,7 @@ public class WalkerTest extends BaseTest {
} }
public void addAuxFile(String expectededMD5sum, File outputfile) { public void addAuxFile(String expectededMD5sum, File outputfile) {
auxillaryFiles.put(expectededMD5sum,outputfile); auxillaryFiles.put(expectededMD5sum, outputfile);
} }
} }
@ -115,20 +124,14 @@ public class WalkerTest extends BaseTest {
protected Pair<List<File>, List<String>> executeTest(final String name, WalkerTestSpec spec) { protected Pair<List<File>, List<String>> executeTest(final String name, WalkerTestSpec spec) {
List<File> tmpFiles = new ArrayList<File>(); List<File> tmpFiles = new ArrayList<File>();
for ( int i = 0; i < spec.nOutputFiles; i++ ) { for (int i = 0; i < spec.nOutputFiles; i++) {
try { String ext = spec.exts == null ? ".tmp" : "." + spec.exts.get(i);
String ext = spec.exts == null ? ".tmp" : "." + spec.exts.get(i); File fl = createTempFile(String.format("walktest.tmp_param.%d", i), ext);
File fl = File.createTempFile(String.format("walktest.tmp_param.%d", i), ext ); tmpFiles.add(fl);
fl.deleteOnExit();
tmpFiles.add( fl );
} catch (IOException ex) {
System.err.println("Cannot create temp file: " + ex.getMessage());
}
} }
final String args = String.format(spec.args, tmpFiles.toArray()); final String args = String.format(spec.args, tmpFiles.toArray());
System.out.println(Utils.dupString('-', 80)); System.out.println(Utils.dupString('-', 80));
System.out.println(String.format("Executing test %s with GATK arguments: %s", name, args));
List<String> md5s = new LinkedList<String>(); List<String> md5s = new LinkedList<String>();
md5s.addAll(spec.md5s); md5s.addAll(spec.md5s);
@ -142,13 +145,22 @@ public class WalkerTest extends BaseTest {
return executeTest(name, md5s, tmpFiles, args); return executeTest(name, md5s, tmpFiles, args);
} }
public File createTempFile(String name, String extension) {
try {
File fl = File.createTempFile(name, extension);
//fl.deleteOnExit();
return fl;
} catch (IOException ex) {
throw new StingException("Cannot create temp file: " + ex.getMessage(), ex);
}
}
/** /**
* execute the test, given the following: * execute the test, given the following:
* @param name the name of the test * @param name the name of the test
* @param md5s the list of md5s * @param md5s the list of md5s
* @param tmpFiles the temp file corresponding to the md5 list * @param tmpFiles the temp file corresponding to the md5 list
* @param args the argument list * @param args the argument list
* @return a pair of file and string lists * @return a pair of file and string lists
*/ */
private Pair<List<File>, List<String>> executeTest(String name, List<String> md5s, List<File> tmpFiles, String args) { private Pair<List<File>, List<String>> executeTest(String name, List<String> md5s, List<File> tmpFiles, String args) {
@ -156,16 +168,23 @@ public class WalkerTest extends BaseTest {
String[] command; String[] command;
// special case for ' and " so we can allow expressions // special case for ' and " so we can allow expressions
if ( args.indexOf('\'') != -1 ) if (args.indexOf('\'') != -1)
command = escapeExpressions(args, "'"); command = escapeExpressions(args, "'");
else if ( args.indexOf('\"') != -1 ) else if (args.indexOf('\"') != -1)
command = escapeExpressions(args, "\""); command = escapeExpressions(args, "\"");
else else
command = args.split(" "); command = args.split(" ");
if (outputFileLocation != null) {
String[] cmd2 = Arrays.copyOf(command, command.length + 2);
cmd2[command.length] = "-o";
cmd2[command.length + 1] = this.outputFileLocation.getAbsolutePath();
command = cmd2;
}
System.out.println(String.format("Executing test %s with GATK arguments: %s", name, Utils.join(" ",command)));
CommandLineExecutable.start(instance, command); CommandLineExecutable.start(instance, command);
if ( CommandLineExecutable.result != 0 ) { if (CommandLineExecutable.result != 0) {
throw new RuntimeException("Error running the GATK with arguments: " + args); throw new RuntimeException("Error running the GATK with arguments: " + args);
} }
@ -175,11 +194,11 @@ public class WalkerTest extends BaseTest {
private static String[] escapeExpressions(String args, String delimiter) { private static String[] escapeExpressions(String args, String delimiter) {
String[] command = {}; String[] command = {};
String[] split = args.split(delimiter); String[] split = args.split(delimiter);
for (int i = 0; i < split.length - 1; i+=2) { for (int i = 0; i < split.length - 1; i += 2) {
command = Utils.concatArrays(command, split[i].trim().split(" ")); command = Utils.concatArrays(command, split[i].trim().split(" "));
command = Utils.concatArrays(command, new String[] { split[i+1] } ); command = Utils.concatArrays(command, new String[]{split[i + 1]});
} }
return Utils.concatArrays(command, split[split.length-1].trim().split(" ")); return Utils.concatArrays(command, split[split.length - 1].trim().split(" "));
} }
@Test @Test

View File

@ -3,6 +3,7 @@ package org.broadinstitute.sting.oneoffprojects.walkers.coverage;
import org.broadinstitute.sting.WalkerTest; import org.broadinstitute.sting.WalkerTest;
import org.junit.Test; import org.junit.Test;
import java.io.File;
import java.util.Arrays; import java.util.Arrays;
/** /**
@ -13,11 +14,11 @@ import java.util.Arrays;
*/ */
public class CoverageStatisticsIntegrationTest extends WalkerTest { public class CoverageStatisticsIntegrationTest extends WalkerTest {
private boolean RUN_TESTS = false; private boolean RUN_TESTS = true;
private String root = "-T CoverageStatistics "; private String root = "-T CoverageStatistics ";
private String buildRootCmd(String ref, String bam, String interval) { private String buildRootCmd(String ref, String bam, String interval) {
return root + "-R "+ref+" -I "+bam+" -L "+interval+" -o %s"; return root + "-R "+ref+" -I "+bam+" -L "+interval;
} }
private void execute(String name, WalkerTestSpec spec) { private void execute(String name, WalkerTestSpec spec) {
@ -28,12 +29,20 @@ public class CoverageStatisticsIntegrationTest extends WalkerTest {
@Test @Test
public void testBaseOutputNoFiltering() { public void testBaseOutputNoFiltering() {
// our base file
File baseOutputFile = this.createTempFile("outputtemp",".tmp");
this.setOutputFileLocation(baseOutputFile);
String bam_file = "/humgen/gsa-hphome1/chartl/projects/depthOfCoverage/testFiles/bams/Ciliopathies_1_88534_3_samples.bam"; String bam_file = "/humgen/gsa-hphome1/chartl/projects/depthOfCoverage/testFiles/bams/Ciliopathies_1_88534_3_samples.bam";
String interval_list = "chr1:855534"; String interval_list = "chr1:855534";
String reference = "/seq/references/Homo_sapiens_assembly18/v0/Homo_sapiens_assembly18.fasta"; String reference = "/seq/references/Homo_sapiens_assembly18/v0/Homo_sapiens_assembly18.fasta";
String cmd = buildRootCmd(reference,bam_file,interval_list) + " -mmq 0 -mbq 0 -omitSampleSummary -omitIntervals -omitLocus"; String cmd = buildRootCmd(reference,bam_file,interval_list) + " -mmq 0 -mbq 0 -omitSampleSummary -omitLocus";
String expected = "2aee1dbcb69bf1e874d56cd23336afa8"; String expected = "d41d8cd98f00b204e9800998ecf8427e";
WalkerTestSpec spec = new WalkerTestSpec(cmd,1, Arrays.asList(expected)); WalkerTestSpec spec = new WalkerTestSpec(cmd,1, Arrays.asList(expected));
// now add the expected files that get generated
spec.addAuxFile("344936e0bb4613544ff137cd1a002d6c",new File(baseOutputFile.getAbsolutePath() + ".interval_statistics"));
execute("testBaseOutputNoFiltering",spec); execute("testBaseOutputNoFiltering",spec);
} }
} }