-
Notifications
You must be signed in to change notification settings - Fork 8
/
align.go
756 lines (681 loc) · 20.5 KB
/
align.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
package align
import (
"bytes"
"errors"
"fmt"
"math"
"math/rand"
"sort"
"strings"
"github.com/fredericlemoine/goalign/io"
)
const (
AMINOACIDS = 0 // Amino acid sequence alphabet
NUCLEOTIDS = 1 // Nucleotid sequence alphabet
GAP = '-'
OTHER = '*'
PSSM_NORM_NONE = 0 // No normalization
PSSM_NORM_FREQ = 1 // Normalization by freq in the site
PSSM_NORM_DATA = 2 // Normalization by aa/nt frequency in data
PSSM_NORM_UNIF = 3 // Normalization by uniform frequency
)
var stdaminoacid = []rune{'A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V'}
var stdnucleotides = []rune{'A', 'C', 'G', 'T'}
type Alignment interface {
AddSequence(name string, sequence string, comment string) error
AddSequenceChar(name string, sequence []rune, comment string) error
GetSequence(name string) (string, bool)
GetSequenceChar(ith int) ([]rune, bool)
GetSequenceName(ith int) (string, bool)
SetSequenceChar(ithAlign, ithSite int, char rune) error
Iterate(it func(name string, sequence string))
IterateChar(it func(name string, sequence []rune))
IterateAll(it func(name string, sequence []rune, comment string))
NbSequences() int
Length() int
ShuffleSequences()
ShuffleSites(rate float64, roguerate float64) []string
SimulateRogue(prop float64, proplen float64) ([]string, []string)
RemoveGaps(cutoff float64)
Sample(nb int) (Alignment, error)
BuildBootstrap() Alignment
Entropy(site int, removegaps bool) (float64, error) // Entropy of the given site
Swap(rate float64)
Recombine(rate float64, lenprop float64)
Rename(namemap map[string]string)
Pssm(log bool, pseudocount float64, normalization int) (pssm map[rune][]float64, err error) // Normalization: PSSM_NORM_NONE, PSSM_NORM_UNIF, PSSM_NORM_DATA
TrimNames(size int) (map[string]string, error)
TrimSequences(trimsize int, fromStart bool) error
AppendSeqIdentifier(identifier string, right bool)
AvgAllelesPerSite() float64
CharStats() map[rune]int64
Alphabet() int
AlphabetCharacters() []rune
Clone() (Alignment, error)
}
type align struct {
length int // Length of alignment
seqmap map[string]*seq // Map of sequences
seqs []*seq // Set of sequences (to preserve order)
alphabet int // AMINOACIDS OR NUCLEOTIDS
}
func NewAlign(alphabet int) *align {
switch alphabet {
case AMINOACIDS, NUCLEOTIDS:
// OK
default:
io.ExitWithMessage(errors.New("Unexpected sequence alphabet type"))
}
return &align{
-1,
make(map[string]*seq),
make([]*seq, 0, 100),
alphabet,
}
}
func (a *align) Iterate(it func(name string, sequence string)) {
for _, seq := range a.seqs {
it(seq.name, string(seq.sequence))
}
}
func (a *align) IterateChar(it func(name string, sequence []rune)) {
for _, seq := range a.seqs {
it(seq.name, seq.sequence)
}
}
func (a *align) IterateAll(it func(name string, sequence []rune, comment string)) {
for _, seq := range a.seqs {
it(seq.name, seq.sequence, seq.comment)
}
}
// Adds a sequence to this alignment
func (a *align) AddSequence(name string, sequence string, comment string) error {
err := a.AddSequenceChar(name, []rune(sequence), comment)
return err
}
func (a *align) AddSequenceChar(name string, sequence []rune, comment string) error {
_, ok := a.seqmap[name]
if ok {
return errors.New("Sequence " + name + " already exists in alignment")
}
if a.length != -1 && a.length != len(sequence) {
return errors.New("Sequence " + name + " does not have same length as other sequences")
}
a.length = len(sequence)
seq := NewSequence(name, sequence, comment)
a.seqmap[name] = seq
a.seqs = append(a.seqs, seq)
return nil
}
// If sequence exists in alignment, return true,sequence
// Otherwise, return false,nil
func (a *align) GetSequence(name string) (string, bool) {
seq, ok := a.seqmap[name]
return seq.Sequence(), ok
}
// If ith >=0 && i < nbSequences() return sequence,true
// Otherwise, return nil, false
func (a *align) GetSequenceChar(ith int) ([]rune, bool) {
if ith >= 0 && ith < a.NbSequences() {
return a.seqs[ith].SequenceChar(), true
}
return nil, false
}
// If ith >=0 && i < nbSequences() return name,true
// Otherwise, return "", false
func (a *align) GetSequenceName(ith int) (string, bool) {
if ith >= 0 && ith < a.NbSequences() {
return a.seqs[ith].Name(), true
}
return "", false
}
func (a *align) SetSequenceChar(ithAlign, ithSite int, char rune) error {
if ithAlign < 0 || ithAlign > a.NbSequences() {
return errors.New("Sequence index is outside alignment")
}
if ithSite < 0 || ithSite > a.Length() {
return errors.New("Site index is outside alignment")
}
a.seqs[ithAlign].sequence[ithSite] = char
return nil
}
// If sequence exists in alignment, return true,sequence
// Otherwise, return false,nil
func (a *align) NbSequences() int {
return len(a.seqs)
}
func (a *align) Length() int {
return a.length
}
// Just shuffle the order of the sequences in the alignment
// Does not change biological information
func (a *align) ShuffleSequences() {
var temp *seq
var n int = a.NbSequences()
for n > 1 {
r := rand.Intn(n)
n--
temp = a.seqs[n]
a.seqs[n] = a.seqs[r]
a.seqs[r] = temp
}
}
// Shuffles vertically rate sites of the alignment
// randomly
// rate must be >=0 and <=1
// Then, take roguerate proportion of the taxa, and will shuffle rate sites among the
// remaining intact sites
// Output: List of tax names that are more shuffled than others (length=roguerate*nbsequences)
func (a *align) ShuffleSites(rate float64, roguerate float64) []string {
if rate < 0 || rate > 1 {
io.ExitWithMessage(errors.New("Shuffle site rate must be >=0 and <=1"))
}
if roguerate < 0 || roguerate > 1 {
io.ExitWithMessage(errors.New("Shuffle rogue rate must be >=0 and <=1"))
}
nb_sites_to_shuffle := int(rate * float64(a.Length()))
nb_rogue_sites_to_shuffle := int(rate * (1.0 - rate) * (float64(a.Length())))
nb_rogue_seq_to_shuffle := int(roguerate * float64(a.NbSequences()))
sitepermutation := rand.Perm(a.Length())
taxpermutation := rand.Perm(a.NbSequences())
rogues := make([]string, nb_rogue_seq_to_shuffle)
if (nb_rogue_sites_to_shuffle + nb_sites_to_shuffle) > a.Length() {
io.ExitWithMessage(errors.New(fmt.Sprintf("Too many sites to shuffle (%d+%d>%d)",
nb_rogue_sites_to_shuffle, nb_sites_to_shuffle, a.Length())))
}
var temp rune
for i := 0; i < nb_sites_to_shuffle; i++ {
site := sitepermutation[i]
var n int = a.NbSequences()
for n > 1 {
r := rand.Intn(n)
n--
temp = a.seqs[n].sequence[site]
a.seqs[n].sequence[site] = a.seqs[r].sequence[site]
a.seqs[r].sequence[site] = temp
}
}
// We shuffle more sites for "rogue" taxa
for i := 0; i < nb_rogue_sites_to_shuffle; i++ {
site := sitepermutation[i+nb_sites_to_shuffle]
for r := 0; r < nb_rogue_seq_to_shuffle; r++ {
j := rand.Intn(r + 1)
seq1 := a.seqs[taxpermutation[r]]
seq2 := a.seqs[taxpermutation[j]]
seq1.sequence[site], seq2.sequence[site] = seq2.sequence[site], seq1.sequence[site]
rogues[r] = seq1.name
}
}
return rogues
}
// Removes positions constituted of [cutoff*100%,100%] Gaps
// Exception fo a cutoff of 0: does not remove positions with 0% gaps
// Cutoff must be between 0 and 1, otherwise set to 0.
// 0 means that positions with > 0 gaps will be removed
// other cutoffs : ]0,1] mean that positions with >= cutoff gaps will be removed
func (a *align) RemoveGaps(cutoff float64) {
var nbgaps int
if cutoff < 0 || cutoff > 1 {
cutoff = 0
}
toremove := make([]int, 0, 10)
for site := 0; site < a.Length(); site++ {
nbgaps = 0
for seq := 0; seq < a.NbSequences(); seq++ {
if a.seqs[seq].sequence[site] == GAP {
nbgaps++
}
}
if (cutoff > 0.0 && float64(nbgaps) >= cutoff*float64(a.NbSequences())) || (cutoff == 0 && nbgaps > 0) {
toremove = append(toremove, site)
}
}
/* Now we remove gap positions, starting at the end */
sort.Ints(toremove)
for i := (len(toremove) - 1); i >= 0; i-- {
for seq := 0; seq < a.NbSequences(); seq++ {
a.seqs[seq].sequence = append(a.seqs[seq].sequence[:toremove[i]], a.seqs[seq].sequence[toremove[i]+1:]...)
}
}
a.length -= len(toremove)
}
// This function renames sequences of the alignment based on the map in argument
// If a name in the map does not exist in the alignment, does nothing
// If a sequence in the alignment does not have a name in the map: does nothing
func (a *align) Rename(namemap map[string]string) {
for seq := 0; seq < a.NbSequences(); seq++ {
newname, ok := namemap[a.seqs[seq].name]
if ok {
a.seqs[seq].name = newname
}
// else {
// io.PrintMessage("Sequence " + a.seqs[seq].name + " not present in the map file")
// }
}
}
// Swaps a rate of the sequences together
// takes rate/2 seqs and swap a part of them with the other
// rate/2 seqs at a random position
// if rate < 0 : does nothing
// if rate > 1 : does nothing
func (a *align) Swap(rate float64) {
var nb_to_shuffle, nb_sites int
var pos int
var tmpchar rune
var seq1, seq2 *seq
if rate < 0 || rate > 1 {
return
}
nb_sites = a.Length()
nb_to_shuffle = (int)(rate * float64(a.NbSequences()))
permutation := rand.Perm(a.NbSequences())
for i := 0; i < int(nb_to_shuffle/2); i++ {
// We take a random position in the sequences and swap both
pos = rand.Intn(nb_sites)
seq1 = a.seqs[permutation[i]]
seq2 = a.seqs[permutation[i+(int)(nb_to_shuffle/2)]]
for pos < nb_sites {
tmpchar = seq1.sequence[pos]
seq1.sequence[pos] = seq2.sequence[pos]
seq2.sequence[pos] = tmpchar
pos++
}
}
}
// Recombines a rate of the sequences to another sequences
// takes rate/2 seqs and copy/paste a portion of them to the other
// rate/2 seqs at a random position
// if rate < 0 : does nothing
// if rate > 1 : does nothing
// prop must be <= 0.5 because it will recombine x% of seqs based on other x% of seqs
func (a *align) Recombine(prop float64, lenprop float64) {
var seq1, seq2 *seq
if prop < 0 || prop > 0.5 {
return
}
if lenprop < 0 || lenprop > 1 {
return
}
nb := int(prop * float64(a.NbSequences()))
lentorecomb := int(lenprop * float64(a.Length()))
permutation := rand.Perm(a.NbSequences())
// We take a random position in the sequences between min and max
for i := 0; i < nb; i++ {
pos := rand.Intn(a.Length() - lentorecomb + 1)
seq1 = a.seqs[permutation[i]]
seq2 = a.seqs[permutation[i+nb]]
for j := pos; j < pos+lentorecomb; j++ {
seq1.sequence[j] = seq2.sequence[j]
}
}
}
// Simulate rogue taxa in the alignment:
// take the proportion prop of sequences as rogue taxa => R
// For each t in R
// * We shuffle the alignment sites of t
// Output: List of rogue sequence names, and List of intact sequence names
func (a *align) SimulateRogue(prop float64, proplen float64) ([]string, []string) {
var seq *seq
if prop < 0 || prop > 1.0 {
return nil, nil
}
if proplen < 0 || proplen > 1.0 {
return nil, nil
}
if proplen == 0 {
prop = 0.0
}
nb := int(prop * float64(a.NbSequences()))
permutation := rand.Perm(a.NbSequences())
seqlist := make([]string, nb)
intactlist := make([]string, a.NbSequences()-nb)
len := int(proplen * float64(a.Length()))
// For each chosen rogue sequence
for r := 0; r < nb; r++ {
seq = a.seqs[permutation[r]]
seqlist[r] = seq.name
sitesToShuffle := rand.Perm(a.Length())[0:len]
// we Shuffle some sequence sites
for i, _ := range sitesToShuffle {
j := rand.Intn(i + 1)
seq.sequence[sitesToShuffle[i]], seq.sequence[sitesToShuffle[j]] = seq.sequence[sitesToShuffle[j]], seq.sequence[sitesToShuffle[i]]
}
}
for nr := nb; nr < a.NbSequences(); nr++ {
seq = a.seqs[permutation[nr]]
intactlist[nr-nb] = seq.name
}
return seqlist, intactlist
}
func (a *align) TrimNames(size int) (map[string]string, error) {
shortmap := make(map[string][]string)
finalshort := make(map[string]string)
if math.Pow10(size-2) < float64(a.NbSequences()) {
return nil, errors.New("New name size (" + fmt.Sprintf("%d", size-2) + ") does not allow to identify that amount of sequences (" + fmt.Sprintf("%d", a.NbSequences()) + ")")
}
for _, seq := range a.seqs {
n := seq.Name()
n = strings.Replace(n, ":", "", -1)
n = strings.Replace(n, "_", "", -1)
// Possible to have 100 Identical shortnames
if len(n) >= size-2 {
n = n[0 : size-2]
} else {
target := len(n)
for m := 0; m < (size - 2 - target); m++ {
n = n + "x"
}
}
if _, ok := shortmap[n]; !ok {
shortmap[n] = make([]string, 0, 100)
}
shortmap[n] = append(shortmap[n], seq.Name())
}
for short, list := range shortmap {
if len(list) > 100 {
return nil, errors.New("More than 100 identical short names (" + short + "), cannot shorten the names")
} else if len(list) > 1 {
i := 1
for _, longname := range list {
finalshort[longname] = short + fmt.Sprintf("%02d", i)
i++
}
} else {
for _, longname := range list {
finalshort[longname] = short + "00"
}
}
}
for long, short := range finalshort {
if seq, ok := a.seqmap[long]; !ok {
return nil, errors.New("The sequence with name " + long + " does not exist")
} else {
seq.name = short
delete(a.seqmap, long)
a.seqmap[short] = seq
}
}
return finalshort, nil
}
// Trims alignment sequences.
// If fromStart, then trims from the start, else trims from the end
// If trimsize >= sequence or trimsize < 0 lengths, then throw an error
func (a *align) TrimSequences(trimsize int, fromStart bool) error {
if trimsize < 0 {
return errors.New("Trim size must not be < 0")
}
if trimsize >= a.Length() {
return errors.New("Trim size must be < alignment length (" + fmt.Sprintf("%d", a.Length()) + ")")
}
for _, seq := range a.seqs {
if fromStart {
seq.sequence = seq.sequence[trimsize:len(seq.sequence)]
} else {
seq.sequence = seq.sequence[0 : len(seq.sequence)-trimsize]
}
}
a.length = a.length - trimsize
return nil
}
// Append a string to all sequence names of the alignment
// If right is true, then append it to the right of each names,
// otherwise, appends it to the left
func (a *align) AppendSeqIdentifier(identifier string, right bool) {
if len(identifier) != 0 {
for _, seq := range a.seqs {
if right {
seq.name = seq.name + identifier
} else {
seq.name = identifier + seq.name
}
}
}
}
// Samples randomly a subset of the sequences
// And returns this new alignment
// If nb < 1 or nb > nbsequences returns nil and an error
func (a *align) Sample(nb int) (Alignment, error) {
if a.NbSequences() < nb || nb < 1 {
return nil, errors.New("Number of sequences to sample is not compatible with alignment")
}
sample := NewAlign(a.alphabet)
permutation := rand.Perm(a.NbSequences())
for i := 0; i < nb; i++ {
seq := a.seqs[permutation[i]]
sample.AddSequenceChar(seq.name, seq.SequenceChar(), seq.Comment())
}
return sample, nil
}
func (a *align) BuildBootstrap() Alignment {
n := a.Length()
boot := NewAlign(a.alphabet)
indices := make([]int, n)
var buf bytes.Buffer
for i := 0; i < n; i++ {
indices[i] = rand.Intn(n)
}
for _, seq := range a.seqs {
buf.Reset()
for _, indice := range indices {
buf.WriteRune(seq.sequence[indice])
}
boot.AddSequenceChar(seq.name, bytes.Runes(buf.Bytes()), seq.Comment())
}
return boot
}
func DetectAlphabet(seq string) int {
isaa := true
isnt := true
for _, nt := range strings.ToUpper(seq) {
couldbent := false
couldbeaa := false
switch nt {
case 'A', 'C', 'B', 'R', 'G', '?', '-', 'D', 'K', 'S', 'H', 'M', 'N', 'V', 'X', 'T', 'W', 'Y':
couldbent = true
couldbeaa = true
case 'U', 'O':
couldbent = true
case 'Q', 'E', 'I', 'L', 'F', 'P', 'Z':
couldbeaa = true
}
isaa = isaa && couldbeaa
isnt = isnt && couldbent
if !(isaa || isnt) {
io.ExitWithMessage(errors.New("Unknown character state in alignment : " + string(nt)))
}
}
if isnt {
return NUCLEOTIDS
} else {
return AMINOACIDS
}
}
// Returns the distribution of all characters
func (a *align) CharStats() map[rune]int64 {
outmap := make(map[rune]int64)
for _, seq := range a.seqs {
for _, r := range seq.sequence {
outmap[r]++
}
}
return outmap
}
func (a *align) Alphabet() int {
return a.alphabet
}
func RandomAlignment(alphabet, length, nbseq int) (Alignment, error) {
al := NewAlign(alphabet)
for i := 0; i < nbseq; i++ {
name := fmt.Sprintf("Seq%04d", i)
if seq, err := RandomSequence(alphabet, length); err != nil {
return nil, err
} else {
al.AddSequenceChar(name, seq, "")
}
}
return al, nil
}
func (a *align) Clone() (Alignment, error) {
c := NewAlign(a.Alphabet())
var err error
a.IterateAll(func(name string, sequence []rune, comment string) {
newseq := make([]rune, 0, len(sequence))
newseq = append(newseq, sequence...)
err = c.AddSequenceChar(name, newseq, comment)
if err != nil {
return
}
})
return c, err
}
func (a *align) AvgAllelesPerSite() float64 {
nballeles := 0
nbsites := 0
for site := 0; site < a.Length(); site++ {
alleles := make(map[rune]bool)
onlygap := true
for seq := 0; seq < a.NbSequences(); seq++ {
s := a.seqs[seq].sequence[site]
if s != GAP {
alleles[s] = true
onlygap = false
}
}
if !onlygap {
nbsites++
}
nballeles += len(alleles)
}
return float64(nballeles) / float64(nbsites)
}
// Entropy of the given site. If the site number is < 0 or > length -> returns an error
// if removegaps is true, do not take into account gap characters
func (a *align) Entropy(site int, removegaps bool) (float64, error) {
if site < 0 || site > a.Length() {
return 1.0, errors.New("Site position is outside alignment")
}
// Number of occurences of each different aa/nt
occur := make(map[rune]int)
total := 0
entropy := 0.0
for seq := 0; seq < a.NbSequences(); seq++ {
s := a.seqs[seq].sequence[site]
if s != OTHER && (!removegaps || s != GAP) {
nb, ok := occur[s]
if !ok {
occur[s] = 1
} else {
occur[s] = nb + 1
}
total++
}
}
for _, v := range occur {
proba := float64(v) / float64(total)
entropy += proba * math.Log(proba)
}
if entropy != 0 {
entropy = -1.0 * entropy
}
if total == 0 {
return math.NaN(), nil
}
return entropy, nil
}
/* Computes a position-specific scoring matrix (PSSM)matrix
(see https://en.wikipedia.org/wiki/Position_weight_matrix)
This matrix may be in log2 scale or not (log argument)
A pseudo count may be added to values (to avoid log2(0))) with pseudocount argument
values may be normalized: normalization arg:
PSSM_NORM_NONE = 0 => No normalization
PSSM_NORM_FREQ = 1 => Normalization by frequency in the site
PSSM_NORM_DATA = 2 => Normalization by frequency in the site and divided by aa/nt frequency in data
PSSM_NORM_UNIF = 3 => Normalization by frequency in the site and divided by uniform frequency (1/4 or 1/20)
*/
func (a *align) Pssm(log bool, pseudocount float64, normalization int) (pssm map[rune][]float64, err error) {
// Number of occurences of each different aa/nt
pssm = make(map[rune][]float64)
var alphabet []rune
var normfactors map[rune]float64
alphabet = a.AlphabetCharacters()
for _, c := range alphabet {
if _, ok := pssm[c]; !ok {
pssm[c] = make([]float64, a.Length())
}
}
/* We compute normalization factors (takes into account pseudo counts) */
normfactors = make(map[rune]float64)
switch normalization {
case PSSM_NORM_NONE:
for _, c := range alphabet {
normfactors[c] = 1.0
}
case PSSM_NORM_UNIF:
for _, c := range alphabet {
normfactors[c] = 1.0 / (float64(a.NbSequences()) + pseudocount) / (1.0 / float64(len(alphabet)))
}
case PSSM_NORM_FREQ:
for _, c := range alphabet {
normfactors[c] = 1.0 / (float64(a.NbSequences()) + pseudocount)
}
case PSSM_NORM_DATA:
stats := a.CharStats()
total := 0.0
for _, c := range alphabet {
if s, ok := stats[c]; !ok {
err = errors.New(fmt.Sprintf("No charchacter %c in alignment statistics", c))
return
} else {
total += float64(s)
}
}
for _, c := range alphabet {
s, _ := stats[c]
normfactors[c] = 1.0 / (float64(a.NbSequences()) + pseudocount) / (float64(s) / total)
}
default:
err = errors.New("Unknown normalization option")
return
}
/* We count nt/aa occurences at each site */
for site := 0; site < a.Length(); site++ {
for seq := 0; seq < a.NbSequences(); seq++ {
s := a.seqs[seq].sequence[site]
if _, ok := normfactors[s]; ok {
if _, ok := pssm[s]; ok {
pssm[s][site] += 1.0
}
}
}
}
/* We add pseudo counts */
if pseudocount > 0 {
for _, v := range pssm {
for i, _ := range v {
v[i] += pseudocount
}
}
}
/* Applying normalization factors */
for k, v := range pssm {
for i, _ := range v {
v[i] = v[i] * normfactors[k]
}
}
/* Applying log2 transform */
if log {
for _, v := range pssm {
for i, _ := range v {
v[i] = math.Log(v[i]) / math.Log(2)
}
}
}
return
}
func (a *align) AlphabetCharacters() (alphabet []rune) {
if a.Alphabet() == AMINOACIDS {
return stdaminoacid
} else {
return stdnucleotides
}
}