PageRenderTime 35ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/fingerprint/R/matrix.R

http://github.com/rajarshi/cdkr
R | 79 lines | 61 code | 10 blank | 8 comment | 11 complexity | 174fbff0c98e0b53538521daaecf91a0 MD5 | raw file
  1. fp.sim.matrix <- function(fplist, fplist2=NULL, method='tanimoto') {
  2. sim <- NA
  3. if (!is.null(fplist2)) {
  4. sim <- do.call('rbind', lapply(fplist, function(fp) unlist(lapply(fplist2, function(x) distance(x,fp)))))
  5. }
  6. if (method == 'dice') {
  7. sim <- .dice.sim.mat(fplist)
  8. } else if (method == 'tanimoto') {
  9. sim <- .tanimoto.sim.mat(fplist)
  10. } else {
  11. sim <- matrix(0,nrow=length(fplist), ncol=length(fplist))
  12. for (i in 1:(length(fplist)-1)) {
  13. v <- unlist(lapply( fplist[(i+1):length(fplist)], distance, fp2=fplist[[i]], method=method))
  14. sim[i,(i+1):length(fplist)] <- v
  15. sim[(i+1):length(fplist),i] <- v
  16. }
  17. }
  18. diag(sim) <- 1.0
  19. return(sim)
  20. }
  21. ## Takes the fingerprints, P bits, for a set of N molecules supplied as
  22. ## a list structure and creates an N x P matrix
  23. fp.to.matrix <- function( fplist ) {
  24. size <- fplist[[1]]@nbit
  25. m <- matrix(0, nrow=length(fplist), ncol=size)
  26. cnt <- 1
  27. for ( i in fplist ) {
  28. m[cnt,i@bits] <- 1
  29. cnt <- cnt + 1
  30. }
  31. m
  32. }
  33. fp.factor.matrix <- function( fplist ) {
  34. size <- fplist[[1]]@nbit
  35. m <- data.frame(fp.to.matrix(fplist))
  36. m[] <- lapply(m, factor, levels=0:1)
  37. m
  38. }
  39. .dice.sim.mat <- function(fplist) {
  40. m <- fp.to.matrix(fplist)
  41. mat<-m%*%t(m)
  42. len<-length(m[,1])
  43. s<-mat.or.vec(len,len)
  44. rs<-rowSums(m) #since its is binary just add the row values.
  45. for (i in 1:(len-1)) {
  46. for (j in (i+1):len) {
  47. s[i,j]=(2*(mat[i,j])/(rs[i]+rs[j]))
  48. s[j,i]=s[i,j]
  49. }
  50. }
  51. diag(s) <- 1.0
  52. return(s)
  53. }
  54. .tanimoto.sim.mat <- function(fplist){
  55. m <- fp.to.matrix(fplist)
  56. mat<-m%*%t(m)
  57. len<-length(m[,1])
  58. s<-mat.or.vec(len,len)
  59. ret <- .C("m_tanimoto", as.double(mat), as.integer(len), as.double(s),
  60. PACKAGE="fingerprint")
  61. ret <- matrix(ret[[3]], nrow=len, ncol=len, byrow=TRUE)
  62. return(ret)
  63. ## for (i in 1:len){
  64. ## for (j in 1:len){
  65. ## s[i,j]<- mat[i,j]/(mat[i,i]+mat[j,j]-mat[i,j]) # Formula for Tanimoto Calculation
  66. ## }
  67. ## }
  68. ## return(s)
  69. }