/**
  * Builds a genotype-allele-counts array given the genotype ploidy and how many genotype you need.
  *
  * <p>The result is guarantee to have exactly {@code length} positions and the elements are sorted
  * in agreement with the standard way to display genotypes following the VCF standard.
  *
  * <p>Notice that is possible to request ploidy ==0. In that case the resulting array will have
  * repetitions of the empty genotype allele count.
  *
  * <p>For example,
  *
  * <pre>
  *         ploidy = 1, length = 5 : [ {A}, {B}, {C}, {D}, {E} ]
  *         ploidy = 2, length = 7 : [ {AA}, {AB}, {BB}, {AC}, {BC}, {CC}, {AD}
  *         ploidy = 3, length = 10 : [ {AAA}, {AAB}, {ABB}, {BBB}, {AAC}, {ABC}, {BBC}, {BCC}, {CCC}, {AAD} ]
  *     </pre>
  *
  * @param ploidy requested ploidy.
  * @param alleleCount number of different alleles that the genotype table must support.
  * @param genotypeOffsetTable table with the offset of the first genotype that contain an allele
  *     given the ploidy and its index.
  * @throws IllegalArgumentException if {@code ploidy} or {@code length} is negative.
  * @return never {@code null}, follows the specification above.
  */
 private static GenotypeAlleleCounts[] buildGenotypeAlleleCountsArray(
     final int ploidy, final int alleleCount, final int[][] genotypeOffsetTable) {
   if (ploidy < 0) {
     throw new IllegalArgumentException("the requested ploidy cannot be negative: " + ploidy);
   }
   if (alleleCount < 0) {
     throw new IllegalArgumentException(
         "the requested maximum allele cannot be negative: " + alleleCount);
   }
   final int length = genotypeOffsetTable[ploidy][alleleCount];
   final int strongRefLength =
       length == GENOTYPE_COUNT_OVERFLOW
           ? MAXIMUM_STRONG_REF_GENOTYPE_PER_PLOIDY
           : Math.min(length, MAXIMUM_STRONG_REF_GENOTYPE_PER_PLOIDY);
   final GenotypeAlleleCounts[] result = new GenotypeAlleleCounts[strongRefLength];
   result[0] = GenotypeAlleleCounts.first(ploidy);
   for (int genotypeIndex = 1; genotypeIndex < strongRefLength; genotypeIndex++) {
     result[genotypeIndex] = result[genotypeIndex - 1].next();
   }
   return result;
 }
Ejemplo n.º 2
0
  @Test(dataProvider = "ploidyData")
  public void testFirst(final int ploidy) {
    final GenotypeAlleleCounts subject = GenotypeAlleleCounts.first(ploidy);
    Assert.assertNotNull(subject);
    Assert.assertEquals(subject.ploidy(), ploidy);
    Assert.assertEquals(subject.distinctAlleleCount(), 1);
    Assert.assertEquals(subject.alleleCountAt(0), ploidy);
    Assert.assertEquals(subject.alleleCountFor(0), ploidy);
    Assert.assertEquals(subject.alleleRankFor(0), 0);
    Assert.assertEquals(subject.alleleRankFor(1), -2);
    Assert.assertTrue(subject.containsAllele(0));
    Assert.assertFalse(subject.containsAllele(1));
    Assert.assertEquals(subject.alleleIndexAt(0), 0);
    Assert.assertEquals(subject.maximumAlleleIndex(), 0);
    Assert.assertEquals(subject.minimumAlleleIndex(), 0);
    Assert.assertTrue(subject.compareTo(subject) == 0);
    Assert.assertTrue(subject.equals(subject));
    Assert.assertEquals(subject.index(), 0);
    Assert.assertEquals(
        subject.asAlleleList(testAlleles), Collections.nCopies(ploidy, testAlleles.get(0)));
    for (int maximumAlleleIndex = 0;
        maximumAlleleIndex <= MAXIMUM_ALLELE_INDEX;
        maximumAlleleIndex++) {
      final int[] expected = new int[maximumAlleleIndex + 1];
      expected[0] = ploidy;
      Assert.assertEquals(subject.alleleCountsByIndex(maximumAlleleIndex), expected);
    }

    Assert.assertNotNull(subject.toString());
    Assert.assertEquals(
        subject.toUnphasedGenotypeString(),
        ploidy == 1 ? "0" : Strings.repeat("0/", ploidy - 1) + "0");
  }
Ejemplo n.º 3
0
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testAlleleCountsByIndexError() {
   final GenotypeAlleleCounts first = GenotypeAlleleCounts.first(2);
   first.alleleCountsByIndex(-1);
 }
Ejemplo n.º 4
0
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testCopyAlleleCountsOffsetError() {
   final GenotypeAlleleCounts first = GenotypeAlleleCounts.first(2);
   first.copyAlleleCounts(new int[3], 4);
 }
