forked from albertwcheng/albert-bioinformatics-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRACorrelation.py
executable file
·412 lines (314 loc) · 8.87 KB
/
RACorrelation.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python
'''
Copyright 2010 Wu Albert Cheng <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
#uses Liao and Zhang, 2006 RA approach
from albertcommon import *
from sys import *
from getopt import getopt
from math import *
from Bio.Cluster.cluster import *
from Bio.Cluster import *
from numpy import std,mean,median,arcsinh
from quantileNormalize import quantileNormalizeMInPlace
#def median(x):
# x2=x[:]
# x2.sort()
# L=len(x2)
# if L%2==0:
# return (float(x2[L/2-1])+x2[L/2])/2
# else:
# return x2[L/2]
#species groupings [ [col,..] , ...] G
#data matrix [ [ S(i,j) ... ] ] M
def transformToRAValues(M,G,startRow):
Mp=M[:] #just make a copy first, then replace their values
for i in range(startRow,len(M)):
row=M[i]
for group in G:
subv=getSubvector(row,group)
sumS=sum(subv)
for j in group:
Mp[i][j]=float(row[j])/sumS
return Mp
def transformToRAasinhValues(M,G,startRow):
Mp=M[:] #just make a copy first, then replace their values
for i in range(startRow,len(M)):
row=M[i]
for group in G:
subv=getSubvector(row,group)
sumS=sum(subv)
for j in group:
Mp[i][j]=arcsinh(float(row[j])/sumS)
return Mp
def transformToRAMSasinhValues(M,G,startRow):
Mp=M[:] #just make a copy first, then replace their values
for i in range(startRow,len(M)):
row=M[i]
for group in G:
subv=getSubvector(row,group)
sumS=sum(subv)
medianS=median(subv)
for j in group:
Mp[i][j]=arcsinh(float(row[j])-medianS/sumS)
return Mp
def transformToRAMSValues(M,G,startRow):
Mp=M[:] #just make a copy first, then replace their values
for i in range(startRow,len(M)):
row=M[i]
for group in G:
subv=getSubvector(row,group)
medianS=median(subv)
sumS=sum(subv)
for j in group:
Mp[i][j]=(float(row[j])-medianS)/sumS
return Mp
def transformToRAstdValues(M,G,startRow):
Mp=M[:] #just make a copy first, then replace their values
for i in range(startRow,len(M)):
row=M[i]
for group in G:
subv=getSubvector(row,group)
meanS=mean(subv)
stdS=std(subv)
for j in group:
Mp[i][j]=(float(row[j])-meanS)/stdS
return Mp
def transformToRA2stdValues(M,G,startRow):
Mp=M[:] #just make a copy first, then replace their values
for i in range(startRow,len(M)):
row=M[i]
for group in G:
subv=getSubvector(row,group)
medianS=median(subv)
stdS=std(subv)
for j in group:
Mp[i][j]=(float(row[j])-medianS)/stdS
return Mp
def sumOfSquares(L):
sos=0.0
for x in L:
sos+=x*x;
return sos
def transformToRA3stdValues(M,G,startRow):
Mp=M[:] #just make a copy first, then replace their values
for i in range(startRow,len(M)):
row=M[i]
for group in G:
subv=getSubvector(row,group)
medianS=median(subv)
#stdS=std(subv)
for k in range(0,len(subv)):
subv[k]=subv[k]-medianS
sos=sumOfSquares(subv)
S=sqrt(1/sos)
for k in range(0,len(subv)):
subv[k]=subv[k]*S
for j,newValue in zip(group,subv):
Mp[i][j]=newValue #median subtraction
#now normalize
return Mp
def RACorrel_inner(Mp,j1,j2,startRow):
N=len(Mp)
sumJ1J2=0
sumJ1=0
sumJ2=0
sumJ1sq=0
sumJ2sq=0
for i in range(startRow,N):
sumJ1J2+=Mp[i][j1]*Mp[i][j2]
sumJ1+=Mp[i][j1]
sumJ2+=Mp[i][j2]
sumJ1sq+=Mp[i][j1]*Mp[i][j1]
sumJ2sq+=Mp[i][j2]*Mp[i][j2]
r=(sumJ1J2-sumJ1*sumJ2/N)/(sqrt(sumJ1sq-(sumJ1*sumJ1)/N)*sqrt(sumJ2sq-(sumJ2*sumJ2)/N))
return r
def distMatrix(C):
#print >> stderr,C
D=[]
for row in C:
Drow=[]
D.append(Drow)
for r in row:
#print >> stderr, r
Drow.append(1.0-r)
return D
def RACorrelation(Mp,colExt):
C=[]
for j1 in colExt:
Crow=[]
C.append(Crow)
for j2 in colExt:
if j1==j2:
Crow.append(1)
elif j1>j2:
Crow.append('na')
else:
#the real calculation is here
Crow.append(RACorrel_inner(Mp,j1,j2,startRow))
for j1 in range(0,len(C)):
for j2 in range(0,len(C)):
if j1>j2:
if C[j1][j2]=='na':
C[j1][j2]=C[j2][j1]
else:
C[j2][j1]=C[j1][j2]
return C
def convertToFloatInPlace(M,groups,startRow):
for i in range(startRow,len(M)):
for group in groups:
for j in group:
#print >> stderr, i,j
M[i][j]=float(M[i][j])
def writeMatrix(stream,M,fs):
for row0 in M:
row=row0[:]
for j in range(0,len(row)):
row[j]=str(row[j])
print >> stream, fs.join(row)
def logtransform(M,logj,logb,startRow):
if len(logj)==0:
return
b=log(logb)
for i in range(startRow,len(M)):
for j in logj:
M[i][j]=log(M[i][j])/b
def normalizeSumOfSquare(M,groups,startRow):
for i in range(startRow,len(M)):
row=M[i]
for group in groups:
subv=getSubvector(row,group)
sos=sumOfSquares(subv)
S=sqrt(1/sos)
for j in group:
M[i][j]=M[i][j]*S
def quantileNorm(M,groups,startRow,method):
Mp=[]
lM=len(M)
for i in range(startRow,lM):
row=M[i]
print >> stderr,"normalizing",(i+1),"of",lM
for group in groups:
subv=getSubvector(row,group)
Mp.append(subv)
quantileNormalizeMInPlace(Mp,method)
for Mpsubv,group in zip(Mp,groups):
for j,newValue in zip(group,Mpsubv):
M[i][j]=newValue
def printUsageAndExit(programName):
print >> stderr, programName,"-g cols -g cols [ -l cols : log transform cols ] [--norm-sos --quantile-norm method='min,max,mean,sum,rank'] ... [ -r output-ra-file-name -m *RA|RAMS|std|std2|std3:using median subtraction ] filename > correlmatrix"
explainColumns(stderr)
exit()
if __name__=='__main__':
programName=argv[0]
startRow=2
headerRow=1
fs="\t"
rafilename=""
mode="RA"
normSOS=False
quantileNormalize=""
colExt=[]
colLogT=[]
logb=2
opts,args=getopt(argv[1:],'g:r:m:l:',['norm-sos','quantile-norm='])
try:
filename,=args
except:
printUsageAndExit(programName)
header,prestarts=getHeader(filename,headerRow,startRow,fs)
groupings=[]
for o,v in opts:
if o=='-g':
cols=getCol0ListFromCol1ListStringAdv(header,v)
groupings.append(cols)
colExt.extend(cols)
elif o=='-l':
cols=getCol0ListFromCol1ListStringAdv(header,v)
colLogT.extend(cols)
elif o=='-r':
rafilename=v
elif o=='-m':
mode=v
elif o=='--norm-sos':
normSOS=True
elif o=='--quantile-norm':
quantileNormalize=v
#now read in file
#read into M
M=[]
fil=open(filename)
for lin in fil:
lin=lin.rstrip()
row=lin.split(fs)
if len(row)<2:
continue
M.append(row)
fil.close()
#now convert relevant col to float
convertToFloatInPlace(M,groupings,startRow-1)
logtransform(M,colLogT,logb,startRow-1)
if mode=="RA":
Mp=transformToRAValues(M,groupings,startRow-1)
elif mode=="RAasinh":
Mp=transformToRAasinhValues(M,groupings,startRow-1)
elif mode=="RAMS":
Mp=transformToRAMSValues(M,groupings,startRow-1)
elif mode=="RAMSasinh":
Mp=transformToRAMSasinhValues(M,groupings,startRow-1)
elif mode=="std":
Mp=transformToRAstdValues(M,groupings,startRow-1)
elif mode=="std2":
Mp=transformToRA2stdValues(M,groupings,startRow-1)
elif mode=="std3":
Mp=transformToRA3stdValues(M,groupings,startRow-1)
else:
print >> stderr,"unknown mode",mode
printUsageAndExit(programName)
if normSOS:
print >> stderr,"normalize sum of squares"
normalizeSumOfSquare(M,groupings,startRow-1)
if quantileNormalize!="":
print >> stderr,"quantile normalize by",quantileNormalize
quantileNorm(M,groupings,startRow-1,quantileNormalize)
if rafilename!="":
print >> stderr,"outputing RA file to",rafilename
fout=open(rafilename,"w")
writeMatrix(fout,Mp,fs)
fout.close()
#now do correlation:
C=RACorrelation(Mp,colExt)
nameExt=getSubvector(header,colExt)
#print >> stderr, C
D=distMatrix(C)
tree=treecluster(distancematrix=D) #no need to transpose
record=Record()
#print >> stderr,C
record.data=C
record.geneid=nameExt
record.genename=nameExt
record.expid=nameExt
record.uniqid="RA"
record.save(filename,expclusters=tree,geneclusters=tree)
#now output correlation
for r in range(0,len(C)):
C[r].insert(0,nameExt[r])
C.insert(0,["Pearson"]+nameExt)
writeMatrix(stdout,C,fs)
#record._savetree(filename,tree,arange,1)
print >> stderr,"<Done>"