/**
   * Helper function to add the read underneath a pileup element to the map
   *
   * @param p Pileup element
   * @param a Corresponding allele
   * @param likelihood Allele likelihood
   */
  public void add(PileupElement p, Allele a, Double likelihood) {
    if (p == null) throw new IllegalArgumentException("Pileup element cannot be null");
    if (p.getRead() == null)
      throw new IllegalArgumentException("Read underlying pileup element cannot be null");
    if (a == null) throw new IllegalArgumentException("Allele for add() cannot be null");

    add(p.getRead(), a, likelihood);
  }
  public Event map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) {

    boolean hasIndel = false;
    boolean hasInsertion = false;
    boolean hasPointEvent = false;

    int furthestStopPos = -1;

    // look at the rods for indels or SNPs
    if (tracker != null) {
      for (VariantContext vc : tracker.getValues(known)) {
        switch (vc.getType()) {
          case INDEL:
            hasIndel = true;
            if (vc.isSimpleInsertion()) hasInsertion = true;
            break;
          case SNP:
            hasPointEvent = true;
            break;
          case MIXED:
            hasPointEvent = true;
            hasIndel = true;
            if (vc.isSimpleInsertion()) hasInsertion = true;
            break;
          default:
            break;
        }
        if (hasIndel) furthestStopPos = vc.getEnd();
      }
    }

    // look at the normal context to get deletions and positions with high entropy
    final ReadBackedPileup pileup = context.getBasePileup();

    int mismatchQualities = 0, totalQualities = 0;
    final byte refBase = ref.getBase();
    for (PileupElement p : pileup) {

      // check the ends of the reads to see how far they extend
      furthestStopPos = Math.max(furthestStopPos, p.getRead().getAlignmentEnd());

      // is it a deletion or insertion?
      if (p.isDeletion() || p.isBeforeInsertion()) {
        hasIndel = true;
        if (p.isBeforeInsertion()) hasInsertion = true;
      }

      // look for mismatches
      else if (lookForMismatchEntropy) {
        if (p.getBase() != refBase) mismatchQualities += p.getQual();
        totalQualities += p.getQual();
      }
    }

    // make sure we're supposed to look for high entropy
    if (lookForMismatchEntropy
        && pileup.getNumberOfElements() >= minReadsAtLocus
        && (double) mismatchQualities / (double) totalQualities >= mismatchThreshold)
      hasPointEvent = true;

    // return null if no event occurred
    if (!hasIndel && !hasPointEvent) return null;

    // return null if we didn't find any usable reads/rods associated with the event
    if (furthestStopPos == -1) return null;

    GenomeLoc eventLoc = context.getLocation();
    if (hasInsertion)
      eventLoc =
          getToolkit()
              .getGenomeLocParser()
              .createGenomeLoc(eventLoc.getContig(), eventLoc.getStart(), eventLoc.getStart() + 1);

    EVENT_TYPE eventType =
        (hasIndel
            ? (hasPointEvent ? EVENT_TYPE.BOTH : EVENT_TYPE.INDEL_EVENT)
            : EVENT_TYPE.POINT_EVENT);

    return new Event(eventLoc, furthestStopPos, eventType);
  }
 @Override
 protected boolean isUsableBase(final PileupElement p) {
   return super.isUsableBase(p) && p.getRead().getCigar() != null;
 }
 @Override
 protected Double getElementForPileupElement(final PileupElement p) {
   final int offset = AlignmentUtils.calcAlignmentByteArrayOffset(p.getRead().getCigar(), p, 0, 0);
   return (double) AnnotationUtils.getFinalVariantReadPosition(p.getRead(), offset);
 }
  public Map<Allele, Double> getLikelihoodsAssociatedWithPileupElement(final PileupElement p) {
    if (!likelihoodReadMap.containsKey(p.getRead())) return null;

    return likelihoodReadMap.get(p.getRead());
  }
 /**
  * Does the current map contain the key associated with a particular SAM record in pileup?
  *
  * @param p Pileup element
  * @return true if the map contains pileup element, else false
  */
 public boolean containsPileupElement(final PileupElement p) {
   return likelihoodReadMap.containsKey(p.getRead());
 }