-
Notifications
You must be signed in to change notification settings - Fork 4
/
TransmissionTimeSample_HIVARTGranichGEMF.py
executable file
·232 lines (219 loc) · 14.6 KB
/
TransmissionTimeSample_HIVARTGranichGEMF.py
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
#! /usr/bin/env python3
'''
Niema Moshiri 2016
"TransmissionTimeSample" module, where the transmission network is simulated by
GEMF (Sahneh et al. 2016) under the HIV-ART model developed by Granich et al
(2008), but where individuals can be "seed infected" (i.e., infected from
outside the contact network) after time 0.
The states of the model are as follows:
- NS = Non-Susceptible
- S = Susceptible
- I1 = Infected Stage 1
- I2 = Infected Stage 2
- I3 = Infected Stage 3
- I4 = Infected Stage 4
- A1 = ART Stage 1
- A2 = ART Stage 2
- A3 = ART Stage 3
- A4 = ART Stage 4
- D = Deceased
Below is an adjacency list representing the model:
- NS -> [S, D]
- S -> [I1, D]
- I1 -> [I2, A1, D]
- I2 -> [I3, A2, D]
- I3 -> [I4, A3, D]
- I4 -> [A4, D]
- A1 -> [I1, A2, D]
- A2 -> [I2, A3, D]
- A3 -> [I3, A4, D]
- A4 -> [I4, D]
- D -> []
'''
from TransmissionTimeSample import TransmissionTimeSample
from TransmissionTimeSample_TransmissionFile import TransmissionTimeSample_TransmissionFile
import modules.FAVITES_ModuleFactory as MF
import FAVITES_GlobalContext as GC
from datetime import datetime
from os.path import expanduser
from os import chdir,getcwd,makedirs
from random import shuffle
from subprocess import call
from sys import stderr
class TransmissionTimeSample_HIVARTGranichGEMF(TransmissionTimeSample):
def cite():
return GC.CITATION_GEMF
def init():
assert "TransmissionNodeSample_GEMF" in str(MF.modules['TransmissionNodeSample']), "Must use TransmissionNodeSample_GEMF module"
assert "EndCriteria_GEMF" in str(MF.modules['EndCriteria']), "Must use EndCriteria_GEMF module"
for p in dir(GC):
if p.startswith('hiv_') and '_to_' in p:
setattr(GC, p, float(getattr(GC,p)))
assert getattr(GC,p) >= 0, "%s must be at least 0" % p
GC.end_time = float(GC.end_time)
assert GC.end_time > 0, "end_time must be positive"
GC.gemf_ready = False
GC.gemf_state_to_num = {'NS':0, 'S':1, 'I1':2, 'I2':3, 'I3':4, 'I4':5, 'A1':6, 'A2':7, 'A3':8, 'A4':9, 'D':10}
GC.gemf_num_to_state = {GC.gemf_state_to_num[state]:state for state in GC.gemf_state_to_num}
freq_sum = 0
for s in GC.gemf_state_to_num.keys():
p = "hiv_freq_%s"%s.lower(); f = getattr(GC,p)
if f > 1:
f = int(f)
else:
f = float(f)
assert f >= 0, "%s must be at least 0" % p
setattr(GC,p,f)
freq_sum += f
assert freq_sum > 1 or abs(freq_sum-1) < 0.000001, "Sum of hiv_freq_* parameters must equal 1"
def prep_GEMF():
# write GEMF parameter file
orig_dir = getcwd()
GC.gemf_path = expanduser(GC.gemf_path.strip())
makedirs(GC.gemf_out_dir, exist_ok=True)
f = open(GC.gemf_out_dir + "/para.txt",'w')
f.write("[NODAL_TRAN_MATRIX]\n0\t" + str(GC.hiv_ns_to_s) + "\t0\t0\t0\t0\t0\t0\t0\t0\t" + str(GC.hiv_ns_to_d) + "\n0\t0\t" + str(GC.hiv_s_to_i1_seed) + "\t0\t0\t0\t0\t0\t0\t0\t" + str(GC.hiv_s_to_d) + "\n0\t0\t0\t" + str(GC.hiv_i1_to_i2) + "\t0\t0\t" + str(GC.hiv_i1_to_a1) + "\t0\t0\t0\t" + str(GC.hiv_i1_to_d) + "\n0\t0\t0\t0\t" + str(GC.hiv_i2_to_i3) + "\t0\t0\t" + str(GC.hiv_i2_to_a2) + "\t0\t0\t" + str(GC.hiv_i2_to_d) + "\n0\t0\t0\t0\t0\t" + str(GC.hiv_i3_to_i4) + "\t0\t0\t" + str(GC.hiv_i3_to_a3) + "\t0\t" + str(GC.hiv_i3_to_d) + "\n0\t0\t0\t0\t0\t0\t0\t0\t0\t" + str(GC.hiv_i4_to_a4) + "\t" + str(GC.hiv_i4_to_d) + "\n0\t0\t" + str(GC.hiv_a1_to_i1) + "\t0\t0\t0\t0\t" + str(GC.hiv_a1_to_a2) + "\t0\t0\t" + str(GC.hiv_a1_to_d) + "\n0\t0\t0\t" + str(GC.hiv_a2_to_i2) + "\t0\t0\t0\t0\t" + str(GC.hiv_a2_to_a3) + "\t0\t" + str(GC.hiv_a2_to_d) + "\n0\t0\t0\t0\t" + str(GC.hiv_a3_to_i3) + "\t0\t0\t0\t0\t" + str(GC.hiv_a3_to_a4) + "\t" + str(GC.hiv_a3_to_d) + "\n0\t0\t0\t0\t0\t" + str(GC.hiv_a4_to_i4) + "\t0\t0\t0\t0\t" + str(GC.hiv_a4_to_d) + "\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n\n") # HIV-ART-specific
f.write("[EDGED_TRAN_MATRIX]\n")
f.write("0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t" + str(GC.hiv_s_to_i1_by_i1) + "\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0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t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n\n")
f.write("0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t" + str(GC.hiv_s_to_i1_by_a3) + "\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n\n")
f.write("0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t" + str(GC.hiv_s_to_i1_by_a4) + "\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\n\n")
f.write("[STATUS_BEGIN]\n0\n\n")
infectious = ['I1','I2','I3','I4','A1','A2','A3','A4']
f.write("[INDUCER_LIST]\n" + ' '.join([str(GC.gemf_state_to_num[i]) for i in infectious]) + "\n\n")
f.write("[SIM_ROUNDS]\n1\n\n")
f.write("[INTERVAL_NUM]\n1\n\n")
f.write("[MAX_TIME]\n" + str(GC.end_time) + "\n\n")
f.write("[MAX_EVENTS]\n" + str(GC.C_INT_MAX) + "\n\n")
f.write("[DIRECTED]\n" + str(int(GC.contact_network.is_directed())) + "\n\n")
f.write("[SHOW_INDUCER]\n1\n\n")
f.write("[DATA_FILE]\n" + '\n'.join(["network.txt"]*len(infectious)) + "\n\n")
f.write("[STATUS_FILE]\nstatus.txt\n\n")
if GC.random_number_seed is not None:
f.write("[RANDOM_SEED]\n%d\n\n"%GC.random_number_seed)
f.write("[OUT_FILE]\noutput.txt")
f.close()
# write GEMF network file
f = open(GC.gemf_out_dir + "/network.txt",'w')
num2node = {}
node2num = {}
for edge in GC.contact_network.edges_iter():
u = edge.get_from()
v = edge.get_to()
if u not in node2num:
num = len(node2num) + 1
node2num[u] = num
num2node[num] = u
if v not in node2num:
num = len(node2num) + 1
node2num[v] = num
num2node[num] = v
f.write(str(node2num[u]) + '\t' + str(node2num[v]) + '\n')
f.close()
# write GEMF to original mapping
f = open(GC.gemf_out_dir + "/gemf2orig.json",'w')
f.write(str({num:num2node[num].get_name() for num in num2node}))
f.close()
# write GEMF status file (NS = 0, S = 1, I1 = 2, I2 = 3, I3 = 4, I4 = 5, A1 = 6, A2 = 7, A3 = 8, A4 = 9, D = 10)
leftover = len(num2node)
start_states = {'seed':[], 'other':[]}
for s in infectious:
k = "hiv_freq_%s"%s.lower()
if isinstance(getattr(GC,k), float):
n = int(len(num2node)*getattr(GC,k))
else:
n = getattr(GC,k)
start_states['seed'] += [GC.gemf_state_to_num[s]]*n
leftover -= n
assert len(start_states['seed']) == len(GC.seed_nodes), "At time 0, A1+A2+A3+A4+I1+I2+I3+I4 = %d, but there are %d seed nodes. Fix hiv_freq_* parameters accordingly" % (len(start_states['seed']),len(GC.seed_nodes))
for s in ['NS','S']:
k = "hiv_freq_%s"%s.lower()
if isinstance(getattr(GC,k), float):
n = int(len(num2node)*getattr(GC,k))
else:
n = getattr(GC,k)
start_states['other'] += [GC.gemf_state_to_num[s]]*n
leftover -= n
start_states['other'] += [GC.gemf_state_to_num['D']]*leftover
shuffle(start_states['seed']); shuffle(start_states['other'])
f = open(GC.gemf_out_dir + "/status.txt",'w')
seeds = {seed for seed in GC.seed_nodes} # seed nodes are assumed to be in I1 and non-seeds to be in NS
for num in sorted(num2node.keys()):
node = num2node[num]
if node in seeds:
s = start_states['seed'].pop()
f.write("%d\n"%s)
node.gemf_state = s
else:
s = start_states['other'].pop()
f.write("%d\n"%s)
node.gemf_state = s
f.close()
# run GEMF
chdir(GC.gemf_out_dir)
try:
call([GC.gemf_path], stdout=open("log.txt",'w'))
except FileNotFoundError:
chdir(GC.START_DIR)
assert False, "GEMF executable was not found: %s" % GC.gemf_path
chdir(orig_dir)
# reload edge-based matrices for ease of use
matrices = open(GC.gemf_out_dir + '/para.txt').read().strip()
outside_infection_matrix = [[float(e) for e in l.split()] for l in matrices[matrices.index('[NODAL_TRAN_MATRIX]'):matrices.index('\n\n[EDGED_TRAN_MATRIX]')].replace('[NODAL_TRAN_MATRIX]\n','').splitlines()]
matrices = [[[float(e) for e in l.split()] for l in m.splitlines()] for m in matrices[matrices.index('[EDGED_TRAN_MATRIX]'):matrices.index('\n\n[STATUS_BEGIN]')].replace('[EDGED_TRAN_MATRIX]\n','').split('\n\n')]
matrices = {GC.gemf_state_to_num[infectious[i]]:matrices[i] for i in range(len(infectious))}
matrices[GC.gemf_state_to_num['S']] = outside_infection_matrix
# convert GEMF output to FAVITES transmission network format
GC.transmission_num = 0
GC.transmission_state = set() # 'node' and 'time'
NUM_INFECTED = len(seeds)
GC.transmission_file = []
for line in open(GC.gemf_out_dir + "/output.txt"):
t,rate,vNum,pre,post,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,lists = [i.strip() for i in line.split()]
pre,post = int(pre),int(post)
vName = num2node[int(vNum)].get_name()
lists = lists.split('],[')
lists[0] += ']'
lists[-1] = '[' + lists[-1]
for i in range(1,len(lists)-1):
if '[' not in lists[i]:
lists[i] = '[' + lists[i] + ']'
lists = [eval(l) for l in lists]
uNums = []
for l in lists:
uNums.extend(l)
if post == GC.gemf_state_to_num['D']:
NUM_INFECTED -= 1
GC.transmission_file.append((vName,vName,float(t)))
if GC.VERBOSE:
print('[%s] Uninfection\tTime %s\tNode %s (%s->%s)\tTotal Infected: %d\tTotal Uninfected: %d' % (datetime.now(),t,vName,GC.gemf_num_to_state[pre],GC.gemf_num_to_state[post],NUM_INFECTED,len(num2node)-NUM_INFECTED), file=stderr)
elif GC.gemf_num_to_state[pre] == 'S' and GC.gemf_num_to_state[post] == 'I1':
NUM_INFECTED += 1
v = num2node[int(vNum)]
uNodes = [num2node[num] for num in uNums]
uRates = [matrices[uNode.gemf_state][pre][post] for uNode in uNodes]
die = {uNodes[i]:GC.prob_exp_min(i, uRates) for i in range(len(uNodes))}
if len(die) != 0:
u = GC.roll(die) # roll die weighted by exponential infectious rates
uName = u.get_name()
if GC.VERBOSE:
print('[%s] Infection\tTime %s\tFrom Node %s (%s)\tTo Node %s (%s->%s)\tTotal Infected: %d\tTotal Uninfected: %d' % (datetime.now(),t,uName,GC.gemf_num_to_state[u.gemf_state],vName,GC.gemf_num_to_state[pre],GC.gemf_num_to_state[post],NUM_INFECTED,len(num2node)-NUM_INFECTED), file=stderr)
elif len(die) == 0 or u == v: # new seed
uName = None
if GC.VERBOSE:
print('[%s] Seed\tTime %s\tNode %s\tTotal Infected: %d\tTotal Uninfected: %d' % (datetime.now(),t,vName,NUM_INFECTED,len(num2node)-NUM_INFECTED), file=stderr)
GC.transmission_file.append((uName,v.get_name(),float(t)))
elif GC.VERBOSE:
print('[%s] Transition\tTime %s\tNode %s (%s->%s)' % (datetime.now(),t,vName,GC.gemf_num_to_state[pre],GC.gemf_num_to_state[post]), file=stderr)
num2node[int(vNum)].gemf_state = post
assert len(GC.transmission_file) != 0, "GEMF didn't output any transmissions"
GC.gemf_ready = True
GC.gemf_num2node = num2node
def sample_time():
if not GC.gemf_ready:
TransmissionTimeSample_HIVARTGranichGEMF.prep_GEMF()
return TransmissionTimeSample_TransmissionFile.sample_time()