Skip to content

Instantly share code, notes, and snippets.

@lianos
Created January 17, 2012 14:38
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 lianos/1626857 to your computer and use it in GitHub Desktop.
Save lianos/1626857 to your computer and use it in GitHub Desktop.
rho statistic with Biostrings
library(Biostrings)
setGeneric("rho", function(x, ...) standardGeneric("rho"))
setMethod("rho", c(x="XStringSet"),
function(x, ...) {
if (!xsbasetype(x) %in% c("DNA", "RNA")) {
stop("`x` must be of DNA or RNA base type")
}
di <- oligonucleotideFrequency(x, 2L, as.prob=TRUE)
nt <- oligonucleotideFrequency(x, 1L, as.prob=TRUE)
denom.1 <- substring(colnames(di), 1, 1)
denom.2 <- substring(colnames(di), 2, 2)
di / (nt[,denom.1,drop=FALSE] * nt[,denom.2,drop=FALSE])
})
setMethod("rho", c(x="XString"),
function(x, ...) {
if (!xsbasetype(x) %in% c("DNA", "RNA")) {
stop("`x` must be of DNA or RNA base type")
}
rho(as(x, 'XStringSet'))
})
setMethod("rho", c(x="character"),
function(x, base.type=c("DNA", "RNA"), ...) {
base.type <- match.arg(base.type)
base.type <- switch(base.type, DNA="DNAStringSet", RNA="RNAStringSet")
rho(as(x, base.type))
})
################################################################################
## Tests
if (FALSE) {
tests.str <- c("GATACA", "GGCCGGCC",
"ATGTATATGAGAAAGGAAGAGCCTAGCGGCTCAGACAAGATTATGACTTCAGTTGTTGTT")
rho(tests.str)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment