/** * Set the cigar of this haplotype to cigar. * * Note that this function consolidates the cigar, so that 1M1M1I1M1M => 2M1I2M * * @param cigar a cigar whose readLength == length() */ public void setCigar( final Cigar cigar ) { this.cigar = AlignmentUtils.consolidateCigar(cigar); if ( this.cigar.getReadLength() != length() ) throw new IllegalArgumentException("Read length " + length() + " not equal to the read length of the cigar " + cigar.getReadLength() + " " + this.cigar); }
/** * Get the haplotype cigar extended by padSize M at the tail, consolidated into a clean cigar * * @param padSize how many additional Ms should be appended to the end of this cigar. Must be >= 0 * @return a newly allocated Cigar that consolidate(getCigar + padSize + M) */ public Cigar getConsolidatedPaddedCigar(final int padSize) { if ( padSize < 0 ) throw new IllegalArgumentException("padSize must be >= 0 but got " + padSize); final Cigar extendedHaplotypeCigar = new Cigar(getCigar().getCigarElements()); if ( padSize > 0 ) extendedHaplotypeCigar.add(new CigarElement(padSize, CigarOperator.M)); return AlignmentUtils.consolidateCigar(extendedHaplotypeCigar); }
@Test(enabled = !DEBUG, dataProvider = "ConsolidateCigarData") public void testConsolidateCigarWithData(final String testCigarString, final String expectedCigarString) { final Cigar testCigar = TextCigarCodec.decode(testCigarString); final Cigar expectedCigar = TextCigarCodec.decode(expectedCigarString); final Cigar actualCigar = AlignmentUtils.consolidateCigar(testCigar); Assert.assertEquals(actualCigar, expectedCigar); }
/** * Calculates the CIGAR for the alignment from the back track matrix * * @param matchingPrefix the prefix match size * @param matchingSuffix the suffix match size * @param sw the Smith-Waterman matrix to use * @param btrack the back track matrix to use * @return non-null SWPairwiseAlignmentResult object */ protected SWPairwiseAlignmentResult calculateCigar(final int matchingPrefix, final int matchingSuffix, final int[][] sw, final int[][] btrack) { final SWPairwiseAlignmentResult SW_result = calculateCigar(sw, btrack, OVERHANG_STRATEGY.INDEL); final LinkedList<CigarElement> lce = new LinkedList<CigarElement>(SW_result.cigar.getCigarElements()); if ( matchingPrefix > 0 ) lce.addFirst(makeElement(State.MATCH, matchingPrefix)); if ( matchingSuffix > 0 ) lce.addLast(makeElement(State.MATCH, matchingSuffix)); return new SWPairwiseAlignmentResult(AlignmentUtils.consolidateCigar(new Cigar(lce)), 0); } }
return AlignmentUtils.consolidateCigar(new Cigar(newElements));
return new SWPairwiseAlignmentResult(AlignmentUtils.consolidateCigar(new Cigar(lce)), alignment_offset);
@Test(dataProvider = "AddCigarElementsData", enabled = !DEBUG) public void testAddCigarElements(final String cigarString, final int pos, final int start, final int end, final String expectedCigarString) { final Cigar cigar = TextCigarCodec.decode(cigarString); final CigarElement elt = cigar.getCigarElement(0); final Cigar expectedCigar = TextCigarCodec.decode(expectedCigarString); final List<CigarElement> elts = new LinkedList<CigarElement>(); final int actualEndPos = AlignmentUtils.addCigarElements(elts, pos, start, end, elt); Assert.assertEquals(actualEndPos, pos + elt.getLength()); Assert.assertEquals(AlignmentUtils.consolidateCigar(new Cigar(elts)), expectedCigar); }
return AlignmentUtils.consolidateCigar(new Cigar(newElements));
rec.setCigar(AlignmentUtils.consolidateCigar(rec.getCigar()));
String theRead = refString + Utils.dupString(indelString1, refIndel1 + indelOp1 * indelSize1) + refString + Utils.dupString(indelString2, refIndel2 + indelOp2 * indelSize2) + refString; Cigar calculatedCigar = CigarUtils.leftAlignCigarSequentially(AlignmentUtils.consolidateCigar(givenCigar), theRef.getBytes(), theRead.getBytes(), preRefString.length(), 0); Assert.assertEquals(AlignmentUtils.consolidateCigar(calculatedCigar).toString(), AlignmentUtils.consolidateCigar(expectedCigar).toString(), "Cigar strings do not match!");
@Test(dataProvider = "SystematicRefAltSWTestData") public void testRefAltSW(final String prefix, final String end, final String refMid, final String altMid, final String midCigar) { // Construct the assembly graph SeqGraph graph = new SeqGraph(11); final int padSize = 0; SeqVertex top = new SeqVertex(Utils.dupString("N", padSize)); SeqVertex ref = new SeqVertex(prefix + refMid + end); SeqVertex alt = new SeqVertex(prefix + altMid + end); SeqVertex bot = new SeqVertex(Utils.dupString("N", padSize)); graph.addVertices(top, ref, alt, bot); graph.addEdges(new BaseEdge(true, 1), top, ref, bot); graph.addEdges(new BaseEdge(false, 1), top, alt, bot); // Construct the test path Path<SeqVertex,BaseEdge> path = Path.makePath(Arrays.asList(top, alt, bot), graph); Cigar expected = new Cigar(); expected.add(new CigarElement(padSize, CigarOperator.M)); if ( ! prefix.equals("") ) expected.add(new CigarElement(prefix.length(), CigarOperator.M)); for ( final CigarElement elt : TextCigarCodec.decode(midCigar).getCigarElements() ) expected.add(elt); if ( ! end.equals("") ) expected.add(new CigarElement(end.length(), CigarOperator.M)); expected.add(new CigarElement(padSize, CigarOperator.M)); expected = AlignmentUtils.consolidateCigar(expected); final String refString = top.getSequenceString() + ref.getSequenceString() + bot.getSequenceString(); final Cigar pathCigar = path.calculateCigar(refString.getBytes()); logger.warn("diffs: " + ref + " vs. " + alt + " cigar " + midCigar); logger.warn("Path " + path + " with cigar " + pathCigar); logger.warn("Expected cigar " + expected); Assert.assertEquals(pathCigar, expected, "Cigar mismatch: ref = " + refString + " vs alt = " + new String(path.getBases())); }
final Cigar swCigar = consolidateCigar(swPairwiseAlignment.getCigar());
final Cigar result = AlignmentUtils.consolidateCigar(cigarToReturn); if( result.getReferenceLength() != cigar.getReferenceLength() ) throw new IllegalStateException("leftAlignCigarSequentially failed to produce a valid CIGAR. Reference lengths differ. Initial cigar " + cigar + " left aligned into " + result);
/** * Write out a representation of this haplotype as a read * * @param haplotype a haplotype to write out. Cannot be null * @param paddedRefLoc the reference location. Cannot be null * @param isAmongBestHaplotypes true if among the best haplotypes, false if it was just one possible but not so good */ private void writeHaplotype(final Haplotype haplotype, final GenomeLoc paddedRefLoc, final boolean isAmongBestHaplotypes) { final GATKSAMRecord record = new GATKSAMRecord(output.getHeader()); record.setReadBases(haplotype.getBases()); record.setAlignmentStart(paddedRefLoc.getStart() + haplotype.getAlignmentStartHapwrtRef()); record.setBaseQualities(Utils.dupBytes((byte) '!', haplotype.getBases().length)); record.setCigar(AlignmentUtils.consolidateCigar(haplotype.getCigar())); record.setMappingQuality(isAmongBestHaplotypes ? 60 : 0); record.setReadName("HC" + uniqueNameCounter++); record.setAttribute(AlignmentUtils.HAPLOTYPE_TAG,haplotype.hashCode()); record.setReadUnmappedFlag(false); record.setReferenceIndex(paddedRefLoc.getContigIndex()); record.setAttribute(SAMTag.RG.toString(), READ_GROUP_ID); record.setFlags(16); output.add(record); }
Assert.assertEquals(path.calculateCigar(ref.getBytes()).toString(), AlignmentUtils.consolidateCigar(expectedCigar).toString(), "Cigar string mismatch");
AlignmentUtils.consolidateCigar(expectedCigar).toString(), "Cigar string mismatch: ref = " + ref + " alt " + new String(path.getBases()));