-
Notifications
You must be signed in to change notification settings - Fork 8
/
nucl_cov.go
175 lines (159 loc) · 4.46 KB
/
nucl_cov.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package mcorr
import (
"bytes"
"fmt"
)
// NuclCov contains covariance of nucleotide acid in a DNA sequence.
type NuclCov struct {
Doublets []int
Alphabet []byte
}
// NewNuclCov return a NuclCov given the alphabet.
func NewNuclCov(alphabet []byte) *NuclCov {
sizeOfAlphabet := len(alphabet)
nc := NuclCov{Alphabet: alphabet}
nc.Doublets = make([]int, sizeOfAlphabet*sizeOfAlphabet)
return &nc
}
// Add insert a pair of nucliotide acids.
// It returns error when the nucliotide acid is not in the alphabet.
func (nc *NuclCov) Add(a, b byte) error {
indexA := bytes.IndexByte(nc.Alphabet, a)
indexB := bytes.IndexByte(nc.Alphabet, b)
sizeOfAlphabet := len(nc.Alphabet)
if indexA >= 0 && indexB >= 0 {
nc.Doublets[indexA*sizeOfAlphabet+indexB]++
return nil
}
var err error
if indexA < 0 && indexB < 0 {
err = fmt.Errorf("%c and %c are not in Alphabet: %s", a, b, string(nc.Alphabet))
} else if indexA < 0 {
err = fmt.Errorf("%c is not in Alphabet: %s", a, string(nc.Alphabet))
} else {
err = fmt.Errorf("%c is not in Alphabet: %s", b, string(nc.Alphabet))
}
return err
}
// Count returns the total number of pairs.
func (nc *NuclCov) Count() int {
n := 0
for _, a := range nc.Doublets {
n += a
}
return n
}
// P00 returns the probability of 00.
func (nc *NuclCov) P00(minAlleleNum int) (xy float64, n int) {
for i := 0; i < len(nc.Doublets); i++ {
if nc.Doublets[i] > minAlleleNum {
for j := i + 1; j < len(nc.Doublets); j++ {
if nc.Doublets[j] > minAlleleNum {
n += nc.Doublets[i] * nc.Doublets[j]
}
}
n += nc.Doublets[i] * (nc.Doublets[i] - 1) / 2
xy += float64(nc.Doublets[i] * (nc.Doublets[i] - 1) / 2)
}
}
return
}
// P11 returns the probability of 11.
func (nc *NuclCov) P11(minAlleleNum int) (xy float64, n int) {
sizeOfAlphabet := len(nc.Alphabet)
for i := 0; i < len(nc.Doublets); i++ {
if nc.Doublets[i] > minAlleleNum {
for j := i + 1; j < len(nc.Doublets); j++ {
if nc.Doublets[j] > minAlleleNum {
c := float64(nc.Doublets[i] * nc.Doublets[j])
if i%sizeOfAlphabet != j%sizeOfAlphabet && i/sizeOfAlphabet != j/sizeOfAlphabet {
xy += c
}
n += nc.Doublets[i] * nc.Doublets[j]
}
}
n += nc.Doublets[i] * (nc.Doublets[i] - 1) / 2
}
}
return
}
//I think what we actually want is the cross-covariance matrix for this
//which is not symmetric, and we need all of the elements of this matrix
//which represent each possible combination except the diagonal which is just the same position
//https://en.wikipedia.org/wiki/Cross-covariance_matrix
func (nc *NuclCov) MateP11APS(nc2 *NuclCov, minAlleleNum int) (xy float64, n int) {
//sizeOfAlphabet := len(nc.Alphabet)
n1 := 0
n2 := 0
for i := 0; i < len(nc.Doublets); i++ {
if nc.Doublets[i] > minAlleleNum {
for j := 0; j < len(nc2.Doublets); j++ {
if nc2.Doublets[j] > minAlleleNum {
c := float64(nc.Doublets[i] * nc2.Doublets[j])
xy += c
}
n2 += nc2.Doublets[j]
}
}
n1 += nc.Doublets[i]
}
//for i := 0; i < len(nc.Doublets); i++ {
// n1 += nc.Doublets[i]
// n2 += nc2.Doublets[i]
//}
n = n1 * n2
return
}
// MateP11 calculate covariance between two clusters.
func (nc *NuclCov) MateP11(nc2 *NuclCov, minAlleleNum int) (xy float64, n int) {
sizeOfAlphabet := len(nc.Alphabet)
for i := 0; i < len(nc.Doublets); i++ {
if nc.Doublets[i] > minAlleleNum {
for j := 0; j < len(nc2.Doublets); j++ {
if i != j && nc2.Doublets[j] > minAlleleNum {
c := float64(nc.Doublets[i] * nc2.Doublets[j])
if i%sizeOfAlphabet != j%sizeOfAlphabet && i/sizeOfAlphabet != j/sizeOfAlphabet {
xy += c
}
}
}
}
}
n1 := 0
n2 := 0
for i := 0; i < len(nc.Doublets); i++ {
n1 += nc.Doublets[i]
n2 += nc2.Doublets[i]
}
n = n1 * n2
return
}
// MateP00 calculate covariance between two clusters.
func (nc *NuclCov) MateP00(nc2 *NuclCov, minAlleleNum int) (xy float64, n int) {
n1, n2 := 0, 0
for i := 0; i < len(nc.Doublets); i++ {
xy += float64(nc.Doublets[i] * nc2.Doublets[i])
n1 += nc.Doublets[i]
n2 += nc2.Doublets[i]
}
n = n1 * n2
return
}
// Append another NuclCov.
func (nc *NuclCov) Append(nc2 *NuclCov) error {
// Check alphabet
diffAlphabetError := fmt.Errorf("Different alphbet %s, %s", string(nc.Alphabet), string(nc2.Alphabet))
if len(nc.Alphabet) != len(nc2.Alphabet) {
return diffAlphabetError
}
for i, a := range nc.Alphabet {
b := nc2.Alphabet[i]
if a != b {
return diffAlphabetError
}
}
for i := 0; i < len(nc.Doublets); i++ {
nc.Doublets[i] += nc2.Doublets[i]
}
return nil
}