Search in sources :

Example 56 with CigarElement

use of htsjdk.samtools.CigarElement in project gridss by PapenfussLab.

the class CigarUtil method splitAfterReadPosition.

/**
 * Split a cigar immediately after the given read base
 * @param list cigar
 * @param readBaseOffset 0-based read offset
 * @return left and right Cigars for corresponding split read alignment
 */
public static Pair<Cigar, Cigar> splitAfterReadPosition(final List<CigarElement> list, final int readBaseOffset) {
    int length = readLength(list);
    if (readBaseOffset >= length)
        throw new IllegalArgumentException(String.format("Offset %d outside of bounds of read with length %d", readBaseOffset, length));
    List<CigarElement> current = new ArrayList<CigarElement>(list.size());
    List<CigarElement> left = current;
    int elementOffset = readBaseOffset;
    for (CigarElement ce : list) {
        if (ce.getOperator().consumesReadBases() && elementOffset >= 0 && elementOffset < ce.getLength()) {
            // split this element
            current.add(new CigarElement(elementOffset + 1, ce.getOperator()));
            clean(current, false);
            current = new ArrayList<CigarElement>(list.size() - current.size() + 2);
            current.add(new CigarElement(ce.getLength() - elementOffset - 1, ce.getOperator()));
        } else {
            // add to current element
            current.add(ce);
        }
        if (ce.getOperator().consumesReadBases()) {
            elementOffset -= ce.getLength();
        }
    }
    left.add(new CigarElement(length - readBaseOffset - 1, CigarOperator.SOFT_CLIP));
    current.add(0, new CigarElement(readBaseOffset + 1, CigarOperator.SOFT_CLIP));
    clean(left, false);
    clean(current, false);
    return Pair.of(new Cigar(left), new Cigar(current));
}
Also used : Cigar(htsjdk.samtools.Cigar) ArrayList(java.util.ArrayList) CigarElement(htsjdk.samtools.CigarElement)

Example 57 with CigarElement

use of htsjdk.samtools.CigarElement in project gridss by PapenfussLab.

the class CigarUtil method offsetOf.

/**
 * Returns the alignment offset of the given base relative to the starting alignment
 * @param cigar alignment CIGAR
 * @param readBaseOffset base offset
 * @return offset relative to first alignment
 */
public static int offsetOf(Cigar cigar, int readBaseOffset) {
    List<CigarElement> cl = cigar.getCigarElements();
    int basesLeft = readBaseOffset;
    int currentAlignmentOffset = 0;
    for (int i = 0; i < cl.size(); i++) {
        CigarElement ce = cl.get(i);
        if (ce.getOperator().consumesReadBases() && ce.getOperator().consumesReferenceBases()) {
            if (basesLeft < ce.getLength()) {
                return currentAlignmentOffset + basesLeft;
            }
        }
        if (ce.getOperator().consumesReferenceBases()) {
            currentAlignmentOffset += ce.getLength();
        }
        if (ce.getOperator().consumesReadBases()) {
            basesLeft = Math.max(0, basesLeft - ce.getLength());
        }
    }
    throw new IllegalArgumentException(String.format("Offset of %d base not defined for %s", readBaseOffset, cigar));
}
Also used : CigarElement(htsjdk.samtools.CigarElement)

Example 58 with CigarElement

use of htsjdk.samtools.CigarElement in project gridss by PapenfussLab.

the class CigarUtil method trimReadBases.

/**
 * Removes the given number of read bases from the given cigar
 * @param cigar CIGAR to trim
 * @param startCount number of starting reads to trim
 * @param endCount number of ending reads to trim
 * @return trimmed cigar
 */
public static Cigar trimReadBases(Cigar cigar, int startCount, int endCount) {
    List<CigarElement> cl = Lists.newArrayList(cigar.getCigarElements());
    for (int i = 0; i < cl.size(); i++) {
        CigarElement ce = cl.get(i);
        if (!ce.getOperator().consumesReadBases()) {
            cl.remove(i);
            i--;
        } else {
            if (startCount < ce.getLength()) {
                cl.set(i, new CigarElement(ce.getLength() - startCount, ce.getOperator()));
                break;
            } else {
                cl.remove(i);
                i--;
                startCount -= ce.getLength();
            }
        }
    }
    for (int i = cl.size() - 1; i >= 0; i--) {
        CigarElement ce = cl.get(i);
        if (!ce.getOperator().consumesReadBases()) {
            cl.remove(i);
        } else {
            if (endCount < ce.getLength()) {
                cl.set(i, new CigarElement(ce.getLength() - endCount, ce.getOperator()));
                break;
            } else {
                cl.remove(i);
                endCount -= ce.getLength();
            }
        }
    }
    clean(cl);
    return new Cigar(cl);
}
Also used : Cigar(htsjdk.samtools.Cigar) CigarElement(htsjdk.samtools.CigarElement)

Example 59 with CigarElement

use of htsjdk.samtools.CigarElement in project gridss by PapenfussLab.

the class CigarUtilTest method CigarOperatorIterator_test.

private void CigarOperatorIterator_test(List<CigarElement> l) {
    List<CigarOperator> co = new ArrayList<CigarOperator>();
    for (CigarElement e : l) {
        for (int i = 0; i < e.getLength(); i++) {
            co.add(e.getOperator());
        }
    }
    List<CigarOperator> result = Lists.newArrayList(new CigarUtil.CigarOperatorIterator(l));
    assertEquals(co, result);
}
Also used : ArrayList(java.util.ArrayList) CigarOperator(htsjdk.samtools.CigarOperator) CigarElement(htsjdk.samtools.CigarElement)

Example 60 with CigarElement

use of htsjdk.samtools.CigarElement in project gridss by PapenfussLab.

the class SplitReadIdentificationHelper method padCigar.

private static void padCigar(SAMRecord record, int pre, int post) {
    List<CigarElement> cigar = Lists.newArrayList(record.getCigar().getCigarElements());
    if (record.getReadNegativeStrandFlag()) {
        cigar.add(0, new CigarElement(post, CigarOperator.SOFT_CLIP));
        cigar.add(new CigarElement(pre, CigarOperator.SOFT_CLIP));
    } else {
        cigar.add(0, new CigarElement(pre, CigarOperator.SOFT_CLIP));
        cigar.add(new CigarElement(post, CigarOperator.SOFT_CLIP));
    }
    cigar = CigarUtil.clean(cigar, false);
    record.setCigar(new Cigar(cigar));
}
Also used : Cigar(htsjdk.samtools.Cigar) 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