forked from AaronHong1024/ONeSAMP_3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonesamp3.py
281 lines (217 loc) · 7.74 KB
/
onesamp3.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
278
279
280
281
#!/usr/bin/python
import subprocess
import sys
import argparse
import os
import numpy as np
import time
from statistics import statisticsClass
NUMBER_OF_STATISTICS = 5
DEBUG = 0 ## BOUCHER: Change this to 1 for debuggin mode
OUTPUTFILENAME = "priors.txt"
BASE_PATH = os.path.dirname(os.path.realpath(__file__))
POPULATION_GENERATOR = "./build/OneSamp"
FINAL_R_ANALYSIS = "./scripts/rScript.r"
#############################################################
## Helper functions
#############################################################
def mean(data):
n = len(data)
mean = sum(data) / n
return mean
def variance(data):
n = len(data)
mean = sum(data) / n
deviations = [(x - mean) ** 2 for x in data]
variance = sum(deviations) / n
return variance
def stdev(data):
import math
var = variance(data)
std_dev = math.sqrt(var)
return std_dev
def normalization(data, mean, variance):
from operator import truediv
normalized_data = [0 for a in range(len(str(data)))]
cnt = 0
for x in range(len(str(data))):
normalized_data[cnt] = truediv((x - mean), float(np.sqrt(variance)))
cnt += 1
return normalized_data
#############################################################
start_time = time.time()
parser = argparse.ArgumentParser()
parser.add_argument("--m", type=float, help="Minimum Allele Frequency")
parser.add_argument("--r", type=float, help="Mutation Rate")
parser.add_argument("--lNe", type=int, help="Lower of Ne Range")
parser.add_argument("--uNe", type=int, help="Upper of Ne Range")
parser.add_argument("--lT", type=float, help="Lower of Theta Range")
parser.add_argument("--uT", type=float, help="Upper of Theta Range")
parser.add_argument("--s", type=int, help="Number of OneSamp Trials")
parser.add_argument("--lD", type=float, help="Lower of Duration Range")
parser.add_argument("--uD", type=float, help="Upper of Duration Range")
parser.add_argument("--i", type=float, help="Missing data for individuals")
parser.add_argument("--l", type=float, help="Missing data for loci")
parser.add_argument("--o", type=str, help="The File Name")
args = parser.parse_args()
#########################################
# INITIALIZING PARAMETERS
#########################################
minAlleleFreq = 0.005
if (args.m):
minAlleleFreq = float(args.m)
mutationRate = 0.000000012
if (args.r):
mutationRate = float(args.r)
lowerNe = 10
if (args.lNe):
lowerNe = int(args.lNe)
upperNe = 500
if (args.uNe):
upperNe = int(args.uNe)
if (int(lowerNe) > int(upperNe)):
print("ERROR:main:lowerNe > upperNe. Fatal Error")
exit()
if (int(lowerNe) < 1):
print("ERROR:main:lowerNe must be a positive value. Fatal Error")
exit()
if (int(upperNe) < 1):
print("ERROR:main:upperNe must be a positive value. Fatal Error")
exit()
rangeNe = "%d,%d" % (lowerNe, upperNe)
lowerTheta = 1
if (args.lT):
lowerTheta = float(args.lT)
upperTheta = 10
if (args.uT):
upperTheta = float(args.uT)
rangeTheta = "%d,%d" % (lowerTheta, upperTheta)
numOneSampTrials = 50000
if (args.s):
numOneSampTrials = int(args.s)
lowerDuration = 2
if (args.lD):
lowerDuration = float(args.lD)
upperDuration = 8
if (args.uD):
upperDuration = float(args.uD)
indivMissing = .2
if (args.i):
indivMissing = float(args.i)
lociMissing = .2
if (args.l):
lociMissing = float(args.l)
rangeDuration = "%d,%d" % (lowerDuration, upperDuration)
fileName = "oneSampIn"
if (args.o):
fileName = str(args.o)
else:
print("WARNING:main: No filename provided. Using oneSampIn")
if (DEBUG):
print("Start calculation of statistics for input population")
rangeTheta = "%d,%d" % (lowerTheta, upperTheta)
#########################################
# STARTING INITIAL POPULATION
#########################################
inputFileStatistics = statisticsClass()
inputFileStatistics.testRead(fileName)
#inputFileStatistics.readData(fileName)
inputFileStatistics.filterIndividuals(indivMissing)
# inputFileStatistics.testfilerIndividuals(indivMissing)
inputFileStatistics.filterLoci(lociMissing)
#inputFileStatistics.stat1()
inputFileStatistics.new_stat1()
inputFileStatistics.newStat4()
inputFileStatistics.stat2()
inputFileStatistics.stat3()
# inputFileStatistics.stat4()
inputFileStatistics.stat5()
numLoci = inputFileStatistics.numLoci
sampleSize = inputFileStatistics.sampleSize
##Creting input file with intial statistics
textList = [str(inputFileStatistics.stat1), str(inputFileStatistics.stat2), str(inputFileStatistics.stat3),
str(inputFileStatistics.stat4), str(inputFileStatistics.stat5)]
with open('./inputPopStats', 'w') as fileINPUT:
fileINPUT.write('\t'.join(textList[0:]) + '\t')
fileINPUT.close()
if (DEBUG):
print("Finish calculation of statistics for input population")
#############################################
# FINISH STATS FOR INITIAL INPUT POPULATION
############################################
#########################################
# STARTING ALL POPULATIONS
#########################################
if (DEBUG):
print("Start calculation of statistics for ALL populations")
statistics1 = []
statistics2 = []
statistics3 = []
statistics4 = []
statistics5 = []
statistics1 = [0 for x in range(numOneSampTrials)]
statistics2 = [0 for x in range(numOneSampTrials)]
statistics3 = [0 for x in range(numOneSampTrials)]
statistics4 = [0 for x in range(numOneSampTrials)]
statistics5 = [0 for x in range(numOneSampTrials)]
fileALLPOP = open('allPopStats', 'w+')
for x in range(numOneSampTrials):
loci = inputFileStatistics.numLoci
sampleSize = inputFileStatistics.sampleSize
#change the intermediate file name
intermediateFilename = "./genePopTiny"
cmd = "%s -u%d -v%s -rC -l%d -i%d -d%s -s -t1 -b%s -f%d -o1 -p > %s" % (
POPULATION_GENERATOR, mutationRate, rangeTheta, loci, sampleSize, rangeDuration, rangeNe, minAlleleFreq,
intermediateFilename)
if (DEBUG):
print(cmd)
returned_value = os.system(cmd)
if returned_value:
print("ERROR:main:Refactor did not run")
exit()
refactorFileStatistics = statisticsClass()
#refactorFileStatistics.readData(intermediateFilename)
refactorFileStatistics.testRead(intermediateFilename)
#refactorFileStatistics.stat1()
refactorFileStatistics.new_stat1()
refactorFileStatistics.stat2()
refactorFileStatistics.stat3()
refactorFileStatistics.newStat4()
refactorFileStatistics.stat5()
statistics1[x] = refactorFileStatistics.stat1
statistics2[x] = refactorFileStatistics.stat2
statistics3[x] = refactorFileStatistics.stat3
statistics4[x] = refactorFileStatistics.stat4
statistics5[x] = refactorFileStatistics.stat5
# Making file with stats from all populations
textList = []
textList = [str(refactorFileStatistics.NE_VALUE), str(refactorFileStatistics.stat1),
str(refactorFileStatistics.stat2),
str(refactorFileStatistics.stat3),
str(refactorFileStatistics.stat4), str(refactorFileStatistics.stat5)]
# print (textList)
fileALLPOP.write('\t'.join(textList) + '\n')
fileALLPOP.close()
#########################################
# FINISHING ALL POPULATIONS
########################################
# STARTING RSCRIPT
#########################################
ALL_POP_STATS_FILE = "allPopStats"
rScriptCMD = "Rscript %s < %s" % (FINAL_R_ANALYSIS, ALL_POP_STATS_FILE)
print(rScriptCMD)
res = os.system(rScriptCMD)
if (res):
print("ERROR:main: Could not run Rscript. FATAL ERROR.")
exit()
if (DEBUG):
print("Finish linear regression")
print("--- %s seconds ---" % (time.time() - start_time))
# Deleting temporary files
delete1 = "rm ./inputPopStats"
delete_INPUTPOP = os.system(delete1)
delete2 = "rm ./allPopStats"
delete_ALLPOP = os.system(delete2)
##########################
# END
#########################