/
host.go
176 lines (158 loc) · 4.82 KB
/
host.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
package contagiongo
import (
"sync"
)
// Host encapsulates pathogens together and ties its evolution to a particular
// set of parameters given by its assigned host type.
type Host interface {
// ID returns the unique ID of the host.
ID() int
// TypeID returns the ID representing the host's type in a multi-host
// simulation. Generally, the host type ID is used to identify hosts
// belonging to the same group that share the same properties.
TypeID() int
// PickPathogens returns a random list of pathogens from the
// current host.
// Returns nil if no pathogen exists.
PickPathogens(n int) []GenotypeNode
// Pathogens returns a list of all pathogens present in the host.
// This elements of the list are pointers to GenotypeNodes.
Pathogens() []GenotypeNode
// PathogenPopSize returns the number of pathogens inside the host.
PathogenPopSize() int
// AddPathogens appends a pathogen to the pathogen space of the host.
// Returns the new pathogen population size.
AddPathogens(p ...GenotypeNode) int
// RemoveAllPathogens removes all the pathogens from the host.
// Internally, this removes all the pointers that refer to GenotypeNodes.
RemoveAllPathogens()
// SetIntrahostModel associates the current host to a given intrahost model.
// The intrahost model governs intrahost processes by specifying the
// mutation, replication, recombination, and infection modes and parameters
// to be used.
SetIntrahostModel(intrahostModel IntrahostModel) error
// SetFitnessModel associates the current host to a given fitness model.
SetFitnessModel(fitnessModel FitnessModel) error
// SetTransmissionModel associates the current host to a given
// transmission model. This model sets the transmission probability
// (if not set in the adjacency matrix) and the transmission size.
SetTransmissionModel(transmissionModel TransmissionModel) error
// GetIntrahostModel retrieves the associated intrahost model
// for the current host.
GetIntrahostModel() IntrahostModel
// GetFitnessModel retrieves the associated fitness model
// for the current host.
GetFitnessModel() FitnessModel
// GetTransmissionModel retrieves the associated transmission model
// for the current host.
GetTransmissionModel() TransmissionModel
}
// sequenceHost is a type of host that represents pathogens as GenotypeNodes
type sequenceHost struct {
sync.RWMutex
IntrahostModel
FitnessModel
TransmissionModel
id int
typeID int
pathogens map[int]GenotypeNode
lastPathogenID int
}
// EmptySequenceHost creates a new host without an intrahost model and
// no pathogens.
func EmptySequenceHost(ids ...int) Host {
h := new(sequenceHost)
h.id = ids[0]
h.typeID = 0 // default
if len(ids) > 1 {
h.typeID = ids[1]
}
h.pathogens = make(map[int]GenotypeNode)
h.IntrahostModel = nil
h.FitnessModel = nil
return h
}
func (h *sequenceHost) ID() int {
return h.id
}
func (h *sequenceHost) TypeID() int {
return h.typeID
}
func (h *sequenceHost) PickPathogens(n int) []GenotypeNode {
if n < 1 {
return []GenotypeNode{}
}
h.RLock()
defer h.RUnlock()
pathogens := make([]GenotypeNode, n)
i := 0
for _, node := range h.pathogens {
pathogens[i] = node
i++
if i >= n {
break
}
}
return pathogens
}
func (h *sequenceHost) Pathogens() []GenotypeNode {
h.RLock()
defer h.RUnlock()
pathogens := make([]GenotypeNode, len(h.pathogens))
i := 0
for _, p := range h.pathogens {
pathogens[i] = p
i++
}
return pathogens
}
func (h *sequenceHost) PathogenPopSize() int {
return len(h.pathogens)
}
func (h *sequenceHost) AddPathogens(p ...GenotypeNode) int {
h.Lock()
defer h.Unlock()
for _, node := range p {
h.lastPathogenID++
h.pathogens[h.lastPathogenID] = node
}
return len(h.pathogens)
}
func (h *sequenceHost) RemoveAllPathogens() {
h.Lock()
defer h.Unlock()
for i := range h.pathogens {
h.pathogens[i] = nil
}
h.pathogens = make(map[int]GenotypeNode)
}
func (h *sequenceHost) SetIntrahostModel(model IntrahostModel) error {
if h.IntrahostModel != nil {
return SetIntrahostModelExistsError(h.IntrahostModel.ModelName(), h.IntrahostModel.ModelID())
}
h.IntrahostModel = model
return nil
}
func (h *sequenceHost) SetFitnessModel(model FitnessModel) error {
if h.FitnessModel != nil {
return SetFitnessModelExistsError(h.FitnessModel.ModelName(), h.FitnessModel.ModelID())
}
h.FitnessModel = model
return nil
}
func (h *sequenceHost) SetTransmissionModel(model TransmissionModel) error {
if h.TransmissionModel != nil {
return SetTransmissionModelExistsError(h.TransmissionModel.ModelName(), h.TransmissionModel.ModelID())
}
h.TransmissionModel = model
return nil
}
func (h *sequenceHost) GetIntrahostModel() IntrahostModel {
return h.IntrahostModel
}
func (h *sequenceHost) GetFitnessModel() FitnessModel {
return h.FitnessModel
}
func (h *sequenceHost) GetTransmissionModel() TransmissionModel {
return h.TransmissionModel
}