Ejemplo n.º 1
0
  public void writeBeagleOutput(
      VariantContext preferredVC, VariantContext otherVC, boolean isValidationSite, double prior) {
    GenomeLoc currentLoc =
        VariantContextUtils.getLocation(getToolkit().getGenomeLocParser(), preferredVC);
    StringBuffer beagleOut = new StringBuffer();

    String marker = String.format("%s:%d ", currentLoc.getContig(), currentLoc.getStart());
    beagleOut.append(marker);
    if (markers != null)
      markers.append(marker).append("\t").append(Integer.toString(markerCounter++)).append("\t");
    for (Allele allele : preferredVC.getAlleles()) {
      String bglPrintString;
      if (allele.isNoCall() || allele.isNull()) bglPrintString = "-";
      else bglPrintString = allele.getBaseString(); // get rid of * in case of reference allele

      beagleOut.append(String.format("%s ", bglPrintString));
      if (markers != null) markers.append(bglPrintString).append("\t");
    }
    if (markers != null) markers.append("\n");

    GenotypesContext preferredGenotypes = preferredVC.getGenotypes();
    GenotypesContext otherGenotypes = goodSite(otherVC) ? otherVC.getGenotypes() : null;
    for (String sample : samples) {
      boolean isMaleOnChrX = CHECK_IS_MALE_ON_CHR_X && getSample(sample).getGender() == Gender.MALE;

      Genotype genotype;
      boolean isValidation;
      // use sample as key into genotypes structure
      if (preferredGenotypes.containsSample(sample)) {
        genotype = preferredGenotypes.get(sample);
        isValidation = isValidationSite;
      } else if (otherGenotypes != null && otherGenotypes.containsSample(sample)) {
        genotype = otherGenotypes.get(sample);
        isValidation = !isValidationSite;
      } else {
        // there is magically no genotype for this sample.
        throw new StingException(
            "Sample "
                + sample
                + " arose with no genotype in variant or validation VCF. This should never happen.");
      }

      /*
       * Use likelihoods if: is validation, prior is negative; or: is not validation, has genotype key
       */
      double[] log10Likelihoods = null;
      if ((isValidation && prior < 0.0) || genotype.hasLikelihoods()) {
        log10Likelihoods = genotype.getLikelihoods().getAsVector();

        // see if we need to randomly mask out genotype in this position.
        if (GenomeAnalysisEngine.getRandomGenerator().nextDouble() <= insertedNoCallRate) {
          // we are masking out this genotype
          log10Likelihoods =
              isMaleOnChrX ? HAPLOID_FLAT_LOG10_LIKELIHOODS : DIPLOID_FLAT_LOG10_LIKELIHOODS;
        }

        if (isMaleOnChrX) {
          log10Likelihoods[1] = -255; // todo -- warning this is dangerous for multi-allele case
        }
      }
      /** otherwise, use the prior uniformly */
      else if (!isValidation && genotype.isCalled() && !genotype.hasLikelihoods()) {
        // hack to deal with input VCFs with no genotype likelihoods.  Just assume the called
        // genotype
        // is confident.  This is useful for Hapmap and 1KG release VCFs.
        double AA = (1.0 - prior) / 2.0;
        double AB = (1.0 - prior) / 2.0;
        double BB = (1.0 - prior) / 2.0;

        if (genotype.isHomRef()) {
          AA = prior;
        } else if (genotype.isHet()) {
          AB = prior;
        } else if (genotype.isHomVar()) {
          BB = prior;
        }

        log10Likelihoods = MathUtils.toLog10(new double[] {AA, isMaleOnChrX ? 0.0 : AB, BB});
      } else {
        log10Likelihoods =
            isMaleOnChrX ? HAPLOID_FLAT_LOG10_LIKELIHOODS : DIPLOID_FLAT_LOG10_LIKELIHOODS;
      }

      writeSampleLikelihoods(beagleOut, preferredVC, log10Likelihoods);
    }

    beagleWriter.println(beagleOut.toString());
  }
  private Map<String, Object> annotateIndel(AlignmentContext stratifiedContext, VariantContext vc) {

    if (!stratifiedContext.hasExtendedEventPileup()) {
      return null;
    }

    ReadBackedExtendedEventPileup pileup = stratifiedContext.getExtendedEventPileup();
    if (pileup == null) return null;
    int totalDepth = pileup.size();

    Map<String, Object> map = new HashMap<String, Object>();
    map.put(getKeyNames().get(0), totalDepth); // put total depth in right away

    if (totalDepth == 0) return map;
    int mq0 = 0; // number of "ref" reads that are acually mq0

    HashMap<String, Integer> alleleCounts = new HashMap<String, Integer>();
    Allele refAllele = vc.getReference();

    for (Allele allele : vc.getAlternateAlleles()) {

      if (allele.isNoCall()) {
        continue; // this does not look so good, should we die???
      }

      alleleCounts.put(getAlleleRepresentation(allele), 0);
    }

    for (ExtendedEventPileupElement e : pileup.toExtendedIterable()) {

      if (e.getMappingQual() == 0) {
        mq0++;
        continue;
      }

      if (e.isInsertion()) {

        final String b = e.getEventBases();
        if (alleleCounts.containsKey(b)) {
          alleleCounts.put(b, alleleCounts.get(b) + 1);
        }

      } else {
        if (e.isDeletion()) {
          if (e.getEventLength() == refAllele.length()) {
            // this is indeed the deletion allele recorded in VC
            final String b = DEL;
            if (alleleCounts.containsKey(b)) {
              alleleCounts.put(b, alleleCounts.get(b) + 1);
            }
          }
          //                    else {
          //                        System.out.print("   deletion of WRONG length found");
          //                    }
        }
      }
    }

    if (mq0 == totalDepth) return map;

    String[] fracs = new String[alleleCounts.size()];
    for (int i = 0; i < vc.getAlternateAlleles().size(); i++)
      fracs[i] =
          String.format(
              "%.3f",
              ((float) alleleCounts.get(getAlleleRepresentation(vc.getAlternateAllele(i))))
                  / (totalDepth - mq0));

    map.put(getKeyNames().get(1), fracs);

    // map.put(getKeyNames().get(0), counts);
    return map;
  }