diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/indels/IntervalCleanerWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/indels/IntervalCleanerWalker.java index 65a0d0db2..029d47b09 100755 --- a/java/src/org/broadinstitute/sting/playground/gatk/walkers/indels/IntervalCleanerWalker.java +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/indels/IntervalCleanerWalker.java @@ -185,7 +185,7 @@ public class IntervalCleanerWalker extends LocusWindowWalker // decide which reads potentially need to be cleaned for ( SAMRecord read : reads ) { // first, move existing indels (for 1 indel reads only) to leftmost position within identical sequence - if ( read.getAlignmentBlocks().size() == 2 ) + if ( AlignmentUtils.getNumAlignmentBlocks(read) == 2 ) read.setCigar(indelRealignment(read.getCigar(), reference, read.getReadString(), read.getAlignmentStart()-(int)leftmostIndex)); AlignedRead aRead = new AlignedRead(read); diff --git a/java/src/org/broadinstitute/sting/playground/indels/AlignmentUtils.java b/java/src/org/broadinstitute/sting/playground/indels/AlignmentUtils.java index b98fc801b..3b55b511d 100644 --- a/java/src/org/broadinstitute/sting/playground/indels/AlignmentUtils.java +++ b/java/src/org/broadinstitute/sting/playground/indels/AlignmentUtils.java @@ -1,5 +1,6 @@ package org.broadinstitute.sting.playground.indels; +import net.sf.samtools.CigarOperator; import net.sf.samtools.SAMRecord; import net.sf.samtools.Cigar; import net.sf.samtools.CigarElement; @@ -139,6 +140,26 @@ public class AlignmentUtils { return mismatches; } + /** Returns number of alignment blocks (continuous stretches of aligned bases) in the specified alignment. + * This method follows closely the SAMRecord::getAlignmentBlocks() implemented in samtools library, but + * it only counts blocks without actually allocating and filling the list of blocks themselves. Hence, this method is + * a much more efficient alternative to r.getAlignmentBlocks.size() in the situations when this number is all that is needed. + * Formally, this method simply returns the number of M elements in the cigar. + * @param r alignment + * @return number of continuous alignment blocks (i.e. 'M' elements of the cigar; all indel and clipping elements are ignored). + */ + public static int getNumAlignmentBlocks(final SAMRecord r) { + int n = 0; + final Cigar cigar = r.getCigar(); + if (cigar == null) return 0; + + for (final CigarElement e : cigar.getCigarElements()) { + if (e.getOperator() == CigarOperator.M ) n++; + } + + return n; + } + /** Reads through the alignment cigar and returns all indels found in the alignment as a collection * of Indel objects. @@ -243,8 +264,8 @@ public class AlignmentUtils { case D : c = 'D'; break; case I : c = 'I'; break; } - b.append(c); b.append(cig.getCigarElement(i).getLength()); + b.append(c); } return b.toString(); }