From 7462a0b2d172af55045982183a05316f756bec19 Mon Sep 17 00:00:00 2001 From: aaron Date: Tue, 23 Mar 2010 15:18:30 +0000 Subject: [PATCH] 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 --- .../sting/gatk/refdata/RodGeliText.java | 11 +- .../refdata/VariantContextAdaptorsTest.java | 105 +++++++++++------- 2 files changed, 69 insertions(+), 47 deletions(-) diff --git a/java/src/org/broadinstitute/sting/gatk/refdata/RodGeliText.java b/java/src/org/broadinstitute/sting/gatk/refdata/RodGeliText.java index 4b5e4c301..7cde55943 100755 --- a/java/src/org/broadinstitute/sting/gatk/refdata/RodGeliText.java +++ b/java/src/org/broadinstitute/sting/gatk/refdata/RodGeliText.java @@ -27,6 +27,7 @@ package org.broadinstitute.sting.gatk.refdata; import org.broadinstitute.sting.utils.GenomeLoc; import org.broadinstitute.sting.utils.GenomeLocParser; +import org.broadinstitute.sting.utils.MathUtils; import org.broadinstitute.sting.utils.Utils; import org.broadinstitute.sting.utils.genotype.Variation; @@ -350,13 +351,15 @@ public class RodGeliText extends BasicReferenceOrderedDatum implements Variation public boolean equals(RodGeliText other) { if (genotypePosteriors.length != genotypePosteriors.length) return false; for (int x = 0; x < genotypePosteriors.length; x++) - if (Double.compare(genotypePosteriors[x],other.genotypePosteriors[x])!=0) return false; - return (loc.equals(other) && + if (MathUtils.compareDoubles(genotypePosteriors[x],other.genotypePosteriors[x],1e-4)!=0) return false; + return (loc.equals(other.getLocation()) && refBase == other.refBase && depth == other.depth && maxMappingQuality == other.maxMappingQuality && bestGenotype.equals(other.bestGenotype) && - Double.compare(lodBtr,other.lodBtr) == 0&& - Double.compare(lodBtnb,other.lodBtr) == 0); + MathUtils.compareDoubles(lodBtr,other.lodBtr,1e-4) == 0&& // 1e-4 is about the precision the + MathUtils.compareDoubles(lodBtnb,other.lodBtnb,1e-4) == 0); // output file seems to cooperate with } + + } diff --git a/java/test/org/broadinstitute/sting/gatk/refdata/VariantContextAdaptorsTest.java b/java/test/org/broadinstitute/sting/gatk/refdata/VariantContextAdaptorsTest.java index de3aec884..36359efe1 100644 --- a/java/test/org/broadinstitute/sting/gatk/refdata/VariantContextAdaptorsTest.java +++ b/java/test/org/broadinstitute/sting/gatk/refdata/VariantContextAdaptorsTest.java @@ -113,16 +113,14 @@ public class VariantContextAdaptorsTest extends BaseTest { * 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 * 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 public void testVariantContextGeliToGeli() { // our input and output files - File knownFile = new File(validationDataLocation + "/well_formed.geli"); // our known good GLF - File tempFile = new File("temp.geli"); // our temporary GLF output -> input file - tempFile.deleteOnExit(); // delete when we're done + File knownFile = new File(validationDataLocation + "/well_formed.geli"); // our known good geli + File tempFile = new File("temp.geli"); // our temporary geli output -> input file + tempFile.deleteOnExit(); // delete when we're done // create our genotype writer for GLFs GenotypeWriter gw = GenotypeWriterFactory.create(GenotypeWriterFactory.GENOTYPE_FORMAT.GELI,tempFile); @@ -133,28 +131,15 @@ public class VariantContextAdaptorsTest extends BaseTest { // buffer the records we see List records = new ArrayList(); - // a little more complicated than the above example, we have to read the file in - AsciiLineReader reader = null; - 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); + // a little more complicated than the GLF example, we have to read the file in + AsciiLineReader reader = createReader(knownFile); + // 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 (line != null && 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); + parseGeli(geliText, line); records.add(geliText); // we know they're all single calls in the reference file VariantContext vc = VariantContextAdaptors.toVariantContext("Geli",geliText); 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 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 List records2 = new ArrayList(); - try { - reader = new AsciiLineReader(new FileInputStream(tempFile)); - } catch (FileNotFoundException e) { - Assert.fail("File not found: " + tempFile); - } - line = "#"; - while (line != null && line.startsWith("#")) - line = readLine(reader); + reader = createReader(tempFile); + // get the first real line (non-header) + line = cleanHeaderFromFile(reader); // while we have records, make a Variant Context and output it to a GLF file while (line != null && line != "") { - try { - geliText.parseLine(null,line.split(TabularROD.DEFAULT_DELIMITER_REGEX)); - } catch (IOException e) { - Assert.fail("IOException: " + e.getMessage()); - } + parseGeli(geliText,line); records2.add(geliText); // we know they're all single calls in the reference file line = readLine(reader); @@ -198,11 +170,58 @@ public class VariantContextAdaptorsTest extends BaseTest { // 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()); // 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++) - // Assert.assertTrue("GLF Records were not preserved when cycling them to and from disc", records.get(x).equals(records2.get(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))); } + /** + * 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) { try { String line = reader.readLine();