private static Map<String, GenotypeLikelihoodsCalculationModel>
      getGenotypeLikelihoodsCalculationObject(Logger logger, UnifiedArgumentCollection UAC) {

    final Map<String, GenotypeLikelihoodsCalculationModel> glcm =
        new HashMap<String, GenotypeLikelihoodsCalculationModel>();
    final List<Class<? extends GenotypeLikelihoodsCalculationModel>> glmClasses =
        new PluginManager<GenotypeLikelihoodsCalculationModel>(
                GenotypeLikelihoodsCalculationModel.class)
            .getPlugins();

    for (int i = 0; i < glmClasses.size(); i++) {
      final Class<? extends GenotypeLikelihoodsCalculationModel> glmClass = glmClasses.get(i);
      final String key =
          glmClass
              .getSimpleName()
              .replaceAll("GenotypeLikelihoodsCalculationModel", "")
              .toUpperCase();
      try {
        final Object args[] = new Object[] {UAC, logger};
        final Constructor c =
            glmClass.getDeclaredConstructor(UnifiedArgumentCollection.class, Logger.class);
        glcm.put(key, (GenotypeLikelihoodsCalculationModel) c.newInstance(args));
      } catch (Exception e) {
        throw new UserException(
            "The likelihoods model provided for the -glm argument ("
                + UAC.GLmodel
                + ") is not a valid option: "
                + e.getMessage());
      }
    }

    return glcm;
  }
  private VariantCallContext estimateReferenceConfidence(
      VariantContext vc,
      Map<String, AlignmentContext> contexts,
      double theta,
      boolean ignoreCoveredSamples,
      double initialPofRef) {
    if (contexts == null) return null;

    double P_of_ref = initialPofRef;

    // for each sample that we haven't examined yet
    for (String sample : samples) {
      boolean isCovered = contexts.containsKey(sample);
      if (ignoreCoveredSamples && isCovered) continue;

      int depth = 0;

      if (isCovered) {
        depth = contexts.get(sample).getBasePileup().depthOfCoverage();
      }

      P_of_ref *= 1.0 - (theta / 2.0) * getRefBinomialProb(depth);
    }

    return new VariantCallContext(
        vc,
        QualityUtils.phredScaleErrorRate(1.0 - P_of_ref) >= UAC.STANDARD_CONFIDENCE_FOR_CALLING,
        false);
  }
Example #3
0
  static Map<String, Object> mergeVariantContextAttributes(VariantContext vc1, VariantContext vc2) {
    Map<String, Object> mergedAttribs = new HashMap<String, Object>();

    List<VariantContext> vcList = new LinkedList<VariantContext>();
    vcList.add(vc1);
    vcList.add(vc2);

    String[] MERGE_OR_ATTRIBS = {VCFConstants.DBSNP_KEY};
    for (String orAttrib : MERGE_OR_ATTRIBS) {
      boolean attribVal = false;
      for (VariantContext vc : vcList) {
        attribVal = vc.getAttributeAsBoolean(orAttrib, false);
        if (attribVal) // already true, so no reason to continue:
        break;
      }
      mergedAttribs.put(orAttrib, attribVal);
    }

    return mergedAttribs;
  }
Example #4
0
    private Allele ensureMergedAllele(
        Allele all1, Allele all2, boolean creatingReferenceForFirstTime) {
      AlleleOneAndTwo all12 = new AlleleOneAndTwo(all1, all2);
      Allele mergedAllele = mergedAlleles.get(all12);

      if (mergedAllele == null) {
        byte[] bases1 = all1.getBases();
        byte[] bases2 = all2.getBases();

        byte[] mergedBases = new byte[bases1.length + intermediateLength + bases2.length];
        System.arraycopy(bases1, 0, mergedBases, 0, bases1.length);
        if (intermediateBases != null)
          System.arraycopy(intermediateBases, 0, mergedBases, bases1.length, intermediateLength);
        System.arraycopy(bases2, 0, mergedBases, bases1.length + intermediateLength, bases2.length);

        mergedAllele = Allele.create(mergedBases, creatingReferenceForFirstTime);
        mergedAlleles.put(all12, mergedAllele);
      }

      return mergedAllele;
    }
Example #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;
  }
