Search in sources :

Example 21 with Allele

use of htsjdk.variant.variantcontext.Allele in project gatk by broadinstitute.

the class AlleleListUnitTest method testShufflePermutation.

@Test(dataProvider = "singleAlleleListData", dependsOnMethods = { "testAsList", "testEquals" })
public void testShufflePermutation(final List<Allele> alleles1) {
    final AlleleList<Allele> originalAlleleList = new IndexedAlleleList<>(alleles1);
    if (originalAlleleList.numberOfAlleles() <= 1) {
        //return because this input is invalid for this test
        return;
    }
    final Allele[] targetAlleleArray = originalAlleleList.asListOfAlleles().toArray(new Allele[originalAlleleList.numberOfAlleles()]);
    final int[] fromIndex = new int[targetAlleleArray.length];
    for (int i = 0; i < fromIndex.length; i++) fromIndex[i] = i;
    for (int i = 0; i < targetAlleleArray.length - 1; i++) {
        final int swapIndex = rnd.nextInt(targetAlleleArray.length - i - 1);
        final int otherIndex = fromIndex[swapIndex + i + 1];
        final Allele other = targetAlleleArray[swapIndex + i + 1];
        fromIndex[swapIndex + i + 1] = fromIndex[i];
        fromIndex[i] = otherIndex;
        targetAlleleArray[swapIndex + i + 1] = targetAlleleArray[i];
        targetAlleleArray[i] = other;
    }
    final AlleleList<Allele> targetAlleleList = new IndexedAlleleList<>(targetAlleleArray);
    final AlleleListPermutation<Allele> permutation = originalAlleleList.permutation(targetAlleleList);
    Assert.assertFalse(permutation.isNonPermuted());
    AlleleListUnitTester.assertAlleleList(originalAlleleList, permutation.fromList());
    AlleleListUnitTester.assertAlleleList(targetAlleleList, permutation.toList());
    Assert.assertFalse(permutation.isPartial());
    Assert.assertEquals(permutation.fromSize(), originalAlleleList.numberOfAlleles());
    Assert.assertEquals(permutation.toSize(), targetAlleleList.numberOfAlleles());
    for (int i = 0; i < permutation.fromSize(); i++) {
        Assert.assertEquals(permutation.toIndex(i), targetAlleleList.indexOfAllele(originalAlleleList.getAllele(i)));
        Assert.assertEquals(permutation.fromIndex(i), originalAlleleList.indexOfAllele(targetAlleleList.getAllele(i)));
        Assert.assertEquals(permutation.fromIndex(i), fromIndex[i]);
    }
    Assert.assertTrue(AlleleList.equals(permutation, targetAlleleList));
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) Test(org.testng.annotations.Test)

Example 22 with Allele

use of htsjdk.variant.variantcontext.Allele in project gatk by broadinstitute.

the class ReadLikelihoodsUnitTest method testAlleleQueries.

private void testAlleleQueries(final AlleleList<Allele> alleles, ReadLikelihoods<Allele> result) {
    final Set<Integer> alleleIndices = new LinkedHashSet<>();
    for (final Allele allele : alleles.asListOfAlleles()) {
        final int indexOfAllele = result.indexOfAllele(allele);
        Assert.assertTrue(indexOfAllele >= 0);
        Assert.assertFalse(alleleIndices.contains(indexOfAllele));
        alleleIndices.add(indexOfAllele);
        Assert.assertSame(allele, alleles.getAllele(indexOfAllele));
    }
}
Also used : Allele(htsjdk.variant.variantcontext.Allele)

Example 23 with Allele

use of htsjdk.variant.variantcontext.Allele in project gatk by broadinstitute.

the class ReadLikelihoodsUnitTest method testAlleleQueries.

private void testAlleleQueries(Allele[] alleles, ReadLikelihoods<Allele> result) {
    final Set<Integer> alleleIndices = new LinkedHashSet<>();
    for (final Allele allele : alleles) {
        final int indexOfAllele = result.indexOfAllele(allele);
        Assert.assertTrue(indexOfAllele >= 0);
        Assert.assertFalse(alleleIndices.contains(indexOfAllele));
        alleleIndices.add(indexOfAllele);
        Assert.assertSame(allele, alleles[indexOfAllele]);
    }
}
Also used : Allele(htsjdk.variant.variantcontext.Allele)

Example 24 with Allele

use of htsjdk.variant.variantcontext.Allele in project gatk by broadinstitute.

the class ReadLikelihoodsUnitTest method testMarginalization.

