-
Notifications
You must be signed in to change notification settings - Fork 2
/
amino_acid.py
executable file
·301 lines (230 loc) · 9.73 KB
/
amino_acid.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
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
import itertools
import pandas
import numpy
import utils
import os
import logging
class Data(object):
DEFAULT_ALLELE = ['A0201']
def __init__(self, filename, allele=None, train=False):
self.is_train_file = train
self.pkl_dir = os.path.dirname(os.getcwd()) + '/mhcPreds/extras/allele_mapping'
self.mapping = utils.load_obj(self.pkl_dir)
self.df = self.allele_df(filename, allele)
self.alleles = numpy.asarray(self.df["mhc"])
self.peptides = numpy.asarray(self.df["sequence"])
self.affinities = numpy.asarray(self.df["meas"])
self.all_peps = ['A', 'R', 'N', 'D', 'C', 'Q', 'E',
'G', 'H', 'I', 'L', 'K', 'M', 'F',
'P', 'S', 'T', 'W', 'Y', 'V']
def allele_df(self, filename, allele=None):
if not allele:
logging.warning('Allele name not provided, using default.')
allele = self.DEFAULT_ALLELE
if self.is_train_file:
allele = self.mapping[allele][0]
else:
allele = self.mapping[allele][1]
df = pandas.read_csv(filename, sep='\t')
return df[df['mhc'] == allele]
def one_hot_encoding(self, kmer_size=9):
X_index, _, original_peptide_indices, counts = fixed_length_one_hot_encoding(peptides=self.peptides,
desired_length=kmer_size)
original_peptide_indices = numpy.asarray(original_peptide_indices)
kmer_affinities = self.affinities[original_peptide_indices]
return X_index, kmer_affinities, original_peptide_indices
def index_lookup(self, aa):
return self.all_peps.index(aa)
def kmer_index_encoding(self, kmer_size=9):
X_index, _, original_peptide_indices, counts = fixed_length_index_encoding(peptides=self.peptides,
desired_length=kmer_size)
original_peptide_indices = numpy.asarray(original_peptide_indices)
kmer_affinities = self.affinities[original_peptide_indices]
return X_index, kmer_affinities, original_peptide_indices
def fixed_length_one_hot_encoding(peptides, desired_length):
fixed_length, original_peptide_indices, counts = fixed_length_from_many_peptides(peptides=peptides,
desired_length=desired_length)
one_hot_encoding = common_amino_acids.hotshot_encoding
X = one_hot_encoding(fixed_length, desired_length)
return X, fixed_length, original_peptide_indices, counts
def fixed_length_index_encoding(peptides, desired_length):
fixed_length, original_peptide_indices, counts = fixed_length_from_many_peptides(peptides=peptides,
desired_length=desired_length)
index_encoding = common_amino_acids.index_encoding
X = index_encoding(fixed_length, desired_length)
return X, fixed_length, original_peptide_indices, counts
def fixed_length_from_many_peptides(peptides, desired_length):
"""
Create a set of fixed-length k-mer peptides from a collection of varying
length peptides.
"""
all_fixed_length_peptides = []
indices = []
counts = []
for i, peptide in enumerate(peptides):
n = len(peptide)
if n == desired_length:
fixed_length_peptides = [peptide]
elif n < desired_length:
try:
fixed_length_peptides = extend_peptide(peptide=peptide, desired_length=desired_length)
except CombinatorialExplosion:
logging.warn(
"Peptide %s is too short to be extended to length %d" % (
peptide, desired_length))
continue
else:
fixed_length_peptides = shorten_peptide(peptide=peptide, desired_length=desired_length)
n_fixed_length = len(fixed_length_peptides)
all_fixed_length_peptides.extend(fixed_length_peptides)
indices.extend([i] * n_fixed_length)
counts.extend([n_fixed_length] * n_fixed_length)
return all_fixed_length_peptides, indices, counts
def extend_peptide(peptide, desired_length, start_offset_extend=2, end_offset_extend=1):
n = len(peptide)
n_missing = desired_length - n
if n_missing > 3:
raise CombinatorialExplosion(
"Cannot transform %s of length %d into a %d-mer peptide" % (
peptide, n, desired_length))
return [
peptide[:i] + extra + peptide[i:]
for i in range(start_offset_extend, n - end_offset_extend + 1)
for extra in all_kmers(n_missing)
]
def shorten_peptide(peptide, desired_length, start_offset_shorten=2, end_offset_shorten=0):
n = len(peptide)
assert n > desired_length, \
"%s (length = %d) is too short! Must be longer than %d" % (
peptide, n, desired_length)
n_skip = n - desired_length
assert n_skip > 0, \
"Expected length of peptide %s %d to be greater than %d" % (
peptide, n, desired_length)
end_range = n - end_offset_shorten - n_skip + 1
return [
peptide[:i] + peptide[i + n_skip:]
for i in range(start_offset_shorten, end_range)
]
class Alphabet(object):
"""
Used to track the order of amino acids used for peptide encodings
"""
def __init__(self, **kwargs):
self.letters_to_names = {}
for (k, v) in kwargs.items():
self.add(k, v)
def add(self, letter, name):
assert letter not in self.letters_to_names
assert len(letter) == 1
self.letters_to_names[letter] = name
def letters(self):
return list(sorted(self.letters_to_names.keys()))
def names(self):
return [self.letters_to_names[k] for k in self.letters()]
def index_dict(self):
return {c: i for (i, c) in enumerate(self.letters())}
def copy(self):
return Alphabet(**self.letters_to_names)
def __getitem__(self, k):
return self.letters_to_names[k]
def __setitem__(self, k, v):
self.add(k, v)
def __len__(self):
return len(self.letters_to_names)
def index_encoding_list(self, peptides):
index_dict = self.index_dict()
return [
[index_dict[amino_acid] for amino_acid in peptide]
for peptide in peptides
]
def index_encoding(self, peptides, peptide_length):
"""
Encode a set of equal length peptides as a matrix of their
amino acid indices.
"""
X = numpy.zeros((len(peptides), peptide_length), dtype=int)
index_dict = self.index_dict()
for i, peptide in enumerate(peptides):
for j, amino_acid in enumerate(peptide):
X[i, j] = index_dict[amino_acid]
return X
def hotshot_encoding(self, peptides, peptide_length):
"""
Encode a set of equal length peptides as a binary matrix,
where each letter is transformed into a length 20 vector with a single
element that is 1 (and the others are 0).
"""
shape = (len(peptides), peptide_length, 20)
index_dict = self.index_dict()
X = numpy.zeros(shape)
for i, peptide in enumerate(peptides):
for j, amino_acid in enumerate(peptide):
k = index_dict[amino_acid]
X[i, j, k] = 1
return X
common_amino_acids = Alphabet(**{
"A": "Alanine",
"R": "Arginine",
"N": "Asparagine",
"D": "Aspartic Acid",
"C": "Cysteine",
"E": "Glutamic Acid",
"Q": "Glutamine",
"G": "Glycine",
"H": "Histidine",
"I": "Isoleucine",
"L": "Leucine",
"K": "Lysine",
"M": "Methionine",
"F": "Phenylalanine",
"P": "Proline",
"S": "Serine",
"T": "Threonine",
"W": "Tryptophan",
"Y": "Tyrosine",
"V": "Valine",
})
common_amino_acid_letters = common_amino_acids.letters()
amino_acids_with_unknown = common_amino_acids.copy()
amino_acids_with_unknown.add("X", "Unknown")
amino_acids_with_unknown_letters = amino_acids_with_unknown.letters()
def all_kmers(k, alphabet=common_amino_acid_letters):
alphabets = [alphabet] * k
return [
"".join(combination)
for combination
in itertools.product(*alphabets)
]
class CombinatorialExplosion(Exception):
pass
"""
class ProtVec(word2vec.Word2Vec):
def __init__(self, corpus_fname=None, corpus=None, n=3, size=100, out="corpus.txt", sg=1, window=25, min_count=2, workers=3):
Either fname or corpus is required.
corpus_fname: fasta file for corpus
corpus: corpus object implemented by gensim
n: n of n-gram
out: corpus output file path
min_count: least appearance count in corpus. if the n-gram appear k times which is below min_count, the model does not remember the n-gram
self.n = n
self.size = size
self.corpus_fname = corpus_fname
if corpus is None and corpus_fname is None:
raise Exception("Either corpus_fname or corpus is needed!")
word2vec.Word2Vec.__init__(self, corpus, size=size, sg=sg, window=window, min_count=min_count, workers=workers)
def to_vecs(self, seq):
convert sequence to three n-length vectors
e.g. 'AGAMQSASM' => [ array([ ... * 100 ], array([ ... * 100 ], array([ ... * 100 ] ]
ngram_patterns = split_ngrams(seq, self.n)
protvecs = []
for ngrams in ngram_patterns:
ngram_vecs = []
for ngram in ngrams:
try:
ngram_vecs.append(self[ngram])
except:
raise Exception("Model has never trained this n-gram: " + ngram)
protvecs.append(sum(ngram_vecs))
return protvecs
"""