-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecter.py
126 lines (106 loc) · 3.46 KB
/
specter.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import numpy as np
from scipy import linalg, diag
from matplotlib import pyplot as plt
from scipy import sparse
import networkx as nx
import pyramid_process
from Bio import PDB
import scipy.spatial.distance
BLOCK = False
def contact_to_distance(M):
N = np.zeros(M.shape)
N[M!=0] = 1/M[M!=0]
s = sparse.csgraph.floyd_warshall(N)
plt.figure()
plt.spy(s, precision=0, markersize=0.00000000000005)
plot_image = plt.imshow(s, vmin=0,vmax=np.percentile(s,99),interpolation='nearest')
plot_image.set_cmap("jet")
plt.show(block=BLOCK)
print s
return s
def distance_to_gram(M):
print M.shape
n, m = M.shape
bary = np.sum(np.triu(M,1))/(n**2)
d = np.sum(M**2,0)/n - bary
G = np.array([[(d[i]+d[j]-M[i][j]**2)/2 for i in range(n)] for j in range(m)])
plt.figure()
plt.spy(G, precision=0, markersize=0.000000000000005)
plot_image = plt.imshow(G, vmin=0,vmax=np.abs(np.percentile(G,99)),interpolation='nearest')
plot_image.set_cmap("jet")
plt.show(block=BLOCK)
print G
return G
def gram_to_coordinates(M, n=None):
if n == None:
n = min(M.shape)
N=M
norm = linalg.norm(M,'fro')
N = M/norm
print N.shape
m = min(N.shape)
eigenValues,eigenVectors = linalg.eigh(N,eigvals=(m-n,m-1))
idx = eigenValues.argsort()[::-1]
L = eigenValues[idx]
E = eigenVectors[:,idx]
print L
V = E*np.sqrt(L)
return V
def contact_to_coordinates(M, n=None):
if n==None:
n = min(M.shape)
return gram_to_coordinates(distance_to_gram(contact_to_distance(M)),n=n)
def remove_disconnected(M):
central_node = np.argmax([np.count_nonzero(r) for r in M])
total_graph = nx.from_numpy_matrix(M)
G = total_graph.subgraph(nx.node_connected_component(total_graph,central_node))
return G
def remove_disconnected_matrix(M):
G = remove_disconnected(M)
s = nx.to_numpy_matrix(G)
return np.array(s)
def coordinates_to_pdb(V,filter=None):
X,Y,Z = tuple(np.split(V,3,1))
f = None
if filter == "cube":
f = (np.mean(X)-2*np.std(X)<X)*(X<np.mean(X)+2*np.std(X))* \
(np.mean(Y)-2*np.std(Y)<Y)*(Y<np.mean(Y)+2*np.std(Y))* \
(np.mean(Z)-2*np.std(Z)<Z)*(Z<np.mean(Z)+2*np.std(Z))
elif filter == "sphere":
f = (X-np.mean(X))**2+(Y-np.mean(Y))**2+(Z-np.mean(Z))**2 \
< np.std(X)**2 + np.std(Y)**2 + np.std(Z)**2
else:
f = abs(X) >= 0.0
Xfiltered = X[f]
Yfiltered = Y[f]
Zfiltered = Z[f]
Xmax = np.max(np.abs(Xfiltered))
Ymax = np.max(np.abs(Yfiltered))
Zmax = np.max(np.abs(Zfiltered))
#
X = Xfiltered*100.0/Xmax
Y = Yfiltered*100.0/Ymax
Z = Zfiltered*100.0/Zmax
X = np.around(X,3)
Y = np.around(Y,3)
Z = np.around(Z,3)
return X,Y,Z
def pdb_to_contact(filename):
return distance_to_contact(pdb_to_distance(filename))
def pdb_to_distance(filename):
p = PDB.PDBParser()
structure = p.get_structure('S', filename)
for chain in structure.get_chains():
atoms = [np.array(atom.get_coord()) for atom in structure.get_atoms()]
print "Atoms retrieved"
D = scipy.spatial.distance.pdist(atoms, 'euclidean')
return scipy.spatial.distance.squareform(D)
def distance_to_contact(D):
m = np.max(1/D[D!=0])
n = min(D.shape)
M = np.zeros(D.shape)
M[D!=0] = 1/D[D!=0]
M[D==0] = m
return M