-
Notifications
You must be signed in to change notification settings - Fork 0
/
featurize_seq.py
105 lines (56 loc) · 1.89 KB
/
featurize_seq.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
import numpy as np
from keras.utils import to_categorical
import random
def kmerize(seq, k, stride):
kmers = []
for i in range(0, len(seq) - k + 1, stride):
kmers.append(seq[i:i + k])
return kmers
def generate_all_unique_kmers(k):
bases = ['A', 'G', 'C', 'T']
unique_kmers = []
for i in range(len(bases) ** k):
base_codes = []
current_val = i
for h in range(1, k + 1):
base_codes.append(current_val % 4)
current_val /= 4
s = ''
for j in base_codes:
s += bases[j]
unique_kmers.append(s)
return unique_kmers
def featurize_seq(seq, k, stride, kmer_list):
bases = ['A', 'T', 'C', 'G']
for i in range(len(seq)):
if seq[i] not in bases:
seq = seq[:i] + random.choice(bases) + seq[i + 1 : ]
#kmer_list = generate_all_unique_kmers(k)
kmers = kmerize(seq, k, stride)
feature_vector = []
for z in kmers:
feature_vector.append(kmer_list.index(z))
#encoded = to_categorical(feature_vector, 4 ** k )
#return encoded, feature_vector
return feature_vector
def embedding_featurize_seq(seq, mk_model, k, stride, kmer_list):
bases = ['A', 'T', 'C', 'G']
for i in range(len(seq)):
if seq[i] not in bases:
seq = seq[:i] + random.choice(bases) + seq[i + 1 : ]
#kmer_list = generate_all_unique_kmers(k)
kmers = kmerize(seq, k, stride)
feature_vector = []
for z in kmers:
feature_vector.extend(list(mk_model.vector(z)))
#encoded = to_categorical(feature_vector, 4 ** k )
#return encoded, feature_vector
return feature_vector
def flatten_feature_vector(feature_vector):
return [item for sublist in feature_vector for item in sublist]
'''
z, feature_vector = featurize_seq('AATTGCTAGGC', 3, 2)
print feature_vector
print z
print flatten_feature_vector(z)
'''