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