File: PFM-methods.r

package info (click to toggle)
r-bioc-tfbstools 1.20.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 920 kB
  • sloc: xml: 1,137; ansic: 590; asm: 54; sh: 13; makefile: 2
file content (136 lines) | stat: -rw-r--r-- 5,405 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

### -----------------------------------------------------------------
### PFMSimilarity method. compare two position frequency matrix.
### PFMSimilarity Exported!
compareMatrix = function(pfmSubject, pfmQuery, openPenalty, extPenalty){
  # The true aligning engine. Taking two ordinary matrixs.
  pfmSubject = normargPfm(pfmSubject)
  pfmQuery = normargPfm(pfmQuery)
  ans = .Call("matrixAligner", pfmQuery, pfmSubject, openPenalty, extPenalty)
  return(ans)
}

setMethod("PFMSimilarity", signature(pfmSubject="matrix", pfmQuery="matrix"),
         function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
           score = compareMatrix(pfmSubject, pfmQuery, openPenalty=openPenalty,
                                 extPenalty=extPenalty)
           relScore = 100 * score / min(ncol(pfmSubject), ncol(pfmQuery)) / 2
           return(c(score=score, relScore=relScore))
         }
         )

setMethod("PFMSimilarity", signature(pfmSubject="PFMatrix", 
                                     pfmQuery="PFMatrix"),
          function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
            ans = PFMSimilarity(Matrix(pfmSubject), Matrix(pfmQuery), 
                               openPenalty=openPenalty, extPenalty=extPenalty)
            return(ans)
          }
          )

setMethod("PFMSimilarity", signature(pfmSubject="PFMatrix", pfmQuery="matrix"),
          function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
            ans = PFMSimilarity(Matrix(pfmSubject), pfmQuery,
                               openPenalty=openPenalty, extPenalty=extPenalty)
            return(ans)
          }
          )

setMethod("PFMSimilarity", signature(pfmSubject="matrix", pfmQuery="PFMatrix"),
          function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
            ans = PFMSimilarity(pfmSubject, Matrix(pfmQuery),
                               openPenalty=openPenalty, extPenalty=extPenalty)
            return(ans)
          }
          )

setMethod("PFMSimilarity", signature(pfmSubject="PFMatrixList", 
                                     pfmQuery="matrix"),
          function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
            ans = lapply(pfmSubject, PFMSimilarity, pfmQuery,
                            openPenalty=openPenalty, extPenalty=extPenalty)
            return(ans)
          }
          )

setMethod("PFMSimilarity", signature(pfmSubject="PFMatrixList", 
                                     pfmQuery="PFMatrix"),
          function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
            ans = lapply(pfmSubject, PFMSimilarity, pfmQuery,
                         openPenalty=openPenalty, extPenalty=extPenalty)
            return(ans)
          }
          )

setMethod("PFMSimilarity", signature(pfmSubject="matrix", pfmQuery="character"),
          function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
            pfmQueryMatrix <- IUPAC2Matrix(pfmQuery)
            PFMSimilarity(pfmSubject, pfmQueryMatrix,
                         openPenalty=openPenalty, extPenalty=extPenalty)
          }
          )

setMethod("PFMSimilarity", signature(pfmSubject="PFMatrix", 
                                     pfmQuery="character"),
          function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
            PFMSimilarity(Matrix(pfmSubject), pfmQuery,
                         openPenalty=openPenalty, extPenalty=extPenalty)
          }
          )
                                    
setMethod("PFMSimilarity", signature(pfmSubject="PFMatrixList", 
                                     pfmQuery="character"),
          function(pfmSubject, pfmQuery, openPenalty=3, extPenalty=0.01){
            ans = lapply(pfmSubject, PFMSimilarity, pfmQuery,
                         openPenalty=openPenalty, extPenalty=extPenalty)
            return(ans)
          }
          )


### -----------------------------------------------------------------
### permute the PFM
### Exported!
setMethod("permuteMatrix", "matrix",
          function(x, type="intra"){
            if(type == "inter")
              stop("Only permutation within matrix is 
                   available for single matrix!")
            type = match.arg(type, c("intra", "inter"))
            x = normargPfm(x)
            index = sample(seq_len(ncol(x)), ncol(x), replace=FALSE)
            x = x[ , index]
            return(x)
          }
          )

setMethod("permuteMatrix", "PFMatrix",
          function(x, type="intra"){
            Matrix(x) = permuteMatrix(Matrix(x), type=type)
            return(x)
          }
          )

setMethod("permuteMatrix", "PFMatrixList",
          function(x, type="intra"){
            type = match.arg(type, c("intra", "inter"))
            if(type == "intra"){
              for(i in seq_len(length(x))){
                x[[i]] = permuteMatrix(x[[i]])
              }
            }else if(type =="inter"){
              allMatrix = do.call(cbind, Matrix(x))
              lengths = sapply(Matrix(x), ncol)
              lengths = c(0, cumsum(lengths))
              index = sample(seq_len(ncol(allMatrix)), 
                             ncol(allMatrix), replace=FALSE)
              for(i in seq_len(length(x))){
                Matrix(x[[i]]) = 
                allMatrix[ , index[(lengths[i]+1):lengths[i+1]]]
              }
            }
            return(x)
          }
          )