-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_k_mers.py
49 lines (33 loc) · 989 Bytes
/
get_k_mers.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
from collections import Counter
def get_all_k_mer(seq):
'''
get all k_mer candidate from sequence.
:param seq:
:return:
'''
result = list()
for k_length in range(1, len(seq)):
for index in range(len(seq) - k_length + 1):
result.append(seq[index: index + k_length])
return result
def get_k_mer(seq, k_lgth):
result = list()
for i in range(len(seq) - k_lgth + 1):
result.append(seq[i: i + k_lgth])
return result
def get_most_common_k_mer(seq, k_lgth):
'''
:param seq_list: k-mer list
:return:
'''
seq_list = get_k_mer(seq, k_lgth)
cnt = Counter(seq_list)
tmp_cnt = cnt.most_common
most_common = tmp_cnt(1)[0][1]
if most_common <= 0:
print("the frequent item is None...")
raise ValueError
result = [x[0] for x in tmp_cnt() if x[1] == most_common]
return result, most_common
string = "ACGTTGCATGTCGCATGATGCATGAGAGCT"
print(get_k_mer(string, 4))