-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrieFind.py
More file actions
333 lines (254 loc) · 11.7 KB
/
TrieFind.py
File metadata and controls
333 lines (254 loc) · 11.7 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from io import BufferedReader, BytesIO
from typing import List, Tuple
from pymongo import MongoClient
from bson.objectid import ObjectId
import mongo
from misc import Bytable, binary_to_list, bytes_to_int, int_to_bytes, list_to_binary
from FoundMap import FoundMap, MemoryMap, ReadOnlyMap, get_foundmap, read_foundmap
from Nodmer import Nodmer
from constants import BINARY_DATA, DATABASE_NAME, DEFAULT_COLLECTION, EXTRACT_KMER, EXTRACT_OBJ, FOUNDMAP_MEMO, FOUNDMAP_MODE, INSERT_MANY, INT_SIZE, LABEL, MAXIMUM_ORDER_SIZE, MONGO_ID, POP, QUEUE_COLLECTION
'''
Trie node object -> two purposed object for <searching> and <saving> kmers
each node belongs to a level of tree and has a label indicating coresponding kmer
<searching>: a trie binding to gkhood is used for searching a specific node for its kmer
also this object will generate all nodes (child_birth) for its gkhood.
each node within gkhood dimensions has a reference to coreponding node in gkhood
<saving>: also known as motif tree, this object will save kmers occurrence information
starting with a single root node, for each new kmer the coresponding path will be traversed
or generated if it isn't exist. each node, in this mode, holds a sorted seen-list
seen-list -> a list containing sequence IDs that coresponding kmer had been seen
'''
class TrieNode:
def __init__(self, label='', level=0):
self.label = label
self.childs = []
self.level = level
# ########################################## #
# searching section #
# ########################################## #
class SearchNode(TrieNode):
# root node is in first (zero) level
def is_root(self):
return self.level == 0
def get_node(self):
if hasattr(self, 'node'):
return self.node
def create_node(self, gkhood):
self.node = Nodmer(gkhood, self.label)
return self.node
'''
recursively, initial a complete tree up to gkhood maximum size level
each node that is within gkhood dimensions will generate a new node
'''
def child_birth(self, gkhood):
if self.level == gkhood.kmax:
return [self.create_node(gkhood)]
self.childs = [SearchNode(self.label + letter, self.level+1) for letter in gkhood.alphabet]
grandchildsNodmers = []
for child in self.childs:
grandchildsNodmers += child.child_birth(gkhood)
if self.level >= gkhood.kmin:
grandchildsNodmers += [self.create_node(gkhood)]
return grandchildsNodmers
# recursively will find a specific node coresponding to a specific kmer
def find(self, kmer):
if len(kmer) == 0:
return self.get_node()
if len(self.childs) == 0:
return None
for child in self.childs:
if child.label[-1] == kmer[0]:
return child.find(kmer[1:])
# ########################################## #
# saving seen kmers section #
# ########################################## #
class WatchNode(TrieNode):
'''
any node with found_list attribute is considered a leaf presenting motif
found_list is an abstract structure that is constructed by lists
a found_list has two list:
found_list[0] -> seq_ids
found_list[1] -> position of occurrence on each sequence
each element with same index in both list are related described below:
any element in second list such as found_list[1][i] is a list of position that
this node of tree occurs in sequence number of found_list[0][i]
[UPDATE]: experiment shows that saving all data in memory results in RAM overflow!
a class named FoundMap implemented to save such data in both memory and disk instead
of found_list which is now removed from TrieFind class
'''
def add_frame(self, kmer, seq_id, position):
if len(kmer) == 0:
# end of the path
if not hasattr(self, 'foundmap'):
self.foundmap = get_foundmap()
self.foundmap.add_location(seq_id, position)
# self.found_list = binary_special_add(self.found_list, seq_id, position)
return
# searching for proper path
for child in self.childs:
if child.label[-1] == kmer[0]:
return child.add_frame(kmer[1:], seq_id, position)
# proper child (path) dose not exist
new_child = WatchNode(self.label + kmer[0], self.level+1)
self.childs += [new_child]
return new_child.add_frame(kmer[1:], seq_id, position)
'''
extracting motifs, nodes that are present in q number of sequences
result_kmer -> indicates output type: 0 for object and 1 for kmer (string-fromat) only
greaterthan
True (on): include all motifs with q-value greater than input q variable
False (off): only include motifs with q-value equal to input variable
'''
def extract_motifs(self, q, result_kmer=EXTRACT_KMER, greaterthan=True):
motifs = []
if hasattr(self, 'foundmap'):
if self.foundmap.get_q() >= q:
if greaterthan or self.foundmap.get_q() == q:
if result_kmer==EXTRACT_KMER :motifs += [self.label]
elif result_kmer==EXTRACT_OBJ :motifs += [self]
for child in self.childs:
motifs += child.extract_motifs(q, result_kmer, greaterthan)
return motifs
'''
special extraction function for chaining
delete child attrubutes after extraction
also return total number of nodes in tree
'''
def extract_motifs_and_delete_childs(self, q, result_kmer=EXTRACT_KMER):
motifs = []
tree_node_count = 1
if hasattr(self, 'foundmap'):
if self.foundmap.get_q() >= q:
if result_kmer==EXTRACT_KMER :motifs += [self.label]
elif result_kmer==EXTRACT_OBJ :motifs += [self]
for child in self.childs:
child_motifs, child_tree_count = child.extract_motifs_and_delete_childs(q, result_kmer)
motifs += child_motifs
tree_node_count += child_tree_count
# delete childs references
del self.childs
return motifs, tree_node_count
def find_max_q(self, q_old=-1):
my_q = -1
if hasattr(self, 'foundmap'):
my_q = self.foundmap.get_q()
childs_max_q = []
for child in self.childs:
childs_max_q += [child.find_max_q(q_old)]
return max(childs_max_q + [my_q, q_old])
def instances_str(self, sequences):
return self.foundmap.instances_to_string_fastalike(self.label, sequences)
def clear(self):
if hasattr(self, 'foundmap'):
self.foundmap.clear()
for child in self.childs:
child.clear()
class WatchNodeC(WatchNode):
def __init__(self, label='', level=0, custom_foundmap_type=FOUNDMAP_MODE):
super().__init__(label=label, level=level)
self.custom_foundmap_type = custom_foundmap_type
# override
def add_frame(self, kmer, seq_id, position):
if len(kmer) == 0:
# end of the path
if not hasattr(self, 'foundmap'):
self.foundmap = get_foundmap(foundmap_type=self.custom_foundmap_type)
self.foundmap.add_location(seq_id, position)
return
# searching for proper path
for child in self.childs:
if child.label[-1] == kmer[0]:
return child.add_frame(kmer[1:], seq_id, position)
# proper child (path) dose not exist
new_child = WatchNodeC(self.label + kmer[0], self.level+1, custom_foundmap_type=self.custom_foundmap_type)
self.childs += [new_child]
return new_child.add_frame(kmer[1:], seq_id, position)
# ########################################## #
# chain section #
# ########################################## #
class ChainNode(Bytable):
def __init__(self, label, foundmap: FoundMap):
self.foundmap = foundmap
self.label = label
'''
serialization methods for byte/object conversion
'''
def to_byte(self):
return int_to_bytes(len(self.label)) + \
bytes(self.label, encoding='ascii') + \
self.foundmap.to_byte()
@staticmethod
def byte_to_object(buffer: BufferedReader):
first_read = buffer.read(INT_SIZE)
if first_read:
label = str(buffer.read(bytes_to_int(first_read)), 'ascii')
foundmap = read_foundmap(buffer)
return ChainNode(label, foundmap)
'''
instances of pattern (non-redundant instances)
use instances_fasta for exact location of each instance or instances_list for only sequences
'''
def instances_fasta(self, sequences):return self.foundmap.remove_redundant().instances_to_string_fastalike(self.label, sequences)
def instances_list(self, sequences):return self.foundmap.remove_redundant().instances_string_list(sequences)
def initial_chainNodes(tuples:List[Tuple[str, FoundMap]], collection_name, client:MongoClient=None)->List[ChainNode]:
if not tuples:return [] # nothing to submit? are you making fun of me? -__-
if not client:client = mongo.get_client();should_close = True
else :should_close = False
order = [] # list of dictionaries for mongod process
objects = [] # return result objects of ReadOnlyMap
collection = client[DATABASE_NAME][collection_name]
for label, foundmap in tuples:
# check for order size limit
if len(order) == MAXIMUM_ORDER_SIZE:
error = mongo.safe_operation(collection, INSERT_MANY, order)
if error:
if should_close:client.close()
return error
else:order = []
readonlymap = ReadOnlyMap(collection_name, ObjectId().binary)
chain_node = ChainNode(label=label, foundmap=readonlymap)
order.append({MONGO_ID :readonlymap.address,
BINARY_DATA:list_to_binary(foundmap.get_list()),
LABEL:label})
objects.append(chain_node)
error = mongo.safe_operation(collection, INSERT_MANY, order)
if should_close:client.close()
if error:return error
else :return objects
def pop_chain_node(client:MongoClient=None):
if not client:client=mongo.get_client();should_close = True
else :should_close = False
collection = client[DATABASE_NAME][QUEUE_COLLECTION]
popy = mongo.safe_operation(collection, POP)
if should_close:client.close()
if not popy:return None
if not isinstance(popy, dict):return popy # as error
return ChainNode(popy[LABEL], MemoryMap(initial=binary_to_list(BytesIO(popy[BINARY_DATA]))))
# ########################################## #
# main function section #
# ########################################## #
def try_this():
tree = WatchNodeC(custom_foundmap_type=FOUNDMAP_MEMO)
tree.add_frame('AAT', 0, 0)
tree.add_frame('ATT', 0, 2)
tree.add_frame('AAT', 1, 5)
tree.add_frame('AAT', 3, 43)
tree.add_frame('AAT', 5, 0)
return tree
# testing binary search-add
def test_main():
t = TrieNode()
t.add_frame('AAT', 0, 0)
t.add_frame('ATT', 0, 0)
t.add_frame('ATT', 1, 0)
t.add_frame('ATT', 2, 1)
t.add_frame('GGG', 1, 0)
t.add_frame('GGG', 2, 0)
t.add_frame('GGG', 0, 0)
q = t.find_max_q()
print(t.extract_motifs(q))
##########################################
# main function call
if __name__ == "__main__":
# test_main()
t = try_this()