Created
July 29, 2016 07:26
-
-
Save dvanic/69f4850ac0335781296849c662241ad6 to your computer and use it in GitHub Desktop.
R gist to summarise CIGAR string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matcher <- function(pattern, x) { | |
ind = gregexpr(pattern, x)[[1]] | |
start = as.numeric(ind) | |
end = start + attr(ind, "match.length")- 2 | |
apply(cbind(start,end), 1, function(y) substr(x, start=y[1], stop=y[2])); | |
} | |
doone <- function(c, cigar) { | |
pat <- paste("\\d+", c , sep="") | |
sum(as.numeric(matcher(pat, cigar)), na.rm=T) | |
} | |
## takes a cigar string and parses it, not very fast but... | |
cigarsums <- function(cigar, chars=c("M","N","D","I","S","H", "P", "X", "=")) { | |
sapply (chars, doone, cigar) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment