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; }