Ejemplo n.º 5
0
  private void testPloidyTwoOrMoreIncrease(final int ploidy) {
    if (ploidy < 2) throw new IllegalArgumentException();

    GenotypeAlleleCounts next = GenotypeAlleleCounts.first(ploidy);

    while (!next.containsAllele(MAXIMUM_ALLELE_INDEX + 1)) {
      final GenotypeAlleleCounts current = next.copy();
      next.increase();
      if (current.distinctAlleleCount() == 1) {
        Assert.assertEquals(next.maximumAlleleIndex(), current.maximumAlleleIndex() + 1);
        Assert.assertEquals(next.distinctAlleleCount(), 2);
        Assert.assertEquals(next.minimumAlleleIndex(), 0);
      } else {
        Assert.assertEquals(next.maximumAlleleIndex(), current.maximumAlleleIndex());
        Assert.assertEquals(
            next.minimumAlleleIndex(),
            current.alleleCountAt(0) > 1
                ? 0
                : current.alleleCountAt(0) == 1
                    ? current.minimumAlleleIndex() + 1
                    : current.minimumAlleleIndex());
      }

      // Checking on 0's new count and current.minAllele + 1 alleles.
      Assert.assertEquals(
          next.alleleCountFor(0), current.alleleCountFor(current.minimumAlleleIndex()) - 1);
      Assert.assertEquals(
          next.alleleCountFor(current.minimumAlleleIndex() + 1),
          current.alleleCountFor(current.minimumAlleleIndex() + 1) + 1);

      // Checks current.minAllele count
      Assert.assertEquals(
          next.alleleCountFor(current.minimumAlleleIndex()),
          current.minimumAlleleIndex() == 0 ? current.alleleCountAt(0) - 1 : 0);

      int totalCountSum = 0;
      final int[] expectedAlleleCountsByIndex =
          new int[Math.max(MAXIMUM_ALLELE_INDEX, next.maximumAlleleIndex()) + 1];
      for (int i = 0; i < next.distinctAlleleCount(); i++) {
        final int count = next.alleleCountAt(i);
        final int index = next.alleleIndexAt(i);
        expectedAlleleCountsByIndex[index] = count;
        // Check consistency of alleleCountAt(x) and alleleCountFor(alleleIndexAt(x))
        Assert.assertEquals(next.alleleCountFor(index), count);
        totalCountSum += count;
        // Check on counts of, in theory, unaffected allele counts.
        if (index > current.minimumAlleleIndex() + 1)
          Assert.assertEquals(next.alleleCountFor(index), current.alleleCountFor(index));
      }
      Assert.assertTrue(
          Arrays.equals(
              next.alleleCountsByIndex(Math.max(MAXIMUM_ALLELE_INDEX, next.maximumAlleleIndex())),
              expectedAlleleCountsByIndex));
      Assert.assertEquals(totalCountSum, ploidy);

      Assert.assertTrue(next.compareTo(current) > 0);
      Assert.assertTrue(current.compareTo(next) < 0);
      Assert.assertTrue(next.compareTo(next) == 0);
      Assert.assertTrue(next.equals(next));
      Assert.assertFalse(next.equals(current));
      Assert.assertFalse(current.equals(next));
      Assert.assertEquals(next.index(), current.index() + 1);
      Assert.assertEquals(next.ploidy(), ploidy);
    }
  }
Ejemplo n.º 6
0
  private void testNextOnePloidyIncrease() {
    final GenotypeAlleleCounts first = GenotypeAlleleCounts.first(1);
    GenotypeAlleleCounts next = first;

    while (!next.containsAllele(MAXIMUM_ALLELE_INDEX + 1)) {
      final GenotypeAlleleCounts current = next.copy();
      next.increase(1);
      Assert.assertEquals(next.minimumAlleleIndex(), next.maximumAlleleIndex());
      Assert.assertEquals(next.minimumAlleleIndex(), current.minimumAlleleIndex() + 1);
      Assert.assertEquals(next.alleleCountAt(0), 1);
      Assert.assertEquals(next.alleleIndexAt(0), next.minimumAlleleIndex());
      Assert.assertEquals(next.alleleRankFor(next.minimumAlleleIndex()), 0);
      Assert.assertEquals(next.alleleRankFor(next.minimumAlleleIndex() + 1), -2);
      Assert.assertEquals(next.alleleCountFor(next.minimumAlleleIndex()), 1);
      Assert.assertEquals(next.alleleCountFor(next.minimumAlleleIndex() + 1), 0);
      Assert.assertEquals(next.ploidy(), 1);

      Assert.assertTrue(next.compareTo(current) > 0);
      Assert.assertTrue(current.compareTo(next) < 0);
      Assert.assertTrue(next.compareTo(next) == 0);
      Assert.assertTrue(next.equals(next));
      Assert.assertFalse(next.equals(current));
      Assert.assertFalse(current.equals(next));

      Assert.assertEquals(next.index(), current.index() + 1);
      Assert.assertEquals(next.ploidy(), current.ploidy());

      for (int maximumAlleleIndex = 0;
          maximumAlleleIndex <= MAXIMUM_ALLELE_INDEX;
          maximumAlleleIndex++) {
        final int[] expected = new int[maximumAlleleIndex + 1];
        if (maximumAlleleIndex >= current.minimumAlleleIndex() + 1)
          expected[current.minimumAlleleIndex() + 1] = 1;
        Assert.assertEquals(next.alleleCountsByIndex(maximumAlleleIndex), expected);
      }
    }
  }