@Test(dataProvider = "marginalizationDataSets")
public void testMarginalization(final String[] samples, final Allele[] alleles, final Map<String, List<GATKRead>> reads, final Map<Allele, List<Allele>> newToOldAlleleMapping) {
    final ReadLikelihoods<Allele> original = new ReadLikelihoods<>(new IndexedSampleList(samples), new IndexedAlleleList<>(alleles), reads);
    fillWithRandomLikelihoods(samples, alleles, original);
    final ReadLikelihoods<Allele> marginalized = original.marginalize(newToOldAlleleMapping);
    Assert.assertNotNull(marginalized);
    Assert.assertEquals(newToOldAlleleMapping.size(), marginalized.numberOfAlleles());
    for (int a = 0; a < marginalized.numberOfAlleles(); a++) {
        final List<Allele> oldAlleles = newToOldAlleleMapping.get(marginalized.getAllele(a));
        Assert.assertNotNull(oldAlleles);
        for (int s = 0; s < samples.length; s++) {
            final LikelihoodMatrix<Allele> oldSmapleLikelihoods = original.sampleMatrix(s);
            final LikelihoodMatrix<Allele> sampleLikelihoods = marginalized.sampleMatrix(s);
            final int sampleReadCount = sampleLikelihoods.numberOfReads();
            final int oldSampleReadCount = oldSmapleLikelihoods.numberOfReads();
            Assert.assertEquals(oldSampleReadCount, sampleReadCount);
            for (int r = 0; r < sampleReadCount; r++) {
                double oldBestLk = Double.NEGATIVE_INFINITY;
                for (final Allele oldAllele : oldAlleles) {
                    oldBestLk = Math.max(oldSmapleLikelihoods.get(original.indexOfAllele(oldAllele), r), oldBestLk);
                }
                Assert.assertEquals(sampleLikelihoods.get(a, r), oldBestLk);
            }
        }
    }
}
Also used : Allele(htsjdk.variant.variantcontext.Allele) Test(org.testng.annotations.Test)

Example 25 with Allele

use of htsjdk.variant.variantcontext.Allele in project gatk by broadinstitute.

the class ReadLikelihoodsUnitTest method testBestAlleleMap.

@Test(dataProvider = "dataSets")
public void testBestAlleleMap(final String[] samples, final Allele[] alleles, final Map<String, List<GATKRead>> reads) {
    final ReadLikelihoods<Allele> original = new ReadLikelihoods<>(new IndexedSampleList(samples), new IndexedAlleleList<>(alleles), reads);
    fillWithRandomLikelihoods(samples, alleles, original);
    final Map<Allele, List<GATKRead>> expected = new LinkedHashMap<>(alleles.length);
    for (final Allele allele : alleles) expected.put(allele, new ArrayList<>());
    final int numberOfAlleles = alleles.length;
    for (int s = 0; s < samples.length; s++) {
        final int sampleReadCount = original.sampleReadCount(s);
        final LikelihoodMatrix<Allele> sampleMatrix = original.sampleMatrix(s);
        for (int r = 0; r < sampleReadCount; r++) {
            int bestindexOfAllele = -1;
            double bestAlleleLk = Double.NEGATIVE_INFINITY;
            double secondBestAlleleLk = Double.NEGATIVE_INFINITY;
            for (int a = 0; a < numberOfAlleles; a++) {
                final double lk = sampleMatrix.get(a, r);
                if (lk > bestAlleleLk) {
                    secondBestAlleleLk = bestAlleleLk;
                    bestAlleleLk = lk;
                    bestindexOfAllele = a;
                } else if (lk > secondBestAlleleLk) {
                    secondBestAlleleLk = lk;
                }
            }
            if ((bestAlleleLk - secondBestAlleleLk) > ReadLikelihoods.BestAllele.INFORMATIVE_THRESHOLD)
                expected.get(alleles[bestindexOfAllele]).add(sampleMatrix.getRead(r));
        }
    }
    final Map<Allele, List<GATKRead>> actual = original.readsByBestAlleleMap();
    Assert.assertEquals(actual.size(), alleles.length);
    for (final Allele allele : alleles) {
        final List<GATKRead> expectedList = expected.get(allele);
        final List<GATKRead> actualList = actual.get(allele);
        final Set<GATKRead> expectedSet = new LinkedHashSet<>(expectedList);
        final Set<GATKRead> actualSet = new LinkedHashSet<>(actualList);
        Assert.assertEquals(actualSet, expectedSet);
    }
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Allele(htsjdk.variant.variantcontext.Allele) Test(org.testng.annotations.Test)

Aggregations

Allele (htsjdk.variant.variantcontext.Allele)91 Test (org.testng.annotations.Test)48 VariantContext (htsjdk.variant.variantcontext.VariantContext)44 VariantContextBuilder (htsjdk.variant.variantcontext.VariantContextBuilder)27 Genotype (htsjdk.variant.variantcontext.Genotype)26 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)22 java.util (java.util)19 Collectors (java.util.stream.Collectors)16 IntStream (java.util.stream.IntStream)14 ReferenceContext (org.broadinstitute.hellbender.engine.ReferenceContext)13 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)12 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)12 File (java.io.File)11 StandardArgumentDefinitions (org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions)11 ReadLikelihoods (org.broadinstitute.hellbender.utils.genotyper.ReadLikelihoods)11 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)11 GenotypeBuilder (htsjdk.variant.variantcontext.GenotypeBuilder)10 VCFConstants (htsjdk.variant.vcf.VCFConstants)10 IOException (java.io.IOException)10 Target (org.broadinstitute.hellbender.tools.exome.Target)10