-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpreprocessing.py
276 lines (238 loc) · 16.3 KB
/
preprocessing.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
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
class Preprocessing:
def __init__(self,miRNACancer, miRNACancerlength, miRNANormal, miRNANormallength, geneCancer, geneNormal, tumor):
self.miRNACancer=miRNACancer
self.miRNACancerlength=miRNACancerlength
self.miRNANormal=miRNANormal
self.miRNANormallength=miRNANormallength
self.geneCancer=geneCancer
self.geneNormal=geneNormal
self.tumor=tumor
self.fix_matrices()
self.geneCancer, self.geneNormal= self.filter_genes()
self.filter_noise()
self.Normal, self.Cancer, self.Indices=self.matrices()
#It allows to have matrices that contains fpkm with ensembl as index and patients in columns
def fix_matrices(self):
#Remove indices not necessary
self.miRNACancer.index=self.miRNACancer.index.droplevel(0)
self.miRNANormal.index=self.miRNANormal.index.droplevel(0)
self.miRNANormallength.index=self.miRNANormallength.index.droplevel(0)
self.miRNACancerlength.index=self.miRNACancerlength.index.droplevel(0)
self.miRNANormal.columns=self.miRNANormal.columns.droplevel(0)
self.miRNACancer.columns=self.miRNACancer.columns.droplevel(0)
self.miRNANormal.columns.name=None
self.miRNACancer.columns.name=None
self.geneNormal.columns=self.geneNormal.columns.droplevel(0)
self.geneCancer.columns=self.geneCancer.columns.droplevel(0)
self.geneNormal.columns.name=None
self.geneCancer.columns.name=None
print('geneNorm ' + str(self.geneNormal.shape))
print('geneCanc ' + str(self.geneCancer.shape))
l=len(self.miRNANormal)
#Transform rpm into fpkm
for i in range (0, l-1):
self.miRNANormal.get_values()[i]=(self.miRNANormal.get_values()[i]*(10**3))/(2*abs(self.miRNANormallength.get_values()[i,1]-self.miRNANormallength.get_values()[i,0]))
self.miRNACancer.get_values()[i]=(self.miRNACancer.get_values()[i]*(10**3))/(2*abs(self.miRNACancerlength.get_values()[i,1]-self.miRNACancerlength.get_values()[i,0]))
self.miRNANormal.index.name='ensembl'
self.miRNACancer.index.name='ensembl'
#Remove genes that are not protein coding, long non coding or mirna.
#Remove also duplicated genes
def filter_genes(self):
matrix_GTypeGName= pd.read_excel('matrix_GTypeGName.xls')
symbolAndEnsembl=pd.DataFrame(self.geneNormal.index.get_level_values(0), self.geneNormal.index.get_level_values(1))
self.geneCancer['ensembl']=self.geneCancer.index.get_level_values(0)
self.geneNormal['ensembl']=self.geneNormal.index.get_level_values(0)
GeneCancerTypes=pd.merge(self.geneCancer,matrix_GTypeGName, left_on="gene_symbol", right_on="Gene name")
GeneNormalTypes=pd.merge(self.geneNormal,matrix_GTypeGName, left_on="gene_symbol", right_on="Gene name")
GeneCancerTypes.index=GeneCancerTypes['ensembl']
GeneNormalTypes.index=GeneNormalTypes['ensembl']
GeneCancerTypes=GeneCancerTypes.drop_duplicates()
GeneNormalTypes=GeneNormalTypes.drop_duplicates()
#Remove all gene types that are not interesting
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='TR_J_gene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='polymorphic_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='unitary_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='IG_D_gene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='scaRNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='TR_V_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='sense_overlapping')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='rRNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='IG_J_gene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='IG_C_gene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='bidirectional_promoter_lncRNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='IG_C_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='3prime_overlapping_ncRNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='TR_C_gene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='IG_J_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='ribozyme')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='TR_J_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='TR_D_gene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='Mt_rRNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='antisense')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='misc_RNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='rRNA_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='transcribed_unprocessed_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='processed_transcript')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='transcribed_processed_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='IG_V_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='sense_intronic')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='IG_V_gene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='TR_V_gene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='TEC')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='transcribed_unitary_pseudogene')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='vaultRNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='macro_lncRNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='scRNA')]
GeneCancerTypes=GeneCancerTypes[(GeneCancerTypes['Gene type']!='non_coding')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='TR_J_gene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='polymorphic_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='unitary_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='IG_D_gene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='scaRNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='TR_V_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='sense_overlapping')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='rRNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='IG_J_gene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='IG_C_gene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='bidirectional_promoter_lncRNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='IG_C_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='3prime_overlapping_ncRNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='TR_C_gene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='IG_J_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='ribozyme')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='TR_J_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='TR_D_gene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='Mt_rRNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='antisense')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='misc_RNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='rRNA_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='transcribed_unprocessed_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='processed_transcript')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='transcribed_processed_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='IG_V_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='sense_intronic')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='IG_V_gene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='TR_V_gene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='TEC')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='transcribed_unitary_pseudogene')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='vaultRNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='macro_lncRNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='scRNA')]
GeneNormalTypes=GeneNormalTypes[(GeneNormalTypes['Gene type']!='non_coding')]
#Remove the shortest gene since data are random
GeneCancer_noMiRNA=GeneCancerTypes[(GeneCancerTypes['Gene type']!='miRNA')]
GeneNormal_noMiRNA=GeneNormalTypes[(GeneNormalTypes['Gene type']!='miRNA')]
GeneCancer_noSnRNA=GeneCancer_noMiRNA[(GeneCancer_noMiRNA['Gene type']!='snRNA')]
GeneNormal_noSnRNA=GeneNormal_noMiRNA[(GeneNormal_noMiRNA['Gene type']!='snRNA')]
GeneCancer_noSnoRNA=GeneCancer_noSnRNA[(GeneCancer_noSnRNA['Gene type']!='snoRNA')]
GeneNormal_noSnoRNA=GeneNormal_noSnRNA[(GeneNormal_noSnRNA['Gene type']!='snoRNA')]
GeneCancer_noTRNA=GeneCancer_noSnoRNA[(GeneCancer_noSnoRNA['Gene type']!='Mt_tRNA')]
GeneNormal_noTRNA=GeneNormal_noSnoRNA[(GeneNormal_noSnoRNA['Gene type']!='Mt_tRNA')]
#Drop the type, keeping only fpkm and as index gene symbol
GeneCancerLong=GeneCancer_noTRNA.drop(['Gene type','Gene name', 'ensembl'], axis=1)
GeneNormalLong=GeneNormal_noTRNA.drop(['Gene type','Gene name', 'ensembl'], axis=1)
GeneCancerLong.index.name='ensembl'
GeneNormalLong.index.name='ensembl'
indexDuplicated_cancer=GeneCancerLong[GeneCancerLong.index.duplicated()].index
indexDuplicated_normal=GeneNormalLong[GeneNormalLong.index.duplicated()].index
indexDuplicated=indexDuplicated_cancer.intersection(indexDuplicated_normal)
noDuplCancer=GeneCancerLong.drop(indexDuplicated)
noDuplNormal=GeneNormalLong.drop(indexDuplicated)
DuplCancer=GeneCancerLong.drop(noDuplCancer.index)
DuplNormal=GeneNormalLong.drop(noDuplNormal.index)
geneCancer_noDupl=noDuplCancer.append(DuplCancer.drop_duplicates())
geneNormal_noDupl=noDuplNormal.append(DuplNormal.drop_duplicates())
diff=geneCancer_noDupl.index.difference(geneNormal_noDupl.index)
geneCancer_noDupl=geneCancer_noDupl.drop(diff, axis=0 )
diff_norm=geneNormal_noDupl.index.difference(geneCancer_noDupl.index)
geneNormal_noDupl=geneNormal_noDupl.drop(diff_norm, axis=0 )
print('normal ' + str(geneNormal_noDupl.shape))
print('cancer ' + str(geneCancer_noDupl.shape))
return geneCancer_noDupl, geneNormal_noDupl
#Remove noisy genes using z-score of fpkm
def filter_noise(self):
#Calculate the mean of each gene
miRNANormal_mean=self.miRNANormal.mean(axis=1)
miRNACancer_mean=self.miRNACancer.mean(axis=1)
geneNormal_mean=self.geneNormal.mean(axis=1)
geneCancer_mean=self.geneCancer.mean(axis=1)
#Extract indices of all the genes/miRNA that have mean equal to 0
miRNANormalIndex_null=miRNANormal_mean[miRNANormal_mean.get_values()==0].index
miRNACancerIndex_null=miRNACancer_mean[miRNACancer_mean.get_values()==0].index
geneNormalIndex_null=geneNormal_mean[geneNormal_mean.get_values()==0].index
geneCancerIndex_null=geneCancer_mean[geneCancer_mean.get_values()==0].index
#Find only those that are in both cancer and normal equal to 0
miRNA_null=miRNANormalIndex_null.intersection(miRNACancerIndex_null)
gene_null=geneNormalIndex_null.intersection(geneCancerIndex_null)
#Drop them
self.miRNANormal=self.miRNANormal.drop(miRNA_null, axis=0)
self.miRNACancer=self.miRNACancer.drop(miRNA_null, axis=0)
self.geneNormal=self.geneNormal.drop(gene_null, axis=0)
self.geneCancer=self.geneCancer.drop(gene_null, axis=0)
#Recalculate the mean
geneNormal_mean=self.geneNormal.mean(axis=1)
geneCancer_mean=self.geneCancer.mean(axis=1)
#Calculate log2
geneNormal_log2=np.log2(self.geneNormal)
geneCancer_log2=np.log2(self.geneCancer)
#Calculate the standard deviation
geneNormal_std=self.geneNormal.std(axis=1)
geneCancer_std=self.geneCancer.std(axis=1)
#Calculate zfpkm
geneNormal_zfpkm=(geneNormal_log2.sub(geneNormal_mean, axis=0)).divide(geneNormal_std, axis=0)
geneCancer_zfpkm=(geneCancer_log2.sub(geneCancer_mean, axis=0)).divide(geneCancer_std, axis=0)
#Calculate the mean for each gene
geneNormal_mean_zfpkm=geneNormal_zfpkm.mean(axis=1)
geneCancer_mean_zfpkm=geneCancer_zfpkm.mean(axis=1)
geneNormalIndex_less=geneNormal_mean_zfpkm[geneNormal_mean_zfpkm.get_values()<(-3)].index
geneCancerIndex_less=geneCancer_mean_zfpkm[geneCancer_mean_zfpkm.get_values()<(-3)].index
gene_less=geneNormalIndex_less.intersection(geneCancerIndex_less)
#Drop them
self.geneNormal=self.geneNormal.drop(gene_less, axis=0)
self.geneCancer=self.geneCancer.drop(gene_less, axis=0)
#Retrieve common patients between genes and mirnas
def common_patients(self):
#Find common patients for cancer and common for normal
a=pd.DataFrame(self.miRNACancer.columns)
b=pd.DataFrame(self.geneCancer.columns)
Cancerpatients=pd.merge(a,b)
c=pd.DataFrame(self.miRNANormal.columns)
d=pd.DataFrame(self.geneNormal.columns)
Normalpatients=pd.merge(c,d)
#Remove patients differents
MiRNACancerDiff=self.miRNACancer.columns.difference(Cancerpatients[0])
MiRNACancerCommon=self.miRNACancer.columns.difference(self.miRNACancer[MiRNACancerDiff].columns)
self.miRNACancer=self.miRNACancer[MiRNACancerCommon]
MiRNANormalDiff=self.miRNANormal.columns.difference(Normalpatients[0])
MiRNANormalCommon=self.miRNANormal.columns.difference(self.miRNANormal[MiRNANormalDiff].columns)
self.miRNANormal=self.miRNANormal[MiRNANormalCommon]
GeneCancerDiff=self.geneCancer.columns.difference(Cancerpatients[0])
GeneCancerCommon=self.geneCancer.columns.difference(self.geneCancer[GeneCancerDiff].columns)
self.geneCancer=self.geneCancer[GeneCancerCommon]
GeneNormalDiff=self.geneNormal.columns.difference(Normalpatients[0])
GeneNormalCommon=self.geneNormal.columns.difference(self.geneNormal[GeneNormalDiff].columns)
self.geneNormal=self.geneNormal[GeneNormalCommon]
#It create the two matrices and it saves the indices with the respective ensembl
def matrices(self):
#Create the two matrices
Normal=self.geneNormal.append(self.miRNANormal)
Cancer=self.miRNACancer.append(self.geneCancer)
#Order the index
Normal=Normal.sort_index()
Cancer=Cancer.sort_index()
#Cancer_stage=Cancer_stage.sort_index()
#Fill nan with 0
Normal=Normal.fillna(0)
Cancer=Cancer.fillna(0)
Normal.to_csv('./Matrices/normal_'+str(self.tumor),sep=';')
Cancer.to_csv('./Matrices/cancer_'+str(self.tumor),sep=';')
#Save indices
Indices=pd.DataFrame(np.arange(0,len(Normal)), Normal.index)
Indices.to_csv('./Matrices/indices_'+str(self.tumor), sep=';')
return Normal, Cancer, Indices