cleaned-up of VariantContextAdapter tests, fixed the double comparisons in equals() in RodGeliText (nice MathUtils.compareDoubles Kiran)

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@3064 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2010-03-23 15:18:30 +00:00
parent a69b8555dd
commit 7462a0b2d1
2 changed files with 69 additions and 47 deletions

View File

@ -27,6 +27,7 @@ package org.broadinstitute.sting.gatk.refdata;
import org.broadinstitute.sting.utils.GenomeLoc; import org.broadinstitute.sting.utils.GenomeLoc;
import org.broadinstitute.sting.utils.GenomeLocParser; import org.broadinstitute.sting.utils.GenomeLocParser;
import org.broadinstitute.sting.utils.MathUtils;
import org.broadinstitute.sting.utils.Utils; import org.broadinstitute.sting.utils.Utils;
import org.broadinstitute.sting.utils.genotype.Variation; import org.broadinstitute.sting.utils.genotype.Variation;
@ -350,13 +351,15 @@ public class RodGeliText extends BasicReferenceOrderedDatum implements Variation
public boolean equals(RodGeliText other) { public boolean equals(RodGeliText other) {
if (genotypePosteriors.length != genotypePosteriors.length) return false; if (genotypePosteriors.length != genotypePosteriors.length) return false;
for (int x = 0; x < genotypePosteriors.length; x++) for (int x = 0; x < genotypePosteriors.length; x++)
if (Double.compare(genotypePosteriors[x],other.genotypePosteriors[x])!=0) return false; if (MathUtils.compareDoubles(genotypePosteriors[x],other.genotypePosteriors[x],1e-4)!=0) return false;
return (loc.equals(other) && return (loc.equals(other.getLocation()) &&
refBase == other.refBase && refBase == other.refBase &&
depth == other.depth && depth == other.depth &&
maxMappingQuality == other.maxMappingQuality && maxMappingQuality == other.maxMappingQuality &&
bestGenotype.equals(other.bestGenotype) && bestGenotype.equals(other.bestGenotype) &&
Double.compare(lodBtr,other.lodBtr) == 0&& MathUtils.compareDoubles(lodBtr,other.lodBtr,1e-4) == 0&& // 1e-4 is about the precision the
Double.compare(lodBtnb,other.lodBtr) == 0); MathUtils.compareDoubles(lodBtnb,other.lodBtnb,1e-4) == 0); // output file seems to cooperate with
} }
} }

View File

