-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_gen.py
More file actions
129 lines (121 loc) · 3.96 KB
/
query_gen.py
File metadata and controls
129 lines (121 loc) · 3.96 KB
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
import sys
import numpy as np
alph = ['A', 'C', 'G', 'T']
'''
Takes as input the name of the file containing the genome, and the name of the file specifying the probabilities for said genome at each position.
Returns a dict of lists in the following format:
For each character in the alphabet (Currently assumed to be ACGT), there is a list of length n, where n is the length of the genome
Each position in this list gives the probability that this position has the given character
'''
def load_data(genome_file, probs_file, alphabet):
matrix = dict()
gf = open(genome_file)
genome = gf.read()
gf.close()
pf = open(probs_file)
probs = pf.read()
pf.close()
probs = probs.split()
genome = list(genome)
for char in alphabet:
matrix[char] = []
for i in range(len(genome)):
if genome[i] not in alphabet:
pass
else:
others = [c for c in alphabet if c!=genome[i]]
otherProb = (1-float(probs[i]))/float(len(alphabet)-1)
matrix[genome[i]].append(float(probs[i]))
for nucleotide in others:
matrix[nucleotide].append(otherProb)
return matrix
def insertion(prob):
if np.random.random_sample() < prob:
return True
else:
return False
def deletion(prob):
if np.random.random_sample() < prob:
return False
else:
return True
def markDeletions(prob, characters):
toDelete = set()
for i in range(len(characters)):
if np.random.random_sample()<prob:
if np.random.random_sample()<0.01:
#1 delete
toDelete.add(i)
else:
toDelete.add(i)
if i>=0:
toDelete.add(i-1)
if i<len(characters):
toDelete.add(i+1)
return toDelete
def deleteMany(prob, positions, characters):
toreturn = []
toDel = markDeletions(prob, characters)
for i in range(len(characters)):
if i not in toDel:
toreturn.append(characters[i])
return toreturn
def substitution(el, prob):
if np.random.random_sample() < prob:
return np.random.choice(alph)
else:
return el
def main():
genome_file = sys.argv[1]
prob_file = sys.argv[2]
length = int(sys.argv[3])
sub = float(sys.argv[4])
insert = float(sys.argv[5])
delete = float(sys.argv[6])
num_queries = int(sys.argv[7])
g_mat = load_data(genome_file, prob_file, alph)
positions = np.arange(len(g_mat[alph[0]]))
starts = []
for i in range(num_queries):
starts.append(np.random.choice(positions))
queries = []
for i in range(num_queries):
query = []
current = starts[i]
for i in range(length):
pro = []
for char in alph:
pro.append(g_mat[char][current])
query.append(np.random.choice(alph,p=pro))
current+=1
queries.append(query)
#Write unmutated
out3 = open("unmutated.txt", "w")
for i in range(len(queries)):
for char in queries[i]:
out3.write(char)
out3.write(", "+str(starts[i]))
out3.write("\n")
#insertions
for i in range(len(queries)):
for j in range(len(queries[i])):
if insertion(insert):
queries[i].insert(j, np.random.choice(alph))
#deletions
queries[i] = deleteMany(delete, [posit for posit in range(len(queries[i]))], queries[i])#[x for x in queries[i] if deletion(delete)]
#substitutions
queries[i] = [substitution(x, sub) for x in queries[i]]
out = open("query.txt", "w")
for i in range(len(queries)):
for q in queries[i]:
for char in q:
out.write(char)
out.write("\n")
out.close()
#out2 = open("params.txt", "w")
#for i in range(num_queries):
# out2.write(str(starts[i])+"\n")
#out2.close()
print(len(g_mat[alph[0]]))
if __name__ == "__main__":
main()