-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFeatureGeneration.py
More file actions
346 lines (271 loc) · 12.9 KB
/
FeatureGeneration.py
File metadata and controls
346 lines (271 loc) · 12.9 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
334
335
336
337
338
339
340
341
342
343
344
345
import math
import itertools
# amino acid dictionary
AminoAcidDictionary = {
'A': 0, 'R': 0, 'N': 0, 'D': 0,
'C': 0, 'Q': 0, 'E': 0, 'G': 0,
'H': 0, 'I': 0, 'L': 0, 'K': 0,
'M': 0, 'F': 0, 'P': 0, 'S': 0,
'T': 0, 'W': 0, 'Y': 0, 'V': 0
}
# this dictionary contains all codones
CodonsDict = {
'TTT': 0, 'TTC': 0, 'TTA': 0, 'TTG': 0, 'CTT': 0,
'CTC': 0, 'CTA': 0, 'CTG': 0, 'ATT': 0, 'ATC': 0,
'ATA': 0, 'ATG': 0, 'GTT': 0, 'GTC': 0, 'GTA': 0,
'GTG': 0, 'TAT': 0, 'TAC': 0, 'TAA': 0, 'TAG': 0,
'CAT': 0, 'CAC': 0, 'CAA': 0, 'CAG': 0, 'AAT': 0,
'AAC': 0, 'AAA': 0, 'AAG': 0, 'GAT': 0, 'GAC': 0,
'GAA': 0, 'GAG': 0, 'TCT': 0, 'TCC': 0, 'TCA': 0,
'TCG': 0, 'CCT': 0, 'CCC': 0, 'CCA': 0, 'CCG': 0,
'ACT': 0, 'ACC': 0, 'ACA': 0, 'ACG': 0, 'GCT': 0,
'GCC': 0, 'GCA': 0, 'GCG': 0, 'TGT': 0, 'TGC': 0,
'TGA': 0, 'TGG': 0, 'CGT': 0, 'CGC': 0, 'CGA': 0,
'CGG': 0, 'AGT': 0, 'AGC': 0, 'AGA': 0, 'AGG': 0,
'GGT': 0, 'GGC': 0, 'GGA': 0, 'GGG': 0}
# this dictionary shows which codons encode the same AA
SynonymousCodons = {
'CYS': ['TGT', 'TGC'],
'ASP': ['GAT', 'GAC'],
'SER': ['TCT', 'TCG', 'TCA', 'TCC', 'AGC', 'AGT'],
'GLN': ['CAA', 'CAG'],
'MET': ['ATG'],
'ASN': ['AAC', 'AAT'],
'PRO': ['CCT', 'CCG', 'CCA', 'CCC'],
'LYS': ['AAG', 'AAA'],
'STOP': ['TAG', 'TGA', 'TAA'],
'THR': ['ACC', 'ACA', 'ACG', 'ACT'],
'PHE': ['TTT', 'TTC'],
'ALA': ['GCA', 'GCC', 'GCG', 'GCT'],
'GLY': ['GGT', 'GGG', 'GGA', 'GGC'],
'ILE': ['ATC', 'ATA', 'ATT'],
'LEU': ['TTA', 'TTG', 'CTC', 'CTT', 'CTG', 'CTA'],
'HIS': ['CAT', 'CAC'],
'ARG': ['CGA', 'CGC', 'CGG', 'CGT', 'AGG', 'AGA'],
'TRP': ['TGG'],
'VAL': ['GTA', 'GTC', 'GTG', 'GTT'],
'GLU': ['GAG', 'GAA'],
'TYR': ['TAT', 'TAC']}
EssentialKmerCount = CodonsDict.copy()
NonEssentialKmerCount = CodonsDict.copy()
EssentialGCContent = dict()
NonEssentialGCContent = dict()
class FeatureProcessing(object):
def __init__(self, read):
super(FeatureProcessing, self).__init__()
self.EssentialGeneSeqInfo = read.getEssentialGeneSeqInfo()
self.EssentialProteinSeqInfo = read.getEssentialProteinInfo()
self.EssentialAnnotationInfo = read.getEssentialGeneAnnoInfo()
self.NonEssentialGeneSeqInfo = read.getNonEssentialGeneSeqInfo()
self.NonEssentialProteinSeqInfo = read.getNonEssentialProteinInfo()
self.NonEssentialAnnotationInfo = read.getNonEssentialGeneAnnoInfo()
# MCL info
self.MCLStatDict = read.getMCLStatistics()
self.combinedMCLDict = read.getCombinedMCLInfo()
self.EssentialGCContentFeatDict = dict()
self.EssentialKmerFeatDict = dict()
self.EssentialCAIRCSUFeatDict = dict()
self.EssentialProteinFeatDict = dict()
self.EssentialGeneSeqLenghtFeatDict = dict()
self.maxEssentialGeneLength = 0
self.NonEssentialGCContentFeatDict = dict()
self.NonEssentialKmerFeatDict = dict()
self.NonEssentialCAIRCSUFeatDict = dict()
self.NonEssentialProteinFeatDict = dict()
self.NonEssentialGeneSeqLengthFeatDict = dict()
self.maxNonEssentialGeneLength = 0
# returns a dictionary with essential gene name as key and gene length as values
def getEssentialGeneLengthFeatDict(self):
return self.EssentialGeneSeqLenghtFeatDict
# returns a dictionary with essential gene name as key and binned gc content as values
def getEssentialGCContentFeatDict(self):
return self.EssentialGCContentFeatDict
# returns a dictionary with essential gene name as key and binned kmer freq as values
def getEssentialKmerFeatDict(self):
return self.EssentialKmerFeatDict
# returns a dictionary with essential gene name as key and CAI, MAXRCSU value of the whole sequence
def getEssentialCAIRCSUFeatDict(self):
return self.EssentialCAIRCSUFeatDict
# returns a dictionary with essential gene name as key and a list containing amino acid freq and protein seq length
def getEssentialProteinFeatDict(self):
return self.EssentialProteinFeatDict
# returns the length of the longest essential gene sequence
def getMaxEssentialGeneLength(self):
return self.maxEssentialGeneLength
# returns a dictionary with non essential gene name as key and gene seq length as values
def getNonEssentialGeneLengthFeatDict(self):
return self.NonEssentialGeneSeqLengthFeatDict
# returns a dictionary with non essential gene name as key and binned gc content as values
def getNonEssentialGCContentFeatDict(self):
return self.NonEssentialGCContentFeatDict
# returns a dictionary with non essential gene name as key and binned kmer freq as values
def getNonEssentialKmerFeatDict(self):
return self.NonEssentialKmerFeatDict
# returns a dictionary with non essential gene name as key and CAI, MAXRCSU value of the whole sequence
def getNonEssentialCAIRCSUFeatDict(self):
return self.NonEssentialCAIRCSUFeatDict
# returns a dictionary with non essential gene name as key and a list containing amino acid freq and protein seq length
def getNonEssentialProteinFeatDict(self):
return self.NonEssentialProteinFeatDict
# returns the length of the longest non essential gene sequence
def getMaxNonEssentialGeneLength(self):
return self.maxNonEssentialGeneLength
# return combined MCL dict
def getCombinedMCLDict(self):
return self.combinedMCLDict
# return MCL statistics dict
def getMCLStatDict(self):
return self.MCLStatDict
# this method is the entry point to process all feature
def getFeatures(self, bins):
# process and store features for essential genes
for seqName in self.EssentialGeneSeqInfo:
# find the gene with maximum length
if len(self.EssentialGeneSeqInfo[seqName]) > self.maxEssentialGeneLength:
self.maxEssentialGeneLength = len(self.EssentialGeneSeqInfo[seqName])
# store gene sequence length
self.EssentialGeneSeqLenghtFeatDict[seqName] = len(self.EssentialGeneSeqInfo[seqName])
# get CAI and Max RCSU from the sequence
CAI, RCSUMax = getCAIRCSU(self.EssentialGeneSeqInfo[seqName])
self.EssentialCAIRCSUFeatDict[seqName] = [CAI, RCSUMax]
# get protein seq feature
proteinFeature = getProteinFeat(self.EssentialProteinSeqInfo[seqName])
self.EssentialProteinFeatDict[seqName] = proteinFeature
# split sequence into bin size and get features of each bin
# kmer frequency of each binned sequence
# gc content of each binned sequence
binnedSeqFeat = list()
binnedGCFeat = list()
binnedSeqLength = len(self.EssentialGeneSeqInfo[seqName]) / bins
startPos = 0
endPos = binnedSeqLength
for i in range(0, bins):
if endPos > len(self.EssentialGeneSeqInfo[seqName]):
binnedSequence = self.EssentialGeneSeqInfo[seqName][startPos:]
else:
binnedSequence = self.EssentialGeneSeqInfo[seqName][startPos:endPos]
startPos = endPos
endPos = endPos + binnedSeqLength
binnedKmerFreqList, binnedKmerDict = getKmerCount(binnedSequence, k = 3)
binnedGCContent = getGCContent(binnedSequence)
binnedSeqFeat.extend(binnedKmerFreqList)
binnedGCFeat.append(binnedGCContent)
# store all kmer info in essential kmer dictionary
for kmer in binnedKmerDict:
EssentialKmerCount[kmer] = EssentialKmerCount[kmer] + binnedKmerDict[kmer]
EssentialGCContent[seqName] = binnedGCContent
self.EssentialKmerFeatDict[seqName] = binnedSeqFeat
self.EssentialGCContentFeatDict[seqName] = binnedGCFeat
# process and store features for non essential genes
for seqName in self.NonEssentialGeneSeqInfo:
# find the gene with maximum length
if len(self.NonEssentialGeneSeqInfo[seqName]) > self.maxNonEssentialGeneLength:
self.maxNonEssentialGeneLength = len(self.NonEssentialGeneSeqInfo[seqName])
# store gene sequence length
self.NonEssentialGeneSeqLengthFeatDict[seqName] = len(self.NonEssentialGeneSeqInfo[seqName])
# get CAI and Max RCSU from the sequence
CAI, RCSUMax = getCAIRCSU(self.NonEssentialGeneSeqInfo[seqName])
self.NonEssentialCAIRCSUFeatDict[seqName] = [CAI, RCSUMax]
# get protein seq feature
proteinFeature = getProteinFeat(self.NonEssentialProteinSeqInfo[seqName])
self.NonEssentialProteinFeatDict[seqName] = proteinFeature
# split sequence into bin size and get features of each bin
# kmer frequency of each binned sequence
# gc content of each binned sequence
binnedSeqFeat = list()
binnedGCFeat = list()
binnedSeqLength = len(self.NonEssentialGeneSeqInfo[seqName]) / bins
startPos = 0
endPos = binnedSeqLength
for i in range(0, bins):
if endPos > len(self.NonEssentialGeneSeqInfo[seqName]):
binnedSequence = self.NonEssentialGeneSeqInfo[seqName][startPos:]
else:
binnedSequence = self.NonEssentialGeneSeqInfo[seqName][startPos:endPos]
startPos = endPos
endPos = endPos + binnedSeqLength
binnedKmerFreqList, binnedKmerDict = getKmerCount(binnedSequence, k = 3)
binnedGCContent = getGCContent(binnedSequence)
binnedSeqFeat.extend(binnedKmerFreqList)
binnedGCFeat.append(binnedGCContent)
# store all kmer info in non essential kmer dictionary
for kmer in binnedKmerDict:
NonEssentialKmerCount[kmer] = NonEssentialKmerCount[kmer] + binnedKmerDict[kmer]
NonEssentialGCContent[seqName] = binnedGCContent
self.NonEssentialKmerFeatDict[seqName] = binnedSeqFeat
self.NonEssentialGCContentFeatDict[seqName] = binnedGCFeat
# returns the GC content of a gene sequence
def getGCContent(sequence):
A, T, C, G = 0, 0, 0, 0
for i in range(len(sequence)):
if sequence[i] == 'A':
A += 1
elif sequence[i] == 'T':
T += 1
elif sequence[i] == 'C':
C += 1
elif sequence[i] == 'G':
G += 1
return float(G + C) / float(A + T + C + G)
# returns the codon Index dictionary and RCSU max value. This method is a part of getCAIRCSUMax method
def generateCodonIndexandMaxRCSU(sequence):
codonCountDict = CodonsDict.copy()
codonIndexDict = dict()
for i in range(0, len(sequence), 1):
codon = sequence[i: i + 3]
if codon in codonCountDict:
codonCountDict[codon] += 1
for aminoacid in SynonymousCodons:
total = 0.0
rcsu = []
codons = SynonymousCodons[aminoacid]
for codon in codons:
total += codonCountDict[codon]
for codon in codons:
denom = float(total) / len(codons)
if denom > 0:
rcsu.append(float(codonCountDict[codon]) / denom)
else:
rcsu.append(1.)
rcsuMax = max(rcsu)
for codonIndex, codon in enumerate(codons):
codonIndexDict[codon] = float(rcsu[codonIndex]) / rcsuMax
return codonIndexDict, rcsuMax
# returns the CAI and MAX RCSU value of a sequence
def getCAIRCSU(sequence):
CAIValue, CAILength = 0, 0
codonIndexDict, RCSUMax = generateCodonIndexandMaxRCSU(sequence)
for i in range(0, len(sequence), 3):
codon = sequence[i: i + 3]
if codon in codonIndexDict and codon not in ['ATG', 'TGG']:
CAIValue += math.log(codonIndexDict[codon])
CAILength += 1
return float(math.exp(CAIValue)) / (CAILength - 1.0), RCSUMax
# returns protein sequence related feature, amino acid count and protein length
def getProteinFeat(sequence):
AminoAcidDict = AminoAcidDictionary.copy()
for i in range(0, len(sequence)):
if sequence[i] in AminoAcidDict:
AminoAcidDict[sequence[i]] += 1
proteinFeat = list()
for key in AminoAcidDict:
proteinFeat.append(AminoAcidDict[key])
proteinFeat.append(len(sequence))
return proteinFeat
# returns kmer count of the sequence
def getKmerCount(sequence, k):
string = 'ATCG'
kMerDict = dict()
kmerFreqList = list()
for i in itertools.product(string, repeat=k):
kmer = ''.join(i)
kMerDict[kmer] = 0
# count kmers in the sequence
for i in range(len(sequence) - k):
kmer = sequence[i:(i + k)]
if kmer in kMerDict:
kMerDict[kmer] += 1
# storing kmer counts in a list
for kmer in kMerDict:
kmerFreqList.append(kMerDict[kmer])
return kmerFreqList, kMerDict