Skip to content

Commit

Permalink
while I'm at it, cleaning up some array assert code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbenjamin committed Aug 28, 2019
1 parent 7375d5b commit 6930a0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,40 @@ public void testUsingQualities() throws Exception {

GATKRead read1 = ArtificialReadUtils.createArtificialRead("aa".getBytes(), new byte[]{50, 50}, "2M");
hg.addRead(read1);
assertEqualsLongArray(hg.firstReadCountsByCycle, new long[]{0, 1, 1});
Assert.assertEquals(hg.firstReadCountsByCycle, new long[]{0, 1, 1});
assertEqualsDoubleArray(hg.firstReadTotalsByCycle, new double[]{0, 50, 50}, 1e-05);
assertEqualsLongArray(hg.secondReadCountsByCycle, new long[]{0, 0, 0});
Assert.assertEquals(hg.secondReadCountsByCycle, new long[]{0, 0, 0});
assertEqualsDoubleArray(hg.secondReadTotalsByCycle, new double[]{0, 0, 0}, 1e-05);

GATKRead read2 = ArtificialReadUtils.createArtificialRead("aaa".getBytes(), new byte[]{11, 12, 13}, "3M");
hg.addRead(read2);
assertEqualsLongArray(hg.firstReadCountsByCycle, new long[]{0, 2, 2, 1});
Assert.assertEquals(hg.firstReadCountsByCycle, new long[]{0, 2, 2, 1});
assertEqualsDoubleArray(hg.firstReadTotalsByCycle, new double[]{0, 61, 62, 13}, 1e-05);
assertEqualsLongArray(hg.secondReadCountsByCycle, new long[]{0, 0, 0, 0});
Assert.assertEquals(hg.secondReadCountsByCycle, new long[]{0, 0, 0, 0});
assertEqualsDoubleArray(hg.secondReadTotalsByCycle, new double[]{0, 0, 0, 0}, 1e-05);

GATKRead read3 = ArtificialReadUtils.createArtificialRead("aa".getBytes(), new byte[]{50, 60}, "2M");
read3.setIsReverseStrand(true);
hg.addRead(read3);
assertEqualsLongArray(hg.firstReadCountsByCycle, new long[]{0, 3, 3, 1});
Assert.assertEquals(hg.firstReadCountsByCycle, new long[]{0, 3, 3, 1});
assertEqualsDoubleArray(hg.firstReadTotalsByCycle, new double[]{0, 121, 112, 13}, 1e-05);
assertEqualsLongArray(hg.secondReadCountsByCycle, new long[]{0, 0, 0, 0});
Assert.assertEquals(hg.secondReadCountsByCycle, new long[]{0, 0, 0, 0});
assertEqualsDoubleArray(hg.secondReadTotalsByCycle, new double[]{0, 0, 0, 0}, 1e-05);

GATKRead read4 = ArtificialReadUtils.createArtificialRead("aaa".getBytes(), new byte[]{11, 13, 15}, "3M");
read4.setIsReverseStrand(true);
hg.addRead(read4);
assertEqualsLongArray(hg.firstReadCountsByCycle, new long[]{0, 4, 4, 2});
Assert.assertEquals(hg.firstReadCountsByCycle, new long[]{0, 4, 4, 2});
assertEqualsDoubleArray(hg.firstReadTotalsByCycle, new double[]{0, 121 + 15, 112 + 13, 13 + 11}, 1e-05);
assertEqualsLongArray(hg.secondReadCountsByCycle, new long[]{0, 0, 0, 0});
Assert.assertEquals(hg.secondReadCountsByCycle, new long[]{0, 0, 0, 0});
assertEqualsDoubleArray(hg.secondReadTotalsByCycle, new double[]{0, 0, 0, 0}, 1e-05);

GATKRead read5 = ArtificialReadUtils.createArtificialRead("aaa".getBytes(), new byte[]{11, 12, 13}, "3M");
read5.setIsSecondOfPair();
hg.addRead(read5);
assertEqualsLongArray(hg.firstReadCountsByCycle, new long[]{0, 4, 4, 2});
Assert.assertEquals(hg.firstReadCountsByCycle, new long[]{0, 4, 4, 2});
assertEqualsDoubleArray(hg.firstReadTotalsByCycle, new double[]{0, 121 + 15, 112 + 13, 13 + 11}, 1e-05);
assertEqualsLongArray(hg.secondReadCountsByCycle, new long[]{0, 1, 1, 1});
Assert.assertEquals(hg.secondReadCountsByCycle, new long[]{0, 1, 1, 1});
assertEqualsDoubleArray(hg.secondReadTotalsByCycle, new double[]{0, 11, 12, 13}, 1e-05);

final MeanQualityByCycleSpark.HistogramGenerator hg2 = new MeanQualityByCycleSpark.HistogramGenerator(false);
Expand All @@ -59,19 +59,19 @@ public void testUsingQualities() throws Exception {

final MeanQualityByCycleSpark.HistogramGenerator hg2Plus1 = hg2.merge(hg); //add short to long
Assert.assertTrue(hg2 == hg2Plus1);
assertEqualsLongArray(hg2Plus1.firstReadCountsByCycle, new long[]{0, 5, 5, 3, 1, 1});
Assert.assertEquals(hg2Plus1.firstReadCountsByCycle, new long[]{0, 5, 5, 3, 1, 1});
assertEqualsDoubleArray(hg2Plus1.firstReadTotalsByCycle, new double[]{0, 121 + 15 + 51, 112 + 13 + 52, 13 + 11 + 53, 54, 55}, 1e-05);
assertEqualsLongArray(hg2Plus1.secondReadCountsByCycle, new long[]{0, 1, 1, 1, 0, 0});
Assert.assertEquals(hg2Plus1.secondReadCountsByCycle, new long[]{0, 1, 1, 1, 0, 0});
assertEqualsDoubleArray(hg2Plus1.secondReadTotalsByCycle, new double[]{0, 11, 12, 13, 0, 0}, 1e-05);

final MeanQualityByCycleSpark.HistogramGenerator hg2_copy = new MeanQualityByCycleSpark.HistogramGenerator(false);
hg2_copy.addRead(read1b);

final MeanQualityByCycleSpark.HistogramGenerator hg1Plus2 = hg.merge(hg2_copy); //add long to short
Assert.assertTrue(hg == hg1Plus2);
assertEqualsLongArray(hg1Plus2.firstReadCountsByCycle, new long[]{0, 5, 5, 3, 1, 1});
Assert.assertEquals(hg1Plus2.firstReadCountsByCycle, new long[]{0, 5, 5, 3, 1, 1});
assertEqualsDoubleArray(hg1Plus2.firstReadTotalsByCycle, new double[]{0, 121 + 15 + 51, 112 + 13 + 52, 13 + 11 + 53, 54, 55}, 1e-05);
assertEqualsLongArray(hg1Plus2.secondReadCountsByCycle, new long[]{0, 1, 1, 1, 0, 0});
Assert.assertEquals(hg1Plus2.secondReadCountsByCycle, new long[]{0, 1, 1, 1, 0, 0});
assertEqualsDoubleArray(hg1Plus2.secondReadTotalsByCycle, new double[]{0, 11, 12, 13, 0, 0}, 1e-05);
}

Expand All @@ -82,18 +82,18 @@ public void testUsingOriginalQualities() throws Exception {

GATKRead read1 = ArtificialReadUtils.createArtificialRead("aa".getBytes(), new byte[]{50, 50}, "2M");
hg.addRead(read1);
assertEqualsLongArray(hg.firstReadCountsByCycle, new long[0]);
Assert.assertEquals(hg.firstReadCountsByCycle, new long[0]);
assertEqualsDoubleArray(hg.firstReadTotalsByCycle, new double[0], 1e-05);
assertEqualsLongArray(hg.secondReadCountsByCycle, new long[0]);
Assert.assertEquals(hg.secondReadCountsByCycle, new long[0]);
assertEqualsDoubleArray(hg.secondReadTotalsByCycle, new double[0], 1e-05);

GATKRead read2 = ArtificialReadUtils.createArtificialRead("aa".getBytes(), new byte[]{50, 50}, "2M");
read2.setAttribute(SAMTag.OQ.name(), SAMUtils.phredToFastq(new byte[]{30, 40}));
hg.addRead(read2);

assertEqualsLongArray(hg.firstReadCountsByCycle, new long[]{0, 1, 1});
Assert.assertEquals(hg.firstReadCountsByCycle, new long[]{0, 1, 1});
assertEqualsDoubleArray(hg.firstReadTotalsByCycle, new double[]{0, 30, 40}, 1e-05);
assertEqualsLongArray(hg.secondReadCountsByCycle, new long[]{0, 0, 0});
Assert.assertEquals(hg.secondReadCountsByCycle, new long[]{0, 0, 0});
assertEqualsDoubleArray(hg.secondReadTotalsByCycle, new double[]{0, 0, 0}, 1e-05);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import htsjdk.variant.variantcontext.*;
import htsjdk.variant.vcf.VCFConstants;
import org.apache.commons.math3.util.MathArrays;
import org.broadinstitute.hellbender.GATKBaseTest;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.broadinstitute.hellbender.utils.MathUtils;
import org.broadinstitute.hellbender.GATKBaseTest;
import org.broadinstitute.hellbender.utils.variant.GATKVCFConstants;
import org.broadinstitute.hellbender.utils.variant.HomoSapiensConstants;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressWarnings("unchecked")
public final class PosteriorProbabilitiesUtilsUnitTest extends GATKBaseTest {
Expand Down Expand Up @@ -98,17 +99,6 @@ private VariantContext makeDeletionVC(final String source, final List<Allele> al
return new VariantContextBuilder(source, "1", start, stop, alleles).genotypes(Arrays.asList(genotypes)).unfiltered().make();
}

private VariantContext makeHomRefBlock(final String source, final Allele refAllele, final Genotype... genotypes) {
final int start = 10;
final int stop = start;
final Map<String, Object> infoMap = new HashMap<>();
infoMap.put(VCFConstants.END_KEY,100);
final List<Allele> alleles = new ArrayList<>();
alleles.add(refAllele);
alleles.add(Allele.NON_REF_ALLELE);
return new VariantContextBuilder(source, "1", start, stop, alleles).genotypes(Arrays.asList(genotypes)).unfiltered().attributes(infoMap).make();
}

@Test
public void testCalculatePosteriorNoExternalData() {
final int numSamples = 12;
Expand Down Expand Up @@ -541,24 +531,6 @@ public void testCalculatePosterior() {
}
}

private boolean arraysApproxEqual(final double[] a, final double[] b, final double tol) {
if ( a.length != b.length ) {
return false;
}

for ( int idx = 0; idx < a.length; idx++ ) {
if ( Math.abs(a[idx]-b[idx]) > tol ) {
return false;
}
}

return true;
}

private String errMsgArray(final double[] a, final double[] b) {
return String.format("Expected %s, Observed %s", Arrays.toString(a), Arrays.toString(b));
}

@Test
public void testPosteriorMultiAllelic() {
// AA AB BB AC BC CC AD BD CD DD
Expand Down Expand Up @@ -591,12 +563,12 @@ public void testPosteriorMultiAllelic() {
-3.8952723, -1.5445506, -3.4951749, -2.6115263, -2.9125508, -0.5618292, -2.2135895,
-1.5316722};

Assert.assertTrue(arraysApproxEqual(expecPrior5, PosteriorProbabilitiesUtils.getDirichletPrior(counts_five,2,false),1e-5),errMsgArray(expecPrior5, PosteriorProbabilitiesUtils.getDirichletPrior(counts_five,2,false)));
assertEqualsDoubleArray(expecPrior5, PosteriorProbabilitiesUtils.getDirichletPrior(counts_five,2,false),1e-5);

Assert.assertTrue(arraysApproxEqual(expected_one,post1,1e-6),errMsgArray(expected_one,post1));
Assert.assertTrue(arraysApproxEqual(expected_two,post2,1e-5),errMsgArray(expected_two,post2));
Assert.assertTrue(arraysApproxEqual(expected_three,post3,1e-5),errMsgArray(expected_three,post3));
Assert.assertTrue(arraysApproxEqual(expected_four,post4,1e-5),errMsgArray(expected_four,post4));
Assert.assertTrue(arraysApproxEqual(expected_five,post5,1e-5),errMsgArray(expected_five,post5));
assertEqualsDoubleArray(expected_one,post1,1e-6);
assertEqualsDoubleArray(expected_two,post2,1e-5);
assertEqualsDoubleArray(expected_three,post3,1e-5);
assertEqualsDoubleArray(expected_four,post4,1e-5);
assertEqualsDoubleArray(expected_five,post5,1e-5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,22 +336,6 @@ protected static void assertEqualsDoubleArray(final double[] actual, final doubl
Assert.assertEquals(actual[i],expected[i],tolerance,"array position " + i);
}

/**
* Checks whether two long arrays contain the same values or not.
* @param actual actual produced array.
* @param expected expected array.
*/
protected static void assertEqualsLongArray(final long[] actual, final long[] expected) {
if (expected == null)
Assert.assertNull(actual);
else {
Assert.assertNotNull(actual);
Assert.assertEquals(actual.length, expected.length,"array length ");
}
for (int i = 0; i < actual.length; i++)
Assert.assertEquals(actual[i],expected[i],"array position " + i);
}

public static void assertEqualsDoubleSmart(final Object actual, final Double expected, final double tolerance) {
Assert.assertTrue(actual instanceof Double, "Not a double");
assertEqualsDoubleSmart((double) (Double) actual, (double) expected, tolerance);
Expand Down

0 comments on commit 6930a0f

Please sign in to comment.