Esempio n. 1
0
  // protected basic manipulation routines
  private static List<Allele> makeAlleles(Collection<Allele> alleles) {
    final List<Allele> alleleList = new ArrayList<Allele>(alleles.size());

    boolean sawRef = false;
    for (final Allele a : alleles) {
      for (final Allele b : alleleList) {
        if (a.equals(b, true))
          throw new IllegalArgumentException("Duplicate allele added to VariantContext: " + a);
      }

      // deal with the case where the first allele isn't the reference
      if (a.isReference()) {
        if (sawRef)
          throw new IllegalArgumentException(
              "Alleles for a VariantContext must contain at most one reference allele: " + alleles);
        alleleList.add(0, a);
        sawRef = true;
      } else alleleList.add(a);
    }

    if (alleleList.isEmpty())
      throw new IllegalArgumentException(
          "Cannot create a VariantContext with an empty allele list");

    if (alleleList.get(0).isNonReference())
      throw new IllegalArgumentException(
          "Alleles for a VariantContext must contain at least one reference allele: " + alleles);

    return alleleList;
  }
  private static AlleleMapper resolveIncompatibleAlleles(
      Allele refAllele, VariantContext vc, Set<Allele> allAlleles) {
    if (refAllele.equals(vc.getReference())) return new AlleleMapper(vc);
    else {
      // we really need to do some work.  The refAllele is the longest reference allele seen at this
      // start site.  So imagine it is:
      //
      // refAllele: ACGTGA
      // myRef:     ACGT
      // myAlt:     -
      //
      // We need to remap all of the alleles in vc to include the extra GA so that
      // myRef => refAllele and myAlt => GA
      //

      Allele myRef = vc.getReference();
      if (refAllele.length() <= myRef.length())
        throw new ReviewedStingException(
            "BUG: myRef=" + myRef + " is longer than refAllele=" + refAllele);
      byte[] extraBases =
          Arrays.copyOfRange(refAllele.getBases(), myRef.length(), refAllele.length());

      //            System.out.printf("Remapping allele at %s%n", vc);
      //            System.out.printf("ref   %s%n", refAllele);
      //            System.out.printf("myref %s%n", myRef );
      //            System.out.printf("extrabases %s%n", new String(extraBases));

      Map<Allele, Allele> map = new HashMap<Allele, Allele>();
      for (Allele a : vc.getAlleles()) {
        if (a.isReference()) map.put(a, refAllele);
        else {
          Allele extended = Allele.extend(a, extraBases);
          for (Allele b : allAlleles) if (extended.equals(b)) extended = b;
          //                    System.out.printf("  Extending %s => %s%n", a, extended);
          map.put(a, extended);
        }
      }

      // debugging
      //            System.out.printf("mapping %s%n", map);

      return new AlleleMapper(map);
    }
  }
Esempio n. 3
0
  public boolean hasAllele(Allele allele, boolean ignoreRefState) {
    if (allele == REF || allele == ALT) // optimization for cached cases
    return true;

    for (Allele a : getAlleles()) {
      if (a.equals(allele, ignoreRefState)) return true;
    }

    return false;
  }
Esempio n. 4
0
  public int[] getGLIndecesOfAlternateAllele(Allele targetAllele) {

    int index = 1;
    for (Allele allele : getAlternateAlleles()) {
      if (allele.equals(targetAllele)) break;
      index++;
    }

    return GenotypeLikelihoods.getPLIndecesOfAlleles(0, index);
  }
Esempio n. 5
0
  static boolean doubleAllelesSegregatePerfectlyAmongSamples(
      VariantContext vc1, VariantContext vc2) {
    // Check that Alleles at vc1 and at vc2 always segregate together in all samples (including
    // reference):
    Map<Allele, Allele> allele1ToAllele2 = new HashMap<Allele, Allele>();
    Map<Allele, Allele> allele2ToAllele1 = new HashMap<Allele, Allele>();

    // Note the segregation of the alleles for the reference genome:
    allele1ToAllele2.put(vc1.getReference(), vc2.getReference());
    allele2ToAllele1.put(vc2.getReference(), vc1.getReference());

    // Note the segregation of the alleles for each sample (and check that it is consistent with the
    // reference and all previous samples).
    for (final Genotype gt1 : vc1.getGenotypes()) {
      Genotype gt2 = vc2.getGenotype(gt1.getSampleName());

      List<Allele> site1Alleles = gt1.getAlleles();
      List<Allele> site2Alleles = gt2.getAlleles();

      Iterator<Allele> all2It = site2Alleles.iterator();
      for (Allele all1 : site1Alleles) {
        Allele all2 = all2It.next();

        Allele all1To2 = allele1ToAllele2.get(all1);
        if (all1To2 == null) allele1ToAllele2.put(all1, all2);
        else if (!all1To2.equals(all2)) // all1 segregates with two different alleles at site 2
        return false;

        Allele all2To1 = allele2ToAllele1.get(all2);
        if (all2To1 == null) allele2ToAllele1.put(all2, all1);
        else if (!all2To1.equals(all1)) // all2 segregates with two different alleles at site 1
        return false;
      }
    }

    return true;
  }
  private static Allele determineReferenceAllele(List<VariantContext> VCs) {
    Allele ref = null;

    for (VariantContext vc : VCs) {
      Allele myRef = vc.getReference();
      if (ref == null || ref.length() < myRef.length()) ref = myRef;
      else if (ref.length() == myRef.length() && !ref.equals(myRef))
        throw new UserException.BadInput(
            String.format(
                "The provided variant file(s) have inconsistent references for the same position(s) at %s:%d, %s vs. %s",
                vc.getChr(), vc.getStart(), ref, myRef));
    }

    return ref;
  }
  private static final boolean hasPLIncompatibleAlleles(
      final Collection<Allele> alleleSet1, final Collection<Allele> alleleSet2) {
    final Iterator<Allele> it1 = alleleSet1.iterator();
    final Iterator<Allele> it2 = alleleSet2.iterator();

    while (it1.hasNext() && it2.hasNext()) {
      final Allele a1 = it1.next();
      final Allele a2 = it2.next();
      if (!a1.equals(a2)) return true;
    }

    // by this point, at least one of the iterators is empty.  All of the elements
    // we've compared are equal up until this point.  But it's possible that the
    // sets aren't the same size, which is indicated by the test below.  If they
    // are of the same size, though, the sets are compatible
    return it1.hasNext() || it2.hasNext();
  }