-
Notifications
You must be signed in to change notification settings - Fork 0
/
k-mer.py
80 lines (66 loc) · 1.87 KB
/
k-mer.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
from filenames import *
gene_list = ['A', 'C', 'G', 'T']
#################################################
# Helper functions define here
#################################################
def backtracking(k, comb, res):
"""
Use backtracking algorithm to find all possible combinations of length k.
Parameters
---------------
k: int
Length of combination.
comb: string
Current combination.
res: list
The list that contains all possible combinations.
Returns
---------------
None
"""
if k == 0:
res.append(comb)
return
for gene in gene_list:
comb += gene
backtracking(k-1, comb, res)
comb = comb[:-1]
def get_k_mer_list(k):
"""
Get the k-mer list with a given k.
Parameters
---------------
k: int
Length of combination.
Returns
---------------
res: list
The list that contains all possible combinations of length k.
"""
res = []
backtracking(k, "", res)
return res
#################################################
# Extract k-mer features
#################################################
# get 4-mer list
k_mer_list = get_k_mer_list(4)
k_mer_dict = {}
for i in range(len(k_mer_list)):
k_mer_dict[k_mer_list[i]] = i
# translate the gene transcript to k-mer features
k_mer_features = []
with open(data_path + sentence_file, "r") as infile:
for line in infile:
words = line.split()
feature = [0] * len(k_mer_list)
for word in words:
feature[k_mer_dict[word]] += 1
feature = [x / len(words) for x in feature]
k_mer_features.append(feature)
# save k-mer features to file
with open(feature_path + k_mer_feature_file, "w+") as outfile:
for feature in k_mer_features:
for item in feature:
outfile.write("%f " % item)
outfile.write("\n")