Search in sources :

Example 26 with CigarOperator

use of htsjdk.samtools.CigarOperator in project jvarkit by lindenb.

the class ReadClipper method clip.

public SAMRecord clip(final SAMRecord rec, final Interval fragment) {
    if (rec.getReadUnmappedFlag()) {
        return rec;
    }
    if (!fragment.getContig().equals(rec.getContig())) {
        return rec;
    }
    if (rec.getAlignmentEnd() < fragment.getStart()) {
        return rec;
    }
    if (rec.getAlignmentStart() > fragment.getEnd()) {
        return rec;
    }
    Cigar cigar = rec.getCigar();
    if (cigar == null) {
        LOG.warning("cigar missing in " + rec);
        return rec;
    }
    final List<BaseOp> bases = new ArrayList<>(cigar.getCigarElements().stream().mapToInt(C -> C.getLength()).sum());
    // expand cigar
    int refPos = rec.getUnclippedStart();
    for (final CigarElement ce : cigar.getCigarElements()) {
        final CigarOperator op = ce.getOperator();
        if (op.equals(CigarOperator.P))
            continue;
        for (int x = 0; x < ce.getLength(); ++x) {
            bases.add(new BaseOp(op, op.consumesReferenceBases() || op.isClipping() ? refPos : -1));
            if (op.consumesReferenceBases() || op.isClipping()) {
                refPos++;
            }
        }
    }
    /* 5' side */
    int newStart = rec.getAlignmentStart();
    int x = 0;
    while (x < bases.size()) {
        final BaseOp b = bases.get(x);
        if (b.refPos != -1 && b.isMatch() && b.refPos >= fragment.getStart()) {
            newStart = b.refPos;
            break;
        } else if (b.isDeletion()) {
            bases.remove(x);
            continue;
        } else if (!b.op.isClipping()) {
            b.op = CigarOperator.S;
            ++x;
        } else {
            ++x;
        }
    }
    /* 3' side */
    x = bases.size() - 1;
    while (x >= 0) {
        final BaseOp b = bases.get(x);
        if (b.refPos != -1 && b.isMatch() && b.refPos <= fragment.getEnd()) {
            break;
        } else if (b.isDeletion()) {
            bases.remove(x);
            --x;
            continue;
        } else if (!b.op.isClipping()) {
            b.op = CigarOperator.S;
            --x;
        } else {
            --x;
        }
    }
    // build new cigar
    boolean found_M = false;
    final List<CigarElement> newcigarlist = new ArrayList<>();
    x = 0;
    while (x < bases.size()) {
        final CigarOperator op = bases.get(x).op;
        if (op.equals(CigarOperator.M) || op.equals(CigarOperator.EQ)) {
            found_M = true;
        }
        int x2 = x;
        while (x2 < bases.size() && op.equals(bases.get(x2).op)) {
            ++x2;
        }
        newcigarlist.add(new CigarElement(x2 - x, op));
        x = x2;
    }
    if (!found_M) {
        SAMUtils.makeReadUnmappedWithOriginalTags(rec);
        if (this.programGroup != null) {
            rec.setAttribute("PG", programGroup);
        }
        return rec;
    }
    cigar = new Cigar(newcigarlist);
    rec.setCigar(cigar);
    rec.setAlignmentStart(newStart);
    if (this.programGroup != null) {
        rec.setAttribute("PG", programGroup);
    }
    return rec;
}
Also used : Cigar(htsjdk.samtools.Cigar) ArrayList(java.util.ArrayList) CigarOperator(htsjdk.samtools.CigarOperator) CigarElement(htsjdk.samtools.CigarElement)

Example 27 with CigarOperator

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

the class PileupElementUnitTest method testIsImmediatelyAfter_afterFirstD.

@Test
public void testIsImmediatelyAfter_afterFirstD() {
    final GATKRead read = ArtificialReadUtils.createArtificialRead("10M10D10M");
    final PileupElement pe = PileupElement.createPileupForReadAndOffset(read, 10);
    Assert.assertFalse(pe.atEndOfCurrentCigar(), "atEndOfCurrentCigar");
    Assert.assertTrue(pe.atStartOfCurrentCigar(), "atStartOfCurrentCigar");
    Assert.assertTrue(pe.isImmediatelyAfter(D));
    for (final CigarOperator op : CigarOperator.values()) {
        if (op != D) {
            Assert.assertFalse(pe.isImmediatelyAfter(op), "isImmediatelyAfter " + op);
        }
    }
    for (final CigarOperator op : CigarOperator.values()) {
        Assert.assertFalse(pe.isImmediatelyBefore(op));
    }
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) CigarOperator(htsjdk.samtools.CigarOperator) LocusIteratorByStateBaseTest(org.broadinstitute.hellbender.utils.locusiterator.LocusIteratorByStateBaseTest) Test(org.testng.annotations.Test) LIBSTest(org.broadinstitute.hellbender.utils.locusiterator.LIBSTest)

Example 28 with CigarOperator

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

the class PileupElementUnitTest method testImmediateBeforeAndAfterTest_simple.

