Search in sources :

Example 46 with CigarElement

use of htsjdk.samtools.CigarElement in project gatk by broadinstitute.

the class CigarConversionUtilsUnitTest method testHandleInvalidCigarUnit.

@Test(dataProvider = "InvalidCigarUnitData")
public void testHandleInvalidCigarUnit(final CigarUnit invalidCigarUnit, final Class<? extends Exception> exceptionClass) {
    boolean sawException = false;
    try {
        final CigarElement convertedElement = CigarConversionUtils.convertCigarUnitToSAMCigarElement(invalidCigarUnit);
    } catch (Exception e) {
        sawException = true;
        Assert.assertEquals(e.getClass(), exceptionClass, "Wrong class of exception thrown for invalid CigarUnit");
    }
    Assert.assertTrue(sawException, "Expected exception of class " + exceptionClass + ", but no exception was thrown");
}
Also used : CigarElement(htsjdk.samtools.CigarElement) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) BaseTest(org.broadinstitute.hellbender.utils.test.BaseTest) Test(org.testng.annotations.Test)

Example 47 with CigarElement

use of htsjdk.samtools.CigarElement in project gatk-protected by broadinstitute.

the class ReferenceConfidenceModel method createReferenceHaplotype.

/**
     * Create a reference haplotype for an active region
     *
     * @param activeRegion the active region
     * @param refBases the ref bases
     * @param paddedReferenceLoc the location spanning of the refBases -- can be longer than activeRegion.getLocation()
     * @return a reference haplotype
     */
public static Haplotype createReferenceHaplotype(final AssemblyRegion activeRegion, final byte[] refBases, final SimpleInterval paddedReferenceLoc) {
    Utils.nonNull(activeRegion, "null region");
    Utils.nonNull(refBases, "null refBases");
    Utils.nonNull(paddedReferenceLoc, "null paddedReferenceLoc");
    final int alignmentStart = activeRegion.getExtendedSpan().getStart() - paddedReferenceLoc.getStart();
    if (alignmentStart < 0) {
        throw new IllegalStateException("Bad alignment start in createReferenceHaplotype " + alignmentStart);
    }
    final Haplotype refHaplotype = new Haplotype(refBases, true);
    refHaplotype.setAlignmentStartHapwrtRef(alignmentStart);
    final Cigar c = new Cigar();
    c.add(new CigarElement(refHaplotype.getBases().length, CigarOperator.M));
    refHaplotype.setCigar(c);
    return refHaplotype;
}
Also used : Cigar(htsjdk.samtools.Cigar) Haplotype(org.broadinstitute.hellbender.utils.haplotype.Haplotype) CigarElement(htsjdk.samtools.CigarElement)

Example 48 with CigarElement

use of htsjdk.samtools.CigarElement in project gatk-protected by broadinstitute.

the class ReadThreadingGraph method mergeDanglingTail.

/**
     * Actually merge the dangling tail if possible
     *
     * @param danglingTailMergeResult   the result from generating a Cigar for the dangling tail against the reference
     * @return 1 if merge was successful, 0 otherwise
     */
@VisibleForTesting
final int mergeDanglingTail(final DanglingChainMergeHelper danglingTailMergeResult) {
    final List<CigarElement> elements = danglingTailMergeResult.cigar.getCigarElements();
    final CigarElement lastElement = elements.get(elements.size() - 1);
    Utils.validateArg(lastElement.getOperator() == CigarOperator.M, "The last Cigar element must be an M");
    final int lastRefIndex = danglingTailMergeResult.cigar.getReferenceLength() - 1;
    final int matchingSuffix = Math.min(longestSuffixMatch(danglingTailMergeResult.referencePathString, danglingTailMergeResult.danglingPathString, lastRefIndex), lastElement.getLength());
    if (matchingSuffix == 0) {
        return 0;
    }
    final int altIndexToMerge = Math.max(danglingTailMergeResult.cigar.getReadLength() - matchingSuffix - 1, 0);
    // there is an important edge condition that we need to handle here: Smith-Waterman correctly calculates that there is a
    // deletion, that deletion is left-aligned such that the LCA node is part of that deletion, and the rest of the dangling
    // tail is a perfect match to the suffix of the reference path.  In this case we need to push the reference index to merge
    // down one position so that we don't incorrectly cut a base off of the deletion.
    final boolean firstElementIsDeletion = elements.get(0).getOperator() == CigarOperator.D;
    final boolean mustHandleLeadingDeletionCase = firstElementIsDeletion && (elements.get(0).getLength() + matchingSuffix == lastRefIndex + 1);
    final int refIndexToMerge = lastRefIndex - matchingSuffix + 1 + (mustHandleLeadingDeletionCase ? 1 : 0);
    // merge back to the LCA, which results in a cycle in the graph.  So we do not want to merge in such a case.
    if (refIndexToMerge == 0) {
        return 0;
    }
    // it's safe to merge now
    addEdge(danglingTailMergeResult.danglingPath.get(altIndexToMerge), danglingTailMergeResult.referencePath.get(refIndexToMerge), ((MyEdgeFactory) getEdgeFactory()).createEdge(false, 1));
    return 1;
}
Also used : CigarElement(htsjdk.samtools.CigarElement) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 49 with CigarElement

