Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimization in CigarUtils to shortcut to M-only CIGAR when provably optimal #5466

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,19 @@ public static Cigar calculateCigar(final byte[] refSeq, final byte[] altSeq, fin

//Note: this is a performance optimization.
// If two strings are equal (a O(n) check) then it's trivial to get CIGAR for them.
if (Arrays.equals(refSeq, altSeq)){
final Cigar matching = new Cigar();
matching.add(new CigarElement(refSeq.length, CigarOperator.MATCH_OR_MISMATCH));
return matching;
// Furthermore, if their lengths are equal and their element-by-element comparison yields two or fewer mismatches
// it's also a trivial M-only CIGAR, because in order to have equal length one would need at least one insertion and
// one deletion, in which case two substitutions is a better alignment.
if (altSeq.length == refSeq.length){
int mismatchCount = 0;
for (int n = 0; n < refSeq.length && mismatchCount <= 2; n++) {
mismatchCount += (altSeq[n] == refSeq[n] ? 0 : 1);
}
if (mismatchCount <= 2) {
final Cigar matching = new Cigar();
matching.add(new CigarElement(refSeq.length, CigarOperator.MATCH_OR_MISMATCH));
return matching;
}
}

final Cigar nonStandard;
Expand Down