private Collection<VariantContext> getVariantContexts(
      RefMetaDataTracker tracker, ReferenceContext ref) {

    List<Feature> features = tracker.getValues(variants, ref.getLocus());
    List<VariantContext> VCs = new ArrayList<VariantContext>(features.size());

    for (Feature record : features) {
      if (VariantContextAdaptors.canBeConvertedToVariantContext(record)) {
        // we need to special case the HapMap format because indels aren't handled correctly
        if (record instanceof RawHapMapFeature) {

          // is it an indel?
          RawHapMapFeature hapmap = (RawHapMapFeature) record;
          if (hapmap.getAlleles()[0].equals(RawHapMapFeature.NULL_ALLELE_STRING)
              || hapmap.getAlleles()[1].equals(RawHapMapFeature.NULL_ALLELE_STRING)) {
            // get the dbsnp object corresponding to this record (needed to help us distinguish
            // between insertions and deletions)
            VariantContext dbsnpVC = getDbsnp(hapmap.getName());
            if (dbsnpVC == null || dbsnpVC.isMixed()) continue;

            Map<String, Allele> alleleMap = new HashMap<String, Allele>(2);
            alleleMap.put(
                RawHapMapFeature.DELETION,
                Allele.create(ref.getBase(), dbsnpVC.isSimpleInsertion()));
            alleleMap.put(
                RawHapMapFeature.INSERTION,
                Allele.create(
                    (char) ref.getBase() + ((RawHapMapFeature) record).getAlleles()[1],
                    !dbsnpVC.isSimpleInsertion()));
            hapmap.setActualAlleles(alleleMap);

            // also, use the correct positioning for insertions
            hapmap.updatePosition(dbsnpVC.getStart());

            if (hapmap.getStart() < ref.getWindow().getStart()) {
              logger.warn(
                  "Hapmap record at "
                      + ref.getLocus()
                      + " represents an indel too large to be converted; skipping...");
              continue;
            }
          }
        }

        // ok, we might actually be able to turn this record in a variant context
        VariantContext vc =
            VariantContextAdaptors.toVariantContext(variants.getName(), record, ref);

        if (vc != null) // sometimes the track has odd stuff in it that can't be converted
        VCs.add(vc);
      }
    }

    return VCs;
  }
  /**
   * Process all remaining intervals
   *
   * @param result number of loci processed by the walker
   */
  @Override
  public void onTraversalDone(final Long result) {
    for (GenomeLoc interval : intervalMap.keySet())
      outputStatsToVCF(intervalMap.get(interval), UNCOVERED_ALLELE);

    GenomeLoc interval = intervalListIterator.peek();
    while (interval != null) {
      outputStatsToVCF(createIntervalStatistic(interval), UNCOVERED_ALLELE);
      intervalListIterator.next();
      interval = intervalListIterator.peek();
    }

    if (thresholds.missingTargets != null) {
      thresholds.missingTargets.close();
    }
  }
 /**
  * Adds all intervals that overlap the current reference locus to the intervalMap
  *
  * @param refLocus the current reference locus
  */
 private void addNewOverlappingIntervals(final GenomeLoc refLocus) {
   GenomeLoc interval = intervalListIterator.peek();
   while (interval != null && !interval.isPast(refLocus)) {
     intervalMap.put(interval, createIntervalStatistic(interval));
     intervalListIterator.next();
     interval = intervalListIterator.peek();
   }
 }
 /**
  * Outputs all intervals that are behind the current reference locus
  *
  * @param refLocus the current reference locus
  * @param refBase the reference allele
  */
 private void outputFinishedIntervals(final GenomeLoc refLocus, final byte refBase) {
   // output any intervals that were finished
   final List<GenomeLoc> toRemove = new LinkedList<>();
   for (GenomeLoc key : intervalMap.keySet()) {
     if (key.isBefore(refLocus)) {
       final IntervalStratification intervalStats = intervalMap.get(key);
       outputStatsToVCF(intervalStats, Allele.create(refBase, true));
       if (hasMissingLoci(intervalStats)) {
         outputMissingInterval(intervalStats);
       }
       toRemove.add(key);
     }
   }
   for (GenomeLoc key : toRemove) {
     intervalMap.remove(key);
   }
 }
  /**
   * Takes the interval, finds it in the stash, prints it to the VCF
   *
   * @param stats The statistics of the interval
   * @param refAllele the reference allele
   */
  private void outputStatsToVCF(final IntervalStratification stats, final Allele refAllele) {
    GenomeLoc interval = stats.getInterval();

    final List<Allele> alleles = new ArrayList<>();
    final Map<String, Object> attributes = new HashMap<>();
    final ArrayList<Genotype> genotypes = new ArrayList<>();

    for (String sample : samples) {
      final GenotypeBuilder gb = new GenotypeBuilder(sample);

      SampleStratification sampleStat = stats.getSampleStatistics(sample);
      gb.attribute(
          GATKVCFConstants.AVG_INTERVAL_DP_BY_SAMPLE_KEY,
          sampleStat.averageCoverage(interval.size()));
      gb.attribute(GATKVCFConstants.LOW_COVERAGE_LOCI, sampleStat.getNLowCoveredLoci());
      gb.attribute(GATKVCFConstants.ZERO_COVERAGE_LOCI, sampleStat.getNUncoveredLoci());
      gb.filters(statusToStrings(stats.getSampleStatistics(sample).callableStatuses(), false));

      genotypes.add(gb.make());
    }
    alleles.add(refAllele);
    alleles.add(SYMBOLIC_ALLELE);
    VariantContextBuilder vcb =
        new VariantContextBuilder(
            "DiagnoseTargets",
            interval.getContig(),
            interval.getStart(),
            interval.getStop(),
            alleles);

    vcb = vcb.log10PError(VariantContext.NO_LOG10_PERROR);
    vcb.filters(new LinkedHashSet<>(statusToStrings(stats.callableStatuses(), true)));

    attributes.put(VCFConstants.END_KEY, interval.getStop());
    attributes.put(GATKVCFConstants.AVG_INTERVAL_DP_KEY, stats.averageCoverage(interval.size()));
    attributes.put(GATKVCFConstants.INTERVAL_GC_CONTENT_KEY, stats.gcContent());

    vcb = vcb.attributes(attributes);
    vcb = vcb.genotypes(genotypes);

    vcfWriter.add(vcb.make());
  }
  /**
   * For each variant in the file, determine the phasing for the child and replace the child's
   * genotype with the trio's genotype
   *
   * @param tracker the reference meta-data tracker
   * @param ref the reference context
   * @param context the alignment context
   * @return null
   */
  @Override
  public Integer map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) {
    if (tracker != null) {
      for (VariantContext vc :
          tracker.getValues(variantCollection.variants, context.getLocation())) {
        vc = vc.subContextFromSamples(samples);
        if (!vc.isPolymorphicInSamples()) continue;

        double log10pSomatic = calcLog10pSomatic(vc);

        // write in the somatic status probability
        Map<String, Object> attrs = new HashMap<String, Object>(); // vc.getAttributes());
        if (!minimalVCF) attrs.putAll(vc.getAttributes());
        attrs.put(SOMATIC_LOD_TAG_NAME, log10pSomatic);
        if (log10pSomatic > somaticMinLOD) {
          attrs.put(VCFConstants.SOMATIC_KEY, true);
          attrs.put(SOMATIC_NONREF_TAG_NAME, calculateTumorNNR(vc));
          attrs.put(SOMATIC_AC_TAG_NAME, calculateTumorAC(vc));
        }
        final VariantContextBuilder builder = new VariantContextBuilder(vc).attributes(attrs);
        VariantContextUtils.calculateChromosomeCounts(builder, false);
        VariantContext newvc = builder.make();

        vcfWriter.add(newvc);
      }

      return null;
    }

    return null;
  }
  @Override
  public Long map(
      final RefMetaDataTracker tracker,
      final ReferenceContext ref,
      final AlignmentContext context) {
    GenomeLoc refLocus = ref.getLocus();

    // process and remove any intervals in the map that are don't overlap the current locus anymore
    // and add all new intervals that may overlap this reference locus
    addNewOverlappingIntervals(refLocus);
    outputFinishedIntervals(refLocus, ref.getBase());

    // at this point, all intervals in intervalMap overlap with this locus, so update all of them
    for (IntervalStratification intervalStratification : intervalMap.values())
      intervalStratification.addLocus(context, ref);

    return 1L;
  }
Example #8
0
 private void addAnnotation(Map<String, Object> annotations, String keyName, String keyValue) {
   // Only add annotations for keys associated with non-empty values:
   if (keyValue != null && keyValue.trim().length() > 0) {
     annotations.put(keyName, keyValue);
   }
 }