use of htsjdk.samtools.CigarElement in project polyGembler by c-zhou.

the class HetCorr method seqLength.

private static int seqLength(final SAMRecord record, final int[] ht_hc) {
    // TODO Auto-generated method stub
    CigarElement co;
    Arrays.fill(ht_hc, 0);
    co = record.getCigar().getFirstCigarElement();
    if (co.getOperator() == CigarOperator.H)
        ht_hc[0] = co.getLength();
    co = record.getCigar().getLastCigarElement();
    if (co.getOperator() == CigarOperator.H)
        ht_hc[1] = co.getLength();
    return record.getReadLength() + ht_hc[0] + ht_hc[1];
}
Also used : CigarElement(htsjdk.samtools.CigarElement)

Example 50 with CigarElement

use of htsjdk.samtools.CigarElement in project polyGembler by c-zhou.

the class SAMSegment method samRecord.

public static SAMSegment samRecord(SAMRecord sam_rc, boolean rev, int qry_ln) {
    if (sam_rc == null)
        return null;
    CigarElement c = sam_rc.getCigar().getFirstCigarElement();
    int hc = c.getOperator() == CigarOperator.HARD_CLIP ? c.getLength() : 0;
    String qseqid = sam_rc.getReadName();
    int qstart = sam_rc.getReadPositionAtReferencePosition(sam_rc.getAlignmentStart()) + hc;
    int qend = sam_rc.getReadPositionAtReferencePosition(sam_rc.getAlignmentEnd()) + hc;
    int sstart = sam_rc.getAlignmentStart();
    int send = sam_rc.getAlignmentEnd();
    String cigar = sam_rc.getCigarString();
    if (sam_rc.getReadNegativeStrandFlag()) {
        qseqid = qseqid + "'";
        int tmp = qstart;
        qstart = qry_ln - qend + 1;
        qend = qry_ln - tmp + 1;
        cigar = Constants.cgRev(cigar);
    }
    return new SAMSegment(qseqid, sam_rc.getReferenceName(), qstart, qend, sstart, send, cigar, sam_rc.getMappingQuality(), sam_rc.getIntegerAttribute("NM"), sam_rc.getIntegerAttribute("AS"));
}
Also used : CigarElement(htsjdk.samtools.CigarElement)

Aggregations

CigarElement (htsjdk.samtools.CigarElement)164 Cigar (htsjdk.samtools.Cigar)97 ArrayList (java.util.ArrayList)50 SAMRecord (htsjdk.samtools.SAMRecord)49 CigarOperator (htsjdk.samtools.CigarOperator)34 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)32 SamReader (htsjdk.samtools.SamReader)31 SAMFileHeader (htsjdk.samtools.SAMFileHeader)24 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)22 Test (org.testng.annotations.Test)19 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)17 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)17 File (java.io.File)16 SAMFileWriter (htsjdk.samtools.SAMFileWriter)15 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)14 Interval (htsjdk.samtools.util.Interval)14 IOException (java.io.IOException)14 VisibleForTesting (com.google.common.annotations.VisibleForTesting)13 List (java.util.List)13 GATKException (org.broadinstitute.hellbender.exceptions.GATKException)13