@ -113,15 +113,13 @@ public class VariantContextAdaptorsTest extends BaseTest {
* and creates VariantContext records. These VC records are then outputted through a genotype writer, * and creates VariantContext records. These VC records are then outputted through a genotype writer,
* and then read back in off of disk and compared to the original records. This way we are positive all * and then read back in off of disk and compared to the original records. This way we are positive all
* the information that encodes a Geli makes it into the VC and then out to disk. * the information that encodes a Geli makes it into the VC and then out to disk.
*
* // TODO: this is a mess, clean it up
*/ */
@Test @Test
public void testVariantContextGeliToGeli() { public void testVariantContextGeliToGeli() {
// our input and output files // our input and output files
File knownFile = new File(validationDataLocation + "/well_formed.geli"); // our known good GLF File knownFile = new File(validationDataLocation + "/well_formed.geli"); // our known good geli
File tempFile = new File("temp.geli"); // our temporary GLF output -> input file File tempFile = new File("temp.geli"); // our temporary geli output -> input file
tempFile.deleteOnExit(); // delete when we're done tempFile.deleteOnExit(); // delete when we're done
// create our genotype writer for GLFs // create our genotype writer for GLFs
@ -133,28 +131,15 @@ public class VariantContextAdaptorsTest extends BaseTest {
// buffer the records we see // buffer the records we see
List<RodGeliText> records = new ArrayList<RodGeliText>(); List<RodGeliText> records = new ArrayList<RodGeliText>();
// a little more complicated than the above example, we have to read the file in // a little more complicated than the GLF example, we have to read the file in
AsciiLineReader reader = null; AsciiLineReader reader = createReader(knownFile);
try {
reader = new AsciiLineReader(new FileInputStream(knownFile));
} catch (FileNotFoundException e) {
Assert.fail("File not found: " + knownFile);
}
String line = "#";
while (line != null && line.startsWith("#"))
line = readLine(reader);
// get the first real line (non-header)
String line = cleanHeaderFromFile(reader);
// while we have records, make a Variant Context and output it to a GLF file // while we have records, make a Variant Context and output it to a GLF file
while (line != null && line != "") { while (line != null && line != "") {
boolean parsed = false; parseGeli(geliText, line);
try {
parsed = geliText.parseLine(null,line.split(TabularROD.DEFAULT_DELIMITER_REGEX));
} catch (IOException e) {
Assert.fail("IOException: " + e.getMessage());
}
if (!parsed) Assert.fail("Unable to parse line" + line);
records.add(geliText); // we know they're all single calls in the reference file records.add(geliText); // we know they're all single calls in the reference file
VariantContext vc = VariantContextAdaptors.toVariantContext("Geli",geliText); VariantContext vc = VariantContextAdaptors.toVariantContext("Geli",geliText);
gw.addCall(vc); gw.addCall(vc);
@ -165,30 +150,17 @@ public class VariantContextAdaptorsTest extends BaseTest {
// now reopen the file with the temp GLF file and read it back in, compare against what we first stored // now reopen the file with the temp GLF file and read it back in, compare against what we first stored
geliText = new RodGeliText("myROD"); geliText = new RodGeliText("myROD");
try {
geliText.initialize(tempFile);
} catch (FileNotFoundException e) {
Assert.fail("Unable to open GLF file" + tempFile);
}
// buffer the new records we see // buffer the new records we see
List<RodGeliText> records2 = new ArrayList<RodGeliText>(); List<RodGeliText> records2 = new ArrayList<RodGeliText>();
try { reader = createReader(tempFile);
reader = new AsciiLineReader(new FileInputStream(tempFile)); // get the first real line (non-header)
} catch (FileNotFoundException e) { line = cleanHeaderFromFile(reader);
Assert.fail("File not found: " + tempFile);
}
line = "#";
while (line != null && line.startsWith("#"))
line = readLine(reader);
// while we have records, make a Variant Context and output it to a GLF file // while we have records, make a Variant Context and output it to a GLF file
while (line != null && line != "") { while (line != null && line != "") {
try { parseGeli(geliText,line);
geliText.parseLine(null,line.split(TabularROD.DEFAULT_DELIMITER_REGEX));
} catch (IOException e) {
Assert.fail("IOException: " + e.getMessage());
}
records2.add(geliText); // we know they're all single calls in the reference file records2.add(geliText); // we know they're all single calls in the reference file
line = readLine(reader); line = readLine(reader);
@ -198,11 +170,58 @@ public class VariantContextAdaptorsTest extends BaseTest {
// compare sizes // compare sizes
Assert.assertEquals("The input GLF file doesn't contain the same number of records as we saw in the first file", records.size(),records2.size()); Assert.assertEquals("The input GLF file doesn't contain the same number of records as we saw in the first file", records.size(),records2.size());
// now compare each record TODO: uncomment out next two lines, fix equals so that rounding doesn't ruin our comparison // now compare each record TODO: uncomment out next two lines, fix equals so that rounding doesn't ruin our comparison
//for (int x = 0; x < records.size(); x++) for (int x = 0; x < records.size(); x++)
// Assert.assertTrue("GLF Records were not preserved when cycling them to and from disc", records.get(x).equals(records2.get(x))); Assert.assertTrue("GLF Records were not preserved when cycling them to and from disc", records.get(x).equals(records2.get(x)));
} }
/**
* parse the geli given a line representation
* @param geliText the object to parse into
* @param line the line to parse with
*/
private void parseGeli(RodGeliText geliText, String line) {
boolean parsed = false;
try {
parsed = geliText.parseLine(null,line.split(TabularROD.DEFAULT_DELIMITER_REGEX));
} catch (IOException e) {
Assert.fail("IOException: " + e.getMessage());
}
if (!parsed) Assert.fail("Unable to parse line" + line);
}
/**
* clean header lines form a geli file
* @param reader the reader
* @return the first line that's not a header
*/
private String cleanHeaderFromFile(AsciiLineReader reader) {
String line = "#";
while (line != null && line.startsWith("#"))
line = readLine(reader);
return line;
}
/**
* create a reader, given a file and the reader object
* @param knownFile the file to read in
* @return AsciiLineReader the ascii line reader
*/
private AsciiLineReader createReader(File knownFile) {
AsciiLineReader reader = null;
try {
reader = new AsciiLineReader(new FileInputStream(knownFile));
} catch (FileNotFoundException e) {
Assert.fail("File not found: " + knownFile);
}
return reader;
}
/**
* read a line from the specified reader. A method to make the above code look cleaner
* @param reader the ascii line reader
* @return a line of text
*/
public String readLine(AsciiLineReader reader) { public String readLine(AsciiLineReader reader) {
try { try {
String line = reader.readLine(); String line = reader.readLine();