-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgmlstsearch.py
executable file
·203 lines (184 loc) · 6.33 KB
/
cgmlstsearch.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
#!/usr/bin/env python3
import argparse
import os.path
import numpy as np
import Trie
import pickle
from copy import copy
from scipy.stats import binom
import cProfile
## dtype
dtype = np.uint8
def readargs():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('--naive', action='store_true')
group.add_argument('--trie', action='store_true')
group.add_argument('--bin', action='store_true')
parser.add_argument('--mongo', action='store_true')
parser.add_argument('--heuristic', action='store_true')
parser.add_argument('--ntypes', type=int, default=100)
parser.add_argument('--nseqs', type=int, default=200000)
parser.add_argument('--schemalength', type=int, default=3500)
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--diversity', type=int, default=100)
parser.add_argument('--seqs', type=str, default=".seqs.npy")
parser.add_argument('--index', type=str, default=".seqs.idx")
parser.add_argument('--distance', type=int, default=10)
parser.add_argument('--create-seqs', action='store_true',
help="Force creation of seqs and indexes")
parser.add_argument('--create-index', action='store_true',
help="Force creation of indices")
return parser.parse_args()
def create_seqs(nseqs, l, diversity, seed):
"""Create random seqs"""
allele_maxima = np.ones(l)
originalseq = np.ones(l)
#originalseq = np.random.randint(low=1,high=diversity,size=l)
seqs = np.ndarray((nseqs, l), dtype=dtype)
seqs[0] = originalseq
#distances = np.random.poisson(diversity,size=nseqs-1)
distances = np.random.binomial(l, diversity/l, size=nseqs-1)
n = 0
for i in distances:
n += 1
sites = np.random.choice(l, size=i, replace=False)
originalseq = seqs[np.random.choice(n)]
newseq = copy(originalseq)
allele_maxima[sites] += 1
newseq[sites] = allele_maxima[sites]
seqs[n] = newseq
return seqs
def search_seqs(seqs, query, maxdist):
## Naive search
hits = list()
for s in seqs:
result = compare(s, query, maxdist)
if result is not None:
hits.append(result)
return hits
def compare(s, query, maxdist):
assert(len(s) == len(query))
d = 0
for a, b in zip(s, query):
if a != b:
d += 1
if d > maxdist:
return
return s
def compare_heuristic(s, query, maxdist, softrange):
d = 0
i = 0
p = 0
checkpoint, low, high = softrange[p]
p += 1
for a, b in zip(s, query):
i += 1
if a != b:
d += 1
if d > maxdist:
return
if i%checkpoint == 0:
if d < low:
return s
elif d > high:
#print(i)
return
if len(softrange) > p:
checkpoint, low, high = softrange[p]
p+=1
return s
def search_seqs_heuristic(seqs, query, maxdist):
## Heuristic search
softrange = []
for i in [10, 100, 1000, 2000]:
b = binom(i, maxdist/len(query))
softrange.append((i, b.ppf([0.0001]), b.ppf([0.9999])))
hits = list()
for k, s in seqs:
result = compare_heuristic(np.array(s), query, maxdist, softrange)
if result is not None:
hits.append(result)
return hits
def index_trie(seqs, indexpath):
if os.path.exists(indexpath) and not args.create_index and not args.create_seqs:
index = pickle.load(open(indexpath, 'rb'))
else:
index = Trie.Tries(7, range(len(seqs)), 100, seqs)
pickle.dump(index, open(indexpath, 'wb'))
assert(len(seqs) == len(index))
return index
def index_bin(seqs, indexpath):
if os.path.exists(indexpath) and not args.create_index and not args.create_seqs:
index = pickle.load(open(indexpath, 'rb'))
else:
index = Bin_index.Bin_indices(7, range(len(seqs[0])), 64)
pickle.dump(index, open(indexpath, 'wb'))
assert(len(seqs) == len(index))
return index
def search_trie_heuristic(index, seqs, query, maxdist):
idx = index.search(query)
print(len(idx))
softrange = []
alpha = 0.01
for i in [10, 100, 1000, 2000]:
b = binom(i, maxdist/len(query))
softrange.append((i, b.ppf([alpha]), b.ppf([1-alpha])))
print(softrange)
hits=list()
for i in idx:
result = compare_heuristic(np.array(seqs[i]), query, maxdist, softrange)
if result is not None:
hits.append(result)
return hits
def search_trie(index, seqs, query, maxdist):
idx = index.search(query)
print(len(idx))
hits=list()
for i in idx:
result = compare(np.array(seqs[i]), query, maxdist)
if result is not None:
hits.append(result)
return hits
def dist(seq,query):
assert(len(seq) == len(query))
d = 0
for a, b in zip(seq, query):
if a != b:
d+=1
return d
if __name__=="__main__":
args = readargs()
if os.path.exists(args.seqs) and args.create_seqs == False:
seqs = np.memmap(args.seqs, mode='r+', dtype=dtype,
shape=(args.nseqs, args.schemalength))
else:
seqs = create_seqs(args.nseqs, args.schemalength,
args.diversity, args.seed)
mm = np.memmap(args.seqs, dtype=dtype, mode='w+',
shape=(args.nseqs, args.schemalength))
mm[:] = seqs[:]
#s = seqs[np.random.choice(args.nseqs)]
s = np.array(seqs[10])
hits=[]
if args.naive:
#cProfile.run("search_seqs(seqs,s,args.distance)")
if args.heuristic:
hits = search_heuristic(seqs, s, args.distance)
else:
hits = search_seqs(seqs, s, args.distance)
elif args.trie:
index = index_trie(seqs, args.index)
#cProfile.run("search_trie(index,seqs,s,args.distance)")
if args.heuristic:
hits = search_trie_heuristic(index, seqs, s, args.distance)
else:
hits = search_trie(index, seqs, s, args.distance)
elif args.bin:
index = index_bin(seqs, args.index)
rough_list = index.search(s, args.distance)
if args.heuristic:
hits = search_heuristic(rough_list, s, args.distance)
else:
hits = search_seqs(rough_list, s, args.distance)
print(len(hits))