Ejemplo n.º 7
0
 private void testNextZeroPloidyIncrease() {
   final GenotypeAlleleCounts first = GenotypeAlleleCounts.first(0);
   final GenotypeAlleleCounts next = first.copy();
   next.increase();
   Assert.assertEquals(first, next);
   Assert.assertEquals(first.compareTo(next), 0);
   Assert.assertEquals(next.compareTo(first), 0);
   Assert.assertEquals(next.distinctAlleleCount(), 0);
   Assert.assertEquals(next.ploidy(), 0);
   Assert.assertEquals(next.index(), 0);
   for (int maximumAlleleIndex = 0; maximumAlleleIndex <= 10; maximumAlleleIndex++) {
     final int[] expected = new int[maximumAlleleIndex + 1];
     Assert.assertEquals(next.alleleCountsByIndex(maximumAlleleIndex), expected);
   }
 }
Ejemplo n.º 8
0
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testFirstError() {
   GenotypeAlleleCounts.first(-1);
 }
Ejemplo n.º 9
0
  private void testNextOnePloidy() {
    final GenotypeAlleleCounts ploidy2 = GenotypeAlleleCounts.first(2);
    final GenotypeAlleleCounts first = GenotypeAlleleCounts.first(1);
    GenotypeAlleleCounts current = first;

    while (!current.containsAllele(MAXIMUM_ALLELE_INDEX + 1)) {
      final GenotypeAlleleCounts next = current.next();
      Assert.assertEquals(next.minimumAlleleIndex(), next.maximumAlleleIndex());
      Assert.assertEquals(next.minimumAlleleIndex(), current.minimumAlleleIndex() + 1);
      Assert.assertEquals(next.alleleCountAt(0), 1);
      Assert.assertEquals(next.alleleIndexAt(0), next.minimumAlleleIndex());
      Assert.assertEquals(next.alleleRankFor(next.minimumAlleleIndex()), 0);
      Assert.assertEquals(next.alleleRankFor(next.minimumAlleleIndex() + 1), -2);
      Assert.assertEquals(next.alleleCountFor(next.minimumAlleleIndex()), 1);
      Assert.assertEquals(next.alleleCountFor(next.minimumAlleleIndex() + 1), 0);
      Assert.assertEquals(next.ploidy(), 1);

      int[] dest = new int[next.distinctAlleleCount() * 2];
      next.copyAlleleCounts(dest, 0);
      Assert.assertEquals(dest, new int[] {next.index(), 1});

      Assert.assertTrue(next.compareTo(current) > 0);
      Assert.assertTrue(current.compareTo(next) < 0);
      Assert.assertTrue(next.compareTo(next) == 0);
      Assert.assertTrue(next.equals(next));
      Assert.assertFalse(next.equals(null));
      Assert.assertFalse(next.equals(next.toString()));
      Assert.assertFalse(next.equals(ploidy2));
      Assert.assertFalse(ploidy2.equals(next));
      Assert.assertFalse(next.equals(current));
      Assert.assertNotEquals(next.hashCode(), current.hashCode());
      Assert.assertFalse(current.equals(next));

      Assert.assertEquals(next.index(), current.index() + 1);
      Assert.assertEquals(next.ploidy(), current.ploidy());

      Assert.assertEquals(
          next.asAlleleList(testAlleles),
          Collections.singletonList(testAlleles.get(next.maximumAlleleIndex())));

      for (int maximumAlleleIndex = 0;
          maximumAlleleIndex <= MAXIMUM_ALLELE_INDEX;
          maximumAlleleIndex++) {
        final int[] expected = new int[maximumAlleleIndex + 1];
        if (maximumAlleleIndex >= current.minimumAlleleIndex() + 1)
          expected[current.minimumAlleleIndex() + 1] = 1;
        Assert.assertEquals(next.alleleCountsByIndex(maximumAlleleIndex), expected);
      }
      current = next;
    }
  }