forked from Bonidia/MathFeature
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChaosGameTheory.py
346 lines (316 loc) · 12.7 KB
/
ChaosGameTheory.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
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
346
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import numpy as np
from Bio import SeqIO
from itertools import product
from scipy.fftpack import fft, ifft
import warnings
import sys
import scipy.stats
import statistics
import os
import collections
warnings.filterwarnings("ignore")
#############################################################################
#############################################################################
def check_files():
for name, label in dataset_labels.items():
if os.path.exists(name):
print("Dataset %s: Found File" % (name))
run = 1
else:
print("Dataset %s: File not exists" % (name))
run = 0
break
return run
def sequence_length():
dlength = []
for name, label in dataset_labels.items():
length = 0
for seq_record in SeqIO.parse(name, "fasta"):
seq = seq_record.seq
if len(seq) > length:
length = len(seq)
dlength.append(length)
# max_length = max(dlength)
return max(dlength)
def file_record(name_seq, mapping, label):
dataset = open(foutput, 'a')
dataset.write("%s," % (str(name_seq)))
for map in mapping:
dataset.write("%s," % (map))
# dataset.write("{0:.4f},".format(metric))
dataset.write(label)
dataset.write("\n")
print("Recorded Sequence: %s" % (name_seq))
return
def chunksTwo(seq, win):
seqlen = len(seq)
for i in range(seqlen):
j = seqlen if i+win>seqlen else i+win
yield seq[i:j]
if j==seqlen: break
return
def frequency_chaos(k):
for finput, label in dataset_labels.items():
for seq_record in SeqIO.parse(finput, "fasta"):
seq = seq_record.seq
seq = seq.upper()
name_seq = seq_record.name
mapping = []
kmer = {}
totalWindows = (len(seq) - k) + 1 # (L - k + 1)
for subseq in chunksTwo(seq, k):
# print(subseq)
if subseq in kmer:
kmer[subseq] = kmer[subseq] + 1
else:
kmer[subseq] = 1
for subseq in chunksTwo(seq, k):
# print(kmer[subseq])
# print(kmer[subseq]/totalWindows)
mapping.append(kmer[subseq]/totalWindows)
padding = ((max_length - k + 1) - len(mapping))
mapping = np.pad(mapping, (0, padding), 'constant')
file_record(name_seq, mapping, label)
return
def classifical_chaos():
for finput, label in dataset_labels.items():
for seq_record in SeqIO.parse(finput, "fasta"):
seq = seq_record.seq
seq = seq.upper()
name_seq = seq_record.name
Sx = []
Sy = []
for nucle in seq:
if nucle == "A":
Sx.append(1)
Sy.append(1)
elif nucle == "C":
Sx.append(-1)
Sy.append(-1)
elif nucle == "T" or nucle =="U":
Sx.append(-1)
Sy.append(1)
else:
Sx.append(1)
Sy.append(-1)
CGR_x = []
CGR_y = []
for i in range(0, len(Sx)):
if i == 0:
CGR_x.append(0.5 * Sx[i])
CGR_y.append(0.5 * Sy[i])
else:
CGR_x.append(0.5 * Sx[i] + 0.5 * CGR_x[i - 1])
CGR_y.append(0.5 * Sy[i] + 0.5 * CGR_y[i - 1])
concat = CGR_x + CGR_y
padding = (max_length - len(Sx)) * 2
mapping = np.pad(concat, (0, padding), 'constant')
file_record(name_seq, mapping, label)
return
#############################################################################
#############################################################################
def header_fourier():
dataset = open(foutput, 'a')
dataset.write("nameseq,average,median,maximum,minimum,peak,"
+ "none_levated_peak,sample_standard_deviation,population_standard_deviation,"
+ "percentile15,percentile25,percentile50,percentile75,amplitude,"
+ "variance,interquartile_range,semi_interquartile_range,"
+ "coefficient_of_variation,skewness,kurtosis,label")
dataset.write("\n")
return
def file_record_fourier(features, name_seq, label_dataset):
dataset = open(foutput, 'a')
dataset.write("%s," % (str(name_seq)))
for metric in features:
dataset.write("%s," % (metric))
# dataset.write("{0:.4f},".format(metric))
dataset.write(label_dataset)
dataset.write("\n")
print("Recorded Sequence: %s" % (name_seq))
return
def feature_extraction(features, spectrum, spectrumTwo):
average = sum(spectrum)/len(spectrum)
features.append(average)
###################################
median = np.median(spectrum)
features.append(median)
###################################
maximum = np.max(spectrum)
features.append(maximum)
###################################
minimum = np.min(spectrum)
features.append(minimum)
###################################
peak = (len(spectrum)/3)/(average)
features.append(peak)
###################################
peak_two = (len(spectrumTwo)/3)/(np.mean(spectrumTwo))
features.append(peak_two)
###################################
standard_deviation = np.std(spectrum) # standard deviation
features.append(standard_deviation)
###################################
standard_deviation_pop = statistics.stdev(spectrum) # population sample standard deviation
features.append(standard_deviation_pop)
###################################
percentile15 = np.percentile(spectrum, 15)
features.append(percentile15)
###################################
percentile25 = np.percentile(spectrum, 25)
features.append(percentile25)
###################################
percentile50 = np.percentile(spectrum, 50)
features.append(percentile50)
###################################
percentile75 = np.percentile(spectrum, 75)
features.append(percentile75)
###################################
amplitude = maximum - minimum
features.append(amplitude)
###################################
# mode = statistics.mode(spectrum)
###################################
variance = statistics.variance(spectrum)
features.append(variance)
###################################
interquartile_range = np.percentile(spectrum, 75) - np.percentile(spectrum, 25)
features.append(interquartile_range)
###################################
semi_interquartile_range = (np.percentile(spectrum, 75) - np.percentile(spectrum, 25))/2
features.append(semi_interquartile_range)
###################################
coefficient_of_variation = standard_deviation/average
features.append(coefficient_of_variation)
###################################
skewness = (3 * (average - median))/standard_deviation
features.append(skewness)
###################################
kurtosis = (np.percentile(spectrum, 75) - np.percentile(spectrum, 25)) / (2 * (np.percentile(spectrum, 90) - np.percentile(spectrum, 10)))
features.append(kurtosis)
###################################
return
def classifical_chaos_fourier():
header_fourier()
for finput, label in dataset_labels.items():
for seq_record in SeqIO.parse(finput, "fasta"):
seq = seq_record.seq
seq = seq.upper()
name_seq = seq_record.name
features = []
spectrum = []
spectrumTwo = []
Sx = []
Sy = []
for nucle in seq:
if nucle == "A":
Sx.append(1)
Sy.append(1)
elif nucle == "C":
Sx.append(-1)
Sy.append(-1)
elif nucle == "T" or nucle =="U":
Sx.append(-1)
Sy.append(1)
else:
Sx.append(1)
Sy.append(-1)
CGR_x = []
CGR_y = []
for i in range(0, len(Sx)):
if i == 0:
CGR_x.append(0.5 * Sx[i])
CGR_y.append(0.5 * Sy[i])
else:
CGR_x.append(0.5 * Sx[i] + 0.5 * CGR_x[i - 1])
CGR_y.append(0.5 * Sy[i] + 0.5 * CGR_y[i - 1])
Fx = fft(CGR_x)
Fy = fft(CGR_y)
for i in range(len(Fx)):
specTotal = (abs(Fx[i])**2) + (abs(Fy[i])**2)
specTwo = (abs(Fx[i])) + (abs(Fy[i]))
spectrum.append(specTotal)
spectrumTwo.append(specTwo)
feature_extraction(features, spectrum, spectrumTwo)
file_record_fourier(features, name_seq, label)
return
def frequency_chaos_fourier(k):
header_fourier()
for finput, label in dataset_labels.items():
for seq_record in SeqIO.parse(finput, "fasta"):
seq = seq_record.seq
seq = seq.upper()
name_seq = seq_record.name
features = []
spectrum = []
spectrumTwo = []
mapping = []
kmer = {}
totalWindows = (len(seq) - k) + 1 # (L - k + 1)
for subseq in chunksTwo(seq, k):
# print(subseq)
if subseq in kmer:
kmer[subseq] = kmer[subseq] + 1
else:
kmer[subseq] = 1
for subseq in chunksTwo(seq, k):
# print(kmer[subseq])
# print(kmer[subseq]/totalWindows)
mapping.append(kmer[subseq]/totalWindows)
Fmap = fft(mapping)
for i in range(len(mapping)):
specTotal = (abs(Fmap[i])**2)
specTwo = (abs(Fmap[i]))
spectrum.append(specTotal)
spectrumTwo.append(specTwo)
feature_extraction(features, spectrum, spectrumTwo)
file_record_fourier(features, name_seq, label)
return
#############################################################################
#############################################################################
if __name__ == "__main__":
print("\n")
print("###################################################################################")
print("########## Feature Extraction: Chaos Game Theory ###########")
print("########## Arguments: -i number of datasets -o output -r representation #########")
print("########## -r: 1 = Classifical Chaos Game Representation #########")
print("########## -r: 2 = Frequency Chaos Game Representation #########")
print("########## -r: 3 = Chaos Game Signal with Classifical Representation #######")
print("########## -r: 4 = Chaos Game Signal with Frequency #########")
print("########## Author: Robson Parmezan Bonidia ###########")
print("###################################################################################")
print("\n")
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--number', help='Fasta format file | Number of dataset or labels')
parser.add_argument('-o', '--output', help='Csv format file | E.g., train.csv')
parser.add_argument('-r', '--approach', help='1 = Classifical Chaos Game Representation, 2 = Frequency Chaos Game Representation, 3 = Chaos Game Signal with Classifical Representation, 4 = Chaos Game Signal with Frequency')
args = parser.parse_args()
n = int(args.number)
foutput = str(args.output)
representation = int(args.approach)
dataset_labels = {}
for i in range(1, n + 1):
name = input("Dataset %s: " % (i))
label = input("Label for %s: " % (name))
print("\n")
dataset_labels[name] = label
if check_files() == 1:
max_length = sequence_length()
if representation == 1:
classifical_chaos()
elif representation == 2:
k = int(input("Frequency of k-mer (e.g., 3, 4): "))
frequency_chaos(k)
elif representation == 3:
classifical_chaos_fourier()
elif representation == 4:
k = int(input("Frequency of k-mer (e.g., 3, 4): "))
frequency_chaos_fourier(k)
else:
print("This package does not contain this approach - Check parameter -r")
else:
print("Some file not exists")
#############################################################################
#############################################################################