Example #6
0
  /**
   * Read in a list of ExactCall objects from reader, keeping only those with starts in startsToKeep
   * or all sites (if this is empty)
   *
   * @param reader a just-opened reader sitting at the start of the file
   * @param startsToKeep a list of start position of the calls to keep, or empty if all calls should
   *     be kept
   * @param parser a genome loc parser to create genome locs
   * @return a list of ExactCall objects in reader
   * @throws IOException
   */
  public static List<ExactCall> readExactLog(
      final BufferedReader reader, final List<Integer> startsToKeep, GenomeLocParser parser)
      throws IOException {
    if (reader == null) throw new IllegalArgumentException("reader cannot be null");
    if (startsToKeep == null) throw new IllegalArgumentException("startsToKeep cannot be null");
    if (parser == null) throw new IllegalArgumentException("GenomeLocParser cannot be null");

    List<ExactCall> calls = new LinkedList<ExactCall>();

    // skip the header line
    reader.readLine();

    // skip the first "type" line
    reader.readLine();

    while (true) {
      final VariantContextBuilder builder = new VariantContextBuilder();
      final List<Allele> alleles = new ArrayList<Allele>();
      final List<Genotype> genotypes = new ArrayList<Genotype>();
      final double[] posteriors = new double[2];
      final double[] priors = MathUtils.normalizeFromLog10(new double[] {0.5, 0.5}, true);
      final List<Integer> mle = new ArrayList<Integer>();
      final Map<Allele, Double> log10pNonRefByAllele = new HashMap<Allele, Double>();
      long runtimeNano = -1;

      GenomeLoc currentLoc = null;
      while (true) {
        final String line = reader.readLine();
        if (line == null) return calls;

        final String[] parts = line.split("\t");
        final GenomeLoc lineLoc = parser.parseGenomeLoc(parts[0]);
        final String variable = parts[1];
        final String key = parts[2];
        final String value = parts[3];

        if (currentLoc == null) currentLoc = lineLoc;

        if (variable.equals("type")) {
          if (startsToKeep.isEmpty() || startsToKeep.contains(currentLoc.getStart())) {
            builder.alleles(alleles);
            final int stop = currentLoc.getStart() + alleles.get(0).length() - 1;
            builder.chr(currentLoc.getContig()).start(currentLoc.getStart()).stop(stop);
            builder.genotypes(genotypes);
            final int[] mleInts = ArrayUtils.toPrimitive(mle.toArray(new Integer[] {}));
            final AFCalcResult result =
                new AFCalcResult(mleInts, 1, alleles, posteriors, priors, log10pNonRefByAllele);
            calls.add(new ExactCall(builder.make(), runtimeNano, result));
          }
          break;
        } else if (variable.equals("allele")) {
          final boolean isRef = key.equals("0");
          alleles.add(Allele.create(value, isRef));
        } else if (variable.equals("PL")) {
          final GenotypeBuilder gb = new GenotypeBuilder(key);
          gb.PL(GenotypeLikelihoods.fromPLField(value).getAsPLs());
          genotypes.add(gb.make());
        } else if (variable.equals("log10PosteriorOfAFEq0")) {
          posteriors[0] = Double.valueOf(value);
        } else if (variable.equals("log10PosteriorOfAFGt0")) {
          posteriors[1] = Double.valueOf(value);
        } else if (variable.equals("MLE")) {
          mle.add(Integer.valueOf(value));
        } else if (variable.equals("pNonRefByAllele")) {
          final Allele a = Allele.create(key);
          log10pNonRefByAllele.put(a, Double.valueOf(value));
        } else if (variable.equals("runtime.nano")) {
          runtimeNano = Long.valueOf(value);
        } else {
          // nothing to do
        }
      }
    }
  }
Example #7
0
  static VariantContext reallyMergeIntoMNP(
      VariantContext vc1, VariantContext vc2, ReferenceSequenceFile referenceFile) {
    int startInter = vc1.getEnd() + 1;
    int endInter = vc2.getStart() - 1;
    byte[] intermediateBases = null;
    if (startInter <= endInter) {
      intermediateBases =
          referenceFile.getSubsequenceAt(vc1.getChr(), startInter, endInter).getBases();
      StringUtil.toUpperCase(intermediateBases);
    }
    MergedAllelesData mergeData =
        new MergedAllelesData(
            intermediateBases, vc1, vc2); // ensures that the reference allele is added

    GenotypesContext mergedGenotypes = GenotypesContext.create();
    for (final Genotype gt1 : vc1.getGenotypes()) {
      Genotype gt2 = vc2.getGenotype(gt1.getSampleName());

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

      List<Allele> mergedAllelesForSample = new LinkedList<Allele>();

      /* NOTE: Since merged alleles are added to mergedAllelesForSample in the SAME order as in the input VC records,
        we preserve phase information (if any) relative to whatever precedes vc1:
      */
      Iterator<Allele> all2It = site2Alleles.iterator();
      for (Allele all1 : site1Alleles) {
        Allele all2 = all2It.next(); // this is OK, since allSamplesAreMergeable()

        Allele mergedAllele = mergeData.ensureMergedAllele(all1, all2);
        mergedAllelesForSample.add(mergedAllele);
      }

      double mergedGQ = Math.max(gt1.getLog10PError(), gt2.getLog10PError());
      Set<String> mergedGtFilters =
          new HashSet<
              String>(); // Since gt1 and gt2 were unfiltered, the Genotype remains unfiltered

      Map<String, Object> mergedGtAttribs = new HashMap<String, Object>();
      PhaseAndQuality phaseQual = calcPhaseForMergedGenotypes(gt1, gt2);
      if (phaseQual.PQ != null) mergedGtAttribs.put(ReadBackedPhasingWalker.PQ_KEY, phaseQual.PQ);

      Genotype mergedGt =
          new Genotype(
              gt1.getSampleName(),
              mergedAllelesForSample,
              mergedGQ,
              mergedGtFilters,
              mergedGtAttribs,
              phaseQual.isPhased);
      mergedGenotypes.add(mergedGt);
    }

    String mergedName = mergeVariantContextNames(vc1.getSource(), vc2.getSource());
    double mergedLog10PError = Math.min(vc1.getLog10PError(), vc2.getLog10PError());
    Set<String> mergedFilters =
        new HashSet<
            String>(); // Since vc1 and vc2 were unfiltered, the merged record remains unfiltered
    Map<String, Object> mergedAttribs = mergeVariantContextAttributes(vc1, vc2);

    // ids
    List<String> mergedIDs = new ArrayList<String>();
    if (vc1.hasID()) mergedIDs.add(vc1.getID());
    if (vc2.hasID()) mergedIDs.add(vc2.getID());
    String mergedID =
        mergedIDs.isEmpty()
            ? VCFConstants.EMPTY_ID_FIELD
            : Utils.join(VCFConstants.ID_FIELD_SEPARATOR, mergedIDs);

    VariantContextBuilder mergedBuilder =
        new VariantContextBuilder(
                mergedName,
                vc1.getChr(),
                vc1.getStart(),
                vc2.getEnd(),
                mergeData.getAllMergedAlleles())
            .id(mergedID)
            .genotypes(mergedGenotypes)
            .log10PError(mergedLog10PError)
            .filters(mergedFilters)
            .attributes(mergedAttribs);
    VariantContextUtils.calculateChromosomeCounts(mergedBuilder, true);
    return mergedBuilder.make();
  }
