-
Notifications
You must be signed in to change notification settings - Fork 0
/
almostInfiniteSites_simulateData.py
277 lines (224 loc) · 8.04 KB
/
almostInfiniteSites_simulateData.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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 30 08:36:25 2015
@author: mathias
"""
from finiteSitesModell_investigations import simulator_KingmanFiniteSites
# import finiteSitesModell_investigations as fsm
import sys
import numpy as np
from psiFromCSV import psiFromCSV
def simData(N,theta,L):
simTree = simulator_KingmanFiniteSites(N,theta/2,L)
S_redundantRowsAndColumns = simTree.getS()
#remove null-rows
S1 = withoutNullColumns(S_redundantRowsAndColumns)
#remove redundant rows, and associate each row with a count-vector n instead
S,n = S_and_n(S1)
return S,n
def simData_XExtraMutations(N,L,k,mutationsMustBeVisible =True):
simTree = simulator_KingmanFiniteSites(N,10*L/2.0,L,False)
result = simTree.untillFirstXInconsistencies(X = k)
if not mutationsMustBeVisible:
# assure that we have k more mutations on the underlying tree than
# we have segregating sites
while result["Inconsistencies"] < k:
result = simTree.untillFirstXInconsistencies(X = k)
else:
# Assure that we have k visible inconsistencies with the infinite sites
# model
while result["typeCount"][0] + result["typeCount"][2] < k:
result = simTree.untillFirstXInconsistencies(X = k)
S_redundantRowsAndColumns = simTree.getS()
#remove null-rows
S1 = withoutNullColumns(S_redundantRowsAndColumns)
#remove redundant rows, and associate each row with a count-vector n instead
S,n = S_and_n(S1)
return S,n
def simData_k_mutations_total(N,L,k,mutation_must_be_visible = True):
simTree = simulator_KingmanFiniteSites(N,10.0*k,L,False)
if mutation_must_be_visible:
result = simTree.until_k_visible_mutations(k)
else:
result = simTree.until_k_mutations(k)
#re-run simulations if we didn't get enough mutations.
while k > len(result['coalescent'].mutations):
simTree = simulator_KingmanFiniteSites(N,10.0*k,L,False)
if mutation_must_be_visible:
result = simTree.until_k_visible_mutations(k)
else:
result = simTree.until_k_mutations(k)
S_redundant_rows_and_columns = result['S']
S_redundant_columns, Nr = S_and_n( S_redundant_rows_and_columns )
S_transpose, Nc = S_and_n( np.transpose(S_redundant_columns) )
S = np.transpose(S_transpose)
return S,Nr,Nc
def withoutNullColumns(S):
rows,columns = S.shape
nonNullColumns = [i for i in range(columns) if max(S[:,i]) > 0]
columns_new = len(nonNullColumns)
if columns_new > 0:
S_new = S[:,nonNullColumns]
S_new.shape = (rows,columns_new)
else:
S_new = np.zeros((rows,1))
return S_new
def S_and_n(S):
rows,columns = S.shape
row_counts = {}
for row in S:
rowAsTuple = tuple(row)
if rowAsTuple in row_counts:
row_counts[rowAsTuple] += 1
else:
row_counts[rowAsTuple] = 1
rowList = row_counts.keys()
rowCounts = row_counts.values()
S_new = np.array(np.r_["0,2",rowList],dtype = int)
n_vec = np.array(rowCounts,dtype = int)
return S_new,n_vec
def toCSV(S,Nr,Nc,fileName = False):
'''
Prints the configuration (S,Nr,Nc) to fileName as a .csv-file.
If no filename is provided, prints to std-out.
'''
a = np.r_[np.c_[np.matrix(Nc), 0] , np.c_[S,Nr]]
#print a
# #sort rows so that Nr is in non-ascending order
# a = a[a[:,-1].argsort()[::-1]]
#
# #sort columns such that Nc is in non-ascending order
# a = a[:,a[0,:].argsort()[::-1]]
if fileName == False:
print '\n'.join([', '.join([str(a[i,j]) for j in xrange(a.shape[1])]) for i in xrange(a.shape[0])])
else:
np.savetxt(fileName, a, fmt = '%d', delimiter=", ")
def toCSV_old(S,n,fileName):
a = np.c_[S,n]
#sort rows so that n is in non-ascending order
a = a[a[:,-1].argsort()[::-1]]
np.savetxt(fileName, a, fmt = '%d', delimiter=", ")
# def psiFromCSV(fileName):
# """
# Takes the path of a .csv-file as input. The last column is presumed to
# encode the "n"-vector enumerating the occurrance of different haplotypes;
# The other columns are presuemd to encode haplotypes.
#
# If the below were the context of myPhi.csv:
# "
# 0, 1, 1, 0, 0, 2, 0, 0, 3, 3, 22
# 0, 0, 2, 1, 0, 3, 0, 0, 2, 1, 18
# 1, 0, 1, 0, 0, 2, 0, 0, 0, 0, 12
# 0, 1, 1, 0, 2, 2, 0, 0, 3, 3, 6
# 0, 1, 1, 0, 0, 2, 0, 0, 0, 3, 6
# 1, 2, 1, 0, 0, 0, 2, 0, 0, 0, 4
# 1, 1, 1, 3, 0, 3, 0, 0, 3, 3, 3
# 1, 1, 1, 0, 0, 2, 0, 0, 3, 3, 3
# 0, 1, 1, 2, 0, 2, 0, 0, 3, 3, 3
# 1, 0, 1, 1, 2, 2, 0, 0, 0, 0, 3
# 0, 1, 1, 0, 2, 2, 0, 1, 3, 3, 3
# 0, 3, 1, 0, 0, 2, 0, 0, 3, 3, 3
# 3, 1, 1, 0, 0, 2, 0, 2, 3, 3, 3
# 0, 3, 1, 1, 0, 3, 0, 0, 0, 1, 2
# 1, 0, 1, 0, 0, 2, 1, 0, 0, 0, 2
# 0, 0, 1, 1, 2, 3, 0, 0, 2, 1, 2
# 0, 0, 1, 1, 0, 3, 0, 0, 0, 1, 1
# 0, 0, 2, 1, 0, 3, 0, 0, 0, 1, 1
# 1, 0, 1, 1, 2, 0, 0, 0, 0, 0, 1
# 1, 1, 1, 3, 0, 3, 0, 3, 3, 3, 1
# 3, 2, 2, 1, 0, 3, 0, 0, 0, 1, 1
# "
# Then psiFromCSV(fileName) would return S,n whereby:
#
# >>> S
# array([[0, 1, 1, 0, 0, 2, 0, 0, 3, 3],
# [0, 0, 2, 1, 0, 3, 0, 0, 2, 1],
# [1, 0, 1, 0, 0, 2, 0, 0, 0, 0],
# [0, 1, 1, 0, 2, 2, 0, 0, 3, 3],
# [0, 1, 1, 0, 0, 2, 0, 0, 0, 3],
# [1, 2, 1, 0, 0, 0, 2, 0, 0, 0],
# [1, 1, 1, 3, 0, 3, 0, 0, 3, 3],
# [1, 1, 1, 0, 0, 2, 0, 0, 3, 3],
# [0, 1, 1, 2, 0, 2, 0, 0, 3, 3],
# [1, 0, 1, 1, 2, 2, 0, 0, 0, 0],
# [0, 1, 1, 0, 2, 2, 0, 1, 3, 3],
# [0, 3, 1, 0, 0, 2, 0, 0, 3, 3],
# [3, 1, 1, 0, 0, 2, 0, 2, 3, 3],
# [0, 3, 1, 1, 0, 3, 0, 0, 0, 1],
# [1, 0, 1, 0, 0, 2, 1, 0, 0, 0],
# [0, 0, 1, 1, 2, 3, 0, 0, 2, 1],
# [0, 0, 1, 1, 0, 3, 0, 0, 0, 1],
# [0, 0, 2, 1, 0, 3, 0, 0, 0, 1],
# [1, 0, 1, 1, 2, 0, 0, 0, 0, 0],
# [1, 1, 1, 3, 0, 3, 0, 3, 3, 3],
# [3, 2, 2, 1, 0, 3, 0, 0, 0, 1]])
# >>> n
# array([22, 18, 12, 6, 6, 4, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1,
# 1, 1, 1, 1])
# """
# raw = np.genfromtxt(fileName, delimiter=',')
# n = np.array(raw[:,-1], dtype = int)
# S = np.array(raw[:,:-1], dtype = int)
# return S,n
def main_old():
#Read from input
N = int(sys.argv[1])
theta = float(sys.argv[2])
L = int(sys.argv[3])
if len(sys.argv) > 4:
save = True
path = sys.argv[4]
thetaStr = ("%1.4f"%theta).replace('.','pt')
# fileName = path+"/psi__N_%i_theta_%s_L_%i.csv"%(N,thetaStr,L)
fileName = path
else:
save = False
# simTree = finiteSitesModell_investigations.simulator_KingmanFiniteSites(N,theta/2,L)
#
# S_redundantRowsAndColumns = simTree.getS()
#
# S1 = withoutNullColumns(S_redundantRowsAndColumns)
#
# S,n = S_and_n(S1)
S,n = simData(N,theta,L)
if save:
toCSV(S,n,fileName)
print "Output saved to %s"%fileName
# ##Test
# Snew,nnew = psiFromCSV(fileName)
# print "successfully loaded from %s"%fileName
# print "S =\n",Snew,"\nn=\n",nnew
else:
print "S =\n",S,"\nn=\n",n
def main():
#Read from input
N = int(sys.argv[1])
L = int(sys.argv[2])
k = int(sys.argv[3])
if len(sys.argv) > 4:
save = True
path = sys.argv[4]
#thetaStr = ("%1.4f"%theta).replace('.','pt')
fileName = path+"/psi__N_%i_L_%i_mutations_%i.csv"%(N,L,k)
else:
save = False
# simTree = finiteSitesModell_investigations.simulator_KingmanFiniteSites(N,theta/2,L)
#
# S_redundantRowsAndColumns = simTree.getS()
#
# S1 = withoutNullColumns(S_redundantRowsAndColumns)
#
# S,n = S_and_n(S1)
S,Nr,Nc = simData_k_mutations_total(N,L,k)
if save:
toCSV(S,Nr,Nc,fileName)
print "Output saved to %s"%fileName
# ##Test
# Snew,nnew = psiFromCSV(fileName)
# print "successfully loaded from %s"%fileName
# print "S =\n",Snew,"\nn=\n",nnew
else:
#print "S =\n",S,"\nn=\n",n
toCSV(S,Nr,Nc)
if __name__ == '__main__':
main()