Search in sources :

Example 66 with Haplotype

use of org.broadinstitute.hellbender.utils.haplotype.Haplotype in project gatk by broadinstitute.

the class AssemblyResultSet method trimTo.

/**
     * Trims an assembly result set down based on a new set of trimmed haplotypes.
     *
     * @param trimmedAssemblyRegion the trimmed down active region.
     *
     * @throws NullPointerException if any argument in {@code null} or
     *      if there are {@code null} entries in {@code originalByTrimmedHaplotypes} for trimmed haplotype keys.
     * @throws IllegalArgumentException if there is no reference haplotype amongst the trimmed ones.
     *
     * @return never {@code null}, a new trimmed assembly result set.
     */
public AssemblyResultSet trimTo(final AssemblyRegion trimmedAssemblyRegion) {
    final Map<Haplotype, Haplotype> originalByTrimmedHaplotypes = calculateOriginalByTrimmedHaplotypes(trimmedAssemblyRegion);
    if (refHaplotype == null) {
        throw new IllegalStateException("refHaplotype is null");
    }
    Utils.nonNull(trimmedAssemblyRegion);
    final AssemblyResultSet result = new AssemblyResultSet();
    for (final Haplotype trimmed : originalByTrimmedHaplotypes.keySet()) {
        final Haplotype original = originalByTrimmedHaplotypes.get(trimmed);
        if (original == null) {
            throw new IllegalStateException("all trimmed haplotypes must have an original one");
        }
        final AssemblyResult as = assemblyResultByHaplotype.get(original);
        if (as == null) {
            result.add(trimmed);
        } else {
            result.add(trimmed, as);
        }
    }
    result.setRegionForGenotyping(trimmedAssemblyRegion);
    result.setFullReferenceWithPadding(fullReferenceWithPadding);
    result.setPaddedReferenceLoc(paddedReferenceLoc);
    if (result.refHaplotype == null) {
        throw new IllegalStateException("missing reference haplotype in the trimmed set");
    }
    result.wasTrimmed = true;
    return result;
}
Also used : Haplotype(org.broadinstitute.hellbender.utils.haplotype.Haplotype)

Example 67 with Haplotype

use of org.broadinstitute.hellbender.utils.haplotype.Haplotype in project gatk by broadinstitute.

the class AssemblyResultSet method trimDownHaplotypes.

private Map<Haplotype, Haplotype> trimDownHaplotypes(final AssemblyRegion trimmedAssemblyRegion, final List<Haplotype> haplotypeList) {
    final Map<Haplotype, Haplotype> originalByTrimmedHaplotypes = new HashMap<>();
    for (final Haplotype h : haplotypeList) {
        final Haplotype trimmed = h.trim(trimmedAssemblyRegion.getExtendedSpan());
        if (trimmed != null) {
            if (originalByTrimmedHaplotypes.containsKey(trimmed)) {
                if (trimmed.isReference()) {
                    originalByTrimmedHaplotypes.remove(trimmed);
                    originalByTrimmedHaplotypes.put(trimmed, h);
                }
            } else {
                originalByTrimmedHaplotypes.put(trimmed, h);
            }
        } else if (h.isReference()) {
            throw new IllegalStateException("trimming eliminates the reference haplotype");
        } else if (debug) {
            logger.info("Throwing out haplotype " + h + " with cigar " + h.getCigar() + " because it starts with or ends with an insertion or deletion when trimmed to " + trimmedAssemblyRegion.getExtendedSpan());
        }
    }
    return originalByTrimmedHaplotypes;
}
Also used : Haplotype(org.broadinstitute.hellbender.utils.haplotype.Haplotype)

Example 68 with Haplotype

use of org.broadinstitute.hellbender.utils.haplotype.Haplotype in project gatk by broadinstitute.

the class AssemblyResultSet method calculateOriginalByTrimmedHaplotypes.

private Map<Haplotype, Haplotype> calculateOriginalByTrimmedHaplotypes(final AssemblyRegion trimmedAssemblyRegion) {
    if (debug) {
        logger.info("Trimming active region " + getRegionForGenotyping() + " with " + getHaplotypeCount() + " haplotypes");
    }
    final List<Haplotype> haplotypeList = getHaplotypeList();
    // trim down the haplotypes
    final Map<Haplotype, Haplotype> originalByTrimmedHaplotypes = trimDownHaplotypes(trimmedAssemblyRegion, haplotypeList);
    // create the final list of trimmed haplotypes
    final List<Haplotype> trimmedHaplotypes = new ArrayList<>(originalByTrimmedHaplotypes.keySet());
    // resort the trimmed haplotypes.
    Collections.sort(trimmedHaplotypes, Haplotype.SIZE_AND_BASE_ORDER);
    final Map<Haplotype, Haplotype> sortedOriginalByTrimmedHaplotypes = mapOriginalToTrimmed(originalByTrimmedHaplotypes, trimmedHaplotypes);
    if (debug) {
        logger.info("Trimmed region to " + trimmedAssemblyRegion.getSpan() + " size " + trimmedAssemblyRegion.getSpan().size() + " reduced number of haplotypes from " + haplotypeList.size() + " to only " + trimmedHaplotypes.size());
        for (final Haplotype remaining : trimmedHaplotypes) {
            logger.info("Remains: " + remaining + " cigar " + remaining.getCigar());
        }
    }
    return sortedOriginalByTrimmedHaplotypes;
}
Also used : Haplotype(org.broadinstitute.hellbender.utils.haplotype.Haplotype)