Example #8
0
 public Set<Allele> getAllMergedAlleles() {
   return new HashSet<Allele>(mergedAlleles.values());
 }
  /**
   * Compute full calls at a given locus. Entry point for engine calls from the UnifiedGenotyper.
   *
   * <p>If allSamples != null, then the output variantCallContext is guarenteed to contain a
   * genotype for every sample in allSamples. If it's null there's no such guarentee. Providing this
   * argument is critical when the resulting calls will be written to a VCF file.
   *
   * @param tracker the meta data tracker
   * @param refContext the reference base
   * @param rawContext contextual information around the locus
   * @param allSamples set of all sample names that we might call (i.e., those in the VCF header)
   * @return the VariantCallContext object
   */
  public List<VariantCallContext> calculateLikelihoodsAndGenotypes(
      final RefMetaDataTracker tracker,
      final ReferenceContext refContext,
      final AlignmentContext rawContext,
      final Set<String> allSamples) {
    final List<VariantCallContext> results = new ArrayList<VariantCallContext>(2);

    final List<GenotypeLikelihoodsCalculationModel.Model> models =
        getGLModelsToUse(tracker, refContext, rawContext);

    final Map<String, org.broadinstitute.sting.utils.genotyper.PerReadAlleleLikelihoodMap>
        perReadAlleleLikelihoodMap =
            new HashMap<
                String, org.broadinstitute.sting.utils.genotyper.PerReadAlleleLikelihoodMap>();

    if (models.isEmpty()) {
      results.add(
          UAC.OutputMode == OUTPUT_MODE.EMIT_ALL_SITES
                  && UAC.GenotypingMode
                      == GenotypeLikelihoodsCalculationModel.GENOTYPING_MODE.GENOTYPE_GIVEN_ALLELES
              ? generateEmptyContext(tracker, refContext, null, rawContext)
              : null);
    } else {
      for (final GenotypeLikelihoodsCalculationModel.Model model : models) {
        perReadAlleleLikelihoodMap.clear();
        final Map<String, AlignmentContext> stratifiedContexts =
            getFilteredAndStratifiedContexts(UAC, refContext, rawContext, model);
        if (stratifiedContexts == null) {
          results.add(
              UAC.OutputMode == OUTPUT_MODE.EMIT_ALL_SITES
                      && UAC.GenotypingMode
                          == GenotypeLikelihoodsCalculationModel.GENOTYPING_MODE
                              .GENOTYPE_GIVEN_ALLELES
                  ? generateEmptyContext(tracker, refContext, null, rawContext)
                  : null);
        } else {
          final VariantContext vc =
              calculateLikelihoods(
                  tracker,
                  refContext,
                  stratifiedContexts,
                  AlignmentContextUtils.ReadOrientation.COMPLETE,
                  null,
                  true,
                  model,
                  perReadAlleleLikelihoodMap);
          if (vc != null)
            results.add(
                calculateGenotypes(
                    tracker,
                    refContext,
                    rawContext,
                    stratifiedContexts,
                    vc,
                    model,
                    true,
                    perReadAlleleLikelihoodMap));
        }
      }
    }

    return results;
  }