Skip to content

Instantly share code, notes, and snippets.

@dvanic
Created July 29, 2016 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvanic/69f4850ac0335781296849c662241ad6 to your computer and use it in GitHub Desktop.
Save dvanic/69f4850ac0335781296849c662241ad6 to your computer and use it in GitHub Desktop.
R gist to summarise CIGAR string
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