@Test(dataProvider = "PrevAndNextTest_simple")
public void testImmediateBeforeAndAfterTest_simple(final GATKRead read, final CigarOperator middleOp) {
    final AlignmentStateMachine state = new AlignmentStateMachine(read);
    state.stepForwardOnGenome();
    //before the 'middleOp'
    final PileupElement pe1 = state.makePileupElement();
    Assert.assertEquals(pe1.getAdjacentOperator(PileupElement.Direction.PREV), null, "PREV");
    Assert.assertEquals(pe1.getAdjacentOperator(PileupElement.Direction.NEXT), middleOp, "NEXT");
    for (final CigarOperator op : CigarOperator.values()) {
        Assert.assertEquals(pe1.isImmediatelyBefore(op), middleOp == op, op.toString());
        Assert.assertFalse(pe1.isImmediatelyAfter(op), op.toString());
    }
    state.stepForwardOnGenome();
    //after the 'middleOp'
    final PileupElement pe2 = state.makePileupElement();
    Assert.assertEquals(pe2.getAdjacentOperator(PileupElement.Direction.PREV), middleOp, "PREV");
    Assert.assertEquals(pe2.getAdjacentOperator(PileupElement.Direction.NEXT), null, "NEXT");
    for (final CigarOperator op : CigarOperator.values()) {
        Assert.assertFalse(pe2.isImmediatelyBefore(op), op.toString());
        Assert.assertEquals(pe2.isImmediatelyAfter(op), op == middleOp, op.toString());
    }
}
Also used : CigarOperator(htsjdk.samtools.CigarOperator) AlignmentStateMachine(org.broadinstitute.hellbender.utils.locusiterator.AlignmentStateMachine) LocusIteratorByStateBaseTest(org.broadinstitute.hellbender.utils.locusiterator.LocusIteratorByStateBaseTest) Test(org.testng.annotations.Test) LIBSTest(org.broadinstitute.hellbender.utils.locusiterator.LIBSTest)

Example 29 with CigarOperator

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

the class PileupElementUnitTest method makePrevAndNextTest.

@DataProvider(name = "PrevAndNextTest")
public Object[][] makePrevAndNextTest() {
    final List<Object[]> tests = new LinkedList<>();
    final List<CigarOperator> operators = Arrays.asList(CigarOperator.I, CigarOperator.P, CigarOperator.S);
    for (final CigarOperator firstOp : Arrays.asList(M)) {
        for (final CigarOperator lastOp : Arrays.asList(M, D)) {
            for (final int nIntermediate : Arrays.asList(1, 2, 3)) {
                for (final List<CigarOperator> combination : Utils.makePermutations(operators, nIntermediate, false)) {
                    final int readLength = 2 + combination.size();
                    final GATKRead read = ArtificialReadUtils.createArtificialRead(header, "read", 0, 1, readLength);
                    read.setBases(Utils.dupBytes((byte) 'A', readLength));
                    read.setBaseQualities(Utils.dupBytes((byte) 30, readLength));
                    String cigar = "1" + firstOp;
                    for (final CigarOperator op : combination) {
                        cigar += "1" + op;
                    }
                    cigar += "1" + lastOp;
                    read.setCigar(cigar);
                    tests.add(new Object[] { read, firstOp, lastOp, combination });
                }
            }
        }
    }
    return tests.toArray(new Object[][] {});
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) CigarOperator(htsjdk.samtools.CigarOperator) DataProvider(org.testng.annotations.DataProvider)

Example 30 with CigarOperator

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

the class PileupElementUnitTest method testIsImmediatelyAfter_insideMs.

@Test
public void testIsImmediatelyAfter_insideMs() {
    final GATKRead read = ArtificialReadUtils.createArtificialRead("10M10D10M");
    final PileupElement pe = PileupElement.createPileupForReadAndOffset(read, 5);
    Assert.assertFalse(pe.atStartOfCurrentCigar(), "atStartOfCurrentCigar");
    Assert.assertFalse(pe.atEndOfCurrentCigar(), "atEndOfCurrentCigar");
    for (final CigarOperator op : CigarOperator.values()) {
        Assert.assertFalse(pe.isImmediatelyAfter(op), "isImmediatelyAfter " + op);
    }
    for (final CigarOperator op : CigarOperator.values()) {
        Assert.assertFalse(pe.isImmediatelyBefore(op), "isImmediatelyBefore " + op);
    }
}
Also used : GATKRead(org.broadinstitute.hellbender.utils.read.GATKRead) CigarOperator(htsjdk.samtools.CigarOperator) LocusIteratorByStateBaseTest(org.broadinstitute.hellbender.utils.locusiterator.LocusIteratorByStateBaseTest) Test(org.testng.annotations.Test) LIBSTest(org.broadinstitute.hellbender.utils.locusiterator.LIBSTest)

Aggregations

CigarOperator (htsjdk.samtools.CigarOperator)48 CigarElement (htsjdk.samtools.CigarElement)31 Cigar (htsjdk.samtools.Cigar)24 SAMRecord (htsjdk.samtools.SAMRecord)22 ArrayList (java.util.ArrayList)17 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)15 SamReader (htsjdk.samtools.SamReader)15 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)13 SAMFileHeader (htsjdk.samtools.SAMFileHeader)12 File (java.io.File)12 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)11 List (java.util.List)11 Parameter (com.beust.jcommander.Parameter)9 Launcher (com.github.lindenb.jvarkit.util.jcommander.Launcher)9 Logger (com.github.lindenb.jvarkit.util.log.Logger)9 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)9 CloserUtil (htsjdk.samtools.util.CloserUtil)9 Program (com.github.lindenb.jvarkit.util.jcommander.Program)8 IndexedFastaSequenceFile (htsjdk.samtools.reference.IndexedFastaSequenceFile)8 HashMap (java.util.HashMap)8