-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneLevelOutDet.py
333 lines (297 loc) · 11 KB
/
GeneLevelOutDet.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
import math
import numpy as np
from scipy import stats
from math import sqrt
from math import exp
import scipy.stats as stats
class GeneLevelOutDet():
'''Gene level outlier detection algorithms
are applied'''
########### Modified Z-Score ###########
#Calculating the MAD #
def mad(self,gene):
gene = np.ma.array(gene[1:]).compressed() # should be faster to not use masked arrays.
med = np.median(gene)
#print("mad",np.median(np.abs(gene - med)))
return np.median(np.abs(gene - med))
# Calculating median #
def median(self,gene):
sortedgene = sorted(gene[1:])
if len(sortedgene) % 2 == 0:
n = len(sortedgene)//2
#print ("median",sortedgene[n]+sortedgene[n-1]/2)
return (sortedgene[n]+sortedgene[n-1])/2
else:
#print("median:",sortedgene[len(sortedgene)//2])
return sortedgene[len(sortedgene)//2]
#Main Modified Z-Score calculation for gene #
def modified_z_score(self,gene,threshold=3.5):
z_c=[]
medianvalue=self.median(gene)
madvalue=self.mad(gene)
for i in gene[1:]:
mzscore= abs(0.6745*(i-medianvalue)/madvalue)
if mzscore>threshold :
z_c.append(i)
#print(len(z_c))
else:
pass
#print("no outlier detected",i)
#print(mzscore>threshold)
return z_c
#Checking whether gene is outlier #
def outlier_detection_modified_z_score(self,gene,thresholdGeneLevel):
z_c=self.modified_z_score(gene,threshold=3.5)
#print(len(z_c))
count=len(gene[1:])-1
if len(z_c) > float(thresholdGeneLevel):
#print('gene is outlier')
#print [gene[0]] + z_c
return [gene[0]] + z_c
else:
pass
#print('gene is not outlier')
return
######### GESD Algorithm ########
def main_gesd(self,gene,thresholdGeneLevel):
n=len(gene[1:])
diff=[]
final_outlier_gesd=[]
for k in range(0, int(n/2)+1):
i=k+1
mean=float(sum(gene[1:])/len(gene[1:])) # mean calculation
sd=sqrt(sum((x-mean)**2 for x in gene[1:])/(len(gene[1:])-1)) # standard deviation
alpha=0.05
p=1-alpha/(2*(n-i+1)) # calculating p-value
t=stats.t.ppf(p,n-i-1)
cv=t*(n-i)/sqrt((n-i-1+t**2)*(n-i+1)) #calculating r critical calues
po=[]
for j in gene[1:]:
if sd==0:
a=0
po.append(a)
else:
R=(abs(float(j-mean)/sd)) # calculating r test for each expression values
po.append(R)
Rmax=max(po)
gene_index=po.index(max(po))
final_outlier_gesd.append(gene[gene_index+1])
difference=Rmax-cv
diff.append(difference)
#print(i,'=>',Rmax)
index=po.index(Rmax)
del gene[int(index)+1] # removing the value that maximize the R test
#print(gene)
final=max(diff)
indx=diff.index(final)
if final > 0:
b=5
#print('there are ',int(indx+1),'outliers in the given data sets')
if int(indx+1)>float(thresholdGeneLevel):
#print('gene is outlier')
#print gene[0]
return [gene[0]] + final_outlier_gesd[0:indx+1]
else:
pass
#print('gene is not outlier')
else:
pass
#print('there are no outliers')
return
########### Adjusted Box Plot ##############
#Calculating median#
def median_abp(self,gene):
sortedgene = sorted(gene[1:])
if len(sortedgene) % 2 == 0:
n = len(sortedgene)//2
#print ("median",(sortedgene[n]+sortedgene[n-1])/2.0)
return (sortedgene[n]+sortedgene[n-1])/2
else:
#print("median:",sortedgene[len(sortedgene)//2])
return sortedgene[len(sortedgene)//2]
#Calculating the lower quartile of box plot #
def lower_q1(self,gene):
sortedgene=sorted(gene[1:])
mdn=self.median_abp(gene)
nr=(len(gene[1:])-1)*0.25
i=int(nr)
f=nr-int(nr)
if f==0:
#print ('q1:',sortedgene[i])
return sortedgene[i]
else:
#print('q1:',sortedgene[i] + f*(sortedgene[i+1]-sortedgene[i]))
return sortedgene[i] + f*(sortedgene[i+1]-sortedgene[i])
# Calculating the upper quartile #
def upper_q3(self,gene):
sortedgene=sorted(gene[1:])
mdn=self.median_abp(gene)
nr=(len(gene[1:])-1)*0.75
i=int(nr)
f=nr-int(nr)
if f==0:
#print ('q1:',sortedgene[i])
return sortedgene[i]
else:
#print('q1:',sortedgene[i] + f*(sortedgene[i+1]-sortedgene[i]))
return sortedgene[i] + f*(sortedgene[i+1]-sortedgene[i])
# Calculating the inter quartile range #
def iqr(self,gene):
sortedgene=sorted(gene[1:])
mdn=self.median_abp(gene)
Q1=self.lower_q1(gene)
Q3=self.upper_q3(gene)
IQR=float(Q3-Q1)
#print("IQR",IQR)
return IQR
#Calculating the medcouple for skewness #
def medcouple(self,gene):
sortedgene=sorted(gene[1:])
mdn=self.median_abp(gene)
mdcouple=[]
if len(sortedgene) %2 ==1:
for j in sortedgene[len(sortedgene)//2:]:
for i in sortedgene[0:len(sortedgene)//2]:
if i==j:
pass
else:
mc=((j-mdn)-(mdn-i))/(j-i)
mdcouple.append(mc)
else:
for j in sortedgene[len(sortedgene)/2:]:
for i in sortedgene[0:len(sortedgene)/2]:
if i==j:
pass
else:
mc=((j-mdn)-(mdn-i))/(j-i)
mdcouple.append(mc)
#print('mdcouple',mdcouple)
return(mdcouple)
#Calculating the median of medcouple #
def medcouplemedian(self,gene):
mdcouple=self.medcouple(gene)
sortedmdcouple = sorted(mdcouple)
if len(sortedmdcouple) % 2 == 0:
n = len(sortedmdcouple)//2
#print ("median",(sortedmdcouple[n]+sortedmdcouple[n-1])/2.0)
return (sortedmdcouple[n]+sortedmdcouple[n-1])/2.0
else:
#print("median:",sortedmdcouple[len(sortedmdcouple)//2])
return sortedmdcouple[len(sortedmdcouple)//2]
#Calculating the lower and upper fences for adjusted box plot#
def lower_upperFence(self,gene):
Q1=self.lower_q1(gene)
Q3=self.upper_q3(gene)
IQR=self.iqr(gene)
MC=self.medcouplemedian(gene)
#print(MC)
if MC > 0:
lower=(Q1 - 1.5*exp((-3.5)*(MC))*IQR)
upper=(Q3 + 1.5*exp((4*MC))*IQR)
#print 'lower:',lower,'upper:',upper
return lower,upper
elif MC < 0:
lower=(Q1 - 1.5*exp((-4.0)*(MC))*IQR)
upper=(Q3 + 1.5*exp((3.5)*(MC))*IQR)
#print 'lower:',lower,'upper:',upper
return lower,upper
else:
lower=(Q1-1.5*IQR)
upper=(Q3+1.5*IQR)
return lower,upper
#Outlier detection in adjusted box plot for each gene expression value #
def outlier_detection_adjusted_box_plot(self,gene,thresholdGeneLevel):
values=self.lower_upperFence(gene)
lowervalue,uppervalue=values[0],values[1]
outlierlist=[]
for i in gene[1:]:
if i<lowervalue or i>uppervalue:
outlierlist.append(i)
else:
continue
#print(outlierlist)
if len(outlierlist)>float(thresholdGeneLevel):
#print'gene is outlier'
return [gene[0]] + outlierlist
else:
pass
#print'gene is not outlier'
return
########## Median Rule ###########
# Calculating the intervals for median rule #
# interquartile range and median are taken from adjusted box plot #
def median_rule(self,gene):
IqR=self.iqr(gene)
mdn=self.median(gene)
C1=mdn - 2.3 *IqR
C2=mdn + 2.3 *IqR
return C1,C2
#Checking for outliers #
def median_rule_outlier_det(self,gene,thresholdGeneLevel):
lower_interval,upper_interval=self.median_rule(gene)
outlierlist_median_rule=[]
for i in gene[1:]:
if i<lower_interval or i>upper_interval:
outlierlist_median_rule.append(i)
else:
continue
#print(outlierlist)
if len(outlierlist_median_rule)>float(thresholdGeneLevel):
#print 'outlier'
#print [gene[0]] +outlierlist_median_rule
return [gene[0]] +outlierlist_median_rule
else:
pass
#print'gene is not outlier'
return
######### Common Outliers for Adjusted Box plot and Median Rule #######
def common_outlier(self,list1,list2,gene):
common_list=[]
for sublist in list1:
for sublist2 in list2:
if sublist[0]==sublist2[0]:
commonOutlierss=len(set(sublist) & set(sublist2))-1
if commonOutlierss > len(gene[1:])*2.0/100:
if sublist2[0] in common_list:
pass
else:
common_list.append(sublist2[0])
else:
pass
else:
pass
#print len(common_list)
return common_list
########### Combining Outlier genes for Generalized ESD and Modified Z-Score
def singleListforMzsGesd(self,lst1,lst2):
commonMzsGesd_list=[]
for i in range (0,len(lst1)):
if lst1[i] in commonMzsGesd_list:
pass
else:
commonMzsGesd_list.append(lst1[i])
for k in range(0,len(lst2)):
if lst1[i][0]==lst2[k][0]:
if lst2[k] in commonMzsGesd_list:
pass
else:
if lst2[k] in commonMzsGesd_list:
pass
else:
commonMzsGesd_list.append(lst2[k])
#print len(commonMzsGesd_list)
return commonMzsGesd_list
################# Deleting gene level outliers #############
def deleting_outliers(self,data,genes):
deleting_index=[]
for gene in genes:
for k in range(0,len(data)):
if gene==data[k][0]:
deleting_index.append(k)
else:
pass
for index in sorted(deleting_index,reverse=True):
del data[index]
#print len(data)
return data
GeneLevel=GeneLevelOutDet()