-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
65 lines (57 loc) · 1.54 KB
/
stats.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
package pkg
import (
"math"
"github.com/shenwei356/bio/seq"
)
var fieldDispatcher = func() map[string]func(seq *seq.Seq) float64 {
fd := make(map[string]func(seq *seq.Seq) float64)
fd["GC"] = SeqGC
fd["GCSKEW"] = SeqGCSkew
fd["ATSKEW"] = SeqATSkew
fd["ENTRO"] = SeqEntropy
return fd
}()
// SeqGC computes the fraction of G or C bases in a sequence (GC content).
func SeqGC(seq *seq.Seq) float64 {
return seq.GC()
}
// SeqGCSkew computes the GC skew of a DNA sequence.
func SeqGCSkew(seq *seq.Seq) float64 {
c := seq.BaseContent("c")
g := seq.BaseContent("g")
if g+c == 0 {
return math.NaN()
}
return (g - c) / (g + c)
}
// SeqATSkew computes the AT skew of a DNA sequence.
func SeqATSkew(seq *seq.Seq) float64 {
a := seq.BaseContent("a")
t := seq.BaseContent("t")
if a+t == 0 {
return math.NaN()
}
return (a - t) / (a + t)
}
// SeqEntropy computes the Shannon entropy (information entropy)
// of a DNA sequence.
func SeqEntropy(seq *seq.Seq) float64 {
var prob, entro float64
for _, letter := range []string{"A", "C", "T", "G"} {
prob = seq.BaseContent(letter)
if prob > 0 {
entro += (prob * math.Log2(prob))
}
}
return -entro
}
// SeqKmerDiv will compute the Kmer profile of the input profile
// and compute its distance to a reference k-mer profile.
func SeqKmerDiv(seq *seq.Seq, ref KmerProfile) float64 {
// Get the k-mer length from field name, compute the
// sequence k-mer profile and its distance to the ref profile
prof := NewKmerProfile(ref.K)
prof.GetSeqKmers(seq)
prof.CountsToFreqs()
return prof.KmerDist(ref)
}