-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditionalEntropy.py
367 lines (295 loc) · 10.9 KB
/
conditionalEntropy.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#In this file I calculate the Conditional Entropy per base and per 3bases.
#As well as the SUmmation of the values that I get from the first.
from __future__ import with_statement
import copy
import csv
import gzip
import math
import matplotlib.pyplot as plt
import pdb
import plotly.plotly as py
import numpy as np
np.set_printoptions(threshold='nan')
import scipy
import time
import urllib
from collections import namedtuple, Counter
from copy import deepcopy
from plotly.graph_objs import *
py.sign_in('Eftychia', '2puhmq6aj8')
from pylab import *
from scipy.stats.stats import pearsonr
from sklearn import datasets, linear_model, cross_validation, metrics, clone, gaussian_process, svm, preprocessing
from sklearn.cross_validation import KFold, cross_val_score, StratifiedKFold
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor, ExtraTreesClassifier, AdaBoostClassifier
from sklearn.externals.six.moves import xrange
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import Pipeline
#Import My Own Functions
from myfunctions import parseGFFAttributes, parseGFF3, find_kmers, romanToNumeric, shortenSequence, findDuplcicates, searchIfInColumn, plotWith4Subplots, plotTwoScales
#############################################################################################################################################
actualArrayWithNames = np.load('Ciandrini/actualArrayWithNames.npy')
print "actualArrayWithNames Loaded", actualArrayWithNames.shape
sortedArray = []
initRates= actualArrayWithNames[:,-1].astype(float)
sortedArray = actualArrayWithNames[initRates.argsort()] # Sort the array by InitiationRate
sortedArray = sortedArray[:,1:]
#sortedArray = np.load('Gritsenko/sortedArray.npy')
print "sortedArray loaded, is the actualArrayWithNames Array (without the names) sorted by Init Rates: ", sortedArray.shape
temp = [] # Gather the Lengths (100upstream + 40CDS)
for i in range(len(sortedArray)):
temp.append(len(sortedArray[i][0]))
temp = np.array(temp)
maxLength=temp.max(axis=0)
print "maxLength", maxLength # Find max length
minLength=temp.min(axis=0)
print "minLength", minLength
print ""
'''
arrayEsetA = []
arrayEsetT = []
arrayEsetG = []
arrayEsetC = []
#Pass from every position
for k in range(maxLength):
#print "position", k
tempSequences = []
#Select the sequences that have this length
for s in range(len(sortedArray)):
#Find Sequence's Length
lenseq = len(sortedArray[s][0]) #int(sortedArray[s][2])
#print "lenseq", lenseq
#If position belongs to the sequence select it
if k in range(lenseq):
tempSequences.append(sortedArray[s])
tempSequences = np.array(tempSequences)
#print "tempSequences", tempSequences.shape
#print ""
#Take the 10% of the sequences with the highest Initiation Rate
topTen = int(len(tempSequences)*0.1)
ninety = len(tempSequences) - topTen
# print "topTen",topTen
# print "ninety", ninety
#Calculate the Pset
As = Ts = Gs = Cs = 0
for i in range(topTen):
lenseq = len(tempSequences[i][0])
if tempSequences[i][0][lenseq-(k+1)] == 'A': #walk from right to left
As += 1
elif tempSequences[i][0][lenseq-(k+1)] == 'T':
Ts += 1
elif tempSequences[i][0][lenseq-(k+1)] == 'G':
Gs += 1
elif tempSequences[i][0][lenseq-(k+1)] == 'C':
Cs += 1
else:
print "error top ten"
pSetA = As/float(topTen)
pSetT = Ts/float(topTen)
pSetG = Gs/float(topTen)
pSetC = Cs/float(topTen)
# print "As", As
# print "Ts", Ts
# print "Gs", Gs
# print "Cs", Cs
# print "pSetA",pSetA
# print "pSetT",pSetT
# print "pSetG",pSetG
# print "pSetC",pSetC
#Calculate the Pback
Ab = Tb = Gb = Cb = Nothingb = 0
count = 0
#print "range: ",topTen,len(tempSequences)
#print "ninety", ninety
for z in range(topTen,len(tempSequences)): #now check for the specific position in the rest 90%
lenseq = len(tempSequences[z][0]) #length !!!!PROSOXI!!!!!
if tempSequences[z][0][lenseq-(k+1)] == 'A': #walk from right to left
Ab += 1
elif tempSequences[z][0][lenseq-(k+1)] == 'T':
Tb += 1
elif tempSequences[z][0][lenseq-(k+1)] == 'G':
Gb += 1
elif tempSequences[z][0][lenseq-(k+1)] == 'C':
Cb += 1
else :
print "error ninety"
pBackA = Ab/float(ninety)
pBackT = Tb/float(ninety)
pBackG = Gb/float(ninety)
pBackC = Cb/float(ninety)
if int (pBackA+pBackT+pBackG+pBackC+0.0000000001) != 1:
print "BIG ERROR", pBackA+pBackT+pBackG+pBackC
#Now for each position calculate the Eset of each base
if pBackA != 0 and (pSetA/pBackA) != 0:
esetA = pSetA*math.log(pSetA/pBackA)
else:
esetA = 0
arrayEsetA.append(esetA)
#print "esetA", esetA
if pBackT != 0 and (pSetT/pBackT) != 0:
esetT = pSetT*math.log(pSetT/pBackT)
else:
esetT = 0
arrayEsetT.append(esetT)
if pBackG != 0 and (pSetG/pBackG) != 0:
esetG = pSetG*math.log(pSetG/pBackG)
else:
esetG = 0
arrayEsetG.append(esetG)
if pBackC != 0 and (pSetC/pBackC) != 0:
esetC = pSetC*math.log(pSetC/pBackC)
else:
esetC = 0
arrayEsetC.append(esetC)
arrayEsetA = np.array(arrayEsetA)
arrayEsetT = np.array(arrayEsetT)
arrayEsetG = np.array(arrayEsetG)
arrayEsetC = np.array(arrayEsetC)
print "arrayEsetA", arrayEsetA.shape
print "arrayEsetT", arrayEsetT.shape
print "arrayEsetG", arrayEsetG.shape
print "arrayEsetC", arrayEsetC.shape
arrayEsetA = np.save('Ciandrini/arrayEsetA',arrayEsetA)
arrayEsetT = np.save('Ciandrini/arrayEsetT',arrayEsetT)
arrayEsetG = np.save('Ciandrini/arrayEsetG',arrayEsetG)
arrayEsetC = np.save('Ciandrini/arrayEsetC',arrayEsetC)
# # PLot
# barWidth = np.arange(len(arrayEsetA))
# plotWith4Subplots(4, arrayEsetA, 'A', arrayEsetT, 'T', arrayEsetG, 'G', arrayEsetC, 'C', barWidth, True, 'Conditional Entropy per base', 'Ciandrini/CEperBase.png')
# exit()
'''
'''
########################~~Summations~~########################################
#Now pass from every sequence and swap every base with its eset value
print "Start Summations"
# actualArrayWithNames = np.load('Gritsenko/forCE.npy')
# print "actualArrayWithNames loaded", actualArrayWithNames.shape
arrayEsetA = np.load('Ciandrini/arrayEsetA.npy')
arrayEsetT = np.load('Ciandrini/arrayEsetT.npy')
arrayEsetG = np.load('Ciandrini/arrayEsetG.npy')
arrayEsetC = np.load('Ciandrini/arrayEsetC.npy')
sumEsetSequences = []
for s in range(len(actualArrayWithNames)):
lenseq = len(actualArrayWithNames[s][1]) # Length !!!!PROSOXI!!!!!
#print "lenseq", lenseq
esetSequence = []
for j in range(maxLength):
#if the position is in between of the length of the sequence
if j in range(lenseq):
if actualArrayWithNames[s][1][j] == 'A':
esetSequence.append(arrayEsetA[j])
elif actualArrayWithNames[s][1][j] == 'T':
esetSequence.append(arrayEsetT[j])
elif actualArrayWithNames[s][1][j] == 'G':
esetSequence.append(arrayEsetG[j])
elif actualArrayWithNames[s][1][j] == 'C':
esetSequence.append(arrayEsetC[j])
else:
print "error esetSequence" #esetSequence.append(arrayEsetNothing[j])
Summation = sum(esetSequence)
sumEsetSequences.append(Summation) #Append the Sum of the values of each Sequence-array.
sumEsetSequences = np.array(sumEsetSequences) # is an array that contains the Summation of the Eset values per sequence
print "Summation of the Conditional Entropy values is done: sumEsetSequences=", sumEsetSequences.shape
np.save("Ciandrini/sumEsetSequences", sumEsetSequences)
'''
sumEsetSequences = np.load("Ciandrini/sumEsetSequences.npy")
featureArray = np.load("Ciandrini/featureArray.npy")
print "sumEsetSequences", sumEsetSequences.shape
print "featureArray", featureArray.shape
tempArray = featureArray[:,0:-1]
tempArray = np.vstack((tempArray.T, sumEsetSequences)).T
featureArray = np.vstack((tempArray.T, featureArray[:,-1])).T
print "featureArray", featureArray.shape
print "featureArray", featureArray[0]
np.save('Ciandrini/featureArray', featureArray)
exit()
#print "sumEsetSequences", sumEsetSequences
# print "Done Conditional Entropy in ", time.time() - start_ConditionalEntropy_time
#plot sumEsetSequences against Init Rates
print type(sumEsetSequences[0])
for i in range(len(sumEsetSequences)):
a = sumEsetSequences[i]
print '{0:.10f}'.format(a)
exit()
y1 = sumEsetSequences
y1 ='{0:.10f}'.format(y1)
print y1
exit()
y2 = actualArrayWithNames[:,-1]
x = np.arange(len(y1))
plotTwoScales(x, 'samples', y1, 'Summations', y2, 'InitRates', 'SummedValues from CE', 'Initiation Rates (log)', 'Gritsenko/plotSummations.png')
exit()
########
# Conditional Entropy per 3-mer
dimers,trimers,dimercounts,trimercounts = find_kmers('ATGC')
trimers = np.array(trimers) # 64 possibilities
essetTrimers = []
for k in range(maxLength-3):
tempSequences = []
#Select the sequences that have this length
for s in range(len(sortedArray)):
#Find Sequence's Length
lenseq = int(sortedArray[s][2])
#print "lenseq", lenseq
#If position belongs to the sequence select it
if k in range(lenseq):
tempSequences.append(sortedArray[s])
tempSequences = np.array(tempSequences)
#print "tempSequences", tempSequences.shape
#print ""
#Take the 10% of the sequences with the highest Initiation Rate
topTen = int(len(tempSequences)*0.1)
ninety = len(tempSequences) - topTen
# print "topTen",topTen
# print "ninety", ninety
#Calculate the Pset
trisets = np.zeros(64)
for i in range(topTen):
lenseq = int(tempSequences[i][2])
reverseseq = tempSequences[i][0][::-1]
#print reverseseq
#print lenseq, tempSequences[i][0]
#print reverseseq[k:k+3]
for tri_index in range(64):
if reverseseq[k:k+3] == trimers[tri_index]:
trisets[tri_index]+=1
trisets = trisets / float(topTen)
#print trisets
tribacks = np.zeros(64)
for i in range(topTen,len(tempSequences)):
lenseq = int(tempSequences[i][2])
reverseseq = tempSequences[i][0][::-1]
#print reverseseq
#print lenseq, tempSequences[i][0]
#print reverseseq[k:k+3]
for tri_index in range(64):
if reverseseq[k:k+3] == trimers[tri_index]:
tribacks[tri_index]+=1
tribacks = tribacks / float(ninety)
#print tribacks
tri_essets = np.zeros(64)
for p in range(64):
if tribacks[p] != 0 and (trisets[p]/tribacks[p]) != 0:
tri_essets[p] = trisets[p]*math.log(trisets[p]/tribacks[p])
else:
tri_essets[p] = 0
#print tri_essets
essetTrimers.append(tri_essets)
print len(essetTrimers)
print essetTrimers[0]
essetTrimers = np.array(essetTrimers)
#Plot the codon Conditional Entropies
for t_counter in range(0,64,4):
print t_counter
arrayEsset1 = essetTrimers[:,t_counter]
arrayEsset2 = essetTrimers[:,t_counter+1]
arrayEsset3 = essetTrimers[:,t_counter+2]
arrayEsset4 = essetTrimers[:,t_counter+3]
print "LENGTHS:",len(arrayEsset1),len(arrayEsset2),len(arrayEsset3),len(arrayEsset4)
essetname1 = trimers[t_counter]
essetname2 = trimers[t_counter+1]
essetname3 = trimers[t_counter+2]
essetname4 = trimers[t_counter+3]
plotWith4Subplots(4, arrayEsset1, essetname1, arrayEsset2, essetname2, arrayEsset3, essetname3, arrayEsset4, essetname4, np.arange(len(arrayEsset1)), True, 'Conditional Entropy per Codon', 'Gritsenko/CEperAminoAcid'+`t_counter`+'.png')