-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteraction_plot.py
More file actions
210 lines (187 loc) · 6.12 KB
/
Copy pathinteraction_plot.py
File metadata and controls
210 lines (187 loc) · 6.12 KB
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
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 5 15:00:05 2021
@author: ShaharGroup-fyu
"""
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
# Create the networkx graph
#### problem need to be solved: width of the graph
import interaction_strength
##Check this with the latest graphx library
# Create a networkx graph object
def create_network(seq, length, size=10):
'''
:param seq:
:param length:
:param size:
:return:
'''
graphg = nx.MultiDiGraph()
# Add residue node to network graphics
i = 1
while i < (length + 1):
graphg.add_node(i, residue=seq[i - 1], pos=(i, size))
i += 1
return graphg
# To help us identify different amino acid. We color code negative charged residues , positive
# charged residues and aromatic residues with different colors.
def seq_color(seq):
'''
:param seq:
:return:
'''
# Create empty list
negacharged = []
posicharged = []
aromatic = []
# Identify special residues and append into corresponding list
for index, i in enumerate(seq):
if i in ['D', 'E']:
negacharged.append(index)
elif i in ['R', 'K', 'H']:
posicharged.append(index)
elif i in ['F', 'Y', 'W']:
aromatic.append(index)
return negacharged, posicharged, aromatic
# Produce position matrix and layout.
def create_position(graphg):
'''
:param graphg:
:return:
'''
# Get the position and the layout for the networkx nodes
pos = nx.get_node_attributes(graphg, 'pos')
layout = dict((n, graphg.node[n]["pos"]) for n in graphg.nodes())
return pos, layout
# Plot function of sequence color coding
def color_text(pos, index, colorselec, seq, ax):
'''
:param pos:
:param index:
:param colorselec:
:param seq:
:param ax:
:return:
'''
# Get the position of each residue
(x, y) = pos[index + 1]
# Get the name of each residue
label = seq[index] # this makes "1" and 1 labeled the same
# Color coding the sequence
ax.text(
x - 0.2,
y,
label,
color=colorselec,
transform=ax.transData,
clip_on=True, fontfamily='monospace'
)
# Color coding the residue based on the position matrix and sequence color code.
def create_color_coding(seq, graphg, pos, negacharged, posicharged, aromatic, figuresize=(10, 10), nodesize=0.1):
'''
:param seq:
:param graphg:
:param pos:
:param negacharged:
:param posicharged:
:param aromatic:
:param figuresize:
:param nodesize:
:return:
'''
fig = plt.figure(figsize=figuresize)
ax = fig.add_subplot(111)
seqdict = {}
for index, i in enumerate(seq):
seqdict[index + 1] = i
nx.draw(graphg, pos, labels=seqdict, with_labels=False, node_size=nodesize, ax=ax)
# Set residue color based on their types
for index, node in enumerate(graphg):
if index in negacharged:
color_text(pos, index, 'r', seq, ax)
elif index in posicharged:
color_text(pos, index, 'b', seq, ax)
elif index in aromatic:
color_text(pos, index, 'orange', seq, ax)
else:
color_text(pos, index, 'black', seq, ax)
return fig, ax
# Plot selected interaction line
def interaction_plotting(interaction, layout, ax, inter_type):
'''
:param interaction:
:param layout:
:param ax:
:param intertype:
:param plot_c:
:return:
'''
if inter_type == 2:
colorset = 'green'
connect = "arc3,rad=-0.5"
if inter_type == 1:
colorset = 'lightgreen'
connect = "arc3,rad=-0.5"
if inter_type == -2:
colorset = 'red'
connect = "arc3,rad=0.5"
if inter_type == -1:
colorset = 'orange'
connect = "arc3,rad=0.5"
# During the debug process, we only consider the condition when plot_c>0.
if inter_type > 0:
#full_strength, overall_strength = interaction_strength.calculate_overall_strength(interaction, inter_type)
full_strength = []
overall_strength = 1
else:
full_strength = []
overall_strength = -1
for index, data in interaction[interaction['plot_value']==inter_type].iterrows():
strength=data['relative_strength']
distance = data['distance']
r1 = data['r_1']
r2 = data['r_2']
if inter_type > 0:
# linewidth = raw_value_new[index]
linewidth = 2 * data['relative_strength'] * inter_type
distance = data['distance']
else:
# linewidth = -1 * raw_value_new[index]
linewidth = -2 * data['relative_strength'] * inter_type * 0.1
# Adjust location to improve visualization effect
a = layout[r1][0] - 0.2
b = layout[r1][1]
c = layout[r2][0] + 0.2
d = layout[r2][1]
# Plot Interaction between pairs
ax.annotate("",
xy=(a, b),
xytext=(c, d),
arrowprops=dict(arrowstyle="-", color=colorset,
shrinkA=10, shrinkB=10, lw=linewidth,
patchA=None, patchB=None,
connectionstyle=connect,
), )
return overall_strength
def interaction_map(seq, length, interaction, figname):
# Create network object
graphg = create_network(seq, length)
# Obtain position list
(pos, layout) = create_position(graphg)
# Color coding different residues
(negacharged, posicharged, aromatic) = seq_color(seq)
(fig, ax) = create_color_coding(seq, graphg, pos, negacharged, posicharged, aromatic)
# Plot interaction between each residue
att1 = interaction_plotting(interaction, layout, ax, 1)
att2 = interaction_plotting(interaction, layout, ax, 2)
rep1 = interaction_plotting(interaction, layout, ax, -1)
rep2 = interaction_plotting(interaction, layout, ax, -2)
graphg.add_edge(1, 5)
# Save the plot to png file
plt.savefig(figname + '.png')
plt.savefig(figname + '.svg')
# Show the plot
plt.show()
# Return the interaction strength