-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmantelfolder.py
198 lines (132 loc) · 5.86 KB
/
mantelfolder.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
#!/usr/bin/python
import numpy as np
from itertools import permutations
from scipy import spatial, stats
import sys
import re
import glob
import subprocess
digits = re.compile(r'(\d+)')
def tokenize(filename):
return tuple(int(token) if match else token
for token, match in
((fragment, digits.search(fragment))
for fragment in digits.split(filename)))
infolder = sys.argv[1]
g=open(sys.argv[2]+".list",'w')
h=open(sys.argv[2]+".dist",'w')
infiles=glob.glob(infolder+"/*")
infiles.sort(key=tokenize)
#thresh=float(sys.argv[3])
#for 57.17
#from https://github.com/jwcarr/MantelTest/blob/master/Mantel.py
def mantel_test(X, Y, perms=10000, method='pearson', tail='two-tail'):
# Ensure that X and Y are formatted as Numpy arrays.
X, Y = np.asarray(X, dtype=float), np.asarray(Y, dtype=float)
# Check that X and Y are valid distance matrices.
if spatial.distance.is_valid_dm(X) == False and spatial.distance.is_valid_y(X) == False:
raise ValueError('X is not a valid condensed or redundant distance matrix')
if spatial.distance.is_valid_dm(Y) == False and spatial.distance.is_valid_y(Y) == False:
raise ValueError('Y is not a valid condensed or redundant distance matrix')
# If X or Y is a redundant distance matrix, reduce it to a condensed distance matrix.
if len(X.shape) == 2:
X = spatial.distance.squareform(X, force='tovector', checks=False)
if len(Y.shape) == 2:
Y = spatial.distance.squareform(Y, force='tovector', checks=False)
# Check for size equality.
if X.shape[0] != Y.shape[0]:
raise ValueError('X and Y are not of equal size')
# Check for minimum size.
if X.shape[0] < 3:
raise ValueError('X and Y should represent at least 3 objects')
# If Spearman correlation is requested, convert X and Y to ranks.
if method == 'spearman':
X, Y = stats.rankdata(X), stats.rankdata(Y)
# Check for valid method parameter.
elif method != 'pearson':
raise ValueError('The method should be set to "pearson" or "spearman"')
# Check for valid tail parameter.
if tail != 'upper' and tail != 'lower' and tail != 'two-tail':
raise ValueError('The tail should be set to "upper", "lower", or "two-tail"')
X_residuals, Y_residuals = X - X.mean(), Y - Y.mean()
# Expand the Y residuals to a redundant matrix.
Y_residuals_as_matrix = spatial.distance.squareform(Y_residuals, force='tomatrix', checks=False)
# Get the number of objects.
m = Y_residuals_as_matrix.shape[0]
# Calculate the number of possible matrix permutations.
n = np.math.factorial(m)
# Initialize an empty array to store temporary permutations of Y_residuals.
Y_residuals_permuted = np.zeros(Y_residuals.shape[0], dtype=float)
# If the number of requested permutations is greater than the number of
# possible permutations (m!) or the perms parameter is set to 0, then run a
# deterministic Mantel test ...
if perms >= n or perms == 0:
# Initialize an empty array to store the covariances.
covariances = np.zeros(n, dtype=float)
# Enumerate all permutations of row/column orders and iterate over them.
for i, order in enumerate(permutations(range(m))):
# Take a permutation of the matrix.
Y_residuals_as_matrix_permuted = Y_residuals_as_matrix[order, :][:, order]
# Condense the permuted version of the matrix. Rather than use
# distance.squareform(), we call directly into the C wrapper for speed.
spatial.distance._distance_wrap.to_vector_from_squareform_wrap(Y_residuals_as_matrix_permuted, Y_residuals_permuted)
# Compute and store the covariance.
covariances[i] = (X_residuals * Y_residuals_permuted).sum()
# ... otherwise run a stochastic Mantel test.
else:
# Initialize an empty array to store the covariances.
covariances = np.zeros(perms, dtype=float)
# Initialize an array to store the permutation order.
order = np.arange(m)
# Store the veridical covariance in 0th position...
covariances[0] = (X_residuals * Y_residuals).sum()
# ...and then run the random permutations.
for i in range(1, perms):
# Choose a random order in which to permute the rows and columns.
np.random.shuffle(order)
# Take a permutation of the matrix.
Y_residuals_as_matrix_permuted = Y_residuals_as_matrix[order, :][:, order]
# Condense the permuted version of the matrix. Rather than use
# distance.squareform(), we call directly into the C wrapper for speed.
spatial.distance._distance_wrap.to_vector_from_squareform_wrap(Y_residuals_as_matrix_permuted, Y_residuals_permuted)
# Compute and store the covariance.
covariances[i] = (X_residuals * Y_residuals_permuted).sum()
# Calculate the veridical correlation coefficient from the veridical covariance.
r = covariances[0] / np.sqrt((X_residuals ** 2).sum() * (Y_residuals ** 2).sum())
# Calculate the empirical p-value for the upper or lower tail.
if tail == 'upper':
p = (covariances >= covariances[0]).sum() / float(covariances.shape[0])
elif tail == 'lower':
p = (covariances <= covariances[0]).sum() / float(covariances.shape[0])
elif tail == 'two-tail':
p = (abs(covariances) >= abs(covariances[0])).sum() / float(covariances.shape[0])
# Calculate the standard score.
z = (covariances[0] - covariances.mean()) / covariances.std()
return r, p, z
print infiles
g.write("source\ttarget\tR\tP\tZ\n")
tot=len(infiles)
h.write(str(tot)+"\n")
c=-1
for i in infiles:
c=c+1
a=np.loadtxt(i,dtype=float)
a=np.asmatrix(a,dtype=float)
#print a
iname=i.split("/")[-1].split(".")[0]
if len(iname)<10:
h.write(iname+" "*(9-len(iname))+" "*c)
else:
h.write(iname[:8]+" "+" "*c)
for j in infiles[c:]:
jname=j.split("/")[-1].split(".")[0]
b=np.loadtxt(j,dtype=float)
b=np.asmatrix(b,dtype=float)
#print b
x=mantel_test(a,b)
#if float(x[0])>=thresh:
g.write(iname+"\t"+jname+"\t"+str(x[0])+"\t"+str(x[1])+"\t"+str(x[2])+"\n")
print iname,jname,x
dist=1-x[0]
h.write(" "+str(dist))
h.write("\n")