/
genotype.go
393 lines (347 loc) · 9.87 KB
/
genotype.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
package contagiongo
import (
"fmt"
"sync"
"github.com/segmentio/ksuid"
)
// Genotype represents a unique pathogen sequence.
type Genotype interface {
GenotypeUID() ksuid.KSUID
// Sequence returns the sequence of the current node.
Sequence() []uint8
// SetSequence changes the sequence of genotype.
SetSequence(sequence []uint8)
// StringSequence returns the string representation of the
// integer-coded sequence of the current node.
StringSequence() string
// Fitness returns the fitness value of this node based on its current
// sequence and the given fitness model. If the fitness of the node has
// been computed before using the same fitness model, then the value is
// returned from memory and is not recomputed.
Fitness(f FitnessModel) float64
// NumSites returns the number of sites being modeled in this pathogen node.
NumSites() int
// StateCounts returns the number of sites by state.
StateCounts() map[uint8]int
// StatePositions returns the indexes of sites in a particular state.
StatePositions(state uint8) []int
}
type genotype struct {
sync.RWMutex
uid ksuid.KSUID
sequence []uint8
statePos map[uint8][]int // key is the state
fitness map[int]float64 // key is the fitness model id
}
// NewGenotype creates a new genotype from sequence.
func NewGenotype(s []uint8) Genotype {
g := new(genotype)
// Generate UID
g.uid = ksuid.New()
// Copy sequence
g.sequence = make([]uint8, len(s))
copy(g.sequence, s)
// Initial count of states
g.statePos = make(map[uint8][]int)
for i, state := range g.sequence {
g.statePos[state] = append(g.statePos[state], i)
}
// Initialize other maps
g.fitness = make(map[int]float64)
return g
}
func (n *genotype) GenotypeUID() ksuid.KSUID {
return n.uid
}
func (n *genotype) Sequence() []uint8 {
return n.sequence
}
func (n *genotype) SetSequence(sequence []uint8) {
n.sequence = nil
n.sequence = make([]uint8, len(sequence))
copy(n.sequence, sequence)
}
func (n *genotype) StringSequence() string {
key := fmt.Sprintf("%v", n.sequence)
key = key[1 : len(key)-1]
return key
}
func (n *genotype) Fitness(f FitnessModel) float64 {
id := f.ModelID()
n.RLock()
fitness, ok := n.fitness[id]
n.RUnlock()
if !ok {
fitness, _ := f.ComputeFitness(n.sequence...)
n.Lock()
n.fitness[id] = fitness
n.Unlock()
return fitness
}
return fitness
// fitness, _ := f.ComputeFitness(n.sequence...)
// return fitness
}
func (n *genotype) NumSites() int {
return len(n.sequence)
}
func (n *genotype) StateCounts() map[uint8]int {
stateCounts := make(map[uint8]int)
for state, positions := range n.statePos {
stateCounts[state] = len(positions)
}
return stateCounts
}
func (n *genotype) StatePositions(state uint8) []int {
return n.statePos[state]
}
// GenotypeSet is a collection of genotypes.
type GenotypeSet interface {
// Add adds the genotype to the set if the sequence does not exist yet.
Add(g Genotype)
// AddSequence creates a new genotype from the sequence if it is not present
// in the set. Otherwise, returns the existing genotype in the set.
AddSequence(s []uint8) Genotype
// Remove removes genotype of a particular sequence from the set.
Remove(s []uint8)
// Size returns the size of the set.
Size() int
Map() map[string]Genotype
}
type genotypeSet struct {
sync.RWMutex
set map[string]Genotype
}
// EmptyGenotypeSet creates a new empty set.
func EmptyGenotypeSet() GenotypeSet {
set := new(genotypeSet)
set.set = make(map[string]Genotype)
return set
}
func (set *genotypeSet) Add(g Genotype) {
key := fmt.Sprintf("%v", g.Sequence())
key = key[1 : len(key)-1]
set.Lock()
defer set.Unlock()
if _, exists := set.set[key]; !exists {
set.set[key] = g
}
}
func (set *genotypeSet) AddSequence(s []uint8) Genotype {
key := fmt.Sprintf("%v", s)
key = key[1 : len(key)-1]
set.Lock()
defer set.Unlock()
g, exists := set.set[key]
if !exists {
g := NewGenotype(s)
set.set[key] = g
return g
}
return g
}
func (set *genotypeSet) Remove(s []uint8) {
key := fmt.Sprintf("%v", s)
key = key[1 : len(key)-1]
set.Lock()
defer set.Unlock()
if _, exists := set.set[key]; exists {
set.set[key] = nil
delete(set.set, key)
}
}
func (set *genotypeSet) Size() int {
return len(set.set)
}
func (set *genotypeSet) Map() map[string]Genotype {
return set.set
}
// GenotypeNode represents a genotype together with its relationship to its parents and children.
type GenotypeNode interface {
// UID returns the unique ID of the node. Uses KSUID to generate
// random unique IDs with effectively no collision.
UID() ksuid.KSUID
GenotypeUID() ksuid.KSUID
// Parents returns the parent of the node.
Parents() []GenotypeNode
// Children returns the children of the node.
Children() []GenotypeNode
// AddChild appends a child to the list of children.
AddChild(child GenotypeNode)
// Sequence returns the sequence of the current node.
Sequence() []uint8
// SetSequence changes the sequence of genotype.
SetSequence(sequence []uint8)
// StringSequence returns the string representation of the
// integer-coded sequence of the current node.
StringSequence() string
// CurrentGenotype returns the current genotype of the current node.
CurrentGenotype() Genotype
// History returns the list of sequences that resulted into the extant
// sequence.
History(h [][]uint8) [][]uint8
// Fitness returns the fitness value of this node based on its current
// sequence and the given fitness model. If the fitness of the node has
// been computed before using the same fitness model, then the value is
// returned from memory and is not recomputed.
Fitness(f FitnessModel) float64
// NumSites returns the number of sites being modeled in this pathogen node.
NumSites() int
// StateCounts returns the number of sites by state.
StateCounts() map[uint8]int
// StatePositions returns the indexes of sites in a particular state.
StatePositions(state uint8) []int
}
type genotypeNode struct {
sync.RWMutex
Genotype
uid ksuid.KSUID
subs int
recombs int
parents []GenotypeNode
children []GenotypeNode
}
// NewGenotypeNode creates a new genotype node from a sequence.
// This should not be used to create a new genotype. Use the NewNode method in
// GenotypeTree instead.
func newGenotypeNode(sequence []uint8, set GenotypeSet, parents ...GenotypeNode) GenotypeNode {
genotype := set.AddSequence(sequence)
// Create new node
n := new(genotypeNode)
n.uid = ksuid.New()
// Assign its parent
if len(parents) > 0 {
n.parents = make([]GenotypeNode, len(parents))
copy(n.parents, parents)
} else {
n.parents = []GenotypeNode{}
}
// Initialize children
n.children = []GenotypeNode{}
// Assign genotype
n.Genotype = genotype
// Add new sequence as child of its parent
for _, parent := range parents {
parent.AddChild(n)
}
return n
}
func (n *genotypeNode) UID() ksuid.KSUID {
return n.uid
}
func (n *genotypeNode) Parents() []GenotypeNode {
return n.parents
}
func (n *genotypeNode) Children() []GenotypeNode {
n.RLock()
defer n.RUnlock()
return n.children
}
func (n *genotypeNode) AddChild(child GenotypeNode) {
n.Lock()
defer n.Unlock()
n.children = append(n.children, child)
}
func (n *genotypeNode) Sequence() []uint8 {
return n.Genotype.Sequence()
}
func (n *genotypeNode) CurrentGenotype() Genotype {
return n.Genotype
}
func (n *genotypeNode) History(h [][]uint8) [][]uint8 {
h = append(h, n.Genotype.Sequence())
if len(n.parents) == 0 {
return h
}
// TODO: Assumes no recombination. Only follows the first parent
return n.parents[0].History(h)
}
// GenotypeTree represents the genotypes as a series of differences
// from its ancestor.
type GenotypeTree interface {
// Set returns the GenotypeSet associated with this tree.
Set() GenotypeSet
// NewNode creates a new genotype node from a given sequence.
// Automatically adds sequence to the genotypeSet if it is not yet present.
NewNode(sequence []uint8, subs int, parents ...GenotypeNode) GenotypeNode
NewRecombinantNode(sequence []uint8, recombs int, parents ...GenotypeNode) GenotypeNode
// Nodes returns the map of genotype node ID found in the tree to its
// corresponding genotype.
NodeMap() map[ksuid.KSUID]GenotypeNode
}
type genotypeTree struct {
sync.RWMutex
nodes map[ksuid.KSUID]GenotypeNode
set GenotypeSet
}
// EmptyGenotypeTree creates a new empty genotype tree.
func EmptyGenotypeTree() GenotypeTree {
tree := new(genotypeTree)
tree.nodes = make(map[ksuid.KSUID]GenotypeNode)
tree.set = EmptyGenotypeSet()
return tree
}
func (t *genotypeTree) Set() GenotypeSet {
t.RLock()
defer t.RUnlock()
return t.set
}
func (t *genotypeTree) NewNode(sequence []uint8, subs int, parents ...GenotypeNode) GenotypeNode {
genotype := t.set.AddSequence(sequence)
// Create new node
n := new(genotypeNode)
n.uid = ksuid.New()
n.subs = subs
// Assign its parent
if len(parents) > 0 {
n.parents = make([]GenotypeNode, len(parents))
copy(n.parents, parents)
} else {
n.parents = []GenotypeNode{}
}
// Initialize children
n.children = []GenotypeNode{}
// Assign genotype
n.Genotype = genotype
// Add new sequence as child of its parent
for _, parent := range parents {
parent.AddChild(n)
}
// Add to tree map
t.Lock()
defer t.Unlock()
t.nodes[n.uid] = n
return n
}
func (t *genotypeTree) NewRecombinantNode(sequence []uint8, recombs int, parents ...GenotypeNode) GenotypeNode {
genotype := t.set.AddSequence(sequence)
// Create new node
n := new(genotypeNode)
n.uid = ksuid.New()
n.recombs = recombs
// Assign its parent
if len(parents) > 0 {
n.parents = make([]GenotypeNode, len(parents))
copy(n.parents, parents)
} else {
n.parents = []GenotypeNode{}
}
// Initialize children
n.children = []GenotypeNode{}
// Assign genotype
n.Genotype = genotype
// Add new sequence as child of its parent
for _, parent := range parents {
parent.AddChild(n)
}
// Add to tree map
t.Lock()
defer t.Unlock()
t.nodes[n.uid] = n
return n
}
func (t *genotypeTree) NodeMap() map[ksuid.KSUID]GenotypeNode {
t.RLock()
defer t.RUnlock()
return t.nodes
}