public boolean sameGenotype(final Genotype other, boolean ignorePhase) { if (getPloidy() != other.getPloidy()) return false; // gotta have the same number of allele to be equal // By default, compare the elements in the lists of alleles, element-by-element Collection<Allele> thisAlleles = this.getAlleles(); Collection<Allele> otherAlleles = other.getAlleles(); if (ignorePhase) { // do not care about order, only identity of Alleles thisAlleles = new TreeSet<Allele>(thisAlleles); //implemented Allele.compareTo() otherAlleles = new TreeSet<Allele>(otherAlleles); } return thisAlleles.equals(otherAlleles); }
public String toString() { return String.format("[%s %s%s%s%s%s%s%s]", getSampleName(), getGenotypeString(false), toStringIfExists(VCFConstants.GENOTYPE_QUALITY_KEY, getGQ()), toStringIfExists(VCFConstants.DEPTH_KEY, getDP()), toStringIfExists(VCFConstants.GENOTYPE_ALLELE_DEPTHS, getAD()), toStringIfExists(VCFConstants.GENOTYPE_PL_KEY, getPL()), toStringIfExists(VCFConstants.GENOTYPE_FILTER_KEY, getFilters()), sortedString(getExtendedAttributes())); }
/** * Same as #getExtendedAttribute with a null default * * @param key * @return */ public Object getExtendedAttribute(final String key) { return getExtendedAttribute(key, null); }
public static void assertGenotypesAreEqual(final Genotype actual, final Genotype expected) { Assert.assertEquals(actual.getSampleName(), expected.getSampleName(), "Genotype names"); Assert.assertEquals(actual.getAlleles(), expected.getAlleles(), "Genotype alleles"); Assert.assertEquals(actual.getGenotypeString(), expected.getGenotypeString(), "Genotype string"); Assert.assertEquals(actual.getType(), expected.getType(), "Genotype type"); // filters are the same Assert.assertEquals(actual.getFilters(), expected.getFilters(), "Genotype fields"); Assert.assertEquals(actual.isFiltered(), expected.isFiltered(), "Genotype isFiltered"); // inline attributes Assert.assertEquals(actual.getDP(), expected.getDP(), "Genotype dp"); Assert.assertTrue(Arrays.equals(actual.getAD(), expected.getAD())); Assert.assertEquals(actual.getGQ(), expected.getGQ(), "Genotype gq"); Assert.assertEquals(actual.hasPL(), expected.hasPL(), "Genotype hasPL"); Assert.assertEquals(actual.hasAD(), expected.hasAD(), "Genotype hasAD"); Assert.assertEquals(actual.hasGQ(), expected.hasGQ(), "Genotype hasGQ"); Assert.assertEquals(actual.hasDP(), expected.hasDP(), "Genotype hasDP"); Assert.assertEquals(actual.hasLikelihoods(), expected.hasLikelihoods(), "Genotype haslikelihoods"); Assert.assertEquals(actual.getLikelihoodsString(), expected.getLikelihoodsString(), "Genotype getlikelihoodsString"); Assert.assertEquals(actual.getLikelihoods(), expected.getLikelihoods(), "Genotype getLikelihoods"); Assert.assertTrue(Arrays.equals(actual.getPL(), expected.getPL())); Assert.assertEquals(actual.getPhredScaledQual(), expected.getPhredScaledQual(), "Genotype phredScaledQual"); assertAttributesEquals(actual.getExtendedAttributes(), expected.getExtendedAttributes()); Assert.assertEquals(actual.isPhased(), expected.isPhased(), "Genotype isPhased"); Assert.assertEquals(actual.getPloidy(), expected.getPloidy(), "Genotype getPloidy"); }
/** * Copy all of the values for this builder from Genotype g * @param g * @return */ public GenotypeBuilder copy(final Genotype g) { name(g.getSampleName()); alleles(g.getAlleles()); phased(g.isPhased()); GQ(g.getGQ()); DP(g.getDP()); AD(g.getAD()); PL(g.getPL()); filter(g.getFilters()); attributes(g.getExtendedAttributes()); return this; }
@Override public String filter(final VariantContext ctx) { if (ctx.getHetCount() == 0) return null; final Map<List<Allele>, Counts> countsMap = new HashMap<List<Allele>, Counts>(); for (final Genotype gt : ctx.getGenotypesOrderedByName()) { if (gt.isNoCall() || !gt.isHet() || !gt.hasAD()) continue; final List<Allele> alleles = gt.getAlleles(); Counts counts = countsMap.get(alleles); if (counts == null) { counts = new Counts(); countsMap.put(alleles, counts); } counts.allele1 += gt.getAD()[ctx.getAlleleIndex(alleles.get(0))]; counts.allele2 += gt.getAD()[ctx.getAlleleIndex(alleles.get(1))]; } for (final Counts counts : countsMap.values()) { final int total = counts.allele1 + counts.allele2; if (total > 0 && Math.min(counts.allele1, counts.allele2) / (double) total < this.hetAlleleBalance) return AB_FILTER; } return null; } }
public boolean hasAnyAttribute(final String key) { if (key.equals(VCFConstants.GENOTYPE_KEY)) { return isAvailable(); } else if (key.equals(VCFConstants.GENOTYPE_QUALITY_KEY)) { return hasGQ(); } else if (key.equals(VCFConstants.GENOTYPE_ALLELE_DEPTHS)) { return hasAD(); } else if (key.equals(VCFConstants.GENOTYPE_PL_KEY)) { return hasPL(); } else if (key.equals(VCFConstants.DEPTH_KEY)) { return hasDP(); } else { return hasExtendedAttribute(key); } }
private void assertGoodVC(final VariantContext vc, final String contig, final int start, final int stop, final boolean nonRef) { Assert.assertEquals(vc.getChr(), contig); Assert.assertEquals(vc.getStart(), start); Assert.assertEquals(vc.getEnd(), stop); if ( nonRef ) { Assert.assertNotEquals(vc.getAlternateAllele(0), GATKVCFConstants.NON_REF_SYMBOLIC_ALLELE); } else { Assert.assertEquals(vc.getNAlleles(), 2); Assert.assertEquals(vc.getAlternateAllele(0), GATKVCFConstants.NON_REF_SYMBOLIC_ALLELE); Assert.assertEquals(vc.getAttributeAsInt(VCFConstants.END_KEY, -1), stop); Assert.assertTrue(vc.hasGenotypes()); Assert.assertTrue(vc.hasGenotype(SAMPLE_NAME)); Assert.assertEquals(vc.getGenotypes().size(), 1); final Genotype g = vc.getGenotype(SAMPLE_NAME); Assert.assertEquals(g.hasAD(), false); Assert.assertEquals(g.hasLikelihoods(), true); Assert.assertEquals(g.hasPL(), true); Assert.assertEquals(g.getPL().length == 3, true); Assert.assertEquals(g.hasDP(), true); Assert.assertEquals(g.hasGQ(), true); } }
private boolean checkGQIsGood(Genotype genotype) { if ( genotype.hasGQ() ) { return genotype.getGQ() >= minGenotypeQuality; } else if ( genotype.hasLikelihoods() ) { double log10gq = GenotypeLikelihoods.getGQLog10FromLikelihoods(genotype.getType().ordinal()-1,genotype.getLikelihoods().getAsVector()); return QualityUtils.phredScaleLog10ErrorRate(log10gq) >= minGenotypeQuality; } return minGenotypeQuality <= 0; }
private boolean genotypeCanBeMergedInCurrentBlock(final Genotype g) { return currentBlock != null && currentBlock.withinBounds(capToMaxGQ(g.getGQ())) && currentBlock.getPloidy() == g.getPloidy() && (currentBlock.getMinPLs() == null || !g.hasPL() || (currentBlock.getMinPLs().length == g.getPL().length)); }
@Override public Map<String, Object> finalizeRawData(VariantContext vc, VariantContext originalVC) { int totalAD = 0; for (final Genotype gt : vc.getGenotypes()){ if(gt != null) { if(gt.hasAD()) { totalAD += MathUtils.sum(gt.getAD()); continue; } // this is needed since the finalizing of HOM_REF genotypes comes after the finalizing of annotations. so the AD field is null at this point. // TODO: this will become unneeded if the above statement is false in which case it can be safely removed. if(gt.hasExtendedAttribute(GATKVCFConstants.MIN_DP_FORMAT_KEY)) { totalAD += Integer.parseInt((String) gt.getExtendedAttribute(GATKVCFConstants.MIN_DP_FORMAT_KEY)); } } } final int depth = vc.getAttributeAsInt(VCFConstants.DEPTH_KEY, 0); return Collections.singletonMap(GATKVCFConstants.FRACTION_INFORMATIVE_READS_KEY, (Object) (depth != 0 ? totalAD / (double) depth : 0)); }
private boolean haveSameGenotypes(final Genotype g1, final Genotype g2) { if ( g1 == null || g2 == null ) return false; if ((g1.isCalled() && g2.isFiltered()) || (g2.isCalled() && g1.isFiltered()) || (g1.isFiltered() && g2.isFiltered() && XLfiltered)) return false; List<Allele> a1s = g1.getAlleles(); List<Allele> a2s = g2.getAlleles(); return (a1s.containsAll(a2s) && a2s.containsAll(a1s)); } @Override
/** * comparable genotypes -> compareTo on the sample names * @param genotype * @return */ @Override public int compareTo(final Genotype genotype) { return getSampleName().compareTo(genotype.getSampleName()); }
public static void assertEquals(final GenotypesContext actual, final GenotypesContext expected) { if (expected == null) { Assert.assertNull(actual); return; } Assert.assertEquals(actual.getSampleNamesOrderedByName(), expected.getSampleNamesOrderedByName(), "Sample names differ"); for (final String name : expected.getSampleNamesOrderedByName()) { Assert.assertEquals(actual.get(name).getAlleles(), expected.get(name).getAlleles(), "Alleles differ for sample " + name); Assert.assertEquals(actual.get(name).getAD(), expected.get(name).getAD()); Assert.assertEquals(actual.get(name).getPL(), expected.get(name).getPL()); } }
@Override public void addGenotype(final BCF2Encoder encoder, final VariantContext vc, final Genotype g) throws IOException { final int samplePloidy = g.getPloidy(); for ( int i = 0; i < nValuesPerGenotype; i++ ) { if ( i < samplePloidy ) { // we encode the actual allele final Allele a = g.getAllele(i); final int offset = getAlleleOffset(a); final int encoded = ((offset+1) << 1) | ((g.isPhased() && i!=0) ? 0x01 : 0x00); encoder.encodeRawBytes(encoded, encodingType); } else { // we need to pad with missing as we have ploidy < max for this sample encoder.encodeRawBytes(encodingType.getMissingBytes(), encodingType); } } }
public static boolean isUnfilteredCalledDiploidGenotype(Genotype gt) { return (! gt.isFiltered() && gt.isCalled() && gt.getPloidy() == 2); }
private boolean sampleHasVariant(final Genotype g) { return (g !=null && !g.isHomRef() && (g.isCalled() || (g.isFiltered() && !XLfiltered))); }
public static String genotypeToString(Map<Allele, String> allelesMap, htsjdk.variant.variantcontext.Genotype genotype) { String genotypeValue; StringBuilder gt = new StringBuilder(); for (Allele allele : genotype.getAlleles()) { if (gt.length() > 0) { gt.append(genotype.isPhased() ? VCFConstants.PHASED : VCFConstants.UNPHASED); } gt.append(allelesMap.get(allele)); } genotypeValue = gt.toString(); return genotypeValue; }