/** * changes the de novo flag from true to false on any sample incorrectly marked as de novo. (note * does not do the opposite) Also deals with the somatic cause. Some variants after splitting will * no longer have a somatic cause. * * @param variant the variant to check * @param checker the de novo checker for the current inheritance scenario * @return the corrected variant */ public static Variant denovoCorrect(DenovoChecker checker, Variant variant) { final Set<Integer> nonDenovoSamples = new HashSet<>(); for (int s = 0; s < variant.getNumberOfSamples(); s++) { final VariantSample sample = variant.getSample(s); if (sample != null) { final VariantSample.DeNovoStatus denovoCall = sample.isDeNovo(); if (denovoCall == VariantSample.DeNovoStatus.IS_DE_NOVO) { if (!checker.isDenovo(variant, s)) { nonDenovoSamples.add(s); } } } } final Variant ret; if (nonDenovoSamples.size() == 0) { ret = variant; } else { final VariantLocus newLocus = variant.getLocus(); final VariantSample[] newSamples = new VariantSample[variant.getNumberOfSamples()]; for (int k = 0; k < newSamples.length; k++) { final VariantSample sample = variant.getSample(k); if (sample != null) { final VariantSample.DeNovoStatus newStatus; if (sample.isDeNovo() == VariantSample.DeNovoStatus.UNSPECIFIED) { newStatus = VariantSample.DeNovoStatus.UNSPECIFIED; } else { newStatus = sample.isDeNovo() == VariantSample.DeNovoStatus.IS_DE_NOVO && !nonDenovoSamples.contains(k) ? VariantSample.DeNovoStatus.IS_DE_NOVO : VariantSample.DeNovoStatus.NOT_DE_NOVO; } newSamples[k] = new VariantSample( sample.getPloidy(), sample.getName(), sample.isIdentity(), sample.getMeasure(), newStatus, sample.getDeNovoPosterior()); VariantSample.copy(sample, newSamples[k]); } } final Variant newVariant = new Variant(newLocus, newSamples); Variant.copy(variant, newVariant); ret = newVariant; } return ret; }
private static Variant createSplitVariant( final Variant original, final int start, final int end, final int id) { final VariantLocus newLocus = createLocus(original, start, end); final VariantSample[] newSamples; if (original.getNumberOfSamples() > 0) { // trim description Description oldDescription = DescriptionNone.SINGLETON; for (int i = 0; i < original.getNumberOfSamples(); i++) { if (original.getSample(i) != null && !(original.getSample(i).getStats().counts().getDescription() instanceof DescriptionNone)) { oldDescription = original.getSample(i).getStats().counts().getDescription(); } } // Incrementally build up mapping of old alleles to new alleles final LinkedHashMap<String, Integer> alleles = new LinkedHashMap<>(); final int[] alleleMap = new int[oldDescription.size()]; for (int i = 0; i < oldDescription.size(); i++) { final String clipped = StringUtils.clip(oldDescription.name(i), start, end); Integer newPos = alleles.get(clipped); if (newPos == null) { newPos = alleles.size(); } alleles.put(clipped, newPos); alleleMap[i] = newPos; } final Description newDescription = new DescriptionCommon(alleles.keySet().toArray(new String[alleles.size()])); newSamples = createVariants(original, start, end, newDescription, alleleMap); } else { newSamples = new VariantSample[0]; } final Variant result = new Variant(newLocus, newSamples); Variant.copy(original, result); result.setPossibleCause(createVariantName(start, end, original.getPossibleCause())); result.setSplitId(id); return result; }
/** * Trim a variant by removing common prefix and suffix from each call and reference and adjusting * all the position information accordingly. * * @param original the original call * @return trimmed variant (possibly original if no change made) */ static Variant trim(final Variant original) { final String ref = original.getLocus().getRefNts(); if (ref.length() == 0) { // Cannot possibly trim if we are inserting, all bases are to be inserted return original; } // Compute set of called alleles and exit if there are none final HashSet<String> catSet = extractCalledAlleles(original); if (catSet.size() == 0) { return original; } // Include actual reference sequence (this will not be "") assert ref.length() > 0; catSet.add(ref); // Compute maximal clip positions based on set of alleles final String[] cats = catSet.toArray(new String[catSet.size()]); final int rightClip = StringUtils.longestSuffix(cats); final int leftClip = StringUtils.longestPrefix(rightClip, cats); // final int leftClip = StringUtils.longestPrefix(cats); // final int rightClip = StringUtils.longestSuffix(cats, leftClip); // Quick exit if no trimming is possible if (leftClip == 0 && rightClip == 0) { return original; } // Create new locus, will be "" for case of pure insertion final VariantLocus newLocus = createLocus(original, leftClip, rightClip); final VariantSample[] newSamples; if (original.getNumberOfSamples() > 0) { // trim description Description oldDescription = DescriptionNone.SINGLETON; for (int i = 0; i < original.getNumberOfSamples(); i++) { if (original.getSample(i) != null && !(original.getSample(i).getStats().counts().getDescription() instanceof DescriptionNone)) { oldDescription = original.getSample(i).getStats().counts().getDescription(); } } final String[] alleles = new String[oldDescription.size()]; final int[] alleleMap = new int[oldDescription.size()]; for (int i = 0; i < oldDescription.size(); i++) { final String clipped = StringUtils.clip(oldDescription.name(i), leftClip, rightClip); alleles[i] = clipped; alleleMap[i] = i; } final Description newDescription = new DescriptionCommon(alleles); // Create new trimmed samples newSamples = createVariants(original, leftClip, rightClip, newDescription, alleleMap); } else { newSamples = new VariantSample[0]; } // Create new variant from the new samples final Variant result = new Variant(newLocus, newSamples); Variant.copy(original, result); // This must be done after the copy result.setPossibleCause(createVariantName(leftClip, rightClip, original.getPossibleCause())); result.setTrimmed(); // System.err.println(original); // System.err.println(result); return result; }