Example 69 with Haplotype

use of org.broadinstitute.hellbender.utils.haplotype.Haplotype in project gatk by broadinstitute.

the class RandomLikelihoodCalculationEngine method computeReadLikelihoods.

@Override
public ReadLikelihoods<Haplotype> computeReadLikelihoods(final AssemblyResultSet assemblyResultSet, final SampleList samples, final Map<String, List<GATKRead>> reads) {
    Utils.nonNull(assemblyResultSet, "assemblyResultSet is null");
    Utils.nonNull(samples, "samples is null");
    Utils.nonNull(reads, "perSampleReadList is null");
    final AlleleList<Haplotype> haplotypes = new IndexedAlleleList<>(assemblyResultSet.getHaplotypeList());
    final ReadLikelihoods<Haplotype> result = new ReadLikelihoods<>(samples, haplotypes, reads);
    final Random rnd = Utils.getRandomGenerator();
    final int sampleCount = samples.numberOfSamples();
    final int alleleCount = haplotypes.numberOfAlleles();
    for (int i = 0; i < sampleCount; i++) {
        final List<GATKRead> sampleReads = result.sampleReads(i);
        final int readCount = sampleReads.size();
        final LikelihoodMatrix<Haplotype> sampleLikelihoods = result.sampleMatrix(i);
        for (int a = 0; a < alleleCount; a++) {
            for (int r = 0; r < readCount; r++) {
                sampleLikelihoods.set(a, r, -Math.abs(rnd.nextDouble()));
            }
        }
    }
    return result;
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) Random(java.util.Random) Haplotype(org.broadinstitute.hellbender.utils.haplotype.Haplotype)

Example 70 with Haplotype

use of org.broadinstitute.hellbender.utils.haplotype.Haplotype in project gatk by broadinstitute.

the class GenotypingGivenAllelesUtils method composeGivenHaplotypes.

/**
     * Create the list of artificial GGA-mode haplotypes by injecting each of the provided alternate alleles into the reference haplotype
     *
     * @param refHaplotype the reference haplotype
     * @param givenHaplotypes the list of alternate alleles in VariantContexts
     * @param activeRegionWindow the window containing the reference haplotype
     *
     * @return a non-null list of haplotypes
     */
public static List<Haplotype> composeGivenHaplotypes(final Haplotype refHaplotype, final List<VariantContext> givenHaplotypes, final GenomeLoc activeRegionWindow) {
    Utils.nonNull(refHaplotype, "reference haplotype may not be null");
    Utils.nonNull(givenHaplotypes, "given haplotypes may not be null");
    Utils.nonNull(activeRegionWindow, "active region window may not be null");
    Utils.validateArg(activeRegionWindow.size() == refHaplotype.length(), "inconsistent reference haplotype and active region window");
    final Set<Haplotype> returnHaplotypes = new LinkedHashSet<>();
    final int activeRegionStart = refHaplotype.getAlignmentStartHapwrtRef();
    for (final VariantContext compVC : givenHaplotypes) {
        Utils.validateArg(GATKVariantContextUtils.overlapsRegion(compVC, activeRegionWindow), " some variant provided does not overlap with active region window");
        for (final Allele compAltAllele : compVC.getAlternateAlleles()) {
            final Haplotype insertedRefHaplotype = refHaplotype.insertAllele(compVC.getReference(), compAltAllele, activeRegionStart + compVC.getStart() - activeRegionWindow.getStart(), compVC.getStart());
            if (insertedRefHaplotype != null) {
                // can be null if the requested allele can't be inserted into the haplotype
                returnHaplotypes.add(insertedRefHaplotype);
            }
        }
    }
    return new ArrayList<>(returnHaplotypes);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Allele(htsjdk.variant.variantcontext.Allele) ArrayList(java.util.ArrayList) VariantContext(htsjdk.variant.variantcontext.VariantContext) Haplotype(org.broadinstitute.hellbender.utils.haplotype.Haplotype)

Aggregations

Haplotype (org.broadinstitute.hellbender.utils.haplotype.Haplotype)88 Test (org.testng.annotations.Test)31 GATKRead (org.broadinstitute.hellbender.utils.read.GATKRead)28 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)28 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)12 DataProvider (org.testng.annotations.DataProvider)10 VariantContext (htsjdk.variant.variantcontext.VariantContext)9 KBestHaplotype (org.broadinstitute.hellbender.tools.walkers.haplotypecaller.graphs.KBestHaplotype)8 ArrayList (java.util.ArrayList)7 Cigar (htsjdk.samtools.Cigar)6 AssemblyRegion (org.broadinstitute.hellbender.engine.AssemblyRegion)6 HomogeneousPloidyModel (org.broadinstitute.hellbender.tools.walkers.genotyper.HomogeneousPloidyModel)6 IndependentSampleGenotypesModel (org.broadinstitute.hellbender.tools.walkers.genotyper.IndependentSampleGenotypesModel)6 PloidyModel (org.broadinstitute.hellbender.tools.walkers.genotyper.PloidyModel)6 ReadThreadingGraph (org.broadinstitute.hellbender.tools.walkers.haplotypecaller.readthreading.ReadThreadingGraph)6 SampleList (org.broadinstitute.hellbender.utils.genotyper.SampleList)6 EventMap (org.broadinstitute.hellbender.utils.haplotype.EventMap)6 CigarElement (htsjdk.samtools.CigarElement)4 Allele (htsjdk.variant.variantcontext.Allele)4 File (java.io.File)4